code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig SCREAMING_SNAKE_CASE :str = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE :str = 'RegNetConfig' # Base docstring SCREAMING_SNAKE_CASE :List[str] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Union[str, Any] = [1, 1088, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE :Optional[int] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Any = 'tabby, tabby cat' SCREAMING_SNAKE_CASE :Optional[int] = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : int ,A : int = 3 ,A : int = 1 ,A : int = 1 ,A : Optional[str] = "relu" ,**A : Dict ,): super().__init__(**A ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb __A = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=A ,strides=A ,padding="VALID" ,groups=A ,use_bias=A ,name="convolution" ,) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) __A = ACTaFN[activation] if activation is not None else tf.identity def UpperCamelCase_ ( self : List[Any] ,A : Any ): __A = self.convolution(self.padding(A ) ) __A = self.normalization(A ) __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : RegNetConfig ,**A : str ): super().__init__(**A ) __A = config.num_channels __A = TFRegNetConvLayer( out_channels=config.embedding_size ,kernel_size=3 ,stride=2 ,activation=config.hidden_act ,name="embedder" ,) def UpperCamelCase_ ( self : Tuple ,A : Optional[Any] ): __A = shape_list(A )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( "Make sure that the channel dimension of the pixel values match with the one set in the configuration." ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) __A = tf.transpose(A ,perm=(0, 2, 3, 1) ) __A = self.embedder(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Optional[int] ,A : int ,A : int = 2 ,**A : Tuple ): super().__init__(**A ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=1 ,strides=A ,use_bias=A ,name="convolution" ) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) def UpperCamelCase_ ( self : Union[str, Any] ,A : tf.Tensor ,A : bool = False ): return self.normalization(self.convolution(A ) ,training=A ) class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Dict ,A : int ,A : int ,**A : str ): super().__init__(**A ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) __A = [ tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="relu" ,name="attention.0" ), tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="sigmoid" ,name="attention.2" ), ] def UpperCamelCase_ ( self : Dict ,A : List[Any] ): # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] __A = self.pooler(A ) for layer_module in self.attention: __A = layer_module(A ) __A = hidden_state * pooled return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : Optional[int] ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.2" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : int ,A : Optional[int] ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : str ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetSELayer(A ,reduced_channels=int(round(in_channels / 4 ) ) ,name="layer.2" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.3" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : Dict ,A : Any ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 2 ,A : int = 2 ,**A : Optional[int] ): super().__init__(**A ) __A = TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer __A = [ # downsampling is done in the first layer with stride of 2 layer(A ,A ,A ,stride=A ,name="layers.0" ), *[layer(A ,A ,A ,name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCamelCase_ ( self : Any ,A : List[str] ): for layer_module in self.layers: __A = layer_module(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Any ,A : RegNetConfig ,**A : List[str] ): super().__init__(**A ) __A = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A ,config.embedding_size ,config.hidden_sizes[0] ,stride=2 if config.downsample_in_first_stage else 1 ,depth=config.depths[0] ,name="stages.0" ,) ) __A = zip(config.hidden_sizes ,config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A ,config.depths[1:] ) ): self.stages.append(TFRegNetStage(A ,A ,A ,depth=A ,name=f'''stages.{i+1}''' ) ) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor ,A : bool = False ,A : bool = True ): __A = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: __A = hidden_states + (hidden_state,) __A = stage_module(A ) if output_hidden_states: __A = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A ,hidden_states=A ) @keras_serializable class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' snake_case_ = RegNetConfig def __init__( self : int ,A : Optional[int] ,**A : Dict ): super().__init__(**A ) __A = config __A = TFRegNetEmbeddings(A ,name="embedder" ) __A = TFRegNetEncoder(A ,name="encoder" ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) @unpack_inputs def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : bool = False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.embedder(A ,training=A ) __A = self.encoder( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = encoder_outputs[0] __A = self.pooler(A ) # Change to NCHW output format have uniformity in the modules __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: __A = tuple([tf.transpose(A ,perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A ,pooler_output=A ,hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states ,) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = RegNetConfig snake_case_ = "regnet" snake_case_ = "pixel_values" @property def UpperCamelCase_ ( self : Optional[Any] ): return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24) ,dtype=tf.floataa )} SCREAMING_SNAKE_CASE :Dict = R'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE :Dict = R'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare RegNet model outputting raw features without any specific head on top." , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,*A : List[Any] ,**A : str ): super().__init__(A ,*A ,**A ) __A = TFRegNetMainLayer(A ,name="regnet" ) @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC ,output_type=A ,config_class=_CONFIG_FOR_DOC ,modality="vision" ,expected_output=_EXPECTED_OUTPUT_SHAPE ,) def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : int=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( pixel_values=A ,output_hidden_states=A ,return_dict=A ,training=A ,) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state ,pooler_output=outputs.pooler_output ,hidden_states=outputs.hidden_states ,) @add_start_docstrings( "\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[int] ,A : RegNetConfig ,*A : str ,**A : Tuple ): super().__init__(A ,*A ,**A ) __A = config.num_labels __A = TFRegNetMainLayer(A ,name="regnet" ) # classification head __A = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels ,name="classifier.1" ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT ,output_type=A ,config_class=_CONFIG_FOR_DOC ,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor = None ,A : tf.Tensor = None ,A : bool = None ,A : bool = None ,A : Union[str, Any]=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = outputs.pooler_output if return_dict else outputs[1] __A = self.classifier[0](A ) __A = self.classifier[1](A ) __A = None if labels is None else self.hf_compute_loss(labels=A ,logits=A ) if not return_dict: __A = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A ,logits=A ,hidden_states=outputs.hidden_states )
15
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 SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[Any] = { 'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json', # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "yolos" def __init__( self : Any ,A : Optional[Any]=7_68 ,A : Dict=12 ,A : Any=12 ,A : str=30_72 ,A : Any="gelu" ,A : str=0.0 ,A : List[str]=0.0 ,A : Dict=0.02 ,A : int=1E-12 ,A : Tuple=[5_12, 8_64] ,A : List[Any]=16 ,A : str=3 ,A : str=True ,A : Any=1_00 ,A : Dict=True ,A : Dict=False ,A : Tuple=1 ,A : Union[str, Any]=5 ,A : Optional[Any]=2 ,A : Union[str, Any]=5 ,A : int=2 ,A : int=0.1 ,**A : List[str] ,): super().__init__(**A ) __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = initializer_range __A = layer_norm_eps __A = image_size __A = patch_size __A = num_channels __A = qkv_bias __A = num_detection_tokens __A = use_mid_position_embeddings __A = auxiliary_loss # Hungarian matcher __A = class_cost __A = bbox_cost __A = giou_cost # Loss coefficients __A = bbox_loss_coefficient __A = giou_loss_coefficient __A = eos_coefficient class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = version.parse("1.11" ) @property def UpperCamelCase_ ( self : str ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def UpperCamelCase_ ( self : List[Any] ): return 1E-4 @property def UpperCamelCase_ ( self : Optional[Any] ): return 12
15
1
def UpperCAmelCase ( a_ , a_ ) -> int: """simple docstring""" return int((input_a, input_a).count(0 ) == 0 ) def UpperCAmelCase ( ) -> None: """simple docstring""" assert and_gate(0 , 0 ) == 0 assert and_gate(0 , 1 ) == 0 assert and_gate(1 , 0 ) == 0 assert and_gate(1 , 1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
15
# Copyright 2021 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from packaging import version from .. import __version__ from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD from .doc import ( add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, copy_func, replace_return_docstrings, ) from .generic import ( ContextManagers, ExplicitEnum, ModelOutput, PaddingStrategy, TensorType, add_model_info_to_auto_map, cached_property, can_return_loss, expand_dims, find_labels, flatten_dict, infer_framework, is_jax_tensor, is_numpy_array, is_tensor, is_tf_symbolic_tensor, is_tf_tensor, is_torch_device, is_torch_dtype, is_torch_tensor, reshape, squeeze, strtobool, tensor_size, to_numpy, to_py_obj, transpose, working_or_temp_dir, ) from .hub import ( CLOUDFRONT_DISTRIB_PREFIX, DISABLE_TELEMETRY, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, EntryNotFoundError, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, cached_file, default_cache_path, define_sagemaker_information, download_url, extract_commit_hash, get_cached_models, get_file_from_repo, get_full_repo_name, has_file, http_user_agent, is_offline_mode, is_remote_url, move_cache, send_example_telemetry, try_to_load_from_cache, ) from .import_utils import ( ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, TORCH_FX_REQUIRED_VERSION, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, ccl_version, direct_transformers_import, get_torch_version, is_accelerate_available, is_apex_available, is_bitsandbytes_available, is_bsa_available, is_coloredlogs_available, is_cython_available, is_datasets_available, is_decord_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_jieba_available, is_jumanpp_available, is_kenlm_available, is_keras_nlp_available, is_librosa_available, is_natten_available, is_ninja_available, is_onnx_available, is_openai_available, is_optimum_available, is_pandas_available, is_peft_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytest_available, is_pytorch_quantization_available, is_rjieba_available, is_sacremoses_available, is_safetensors_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_sudachi_available, is_tensorflow_probability_available, is_tensorflow_text_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_bfaa_cpu_available, is_torch_bfaa_gpu_available, is_torch_compile_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_neuroncore_available, is_torch_tensorrt_fx_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_torchdistx_available, is_torchdynamo_available, is_torchvision_available, is_training_run_on_sagemaker, is_vision_available, requires_backends, torch_only_method, ) SCREAMING_SNAKE_CASE :List[str] = 'pytorch_model.bin' SCREAMING_SNAKE_CASE :str = 'pytorch_model.bin.index.json' SCREAMING_SNAKE_CASE :Optional[int] = 'adapter_config.json' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.bin' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.safetensors' SCREAMING_SNAKE_CASE :str = 'tf_model.h5' SCREAMING_SNAKE_CASE :List[Any] = 'tf_model.h5.index.json' SCREAMING_SNAKE_CASE :str = 'model.ckpt' SCREAMING_SNAKE_CASE :List[Any] = 'flax_model.msgpack' SCREAMING_SNAKE_CASE :Optional[int] = 'flax_model.msgpack.index.json' SCREAMING_SNAKE_CASE :Tuple = 'model.safetensors' SCREAMING_SNAKE_CASE :List[Any] = 'model.safetensors.index.json' SCREAMING_SNAKE_CASE :str = 'config.json' SCREAMING_SNAKE_CASE :int = 'preprocessor_config.json' SCREAMING_SNAKE_CASE :Optional[Any] = FEATURE_EXTRACTOR_NAME SCREAMING_SNAKE_CASE :Optional[int] = 'generation_config.json' SCREAMING_SNAKE_CASE :List[str] = 'modelcard.json' SCREAMING_SNAKE_CASE :Optional[int] = '▁' SCREAMING_SNAKE_CASE :Optional[Any] = SENTENCEPIECE_UNDERLINE # Kept for backward compatibility SCREAMING_SNAKE_CASE :str = [ [[0, 1, 0, 1], [1, 0, 0, 1]] ] * 2 # Needs to have 0s and 1s only since XLM uses it for langs too. SCREAMING_SNAKE_CASE :Optional[Any] = [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]] SCREAMING_SNAKE_CASE :List[Any] = [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]] def UpperCAmelCase ( a_ ) -> Dict: """simple docstring""" if version.parse(a_ ) < version.parse(a_ ): if "dev" in min_version: __A = ( "This example requires a source install from HuggingFace Transformers (see " "`https://huggingface.co/docs/transformers/installation#install-from-source`)," ) else: __A = F'''This example requires a minimum version of {min_version},''' error_message += F''' but the version found is {__version__}.\n''' raise ImportError( error_message + "Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other " "versions of HuggingFace Transformers." )
15
1
# Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING, Dict, Optional import numpy as np import pyarrow as pa from .. import config from ..utils.logging import get_logger from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import jax import jaxlib SCREAMING_SNAKE_CASE :Union[str, Any] = get_logger() SCREAMING_SNAKE_CASE :Optional[dict] = None class UpperCAmelCase ( TensorFormatter[Mapping, "jax.Array", Mapping] ): '''simple docstring''' def __init__( self : List[str] ,A : Tuple=None ,A : List[Any]=None ,**A : str ): super().__init__(features=A ) import jax from jaxlib.xla_client import Device if isinstance(A ,A ): raise ValueError( f'''Expected {device} to be a `str` not {type(A )}, as `jaxlib.xla_extension.Device` ''' "is not serializable neither with `pickle` nor with `dill`. Instead you can surround " "the device with `str()` to get its string identifier that will be internally mapped " "to the actual `jaxlib.xla_extension.Device`." ) __A = device if isinstance(A ,A ) else str(jax.devices()[0] ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: __A = self._map_devices_to_str() if self.device not in list(DEVICE_MAPPING.keys() ): logger.warning( f'''Device with string identifier {self.device} not listed among the available ''' f'''devices: {list(DEVICE_MAPPING.keys() )}, so falling back to the default ''' f'''device: {str(jax.devices()[0] )}.''' ) __A = str(jax.devices()[0] ) __A = jnp_array_kwargs @staticmethod def UpperCamelCase_ ( ): import jax return {str(A ): device for device in jax.devices()} def UpperCamelCase_ ( self : Any ,A : Tuple ): import jax import jax.numpy as jnp if isinstance(A ,A ) and column: if all( isinstance(A ,jax.Array ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return jnp.stack(A ,axis=0 ) return column def UpperCamelCase_ ( self : int ,A : int ): import jax import jax.numpy as jnp if isinstance(A ,(str, bytes, type(A )) ): return value elif isinstance(A ,(np.character, np.ndarray) ) and np.issubdtype(value.dtype ,np.character ): return value.tolist() __A = {} if isinstance(A ,(np.number, np.ndarray) ) and np.issubdtype(value.dtype ,np.integer ): # the default int precision depends on the jax config # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision if jax.config.jax_enable_xaa: __A = {"dtype": jnp.intaa} else: __A = {"dtype": jnp.intaa} elif isinstance(A ,(np.number, np.ndarray) ) and np.issubdtype(value.dtype ,np.floating ): __A = {"dtype": jnp.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(A ,PIL.Image.Image ): __A = np.asarray(A ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: __A = self._map_devices_to_str() with jax.default_device(DEVICE_MAPPING[self.device] ): # calling jnp.array on a np.ndarray does copy the data # see https://github.com/google/jax/issues/4486 return jnp.array(A ,**{**default_dtype, **self.jnp_array_kwargs} ) def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, Any] ): import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(A ,torch.Tensor ): return self._tensorize(data_struct.detach().cpu().numpy()[()] ) if hasattr(A ,"__array__" ) and not isinstance(A ,jax.Array ): __A = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(A ,np.ndarray ): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(A ) for substruct in data_struct] ) elif isinstance(A ,(list, tuple) ): return self._consolidate([self.recursive_tensorize(A ) for substruct in data_struct] ) return self._tensorize(A ) def UpperCamelCase_ ( self : Any ,A : dict ): return map_nested(self._recursive_tensorize ,A ,map_list=A ) def UpperCamelCase_ ( self : List[str] ,A : pa.Table ): __A = self.numpy_arrow_extractor().extract_row(A ) __A = self.python_features_decoder.decode_row(A ) return self.recursive_tensorize(A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : pa.Table ): __A = self.numpy_arrow_extractor().extract_column(A ) __A = self.python_features_decoder.decode_column(A ,pa_table.column_names[0] ) __A = self.recursive_tensorize(A ) __A = self._consolidate(A ) return column def UpperCamelCase_ ( self : Dict ,A : pa.Table ): __A = self.numpy_arrow_extractor().extract_batch(A ) __A = self.python_features_decoder.decode_batch(A ) __A = self.recursive_tensorize(A ) for column_name in batch: __A = self._consolidate(batch[column_name] ) return batch
15
def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" __A = [0] * len(a_ ) __A = [] __A = [1] * len(a_ ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(a_ ) ): if indegree[i] == 0: queue.append(a_ ) while queue: __A = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: __A = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(a_ ) print(max(a_ ) ) # Adjacency list of Graph SCREAMING_SNAKE_CASE :List[Any] = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
15
1
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 UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self : Union[str, Any] ): __A = tempfile.mkdtemp() # fmt: off __A = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest"] # fmt: on __A = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file ,"w" ,encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) __A = { "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], } __A = os.path.join(self.tmpdirname ,A ) with open(self.image_processor_file ,"w" ,encoding="utf-8" ) as fp: json.dump(A ,A ) def UpperCamelCase_ ( self : int ,**A : List[Any] ): return BertTokenizer.from_pretrained(self.tmpdirname ,**A ) def UpperCamelCase_ ( self : int ,**A : Optional[Any] ): return ViTImageProcessor.from_pretrained(self.tmpdirname ,**A ) def UpperCamelCase_ ( self : Union[str, Any] ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase_ ( self : Dict ): __A = [np.random.randint(2_55 ,size=(3, 30, 4_00) ,dtype=np.uinta )] __A = [Image.fromarray(np.moveaxis(A ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase_ ( self : Dict ): __A = self.get_tokenizer() __A = self.get_image_processor() __A = VisionTextDualEncoderProcessor(tokenizer=A ,image_processor=A ) processor.save_pretrained(self.tmpdirname ) __A = 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 ,A ) def UpperCamelCase_ ( self : Tuple ): __A = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __A = self.get_tokenizer(bos_token="(BOS)" ,eos_token="(EOS)" ) __A = self.get_image_processor(do_normalize=A ,padding_value=1.0 ) __A = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname ,bos_token="(BOS)" ,eos_token="(EOS)" ,do_normalize=A ,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 ,A ) def UpperCamelCase_ ( self : int ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = VisionTextDualEncoderProcessor(tokenizer=A ,image_processor=A ) __A = self.prepare_image_inputs() __A = image_processor(A ,return_tensors="np" ) __A = processor(images=A ,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 : Optional[int] ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = VisionTextDualEncoderProcessor(tokenizer=A ,image_processor=A ) __A = "lower newer" __A = processor(text=A ) __A = tokenizer(A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCamelCase_ ( self : Any ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = VisionTextDualEncoderProcessor(tokenizer=A ,image_processor=A ) __A = "lower newer" __A = self.prepare_image_inputs() __A = processor(text=A ,images=A ) 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(A ): processor() def UpperCamelCase_ ( self : int ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = VisionTextDualEncoderProcessor(tokenizer=A ,image_processor=A ) __A = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __A = processor.batch_decode(A ) __A = tokenizer.batch_decode(A ) self.assertListEqual(A ,A ) def UpperCamelCase_ ( self : int ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = VisionTextDualEncoderProcessor(tokenizer=A ,image_processor=A ) __A = "lower newer" __A = self.prepare_image_inputs() __A = processor(text=A ,images=A ) self.assertListEqual(list(inputs.keys() ) ,processor.model_input_names )
15
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items() ) def UpperCAmelCase ( a_ , a_ ) -> Tuple: """simple docstring""" __A = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue __A = key.replace("heads.cmd.mim_head.cls.predictions" , "mmm_image_head" ) __A = key.replace("heads.cmd.mlm_head.cls.predictions" , "mmm_text_head" ) __A = key.replace("heads.cmd.itm_head.cls" , "itm_head" ) __A = key.replace("heads.cmd.itm_head.pooler" , "itm_head.pooler" ) __A = key.replace("heads.cmd.clip_head.logit_scale" , "flava.logit_scale" ) __A = key.replace("heads.fairseq_mlm.cls.predictions" , "mlm_head" ) __A = key.replace("heads.imagenet.mim_head.cls.predictions" , "mim_head" ) __A = key.replace("mm_text_projection" , "flava.text_to_mm_projection" ) __A = key.replace("mm_image_projection" , "flava.image_to_mm_projection" ) __A = key.replace("image_encoder.module" , "flava.image_model" ) __A = key.replace("text_encoder.module" , "flava.text_model" ) __A = key.replace("mm_encoder.module.encoder.cls_token" , "flava.multimodal_model.cls_token" ) __A = key.replace("mm_encoder.module" , "flava.multimodal_model" ) __A = key.replace("text_projection" , "flava.text_projection" ) __A = key.replace("image_projection" , "flava.image_projection" ) __A = value.float() for key, value in codebook_state_dict.items(): __A = value return upgrade @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_ , a_=None ) -> Tuple: """simple docstring""" if config_path is not None: __A = FlavaConfig.from_pretrained(a_ ) else: __A = FlavaConfig() __A = FlavaForPreTraining(a_ ).eval() __A = convert_dalle_checkpoint(a_ , a_ , save_checkpoint=a_ ) if os.path.exists(a_ ): __A = torch.load(a_ , map_location="cpu" ) else: __A = torch.hub.load_state_dict_from_url(a_ , map_location="cpu" ) __A = upgrade_state_dict(a_ , a_ ) hf_model.load_state_dict(a_ ) __A = hf_model.state_dict() __A = count_parameters(a_ ) __A = count_parameters(a_ ) + count_parameters(a_ ) assert torch.allclose(a_ , a_ , atol=1E-3 ) hf_model.save_pretrained(a_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Any = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to flava checkpoint') parser.add_argument('--codebook_path', default=None, type=str, help='Path to flava codebook checkpoint') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
15
1
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) SCREAMING_SNAKE_CASE :Union[str, Any] = logging.getLogger(__name__) SCREAMING_SNAKE_CASE :int = 'Hello world! cécé herlolip' SCREAMING_SNAKE_CASE :Any = 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 UpperCAmelCase ( a_ , a_ ) -> Dict: """simple docstring""" __A = BertAbsConfig( temp_dir="." , finetune_bert=a_ , large=a_ , share_emb=a_ , use_bert_emb=a_ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) __A = torch.load(a_ , lambda a_ , a_ : storage ) __A = AbsSummarizer(a_ , torch.device("cpu" ) , a_ ) original.eval() __A = BertAbsSummarizer(a_ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) __A = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs __A = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(a_ )) ) __A = torch.tensor(a_ ).unsqueeze(0 ) __A = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(a_ )) ) __A = torch.tensor(a_ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass __A = encoder_input_ids __A = decoder_input_ids __A = __A = None __A = None __A = __A = None __A = __A = None __A = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical __A = original(a_ , a_ , a_ , a_ , a_ , a_ , a_ )[0] __A = original.generator(a_ ) __A = new_model( a_ , a_ , a_ , a_ , a_ )[0] __A = new_model.generator(a_ ) __A = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(a_ ) ) __A = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(a_ ) ) __A = torch.allclose(a_ , a_ , atol=1E-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :int = 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.', ) SCREAMING_SNAKE_CASE :List[str] = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
15
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Optional[int] = {'vocab_file': 'sentencepiece.bpe.model'} SCREAMING_SNAKE_CASE :Tuple = { 'vocab_file': { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model', } } SCREAMING_SNAKE_CASE :List[Any] = { 'camembert-base': 512, } SCREAMING_SNAKE_CASE :List[str] = '▁' class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[Any] ,A : List[str] ,A : List[Any]="<s>" ,A : Tuple="</s>" ,A : Any="</s>" ,A : Optional[Any]="<s>" ,A : Tuple="<unk>" ,A : str="<pad>" ,A : int="<mask>" ,A : Optional[int]=["<s>NOTUSED", "</s>NOTUSED"] ,A : Optional[Dict[str, Any]] = None ,**A : Optional[Any] ,): # Mask token behave like a normal word, i.e. include the space before it __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,sep_token=A ,cls_token=A ,pad_token=A ,mask_token=A ,additional_special_tokens=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(A ) ) __A = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __A = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __A = len(self.fairseq_tokens_to_ids ) __A = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __A = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCamelCase_ ( self : int ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def UpperCamelCase_ ( self : Union[str, Any] ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase_ ( self : Dict ): return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCamelCase_ ( self : int ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Dict ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(A ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : Tuple ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCamelCase_ ( self : Optional[Any] ,A : Dict ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def __getstate__( self : Dict ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Union[str, Any] ,A : Any ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
15
1
import unittest from transformers import SqueezeBertConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[int] ,A : Optional[Any] ,A : Optional[int]=13 ,A : List[str]=7 ,A : List[str]=True ,A : str=True ,A : int=False ,A : List[Any]=True ,A : Tuple=99 ,A : Optional[int]=32 ,A : Optional[Any]=5 ,A : List[Any]=4 ,A : Optional[Any]=64 ,A : List[Any]="gelu" ,A : List[Any]=0.1 ,A : Union[str, Any]=0.1 ,A : Union[str, Any]=5_12 ,A : Union[str, Any]=16 ,A : Optional[Any]=2 ,A : str=0.02 ,A : int=3 ,A : List[str]=4 ,A : List[str]=None ,A : Union[str, Any]=2 ,A : str=2 ,A : Dict=2 ,A : Optional[Any]=2 ,A : Any=4 ,A : Optional[Any]=1 ,): __A = parent __A = batch_size __A = seq_length __A = is_training __A = use_input_mask __A = use_token_type_ids __A = use_labels __A = vocab_size __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = type_vocab_size __A = type_sequence_label_size __A = initializer_range __A = num_labels __A = num_choices __A = scope __A = q_groups __A = k_groups __A = v_groups __A = post_attention_groups __A = intermediate_groups __A = output_groups def UpperCamelCase_ ( self : List[str] ): __A = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) __A = None if self.use_input_mask: __A = random_attention_mask([self.batch_size, self.seq_length] ) __A = None __A = None __A = None if self.use_labels: __A = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) __A = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) __A = ids_tensor([self.batch_size] ,self.num_choices ) __A = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase_ ( self : str ): return SqueezeBertConfig( embedding_size=self.hidden_size ,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 ,attention_probs_dropout_prob=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,q_groups=self.q_groups ,k_groups=self.k_groups ,v_groups=self.v_groups ,post_attention_groups=self.post_attention_groups ,intermediate_groups=self.intermediate_groups ,output_groups=self.output_groups ,) def UpperCamelCase_ ( self : int ,A : List[str] ,A : Any ,A : int ,A : str ,A : int ,A : str ): __A = SqueezeBertModel(config=A ) model.to(A ) model.eval() __A = model(A ,A ) __A = model(A ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase_ ( self : str ,A : Any ,A : Any ,A : Optional[int] ,A : int ,A : int ,A : Union[str, Any] ): __A = SqueezeBertForMaskedLM(config=A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase_ ( self : List[str] ,A : Optional[int] ,A : List[Any] ,A : Union[str, Any] ,A : Optional[Any] ,A : Dict ,A : int ): __A = SqueezeBertForQuestionAnswering(config=A ) model.to(A ) model.eval() __A = model( A ,attention_mask=A ,start_positions=A ,end_positions=A ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def UpperCamelCase_ ( self : Optional[int] ,A : Optional[Any] ,A : List[Any] ,A : Tuple ,A : Dict ,A : Optional[int] ,A : Dict ): __A = self.num_labels __A = SqueezeBertForSequenceClassification(A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def UpperCamelCase_ ( self : Dict ,A : Tuple ,A : int ,A : Any ,A : Union[str, Any] ,A : Union[str, Any] ,A : Any ): __A = self.num_labels __A = SqueezeBertForTokenClassification(config=A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase_ ( self : Tuple ,A : Optional[Any] ,A : int ,A : Optional[Any] ,A : Tuple ,A : List[Any] ,A : Tuple ): __A = self.num_choices __A = SqueezeBertForMultipleChoice(config=A ) model.to(A ) model.eval() __A = input_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() __A = input_mask.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() __A = model( A ,attention_mask=A ,labels=A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def UpperCamelCase_ ( self : Any ): __A = self.prepare_config_and_inputs() ((__A) , (__A) , (__A) , (__A) , (__A) , (__A)) = config_and_inputs __A = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = ( ( SqueezeBertModel, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, ) if is_torch_available() else None ) snake_case_ = ( { "feature-extraction": SqueezeBertModel, "fill-mask": SqueezeBertForMaskedLM, "question-answering": SqueezeBertForQuestionAnswering, "text-classification": SqueezeBertForSequenceClassification, "token-classification": SqueezeBertForTokenClassification, "zero-shot": SqueezeBertForSequenceClassification, } if is_torch_available() else {} ) snake_case_ = False snake_case_ = True snake_case_ = False def UpperCamelCase_ ( self : str ): __A = SqueezeBertModelTester(self ) __A = ConfigTester(self ,config_class=A ,dim=37 ) def UpperCamelCase_ ( self : Union[str, Any] ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : Dict ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_model(*A ) def UpperCamelCase_ ( self : Optional[Any] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_masked_lm(*A ) def UpperCamelCase_ ( self : int ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_question_answering(*A ) def UpperCamelCase_ ( self : Dict ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_sequence_classification(*A ) def UpperCamelCase_ ( self : Optional[int] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_token_classification(*A ) def UpperCamelCase_ ( self : List[str] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_multiple_choice(*A ) @slow def UpperCamelCase_ ( self : List[str] ): for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __A = SqueezeBertModel.from_pretrained(A ) self.assertIsNotNone(A ) @require_sentencepiece @require_tokenizers @require_torch class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def UpperCamelCase_ ( self : int ): __A = SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli" ) __A = torch.tensor([[1, 2_94_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 13, 15_88, 2]] ) __A = model(A )[0] __A = torch.Size((1, 3) ) self.assertEqual(output.shape ,A ) __A = torch.tensor([[0.64_01, -0.03_49, -0.60_41]] ) self.assertTrue(torch.allclose(A ,A ,atol=1E-4 ) )
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return [tuple(a_ )] __A = [] def generate(a_ , a_ ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , a_ ) for i in range(k - 1 ): if k % 2 == 0: # k is even __A , __A = arr[k - 1], arr[i] else: # k is odd __A , __A = arr[k - 1], arr[0] generate(k - 1 , a_ ) generate(len(a_ ) , a_ ) return res if __name__ == "__main__": SCREAMING_SNAKE_CASE :int = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :Dict = [int(item) for item in user_input.split(',')] print(heaps(arr))
15
1
import argparse import os import torch from transformers.utils import WEIGHTS_NAME SCREAMING_SNAKE_CASE :Optional[Any] = ['small', 'medium', 'large'] SCREAMING_SNAKE_CASE :Any = 'lm_head.decoder.weight' SCREAMING_SNAKE_CASE :Any = 'lm_head.weight' def UpperCAmelCase ( a_ , a_ ) -> Tuple: """simple docstring""" __A = torch.load(a_ ) __A = d.pop(a_ ) os.makedirs(a_ , exist_ok=a_ ) torch.save(a_ , os.path.join(a_ , a_ ) ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Optional[Any] = argparse.ArgumentParser() parser.add_argument('--dialogpt_path', default='.', type=str) SCREAMING_SNAKE_CASE :Any = parser.parse_args() for MODEL in DIALOGPT_MODELS: SCREAMING_SNAKE_CASE :int = os.path.join(args.dialogpt_path, f'''{MODEL}_ft.pkl''') SCREAMING_SNAKE_CASE :str = f'''./DialoGPT-{MODEL}''' convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return lst __A = 1 while i < len(a_ ): if lst[i - 1] <= lst[i]: i += 1 else: __A , __A = lst[i], lst[i - 1] i -= 1 if i == 0: __A = 1 return lst if __name__ == "__main__": SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :List[Any] = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
15
1
import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging SCREAMING_SNAKE_CASE :str = logging.get_logger(__name__) def UpperCAmelCase ( a_ , a_ , a_ ) -> None: """simple docstring""" __A = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(a_ ) == len(a_ ), F'''{len(a_ )} != {len(a_ )}''' dest_layers.load_state_dict(layers_to_copy.state_dict() ) SCREAMING_SNAKE_CASE :Tuple = { # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 12: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 11], 4: [0, 4, 8, 11], 6: [0, 2, 4, 7, 9, 11], 9: [0, 1, 2, 4, 5, 7, 9, 10, 11], 12: list(range(12)), }, 16: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 15], 3: [0, 8, 15], 4: [0, 5, 10, 15], 6: [0, 3, 6, 9, 12, 15], 8: [0, 2, 4, 6, 8, 10, 12, 15], 9: [0, 1, 3, 5, 7, 9, 11, 13, 15], 12: [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 15], 16: list(range(16)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } SCREAMING_SNAKE_CASE :List[Any] = { # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 12: {1: [11], 2: [5, 11], 3: [3, 7, 11], 6: [1, 3, 5, 8, 10, 11]}, 16: {1: [15], 4: [4, 9, 12, 15], 8: [1, 3, 5, 7, 9, 11, 13, 15]}, } def UpperCAmelCase ( a_ , a_ ) -> str: """simple docstring""" try: __A = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( F'''no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first''' F''' {n_student}''' ) return list(range(a_ ) ) def UpperCAmelCase ( a_ , a_ ) -> List[int]: """simple docstring""" if n_student > n_teacher: raise ValueError(F'''Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}''' ) elif n_teacher == n_student: return list(range(a_ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def UpperCAmelCase ( a_ , a_ = "student" , a_ = None , a_ = None , a_=False , a_=None , a_=None , **a_ , ) -> Tuple[PreTrainedModel, List[int], List[int]]: """simple docstring""" __A = "encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher." assert (e is not None) or (d is not None), _msg if isinstance(a_ , a_ ): AutoTokenizer.from_pretrained(a_ ).save_pretrained(a_ ) # purely for convenience __A = AutoModelForSeqaSeqLM.from_pretrained(a_ ).eval() else: assert isinstance(a_ , a_ ), F'''teacher must be a model or string got type {type(a_ )}''' __A = teacher.config.to_diff_dict() try: __A , __A = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: __A = teacher_e if d is None: __A = teacher_d init_kwargs.update({"encoder_layers": e, "decoder_layers": d} ) except AttributeError: # T5 if hasattr(teacher.config , "num_encoder_layers" ): __A , __A = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: __A , __A = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: __A = teacher_e if d is None: __A = teacher_d if hasattr(teacher.config , "num_encoder_layers" ): init_kwargs.update({"num_encoder_layers": e, "num_decoder_layers": d} ) else: init_kwargs.update({"num_layers": e, "num_decoder_layers": d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(a_ ) # Copy weights __A = teacher.config_class(**a_ ) __A = AutoModelForSeqaSeqLM.from_config(a_ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. __A = student.load_state_dict(teacher.state_dict() , strict=a_ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save __A , __A = list(range(a_ ) ), list(range(a_ ) ) logger.info( F'''Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to''' F''' {save_path}''' ) student.save_pretrained(a_ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: __A = pick_layers_to_copy(a_ , a_ ) if d_layers_to_copy is None: __A = pick_layers_to_copy(a_ , a_ ) try: if hasattr( a_ , "prophetnet" ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , a_ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , a_ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , a_ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , a_ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , a_ ) copy_layers(teacher.decoder.block , student.decoder.block , a_ ) logger.info( F'''Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}''' ) __A = { "teacher_type": teacher.config.model_type, "copied_encoder_layers": e_layers_to_copy, "copied_decoder_layers": d_layers_to_copy, } student.save_pretrained(a_ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
15
from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 snake_case_ = 42 snake_case_ = None class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 2 @register_to_config def __init__( self : str ,A : float = 0.02 ,A : float = 1_00 ,A : float = 1.0_07 ,A : float = 80 ,A : float = 0.05 ,A : float = 50 ,): # standard deviation of the initial noise distribution __A = sigma_max # setable values __A = None __A = None __A = None # sigma(t_i) def UpperCamelCase_ ( self : str ,A : torch.FloatTensor ,A : Optional[int] = None ): return sample def UpperCamelCase_ ( self : Dict ,A : int ,A : Union[str, torch.device] = None ): __A = num_inference_steps __A = np.arange(0 ,self.num_inference_steps )[::-1].copy() __A = torch.from_numpy(A ).to(A ) __A = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] __A = torch.tensor(A ,dtype=torch.floataa ,device=A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : torch.FloatTensor ,A : float ,A : Optional[torch.Generator] = None ): if self.config.s_min <= sigma <= self.config.s_max: __A = min(self.config.s_churn / self.num_inference_steps ,2**0.5 - 1 ) else: __A = 0 # sample eps ~ N(0, S_noise^2 * I) __A = self.config.s_noise * randn_tensor(sample.shape ,generator=A ).to(sample.device ) __A = sigma + gamma * sigma __A = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def UpperCamelCase_ ( self : Dict ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_hat + sigma_hat * model_output __A = (sample_hat - pred_original_sample) / sigma_hat __A = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : Optional[int] ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_prev + sigma_prev * model_output __A = (sample_prev - pred_original_sample) / sigma_prev __A = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : List[Any] ,A : Dict ,A : List[str] ,A : str ): raise NotImplementedError()
15
1
from __future__ import annotations from collections.abc import Generator def UpperCAmelCase ( ) -> Generator[int, None, None]: """simple docstring""" __A = {} __A = 2 while True: __A = factor_map.pop(a_ , a_ ) if factor: __A = factor + prime while x in factor_map: x += factor __A = factor else: __A = prime yield prime prime += 1 def UpperCAmelCase ( a_ = 1E10 ) -> int: """simple docstring""" __A = sieve() __A = 1 while True: __A = next(a_ ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(a_ ) n += 2 if __name__ == "__main__": print(solution())
15
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version SCREAMING_SNAKE_CASE :Union[str, Any] = get_logger(__name__) class UpperCAmelCase : '''simple docstring''' snake_case_ = "dummy_data" snake_case_ = "datasets" snake_case_ = False def __init__( self : Optional[int] ,A : str ,A : str ,A : Union[Version, str] ,A : Optional[str] = None ,A : bool = False ,A : bool = True ,A : Optional[List[Callable]] = None ,): __A = 0 __A = dataset_name __A = cache_dir __A = use_local_dummy_data __A = config # download_callbacks take a single url as input __A = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root __A = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general __A = str(A ) # to be downloaded __A = None __A = None @property def UpperCamelCase_ ( self : Union[str, Any] ): if self._dummy_file is None: __A = self.download_dummy_data() return self._dummy_file @property def UpperCamelCase_ ( self : Optional[Any] ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("dummy" ,self.config.name ,self.version_name ) # structure is dummy / version_name return os.path.join("dummy" ,self.version_name ) @property def UpperCamelCase_ ( self : List[Any] ): return os.path.join(self.dummy_data_folder ,"dummy_data.zip" ) def UpperCamelCase_ ( self : Tuple ): __A = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) __A = cached_path( A ,cache_dir=self.cache_dir ,extract_compressed_file=A ,force_extract=A ) return os.path.join(A ,self.dummy_file_name ) @property def UpperCamelCase_ ( self : str ): return os.path.join(self.datasets_scripts_dir ,self.dataset_name ,self.dummy_zip_file ) @property def UpperCamelCase_ ( self : Any ): if self._bucket_url is None: __A = hf_github_url(self.dataset_name ,self.dummy_zip_file.replace(os.sep ,"/" ) ) return self._bucket_url @property def UpperCamelCase_ ( self : Tuple ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep ,"/" ).split("/" )[:-1] ) def UpperCamelCase_ ( self : List[str] ,A : List[Any] ,*A : Dict ): if self.load_existing_dummy_data: # dummy data is downloaded and tested __A = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned __A = self.dummy_file_name # special case when data_url is a dict if isinstance(A ,A ): return self.create_dummy_data_dict(A ,A ) elif isinstance(A ,(list, tuple) ): return self.create_dummy_data_list(A ,A ) else: return self.create_dummy_data_single(A ,A ) def UpperCamelCase_ ( self : str ,A : List[Any] ,*A : List[Any] ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : List[str] ,A : List[str] ,A : Tuple ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : Any ,A : Any ,*A : Optional[Any] ,**A : List[str] ): return path def UpperCamelCase_ ( self : str ): return {} def UpperCamelCase_ ( self : int ,A : int ,A : Tuple ): __A = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(A ,A ): for single_url in single_urls: download_callback(A ) else: __A = single_urls download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(A ,A ): __A = [os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) for x in single_urls] else: __A = single_urls __A = os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) __A = value # make sure that values are unique if all(isinstance(A ,A ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique __A = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,A : str ): __A = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one __A = all(bool(re.findall("[0-9]{3,}-of-[0-9]{3,}" ,A ) ) for url in data_url ) __A = all( url.startswith("https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): __A = [data_url[0]] * len(A ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(single_url.split("/" )[-1] ) ) dummy_data_list.append(A ) return dummy_data_list def UpperCamelCase_ ( self : str ,A : List[Any] ,A : Optional[Any] ): for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(data_url.split("/" )[-1] ) ) if os.path.exists(A ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def UpperCamelCase_ ( self : int ): pass def UpperCamelCase_ ( self : Dict ): pass def UpperCamelCase_ ( self : Optional[Any] ,A : List[Any] ): def _iter_archive_members(A : Optional[Any] ): # this preserves the order of the members inside the ZIP archive __A = Path(self.dummy_file ).parent __A = path.relative_to(A ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: __A = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(A ) __A = Path(A ) __A = _iter_archive_members(A ) if self.use_local_dummy_data else path.rglob("*" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((".", "__") ): yield file_path.relative_to(A ).as_posix(), file_path.open("rb" ) def UpperCamelCase_ ( self : List[Any] ,A : Any ): if not isinstance(A ,A ): __A = [paths] for path in paths: if os.path.isfile(A ): if os.path.basename(A ).startswith((".", "__") ): return yield path else: for dirpath, dirnames, filenames in os.walk(A ): if os.path.basename(A ).startswith((".", "__") ): continue dirnames.sort() for filename in sorted(A ): if filename.startswith((".", "__") ): continue yield os.path.join(A ,A )
15
1
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self : Dict ): __A = tempfile.mkdtemp() __A = BlipImageProcessor() __A = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel" ) __A = BlipProcessor(A ,A ) processor.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self : Union[str, Any] ,**A : int ): return AutoProcessor.from_pretrained(self.tmpdirname ,**A ).tokenizer def UpperCamelCase_ ( self : List[str] ,**A : Optional[Any] ): return AutoProcessor.from_pretrained(self.tmpdirname ,**A ).image_processor def UpperCamelCase_ ( self : Optional[int] ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase_ ( self : int ): __A = [np.random.randint(2_55 ,size=(3, 30, 4_00) ,dtype=np.uinta )] __A = [Image.fromarray(np.moveaxis(A ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase_ ( self : List[Any] ): __A = BlipProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __A = self.get_tokenizer(bos_token="(BOS)" ,eos_token="(EOS)" ) __A = self.get_image_processor(do_normalize=A ,padding_value=1.0 ) __A = BlipProcessor.from_pretrained( self.tmpdirname ,bos_token="(BOS)" ,eos_token="(EOS)" ,do_normalize=A ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,A ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,A ) def UpperCamelCase_ ( self : List[Any] ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = BlipProcessor(tokenizer=A ,image_processor=A ) __A = self.prepare_image_inputs() __A = image_processor(A ,return_tensors="np" ) __A = processor(images=A ,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 : Optional[int] ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = BlipProcessor(tokenizer=A ,image_processor=A ) __A = "lower newer" __A = processor(text=A ) __A = tokenizer(A ,return_token_type_ids=A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCamelCase_ ( self : List[Any] ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = BlipProcessor(tokenizer=A ,image_processor=A ) __A = "lower newer" __A = self.prepare_image_inputs() __A = processor(text=A ,images=A ) self.assertListEqual(list(inputs.keys() ) ,["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(A ): processor() def UpperCamelCase_ ( self : Optional[int] ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = BlipProcessor(tokenizer=A ,image_processor=A ) __A = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __A = processor.batch_decode(A ) __A = tokenizer.batch_decode(A ) self.assertListEqual(A ,A ) def UpperCamelCase_ ( self : Union[str, Any] ): __A = self.get_image_processor() __A = self.get_tokenizer() __A = BlipProcessor(tokenizer=A ,image_processor=A ) __A = "lower newer" __A = self.prepare_image_inputs() __A = processor(text=A ,images=A ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) ,["pixel_values", "input_ids", "attention_mask"] )
15
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available SCREAMING_SNAKE_CASE :List[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :List[str] = ['BartphoTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys SCREAMING_SNAKE_CASE :Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
15
1
SCREAMING_SNAKE_CASE :Tuple = {str(digit): digit**5 for digit in range(10)} def UpperCAmelCase ( a_ ) -> int: """simple docstring""" return sum(DIGITS_FIFTH_POWER[digit] for digit in str(a_ ) ) def UpperCAmelCase ( ) -> int: """simple docstring""" return sum( number for number in range(1_0_0_0 , 1_0_0_0_0_0_0 ) if number == digits_fifth_powers_sum(a_ ) ) if __name__ == "__main__": print(solution())
15
from typing import Dict, Optional import numpy as np import datasets SCREAMING_SNAKE_CASE :List[Any] = '\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n' SCREAMING_SNAKE_CASE :List[str] = '\nArgs:\n predictions (`List[ndarray]`):\n List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n references (`List[ndarray]`):\n List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n num_labels (`int`):\n Number of classes (categories).\n ignore_index (`int`):\n Index that will be ignored during evaluation.\n nan_to_num (`int`, *optional*):\n If specified, NaN values will be replaced by the number defined by the user.\n label_map (`dict`, *optional*):\n If specified, dictionary mapping old label indices to new label indices.\n reduce_labels (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n `Dict[str, float | ndarray]` comprising various elements:\n - *mean_iou* (`float`):\n Mean Intersection-over-Union (IoU averaged over all categories).\n - *mean_accuracy* (`float`):\n Mean accuracy (averaged over all categories).\n - *overall_accuracy* (`float`):\n Overall accuracy on all images.\n - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n Per category accuracy.\n - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n Per category IoU.\n\nExamples:\n\n >>> import numpy as np\n\n >>> mean_iou = datasets.load_metric("mean_iou")\n\n >>> # suppose one has 3 different segmentation maps predicted\n >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n >>> predicted = [predicted_1, predicted_2, predicted_3]\n >>> ground_truth = [actual_1, actual_2, actual_3]\n\n >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), \'per_category_accuracy\': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])}\n' SCREAMING_SNAKE_CASE :str = '\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}' def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Tuple: """simple docstring""" if label_map is not None: for old_id, new_id in label_map.items(): __A = new_id # turn into Numpy arrays __A = np.array(a_ ) __A = np.array(a_ ) if reduce_labels: __A = 2_5_5 __A = label - 1 __A = 2_5_5 __A = label != ignore_index __A = np.not_equal(a_ , a_ ) __A = pred_label[mask] __A = np.array(a_ )[mask] __A = pred_label[pred_label == label] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Union[str, Any]: """simple docstring""" __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(a_ , a_ ): __A , __A , __A , __A = intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = None , a_ = False , ) -> str: """simple docstring""" __A , __A , __A , __A = total_intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) # compute metrics __A = {} __A = total_area_intersect.sum() / total_area_label.sum() __A = total_area_intersect / total_area_union __A = total_area_intersect / total_area_label __A = np.nanmean(a_ ) __A = np.nanmean(a_ ) __A = all_acc __A = iou __A = acc if nan_to_num is not None: __A = {metric: np.nan_to_num(a_ , nan=a_ ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { "predictions": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), "references": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), } ) ,reference_urls=[ "https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py" ] ,) def UpperCamelCase_ ( self : int ,A : Optional[Any] ,A : Optional[Any] ,A : int ,A : bool ,A : Optional[int] = None ,A : Optional[Dict[int, int]] = None ,A : bool = False ,): __A = mean_iou( results=A ,gt_seg_maps=A ,num_labels=A ,ignore_index=A ,nan_to_num=A ,label_map=A ,reduce_labels=A ,) return iou_result
15
1
from __future__ import annotations import math from collections.abc import Callable def UpperCAmelCase ( a_ , a_ , a_ , a_ = 1_0_0 , ) -> float: """simple docstring""" __A = x_start __A = fnc(a_ ) __A = 0.0 for _ in range(a_ ): # Approximates curve as a sequence of linear lines and sums their length __A = (x_end - x_start) / steps + xa __A = fnc(a_ ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step __A = xa __A = fxa return length if __name__ == "__main__": def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" return math.sin(1_0 * x ) print('f(x) = sin(10 * x)') print('The length of the curve from x = -10 to x = 10 is:') SCREAMING_SNAKE_CASE :Tuple = 10 while i <= 10_0000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
15
import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE :List[str] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[str] = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Dict = { 'vocab_file': { 'AI-Sweden/gpt-sw3-126m': 'https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-350m': 'https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-1.6b': 'https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-6.7b': 'https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-20b': 'https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model', } } SCREAMING_SNAKE_CASE :Optional[Any] = { 'AI-Sweden/gpt-sw3-126m': 2048, 'AI-Sweden/gpt-sw3-350m': 2048, 'AI-Sweden/gpt-sw3-1.6b': 2048, 'AI-Sweden/gpt-sw3-6.7b': 2048, 'AI-Sweden/gpt-sw3-20b': 2048, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[int] ,A : Optional[Any] ,A : Optional[int]=False ,A : int=False ,A : Union[str, Any]=False ,A : int=None ,A : Optional[Any]=None ,A : Union[str, Any]=None ,A : Optional[Any]=None ,A : Optional[Dict[str, Any]] = None ,**A : Tuple ,): __A = {} if sp_model_kwargs is None else sp_model_kwargs __A = kwargs.get("name_or_path" ) if name_or_path is None: logger.warning( "name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b," " you are testing the model, this can safely be ignored" ) __A = "None" # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing __A = "<|endoftext|>" if eos_token is None else eos_token __A = "<unk>" if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: __A = unk_token if pad_token is None else pad_token __A = eos_token if bos_token is None else bos_token else: __A = "<pad>" if pad_token is None else pad_token __A = "<s>" if bos_token is None else bos_token super().__init__( do_lower_case=A ,remove_space=A ,keep_accents=A ,bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = do_lower_case __A = remove_space __A = keep_accents __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) # Used for whitespace normalization in input texts # fmt : off __A = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", "„"} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing __A = re.compile( f'''[{''.join(map(A ,list(range(0 ,9 ) ) + list(range(11 ,32 ) ) + list(range(1_27 ,1_60 ) ) + [1_60, 1_73, 82_03] ) )}]''' ) def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Optional[Any] ,A : Union[str, Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def UpperCamelCase_ ( self : List[str] ): return len(self.sp_model ) def UpperCamelCase_ ( self : int ,A : str ): __A = self.non_printing_characters_re.sub("" ,A ) # Normalize whitespaces __A = "".join([char if char not in self.whitespaces else " " for char in text] ) # NFC Unicode normalization __A = unicodedata.normalize("NFC" ,A ) return text def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,**A : Optional[int] ): __A = self.preprocess_text(A ) return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : int ): return self.sp_model.IdToPiece(A ) @staticmethod def UpperCamelCase_ ( A : str ): return out_string def UpperCamelCase_ ( self : str ,A : List[str] ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string def UpperCamelCase_ ( self : str ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : List[str] ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, List[str]] ,A : Union[str, bool] = False ): if isinstance(A ,A ): __A = self.preprocess_text(A ) __A = self.sp_model.encode(A ) else: __A = [self.preprocess_text(A ) for t in text] __A = self.sp_model.encode(A ) if return_tensors is True or return_tensors == "pt": __A = torch.tensor(A ) return token_ids def UpperCamelCase_ ( self : List[Any] ,A : Union[int, List[int]] ): return self.sp_model.decode(A ) def UpperCamelCase_ ( self : List[str] ,A : "Conversation" ): __A = [f'''User: {text}''' if is_user else f'''Bot: {text}''' for is_user, text in conversation.iter_texts()] __A = ( f'''{self.eos_token}{self.bos_token}''' + f'''{self.bos_token}'''.join(A ) + f'''{self.bos_token}Bot:''' ) return self.encode(text=A )
15
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[Any] = { 'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json', # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "yolos" def __init__( self : Any ,A : Optional[Any]=7_68 ,A : Dict=12 ,A : Any=12 ,A : str=30_72 ,A : Any="gelu" ,A : str=0.0 ,A : List[str]=0.0 ,A : Dict=0.02 ,A : int=1E-12 ,A : Tuple=[5_12, 8_64] ,A : List[Any]=16 ,A : str=3 ,A : str=True ,A : Any=1_00 ,A : Dict=True ,A : Dict=False ,A : Tuple=1 ,A : Union[str, Any]=5 ,A : Optional[Any]=2 ,A : Union[str, Any]=5 ,A : int=2 ,A : int=0.1 ,**A : List[str] ,): super().__init__(**A ) __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = initializer_range __A = layer_norm_eps __A = image_size __A = patch_size __A = num_channels __A = qkv_bias __A = num_detection_tokens __A = use_mid_position_embeddings __A = auxiliary_loss # Hungarian matcher __A = class_cost __A = bbox_cost __A = giou_cost # Loss coefficients __A = bbox_loss_coefficient __A = giou_loss_coefficient __A = eos_coefficient class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = version.parse("1.11" ) @property def UpperCamelCase_ ( self : str ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def UpperCamelCase_ ( self : List[Any] ): return 1E-4 @property def UpperCamelCase_ ( self : Optional[Any] ): return 12
15
import numpy as np def UpperCAmelCase ( a_ , a_ , a_ = 1E-12 , a_ = 1_0_0 , ) -> tuple[float, np.ndarray]: """simple docstring""" assert np.shape(a_ )[0] == np.shape(a_ )[1] # Ensure proper dimensionality. assert np.shape(a_ )[0] == np.shape(a_ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(a_ ) == np.iscomplexobj(a_ ) __A = np.iscomplexobj(a_ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(a_ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. __A = False __A = 0 __A = 0 __A = 1E12 while not convergence: # Multiple matrix by the vector. __A = np.dot(a_ , a_ ) # Normalize the resulting output vector. __A = w / np.linalg.norm(a_ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) __A = vector.conj().T if is_complex else vector.T __A = np.dot(a_ , np.dot(a_ , a_ ) ) # Check convergence. __A = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: __A = True __A = lambda_ if is_complex: __A = np.real(lambda_ ) return lambda_, vector def UpperCAmelCase ( ) -> None: """simple docstring""" __A = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) __A = np.array([4_1, 4, 2_0] ) __A = real_input_matrix.astype(np.complexaaa ) __A = np.triu(1J * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T __A = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": __A = real_input_matrix __A = real_vector elif problem_type == "complex": __A = complex_input_matrix __A = complex_vector # Our implementation. __A , __A = power_iteration(a_ , a_ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). __A , __A = np.linalg.eigh(a_ ) # Last eigenvalue is the maximum one. __A = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. __A = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1E-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(a_ ) - np.abs(a_ ) ) <= 1E-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
15
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE :List[Any] = { 'configuration_lxmert': ['LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LxmertConfig'], 'tokenization_lxmert': ['LxmertTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :int = ['LxmertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :Optional[int] = [ 'LxmertEncoder', 'LxmertForPreTraining', 'LxmertForQuestionAnswering', 'LxmertModel', 'LxmertPreTrainedModel', 'LxmertVisualFeatureEncoder', 'LxmertXLayer', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :List[Any] = [ 'TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFLxmertForPreTraining', 'TFLxmertMainLayer', 'TFLxmertModel', 'TFLxmertPreTrainedModel', 'TFLxmertVisualFeatureEncoder', ] if TYPE_CHECKING: from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig from .tokenization_lxmert import LxmertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_lxmert_fast import LxmertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_lxmert import ( LxmertEncoder, LxmertForPreTraining, LxmertForQuestionAnswering, LxmertModel, LxmertPreTrainedModel, LxmertVisualFeatureEncoder, LxmertXLayer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_lxmert import ( TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFLxmertForPreTraining, TFLxmertMainLayer, TFLxmertModel, TFLxmertPreTrainedModel, TFLxmertVisualFeatureEncoder, ) else: import sys SCREAMING_SNAKE_CASE :Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
15
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig SCREAMING_SNAKE_CASE :str = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE :str = 'RegNetConfig' # Base docstring SCREAMING_SNAKE_CASE :List[str] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Union[str, Any] = [1, 1088, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE :Optional[int] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Any = 'tabby, tabby cat' SCREAMING_SNAKE_CASE :Optional[int] = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : int ,A : int = 3 ,A : int = 1 ,A : int = 1 ,A : Optional[str] = "relu" ,**A : Dict ,): super().__init__(**A ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb __A = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=A ,strides=A ,padding="VALID" ,groups=A ,use_bias=A ,name="convolution" ,) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) __A = ACTaFN[activation] if activation is not None else tf.identity def UpperCamelCase_ ( self : List[Any] ,A : Any ): __A = self.convolution(self.padding(A ) ) __A = self.normalization(A ) __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : RegNetConfig ,**A : str ): super().__init__(**A ) __A = config.num_channels __A = TFRegNetConvLayer( out_channels=config.embedding_size ,kernel_size=3 ,stride=2 ,activation=config.hidden_act ,name="embedder" ,) def UpperCamelCase_ ( self : Tuple ,A : Optional[Any] ): __A = shape_list(A )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( "Make sure that the channel dimension of the pixel values match with the one set in the configuration." ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) __A = tf.transpose(A ,perm=(0, 2, 3, 1) ) __A = self.embedder(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Optional[int] ,A : int ,A : int = 2 ,**A : Tuple ): super().__init__(**A ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=1 ,strides=A ,use_bias=A ,name="convolution" ) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) def UpperCamelCase_ ( self : Union[str, Any] ,A : tf.Tensor ,A : bool = False ): return self.normalization(self.convolution(A ) ,training=A ) class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Dict ,A : int ,A : int ,**A : str ): super().__init__(**A ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) __A = [ tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="relu" ,name="attention.0" ), tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="sigmoid" ,name="attention.2" ), ] def UpperCamelCase_ ( self : Dict ,A : List[Any] ): # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] __A = self.pooler(A ) for layer_module in self.attention: __A = layer_module(A ) __A = hidden_state * pooled return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : Optional[int] ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.2" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : int ,A : Optional[int] ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : str ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetSELayer(A ,reduced_channels=int(round(in_channels / 4 ) ) ,name="layer.2" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.3" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : Dict ,A : Any ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 2 ,A : int = 2 ,**A : Optional[int] ): super().__init__(**A ) __A = TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer __A = [ # downsampling is done in the first layer with stride of 2 layer(A ,A ,A ,stride=A ,name="layers.0" ), *[layer(A ,A ,A ,name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCamelCase_ ( self : Any ,A : List[str] ): for layer_module in self.layers: __A = layer_module(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Any ,A : RegNetConfig ,**A : List[str] ): super().__init__(**A ) __A = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A ,config.embedding_size ,config.hidden_sizes[0] ,stride=2 if config.downsample_in_first_stage else 1 ,depth=config.depths[0] ,name="stages.0" ,) ) __A = zip(config.hidden_sizes ,config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A ,config.depths[1:] ) ): self.stages.append(TFRegNetStage(A ,A ,A ,depth=A ,name=f'''stages.{i+1}''' ) ) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor ,A : bool = False ,A : bool = True ): __A = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: __A = hidden_states + (hidden_state,) __A = stage_module(A ) if output_hidden_states: __A = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A ,hidden_states=A ) @keras_serializable class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' snake_case_ = RegNetConfig def __init__( self : int ,A : Optional[int] ,**A : Dict ): super().__init__(**A ) __A = config __A = TFRegNetEmbeddings(A ,name="embedder" ) __A = TFRegNetEncoder(A ,name="encoder" ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) @unpack_inputs def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : bool = False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.embedder(A ,training=A ) __A = self.encoder( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = encoder_outputs[0] __A = self.pooler(A ) # Change to NCHW output format have uniformity in the modules __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: __A = tuple([tf.transpose(A ,perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A ,pooler_output=A ,hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states ,) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = RegNetConfig snake_case_ = "regnet" snake_case_ = "pixel_values" @property def UpperCamelCase_ ( self : Optional[Any] ): return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24) ,dtype=tf.floataa )} SCREAMING_SNAKE_CASE :Dict = R'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE :Dict = R'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare RegNet model outputting raw features without any specific head on top." , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,*A : List[Any] ,**A : str ): super().__init__(A ,*A ,**A ) __A = TFRegNetMainLayer(A ,name="regnet" ) @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC ,output_type=A ,config_class=_CONFIG_FOR_DOC ,modality="vision" ,expected_output=_EXPECTED_OUTPUT_SHAPE ,) def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : int=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( pixel_values=A ,output_hidden_states=A ,return_dict=A ,training=A ,) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state ,pooler_output=outputs.pooler_output ,hidden_states=outputs.hidden_states ,) @add_start_docstrings( "\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[int] ,A : RegNetConfig ,*A : str ,**A : Tuple ): super().__init__(A ,*A ,**A ) __A = config.num_labels __A = TFRegNetMainLayer(A ,name="regnet" ) # classification head __A = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels ,name="classifier.1" ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT ,output_type=A ,config_class=_CONFIG_FOR_DOC ,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor = None ,A : tf.Tensor = None ,A : bool = None ,A : bool = None ,A : Union[str, Any]=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = outputs.pooler_output if return_dict else outputs[1] __A = self.classifier[0](A ) __A = self.classifier[1](A ) __A = None if labels is None else self.hf_compute_loss(labels=A ,logits=A ) if not return_dict: __A = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A ,logits=A ,hidden_states=outputs.hidden_states )
15
1
# This script creates a super tiny model that is useful inside tests, when we just want to test that # the machinery works, without needing to the check the quality of the outcomes. # # This version creates a tiny vocab first, and then a tiny model - so the outcome is truly tiny - # all files ~60KB. As compared to taking a full-size model, reducing to the minimum its layers and # emb dimensions, but keeping the full vocab + merges files, leading to ~3MB in total for all files. # The latter is done by `fsmt-make-super-tiny-model.py`. # # It will be used then as "stas/tiny-wmt19-en-ru" from pathlib import Path import json import tempfile from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE :Optional[Any] = 'tiny-wmt19-en-ru' # Build # borrowed from a test SCREAMING_SNAKE_CASE :Union[str, Any] = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'w</w>', 'r</w>', 't</w>', 'lo', 'low', 'er</w>', 'low</w>', 'lowest</w>', 'newer</w>', 'wider</w>', '<unk>', ] SCREAMING_SNAKE_CASE :Optional[Any] = dict(zip(vocab, range(len(vocab)))) SCREAMING_SNAKE_CASE :Tuple = ['l o 123', 'lo w 1456', 'e r</w> 1789', ''] with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE :Optional[int] = Path(tmpdirname) SCREAMING_SNAKE_CASE :Optional[int] = build_dir / VOCAB_FILES_NAMES['src_vocab_file'] SCREAMING_SNAKE_CASE :Any = build_dir / VOCAB_FILES_NAMES['tgt_vocab_file'] SCREAMING_SNAKE_CASE :str = build_dir / VOCAB_FILES_NAMES['merges_file'] with open(src_vocab_file, 'w') as fp: fp.write(json.dumps(vocab_tokens)) with open(tgt_vocab_file, 'w') as fp: fp.write(json.dumps(vocab_tokens)) with open(merges_file, 'w') as fp: fp.write('\n'.join(merges)) SCREAMING_SNAKE_CASE :Tuple = FSMTTokenizer( langs=['en', 'ru'], src_vocab_size=len(vocab), tgt_vocab_size=len(vocab), src_vocab_file=src_vocab_file, tgt_vocab_file=tgt_vocab_file, merges_file=merges_file, ) SCREAMING_SNAKE_CASE :List[str] = FSMTConfig( langs=['ru', 'en'], src_vocab_size=1000, tgt_vocab_size=1000, d_model=4, encoder_layers=1, decoder_layers=1, encoder_ffn_dim=4, decoder_ffn_dim=4, encoder_attention_heads=1, decoder_attention_heads=1, ) SCREAMING_SNAKE_CASE :Tuple = FSMTForConditionalGeneration(config) print(f'''num of params {tiny_model.num_parameters()}''') # Test SCREAMING_SNAKE_CASE :List[str] = tokenizer(['Making tiny model'], return_tensors='pt') SCREAMING_SNAKE_CASE :str = tiny_model(**batch) print('test output:', len(outputs.logits[0])) # Save tiny_model.half() # makes it smaller tiny_model.save_pretrained(mname_tiny) tokenizer.save_pretrained(mname_tiny) print(f'''Generated {mname_tiny}''') # Upload # transformers-cli upload tiny-wmt19-en-ru
15
import math def UpperCAmelCase ( a_ , a_ = 0 , a_ = 0 ) -> list: """simple docstring""" __A = end or len(a_ ) for i in range(a_ , a_ ): __A = i __A = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __A = array[temp_index - 1] temp_index -= 1 __A = temp_index_value return array def UpperCAmelCase ( a_ , a_ , a_ ) -> None: # Max Heap """simple docstring""" __A = index __A = 2 * index + 1 # Left Node __A = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __A = left_index if right_index < heap_size and array[largest] < array[right_index]: __A = right_index if largest != index: __A , __A = array[largest], array[index] heapify(a_ , a_ , a_ ) def UpperCAmelCase ( a_ ) -> list: """simple docstring""" __A = len(a_ ) for i in range(n // 2 , -1 , -1 ): heapify(a_ , a_ , a_ ) for i in range(n - 1 , 0 , -1 ): __A , __A = array[0], array[i] heapify(a_ , 0 , a_ ) return array def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" __A = low __A = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __A , __A = array[j], array[i] i += 1 def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) == 0: return array __A = 2 * math.ceil(math.loga(len(a_ ) ) ) __A = 1_6 return intro_sort(a_ , 0 , len(a_ ) , a_ , a_ ) def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ ) -> list: """simple docstring""" while end - start > size_threshold: if max_depth == 0: return heap_sort(a_ ) max_depth -= 1 __A = median_of_a(a_ , a_ , start + ((end - start) // 2) + 1 , end - 1 ) __A = partition(a_ , a_ , a_ , a_ ) intro_sort(a_ , a_ , a_ , a_ , a_ ) __A = p return insertion_sort(a_ , a_ , a_ ) if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma : ').strip() SCREAMING_SNAKE_CASE :str = [float(item) for item in user_input.split(',')] print(sort(unsorted))
15
1
def UpperCAmelCase ( a_ ) -> List[Any]: """simple docstring""" __A = [] __A = set({"(", "[", "{"} ) __A = set({")", "]", "}"} ) __A = {"{": "}", "[": "]", "(": ")"} for i in range(len(a_ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(a_ ) == 0 or (len(a_ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(a_ ) == 0 def UpperCAmelCase ( ) -> str: """simple docstring""" __A = input("Enter sequence of brackets: " ) if is_balanced(a_ ): print(a_ , "is balanced" ) else: print(a_ , "is not balanced" ) if __name__ == "__main__": main()
15
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE :Optional[int] = NewType('DataClass', Any) SCREAMING_SNAKE_CASE :int = NewType('DataClassType', Any) def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" if isinstance(a_ , a_ ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( F'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def UpperCAmelCase ( a_ ) -> Callable[[str], Any]: """simple docstring""" __A = {str(a_ ): choice for choice in choices} return lambda a_ : str_to_choice.get(a_ , a_ ) def UpperCAmelCase ( *, a_ = None , a_ = None , a_ = dataclasses.MISSING , a_ = dataclasses.MISSING , a_ = None , **a_ , ) -> dataclasses.Field: """simple docstring""" if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls __A = {} if aliases is not None: __A = aliases if help is not None: __A = help return dataclasses.field(metadata=a_ , default=a_ , default_factory=a_ , **a_ ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 def __init__( self : Union[str, Any] ,A : Union[DataClassType, Iterable[DataClassType]] ,**A : List[Any] ): # To make the default appear when using --help if "formatter_class" not in kwargs: __A = ArgumentDefaultsHelpFormatter super().__init__(**A ) if dataclasses.is_dataclass(A ): __A = [dataclass_types] __A = list(A ) for dtype in self.dataclass_types: self._add_dataclass_arguments(A ) @staticmethod def UpperCamelCase_ ( A : ArgumentParser ,A : dataclasses.Field ): __A = f'''--{field.name}''' __A = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type ,A ): raise RuntimeError( "Unresolved type detected, which should have been done with the help of " "`typing.get_type_hints` method by default" ) __A = kwargs.pop("aliases" ,[] ) if isinstance(A ,A ): __A = [aliases] __A = getattr(field.type ,"__origin__" ,field.type ) if origin_type is Union or (hasattr(A ,"UnionType" ) and isinstance(A ,types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(A ) not in field.type.__args__ ): raise ValueError( "Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because" " the argument parser only supports one type per argument." f''' Problem encountered in field \'{field.name}\'.''' ) if type(A ) not in field.type.__args__: # filter `str` in Union __A = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] __A = getattr(field.type ,"__origin__" ,field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) __A = ( field.type.__args__[0] if isinstance(A ,field.type.__args__[1] ) else field.type.__args__[1] ) __A = getattr(field.type ,"__origin__" ,field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) __A = {} if origin_type is Literal or (isinstance(field.type ,A ) and issubclass(field.type ,A )): if origin_type is Literal: __A = field.type.__args__ else: __A = [x.value for x in field.type] __A = make_choice_type_function(kwargs["choices"] ) if field.default is not dataclasses.MISSING: __A = field.default else: __A = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument __A = copy(A ) # Hack because type=bool in argparse does not behave as we want. __A = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. __A = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way __A = default # This tells argparse we accept 0 or 1 value after --field_name __A = "?" # This is the value that will get picked if we do --field_name (without value) __A = True elif isclass(A ) and issubclass(A ,A ): __A = field.type.__args__[0] __A = "+" if field.default_factory is not dataclasses.MISSING: __A = field.default_factory() elif field.default is dataclasses.MISSING: __A = True else: __A = field.type if field.default is not dataclasses.MISSING: __A = field.default elif field.default_factory is not dataclasses.MISSING: __A = field.default_factory() else: __A = True parser.add_argument(A ,*A ,**A ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): __A = False parser.add_argument(f'''--no_{field.name}''' ,action="store_false" ,dest=field.name ,**A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : DataClassType ): if hasattr(A ,"_argument_group_name" ): __A = self.add_argument_group(dtype._argument_group_name ) else: __A = self try: __A = get_type_hints(A ) except NameError: raise RuntimeError( f'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' "removing line of `from __future__ import annotations` which opts in Postponed " "Evaluation of Annotations (PEP 563)" ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(A ): __A = ".".join(map(A ,sys.version_info[:3] ) ) raise RuntimeError( f'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' "line of `from __future__ import annotations` which opts in union types as " "`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To " "support Python versions that lower than 3.10, you need to use " "`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of " "`X | None`." ) from ex raise for field in dataclasses.fields(A ): if not field.init: continue __A = type_hints[field.name] self._parse_dataclass_field(A ,A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : List[Any]=None ,A : List[Any]=False ,A : Optional[Any]=True ,A : Union[str, Any]=None ,A : Union[str, Any]=None ,): if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): __A = [] if args_filename: args_files.append(Path(A ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix(".args" ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values __A = ArgumentParser() args_file_parser.add_argument(A ,type=A ,action="append" ) # Use only remaining args for further parsing (remove the args_file_flag) __A , __A = args_file_parser.parse_known_args(args=A ) __A = vars(A ).get(args_file_flag.lstrip("-" ) ,A ) if cmd_args_file_paths: args_files.extend([Path(A ) for p in cmd_args_file_paths] ) __A = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last __A = file_args + args if args is not None else file_args + sys.argv[1:] __A , __A = self.parse_known_args(args=A ) __A = [] for dtype in self.dataclass_types: __A = {f.name for f in dataclasses.fields(A ) if f.init} __A = {k: v for k, v in vars(A ).items() if k in keys} for k in keys: delattr(A ,A ) __A = dtype(**A ) outputs.append(A ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(A ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(f'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def UpperCamelCase_ ( self : Dict ,A : Dict[str, Any] ,A : bool = False ): __A = set(args.keys() ) __A = [] for dtype in self.dataclass_types: __A = {f.name for f in dataclasses.fields(A ) if f.init} __A = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) __A = dtype(**A ) outputs.append(A ) if not allow_extra_keys and unused_keys: raise ValueError(f'''Some keys are not used by the HfArgumentParser: {sorted(A )}''' ) return tuple(A ) def UpperCamelCase_ ( self : List[str] ,A : str ,A : bool = False ): with open(Path(A ) ,encoding="utf-8" ) as open_json_file: __A = json.loads(open_json_file.read() ) __A = self.parse_dict(A ,allow_extra_keys=A ) return tuple(A ) def UpperCamelCase_ ( self : int ,A : str ,A : bool = False ): __A = self.parse_dict(yaml.safe_load(Path(A ).read_text() ) ,allow_extra_keys=A ) return tuple(A )
15
1
from typing import Dict from .base import GenericTensor, Pipeline class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def UpperCamelCase_ ( self : int ,A : Optional[int]=None ,A : Tuple=None ,A : List[str]=None ,**A : List[str] ): if tokenize_kwargs is None: __A = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( "truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)" ) __A = truncation __A = tokenize_kwargs __A = {} if return_tensors is not None: __A = return_tensors return preprocess_params, {}, postprocess_params def UpperCamelCase_ ( self : List[str] ,A : str ,**A : str ): __A = self.framework __A = self.tokenizer(A ,return_tensors=A ,**A ) return model_inputs def UpperCamelCase_ ( self : Any ,A : Dict ): __A = self.model(**A ) return model_outputs def UpperCamelCase_ ( self : str ,A : List[str] ,A : int=False ): # [0] is the first available tensor, logits or last_hidden_state. if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self : int ,*A : Tuple ,**A : Optional[Any] ): return super().__call__(*A ,**A )
15
SCREAMING_SNAKE_CASE :Any = 256 # Modulus to hash a string SCREAMING_SNAKE_CASE :Union[str, Any] = 100_0003 def UpperCAmelCase ( a_ , a_ ) -> bool: """simple docstring""" __A = len(a_ ) __A = len(a_ ) if p_len > t_len: return False __A = 0 __A = 0 __A = 1 # Calculating the hash of pattern and substring of text for i in range(a_ ): __A = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus __A = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue __A = (modulus_power * alphabet_size) % modulus for i in range(0 , t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash __A = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def UpperCAmelCase ( ) -> None: """simple docstring""" __A = "abc1abc12" __A = "alskfjaldsabc1abc1abc12k23adsfabcabc" __A = "alskfjaldsk23adsfabcabc" assert rabin_karp(a_ , a_ ) and not rabin_karp(a_ , a_ ) # Test 2) __A = "ABABX" __A = "ABABZABABYABABX" assert rabin_karp(a_ , a_ ) # Test 3) __A = "AAAB" __A = "ABAAAAAB" assert rabin_karp(a_ , a_ ) # Test 4) __A = "abcdabcy" __A = "abcxabcdabxabcdabcdabcy" assert rabin_karp(a_ , a_ ) # Test 5) __A = "Lü" __A = "Lüsai" assert rabin_karp(a_ , a_ ) __A = "Lue" assert not rabin_karp(a_ , a_ ) print("Success." ) if __name__ == "__main__": test_rabin_karp()
15
1
import unittest import numpy as np import requests from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: SCREAMING_SNAKE_CASE :Optional[Any] = False if is_vision_available(): from PIL import Image from transformers import PixaStructImageProcessor class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self : int ,A : List[Any] ,A : List[Any]=7 ,A : Any=3 ,A : Union[str, Any]=18 ,A : str=30 ,A : List[str]=4_00 ,A : Union[str, Any]=None ,A : str=True ,A : Union[str, Any]=True ,A : Union[str, Any]=None ,): __A = size if size is not None else {"height": 20, "width": 20} __A = parent __A = batch_size __A = num_channels __A = image_size __A = min_resolution __A = max_resolution __A = size __A = do_normalize __A = do_convert_rgb __A = [5_12, 10_24, 20_48, 40_96] __A = patch_size if patch_size is not None else {"height": 16, "width": 16} def UpperCamelCase_ ( self : Any ): return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb} def UpperCamelCase_ ( self : Union[str, Any] ): __A = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg" __A = Image.open(requests.get(A ,stream=A ).raw ).convert("RGB" ) return raw_image @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." , ) @require_torch @require_vision class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = PixaStructImageProcessor if is_vision_available() else None def UpperCamelCase_ ( self : str ): __A = PixaStructImageProcessingTester(self ) @property def UpperCamelCase_ ( self : str ): return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase_ ( self : Tuple ): __A = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A ,"do_normalize" ) ) self.assertTrue(hasattr(A ,"do_convert_rgb" ) ) def UpperCamelCase_ ( self : Any ): __A = self.image_processor_tester.prepare_dummy_image() __A = self.image_processing_class(**self.image_processor_dict ) __A = 20_48 __A = image_processor(A ,return_tensors="pt" ,max_patches=A ) self.assertTrue(torch.allclose(inputs.flattened_patches.mean() ,torch.tensor(0.06_06 ) ,atol=1E-3 ,rtol=1E-3 ) ) def UpperCamelCase_ ( self : Optional[int] ): # Initialize image_processor __A = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __A = prepare_image_inputs(self.image_processor_tester ,equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A ,Image.Image ) # Test not batched input __A = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input __A = image_processor( image_inputs[0] ,return_tensors="pt" ,max_patches=A ).flattened_patches self.assertEqual( encoded_images.shape ,(1, max_patch, expected_hidden_dim) ,) # Test batched __A = image_processor( A ,return_tensors="pt" ,max_patches=A ).flattened_patches self.assertEqual( encoded_images.shape ,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,) def UpperCamelCase_ ( self : Tuple ): # Initialize image_processor __A = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __A = prepare_image_inputs(self.image_processor_tester ,equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A ,Image.Image ) # Test not batched input __A = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * self.image_processor_tester.num_channels ) + 2 __A = True for max_patch in self.image_processor_tester.max_patches: # Test not batched input with self.assertRaises(A ): __A = image_processor( image_inputs[0] ,return_tensors="pt" ,max_patches=A ).flattened_patches __A = "Hello" __A = image_processor( image_inputs[0] ,return_tensors="pt" ,max_patches=A ,header_text=A ).flattened_patches self.assertEqual( encoded_images.shape ,(1, max_patch, expected_hidden_dim) ,) # Test batched __A = image_processor( A ,return_tensors="pt" ,max_patches=A ,header_text=A ).flattened_patches self.assertEqual( encoded_images.shape ,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,) def UpperCamelCase_ ( self : Tuple ): # Initialize image_processor __A = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __A = prepare_image_inputs(self.image_processor_tester ,equal_resolution=A ,numpify=A ) for image in image_inputs: self.assertIsInstance(A ,np.ndarray ) __A = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input __A = image_processor( image_inputs[0] ,return_tensors="pt" ,max_patches=A ).flattened_patches self.assertEqual( encoded_images.shape ,(1, max_patch, expected_hidden_dim) ,) # Test batched __A = image_processor( A ,return_tensors="pt" ,max_patches=A ).flattened_patches self.assertEqual( encoded_images.shape ,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,) def UpperCamelCase_ ( self : Optional[int] ): # Initialize image_processor __A = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __A = prepare_image_inputs(self.image_processor_tester ,equal_resolution=A ,torchify=A ) for image in image_inputs: self.assertIsInstance(A ,torch.Tensor ) # Test not batched input __A = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input __A = image_processor( image_inputs[0] ,return_tensors="pt" ,max_patches=A ).flattened_patches self.assertEqual( encoded_images.shape ,(1, max_patch, expected_hidden_dim) ,) # Test batched __A = image_processor( A ,return_tensors="pt" ,max_patches=A ).flattened_patches self.assertEqual( encoded_images.shape ,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,) @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." , ) @require_torch @require_vision class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = PixaStructImageProcessor if is_vision_available() else None def UpperCamelCase_ ( self : Union[str, Any] ): __A = PixaStructImageProcessingTester(self ,num_channels=4 ) __A = 3 @property def UpperCamelCase_ ( self : Dict ): return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase_ ( self : Tuple ): __A = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A ,"do_normalize" ) ) self.assertTrue(hasattr(A ,"do_convert_rgb" ) ) def UpperCamelCase_ ( self : Dict ): # Initialize image_processor __A = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __A = prepare_image_inputs(self.image_processor_tester ,equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A ,Image.Image ) # Test not batched input __A = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * (self.image_processor_tester.num_channels - 1) ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input __A = image_processor( image_inputs[0] ,return_tensors="pt" ,max_patches=A ).flattened_patches self.assertEqual( encoded_images.shape ,(1, max_patch, expected_hidden_dim) ,) # Test batched __A = image_processor( A ,return_tensors="pt" ,max_patches=A ).flattened_patches self.assertEqual( encoded_images.shape ,(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,)
15
import argparse import json import os import torch from transformers.file_utils import has_file from diffusers import UNetaDConditionModel, UNetaDModel SCREAMING_SNAKE_CASE :Union[str, Any] = False SCREAMING_SNAKE_CASE :Any = True SCREAMING_SNAKE_CASE :Tuple = False if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser() parser.add_argument( '--repo_path', default=None, type=str, required=True, help='The config json file corresponding to the architecture.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') SCREAMING_SNAKE_CASE :Union[str, Any] = parser.parse_args() SCREAMING_SNAKE_CASE :Dict = { 'image_size': 'sample_size', 'num_res_blocks': 'layers_per_block', 'block_channels': 'block_out_channels', 'down_blocks': 'down_block_types', 'up_blocks': 'up_block_types', 'downscale_freq_shift': 'freq_shift', 'resnet_num_groups': 'norm_num_groups', 'resnet_act_fn': 'act_fn', 'resnet_eps': 'norm_eps', 'num_head_channels': 'attention_head_dim', } SCREAMING_SNAKE_CASE :Optional[int] = { 'time_steps': 'time_proj', 'mid': 'mid_block', 'downsample_blocks': 'down_blocks', 'upsample_blocks': 'up_blocks', } SCREAMING_SNAKE_CASE :int = '' if has_file(args.repo_path, 'config.json') else 'unet' with open(os.path.join(args.repo_path, subfolder, 'config.json'), 'r', encoding='utf-8') as reader: SCREAMING_SNAKE_CASE :Dict = reader.read() SCREAMING_SNAKE_CASE :List[str] = json.loads(text) if do_only_config: for key in config_parameters_to_change.keys(): config.pop(key, None) if has_file(args.repo_path, 'config.json'): SCREAMING_SNAKE_CASE :Optional[int] = UNetaDModel(**config) else: SCREAMING_SNAKE_CASE :Optional[Any] = UNetaDConditionModel if 'ldm-text2im-large-256' in args.repo_path else UNetaDModel SCREAMING_SNAKE_CASE :List[str] = class_name(**config) if do_only_config: model.save_config(os.path.join(args.repo_path, subfolder)) SCREAMING_SNAKE_CASE :List[str] = dict(model.config) if do_only_renaming: for key, value in config_parameters_to_change.items(): if key in config: SCREAMING_SNAKE_CASE :Optional[Any] = config[key] del config[key] SCREAMING_SNAKE_CASE :Optional[Any] = [k.replace('UNetRes', '') for k in config['down_block_types']] SCREAMING_SNAKE_CASE :List[Any] = [k.replace('UNetRes', '') for k in config['up_block_types']] if do_only_weights: SCREAMING_SNAKE_CASE :Tuple = torch.load(os.path.join(args.repo_path, subfolder, 'diffusion_pytorch_model.bin')) SCREAMING_SNAKE_CASE :Any = {} for param_key, param_value in state_dict.items(): if param_key.endswith('.op.bias') or param_key.endswith('.op.weight'): continue SCREAMING_SNAKE_CASE :List[str] = False for key, new_key in key_parameters_to_change.items(): if not has_changed and param_key.split('.')[0] == key: SCREAMING_SNAKE_CASE :List[Any] = param_value SCREAMING_SNAKE_CASE :str = True if not has_changed: SCREAMING_SNAKE_CASE :List[str] = param_value model.load_state_dict(new_state_dict) model.save_pretrained(os.path.join(args.repo_path, subfolder))
15
1
import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin SCREAMING_SNAKE_CASE :int = logging.get_logger(__name__) enable_full_determinism() class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = UNetaDModel snake_case_ = "sample" @property def UpperCamelCase_ ( self : List[str] ): __A = 4 __A = 3 __A = (32, 32) __A = floats_tensor((batch_size, num_channels) + sizes ).to(A ) __A = torch.tensor([10] ).to(A ) return {"sample": noise, "timestep": time_step} @property def UpperCamelCase_ ( self : Any ): return (3, 32, 32) @property def UpperCamelCase_ ( self : Any ): return (3, 32, 32) def UpperCamelCase_ ( self : Dict ): __A = { "block_out_channels": (32, 64), "down_block_types": ("DownBlock2D", "AttnDownBlock2D"), "up_block_types": ("AttnUpBlock2D", "UpBlock2D"), "attention_head_dim": 3, "out_channels": 3, "in_channels": 3, "layers_per_block": 2, "sample_size": 32, } __A = self.dummy_input return init_dict, inputs_dict class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = UNetaDModel snake_case_ = "sample" @property def UpperCamelCase_ ( self : Tuple ): __A = 4 __A = 4 __A = (32, 32) __A = floats_tensor((batch_size, num_channels) + sizes ).to(A ) __A = torch.tensor([10] ).to(A ) return {"sample": noise, "timestep": time_step} @property def UpperCamelCase_ ( self : List[str] ): return (4, 32, 32) @property def UpperCamelCase_ ( self : int ): return (4, 32, 32) def UpperCamelCase_ ( self : Union[str, Any] ): __A = { "sample_size": 32, "in_channels": 4, "out_channels": 4, "layers_per_block": 2, "block_out_channels": (32, 64), "attention_head_dim": 32, "down_block_types": ("DownBlock2D", "DownBlock2D"), "up_block_types": ("UpBlock2D", "UpBlock2D"), } __A = self.dummy_input return init_dict, inputs_dict def UpperCamelCase_ ( self : Dict ): __A , __A = UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,output_loading_info=A ) self.assertIsNotNone(A ) self.assertEqual(len(loading_info["missing_keys"] ) ,0 ) model.to(A ) __A = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != "cuda" ,"This test is supposed to run on GPU" ) def UpperCamelCase_ ( self : Union[str, Any] ): __A , __A = UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,output_loading_info=A ) model.to(A ) __A = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != "cuda" ,"This test is supposed to run on GPU" ) def UpperCamelCase_ ( self : Union[str, Any] ): # by defautl model loading will use accelerate as `low_cpu_mem_usage=True` __A , __A = UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ,output_loading_info=A ) model_accelerate.to(A ) model_accelerate.eval() __A = torch.randn( 1 ,model_accelerate.config.in_channels ,model_accelerate.config.sample_size ,model_accelerate.config.sample_size ,generator=torch.manual_seed(0 ) ,) __A = noise.to(A ) __A = torch.tensor([10] * noise.shape[0] ).to(A ) __A = model_accelerate(A ,A )["sample"] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() __A , __A = UNetaDModel.from_pretrained( "fusing/unet-ldm-dummy-update" ,output_loading_info=A ,low_cpu_mem_usage=A ) model_normal_load.to(A ) model_normal_load.eval() __A = model_normal_load(A ,A )["sample"] assert torch_all_close(A ,A ,rtol=1E-3 ) def UpperCamelCase_ ( self : Optional[int] ): __A = UNetaDModel.from_pretrained("fusing/unet-ldm-dummy-update" ) model.eval() model.to(A ) __A = torch.randn( 1 ,model.config.in_channels ,model.config.sample_size ,model.config.sample_size ,generator=torch.manual_seed(0 ) ,) __A = noise.to(A ) __A = torch.tensor([10] * noise.shape[0] ).to(A ) with torch.no_grad(): __A = model(A ,A ).sample __A = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off __A = torch.tensor([-13.32_58, -20.11_00, -15.98_73, -17.66_17, -23.05_96, -17.94_19, -13.36_75, -16.18_89, -12.38_00] ) # fmt: on self.assertTrue(torch_all_close(A ,A ,rtol=1E-3 ) ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = UNetaDModel snake_case_ = "sample" @property def UpperCamelCase_ ( self : Optional[Any] ,A : List[str]=(32, 32) ): __A = 4 __A = 3 __A = floats_tensor((batch_size, num_channels) + sizes ).to(A ) __A = torch.tensor(batch_size * [10] ).to(dtype=torch.intaa ,device=A ) return {"sample": noise, "timestep": time_step} @property def UpperCamelCase_ ( self : List[str] ): return (3, 32, 32) @property def UpperCamelCase_ ( self : List[Any] ): return (3, 32, 32) def UpperCamelCase_ ( self : int ): __A = { "block_out_channels": [32, 64, 64, 64], "in_channels": 3, "layers_per_block": 1, "out_channels": 3, "time_embedding_type": "fourier", "norm_eps": 1E-6, "mid_block_scale_factor": math.sqrt(2.0 ), "norm_num_groups": None, "down_block_types": [ "SkipDownBlock2D", "AttnSkipDownBlock2D", "SkipDownBlock2D", "SkipDownBlock2D", ], "up_block_types": [ "SkipUpBlock2D", "SkipUpBlock2D", "AttnSkipUpBlock2D", "SkipUpBlock2D", ], } __A = self.dummy_input return init_dict, inputs_dict @slow def UpperCamelCase_ ( self : List[str] ): __A , __A = UNetaDModel.from_pretrained("google/ncsnpp-celebahq-256" ,output_loading_info=A ) self.assertIsNotNone(A ) self.assertEqual(len(loading_info["missing_keys"] ) ,0 ) model.to(A ) __A = self.dummy_input __A = floats_tensor((4, 3) + (2_56, 2_56) ).to(A ) __A = noise __A = model(**A ) assert image is not None, "Make sure output is not None" @slow def UpperCamelCase_ ( self : Tuple ): __A = UNetaDModel.from_pretrained("google/ncsnpp-celebahq-256" ) model.to(A ) __A = 4 __A = 3 __A = (2_56, 2_56) __A = torch.ones((batch_size, num_channels) + sizes ).to(A ) __A = torch.tensor(batch_size * [1E-4] ).to(A ) with torch.no_grad(): __A = model(A ,A ).sample __A = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off __A = torch.tensor([-48_42.86_91, -64_99.66_31, -38_00.19_53, -79_78.26_86, -1_09_80.71_29, -2_00_28.85_35, 81_48.28_22, 23_42.29_05, 5_67.76_08] ) # fmt: on self.assertTrue(torch_all_close(A ,A ,rtol=1E-2 ) ) def UpperCamelCase_ ( self : str ): __A = UNetaDModel.from_pretrained("fusing/ncsnpp-ffhq-ve-dummy-update" ) model.to(A ) __A = 4 __A = 3 __A = (32, 32) __A = torch.ones((batch_size, num_channels) + sizes ).to(A ) __A = torch.tensor(batch_size * [1E-4] ).to(A ) with torch.no_grad(): __A = model(A ,A ).sample __A = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off __A = torch.tensor([-0.03_25, -0.09_00, -0.08_69, -0.03_32, -0.07_25, -0.02_70, -0.01_01, 0.02_27, 0.02_56] ) # fmt: on self.assertTrue(torch_all_close(A ,A ,rtol=1E-2 ) ) def UpperCamelCase_ ( self : Union[str, Any] ): # not required for this model pass
15
import argparse import math import traceback import dateutil.parser as date_parser import requests def UpperCAmelCase ( a_ ) -> str: """simple docstring""" __A = {} __A = job["started_at"] __A = job["completed_at"] __A = date_parser.parse(a_ ) __A = date_parser.parse(a_ ) __A = round((end_datetime - start_datetime).total_seconds() / 60.0 ) __A = start __A = end __A = duration_in_min return job_info def UpperCAmelCase ( a_ , a_=None ) -> str: """simple docstring""" __A = None if token is not None: __A = {"Accept": "application/vnd.github+json", "Authorization": F'''Bearer {token}'''} __A = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' __A = requests.get(a_ , headers=a_ ).json() __A = {} try: job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) __A = math.ceil((result["total_count"] - 1_0_0) / 1_0_0 ) for i in range(a_ ): __A = requests.get(url + F'''&page={i + 2}''' , headers=a_ ).json() job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) return job_time except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": SCREAMING_SNAKE_CASE :Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() SCREAMING_SNAKE_CASE :Union[str, Any] = get_job_time(args.workflow_run_id) SCREAMING_SNAKE_CASE :Optional[int] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f'''{k}: {v["duration"]}''')
15
1
import flax.linen as nn import jax import jax.numpy as jnp class UpperCAmelCase ( nn.Module ): '''simple docstring''' snake_case_ = 42 snake_case_ = jnp.floataa def UpperCamelCase_ ( self : int ): __A = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : List[str] ,A : str ): __A , __A , __A , __A = hidden_states.shape __A = jax.image.resize( A ,shape=(batch, height * 2, width * 2, channels) ,method="nearest" ,) __A = self.conv(A ) return hidden_states class UpperCAmelCase ( nn.Module ): '''simple docstring''' snake_case_ = 42 snake_case_ = jnp.floataa def UpperCamelCase_ ( self : int ): __A = nn.Conv( self.out_channels ,kernel_size=(3, 3) ,strides=(2, 2) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) def __call__( self : Any ,A : str ): # pad = ((0, 0), (0, 1), (0, 1), (0, 0)) # pad height and width dim # hidden_states = jnp.pad(hidden_states, pad_width=pad) __A = self.conv(A ) return hidden_states class UpperCAmelCase ( nn.Module ): '''simple docstring''' snake_case_ = 42 snake_case_ = None snake_case_ = 0.0 snake_case_ = None snake_case_ = jnp.floataa def UpperCamelCase_ ( self : Optional[int] ): __A = self.in_channels if self.out_channels is None else self.out_channels __A = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __A = nn.Conv( A ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __A = nn.Dense(A ,dtype=self.dtype ) __A = nn.GroupNorm(num_groups=32 ,epsilon=1E-5 ) __A = nn.Dropout(self.dropout_prob ) __A = nn.Conv( A ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) __A = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut __A = None if use_nin_shortcut: __A = nn.Conv( A ,kernel_size=(1, 1) ,strides=(1, 1) ,padding="VALID" ,dtype=self.dtype ,) def __call__( self : List[Any] ,A : List[str] ,A : str ,A : Optional[int]=True ): __A = hidden_states __A = self.norma(A ) __A = nn.swish(A ) __A = self.conva(A ) __A = self.time_emb_proj(nn.swish(A ) ) __A = jnp.expand_dims(jnp.expand_dims(A ,1 ) ,1 ) __A = hidden_states + temb __A = self.norma(A ) __A = nn.swish(A ) __A = self.dropout(A ,A ) __A = self.conva(A ) if self.conv_shortcut is not None: __A = self.conv_shortcut(A ) return hidden_states + residual
15
import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" __A = args.pruning_method __A = args.threshold __A = args.model_name_or_path.rstrip("/" ) __A = args.target_model_path print(F'''Load fine-pruned model from {model_name_or_path}''' ) __A = torch.load(os.path.join(a_ , "pytorch_model.bin" ) ) __A = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: __A = tensor print(F'''Copied layer {name}''' ) elif "classifier" in name or "qa_output" in name: __A = tensor print(F'''Copied layer {name}''' ) elif "bias" in name: __A = tensor print(F'''Copied layer {name}''' ) else: if pruning_method == "magnitude": __A = MagnitudeBinarizer.apply(inputs=a_ , threshold=a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "topK": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A = TopKBinarizer.apply(a_ , a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A = ThresholdBinarizer.apply(a_ , a_ , a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "l0": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A , __A = -0.1, 1.1 __A = torch.sigmoid(a_ ) __A = s * (r - l) + l __A = s_bar.clamp(min=0.0 , max=1.0 ) __A = tensor * mask print(F'''Pruned layer {name}''' ) else: raise ValueError("Unknown pruning method" ) if target_model_path is None: __A = os.path.join( os.path.dirname(a_ ) , F'''bertarized_{os.path.basename(a_ )}''' ) if not os.path.isdir(a_ ): shutil.copytree(a_ , a_ ) print(F'''\nCreated folder {target_model_path}''' ) torch.save(a_ , os.path.join(a_ , "pytorch_model.bin" ) ) print("\nPruned model saved! See you later!" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser() parser.add_argument( '--pruning_method', choices=['l0', 'magnitude', 'topK', 'sigmoied_threshold'], type=str, required=True, help=( 'Pruning Method (l0 = L0 regularization, magnitude = Magnitude pruning, topK = Movement pruning,' ' sigmoied_threshold = Soft movement pruning)' ), ) parser.add_argument( '--threshold', type=float, required=False, help=( 'For `magnitude` and `topK`, it is the level of remaining weights (in %) in the fine-pruned model.' 'For `sigmoied_threshold`, it is the threshold \tau against which the (sigmoied) scores are compared.' 'Not needed for `l0`' ), ) parser.add_argument( '--model_name_or_path', type=str, required=True, help='Folder containing the model that was previously fine-pruned', ) parser.add_argument( '--target_model_path', default=None, type=str, required=False, help='Folder containing the model that was previously fine-pruned', ) SCREAMING_SNAKE_CASE :str = parser.parse_args() main(args)
15
1
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return lst __A = 1 while i < len(a_ ): if lst[i - 1] <= lst[i]: i += 1 else: __A , __A = lst[i], lst[i - 1] i -= 1 if i == 0: __A = 1 return lst if __name__ == "__main__": SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :List[Any] = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
15
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :List[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :int = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Union[str, Any] = { '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' ), } } SCREAMING_SNAKE_CASE :int = { 'google/bigbird-roberta-base': 4096, 'google/bigbird-roberta-large': 4096, 'google/bigbird-base-trivia-itc': 4096, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] snake_case_ = [] def __init__( self : Any ,A : List[str] ,A : str="<unk>" ,A : int="<s>" ,A : Union[str, Any]="</s>" ,A : List[str]="<pad>" ,A : int="[SEP]" ,A : Optional[Any]="[MASK]" ,A : Tuple="[CLS]" ,A : Optional[Dict[str, Any]] = None ,**A : Any ,): __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else bos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else eos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else unk_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else pad_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else cls_token __A = 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 __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sep_token=A ,mask_token=A ,cls_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) @property def UpperCamelCase_ ( self : List[str] ): return self.sp_model.get_piece_size() def UpperCamelCase_ ( self : Optional[Any] ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : str ,A : Optional[Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Tuple ): return self.sp_model.piece_to_id(A ) def UpperCamelCase_ ( self : List[Any] ,A : Tuple ): __A = self.sp_model.IdToPiece(A ) return token def UpperCamelCase_ ( self : List[Any] ,A : int ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def UpperCamelCase_ ( self : Tuple ,A : List[int] ,A : bool = False ,A : bool = None ,A : bool = True ,**A : Union[str, Any] ,): __A = kwargs.pop("use_source_tokenizer" ,A ) __A = self.convert_ids_to_tokens(A ,skip_special_tokens=A ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __A = [] __A = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) __A = [] sub_texts.append(A ) else: current_sub_text.append(A ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: __A = re.sub(R" (\[(MASK|SEP)\])" ,R"\1" ," ".join(A ) ) else: __A = "".join(A ) __A = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __A = self.clean_up_tokenization(A ) return clean_text else: return text def UpperCamelCase_ ( self : str ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def UpperCamelCase_ ( self : Optional[int] ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=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 : Any ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
15
1
def UpperCAmelCase ( a_ , a_ , a_ ) -> float: """simple docstring""" return round(float(moles / volume ) * nfactor ) def UpperCAmelCase ( a_ , a_ , a_ ) -> float: """simple docstring""" return round(float((moles * 0.0_821 * temperature) / (volume) ) ) def UpperCAmelCase ( a_ , a_ , a_ ) -> float: """simple docstring""" return round(float((moles * 0.0_821 * temperature) / (pressure) ) ) def UpperCAmelCase ( a_ , a_ , a_ ) -> float: """simple docstring""" return round(float((pressure * volume) / (0.0_821 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
15
import PIL.Image import PIL.ImageOps from packaging import version from PIL import Image if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('9.1.0'): SCREAMING_SNAKE_CASE :Any = { 'linear': PIL.Image.Resampling.BILINEAR, 'bilinear': PIL.Image.Resampling.BILINEAR, 'bicubic': PIL.Image.Resampling.BICUBIC, 'lanczos': PIL.Image.Resampling.LANCZOS, 'nearest': PIL.Image.Resampling.NEAREST, } else: SCREAMING_SNAKE_CASE :int = { 'linear': PIL.Image.LINEAR, 'bilinear': PIL.Image.BILINEAR, 'bicubic': PIL.Image.BICUBIC, 'lanczos': PIL.Image.LANCZOS, 'nearest': PIL.Image.NEAREST, } def UpperCAmelCase ( a_ ) -> Optional[Any]: """simple docstring""" __A = (images / 2 + 0.5).clamp(0 , 1 ) __A = images.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __A = numpy_to_pil(a_ ) return images def UpperCAmelCase ( a_ ) -> int: """simple docstring""" if images.ndim == 3: __A = images[None, ...] __A = (images * 2_5_5).round().astype("uint8" ) if images.shape[-1] == 1: # special case for grayscale (single channel) images __A = [Image.fromarray(image.squeeze() , mode="L" ) for image in images] else: __A = [Image.fromarray(a_ ) for image in images] return pil_images
15
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from argparse import ArgumentParser from accelerate.commands.config import get_config_parser from accelerate.commands.env import env_command_parser from accelerate.commands.launch import launch_command_parser from accelerate.commands.test import test_command_parser from accelerate.commands.tpu import tpu_command_parser def UpperCAmelCase ( ) -> Any: """simple docstring""" __A = ArgumentParser("Accelerate CLI tool" , usage="accelerate <command> [<args>]" , allow_abbrev=a_ ) __A = parser.add_subparsers(help="accelerate command helpers" ) # Register commands get_config_parser(subparsers=a_ ) env_command_parser(subparsers=a_ ) launch_command_parser(subparsers=a_ ) tpu_command_parser(subparsers=a_ ) test_command_parser(subparsers=a_ ) # Let's go __A = parser.parse_args() if not hasattr(a_ , "func" ): parser.print_help() exit(1 ) # Run args.func(a_ ) if __name__ == "__main__": main()
15
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 SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[Any] = { 'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json', # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "yolos" def __init__( self : Any ,A : Optional[Any]=7_68 ,A : Dict=12 ,A : Any=12 ,A : str=30_72 ,A : Any="gelu" ,A : str=0.0 ,A : List[str]=0.0 ,A : Dict=0.02 ,A : int=1E-12 ,A : Tuple=[5_12, 8_64] ,A : List[Any]=16 ,A : str=3 ,A : str=True ,A : Any=1_00 ,A : Dict=True ,A : Dict=False ,A : Tuple=1 ,A : Union[str, Any]=5 ,A : Optional[Any]=2 ,A : Union[str, Any]=5 ,A : int=2 ,A : int=0.1 ,**A : List[str] ,): super().__init__(**A ) __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = initializer_range __A = layer_norm_eps __A = image_size __A = patch_size __A = num_channels __A = qkv_bias __A = num_detection_tokens __A = use_mid_position_embeddings __A = auxiliary_loss # Hungarian matcher __A = class_cost __A = bbox_cost __A = giou_cost # Loss coefficients __A = bbox_loss_coefficient __A = giou_loss_coefficient __A = eos_coefficient class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = version.parse("1.11" ) @property def UpperCamelCase_ ( self : str ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def UpperCamelCase_ ( self : List[Any] ): return 1E-4 @property def UpperCamelCase_ ( self : Optional[Any] ): return 12
15
1
from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_tf_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_tf_available(): import tensorflow as tf SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) @dataclass class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = [ "no_inference", "no_cuda", "no_tpu", "no_speed", "no_memory", "no_env_print", "no_multi_process", ] def __init__( self : Any ,**A : int ): for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: __A = deprecated_arg[3:] __A = not kwargs.pop(A ) logger.warning( f'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or''' f''' {positive_arg}={kwargs[positive_arg]}''' ) __A = kwargs.pop("tpu_name" ,self.tpu_name ) __A = kwargs.pop("device_idx" ,self.device_idx ) __A = kwargs.pop("eager_mode" ,self.eager_mode ) __A = kwargs.pop("use_xla" ,self.use_xla ) super().__init__(**A ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Name of TPU"} , ) snake_case_ = field( default=0 , metadata={"help": "CPU / GPU device index. Defaults to 0."} , ) snake_case_ = field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Benchmark models in eager model."} ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": "Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`." } , ) @cached_property def UpperCamelCase_ ( self : Dict ): requires_backends(self ,["tf"] ) __A = None if self.tpu: try: if self.tpu_name: __A = tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name ) else: __A = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: __A = None return tpu @cached_property def UpperCamelCase_ ( self : Tuple ): requires_backends(self ,["tf"] ) if self.is_tpu: tf.config.experimental_connect_to_cluster(self._setup_tpu ) tf.tpu.experimental.initialize_tpu_system(self._setup_tpu ) __A = tf.distribute.TPUStrategy(self._setup_tpu ) else: # currently no multi gpu is allowed if self.is_gpu: # TODO: Currently only single GPU is supported tf.config.set_visible_devices(self.gpu_list[self.device_idx] ,"GPU" ) __A = tf.distribute.OneDeviceStrategy(device=f'''/gpu:{self.device_idx}''' ) else: tf.config.set_visible_devices([] ,"GPU" ) # disable GPU __A = tf.distribute.OneDeviceStrategy(device=f'''/cpu:{self.device_idx}''' ) return strategy @property def UpperCamelCase_ ( self : List[Any] ): requires_backends(self ,["tf"] ) return self._setup_tpu is not None @property def UpperCamelCase_ ( self : Any ): requires_backends(self ,["tf"] ) return self._setup_strategy @property def UpperCamelCase_ ( self : int ): requires_backends(self ,["tf"] ) return tf.config.list_physical_devices("GPU" ) @property def UpperCamelCase_ ( self : Tuple ): requires_backends(self ,["tf"] ) if self.cuda: return len(self.gpu_list ) return 0 @property def UpperCamelCase_ ( self : Union[str, Any] ): return self.n_gpu > 0
15
# Copyright 2021 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from packaging import version from .. import __version__ from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD from .doc import ( add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, copy_func, replace_return_docstrings, ) from .generic import ( ContextManagers, ExplicitEnum, ModelOutput, PaddingStrategy, TensorType, add_model_info_to_auto_map, cached_property, can_return_loss, expand_dims, find_labels, flatten_dict, infer_framework, is_jax_tensor, is_numpy_array, is_tensor, is_tf_symbolic_tensor, is_tf_tensor, is_torch_device, is_torch_dtype, is_torch_tensor, reshape, squeeze, strtobool, tensor_size, to_numpy, to_py_obj, transpose, working_or_temp_dir, ) from .hub import ( CLOUDFRONT_DISTRIB_PREFIX, DISABLE_TELEMETRY, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, EntryNotFoundError, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, cached_file, default_cache_path, define_sagemaker_information, download_url, extract_commit_hash, get_cached_models, get_file_from_repo, get_full_repo_name, has_file, http_user_agent, is_offline_mode, is_remote_url, move_cache, send_example_telemetry, try_to_load_from_cache, ) from .import_utils import ( ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, TORCH_FX_REQUIRED_VERSION, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, ccl_version, direct_transformers_import, get_torch_version, is_accelerate_available, is_apex_available, is_bitsandbytes_available, is_bsa_available, is_coloredlogs_available, is_cython_available, is_datasets_available, is_decord_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_jieba_available, is_jumanpp_available, is_kenlm_available, is_keras_nlp_available, is_librosa_available, is_natten_available, is_ninja_available, is_onnx_available, is_openai_available, is_optimum_available, is_pandas_available, is_peft_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytest_available, is_pytorch_quantization_available, is_rjieba_available, is_sacremoses_available, is_safetensors_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_sudachi_available, is_tensorflow_probability_available, is_tensorflow_text_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_bfaa_cpu_available, is_torch_bfaa_gpu_available, is_torch_compile_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_neuroncore_available, is_torch_tensorrt_fx_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_torchdistx_available, is_torchdynamo_available, is_torchvision_available, is_training_run_on_sagemaker, is_vision_available, requires_backends, torch_only_method, ) SCREAMING_SNAKE_CASE :List[str] = 'pytorch_model.bin' SCREAMING_SNAKE_CASE :str = 'pytorch_model.bin.index.json' SCREAMING_SNAKE_CASE :Optional[int] = 'adapter_config.json' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.bin' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.safetensors' SCREAMING_SNAKE_CASE :str = 'tf_model.h5' SCREAMING_SNAKE_CASE :List[Any] = 'tf_model.h5.index.json' SCREAMING_SNAKE_CASE :str = 'model.ckpt' SCREAMING_SNAKE_CASE :List[Any] = 'flax_model.msgpack' SCREAMING_SNAKE_CASE :Optional[int] = 'flax_model.msgpack.index.json' SCREAMING_SNAKE_CASE :Tuple = 'model.safetensors' SCREAMING_SNAKE_CASE :List[Any] = 'model.safetensors.index.json' SCREAMING_SNAKE_CASE :str = 'config.json' SCREAMING_SNAKE_CASE :int = 'preprocessor_config.json' SCREAMING_SNAKE_CASE :Optional[Any] = FEATURE_EXTRACTOR_NAME SCREAMING_SNAKE_CASE :Optional[int] = 'generation_config.json' SCREAMING_SNAKE_CASE :List[str] = 'modelcard.json' SCREAMING_SNAKE_CASE :Optional[int] = '▁' SCREAMING_SNAKE_CASE :Optional[Any] = SENTENCEPIECE_UNDERLINE # Kept for backward compatibility SCREAMING_SNAKE_CASE :str = [ [[0, 1, 0, 1], [1, 0, 0, 1]] ] * 2 # Needs to have 0s and 1s only since XLM uses it for langs too. SCREAMING_SNAKE_CASE :Optional[Any] = [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]] SCREAMING_SNAKE_CASE :List[Any] = [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]] def UpperCAmelCase ( a_ ) -> Dict: """simple docstring""" if version.parse(a_ ) < version.parse(a_ ): if "dev" in min_version: __A = ( "This example requires a source install from HuggingFace Transformers (see " "`https://huggingface.co/docs/transformers/installation#install-from-source`)," ) else: __A = F'''This example requires a minimum version of {min_version},''' error_message += F''' but the version found is {__version__}.\n''' raise ImportError( error_message + "Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other " "versions of HuggingFace Transformers." )
15
1
import tempfile import unittest from pathlib import Path from shutil import copyfile from transformers import BatchEncoding, MarianTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import is_sentencepiece_available, is_tf_available, is_torch_available if is_sentencepiece_available(): from transformers.models.marian.tokenization_marian import VOCAB_FILES_NAMES, save_json from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE :str = get_tests_dir('fixtures/test_sentencepiece.model') SCREAMING_SNAKE_CASE :Optional[Any] = {'target_lang': 'fi', 'source_lang': 'en'} SCREAMING_SNAKE_CASE :Optional[Any] = '>>zh<<' SCREAMING_SNAKE_CASE :int = 'Helsinki-NLP/' if is_torch_available(): SCREAMING_SNAKE_CASE :int = 'pt' elif is_tf_available(): SCREAMING_SNAKE_CASE :List[str] = 'tf' else: SCREAMING_SNAKE_CASE :Tuple = 'jax' @require_sentencepiece class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = MarianTokenizer snake_case_ = False snake_case_ = True def UpperCamelCase_ ( self : Tuple ): super().setUp() __A = ["</s>", "<unk>", "▁This", "▁is", "▁a", "▁t", "est", "\u0120", "<pad>"] __A = dict(zip(A ,range(len(A ) ) ) ) __A = Path(self.tmpdirname ) save_json(A ,save_dir / VOCAB_FILES_NAMES["vocab"] ) save_json(A ,save_dir / VOCAB_FILES_NAMES["tokenizer_config_file"] ) if not (save_dir / VOCAB_FILES_NAMES["source_spm"]).exists(): copyfile(A ,save_dir / VOCAB_FILES_NAMES["source_spm"] ) copyfile(A ,save_dir / VOCAB_FILES_NAMES["target_spm"] ) __A = MarianTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self : Dict ,**A : Tuple ): return MarianTokenizer.from_pretrained(self.tmpdirname ,**A ) def UpperCamelCase_ ( self : int ,A : Dict ): return ( "This is a test", "This is a test", ) def UpperCamelCase_ ( self : Optional[int] ): __A = "</s>" __A = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A ) ,A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A ) ,A ) def UpperCamelCase_ ( self : Any ): __A = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"</s>" ) self.assertEqual(vocab_keys[1] ,"<unk>" ) self.assertEqual(vocab_keys[-1] ,"<pad>" ) self.assertEqual(len(A ) ,9 ) def UpperCamelCase_ ( self : List[Any] ): self.assertEqual(self.get_tokenizer().vocab_size ,9 ) def UpperCamelCase_ ( self : str ): __A = MarianTokenizer.from_pretrained(f'''{ORG_NAME}opus-mt-en-de''' ) __A = en_de_tokenizer(["I am a small frog"] ,return_tensors=A ) self.assertIsInstance(A ,A ) __A = [38, 1_21, 14, 6_97, 3_88_48, 0] self.assertListEqual(A ,batch.input_ids[0] ) __A = tempfile.mkdtemp() en_de_tokenizer.save_pretrained(A ) __A = [x.name for x in Path(A ).glob("*" )] self.assertIn("source.spm" ,A ) MarianTokenizer.from_pretrained(A ) def UpperCamelCase_ ( self : Optional[Any] ): __A = self.get_tokenizer() __A = tok( ["I am a small frog" * 10_00, "I am a small frog"] ,padding=A ,truncation=A ,return_tensors=A ) self.assertIsInstance(A ,A ) self.assertEqual(batch.input_ids.shape ,(2, 5_12) ) def UpperCamelCase_ ( self : Tuple ): __A = self.get_tokenizer() __A = tok(["I am a tiny frog", "I am a small frog"] ,padding=A ,return_tensors=A ) self.assertIsInstance(A ,A ) self.assertEqual(batch_smaller.input_ids.shape ,(2, 10) ) @slow def UpperCamelCase_ ( self : Optional[Any] ): # fmt: off __A = {"input_ids": [[4_34_95, 4_62, 20, 4_21_64, 13_69, 52, 4_64, 1_32, 17_03, 4_92, 13, 74_91, 3_89_99, 6, 8, 4_64, 1_32, 17_03, 4_92, 13, 46_69, 3_78_67, 13, 75_25, 27, 15_93, 9_88, 13, 3_39_72, 70_29, 6, 20, 82_51, 3_83, 2, 2_70, 58_66, 37_88, 2, 23_53, 82_51, 1_23_38, 2, 1_39_58, 3_87, 2, 36_29, 69_53, 1_88, 29_00, 2, 1_39_58, 80_11, 1_15_01, 23, 84_60, 40_73, 3_40_09, 20, 4_35, 1_14_39, 27, 8, 84_60, 40_73, 60_04, 20, 99_88, 3_75, 27, 33, 2_66, 19_45, 10_76, 13_50, 3_78_67, 32_88, 5, 5_77, 10_76, 43_74, 8, 50_82, 5, 2_64_53, 2_57, 5_56, 4_03, 2, 2_42, 1_32, 3_83, 3_16, 4_92, 8, 1_07_67, 6, 3_16, 3_04, 42_39, 3, 0], [1_48, 1_57_22, 19, 18_39, 12, 13_50, 13, 2_23_27, 50_82, 54_18, 4_75_67, 3_59_38, 59, 3_18, 1_95_52, 1_08, 21_83, 54, 1_49_76, 48_35, 32, 5_47, 11_14, 8, 3_15, 24_17, 5, 92, 1_90_88, 3, 0, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00], [36, 63_95, 1_25_70, 3_91_47, 1_15_97, 6, 2_66, 4, 4_54_05, 72_96, 3, 0, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00, 5_81_00]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=A ,model_name="Helsinki-NLP/opus-mt-en-de" ,revision="1a8c2263da11e68e50938f97e10cd57820bd504c" ,decode_kwargs={"use_source_tokenizer": True} ,) def UpperCamelCase_ ( self : Tuple ): __A = MarianTokenizer.from_pretrained("hf-internal-testing/test-marian-two-vocabs" ) __A = "Tämä on testi" __A = "This is a test" __A = [76, 7, 20_47, 2] __A = [69, 12, 11, 9_40, 2] __A = tokenizer(A ).input_ids self.assertListEqual(A ,A ) __A = tokenizer(text_target=A ).input_ids self.assertListEqual(A ,A ) __A = tokenizer.decode(A ,skip_special_tokens=A ) self.assertEqual(A ,A )
15
def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" __A = [0] * len(a_ ) __A = [] __A = [1] * len(a_ ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(a_ ) ): if indegree[i] == 0: queue.append(a_ ) while queue: __A = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: __A = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(a_ ) print(max(a_ ) ) # Adjacency list of Graph SCREAMING_SNAKE_CASE :List[Any] = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
15
1
def UpperCAmelCase ( a_ , a_ ) -> bool: """simple docstring""" __A = len(a_ ) __A = [[False] * (required_sum + 1) for _ in range(arr_len + 1 )] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 for i in range(arr_len + 1 ): __A = True # sum is not zero and set is empty then false for i in range(1 , required_sum + 1 ): __A = False for i in range(1 , arr_len + 1 ): for j in range(1 , required_sum + 1 ): if arr[i - 1] > j: __A = subset[i - 1][j] if arr[i - 1] <= j: __A = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] return subset[arr_len][required_sum] if __name__ == "__main__": import doctest doctest.testmod()
15
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items() ) def UpperCAmelCase ( a_ , a_ ) -> Tuple: """simple docstring""" __A = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue __A = key.replace("heads.cmd.mim_head.cls.predictions" , "mmm_image_head" ) __A = key.replace("heads.cmd.mlm_head.cls.predictions" , "mmm_text_head" ) __A = key.replace("heads.cmd.itm_head.cls" , "itm_head" ) __A = key.replace("heads.cmd.itm_head.pooler" , "itm_head.pooler" ) __A = key.replace("heads.cmd.clip_head.logit_scale" , "flava.logit_scale" ) __A = key.replace("heads.fairseq_mlm.cls.predictions" , "mlm_head" ) __A = key.replace("heads.imagenet.mim_head.cls.predictions" , "mim_head" ) __A = key.replace("mm_text_projection" , "flava.text_to_mm_projection" ) __A = key.replace("mm_image_projection" , "flava.image_to_mm_projection" ) __A = key.replace("image_encoder.module" , "flava.image_model" ) __A = key.replace("text_encoder.module" , "flava.text_model" ) __A = key.replace("mm_encoder.module.encoder.cls_token" , "flava.multimodal_model.cls_token" ) __A = key.replace("mm_encoder.module" , "flava.multimodal_model" ) __A = key.replace("text_projection" , "flava.text_projection" ) __A = key.replace("image_projection" , "flava.image_projection" ) __A = value.float() for key, value in codebook_state_dict.items(): __A = value return upgrade @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_ , a_=None ) -> Tuple: """simple docstring""" if config_path is not None: __A = FlavaConfig.from_pretrained(a_ ) else: __A = FlavaConfig() __A = FlavaForPreTraining(a_ ).eval() __A = convert_dalle_checkpoint(a_ , a_ , save_checkpoint=a_ ) if os.path.exists(a_ ): __A = torch.load(a_ , map_location="cpu" ) else: __A = torch.hub.load_state_dict_from_url(a_ , map_location="cpu" ) __A = upgrade_state_dict(a_ , a_ ) hf_model.load_state_dict(a_ ) __A = hf_model.state_dict() __A = count_parameters(a_ ) __A = count_parameters(a_ ) + count_parameters(a_ ) assert torch.allclose(a_ , a_ , atol=1E-3 ) hf_model.save_pretrained(a_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Any = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to flava checkpoint') parser.add_argument('--codebook_path', default=None, type=str, help='Path to flava codebook checkpoint') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
15
1
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / 'utils')) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 SCREAMING_SNAKE_CASE :Union[str, Any] = get_tests_dir('fixtures') class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self : List[str] ): # A mock response for an HTTP head request to emulate server down __A = mock.Mock() __A = 5_00 __A = {} __A = HTTPError __A = {} # Download this model to make sure it's in the cache. __A = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" ,return_value=A ) as mock_head: __A = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # This check we did call the fake head request mock_head.assert_called() def UpperCamelCase_ ( self : Optional[int] ): # This test is for deprecated behavior and can be removed in v5 __A = WavaVecaFeatureExtractor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json" ) @is_staging_test class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def UpperCamelCase_ ( cls : List[str] ): __A = TOKEN HfFolder.save_token(A ) @classmethod def UpperCamelCase_ ( cls : Union[str, Any] ): try: delete_repo(token=cls._token ,repo_id="test-feature-extractor" ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id="valid_org/test-feature-extractor-org" ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id="test-dynamic-feature-extractor" ) except HTTPError: pass def UpperCamelCase_ ( self : Dict ): __A = WavaVecaFeatureExtractor.from_pretrained(A ) feature_extractor.push_to_hub("test-feature-extractor" ,use_auth_token=self._token ) __A = WavaVecaFeatureExtractor.from_pretrained(f'''{USER}/test-feature-extractor''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(A ,getattr(A ,A ) ) # Reset repo delete_repo(token=self._token ,repo_id="test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( A ,repo_id="test-feature-extractor" ,push_to_hub=A ,use_auth_token=self._token ) __A = WavaVecaFeatureExtractor.from_pretrained(f'''{USER}/test-feature-extractor''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(A ,getattr(A ,A ) ) def UpperCamelCase_ ( self : Optional[Any] ): __A = WavaVecaFeatureExtractor.from_pretrained(A ) feature_extractor.push_to_hub("valid_org/test-feature-extractor" ,use_auth_token=self._token ) __A = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(A ,getattr(A ,A ) ) # Reset repo delete_repo(token=self._token ,repo_id="valid_org/test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( A ,repo_id="valid_org/test-feature-extractor-org" ,push_to_hub=A ,use_auth_token=self._token ) __A = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(A ,getattr(A ,A ) ) def UpperCamelCase_ ( self : Tuple ): CustomFeatureExtractor.register_for_auto_class() __A = CustomFeatureExtractor.from_pretrained(A ) feature_extractor.push_to_hub("test-dynamic-feature-extractor" ,use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map ,{"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"} ,) __A = AutoFeatureExtractor.from_pretrained( f'''{USER}/test-dynamic-feature-extractor''' ,trust_remote_code=A ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ ,"CustomFeatureExtractor" )
15
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Optional[int] = {'vocab_file': 'sentencepiece.bpe.model'} SCREAMING_SNAKE_CASE :Tuple = { 'vocab_file': { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model', } } SCREAMING_SNAKE_CASE :List[Any] = { 'camembert-base': 512, } SCREAMING_SNAKE_CASE :List[str] = '▁' class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[Any] ,A : List[str] ,A : List[Any]="<s>" ,A : Tuple="</s>" ,A : Any="</s>" ,A : Optional[Any]="<s>" ,A : Tuple="<unk>" ,A : str="<pad>" ,A : int="<mask>" ,A : Optional[int]=["<s>NOTUSED", "</s>NOTUSED"] ,A : Optional[Dict[str, Any]] = None ,**A : Optional[Any] ,): # Mask token behave like a normal word, i.e. include the space before it __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,sep_token=A ,cls_token=A ,pad_token=A ,mask_token=A ,additional_special_tokens=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(A ) ) __A = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __A = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __A = len(self.fairseq_tokens_to_ids ) __A = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __A = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCamelCase_ ( self : int ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def UpperCamelCase_ ( self : Union[str, Any] ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase_ ( self : Dict ): return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCamelCase_ ( self : int ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Dict ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(A ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : Tuple ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCamelCase_ ( self : Optional[Any] ,A : Dict ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def __getstate__( self : Dict ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Union[str, Any] ,A : Any ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
15
1
import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient SCREAMING_SNAKE_CASE :int = WebClient(token=os.environ['CI_SLACK_BOT_TOKEN']) def UpperCAmelCase ( a_ ) -> Union[str, Any]: """simple docstring""" __A = test_results.split(" " ) __A = 0 __A = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. __A = expressions[-2] if "=" in expressions[-1] else expressions[-1] for i, expression in enumerate(a_ ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" __A = {} __A = None __A = False for line in failures_short_lines.split("\n" ): if re.search(r"_ \[doctest\]" , a_ ): __A = True __A = line.split(" " )[2] elif in_error and not line.split(" " )[0].isdigit(): __A = line __A = False return failures class UpperCAmelCase : '''simple docstring''' def __init__( self : Optional[Any] ,A : str ,A : Dict ): __A = title __A = doc_test_results["time_spent"].split("," )[0] __A = doc_test_results["success"] __A = doc_test_results["failures"] __A = self.n_success + self.n_failures # Failures and success of the modeling tests __A = doc_test_results @property def UpperCamelCase_ ( self : Optional[int] ): __A = [self._time_spent] __A = 0 for time in time_spent: __A = time.split(":" ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(A ) == 1: __A = [0, 0, time_parts[0]] __A , __A , __A = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 36_00 + minutes * 60 + seconds __A , __A , __A = total_secs // 36_00, (total_secs % 36_00) // 60, total_secs % 60 return f'''{int(A )}h{int(A )}m{int(A )}s''' @property def UpperCamelCase_ ( self : Union[str, Any] ): return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def UpperCamelCase_ ( self : Optional[Any] ): return { "type": "section", "text": { "type": "plain_text", "text": f'''🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.''', "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''', }, } @property def UpperCamelCase_ ( self : Dict ): return { "type": "section", "text": { "type": "plain_text", "text": ( f'''There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in''' f''' {self.time}.''' ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''', }, } @property def UpperCamelCase_ ( self : List[str] ): __A = 40 __A = {k: v["failed"] for k, v in doc_test_results.items() if isinstance(A ,A )} __A = "" for category, failures in category_failures.items(): if len(A ) == 0: continue if report != "": report += "\n\n" report += f'''*{category} failures*:'''.ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(A ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": f'''The following examples had failures:\n\n\n{report}\n''', }, } @property def UpperCamelCase_ ( self : List[Any] ): __A = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(A ) @staticmethod def UpperCamelCase_ ( ): __A = [ { "type": "section", "text": { "type": "plain_text", "text": "There was an issue running the tests.", }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''', }, } ] print("Sending the following payload" ) print(json.dumps({"blocks": json.loads(A )} ) ) client.chat_postMessage( channel=os.environ["CI_SLACK_CHANNEL_ID_DAILY"] ,text="There was an issue running the tests." ,blocks=A ,) def UpperCamelCase_ ( self : Tuple ): print("Sending the following payload" ) print(json.dumps({"blocks": json.loads(self.payload )} ) ) __A = f'''{self.n_failures} failures out of {self.n_tests} tests,''' if self.n_failures else "All tests passed." __A = client.chat_postMessage( channel=os.environ["CI_SLACK_CHANNEL_ID_DAILY"] ,blocks=self.payload ,text=A ,) def UpperCamelCase_ ( self : List[str] ,A : List[str] ,A : List[Any] ,A : Optional[int] ,A : Union[str, Any] ): __A = "" for key, value in failures.items(): __A = value[:2_00] + " [Truncated]" if len(A ) > 2_50 else value failures_text += f'''*{key}*\n_{value}_\n\n''' __A = job_name __A = {"type": "section", "text": {"type": "mrkdwn", "text": text}} if job_link is not None: __A = { "type": "button", "text": {"type": "plain_text", "text": "GitHub Action job", "emoji": True}, "url": job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def UpperCamelCase_ ( self : Tuple ): if self.thread_ts is None: raise ValueError("Can only post reply if a post has been made." ) __A = self.doc_test_results.pop("job_link" ) self.doc_test_results.pop("failures" ) self.doc_test_results.pop("success" ) self.doc_test_results.pop("time_spent" ) __A = sorted(self.doc_test_results.items() ,key=lambda A : t[0] ) for job, job_result in sorted_dict: if len(job_result["failures"] ): __A = f'''*Num failures* :{len(job_result['failed'] )} \n''' __A = job_result["failures"] __A = self.get_reply_blocks(A ,A ,A ,text=A ) print("Sending the following reply" ) print(json.dumps({"blocks": blocks} ) ) client.chat_postMessage( channel=os.environ["CI_SLACK_CHANNEL_ID_DAILY"] ,text=f'''Results for {job}''' ,blocks=A ,thread_ts=self.thread_ts["ts"] ,) time.sleep(1 ) def UpperCAmelCase ( ) -> str: """simple docstring""" __A = os.environ["GITHUB_RUN_ID"] __A = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100''' __A = requests.get(a_ ).json() __A = {} try: jobs.update({job["name"]: job["html_url"] for job in result["jobs"]} ) __A = math.ceil((result["total_count"] - 1_0_0) / 1_0_0 ) for i in range(a_ ): __A = requests.get(url + F'''&page={i + 2}''' ).json() jobs.update({job["name"]: job["html_url"] for job in result["jobs"]} ) return jobs except Exception as e: print("Unknown error, could not fetch links." , a_ ) return {} def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" __A = {} if os.path.exists(a_ ): __A = os.listdir(a_ ) for file in files: try: with open(os.path.join(a_ , a_ ) , encoding="utf-8" ) as f: __A = f.read() except UnicodeDecodeError as e: raise ValueError(F'''Could not open {os.path.join(a_ , a_ )}.''' ) from e return _artifact def UpperCAmelCase ( ) -> List[str]: """simple docstring""" class UpperCAmelCase : '''simple docstring''' def __init__( self : List[Any] ,A : str ): __A = name __A = [] def __str__( self : Tuple ): return self.name def UpperCamelCase_ ( self : Dict ,A : str ): self.paths.append({"name": self.name, "path": path} ) __A = {} __A = filter(os.path.isdir , os.listdir() ) for directory in directories: __A = directory if artifact_name not in _available_artifacts: __A = Artifact(a_ ) _available_artifacts[artifact_name].add_path(a_ ) return _available_artifacts if __name__ == "__main__": SCREAMING_SNAKE_CASE :int = get_job_links() SCREAMING_SNAKE_CASE :Optional[int] = retrieve_available_artifacts() SCREAMING_SNAKE_CASE :Dict = collections.OrderedDict( [ ('*.py', 'API Examples'), ('*.md', 'MD Examples'), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' SCREAMING_SNAKE_CASE :List[Any] = { v: { 'failed': [], 'failures': {}, } for v in docs.values() } # Link to the GitHub Action job SCREAMING_SNAKE_CASE :Union[str, Any] = github_actions_job_links.get('run_doctests') SCREAMING_SNAKE_CASE :Tuple = available_artifacts['doc_tests_gpu_test_reports'].paths[0] SCREAMING_SNAKE_CASE :Any = retrieve_artifact(artifact_path['name']) if "stats" in artifact: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :List[Any] = handle_test_results(artifact['stats']) SCREAMING_SNAKE_CASE :List[str] = failed SCREAMING_SNAKE_CASE :Dict = success SCREAMING_SNAKE_CASE :List[Any] = time_spent[1:-1] + ', ' SCREAMING_SNAKE_CASE :str = extract_first_line_failure(artifact['failures_short']) for line in artifact["summary_short"].split('\n'): if re.search('FAILED', line): SCREAMING_SNAKE_CASE :Optional[int] = line.replace('FAILED ', '') SCREAMING_SNAKE_CASE :Any = line.split()[0].replace('\n', '') if "::" in line: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :Tuple = line.split('::') else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :List[str] = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): SCREAMING_SNAKE_CASE :List[str] = docs[file_regex] doc_test_results[category]["failed"].append(test) SCREAMING_SNAKE_CASE :str = all_failures[test] if test in all_failures else 'N/A' SCREAMING_SNAKE_CASE :Optional[Any] = failure break SCREAMING_SNAKE_CASE :Optional[Any] = Message('🤗 Results of the doc tests.', doc_test_results) message.post() message.post_reply()
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return [tuple(a_ )] __A = [] def generate(a_ , a_ ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , a_ ) for i in range(k - 1 ): if k % 2 == 0: # k is even __A , __A = arr[k - 1], arr[i] else: # k is odd __A , __A = arr[k - 1], arr[0] generate(k - 1 , a_ ) generate(len(a_ ) , a_ ) return res if __name__ == "__main__": SCREAMING_SNAKE_CASE :int = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :Dict = [int(item) for item in user_input.split(',')] print(heaps(arr))
15
1
import math class UpperCAmelCase : '''simple docstring''' def __init__( self : Optional[int] ,A : Dict=0 ): # a graph with Node 0,1,...,N-1 __A = n __A = [ [math.inf for j in range(0 ,A )] for i in range(0 ,A ) ] # adjacency matrix for weight __A = [ [math.inf for j in range(0 ,A )] for i in range(0 ,A ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase_ ( self : List[str] ,A : Tuple ,A : Union[str, Any] ,A : Optional[int] ): __A = w def UpperCamelCase_ ( self : int ): for k in range(0 ,self.n ): for i in range(0 ,self.n ): for j in range(0 ,self.n ): __A = min(self.dp[i][j] ,self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase_ ( self : Optional[int] ,A : str ,A : Union[str, Any] ): return self.dp[u][v] if __name__ == "__main__": SCREAMING_SNAKE_CASE :Optional[int] = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return lst __A = 1 while i < len(a_ ): if lst[i - 1] <= lst[i]: i += 1 else: __A , __A = lst[i], lst[i - 1] i -= 1 if i == 0: __A = 1 return lst if __name__ == "__main__": SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :List[Any] = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
15
1
import itertools from dataclasses import dataclass from typing import Optional import pandas as pd import pyarrow as pa import datasets from datasets.table import table_cast @dataclass class UpperCAmelCase ( datasets.BuilderConfig ): '''simple docstring''' snake_case_ = None class UpperCAmelCase ( datasets.ArrowBasedBuilder ): '''simple docstring''' snake_case_ = PandasConfig def UpperCamelCase_ ( self : List[Any] ): return datasets.DatasetInfo(features=self.config.features ) def UpperCamelCase_ ( self : str ,A : Dict ): if not self.config.data_files: raise ValueError(f'''At least one data file must be specified, but got data_files={self.config.data_files}''' ) __A = dl_manager.download_and_extract(self.config.data_files ) if isinstance(A ,(str, list, tuple) ): __A = data_files if isinstance(A ,A ): __A = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive __A = [dl_manager.iter_files(A ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN ,gen_kwargs={"files": files} )] __A = [] for split_name, files in data_files.items(): if isinstance(A ,A ): __A = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive __A = [dl_manager.iter_files(A ) for file in files] splits.append(datasets.SplitGenerator(name=A ,gen_kwargs={"files": files} ) ) return splits def UpperCamelCase_ ( self : Dict ,A : pa.Table ): if self.config.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example __A = table_cast(A ,self.config.features.arrow_schema ) return pa_table def UpperCamelCase_ ( self : Dict ,A : Union[str, Any] ): for i, file in enumerate(itertools.chain.from_iterable(A ) ): with open(A ,"rb" ) as f: __A = pa.Table.from_pandas(pd.read_pickle(A ) ) yield i, self._cast_table(A )
15
from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 snake_case_ = 42 snake_case_ = None class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 2 @register_to_config def __init__( self : str ,A : float = 0.02 ,A : float = 1_00 ,A : float = 1.0_07 ,A : float = 80 ,A : float = 0.05 ,A : float = 50 ,): # standard deviation of the initial noise distribution __A = sigma_max # setable values __A = None __A = None __A = None # sigma(t_i) def UpperCamelCase_ ( self : str ,A : torch.FloatTensor ,A : Optional[int] = None ): return sample def UpperCamelCase_ ( self : Dict ,A : int ,A : Union[str, torch.device] = None ): __A = num_inference_steps __A = np.arange(0 ,self.num_inference_steps )[::-1].copy() __A = torch.from_numpy(A ).to(A ) __A = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] __A = torch.tensor(A ,dtype=torch.floataa ,device=A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : torch.FloatTensor ,A : float ,A : Optional[torch.Generator] = None ): if self.config.s_min <= sigma <= self.config.s_max: __A = min(self.config.s_churn / self.num_inference_steps ,2**0.5 - 1 ) else: __A = 0 # sample eps ~ N(0, S_noise^2 * I) __A = self.config.s_noise * randn_tensor(sample.shape ,generator=A ).to(sample.device ) __A = sigma + gamma * sigma __A = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def UpperCamelCase_ ( self : Dict ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_hat + sigma_hat * model_output __A = (sample_hat - pred_original_sample) / sigma_hat __A = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : Optional[int] ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_prev + sigma_prev * model_output __A = (sample_prev - pred_original_sample) / sigma_prev __A = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : List[Any] ,A : Dict ,A : List[str] ,A : str ): raise NotImplementedError()
15
1
def UpperCAmelCase ( a_ ) -> float: """simple docstring""" __A = 0 while len(a_ ) > 1: __A = 0 # Consider two files with minimum cost to be merged for _ in range(2 ): __A = files.index(min(a_ ) ) temp += files[min_index] files.pop(a_ ) files.append(a_ ) optimal_merge_cost += temp return optimal_merge_cost if __name__ == "__main__": import doctest doctest.testmod()
15
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version SCREAMING_SNAKE_CASE :Union[str, Any] = get_logger(__name__) class UpperCAmelCase : '''simple docstring''' snake_case_ = "dummy_data" snake_case_ = "datasets" snake_case_ = False def __init__( self : Optional[int] ,A : str ,A : str ,A : Union[Version, str] ,A : Optional[str] = None ,A : bool = False ,A : bool = True ,A : Optional[List[Callable]] = None ,): __A = 0 __A = dataset_name __A = cache_dir __A = use_local_dummy_data __A = config # download_callbacks take a single url as input __A = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root __A = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general __A = str(A ) # to be downloaded __A = None __A = None @property def UpperCamelCase_ ( self : Union[str, Any] ): if self._dummy_file is None: __A = self.download_dummy_data() return self._dummy_file @property def UpperCamelCase_ ( self : Optional[Any] ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("dummy" ,self.config.name ,self.version_name ) # structure is dummy / version_name return os.path.join("dummy" ,self.version_name ) @property def UpperCamelCase_ ( self : List[Any] ): return os.path.join(self.dummy_data_folder ,"dummy_data.zip" ) def UpperCamelCase_ ( self : Tuple ): __A = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) __A = cached_path( A ,cache_dir=self.cache_dir ,extract_compressed_file=A ,force_extract=A ) return os.path.join(A ,self.dummy_file_name ) @property def UpperCamelCase_ ( self : str ): return os.path.join(self.datasets_scripts_dir ,self.dataset_name ,self.dummy_zip_file ) @property def UpperCamelCase_ ( self : Any ): if self._bucket_url is None: __A = hf_github_url(self.dataset_name ,self.dummy_zip_file.replace(os.sep ,"/" ) ) return self._bucket_url @property def UpperCamelCase_ ( self : Tuple ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep ,"/" ).split("/" )[:-1] ) def UpperCamelCase_ ( self : List[str] ,A : List[Any] ,*A : Dict ): if self.load_existing_dummy_data: # dummy data is downloaded and tested __A = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned __A = self.dummy_file_name # special case when data_url is a dict if isinstance(A ,A ): return self.create_dummy_data_dict(A ,A ) elif isinstance(A ,(list, tuple) ): return self.create_dummy_data_list(A ,A ) else: return self.create_dummy_data_single(A ,A ) def UpperCamelCase_ ( self : str ,A : List[Any] ,*A : List[Any] ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : List[str] ,A : List[str] ,A : Tuple ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : Any ,A : Any ,*A : Optional[Any] ,**A : List[str] ): return path def UpperCamelCase_ ( self : str ): return {} def UpperCamelCase_ ( self : int ,A : int ,A : Tuple ): __A = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(A ,A ): for single_url in single_urls: download_callback(A ) else: __A = single_urls download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(A ,A ): __A = [os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) for x in single_urls] else: __A = single_urls __A = os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) __A = value # make sure that values are unique if all(isinstance(A ,A ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique __A = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,A : str ): __A = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one __A = all(bool(re.findall("[0-9]{3,}-of-[0-9]{3,}" ,A ) ) for url in data_url ) __A = all( url.startswith("https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): __A = [data_url[0]] * len(A ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(single_url.split("/" )[-1] ) ) dummy_data_list.append(A ) return dummy_data_list def UpperCamelCase_ ( self : str ,A : List[Any] ,A : Optional[Any] ): for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(data_url.split("/" )[-1] ) ) if os.path.exists(A ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def UpperCamelCase_ ( self : int ): pass def UpperCamelCase_ ( self : Dict ): pass def UpperCamelCase_ ( self : Optional[Any] ,A : List[Any] ): def _iter_archive_members(A : Optional[Any] ): # this preserves the order of the members inside the ZIP archive __A = Path(self.dummy_file ).parent __A = path.relative_to(A ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: __A = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(A ) __A = Path(A ) __A = _iter_archive_members(A ) if self.use_local_dummy_data else path.rglob("*" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((".", "__") ): yield file_path.relative_to(A ).as_posix(), file_path.open("rb" ) def UpperCamelCase_ ( self : List[Any] ,A : Any ): if not isinstance(A ,A ): __A = [paths] for path in paths: if os.path.isfile(A ): if os.path.basename(A ).startswith((".", "__") ): return yield path else: for dirpath, dirnames, filenames in os.walk(A ): if os.path.basename(A ).startswith((".", "__") ): continue dirnames.sort() for filename in sorted(A ): if filename.startswith((".", "__") ): continue yield os.path.join(A ,A )
15
1
from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record SCREAMING_SNAKE_CASE :Optional[int] = '\\n@article{wang2019superglue,\n title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},\n author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},\n journal={arXiv preprint arXiv:1905.00537},\n year={2019}\n}\n' SCREAMING_SNAKE_CASE :Union[str, Any] = '\\nSuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after\nGLUE with a new set of more difficult language understanding tasks, improved\nresources, and a new public leaderboard.\n' SCREAMING_SNAKE_CASE :str = '\nCompute SuperGLUE evaluation metric associated to each SuperGLUE dataset.\nArgs:\n predictions: list of predictions to score. Depending on the SuperGlUE subset:\n - for \'record\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'prediction_text\': the predicted answer text\n - for \'multirc\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question-answer pair as specified by the dataset\n - \'prediction\': the predicted answer label\n - otherwise: list of predicted labels\n references: list of reference labels. Depending on the SuperGLUE subset:\n - for \'record\': list of question-answers dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'answers\': list of possible answers\n - otherwise: list of reference labels\nReturns: depending on the SuperGLUE subset:\n - for \'record\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1\': F1 score\n - for \'multirc\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1_m\': Per-question macro-F1 score\n - \'f1_a\': Average F1 score over all answers\n - for \'axb\':\n \'matthews_correlation\': Matthew Correlation\n - for \'cb\':\n - \'accuracy\': Accuracy\n - \'f1\': F1 score\n - for all others:\n - \'accuracy\': Accuracy\nExamples:\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\') # any of ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\')\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\')\n >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}]\n >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\')\n >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'matthews_correlation\': 1.0}\n' def UpperCAmelCase ( a_ , a_ ) -> Dict: """simple docstring""" return float((preds == labels).mean() ) def UpperCAmelCase ( a_ , a_ , a_="binary" ) -> Union[str, Any]: """simple docstring""" __A = simple_accuracy(a_ , a_ ) __A = float(fa_score(y_true=a_ , y_pred=a_ , average=a_ ) ) return { "accuracy": acc, "f1": fa, } def UpperCAmelCase ( a_ , a_ ) -> Union[str, Any]: """simple docstring""" __A = {} for id_pred, label in zip(a_ , a_ ): __A = F'''{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}''' __A = id_pred["prediction"] if question_id in question_map: question_map[question_id].append((pred, label) ) else: __A = [(pred, label)] __A , __A = [], [] for question, preds_labels in question_map.items(): __A , __A = zip(*a_ ) __A = fa_score(y_true=a_ , y_pred=a_ , average="macro" ) fas.append(a_ ) __A = int(sum(pred == label for pred, label in preds_labels ) == len(a_ ) ) ems.append(a_ ) __A = float(sum(a_ ) / len(a_ ) ) __A = sum(a_ ) / len(a_ ) __A = float(fa_score(y_true=a_ , y_pred=[id_pred["prediction"] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[str] ): if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( "You should supply a configuration name selected in " "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]" ) return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features(self._get_feature_types() ) ,codebase_urls=[] ,reference_urls=[] ,format="numpy" if not self.config_name == "record" and not self.config_name == "multirc" else None ,) def UpperCamelCase_ ( self : List[Any] ): if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value("int64" ), "query": datasets.Value("int64" ), }, "prediction_text": datasets.Value("string" ), }, "references": { "idx": { "passage": datasets.Value("int64" ), "query": datasets.Value("int64" ), }, "answers": datasets.Sequence(datasets.Value("string" ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value("int64" ), "paragraph": datasets.Value("int64" ), "question": datasets.Value("int64" ), }, "prediction": datasets.Value("int64" ), }, "references": datasets.Value("int64" ), } else: return { "predictions": datasets.Value("int64" ), "references": datasets.Value("int64" ), } def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, Any] ,A : Dict ): if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(A ,A )} elif self.config_name == "cb": return acc_and_fa(A ,A ,fa_avg="macro" ) elif self.config_name == "record": __A = [ { "qas": [ {"id": ref["idx"]["query"], "answers": [{"text": ans} for ans in ref["answers"]]} for ref in references ] } ] __A = {pred["idx"]["query"]: pred["prediction_text"] for pred in predictions} return evaluate_record(A ,A )[0] elif self.config_name == "multirc": return evaluate_multirc(A ,A ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(A ,A )} else: raise KeyError( "You should supply a configuration name selected in " "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]" )
15
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available SCREAMING_SNAKE_CASE :List[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :List[str] = ['BartphoTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys SCREAMING_SNAKE_CASE :Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
15
1
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinvaConfig, SwinvaForImageClassification def UpperCAmelCase ( a_ ) -> Optional[Any]: """simple docstring""" __A = SwinvaConfig() __A = swinva_name.split("_" ) __A = name_split[1] if "to" in name_split[3]: __A = int(name_split[3][-3:] ) else: __A = int(name_split[3] ) if "to" in name_split[2]: __A = int(name_split[2][-2:] ) else: __A = int(name_split[2][6:] ) if model_size == "tiny": __A = 9_6 __A = (2, 2, 6, 2) __A = (3, 6, 1_2, 2_4) elif model_size == "small": __A = 9_6 __A = (2, 2, 1_8, 2) __A = (3, 6, 1_2, 2_4) elif model_size == "base": __A = 1_2_8 __A = (2, 2, 1_8, 2) __A = (4, 8, 1_6, 3_2) else: __A = 1_9_2 __A = (2, 2, 1_8, 2) __A = (6, 1_2, 2_4, 4_8) if "to" in swinva_name: __A = (1_2, 1_2, 1_2, 6) if ("22k" in swinva_name) and ("to" not in swinva_name): __A = 2_1_8_4_1 __A = "huggingface/label-files" __A = "imagenet-22k-id2label.json" __A = json.load(open(hf_hub_download(a_ , a_ , repo_type="dataset" ) , "r" ) ) __A = {int(a_ ): v for k, v in idalabel.items()} __A = idalabel __A = {v: k for k, v in idalabel.items()} else: __A = 1_0_0_0 __A = "huggingface/label-files" __A = "imagenet-1k-id2label.json" __A = json.load(open(hf_hub_download(a_ , a_ , repo_type="dataset" ) , "r" ) ) __A = {int(a_ ): v for k, v in idalabel.items()} __A = idalabel __A = {v: k for k, v in idalabel.items()} __A = img_size __A = num_classes __A = embed_dim __A = depths __A = num_heads __A = window_size return config def UpperCAmelCase ( a_ ) -> Optional[Any]: """simple docstring""" if "patch_embed.proj" in name: __A = name.replace("patch_embed.proj" , "embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: __A = name.replace("patch_embed.norm" , "embeddings.norm" ) if "layers" in name: __A = "encoder." + name if "attn.proj" in name: __A = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name: __A = name.replace("attn" , "attention.self" ) if "norm1" in name: __A = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: __A = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: __A = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __A = name.replace("mlp.fc2" , "output.dense" ) if "q_bias" in name: __A = name.replace("q_bias" , "query.bias" ) if "k_bias" in name: __A = name.replace("k_bias" , "key.bias" ) if "v_bias" in name: __A = name.replace("v_bias" , "value.bias" ) if "cpb_mlp" in name: __A = name.replace("cpb_mlp" , "continuous_position_bias_mlp" ) if name == "norm.weight": __A = "layernorm.weight" if name == "norm.bias": __A = "layernorm.bias" if "head" in name: __A = name.replace("head" , "classifier" ) else: __A = "swinv2." + name return name def UpperCAmelCase ( a_ , a_ ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): __A = orig_state_dict.pop(a_ ) if "mask" in key: continue elif "qkv" in key: __A = key.split("." ) __A = int(key_split[1] ) __A = int(key_split[3] ) __A = model.swinva.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: __A = val[:dim, :] __A = val[dim : dim * 2, :] __A = val[-dim:, :] else: __A = val[:dim] __A = val[ dim : dim * 2 ] __A = val[-dim:] else: __A = val return orig_state_dict def UpperCAmelCase ( a_ , a_ ) -> Tuple: """simple docstring""" __A = timm.create_model(a_ , pretrained=a_ ) timm_model.eval() __A = get_swinva_config(a_ ) __A = SwinvaForImageClassification(a_ ) model.eval() __A = convert_state_dict(timm_model.state_dict() , a_ ) model.load_state_dict(a_ ) __A = "http://images.cocodataset.org/val2017/000000039769.jpg" __A = AutoImageProcessor.from_pretrained("microsoft/{}".format(swinva_name.replace("_" , "-" ) ) ) __A = Image.open(requests.get(a_ , stream=a_ ).raw ) __A = image_processor(images=a_ , return_tensors="pt" ) __A = timm_model(inputs["pixel_values"] ) __A = model(**a_ ).logits assert torch.allclose(a_ , a_ , atol=1E-3 ) print(F'''Saving model {swinva_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(a_ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(a_ ) model.push_to_hub( repo_path_or_name=Path(a_ , a_ ) , organization="nandwalritik" , commit_message="Add model" , ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '--swinv2_name', default='swinv2_tiny_patch4_window8_256', type=str, help='Name of the Swinv2 timm 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.' ) SCREAMING_SNAKE_CASE :List[Any] = parser.parse_args() convert_swinva_checkpoint(args.swinva_name, args.pytorch_dump_folder_path)
15
from typing import Dict, Optional import numpy as np import datasets SCREAMING_SNAKE_CASE :List[Any] = '\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n' SCREAMING_SNAKE_CASE :List[str] = '\nArgs:\n predictions (`List[ndarray]`):\n List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n references (`List[ndarray]`):\n List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n num_labels (`int`):\n Number of classes (categories).\n ignore_index (`int`):\n Index that will be ignored during evaluation.\n nan_to_num (`int`, *optional*):\n If specified, NaN values will be replaced by the number defined by the user.\n label_map (`dict`, *optional*):\n If specified, dictionary mapping old label indices to new label indices.\n reduce_labels (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n `Dict[str, float | ndarray]` comprising various elements:\n - *mean_iou* (`float`):\n Mean Intersection-over-Union (IoU averaged over all categories).\n - *mean_accuracy* (`float`):\n Mean accuracy (averaged over all categories).\n - *overall_accuracy* (`float`):\n Overall accuracy on all images.\n - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n Per category accuracy.\n - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n Per category IoU.\n\nExamples:\n\n >>> import numpy as np\n\n >>> mean_iou = datasets.load_metric("mean_iou")\n\n >>> # suppose one has 3 different segmentation maps predicted\n >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n >>> predicted = [predicted_1, predicted_2, predicted_3]\n >>> ground_truth = [actual_1, actual_2, actual_3]\n\n >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), \'per_category_accuracy\': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])}\n' SCREAMING_SNAKE_CASE :str = '\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}' def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Tuple: """simple docstring""" if label_map is not None: for old_id, new_id in label_map.items(): __A = new_id # turn into Numpy arrays __A = np.array(a_ ) __A = np.array(a_ ) if reduce_labels: __A = 2_5_5 __A = label - 1 __A = 2_5_5 __A = label != ignore_index __A = np.not_equal(a_ , a_ ) __A = pred_label[mask] __A = np.array(a_ )[mask] __A = pred_label[pred_label == label] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Union[str, Any]: """simple docstring""" __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(a_ , a_ ): __A , __A , __A , __A = intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = None , a_ = False , ) -> str: """simple docstring""" __A , __A , __A , __A = total_intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) # compute metrics __A = {} __A = total_area_intersect.sum() / total_area_label.sum() __A = total_area_intersect / total_area_union __A = total_area_intersect / total_area_label __A = np.nanmean(a_ ) __A = np.nanmean(a_ ) __A = all_acc __A = iou __A = acc if nan_to_num is not None: __A = {metric: np.nan_to_num(a_ , nan=a_ ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { "predictions": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), "references": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), } ) ,reference_urls=[ "https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py" ] ,) def UpperCamelCase_ ( self : int ,A : Optional[Any] ,A : Optional[Any] ,A : int ,A : bool ,A : Optional[int] = None ,A : Optional[Dict[int, int]] = None ,A : bool = False ,): __A = mean_iou( results=A ,gt_seg_maps=A ,num_labels=A ,ignore_index=A ,nan_to_num=A ,label_map=A ,reduce_labels=A ,) return iou_result
15
1
class UpperCAmelCase : '''simple docstring''' def __init__( self : Any ): __A = {} # Mapping from char to TrieNode __A = False def UpperCamelCase_ ( self : List[str] ,A : list[str] ): for word in words: self.insert(A ) def UpperCamelCase_ ( self : Optional[int] ,A : str ): __A = self for char in word: if char not in curr.nodes: __A = TrieNode() __A = curr.nodes[char] __A = True def UpperCamelCase_ ( self : int ,A : str ): __A = self for char in word: if char not in curr.nodes: return False __A = curr.nodes[char] return curr.is_leaf def UpperCamelCase_ ( self : Dict ,A : str ): def _delete(A : TrieNode ,A : str ,A : int ) -> bool: if index == len(A ): # If word does not exist if not curr.is_leaf: return False __A = False return len(curr.nodes ) == 0 __A = word[index] __A = curr.nodes.get(A ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted __A = _delete(A ,A ,index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self ,A ,0 ) def UpperCAmelCase ( a_ , a_ ) -> None: """simple docstring""" if node.is_leaf: print(a_ , end=" " ) for key, value in node.nodes.items(): print_words(a_ , word + key ) def UpperCAmelCase ( ) -> bool: """simple docstring""" __A = "banana bananas bandana band apple all beast".split() __A = TrieNode() root.insert_many(a_ ) # print_words(root, "") assert all(root.find(a_ ) for word in words ) assert root.find("banana" ) assert not root.find("bandanas" ) assert not root.find("apps" ) assert root.find("apple" ) assert root.find("all" ) root.delete("all" ) assert not root.find("all" ) root.delete("banana" ) assert not root.find("banana" ) assert root.find("bananas" ) return True def UpperCAmelCase ( a_ , a_ ) -> None: """simple docstring""" print(str(a_ ) , "works!" if passes else "doesn't work :(" ) def UpperCAmelCase ( ) -> None: """simple docstring""" assert test_trie() def UpperCAmelCase ( ) -> None: """simple docstring""" print_results("Testing trie functionality" , test_trie() ) if __name__ == "__main__": main()
15
import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE :List[str] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[str] = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Dict = { 'vocab_file': { 'AI-Sweden/gpt-sw3-126m': 'https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-350m': 'https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-1.6b': 'https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-6.7b': 'https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-20b': 'https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model', } } SCREAMING_SNAKE_CASE :Optional[Any] = { 'AI-Sweden/gpt-sw3-126m': 2048, 'AI-Sweden/gpt-sw3-350m': 2048, 'AI-Sweden/gpt-sw3-1.6b': 2048, 'AI-Sweden/gpt-sw3-6.7b': 2048, 'AI-Sweden/gpt-sw3-20b': 2048, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[int] ,A : Optional[Any] ,A : Optional[int]=False ,A : int=False ,A : Union[str, Any]=False ,A : int=None ,A : Optional[Any]=None ,A : Union[str, Any]=None ,A : Optional[Any]=None ,A : Optional[Dict[str, Any]] = None ,**A : Tuple ,): __A = {} if sp_model_kwargs is None else sp_model_kwargs __A = kwargs.get("name_or_path" ) if name_or_path is None: logger.warning( "name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b," " you are testing the model, this can safely be ignored" ) __A = "None" # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing __A = "<|endoftext|>" if eos_token is None else eos_token __A = "<unk>" if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: __A = unk_token if pad_token is None else pad_token __A = eos_token if bos_token is None else bos_token else: __A = "<pad>" if pad_token is None else pad_token __A = "<s>" if bos_token is None else bos_token super().__init__( do_lower_case=A ,remove_space=A ,keep_accents=A ,bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = do_lower_case __A = remove_space __A = keep_accents __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) # Used for whitespace normalization in input texts # fmt : off __A = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", "„"} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing __A = re.compile( f'''[{''.join(map(A ,list(range(0 ,9 ) ) + list(range(11 ,32 ) ) + list(range(1_27 ,1_60 ) ) + [1_60, 1_73, 82_03] ) )}]''' ) def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Optional[Any] ,A : Union[str, Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def UpperCamelCase_ ( self : List[str] ): return len(self.sp_model ) def UpperCamelCase_ ( self : int ,A : str ): __A = self.non_printing_characters_re.sub("" ,A ) # Normalize whitespaces __A = "".join([char if char not in self.whitespaces else " " for char in text] ) # NFC Unicode normalization __A = unicodedata.normalize("NFC" ,A ) return text def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,**A : Optional[int] ): __A = self.preprocess_text(A ) return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : int ): return self.sp_model.IdToPiece(A ) @staticmethod def UpperCamelCase_ ( A : str ): return out_string def UpperCamelCase_ ( self : str ,A : List[str] ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string def UpperCamelCase_ ( self : str ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : List[str] ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, List[str]] ,A : Union[str, bool] = False ): if isinstance(A ,A ): __A = self.preprocess_text(A ) __A = self.sp_model.encode(A ) else: __A = [self.preprocess_text(A ) for t in text] __A = self.sp_model.encode(A ) if return_tensors is True or return_tensors == "pt": __A = torch.tensor(A ) return token_ids def UpperCamelCase_ ( self : List[Any] ,A : Union[int, List[int]] ): return self.sp_model.decode(A ) def UpperCamelCase_ ( self : List[str] ,A : "Conversation" ): __A = [f'''User: {text}''' if is_user else f'''Bot: {text}''' for is_user, text in conversation.iter_texts()] __A = ( f'''{self.eos_token}{self.bos_token}''' + f'''{self.bos_token}'''.join(A ) + f'''{self.bos_token}Bot:''' ) return self.encode(text=A )
15
1
import argparse import math import traceback import dateutil.parser as date_parser import requests def UpperCAmelCase ( a_ ) -> str: """simple docstring""" __A = {} __A = job["started_at"] __A = job["completed_at"] __A = date_parser.parse(a_ ) __A = date_parser.parse(a_ ) __A = round((end_datetime - start_datetime).total_seconds() / 60.0 ) __A = start __A = end __A = duration_in_min return job_info def UpperCAmelCase ( a_ , a_=None ) -> str: """simple docstring""" __A = None if token is not None: __A = {"Accept": "application/vnd.github+json", "Authorization": F'''Bearer {token}'''} __A = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' __A = requests.get(a_ , headers=a_ ).json() __A = {} try: job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) __A = math.ceil((result["total_count"] - 1_0_0) / 1_0_0 ) for i in range(a_ ): __A = requests.get(url + F'''&page={i + 2}''' , headers=a_ ).json() job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) return job_time except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": SCREAMING_SNAKE_CASE :Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() SCREAMING_SNAKE_CASE :Union[str, Any] = get_job_time(args.workflow_run_id) SCREAMING_SNAKE_CASE :Optional[int] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f'''{k}: {v["duration"]}''')
15
import numpy as np def UpperCAmelCase ( a_ , a_ , a_ = 1E-12 , a_ = 1_0_0 , ) -> tuple[float, np.ndarray]: """simple docstring""" assert np.shape(a_ )[0] == np.shape(a_ )[1] # Ensure proper dimensionality. assert np.shape(a_ )[0] == np.shape(a_ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(a_ ) == np.iscomplexobj(a_ ) __A = np.iscomplexobj(a_ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(a_ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. __A = False __A = 0 __A = 0 __A = 1E12 while not convergence: # Multiple matrix by the vector. __A = np.dot(a_ , a_ ) # Normalize the resulting output vector. __A = w / np.linalg.norm(a_ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) __A = vector.conj().T if is_complex else vector.T __A = np.dot(a_ , np.dot(a_ , a_ ) ) # Check convergence. __A = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: __A = True __A = lambda_ if is_complex: __A = np.real(lambda_ ) return lambda_, vector def UpperCAmelCase ( ) -> None: """simple docstring""" __A = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) __A = np.array([4_1, 4, 2_0] ) __A = real_input_matrix.astype(np.complexaaa ) __A = np.triu(1J * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T __A = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": __A = real_input_matrix __A = real_vector elif problem_type == "complex": __A = complex_input_matrix __A = complex_vector # Our implementation. __A , __A = power_iteration(a_ , a_ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). __A , __A = np.linalg.eigh(a_ ) # Last eigenvalue is the maximum one. __A = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. __A = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1E-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(a_ ) - np.abs(a_ ) ) <= 1E-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
15
1
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...image_processing_utils import ImageProcessingMixin from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) SCREAMING_SNAKE_CASE :Union[str, Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[Any] = OrderedDict( [ ('align', 'EfficientNetImageProcessor'), ('beit', 'BeitImageProcessor'), ('bit', 'BitImageProcessor'), ('blip', 'BlipImageProcessor'), ('blip-2', 'BlipImageProcessor'), ('bridgetower', 'BridgeTowerImageProcessor'), ('chinese_clip', 'ChineseCLIPImageProcessor'), ('clip', 'CLIPImageProcessor'), ('clipseg', 'ViTImageProcessor'), ('conditional_detr', 'ConditionalDetrImageProcessor'), ('convnext', 'ConvNextImageProcessor'), ('convnextv2', 'ConvNextImageProcessor'), ('cvt', 'ConvNextImageProcessor'), ('data2vec-vision', 'BeitImageProcessor'), ('deformable_detr', 'DeformableDetrImageProcessor'), ('deit', 'DeiTImageProcessor'), ('deta', 'DetaImageProcessor'), ('detr', 'DetrImageProcessor'), ('dinat', 'ViTImageProcessor'), ('donut-swin', 'DonutImageProcessor'), ('dpt', 'DPTImageProcessor'), ('efficientformer', 'EfficientFormerImageProcessor'), ('efficientnet', 'EfficientNetImageProcessor'), ('flava', 'FlavaImageProcessor'), ('focalnet', 'BitImageProcessor'), ('git', 'CLIPImageProcessor'), ('glpn', 'GLPNImageProcessor'), ('groupvit', 'CLIPImageProcessor'), ('imagegpt', 'ImageGPTImageProcessor'), ('instructblip', 'BlipImageProcessor'), ('layoutlmv2', 'LayoutLMv2ImageProcessor'), ('layoutlmv3', 'LayoutLMv3ImageProcessor'), ('levit', 'LevitImageProcessor'), ('mask2former', 'Mask2FormerImageProcessor'), ('maskformer', 'MaskFormerImageProcessor'), ('mgp-str', 'ViTImageProcessor'), ('mobilenet_v1', 'MobileNetV1ImageProcessor'), ('mobilenet_v2', 'MobileNetV2ImageProcessor'), ('mobilevit', 'MobileViTImageProcessor'), ('mobilevit', 'MobileViTImageProcessor'), ('mobilevitv2', 'MobileViTImageProcessor'), ('nat', 'ViTImageProcessor'), ('oneformer', 'OneFormerImageProcessor'), ('owlvit', 'OwlViTImageProcessor'), ('perceiver', 'PerceiverImageProcessor'), ('pix2struct', 'Pix2StructImageProcessor'), ('poolformer', 'PoolFormerImageProcessor'), ('regnet', 'ConvNextImageProcessor'), ('resnet', 'ConvNextImageProcessor'), ('sam', 'SamImageProcessor'), ('segformer', 'SegformerImageProcessor'), ('swiftformer', 'ViTImageProcessor'), ('swin', 'ViTImageProcessor'), ('swin2sr', 'Swin2SRImageProcessor'), ('swinv2', 'ViTImageProcessor'), ('table-transformer', 'DetrImageProcessor'), ('timesformer', 'VideoMAEImageProcessor'), ('tvlt', 'TvltImageProcessor'), ('upernet', 'SegformerImageProcessor'), ('van', 'ConvNextImageProcessor'), ('videomae', 'VideoMAEImageProcessor'), ('vilt', 'ViltImageProcessor'), ('vit', 'ViTImageProcessor'), ('vit_hybrid', 'ViTHybridImageProcessor'), ('vit_mae', 'ViTImageProcessor'), ('vit_msn', 'ViTImageProcessor'), ('xclip', 'CLIPImageProcessor'), ('yolos', 'YolosImageProcessor'), ] ) SCREAMING_SNAKE_CASE :Optional[int] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES) def UpperCAmelCase ( a_ ) -> Tuple: """simple docstring""" for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): if class_name in extractors: __A = model_type_to_module_name(a_ ) __A = importlib.import_module(F'''.{module_name}''' , "transformers.models" ) try: return getattr(a_ , a_ ) except AttributeError: continue for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items(): if getattr(a_ , "__name__" , a_ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. __A = importlib.import_module("transformers" ) if hasattr(a_ , a_ ): return getattr(a_ , a_ ) return None def UpperCAmelCase ( a_ , a_ = None , a_ = False , a_ = False , a_ = None , a_ = None , a_ = None , a_ = False , **a_ , ) -> List[str]: """simple docstring""" __A = get_file_from_repo( a_ , a_ , cache_dir=a_ , force_download=a_ , resume_download=a_ , proxies=a_ , use_auth_token=a_ , revision=a_ , local_files_only=a_ , ) if resolved_config_file is None: logger.info( "Could not locate the image processor configuration file, will try to use the model config instead." ) return {} with open(a_ , encoding="utf-8" ) as reader: return json.load(a_ ) class UpperCAmelCase : '''simple docstring''' def __init__( self : Optional[int] ): raise EnvironmentError( "AutoImageProcessor is designed to be instantiated " "using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method." ) @classmethod @replace_list_option_in_docstrings(A ) def UpperCamelCase_ ( cls : Union[str, Any] ,A : Tuple ,**A : Optional[int] ): __A = kwargs.pop("config" ,A ) __A = kwargs.pop("trust_remote_code" ,A ) __A = True __A , __A = ImageProcessingMixin.get_image_processor_dict(A ,**A ) __A = config_dict.get("image_processor_type" ,A ) __A = None if "AutoImageProcessor" in config_dict.get("auto_map" ,{} ): __A = config_dict["auto_map"]["AutoImageProcessor"] # If we still don't have the image processor class, check if we're loading from a previous feature extractor config # and if so, infer the image processor class from there. if image_processor_class is None and image_processor_auto_map is None: __A = config_dict.pop("feature_extractor_type" ,A ) if feature_extractor_class is not None: logger.warning( "Could not find image processor class in the image processor config or the model config. Loading" " based on pattern matching with the model's feature extractor configuration." ) __A = feature_extractor_class.replace("FeatureExtractor" ,"ImageProcessor" ) if "AutoFeatureExtractor" in config_dict.get("auto_map" ,{} ): __A = config_dict["auto_map"]["AutoFeatureExtractor"] __A = feature_extractor_auto_map.replace("FeatureExtractor" ,"ImageProcessor" ) logger.warning( "Could not find image processor auto map in the image processor config or the model config." " Loading based on pattern matching with the model's feature extractor configuration." ) # If we don't find the image processor class in the image processor config, let's try the model config. if image_processor_class is None and image_processor_auto_map is None: if not isinstance(A ,A ): __A = AutoConfig.from_pretrained(A ,**A ) # It could be in `config.image_processor_type`` __A = getattr(A ,"image_processor_type" ,A ) if hasattr(A ,"auto_map" ) and "AutoImageProcessor" in config.auto_map: __A = config.auto_map["AutoImageProcessor"] if image_processor_class is not None: __A = image_processor_class_from_name(A ) __A = image_processor_auto_map is not None __A = image_processor_class is not None or type(A ) in IMAGE_PROCESSOR_MAPPING __A = resolve_trust_remote_code( A ,A ,A ,A ) if has_remote_code and trust_remote_code: __A = get_class_from_dynamic_module( A ,A ,**A ) __A = kwargs.pop("code_revision" ,A ) if os.path.isdir(A ): image_processor_class.register_for_auto_class() return image_processor_class.from_dict(A ,**A ) elif image_processor_class is not None: return image_processor_class.from_dict(A ,**A ) # Last try: we use the IMAGE_PROCESSOR_MAPPING. elif type(A ) in IMAGE_PROCESSOR_MAPPING: __A = IMAGE_PROCESSOR_MAPPING[type(A )] return image_processor_class.from_dict(A ,**A ) raise ValueError( f'''Unrecognized image processor in {pretrained_model_name_or_path}. Should have a ''' f'''`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following ''' f'''`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def UpperCamelCase_ ( A : Optional[Any] ,A : Any ): IMAGE_PROCESSOR_MAPPING.register(A ,A )
15
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig SCREAMING_SNAKE_CASE :str = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE :str = 'RegNetConfig' # Base docstring SCREAMING_SNAKE_CASE :List[str] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Union[str, Any] = [1, 1088, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE :Optional[int] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Any = 'tabby, tabby cat' SCREAMING_SNAKE_CASE :Optional[int] = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : int ,A : int = 3 ,A : int = 1 ,A : int = 1 ,A : Optional[str] = "relu" ,**A : Dict ,): super().__init__(**A ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb __A = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=A ,strides=A ,padding="VALID" ,groups=A ,use_bias=A ,name="convolution" ,) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) __A = ACTaFN[activation] if activation is not None else tf.identity def UpperCamelCase_ ( self : List[Any] ,A : Any ): __A = self.convolution(self.padding(A ) ) __A = self.normalization(A ) __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : RegNetConfig ,**A : str ): super().__init__(**A ) __A = config.num_channels __A = TFRegNetConvLayer( out_channels=config.embedding_size ,kernel_size=3 ,stride=2 ,activation=config.hidden_act ,name="embedder" ,) def UpperCamelCase_ ( self : Tuple ,A : Optional[Any] ): __A = shape_list(A )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( "Make sure that the channel dimension of the pixel values match with the one set in the configuration." ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) __A = tf.transpose(A ,perm=(0, 2, 3, 1) ) __A = self.embedder(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Optional[int] ,A : int ,A : int = 2 ,**A : Tuple ): super().__init__(**A ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=1 ,strides=A ,use_bias=A ,name="convolution" ) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) def UpperCamelCase_ ( self : Union[str, Any] ,A : tf.Tensor ,A : bool = False ): return self.normalization(self.convolution(A ) ,training=A ) class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Dict ,A : int ,A : int ,**A : str ): super().__init__(**A ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) __A = [ tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="relu" ,name="attention.0" ), tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="sigmoid" ,name="attention.2" ), ] def UpperCamelCase_ ( self : Dict ,A : List[Any] ): # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] __A = self.pooler(A ) for layer_module in self.attention: __A = layer_module(A ) __A = hidden_state * pooled return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : Optional[int] ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.2" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : int ,A : Optional[int] ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : str ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetSELayer(A ,reduced_channels=int(round(in_channels / 4 ) ) ,name="layer.2" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.3" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : Dict ,A : Any ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 2 ,A : int = 2 ,**A : Optional[int] ): super().__init__(**A ) __A = TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer __A = [ # downsampling is done in the first layer with stride of 2 layer(A ,A ,A ,stride=A ,name="layers.0" ), *[layer(A ,A ,A ,name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCamelCase_ ( self : Any ,A : List[str] ): for layer_module in self.layers: __A = layer_module(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Any ,A : RegNetConfig ,**A : List[str] ): super().__init__(**A ) __A = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A ,config.embedding_size ,config.hidden_sizes[0] ,stride=2 if config.downsample_in_first_stage else 1 ,depth=config.depths[0] ,name="stages.0" ,) ) __A = zip(config.hidden_sizes ,config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A ,config.depths[1:] ) ): self.stages.append(TFRegNetStage(A ,A ,A ,depth=A ,name=f'''stages.{i+1}''' ) ) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor ,A : bool = False ,A : bool = True ): __A = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: __A = hidden_states + (hidden_state,) __A = stage_module(A ) if output_hidden_states: __A = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A ,hidden_states=A ) @keras_serializable class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' snake_case_ = RegNetConfig def __init__( self : int ,A : Optional[int] ,**A : Dict ): super().__init__(**A ) __A = config __A = TFRegNetEmbeddings(A ,name="embedder" ) __A = TFRegNetEncoder(A ,name="encoder" ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) @unpack_inputs def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : bool = False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.embedder(A ,training=A ) __A = self.encoder( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = encoder_outputs[0] __A = self.pooler(A ) # Change to NCHW output format have uniformity in the modules __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: __A = tuple([tf.transpose(A ,perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A ,pooler_output=A ,hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states ,) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = RegNetConfig snake_case_ = "regnet" snake_case_ = "pixel_values" @property def UpperCamelCase_ ( self : Optional[Any] ): return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24) ,dtype=tf.floataa )} SCREAMING_SNAKE_CASE :Dict = R'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE :Dict = R'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare RegNet model outputting raw features without any specific head on top." , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,*A : List[Any] ,**A : str ): super().__init__(A ,*A ,**A ) __A = TFRegNetMainLayer(A ,name="regnet" ) @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC ,output_type=A ,config_class=_CONFIG_FOR_DOC ,modality="vision" ,expected_output=_EXPECTED_OUTPUT_SHAPE ,) def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : int=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( pixel_values=A ,output_hidden_states=A ,return_dict=A ,training=A ,) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state ,pooler_output=outputs.pooler_output ,hidden_states=outputs.hidden_states ,) @add_start_docstrings( "\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[int] ,A : RegNetConfig ,*A : str ,**A : Tuple ): super().__init__(A ,*A ,**A ) __A = config.num_labels __A = TFRegNetMainLayer(A ,name="regnet" ) # classification head __A = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels ,name="classifier.1" ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT ,output_type=A ,config_class=_CONFIG_FOR_DOC ,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor = None ,A : tf.Tensor = None ,A : bool = None ,A : bool = None ,A : Union[str, Any]=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = outputs.pooler_output if return_dict else outputs[1] __A = self.classifier[0](A ) __A = self.classifier[1](A ) __A = None if labels is None else self.hf_compute_loss(labels=A ,logits=A ) if not return_dict: __A = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A ,logits=A ,hidden_states=outputs.hidden_states )
15
1
import os import tempfile import unittest from transformers import NezhaConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, NezhaModel, ) from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCAmelCase : '''simple docstring''' def __init__( self : List[str] ,A : List[str] ,A : str=13 ,A : str=7 ,A : List[str]=True ,A : Optional[int]=True ,A : str=True ,A : Dict=True ,A : Optional[int]=99 ,A : Optional[Any]=32 ,A : int=5 ,A : Dict=4 ,A : Optional[int]=37 ,A : Tuple="gelu" ,A : List[str]=0.1 ,A : List[str]=0.1 ,A : Any=1_28 ,A : str=32 ,A : Any=16 ,A : List[Any]=2 ,A : List[str]=0.02 ,A : Tuple=3 ,A : Optional[int]=4 ,A : Any=None ,): __A = parent __A = batch_size __A = seq_length __A = is_training __A = use_input_mask __A = use_token_type_ids __A = use_labels __A = vocab_size __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = type_vocab_size __A = type_sequence_label_size __A = initializer_range __A = num_labels __A = num_choices __A = scope def UpperCamelCase_ ( self : Tuple ): __A = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) __A = None if self.use_input_mask: __A = random_attention_mask([self.batch_size, self.seq_length] ) __A = None if self.use_token_type_ids: __A = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) __A = None __A = None __A = None if self.use_labels: __A = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) __A = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) __A = ids_tensor([self.batch_size] ,self.num_choices ) __A = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase_ ( self : int ): return NezhaConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,is_decoder=A ,initializer_range=self.initializer_range ,) def UpperCamelCase_ ( self : Tuple ): ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = self.prepare_config_and_inputs() __A = True __A = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __A = ids_tensor([self.batch_size, self.seq_length] ,vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def UpperCamelCase_ ( self : str ,A : Tuple ,A : List[str] ,A : List[Any] ,A : Optional[Any] ,A : Optional[Any] ,A : int ,A : Optional[int] ): __A = NezhaModel(config=A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,token_type_ids=A ) __A = model(A ,token_type_ids=A ) __A = model(A ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def UpperCamelCase_ ( self : Optional[int] ,A : Optional[int] ,A : Union[str, Any] ,A : Any ,A : Any ,A : Dict ,A : Union[str, Any] ,A : List[str] ,A : List[Any] ,A : Union[str, Any] ,): __A = True __A = NezhaModel(A ) model.to(A ) model.eval() __A = model( A ,attention_mask=A ,token_type_ids=A ,encoder_hidden_states=A ,encoder_attention_mask=A ,) __A = model( A ,attention_mask=A ,token_type_ids=A ,encoder_hidden_states=A ,) __A = model(A ,attention_mask=A ,token_type_ids=A ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def UpperCamelCase_ ( self : int ,A : int ,A : Any ,A : List[str] ,A : Tuple ,A : Optional[int] ,A : Any ,A : Union[str, Any] ): __A = NezhaForMaskedLM(config=A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,token_type_ids=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase_ ( self : Union[str, Any] ,A : List[str] ,A : List[Any] ,A : Tuple ,A : Union[str, Any] ,A : Tuple ,A : Union[str, Any] ,A : List[Any] ): __A = NezhaForNextSentencePrediction(config=A ) model.to(A ) model.eval() __A = model( A ,attention_mask=A ,token_type_ids=A ,labels=A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, 2) ) def UpperCamelCase_ ( self : Dict ,A : int ,A : List[Any] ,A : Optional[Any] ,A : Union[str, Any] ,A : Any ,A : Tuple ,A : Any ): __A = NezhaForPreTraining(config=A ) model.to(A ) model.eval() __A = model( A ,attention_mask=A ,token_type_ids=A ,labels=A ,next_sentence_label=A ,) 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 : Optional[int] ,A : int ,A : Any ,A : Optional[Any] ,A : Tuple ,A : Optional[Any] ,A : List[Any] ,A : List[Any] ): __A = NezhaForQuestionAnswering(config=A ) model.to(A ) model.eval() __A = model( A ,attention_mask=A ,token_type_ids=A ,start_positions=A ,end_positions=A ,) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def UpperCamelCase_ ( self : List[Any] ,A : List[Any] ,A : Optional[Any] ,A : List[Any] ,A : Union[str, Any] ,A : Tuple ,A : Tuple ,A : List[str] ): __A = self.num_labels __A = NezhaForSequenceClassification(A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,token_type_ids=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def UpperCamelCase_ ( self : str ,A : List[Any] ,A : Optional[Any] ,A : Union[str, Any] ,A : Tuple ,A : List[str] ,A : Union[str, Any] ,A : Optional[Any] ): __A = self.num_labels __A = NezhaForTokenClassification(config=A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,token_type_ids=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase_ ( self : Union[str, Any] ,A : List[str] ,A : Any ,A : str ,A : Optional[int] ,A : Tuple ,A : Union[str, Any] ,A : Any ): __A = self.num_choices __A = NezhaForMultipleChoice(config=A ) model.to(A ) model.eval() __A = input_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() __A = token_type_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() __A = input_mask.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() __A = model( A ,attention_mask=A ,token_type_ids=A ,labels=A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def UpperCamelCase_ ( self : Dict ): __A = self.prepare_config_and_inputs() ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = config_and_inputs __A = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = ( ( NezhaModel, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, ) if is_torch_available() else () ) snake_case_ = ( { "feature-extraction": NezhaModel, "fill-mask": NezhaForMaskedLM, "question-answering": NezhaForQuestionAnswering, "text-classification": NezhaForSequenceClassification, "token-classification": NezhaForTokenClassification, "zero-shot": NezhaForSequenceClassification, } if is_torch_available() else {} ) snake_case_ = True def UpperCamelCase_ ( self : Tuple ,A : Any ,A : Union[str, Any] ,A : int=False ): __A = super()._prepare_for_class(A ,A ,return_labels=A ) if return_labels: if model_class in get_values(A ): __A = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) ,dtype=torch.long ,device=A ) __A = torch.zeros( self.model_tester.batch_size ,dtype=torch.long ,device=A ) return inputs_dict def UpperCamelCase_ ( self : int ): __A = NezhaModelTester(self ) __A = ConfigTester(self ,config_class=A ,hidden_size=37 ) def UpperCamelCase_ ( self : str ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : int ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def UpperCamelCase_ ( self : Dict ): __A = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*A ) def UpperCamelCase_ ( self : Dict ): # This regression test was failing with PyTorch < 1.3 ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = self.model_tester.prepare_config_and_inputs_for_decoder() __A = None self.model_tester.create_and_check_model_as_decoder( A ,A ,A ,A ,A ,A ,A ,A ,A ,) def UpperCamelCase_ ( self : List[Any] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A ) def UpperCamelCase_ ( self : Union[str, Any] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A ) def UpperCamelCase_ ( self : int ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_next_sequence_prediction(*A ) def UpperCamelCase_ ( self : Any ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*A ) def UpperCamelCase_ ( self : Optional[int] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A ) def UpperCamelCase_ ( self : List[Any] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A ) def UpperCamelCase_ ( self : Dict ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A ) @slow def UpperCamelCase_ ( self : int ): for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __A = NezhaModel.from_pretrained(A ) self.assertIsNotNone(A ) @slow @require_torch_gpu def UpperCamelCase_ ( self : Tuple ): __A , __A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # NezhaForMultipleChoice behaves incorrectly in JIT environments. if model_class == NezhaForMultipleChoice: return __A = True __A = model_class(config=A ) __A = self._prepare_for_class(A ,A ) __A = torch.jit.trace( A ,(inputs_dict["input_ids"].to("cpu" ), inputs_dict["attention_mask"].to("cpu" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(A ,os.path.join(A ,"bert.pt" ) ) __A = torch.jit.load(os.path.join(A ,"bert.pt" ) ,map_location=A ) loaded(inputs_dict["input_ids"].to(A ) ,inputs_dict["attention_mask"].to(A ) ) @require_torch class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def UpperCamelCase_ ( self : List[Any] ): __A = NezhaModel.from_pretrained("sijunhe/nezha-cn-base" ) __A = torch.tensor([[0, 1, 2, 3, 4, 5]] ) __A = torch.tensor([[0, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __A = model(A ,attention_mask=A )[0] __A = torch.Size((1, 6, 7_68) ) self.assertEqual(output.shape ,A ) __A = torch.tensor([[[0.06_85, 0.24_41, 0.11_02], [0.06_00, 0.19_06, 0.13_49], [0.02_21, 0.08_19, 0.05_86]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,A ,atol=1E-4 ) ) @slow def UpperCamelCase_ ( self : str ): __A = NezhaForMaskedLM.from_pretrained("sijunhe/nezha-cn-base" ) __A = torch.tensor([[0, 1, 2, 3, 4, 5]] ) __A = torch.tensor([[1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __A = model(A ,attention_mask=A )[0] __A = torch.Size((1, 6, 2_11_28) ) self.assertEqual(output.shape ,A ) __A = torch.tensor( [[-2.79_39, -1.79_02, -2.21_89], [-2.85_85, -1.89_08, -2.37_23], [-2.64_99, -1.77_50, -2.25_58]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,A ,atol=1E-4 ) )
15
import math def UpperCAmelCase ( a_ , a_ = 0 , a_ = 0 ) -> list: """simple docstring""" __A = end or len(a_ ) for i in range(a_ , a_ ): __A = i __A = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __A = array[temp_index - 1] temp_index -= 1 __A = temp_index_value return array def UpperCAmelCase ( a_ , a_ , a_ ) -> None: # Max Heap """simple docstring""" __A = index __A = 2 * index + 1 # Left Node __A = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __A = left_index if right_index < heap_size and array[largest] < array[right_index]: __A = right_index if largest != index: __A , __A = array[largest], array[index] heapify(a_ , a_ , a_ ) def UpperCAmelCase ( a_ ) -> list: """simple docstring""" __A = len(a_ ) for i in range(n // 2 , -1 , -1 ): heapify(a_ , a_ , a_ ) for i in range(n - 1 , 0 , -1 ): __A , __A = array[0], array[i] heapify(a_ , 0 , a_ ) return array def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" __A = low __A = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __A , __A = array[j], array[i] i += 1 def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) == 0: return array __A = 2 * math.ceil(math.loga(len(a_ ) ) ) __A = 1_6 return intro_sort(a_ , 0 , len(a_ ) , a_ , a_ ) def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ ) -> list: """simple docstring""" while end - start > size_threshold: if max_depth == 0: return heap_sort(a_ ) max_depth -= 1 __A = median_of_a(a_ , a_ , start + ((end - start) // 2) + 1 , end - 1 ) __A = partition(a_ , a_ , a_ , a_ ) intro_sort(a_ , a_ , a_ , a_ , a_ ) __A = p return insertion_sort(a_ , a_ , a_ ) if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma : ').strip() SCREAMING_SNAKE_CASE :str = [float(item) for item in user_input.split(',')] print(sort(unsorted))
15
1
import json import os from typing import Optional, Tuple import regex as re from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :int = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Union[str, Any] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', } SCREAMING_SNAKE_CASE :List[str] = { 'vocab_file': {'ctrl': 'https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-vocab.json'}, 'merges_file': {'ctrl': 'https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-merges.txt'}, } SCREAMING_SNAKE_CASE :List[Any] = { 'ctrl': 256, } SCREAMING_SNAKE_CASE :Optional[Any] = { 'Pregnancy': 16_8629, 'Christianity': 7675, 'Explain': 10_6423, 'Fitness': 6_3440, 'Saving': 6_3163, 'Ask': 2_7171, 'Ass': 9_5985, 'Joke': 16_3509, 'Questions': 4_5622, 'Thoughts': 4_9605, 'Retail': 5_2342, 'Feminism': 16_4338, 'Writing': 1_1992, 'Atheism': 19_2263, 'Netflix': 4_8616, 'Computing': 3_9639, 'Opinion': 4_3213, 'Alone': 4_4967, 'Funny': 5_8917, 'Gaming': 4_0358, 'Human': 4088, 'India': 1331, 'Joker': 7_7138, 'Diet': 3_6206, 'Legal': 1_1859, 'Norman': 4939, 'Tip': 7_2689, 'Weight': 5_2343, 'Movies': 4_6273, 'Running': 2_3425, 'Science': 2090, 'Horror': 3_7793, 'Confession': 6_0572, 'Finance': 1_2250, 'Politics': 1_6360, 'Scary': 19_1985, 'Support': 1_2654, 'Technologies': 3_2516, 'Teenage': 6_6160, 'Event': 3_2769, 'Learned': 6_7460, 'Notion': 18_2770, 'Wikipedia': 3_7583, 'Books': 6665, 'Extract': 7_6050, 'Confessions': 10_2701, 'Conspiracy': 7_5932, 'Links': 6_3674, 'Narcissus': 15_0425, 'Relationship': 5_4766, 'Relationships': 13_4796, 'Reviews': 4_1671, 'News': 4256, 'Translation': 2_6820, 'multilingual': 12_8406, } def UpperCAmelCase ( a_ ) -> Union[str, Any]: """simple docstring""" __A = set() __A = word[0] for char in word[1:]: pairs.add((prev_char, char) ) __A = char __A = set(a_ ) return pairs class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = CONTROL_CODES def __init__( self : Dict ,A : List[Any] ,A : Tuple ,A : Optional[int]="<unk>" ,**A : str ): super().__init__(unk_token=A ,**A ) with open(A ,encoding="utf-8" ) as vocab_handle: __A = json.load(A ) __A = {v: k for k, v in self.encoder.items()} with open(A ,encoding="utf-8" ) as merges_handle: __A = merges_handle.read().split("\n" )[1:-1] __A = [tuple(merge.split() ) for merge in merges] __A = dict(zip(A ,range(len(A ) ) ) ) __A = {} @property def UpperCamelCase_ ( self : int ): return len(self.encoder ) def UpperCamelCase_ ( self : Optional[Any] ): return dict(self.encoder ,**self.added_tokens_encoder ) def UpperCamelCase_ ( self : Any ,A : Optional[Any] ): if token in self.cache: return self.cache[token] __A = tuple(A ) __A = tuple(list(word[:-1] ) + [word[-1] + "</w>"] ) __A = get_pairs(A ) if not pairs: return token while True: __A = min(A ,key=lambda A : self.bpe_ranks.get(A ,float("inf" ) ) ) if bigram not in self.bpe_ranks: break __A , __A = bigram __A = [] __A = 0 while i < len(A ): try: __A = word.index(A ,A ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) __A = j if word[i] == first and i < len(A ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 __A = tuple(A ) __A = new_word if len(A ) == 1: break else: __A = get_pairs(A ) __A = "@@ ".join(A ) __A = word[:-4] __A = word return word def UpperCamelCase_ ( self : Tuple ,A : Optional[Any] ): __A = [] __A = re.findall(R"\S+\n?" ,A ) for token in words: split_tokens.extend(list(self.bpe(A ).split(" " ) ) ) return split_tokens def UpperCamelCase_ ( self : int ,A : Optional[Any] ): return self.encoder.get(A ,self.encoder.get(self.unk_token ) ) def UpperCamelCase_ ( self : List[str] ,A : Dict ): return self.decoder.get(A ,self.unk_token ) def UpperCamelCase_ ( self : Optional[int] ,A : Union[str, Any] ): __A = " ".join(A ).replace("@@ " ,"" ).strip() return out_string def UpperCamelCase_ ( self : Dict ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = os.path.join( A ,(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) __A = os.path.join( A ,(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(A ,"w" ,encoding="utf-8" ) as f: f.write(json.dumps(self.encoder ,indent=2 ,sort_keys=A ,ensure_ascii=A ) + "\n" ) __A = 0 with open(A ,"w" ,encoding="utf-8" ) as writer: writer.write("#version: 0.2\n" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() ,key=lambda A : kv[1] ): if index != token_index: logger.warning( f'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' " Please check that the tokenizer is not corrupted!" ) __A = token_index writer.write(" ".join(A ) + "\n" ) index += 1 return vocab_file, merge_file # def decode(self, token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=True): # filtered_tokens = ' '.join(self.convert_ids_to_tokens(token_ids, skip_special_tokens=skip_special_tokens)) # tokens_generated_so_far = re.sub('(@@ )', '', string=filtered_tokens) # tokens_generated_so_far = re.sub('(@@ ?$)', '', string=tokens_generated_so_far) # return ''.join(tokens_generated_so_far)
15
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE :Optional[int] = NewType('DataClass', Any) SCREAMING_SNAKE_CASE :int = NewType('DataClassType', Any) def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" if isinstance(a_ , a_ ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( F'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def UpperCAmelCase ( a_ ) -> Callable[[str], Any]: """simple docstring""" __A = {str(a_ ): choice for choice in choices} return lambda a_ : str_to_choice.get(a_ , a_ ) def UpperCAmelCase ( *, a_ = None , a_ = None , a_ = dataclasses.MISSING , a_ = dataclasses.MISSING , a_ = None , **a_ , ) -> dataclasses.Field: """simple docstring""" if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls __A = {} if aliases is not None: __A = aliases if help is not None: __A = help return dataclasses.field(metadata=a_ , default=a_ , default_factory=a_ , **a_ ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 def __init__( self : Union[str, Any] ,A : Union[DataClassType, Iterable[DataClassType]] ,**A : List[Any] ): # To make the default appear when using --help if "formatter_class" not in kwargs: __A = ArgumentDefaultsHelpFormatter super().__init__(**A ) if dataclasses.is_dataclass(A ): __A = [dataclass_types] __A = list(A ) for dtype in self.dataclass_types: self._add_dataclass_arguments(A ) @staticmethod def UpperCamelCase_ ( A : ArgumentParser ,A : dataclasses.Field ): __A = f'''--{field.name}''' __A = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type ,A ): raise RuntimeError( "Unresolved type detected, which should have been done with the help of " "`typing.get_type_hints` method by default" ) __A = kwargs.pop("aliases" ,[] ) if isinstance(A ,A ): __A = [aliases] __A = getattr(field.type ,"__origin__" ,field.type ) if origin_type is Union or (hasattr(A ,"UnionType" ) and isinstance(A ,types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(A ) not in field.type.__args__ ): raise ValueError( "Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because" " the argument parser only supports one type per argument." f''' Problem encountered in field \'{field.name}\'.''' ) if type(A ) not in field.type.__args__: # filter `str` in Union __A = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] __A = getattr(field.type ,"__origin__" ,field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) __A = ( field.type.__args__[0] if isinstance(A ,field.type.__args__[1] ) else field.type.__args__[1] ) __A = getattr(field.type ,"__origin__" ,field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) __A = {} if origin_type is Literal or (isinstance(field.type ,A ) and issubclass(field.type ,A )): if origin_type is Literal: __A = field.type.__args__ else: __A = [x.value for x in field.type] __A = make_choice_type_function(kwargs["choices"] ) if field.default is not dataclasses.MISSING: __A = field.default else: __A = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument __A = copy(A ) # Hack because type=bool in argparse does not behave as we want. __A = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. __A = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way __A = default # This tells argparse we accept 0 or 1 value after --field_name __A = "?" # This is the value that will get picked if we do --field_name (without value) __A = True elif isclass(A ) and issubclass(A ,A ): __A = field.type.__args__[0] __A = "+" if field.default_factory is not dataclasses.MISSING: __A = field.default_factory() elif field.default is dataclasses.MISSING: __A = True else: __A = field.type if field.default is not dataclasses.MISSING: __A = field.default elif field.default_factory is not dataclasses.MISSING: __A = field.default_factory() else: __A = True parser.add_argument(A ,*A ,**A ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): __A = False parser.add_argument(f'''--no_{field.name}''' ,action="store_false" ,dest=field.name ,**A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : DataClassType ): if hasattr(A ,"_argument_group_name" ): __A = self.add_argument_group(dtype._argument_group_name ) else: __A = self try: __A = get_type_hints(A ) except NameError: raise RuntimeError( f'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' "removing line of `from __future__ import annotations` which opts in Postponed " "Evaluation of Annotations (PEP 563)" ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(A ): __A = ".".join(map(A ,sys.version_info[:3] ) ) raise RuntimeError( f'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' "line of `from __future__ import annotations` which opts in union types as " "`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To " "support Python versions that lower than 3.10, you need to use " "`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of " "`X | None`." ) from ex raise for field in dataclasses.fields(A ): if not field.init: continue __A = type_hints[field.name] self._parse_dataclass_field(A ,A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : List[Any]=None ,A : List[Any]=False ,A : Optional[Any]=True ,A : Union[str, Any]=None ,A : Union[str, Any]=None ,): if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): __A = [] if args_filename: args_files.append(Path(A ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix(".args" ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values __A = ArgumentParser() args_file_parser.add_argument(A ,type=A ,action="append" ) # Use only remaining args for further parsing (remove the args_file_flag) __A , __A = args_file_parser.parse_known_args(args=A ) __A = vars(A ).get(args_file_flag.lstrip("-" ) ,A ) if cmd_args_file_paths: args_files.extend([Path(A ) for p in cmd_args_file_paths] ) __A = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last __A = file_args + args if args is not None else file_args + sys.argv[1:] __A , __A = self.parse_known_args(args=A ) __A = [] for dtype in self.dataclass_types: __A = {f.name for f in dataclasses.fields(A ) if f.init} __A = {k: v for k, v in vars(A ).items() if k in keys} for k in keys: delattr(A ,A ) __A = dtype(**A ) outputs.append(A ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(A ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(f'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def UpperCamelCase_ ( self : Dict ,A : Dict[str, Any] ,A : bool = False ): __A = set(args.keys() ) __A = [] for dtype in self.dataclass_types: __A = {f.name for f in dataclasses.fields(A ) if f.init} __A = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) __A = dtype(**A ) outputs.append(A ) if not allow_extra_keys and unused_keys: raise ValueError(f'''Some keys are not used by the HfArgumentParser: {sorted(A )}''' ) return tuple(A ) def UpperCamelCase_ ( self : List[str] ,A : str ,A : bool = False ): with open(Path(A ) ,encoding="utf-8" ) as open_json_file: __A = json.loads(open_json_file.read() ) __A = self.parse_dict(A ,allow_extra_keys=A ) return tuple(A ) def UpperCamelCase_ ( self : int ,A : str ,A : bool = False ): __A = self.parse_dict(yaml.safe_load(Path(A ).read_text() ) ,allow_extra_keys=A ) return tuple(A )
15
1
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :List[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :int = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Union[str, Any] = { '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' ), } } SCREAMING_SNAKE_CASE :int = { 'google/bigbird-roberta-base': 4096, 'google/bigbird-roberta-large': 4096, 'google/bigbird-base-trivia-itc': 4096, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] snake_case_ = [] def __init__( self : Any ,A : List[str] ,A : str="<unk>" ,A : int="<s>" ,A : Union[str, Any]="</s>" ,A : List[str]="<pad>" ,A : int="[SEP]" ,A : Optional[Any]="[MASK]" ,A : Tuple="[CLS]" ,A : Optional[Dict[str, Any]] = None ,**A : Any ,): __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else bos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else eos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else unk_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else pad_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else cls_token __A = 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 __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sep_token=A ,mask_token=A ,cls_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) @property def UpperCamelCase_ ( self : List[str] ): return self.sp_model.get_piece_size() def UpperCamelCase_ ( self : Optional[Any] ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : str ,A : Optional[Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Tuple ): return self.sp_model.piece_to_id(A ) def UpperCamelCase_ ( self : List[Any] ,A : Tuple ): __A = self.sp_model.IdToPiece(A ) return token def UpperCamelCase_ ( self : List[Any] ,A : int ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def UpperCamelCase_ ( self : Tuple ,A : List[int] ,A : bool = False ,A : bool = None ,A : bool = True ,**A : Union[str, Any] ,): __A = kwargs.pop("use_source_tokenizer" ,A ) __A = self.convert_ids_to_tokens(A ,skip_special_tokens=A ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __A = [] __A = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) __A = [] sub_texts.append(A ) else: current_sub_text.append(A ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: __A = re.sub(R" (\[(MASK|SEP)\])" ,R"\1" ," ".join(A ) ) else: __A = "".join(A ) __A = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __A = self.clean_up_tokenization(A ) return clean_text else: return text def UpperCamelCase_ ( self : str ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def UpperCamelCase_ ( self : Optional[int] ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=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 : Any ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
15
SCREAMING_SNAKE_CASE :Any = 256 # Modulus to hash a string SCREAMING_SNAKE_CASE :Union[str, Any] = 100_0003 def UpperCAmelCase ( a_ , a_ ) -> bool: """simple docstring""" __A = len(a_ ) __A = len(a_ ) if p_len > t_len: return False __A = 0 __A = 0 __A = 1 # Calculating the hash of pattern and substring of text for i in range(a_ ): __A = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus __A = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue __A = (modulus_power * alphabet_size) % modulus for i in range(0 , t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash __A = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def UpperCAmelCase ( ) -> None: """simple docstring""" __A = "abc1abc12" __A = "alskfjaldsabc1abc1abc12k23adsfabcabc" __A = "alskfjaldsk23adsfabcabc" assert rabin_karp(a_ , a_ ) and not rabin_karp(a_ , a_ ) # Test 2) __A = "ABABX" __A = "ABABZABABYABABX" assert rabin_karp(a_ , a_ ) # Test 3) __A = "AAAB" __A = "ABAAAAAB" assert rabin_karp(a_ , a_ ) # Test 4) __A = "abcdabcy" __A = "abcxabcdabxabcdabcdabcy" assert rabin_karp(a_ , a_ ) # Test 5) __A = "Lü" __A = "Lüsai" assert rabin_karp(a_ , a_ ) __A = "Lue" assert not rabin_karp(a_ , a_ ) print("Success." ) if __name__ == "__main__": test_rabin_karp()
15
1
from sklearn.metrics import recall_score import datasets SCREAMING_SNAKE_CASE :Union[str, Any] = '\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n' SCREAMING_SNAKE_CASE :str = '\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the \'positive class\' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`.\n - `\'binary\'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n - `\'micro\'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n - `\'macro\'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - `\'weighted\'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.\n - `\'samples\'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n - `\'warn\'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n - `0`: If there is a zero division, the return value is `0`.\n - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n Example 1-A simple example with some errors\n >>> recall_metric = datasets.load_metric(\'recall\')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n >>> print(results)\n {\'recall\': 0.6666666666666666}\n\n Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n >>> recall_metric = datasets.load_metric(\'recall\')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n >>> print(results)\n {\'recall\': 0.5}\n\n Example 3-The same example as Example 1, but with `sample_weight` included.\n >>> recall_metric = datasets.load_metric(\'recall\')\n >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n >>> print(results)\n {\'recall\': 0.55}\n\n Example 4-A multiclass example, using different averages.\n >>> recall_metric = datasets.load_metric(\'recall\')\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'macro\')\n >>> print(results)\n {\'recall\': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'micro\')\n >>> print(results)\n {\'recall\': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'weighted\')\n >>> print(results)\n {\'recall\': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {\'recall\': array([1., 0., 0.])}\n' SCREAMING_SNAKE_CASE :Any = '\n@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : Dict ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("int32" ) ), "references": datasets.Sequence(datasets.Value("int32" ) ), } if self.config_name == "multilabel" else { "predictions": datasets.Value("int32" ), "references": datasets.Value("int32" ), } ) ,reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"] ,) def UpperCamelCase_ ( self : Any ,A : List[str] ,A : str ,A : Optional[int]=None ,A : int=1 ,A : Optional[int]="binary" ,A : Any=None ,A : str="warn" ,): __A = recall_score( A ,A ,labels=A ,pos_label=A ,average=A ,sample_weight=A ,zero_division=A ,) return {"recall": float(A ) if score.size == 1 else score}
15
import argparse import json import os import torch from transformers.file_utils import has_file from diffusers import UNetaDConditionModel, UNetaDModel SCREAMING_SNAKE_CASE :Union[str, Any] = False SCREAMING_SNAKE_CASE :Any = True SCREAMING_SNAKE_CASE :Tuple = False if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser() parser.add_argument( '--repo_path', default=None, type=str, required=True, help='The config json file corresponding to the architecture.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') SCREAMING_SNAKE_CASE :Union[str, Any] = parser.parse_args() SCREAMING_SNAKE_CASE :Dict = { 'image_size': 'sample_size', 'num_res_blocks': 'layers_per_block', 'block_channels': 'block_out_channels', 'down_blocks': 'down_block_types', 'up_blocks': 'up_block_types', 'downscale_freq_shift': 'freq_shift', 'resnet_num_groups': 'norm_num_groups', 'resnet_act_fn': 'act_fn', 'resnet_eps': 'norm_eps', 'num_head_channels': 'attention_head_dim', } SCREAMING_SNAKE_CASE :Optional[int] = { 'time_steps': 'time_proj', 'mid': 'mid_block', 'downsample_blocks': 'down_blocks', 'upsample_blocks': 'up_blocks', } SCREAMING_SNAKE_CASE :int = '' if has_file(args.repo_path, 'config.json') else 'unet' with open(os.path.join(args.repo_path, subfolder, 'config.json'), 'r', encoding='utf-8') as reader: SCREAMING_SNAKE_CASE :Dict = reader.read() SCREAMING_SNAKE_CASE :List[str] = json.loads(text) if do_only_config: for key in config_parameters_to_change.keys(): config.pop(key, None) if has_file(args.repo_path, 'config.json'): SCREAMING_SNAKE_CASE :Optional[int] = UNetaDModel(**config) else: SCREAMING_SNAKE_CASE :Optional[Any] = UNetaDConditionModel if 'ldm-text2im-large-256' in args.repo_path else UNetaDModel SCREAMING_SNAKE_CASE :List[str] = class_name(**config) if do_only_config: model.save_config(os.path.join(args.repo_path, subfolder)) SCREAMING_SNAKE_CASE :List[str] = dict(model.config) if do_only_renaming: for key, value in config_parameters_to_change.items(): if key in config: SCREAMING_SNAKE_CASE :Optional[Any] = config[key] del config[key] SCREAMING_SNAKE_CASE :Optional[Any] = [k.replace('UNetRes', '') for k in config['down_block_types']] SCREAMING_SNAKE_CASE :List[Any] = [k.replace('UNetRes', '') for k in config['up_block_types']] if do_only_weights: SCREAMING_SNAKE_CASE :Tuple = torch.load(os.path.join(args.repo_path, subfolder, 'diffusion_pytorch_model.bin')) SCREAMING_SNAKE_CASE :Any = {} for param_key, param_value in state_dict.items(): if param_key.endswith('.op.bias') or param_key.endswith('.op.weight'): continue SCREAMING_SNAKE_CASE :List[str] = False for key, new_key in key_parameters_to_change.items(): if not has_changed and param_key.split('.')[0] == key: SCREAMING_SNAKE_CASE :List[Any] = param_value SCREAMING_SNAKE_CASE :str = True if not has_changed: SCREAMING_SNAKE_CASE :List[str] = param_value model.load_state_dict(new_state_dict) model.save_pretrained(os.path.join(args.repo_path, subfolder))
15
1
from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE :str = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Any = { 'studio-ousia/luke-base': 'https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json', 'studio-ousia/luke-large': 'https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json', } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "luke" def __init__( self : Dict ,A : Optional[int]=5_02_67 ,A : List[Any]=50_00_00 ,A : Any=7_68 ,A : Tuple=2_56 ,A : List[Any]=12 ,A : Tuple=12 ,A : List[Any]=30_72 ,A : Any="gelu" ,A : Any=0.1 ,A : Optional[int]=0.1 ,A : Dict=5_12 ,A : str=2 ,A : List[str]=0.02 ,A : List[str]=1E-12 ,A : List[Any]=True ,A : Dict=None ,A : Optional[Any]=1 ,A : Optional[int]=0 ,A : Any=2 ,**A : Tuple ,): super().__init__(pad_token_id=A ,bos_token_id=A ,eos_token_id=A ,**A ) __A = vocab_size __A = entity_vocab_size __A = hidden_size __A = entity_emb_size __A = num_hidden_layers __A = num_attention_heads __A = hidden_act __A = intermediate_size __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = type_vocab_size __A = initializer_range __A = layer_norm_eps __A = use_entity_aware_attention __A = classifier_dropout
15
import argparse import math import traceback import dateutil.parser as date_parser import requests def UpperCAmelCase ( a_ ) -> str: """simple docstring""" __A = {} __A = job["started_at"] __A = job["completed_at"] __A = date_parser.parse(a_ ) __A = date_parser.parse(a_ ) __A = round((end_datetime - start_datetime).total_seconds() / 60.0 ) __A = start __A = end __A = duration_in_min return job_info def UpperCAmelCase ( a_ , a_=None ) -> str: """simple docstring""" __A = None if token is not None: __A = {"Accept": "application/vnd.github+json", "Authorization": F'''Bearer {token}'''} __A = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' __A = requests.get(a_ , headers=a_ ).json() __A = {} try: job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) __A = math.ceil((result["total_count"] - 1_0_0) / 1_0_0 ) for i in range(a_ ): __A = requests.get(url + F'''&page={i + 2}''' , headers=a_ ).json() job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) return job_time except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": SCREAMING_SNAKE_CASE :Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() SCREAMING_SNAKE_CASE :Union[str, Any] = get_job_time(args.workflow_run_id) SCREAMING_SNAKE_CASE :Optional[int] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f'''{k}: {v["duration"]}''')
15
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available SCREAMING_SNAKE_CASE :Optional[int] = {'configuration_speech_encoder_decoder': ['SpeechEncoderDecoderConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :int = ['SpeechEncoderDecoderModel'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :Optional[Any] = ['FlaxSpeechEncoderDecoderModel'] if TYPE_CHECKING: from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel else: import sys SCREAMING_SNAKE_CASE :Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
15
import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" __A = args.pruning_method __A = args.threshold __A = args.model_name_or_path.rstrip("/" ) __A = args.target_model_path print(F'''Load fine-pruned model from {model_name_or_path}''' ) __A = torch.load(os.path.join(a_ , "pytorch_model.bin" ) ) __A = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: __A = tensor print(F'''Copied layer {name}''' ) elif "classifier" in name or "qa_output" in name: __A = tensor print(F'''Copied layer {name}''' ) elif "bias" in name: __A = tensor print(F'''Copied layer {name}''' ) else: if pruning_method == "magnitude": __A = MagnitudeBinarizer.apply(inputs=a_ , threshold=a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "topK": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A = TopKBinarizer.apply(a_ , a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A = ThresholdBinarizer.apply(a_ , a_ , a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "l0": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A , __A = -0.1, 1.1 __A = torch.sigmoid(a_ ) __A = s * (r - l) + l __A = s_bar.clamp(min=0.0 , max=1.0 ) __A = tensor * mask print(F'''Pruned layer {name}''' ) else: raise ValueError("Unknown pruning method" ) if target_model_path is None: __A = os.path.join( os.path.dirname(a_ ) , F'''bertarized_{os.path.basename(a_ )}''' ) if not os.path.isdir(a_ ): shutil.copytree(a_ , a_ ) print(F'''\nCreated folder {target_model_path}''' ) torch.save(a_ , os.path.join(a_ , "pytorch_model.bin" ) ) print("\nPruned model saved! See you later!" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser() parser.add_argument( '--pruning_method', choices=['l0', 'magnitude', 'topK', 'sigmoied_threshold'], type=str, required=True, help=( 'Pruning Method (l0 = L0 regularization, magnitude = Magnitude pruning, topK = Movement pruning,' ' sigmoied_threshold = Soft movement pruning)' ), ) parser.add_argument( '--threshold', type=float, required=False, help=( 'For `magnitude` and `topK`, it is the level of remaining weights (in %) in the fine-pruned model.' 'For `sigmoied_threshold`, it is the threshold \tau against which the (sigmoied) scores are compared.' 'Not needed for `l0`' ), ) parser.add_argument( '--model_name_or_path', type=str, required=True, help='Folder containing the model that was previously fine-pruned', ) parser.add_argument( '--target_model_path', default=None, type=str, required=False, help='Folder containing the model that was previously fine-pruned', ) SCREAMING_SNAKE_CASE :str = parser.parse_args() main(args)
15
1
from argparse import ArgumentParser from .env import EnvironmentCommand def UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" __A = ArgumentParser("Diffusers CLI tool" , usage="diffusers-cli <command> [<args>]" ) __A = parser.add_subparsers(help="diffusers-cli command helpers" ) # Register commands EnvironmentCommand.register_subcommand(a_ ) # Let's go __A = parser.parse_args() if not hasattr(a_ , "func" ): parser.print_help() exit(1 ) # Run __A = args.func(a_ ) service.run() if __name__ == "__main__": main()
15
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :List[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :int = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Union[str, Any] = { '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' ), } } SCREAMING_SNAKE_CASE :int = { 'google/bigbird-roberta-base': 4096, 'google/bigbird-roberta-large': 4096, 'google/bigbird-base-trivia-itc': 4096, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] snake_case_ = [] def __init__( self : Any ,A : List[str] ,A : str="<unk>" ,A : int="<s>" ,A : Union[str, Any]="</s>" ,A : List[str]="<pad>" ,A : int="[SEP]" ,A : Optional[Any]="[MASK]" ,A : Tuple="[CLS]" ,A : Optional[Dict[str, Any]] = None ,**A : Any ,): __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else bos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else eos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else unk_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else pad_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else cls_token __A = 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 __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sep_token=A ,mask_token=A ,cls_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) @property def UpperCamelCase_ ( self : List[str] ): return self.sp_model.get_piece_size() def UpperCamelCase_ ( self : Optional[Any] ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : str ,A : Optional[Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Tuple ): return self.sp_model.piece_to_id(A ) def UpperCamelCase_ ( self : List[Any] ,A : Tuple ): __A = self.sp_model.IdToPiece(A ) return token def UpperCamelCase_ ( self : List[Any] ,A : int ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def UpperCamelCase_ ( self : Tuple ,A : List[int] ,A : bool = False ,A : bool = None ,A : bool = True ,**A : Union[str, Any] ,): __A = kwargs.pop("use_source_tokenizer" ,A ) __A = self.convert_ids_to_tokens(A ,skip_special_tokens=A ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __A = [] __A = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) __A = [] sub_texts.append(A ) else: current_sub_text.append(A ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: __A = re.sub(R" (\[(MASK|SEP)\])" ,R"\1" ," ".join(A ) ) else: __A = "".join(A ) __A = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __A = self.clean_up_tokenization(A ) return clean_text else: return text def UpperCamelCase_ ( self : str ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def UpperCamelCase_ ( self : Optional[int] ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=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 : Any ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
15
1
def UpperCAmelCase ( a_ , a_ , a_ ) -> Optional[int]: """simple docstring""" if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(a_ , n - 1 , a_ ) * a) % mod else: __A = binary_exponentiation(a_ , n / 2 , a_ ) return (b * b) % mod # a prime number SCREAMING_SNAKE_CASE :Any = 701 SCREAMING_SNAKE_CASE :List[str] = 10_0000_0000 SCREAMING_SNAKE_CASE :List[str] = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
15
import PIL.Image import PIL.ImageOps from packaging import version from PIL import Image if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('9.1.0'): SCREAMING_SNAKE_CASE :Any = { 'linear': PIL.Image.Resampling.BILINEAR, 'bilinear': PIL.Image.Resampling.BILINEAR, 'bicubic': PIL.Image.Resampling.BICUBIC, 'lanczos': PIL.Image.Resampling.LANCZOS, 'nearest': PIL.Image.Resampling.NEAREST, } else: SCREAMING_SNAKE_CASE :int = { 'linear': PIL.Image.LINEAR, 'bilinear': PIL.Image.BILINEAR, 'bicubic': PIL.Image.BICUBIC, 'lanczos': PIL.Image.LANCZOS, 'nearest': PIL.Image.NEAREST, } def UpperCAmelCase ( a_ ) -> Optional[Any]: """simple docstring""" __A = (images / 2 + 0.5).clamp(0 , 1 ) __A = images.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __A = numpy_to_pil(a_ ) return images def UpperCAmelCase ( a_ ) -> int: """simple docstring""" if images.ndim == 3: __A = images[None, ...] __A = (images * 2_5_5).round().astype("uint8" ) if images.shape[-1] == 1: # special case for grayscale (single channel) images __A = [Image.fromarray(image.squeeze() , mode="L" ) for image in images] else: __A = [Image.fromarray(a_ ) for image in images] return pil_images
15
1
def UpperCAmelCase ( a_ , a_ ) -> float: """simple docstring""" if mass < 0: raise ValueError("The mass of a body cannot be negative" ) return 0.5 * mass * abs(a_ ) * abs(a_ ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
15
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 SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[Any] = { 'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json', # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "yolos" def __init__( self : Any ,A : Optional[Any]=7_68 ,A : Dict=12 ,A : Any=12 ,A : str=30_72 ,A : Any="gelu" ,A : str=0.0 ,A : List[str]=0.0 ,A : Dict=0.02 ,A : int=1E-12 ,A : Tuple=[5_12, 8_64] ,A : List[Any]=16 ,A : str=3 ,A : str=True ,A : Any=1_00 ,A : Dict=True ,A : Dict=False ,A : Tuple=1 ,A : Union[str, Any]=5 ,A : Optional[Any]=2 ,A : Union[str, Any]=5 ,A : int=2 ,A : int=0.1 ,**A : List[str] ,): super().__init__(**A ) __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = initializer_range __A = layer_norm_eps __A = image_size __A = patch_size __A = num_channels __A = qkv_bias __A = num_detection_tokens __A = use_mid_position_embeddings __A = auxiliary_loss # Hungarian matcher __A = class_cost __A = bbox_cost __A = giou_cost # Loss coefficients __A = bbox_loss_coefficient __A = giou_loss_coefficient __A = eos_coefficient class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = version.parse("1.11" ) @property def UpperCamelCase_ ( self : str ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def UpperCamelCase_ ( self : List[Any] ): return 1E-4 @property def UpperCamelCase_ ( self : Optional[Any] ): return 12
15
1
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split SCREAMING_SNAKE_CASE :str = datasets.load_iris() SCREAMING_SNAKE_CASE :Dict = np.array(data['data']) SCREAMING_SNAKE_CASE :Union[str, Any] = np.array(data['target']) SCREAMING_SNAKE_CASE :Dict = data['target_names'] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :List[str] = train_test_split(X, y) def UpperCAmelCase ( a_ , a_ ) -> Union[str, Any]: """simple docstring""" return np.linalg.norm(np.array(a_ ) - np.array(a_ ) ) def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_=5 ) -> List[Any]: """simple docstring""" __A = zip(a_ , a_ ) # List of distances of all points from the point to be classified __A = [] for data_point in data: __A = euclidean_distance(data_point[0] , a_ ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. __A = [i[1] for i in sorted(a_ )[:k]] # Most commonly occurring class among them # is the class into which the point is classified __A = Counter(a_ ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
15
# Copyright 2021 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from packaging import version from .. import __version__ from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD from .doc import ( add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, copy_func, replace_return_docstrings, ) from .generic import ( ContextManagers, ExplicitEnum, ModelOutput, PaddingStrategy, TensorType, add_model_info_to_auto_map, cached_property, can_return_loss, expand_dims, find_labels, flatten_dict, infer_framework, is_jax_tensor, is_numpy_array, is_tensor, is_tf_symbolic_tensor, is_tf_tensor, is_torch_device, is_torch_dtype, is_torch_tensor, reshape, squeeze, strtobool, tensor_size, to_numpy, to_py_obj, transpose, working_or_temp_dir, ) from .hub import ( CLOUDFRONT_DISTRIB_PREFIX, DISABLE_TELEMETRY, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, EntryNotFoundError, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, cached_file, default_cache_path, define_sagemaker_information, download_url, extract_commit_hash, get_cached_models, get_file_from_repo, get_full_repo_name, has_file, http_user_agent, is_offline_mode, is_remote_url, move_cache, send_example_telemetry, try_to_load_from_cache, ) from .import_utils import ( ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, TORCH_FX_REQUIRED_VERSION, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, ccl_version, direct_transformers_import, get_torch_version, is_accelerate_available, is_apex_available, is_bitsandbytes_available, is_bsa_available, is_coloredlogs_available, is_cython_available, is_datasets_available, is_decord_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_jieba_available, is_jumanpp_available, is_kenlm_available, is_keras_nlp_available, is_librosa_available, is_natten_available, is_ninja_available, is_onnx_available, is_openai_available, is_optimum_available, is_pandas_available, is_peft_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytest_available, is_pytorch_quantization_available, is_rjieba_available, is_sacremoses_available, is_safetensors_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_sudachi_available, is_tensorflow_probability_available, is_tensorflow_text_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_bfaa_cpu_available, is_torch_bfaa_gpu_available, is_torch_compile_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_neuroncore_available, is_torch_tensorrt_fx_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_torchdistx_available, is_torchdynamo_available, is_torchvision_available, is_training_run_on_sagemaker, is_vision_available, requires_backends, torch_only_method, ) SCREAMING_SNAKE_CASE :List[str] = 'pytorch_model.bin' SCREAMING_SNAKE_CASE :str = 'pytorch_model.bin.index.json' SCREAMING_SNAKE_CASE :Optional[int] = 'adapter_config.json' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.bin' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.safetensors' SCREAMING_SNAKE_CASE :str = 'tf_model.h5' SCREAMING_SNAKE_CASE :List[Any] = 'tf_model.h5.index.json' SCREAMING_SNAKE_CASE :str = 'model.ckpt' SCREAMING_SNAKE_CASE :List[Any] = 'flax_model.msgpack' SCREAMING_SNAKE_CASE :Optional[int] = 'flax_model.msgpack.index.json' SCREAMING_SNAKE_CASE :Tuple = 'model.safetensors' SCREAMING_SNAKE_CASE :List[Any] = 'model.safetensors.index.json' SCREAMING_SNAKE_CASE :str = 'config.json' SCREAMING_SNAKE_CASE :int = 'preprocessor_config.json' SCREAMING_SNAKE_CASE :Optional[Any] = FEATURE_EXTRACTOR_NAME SCREAMING_SNAKE_CASE :Optional[int] = 'generation_config.json' SCREAMING_SNAKE_CASE :List[str] = 'modelcard.json' SCREAMING_SNAKE_CASE :Optional[int] = '▁' SCREAMING_SNAKE_CASE :Optional[Any] = SENTENCEPIECE_UNDERLINE # Kept for backward compatibility SCREAMING_SNAKE_CASE :str = [ [[0, 1, 0, 1], [1, 0, 0, 1]] ] * 2 # Needs to have 0s and 1s only since XLM uses it for langs too. SCREAMING_SNAKE_CASE :Optional[Any] = [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]] SCREAMING_SNAKE_CASE :List[Any] = [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]] def UpperCAmelCase ( a_ ) -> Dict: """simple docstring""" if version.parse(a_ ) < version.parse(a_ ): if "dev" in min_version: __A = ( "This example requires a source install from HuggingFace Transformers (see " "`https://huggingface.co/docs/transformers/installation#install-from-source`)," ) else: __A = F'''This example requires a minimum version of {min_version},''' error_message += F''' but the version found is {__version__}.\n''' raise ImportError( error_message + "Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other " "versions of HuggingFace Transformers." )
15
1
def UpperCAmelCase ( a_ , a_ ) -> int: """simple docstring""" return x if y == 0 else greatest_common_divisor(a_ , x % y ) def UpperCAmelCase ( a_ , a_ ) -> int: """simple docstring""" return (x * y) // greatest_common_divisor(a_ , a_ ) def UpperCAmelCase ( a_ = 2_0 ) -> int: """simple docstring""" __A = 1 for i in range(1 , n + 1 ): __A = lcm(a_ , a_ ) return g if __name__ == "__main__": print(f'''{solution() = }''')
15
def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" __A = [0] * len(a_ ) __A = [] __A = [1] * len(a_ ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(a_ ) ): if indegree[i] == 0: queue.append(a_ ) while queue: __A = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: __A = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(a_ ) print(max(a_ ) ) # Adjacency list of Graph SCREAMING_SNAKE_CASE :List[Any] = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
15
1
import re from filelock import FileLock try: import nltk SCREAMING_SNAKE_CASE :Dict = True except (ImportError, ModuleNotFoundError): SCREAMING_SNAKE_CASE :int = False if NLTK_AVAILABLE: with FileLock('.lock') as lock: nltk.download('punkt', quiet=True) def UpperCAmelCase ( a_ ) -> str: """simple docstring""" re.sub("<n>" , "" , a_ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(a_ ) )
15
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items() ) def UpperCAmelCase ( a_ , a_ ) -> Tuple: """simple docstring""" __A = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue __A = key.replace("heads.cmd.mim_head.cls.predictions" , "mmm_image_head" ) __A = key.replace("heads.cmd.mlm_head.cls.predictions" , "mmm_text_head" ) __A = key.replace("heads.cmd.itm_head.cls" , "itm_head" ) __A = key.replace("heads.cmd.itm_head.pooler" , "itm_head.pooler" ) __A = key.replace("heads.cmd.clip_head.logit_scale" , "flava.logit_scale" ) __A = key.replace("heads.fairseq_mlm.cls.predictions" , "mlm_head" ) __A = key.replace("heads.imagenet.mim_head.cls.predictions" , "mim_head" ) __A = key.replace("mm_text_projection" , "flava.text_to_mm_projection" ) __A = key.replace("mm_image_projection" , "flava.image_to_mm_projection" ) __A = key.replace("image_encoder.module" , "flava.image_model" ) __A = key.replace("text_encoder.module" , "flava.text_model" ) __A = key.replace("mm_encoder.module.encoder.cls_token" , "flava.multimodal_model.cls_token" ) __A = key.replace("mm_encoder.module" , "flava.multimodal_model" ) __A = key.replace("text_projection" , "flava.text_projection" ) __A = key.replace("image_projection" , "flava.image_projection" ) __A = value.float() for key, value in codebook_state_dict.items(): __A = value return upgrade @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_ , a_=None ) -> Tuple: """simple docstring""" if config_path is not None: __A = FlavaConfig.from_pretrained(a_ ) else: __A = FlavaConfig() __A = FlavaForPreTraining(a_ ).eval() __A = convert_dalle_checkpoint(a_ , a_ , save_checkpoint=a_ ) if os.path.exists(a_ ): __A = torch.load(a_ , map_location="cpu" ) else: __A = torch.hub.load_state_dict_from_url(a_ , map_location="cpu" ) __A = upgrade_state_dict(a_ , a_ ) hf_model.load_state_dict(a_ ) __A = hf_model.state_dict() __A = count_parameters(a_ ) __A = count_parameters(a_ ) + count_parameters(a_ ) assert torch.allclose(a_ , a_ , atol=1E-3 ) hf_model.save_pretrained(a_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Any = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to flava checkpoint') parser.add_argument('--codebook_path', default=None, type=str, help='Path to flava codebook checkpoint') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
15
1
import logging import numpy as np import pytest from scipy.linalg import eigh logging.basicConfig(level=logging.INFO, format='%(message)s') def UpperCAmelCase ( a_ ) -> np.ndarray: """simple docstring""" return input_array.reshape((input_array.size, 1) ) def UpperCAmelCase ( a_ , a_ , a_ ) -> np.ndarray: """simple docstring""" __A = np.nan for i in range(a_ ): __A = features[:, labels == i] __A = data.mean(1 ) # Centralize the data of class i __A = data - column_reshape(a_ ) if i > 0: # If covariance_sum is not None covariance_sum += np.dot(a_ , centered_data.T ) else: # If covariance_sum is np.nan (i.e. first loop) __A = np.dot(a_ , centered_data.T ) return covariance_sum / features.shape[1] def UpperCAmelCase ( a_ , a_ , a_ ) -> np.ndarray: """simple docstring""" __A = features.mean(1 ) __A = np.nan for i in range(a_ ): __A = features[:, labels == i] __A = data.shape[1] __A = data.mean(1 ) if i > 0: # If covariance_sum is not None covariance_sum += device_data * np.dot( column_reshape(a_ ) - column_reshape(a_ ) , (column_reshape(a_ ) - column_reshape(a_ )).T , ) else: # If covariance_sum is np.nan (i.e. first loop) __A = device_data * np.dot( column_reshape(a_ ) - column_reshape(a_ ) , (column_reshape(a_ ) - column_reshape(a_ )).T , ) return covariance_sum / features.shape[1] def UpperCAmelCase ( a_ , a_ ) -> np.ndarray: """simple docstring""" if features.any(): __A = features.mean(1 ) # Center the dataset __A = features - np.reshape(a_ , (data_mean.size, 1) ) __A = np.dot(a_ , centered_data.T ) / features.shape[1] __A , __A = np.linalg.eigh(a_ ) # Take all the columns in the reverse order (-1), and then takes only the first __A = eigenvectors[:, ::-1][:, 0:dimensions] # Project the database on the new space __A = np.dot(filtered_eigenvectors.T , a_ ) logging.info("Principal Component Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=a_ ) logging.error("Dataset empty" ) raise AssertionError def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> np.ndarray: """simple docstring""" assert classes > dimensions # Check if features have been already loaded if features.any: __A , __A = eigh( covariance_between_classes(a_ , a_ , a_ ) , covariance_within_classes(a_ , a_ , a_ ) , ) __A = eigenvectors[:, ::-1][:, :dimensions] __A , __A , __A = np.linalg.svd(a_ ) __A = svd_matrix[:, 0:dimensions] __A = np.dot(filtered_svd_matrix.T , a_ ) logging.info("Linear Discriminant Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=a_ ) logging.error("Dataset empty" ) raise AssertionError def UpperCAmelCase ( ) -> None: """simple docstring""" __A = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]] ) __A = np.array([0, 0, 0, 1, 1] ) __A = 2 __A = 2 # Assert that the function raises an AssertionError if dimensions > classes with pytest.raises(a_ ) as error_info: __A = linear_discriminant_analysis( a_ , a_ , a_ , a_ ) if isinstance(a_ , np.ndarray ): raise AssertionError( "Did not raise AssertionError for dimensions > classes" ) assert error_info.type is AssertionError def UpperCAmelCase ( ) -> None: """simple docstring""" __A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) __A = 2 __A = np.array([[6.92_820_323, 8.66_025_404, 10.39_230_485], [3.0, 3.0, 3.0]] ) with pytest.raises(a_ ) as error_info: __A = principal_component_analysis(a_ , a_ ) if not np.allclose(a_ , a_ ): raise AssertionError assert error_info.type is AssertionError if __name__ == "__main__": import doctest doctest.testmod()
15
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Optional[int] = {'vocab_file': 'sentencepiece.bpe.model'} SCREAMING_SNAKE_CASE :Tuple = { 'vocab_file': { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model', } } SCREAMING_SNAKE_CASE :List[Any] = { 'camembert-base': 512, } SCREAMING_SNAKE_CASE :List[str] = '▁' class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[Any] ,A : List[str] ,A : List[Any]="<s>" ,A : Tuple="</s>" ,A : Any="</s>" ,A : Optional[Any]="<s>" ,A : Tuple="<unk>" ,A : str="<pad>" ,A : int="<mask>" ,A : Optional[int]=["<s>NOTUSED", "</s>NOTUSED"] ,A : Optional[Dict[str, Any]] = None ,**A : Optional[Any] ,): # Mask token behave like a normal word, i.e. include the space before it __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,sep_token=A ,cls_token=A ,pad_token=A ,mask_token=A ,additional_special_tokens=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(A ) ) __A = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __A = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __A = len(self.fairseq_tokens_to_ids ) __A = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __A = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCamelCase_ ( self : int ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def UpperCamelCase_ ( self : Union[str, Any] ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase_ ( self : Dict ): return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCamelCase_ ( self : int ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Dict ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(A ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : Tuple ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCamelCase_ ( self : Optional[Any] ,A : Dict ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def __getstate__( self : Dict ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Union[str, Any] ,A : Any ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
15
1
import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness SCREAMING_SNAKE_CASE :Dict = '\\n@misc{chen2021evaluating,\n title={Evaluating Large Language Models Trained on Code},\n author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \\nand Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \\nand Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \\nand Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \\nand Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \\nand Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \\nand Mohammad Bavarian and Clemens Winter and Philippe Tillet \\nand Felipe Petroski Such and Dave Cummings and Matthias Plappert \\nand Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \\nand William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \\nand Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \\nand William Saunders and Christopher Hesse and Andrew N. Carr \\nand Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \\nand Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \\nand Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \\nand Sam McCandlish and Ilya Sutskever and Wojciech Zaremba},\n year={2021},\n eprint={2107.03374},\n archivePrefix={arXiv},\n primaryClass={cs.LG}\n}\n' SCREAMING_SNAKE_CASE :List[Any] = '\\nThis metric implements the evaluation harness for the HumanEval problem solving dataset\ndescribed in the paper "Evaluating Large Language Models Trained on Code"\n(https://arxiv.org/abs/2107.03374).\n' SCREAMING_SNAKE_CASE :Dict = '\nCalculates how good are predictions given some references, using certain scores\nArgs:\n predictions: list of candidates to evaluate. Each candidates should be a list\n of strings with several code candidates to solve the problem.\n references: a list with a test for each prediction. Each test should evaluate the\n correctness of a code candidate.\n k: number of code candidates to consider in the evaluation (Default: [1, 10, 100])\n num_workers: number of workers used to evaluate the canidate programs (Default: 4).\n timeout:\nReturns:\n pass_at_k: dict with pass rates for each k\n results: dict with granular results of each unittest\nExamples:\n >>> code_eval = datasets.load_metric("code_eval")\n >>> test_cases = ["assert add(2,3)==5"]\n >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]]\n >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2])\n >>> print(pass_at_k)\n {\'pass@1\': 0.5, \'pass@2\': 1.0}\n' SCREAMING_SNAKE_CASE :Union[str, Any] = '\n################################################################################\n !!!WARNING!!!\n################################################################################\nThe "code_eval" metric executes untrusted model-generated code in Python.\nAlthough it is highly unlikely that model-generated code will do something\novertly malicious in response to this test suite, model-generated code may act\ndestructively due to a lack of model capability or alignment.\nUsers are strongly encouraged to sandbox this evaluation suite so that it\ndoes not perform destructive actions on their host or network. For more\ninformation on how OpenAI sandboxes its code, see the paper "Evaluating Large\nLanguage Models Trained on Code" (https://arxiv.org/abs/2107.03374).\n\nOnce you have read this disclaimer and taken appropriate precautions,\nset the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this\nwith:\n\n>>> import os\n>>> os.environ["HF_ALLOW_CODE_EVAL"] = "1"\n\n################################################################################\\n' SCREAMING_SNAKE_CASE :List[Any] = 'The MIT License\n\nCopyright (c) OpenAI (https://openai.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : Optional[Any] ): return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ) ), "references": datasets.Value("string" ), } ) ,homepage="https://github.com/openai/human-eval" ,codebase_urls=["https://github.com/openai/human-eval"] ,reference_urls=["https://github.com/openai/human-eval"] ,license=_LICENSE ,) def UpperCamelCase_ ( self : str ,A : int ,A : Union[str, Any] ,A : Optional[int]=[1, 10, 1_00] ,A : Union[str, Any]=4 ,A : Dict=3.0 ): if os.getenv("HF_ALLOW_CODE_EVAL" ,0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError("This metric is currently not supported on Windows." ) with ThreadPoolExecutor(max_workers=A ) as executor: __A = [] __A = Counter() __A = 0 __A = defaultdict(A ) for task_id, (candidates, test_case) in enumerate(zip(A ,A ) ): for candidate in candidates: __A = candidate + "\n" + test_case __A = (test_program, timeout, task_id, completion_id[task_id]) __A = executor.submit(A ,*A ) futures.append(A ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(A ): __A = future.result() results[result["task_id"]].append((result["completion_id"], result) ) __A , __A = [], [] for result in results.values(): result.sort() __A = [r[1]["passed"] for r in result] total.append(len(A ) ) correct.append(sum(A ) ) __A = np.array(A ) __A = np.array(A ) __A = k __A = {f'''pass@{k}''': estimate_pass_at_k(A ,A ,A ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def UpperCAmelCase ( a_ , a_ , a_ ) -> Union[str, Any]: """simple docstring""" def estimator(a_ , a_ , a_ ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 , n + 1 ) ) if isinstance(a_ , a_ ): __A = itertools.repeat(a_ , len(a_ ) ) else: assert len(a_ ) == len(a_ ) __A = iter(a_ ) return np.array([estimator(int(a_ ) , int(a_ ) , a_ ) for n, c in zip(a_ , a_ )] )
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return [tuple(a_ )] __A = [] def generate(a_ , a_ ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , a_ ) for i in range(k - 1 ): if k % 2 == 0: # k is even __A , __A = arr[k - 1], arr[i] else: # k is odd __A , __A = arr[k - 1], arr[0] generate(k - 1 , a_ ) generate(len(a_ ) , a_ ) return res if __name__ == "__main__": SCREAMING_SNAKE_CASE :int = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :Dict = [int(item) for item in user_input.split(',')] print(heaps(arr))
15
1
import argparse import collections import json import os import re import string import sys import numpy as np SCREAMING_SNAKE_CASE :Any = re.compile(R'\b(a|an|the)\b', re.UNICODE) SCREAMING_SNAKE_CASE :List[str] = None def UpperCAmelCase ( ) -> Any: """simple docstring""" __A = argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0." ) parser.add_argument("data_file" , metavar="data.json" , help="Input data JSON file." ) parser.add_argument("pred_file" , metavar="pred.json" , help="Model predictions." ) parser.add_argument( "--out-file" , "-o" , metavar="eval.json" , help="Write accuracy metrics to file (default is stdout)." ) parser.add_argument( "--na-prob-file" , "-n" , metavar="na_prob.json" , help="Model estimates of probability of no answer." ) parser.add_argument( "--na-prob-thresh" , "-t" , type=a_ , default=1.0 , help="Predict \"\" if no-answer probability exceeds this (default = 1.0)." , ) parser.add_argument( "--out-image-dir" , "-p" , metavar="out_images" , default=a_ , help="Save precision-recall curves to directory." ) parser.add_argument("--verbose" , "-v" , action="store_true" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" __A = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: __A = bool(qa["answers"]["text"] ) return qid_to_has_ans def UpperCAmelCase ( a_ ) -> str: """simple docstring""" def remove_articles(a_ ): return ARTICLES_REGEX.sub(" " , a_ ) def white_space_fix(a_ ): return " ".join(text.split() ) def remove_punc(a_ ): __A = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(a_ ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(a_ ) ) ) ) def UpperCAmelCase ( a_ ) -> Dict: """simple docstring""" if not s: return [] return normalize_answer(a_ ).split() def UpperCAmelCase ( a_ , a_ ) -> Tuple: """simple docstring""" return int(normalize_answer(a_ ) == normalize_answer(a_ ) ) def UpperCAmelCase ( a_ , a_ ) -> List[str]: """simple docstring""" __A = get_tokens(a_ ) __A = get_tokens(a_ ) __A = collections.Counter(a_ ) & collections.Counter(a_ ) __A = sum(common.values() ) if len(a_ ) == 0 or len(a_ ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 __A = 1.0 * num_same / len(a_ ) __A = 1.0 * num_same / len(a_ ) __A = (2 * precision * recall) / (precision + recall) return fa def UpperCAmelCase ( a_ , a_ ) -> Optional[Any]: """simple docstring""" __A = {} __A = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: __A = qa["id"] __A = [t for t in qa["answers"]["text"] if normalize_answer(a_ )] if not gold_answers: # For unanswerable questions, only correct answer is empty string __A = [""] if qid not in preds: print(F'''Missing prediction for {qid}''' ) continue __A = preds[qid] # Take max over all gold answers __A = max(compute_exact(a_ , a_ ) for a in gold_answers ) __A = max(compute_fa(a_ , a_ ) for a in gold_answers ) return exact_scores, fa_scores def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> Dict: """simple docstring""" __A = {} for qid, s in scores.items(): __A = na_probs[qid] > na_prob_thresh if pred_na: __A = float(not qid_to_has_ans[qid] ) else: __A = s return new_scores def UpperCAmelCase ( a_ , a_ , a_=None ) -> int: """simple docstring""" if not qid_list: __A = len(a_ ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: __A = len(a_ ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("f1", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("total", total), ] ) def UpperCAmelCase ( a_ , a_ , a_ ) -> str: """simple docstring""" for k in new_eval: __A = new_eval[k] def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> Union[str, Any]: """simple docstring""" plt.step(a_ , a_ , color="b" , alpha=0.2 , where="post" ) plt.fill_between(a_ , a_ , step="post" , alpha=0.2 , color="b" ) plt.xlabel("Recall" ) plt.ylabel("Precision" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(a_ ) plt.savefig(a_ ) plt.clf() def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_=None , a_=None ) -> int: """simple docstring""" __A = sorted(a_ , key=lambda a_ : na_probs[k] ) __A = 0.0 __A = 1.0 __A = 0.0 __A = [1.0] __A = [0.0] __A = 0.0 for i, qid in enumerate(a_ ): if qid_to_has_ans[qid]: true_pos += scores[qid] __A = true_pos / float(i + 1 ) __A = true_pos / float(a_ ) if i == len(a_ ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(a_ ) recalls.append(a_ ) if out_image: plot_pr_curve(a_ , a_ , a_ , a_ ) return {"ap": 100.0 * avg_prec} def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ , a_ ) -> List[str]: """simple docstring""" if out_image_dir and not os.path.exists(a_ ): os.makedirs(a_ ) __A = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return __A = make_precision_recall_eval( a_ , a_ , a_ , a_ , out_image=os.path.join(a_ , "pr_exact.png" ) , title="Precision-Recall curve for Exact Match score" , ) __A = make_precision_recall_eval( a_ , a_ , a_ , a_ , out_image=os.path.join(a_ , "pr_f1.png" ) , title="Precision-Recall curve for F1 score" , ) __A = {k: float(a_ ) for k, v in qid_to_has_ans.items()} __A = make_precision_recall_eval( a_ , a_ , a_ , a_ , out_image=os.path.join(a_ , "pr_oracle.png" ) , title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)" , ) merge_eval(a_ , a_ , "pr_exact" ) merge_eval(a_ , a_ , "pr_f1" ) merge_eval(a_ , a_ , "pr_oracle" ) def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> List[str]: """simple docstring""" if not qid_list: return __A = [na_probs[k] for k in qid_list] __A = np.ones_like(a_ ) / float(len(a_ ) ) plt.hist(a_ , weights=a_ , bins=2_0 , range=(0.0, 1.0) ) plt.xlabel("Model probability of no-answer" ) plt.ylabel("Proportion of dataset" ) plt.title(F'''Histogram of no-answer probability: {name}''' ) plt.savefig(os.path.join(a_ , F'''na_prob_hist_{name}.png''' ) ) plt.clf() def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> Any: """simple docstring""" __A = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) __A = num_no_ans __A = cur_score __A = 0.0 __A = sorted(a_ , key=lambda a_ : na_probs[k] ) for i, qid in enumerate(a_ ): if qid not in scores: continue if qid_to_has_ans[qid]: __A = scores[qid] else: if preds[qid]: __A = -1 else: __A = 0 cur_score += diff if cur_score > best_score: __A = cur_score __A = na_probs[qid] return 100.0 * best_score / len(a_ ), best_thresh def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ , a_ ) -> Tuple: """simple docstring""" __A , __A = find_best_thresh(a_ , a_ , a_ , a_ ) __A , __A = find_best_thresh(a_ , a_ , a_ , a_ ) __A = best_exact __A = exact_thresh __A = best_fa __A = fa_thresh def UpperCAmelCase ( ) -> int: """simple docstring""" with open(OPTS.data_file ) as f: __A = json.load(a_ ) __A = dataset_json["data"] with open(OPTS.pred_file ) as f: __A = json.load(a_ ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: __A = json.load(a_ ) else: __A = {k: 0.0 for k in preds} __A = make_qid_to_has_ans(a_ ) # maps qid to True/False __A = [k for k, v in qid_to_has_ans.items() if v] __A = [k for k, v in qid_to_has_ans.items() if not v] __A , __A = get_raw_scores(a_ , a_ ) __A = apply_no_ans_threshold(a_ , a_ , a_ , OPTS.na_prob_thresh ) __A = apply_no_ans_threshold(a_ , a_ , a_ , OPTS.na_prob_thresh ) __A = make_eval_dict(a_ , a_ ) if has_ans_qids: __A = make_eval_dict(a_ , a_ , qid_list=a_ ) merge_eval(a_ , a_ , "HasAns" ) if no_ans_qids: __A = make_eval_dict(a_ , a_ , qid_list=a_ ) merge_eval(a_ , a_ , "NoAns" ) if OPTS.na_prob_file: find_all_best_thresh(a_ , a_ , a_ , a_ , a_ , a_ ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(a_ , a_ , a_ , a_ , a_ , OPTS.out_image_dir ) histogram_na_prob(a_ , a_ , OPTS.out_image_dir , "hasAns" ) histogram_na_prob(a_ , a_ , OPTS.out_image_dir , "noAns" ) if OPTS.out_file: with open(OPTS.out_file , "w" ) as f: json.dump(a_ , a_ ) else: print(json.dumps(a_ , indent=2 ) ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :str = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt main()
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return lst __A = 1 while i < len(a_ ): if lst[i - 1] <= lst[i]: i += 1 else: __A , __A = lst[i], lst[i - 1] i -= 1 if i == 0: __A = 1 return lst if __name__ == "__main__": SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :List[Any] = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
15
1
from typing import List, Optional import numpy as np from ...processing_utils import ProcessorMixin from ...utils import to_numpy class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "EncodecFeatureExtractor" snake_case_ = ("T5Tokenizer", "T5TokenizerFast") def __init__( self : Tuple ,A : Any ,A : List[str] ): super().__init__(A ,A ) __A = self.feature_extractor __A = False def UpperCamelCase_ ( self : Optional[int] ,A : Tuple=None ,A : List[str]=None ,A : Union[str, Any]=True ): return self.tokenizer.get_decoder_prompt_ids(task=A ,language=A ,no_timestamps=A ) def __call__( self : int ,*A : Any ,**A : Any ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*A ,**A ) __A = kwargs.pop("audio" ,A ) __A = kwargs.pop("sampling_rate" ,A ) __A = kwargs.pop("text" ,A ) if len(A ) > 0: __A = args[0] __A = args[1:] if audio is None and text is None: raise ValueError("You need to specify either an `audio` or `text` input to process." ) if text is not None: __A = self.tokenizer(A ,**A ) if audio is not None: __A = self.feature_extractor(A ,*A ,sampling_rate=A ,**A ) if audio is None: return inputs elif text is None: return audio_inputs else: __A = audio_inputs["input_values"] if "padding_mask" in audio_inputs: __A = audio_inputs["padding_mask"] return inputs def UpperCamelCase_ ( self : List[Any] ,*A : Optional[Any] ,**A : Optional[int] ): __A = kwargs.pop("audio" ,A ) __A = kwargs.pop("padding_mask" ,A ) if len(A ) > 0: __A = args[0] __A = args[1:] if audio_values is not None: return self._decode_audio(A ,padding_mask=A ) else: return self.tokenizer.batch_decode(*A ,**A ) def UpperCamelCase_ ( self : List[Any] ,*A : Dict ,**A : Tuple ): return self.tokenizer.decode(*A ,**A ) def UpperCamelCase_ ( self : int ,A : Union[str, Any] ,A : Optional = None ): __A = to_numpy(A ) __A , __A , __A = audio_values.shape if padding_mask is None: return list(A ) __A = to_numpy(A ) # 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) __A = seq_len - padding_mask.shape[-1] __A = 1 - self.feature_extractor.padding_value __A = np.pad(A ,((0, 0), (0, difference)) ,"constant" ,constant_values=A ) __A = audio_values.tolist() for i in range(A ): __A = np.asarray(audio_values[i] )[ padding_mask[i][None, :] != self.feature_extractor.padding_value ] __A = sliced_audio.reshape(A ,-1 ) return audio_values
15
from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 snake_case_ = 42 snake_case_ = None class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 2 @register_to_config def __init__( self : str ,A : float = 0.02 ,A : float = 1_00 ,A : float = 1.0_07 ,A : float = 80 ,A : float = 0.05 ,A : float = 50 ,): # standard deviation of the initial noise distribution __A = sigma_max # setable values __A = None __A = None __A = None # sigma(t_i) def UpperCamelCase_ ( self : str ,A : torch.FloatTensor ,A : Optional[int] = None ): return sample def UpperCamelCase_ ( self : Dict ,A : int ,A : Union[str, torch.device] = None ): __A = num_inference_steps __A = np.arange(0 ,self.num_inference_steps )[::-1].copy() __A = torch.from_numpy(A ).to(A ) __A = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] __A = torch.tensor(A ,dtype=torch.floataa ,device=A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : torch.FloatTensor ,A : float ,A : Optional[torch.Generator] = None ): if self.config.s_min <= sigma <= self.config.s_max: __A = min(self.config.s_churn / self.num_inference_steps ,2**0.5 - 1 ) else: __A = 0 # sample eps ~ N(0, S_noise^2 * I) __A = self.config.s_noise * randn_tensor(sample.shape ,generator=A ).to(sample.device ) __A = sigma + gamma * sigma __A = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def UpperCamelCase_ ( self : Dict ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_hat + sigma_hat * model_output __A = (sample_hat - pred_original_sample) / sigma_hat __A = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : Optional[int] ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_prev + sigma_prev * model_output __A = (sample_prev - pred_original_sample) / sigma_prev __A = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : List[Any] ,A : Dict ,A : List[str] ,A : str ): raise NotImplementedError()
15
1
import argparse import os import re import packaging.version SCREAMING_SNAKE_CASE :int = 'examples/' SCREAMING_SNAKE_CASE :Dict = { 'examples': (re.compile(R'^check_min_version\("[^"]+"\)\s*$', re.MULTILINE), 'check_min_version("VERSION")\n'), 'init': (re.compile(R'^__version__\s+=\s+"([^"]+)"\s*$', re.MULTILINE), '__version__ = "VERSION"\n'), 'setup': (re.compile(R'^(\s*)version\s*=\s*"[^"]+",', re.MULTILINE), R'\1version="VERSION",'), 'doc': (re.compile(R'^(\s*)release\s*=\s*"[^"]+"$', re.MULTILINE), 'release = "VERSION"\n'), } SCREAMING_SNAKE_CASE :Optional[Any] = { 'init': 'src/transformers/__init__.py', 'setup': 'setup.py', } SCREAMING_SNAKE_CASE :Optional[Any] = 'README.md' def UpperCAmelCase ( a_ , a_ , a_ ) -> Optional[Any]: """simple docstring""" with open(a_ , "r" , encoding="utf-8" , newline="\n" ) as f: __A = f.read() __A , __A = REPLACE_PATTERNS[pattern] __A = replace.replace("VERSION" , a_ ) __A = re_pattern.sub(a_ , a_ ) with open(a_ , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(a_ ) def UpperCAmelCase ( a_ ) -> Optional[Any]: """simple docstring""" for folder, directories, fnames in os.walk(a_ ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("research_projects" ) if "legacy" in directories: directories.remove("legacy" ) for fname in fnames: if fname.endswith(".py" ): update_version_in_file(os.path.join(a_ , a_ ) , a_ , pattern="examples" ) def UpperCAmelCase ( a_ , a_=False ) -> Any: """simple docstring""" for pattern, fname in REPLACE_FILES.items(): update_version_in_file(a_ , a_ , a_ ) if not patch: update_version_in_examples(a_ ) def UpperCAmelCase ( ) -> List[Any]: """simple docstring""" __A = "🤗 Transformers currently provides the following architectures" __A = "1. Want to contribute a new model?" with open(a_ , "r" , encoding="utf-8" , newline="\n" ) as f: __A = f.readlines() # Find the start of the list. __A = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __A = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("1." ): __A = lines[index].replace( "https://huggingface.co/docs/transformers/main/model_doc" , "https://huggingface.co/docs/transformers/model_doc" , ) index += 1 with open(a_ , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(a_ ) def UpperCAmelCase ( ) -> Dict: """simple docstring""" with open(REPLACE_FILES["init"] , "r" ) as f: __A = f.read() __A = REPLACE_PATTERNS["init"][0].search(a_ ).groups()[0] return packaging.version.parse(a_ ) def UpperCAmelCase ( a_=False ) -> Dict: """simple docstring""" __A = get_version() if patch and default_version.is_devrelease: raise ValueError("Can't create a patch version from the dev branch, checkout a released version!" ) if default_version.is_devrelease: __A = default_version.base_version elif patch: __A = F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}''' else: __A = F'''{default_version.major}.{default_version.minor + 1}.0''' # Now let's ask nicely if that's the right one. __A = input(F'''Which version are you releasing? [{default_version}]''' ) if len(a_ ) == 0: __A = default_version print(F'''Updating version to {version}.''' ) global_version_update(a_ , patch=a_ ) if not patch: print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() def UpperCAmelCase ( ) -> List[Any]: """simple docstring""" __A = get_version() __A = F'''{current_version.major}.{current_version.minor + 1}.0.dev0''' __A = current_version.base_version # Check with the user we got that right. __A = input(F'''Which version are we developing now? [{dev_version}]''' ) if len(a_ ) == 0: __A = dev_version print(F'''Updating version to {version}.''' ) global_version_update(a_ ) print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() if __name__ == "__main__": SCREAMING_SNAKE_CASE :str = argparse.ArgumentParser() parser.add_argument('--post_release', action='store_true', help='Whether this is pre or post release.') parser.add_argument('--patch', action='store_true', help='Whether or not this is a patch release.') SCREAMING_SNAKE_CASE :Dict = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('Nothing to do after a patch :-)') else: post_release_work()
15
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version SCREAMING_SNAKE_CASE :Union[str, Any] = get_logger(__name__) class UpperCAmelCase : '''simple docstring''' snake_case_ = "dummy_data" snake_case_ = "datasets" snake_case_ = False def __init__( self : Optional[int] ,A : str ,A : str ,A : Union[Version, str] ,A : Optional[str] = None ,A : bool = False ,A : bool = True ,A : Optional[List[Callable]] = None ,): __A = 0 __A = dataset_name __A = cache_dir __A = use_local_dummy_data __A = config # download_callbacks take a single url as input __A = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root __A = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general __A = str(A ) # to be downloaded __A = None __A = None @property def UpperCamelCase_ ( self : Union[str, Any] ): if self._dummy_file is None: __A = self.download_dummy_data() return self._dummy_file @property def UpperCamelCase_ ( self : Optional[Any] ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("dummy" ,self.config.name ,self.version_name ) # structure is dummy / version_name return os.path.join("dummy" ,self.version_name ) @property def UpperCamelCase_ ( self : List[Any] ): return os.path.join(self.dummy_data_folder ,"dummy_data.zip" ) def UpperCamelCase_ ( self : Tuple ): __A = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) __A = cached_path( A ,cache_dir=self.cache_dir ,extract_compressed_file=A ,force_extract=A ) return os.path.join(A ,self.dummy_file_name ) @property def UpperCamelCase_ ( self : str ): return os.path.join(self.datasets_scripts_dir ,self.dataset_name ,self.dummy_zip_file ) @property def UpperCamelCase_ ( self : Any ): if self._bucket_url is None: __A = hf_github_url(self.dataset_name ,self.dummy_zip_file.replace(os.sep ,"/" ) ) return self._bucket_url @property def UpperCamelCase_ ( self : Tuple ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep ,"/" ).split("/" )[:-1] ) def UpperCamelCase_ ( self : List[str] ,A : List[Any] ,*A : Dict ): if self.load_existing_dummy_data: # dummy data is downloaded and tested __A = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned __A = self.dummy_file_name # special case when data_url is a dict if isinstance(A ,A ): return self.create_dummy_data_dict(A ,A ) elif isinstance(A ,(list, tuple) ): return self.create_dummy_data_list(A ,A ) else: return self.create_dummy_data_single(A ,A ) def UpperCamelCase_ ( self : str ,A : List[Any] ,*A : List[Any] ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : List[str] ,A : List[str] ,A : Tuple ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : Any ,A : Any ,*A : Optional[Any] ,**A : List[str] ): return path def UpperCamelCase_ ( self : str ): return {} def UpperCamelCase_ ( self : int ,A : int ,A : Tuple ): __A = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(A ,A ): for single_url in single_urls: download_callback(A ) else: __A = single_urls download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(A ,A ): __A = [os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) for x in single_urls] else: __A = single_urls __A = os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) __A = value # make sure that values are unique if all(isinstance(A ,A ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique __A = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,A : str ): __A = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one __A = all(bool(re.findall("[0-9]{3,}-of-[0-9]{3,}" ,A ) ) for url in data_url ) __A = all( url.startswith("https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): __A = [data_url[0]] * len(A ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(single_url.split("/" )[-1] ) ) dummy_data_list.append(A ) return dummy_data_list def UpperCamelCase_ ( self : str ,A : List[Any] ,A : Optional[Any] ): for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(data_url.split("/" )[-1] ) ) if os.path.exists(A ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def UpperCamelCase_ ( self : int ): pass def UpperCamelCase_ ( self : Dict ): pass def UpperCamelCase_ ( self : Optional[Any] ,A : List[Any] ): def _iter_archive_members(A : Optional[Any] ): # this preserves the order of the members inside the ZIP archive __A = Path(self.dummy_file ).parent __A = path.relative_to(A ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: __A = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(A ) __A = Path(A ) __A = _iter_archive_members(A ) if self.use_local_dummy_data else path.rglob("*" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((".", "__") ): yield file_path.relative_to(A ).as_posix(), file_path.open("rb" ) def UpperCamelCase_ ( self : List[Any] ,A : Any ): if not isinstance(A ,A ): __A = [paths] for path in paths: if os.path.isfile(A ): if os.path.basename(A ).startswith((".", "__") ): return yield path else: for dirpath, dirnames, filenames in os.walk(A ): if os.path.basename(A ).startswith((".", "__") ): continue dirnames.sort() for filename in sorted(A ): if filename.startswith((".", "__") ): continue yield os.path.join(A ,A )
15
1
SCREAMING_SNAKE_CASE :List[str] = '0.18.2' from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
15
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available SCREAMING_SNAKE_CASE :List[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :List[str] = ['BartphoTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys SCREAMING_SNAKE_CASE :Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
15
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( HubertConfig, HubertForCTC, HubertModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() SCREAMING_SNAKE_CASE :int = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Dict = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ ) -> Optional[Any]: """simple docstring""" for attribute in key.split("." ): __A = getattr(a_ , a_ ) if weight_type is not None: __A = getattr(a_ , a_ ).shape else: __A = hf_pointer.shape assert hf_shape == value.shape, ( F'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be''' F''' {value.shape} for {full_name}''' ) if weight_type == "weight": __A = value elif weight_type == "weight_g": __A = value elif weight_type == "weight_v": __A = value elif weight_type == "bias": __A = value else: __A = value logger.info(F'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.''' ) def UpperCAmelCase ( a_ , a_ , a_ ) -> Optional[Any]: """simple docstring""" __A = [] __A = fairseq_model.state_dict() __A = hf_model.hubert.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): __A = False if "conv_layers" in name: load_conv_layer( a_ , a_ , a_ , a_ , hf_model.config.feat_extract_norm == "group" , ) __A = True else: for key, mapped_key in MAPPING.items(): __A = "hubert." + mapped_key if (is_finetuned and mapped_key != "lm_head") else mapped_key if key in name or (key.split("w2v_model." )[-1] == name.split("." )[0] and not is_finetuned): __A = True if "*" in mapped_key: __A = name.split(a_ )[0].split("." )[-2] __A = mapped_key.replace("*" , a_ ) if "weight_g" in name: __A = "weight_g" elif "weight_v" in name: __A = "weight_v" elif "weight" in name: __A = "weight" elif "bias" in name: __A = "bias" else: __A = None set_recursively(a_ , a_ , a_ , a_ , a_ ) continue if not is_used: unused_weights.append(a_ ) logger.warning(F'''Unused weights: {unused_weights}''' ) def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ ) -> str: """simple docstring""" __A = full_name.split("conv_layers." )[-1] __A = name.split("." ) __A = int(items[0] ) __A = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) __A = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) __A = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) __A = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) __A = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(a_ ) @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_=None , a_=None , a_=True ) -> Tuple: """simple docstring""" if config_path is not None: __A = HubertConfig.from_pretrained(a_ ) else: __A = HubertConfig() if is_finetuned: if dict_path: __A = Dictionary.load(a_ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __A = target_dict.pad_index __A = target_dict.bos_index __A = target_dict.eos_index __A = len(target_dict.symbols ) __A = os.path.join(a_ , "vocab.json" ) if not os.path.isdir(a_ ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(a_ ) ) return os.makedirs(a_ , exist_ok=a_ ) with open(a_ , "w" , encoding="utf-8" ) as vocab_handle: json.dump(target_dict.indices , a_ ) __A = WavaVecaCTCTokenizer( a_ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=a_ , ) __A = True if config.feat_extract_norm == "layer" else False __A = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6_0_0_0 , padding_value=0 , do_normalize=a_ , return_attention_mask=a_ , ) __A = WavaVecaProcessor(feature_extractor=a_ , tokenizer=a_ ) processor.save_pretrained(a_ ) __A = HubertForCTC(a_ ) else: __A = HubertModel(a_ ) if is_finetuned: __A , __A , __A = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: __A , __A , __A = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) __A = model[0].eval() recursively_load_weights(a_ , a_ , a_ ) hf_wavavec.save_pretrained(a_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Optional[Any] = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) SCREAMING_SNAKE_CASE :List[Any] = parser.parse_args() convert_hubert_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
15
from typing import Dict, Optional import numpy as np import datasets SCREAMING_SNAKE_CASE :List[Any] = '\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n' SCREAMING_SNAKE_CASE :List[str] = '\nArgs:\n predictions (`List[ndarray]`):\n List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n references (`List[ndarray]`):\n List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n num_labels (`int`):\n Number of classes (categories).\n ignore_index (`int`):\n Index that will be ignored during evaluation.\n nan_to_num (`int`, *optional*):\n If specified, NaN values will be replaced by the number defined by the user.\n label_map (`dict`, *optional*):\n If specified, dictionary mapping old label indices to new label indices.\n reduce_labels (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n `Dict[str, float | ndarray]` comprising various elements:\n - *mean_iou* (`float`):\n Mean Intersection-over-Union (IoU averaged over all categories).\n - *mean_accuracy* (`float`):\n Mean accuracy (averaged over all categories).\n - *overall_accuracy* (`float`):\n Overall accuracy on all images.\n - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n Per category accuracy.\n - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n Per category IoU.\n\nExamples:\n\n >>> import numpy as np\n\n >>> mean_iou = datasets.load_metric("mean_iou")\n\n >>> # suppose one has 3 different segmentation maps predicted\n >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n >>> predicted = [predicted_1, predicted_2, predicted_3]\n >>> ground_truth = [actual_1, actual_2, actual_3]\n\n >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), \'per_category_accuracy\': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])}\n' SCREAMING_SNAKE_CASE :str = '\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}' def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Tuple: """simple docstring""" if label_map is not None: for old_id, new_id in label_map.items(): __A = new_id # turn into Numpy arrays __A = np.array(a_ ) __A = np.array(a_ ) if reduce_labels: __A = 2_5_5 __A = label - 1 __A = 2_5_5 __A = label != ignore_index __A = np.not_equal(a_ , a_ ) __A = pred_label[mask] __A = np.array(a_ )[mask] __A = pred_label[pred_label == label] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Union[str, Any]: """simple docstring""" __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(a_ , a_ ): __A , __A , __A , __A = intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = None , a_ = False , ) -> str: """simple docstring""" __A , __A , __A , __A = total_intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) # compute metrics __A = {} __A = total_area_intersect.sum() / total_area_label.sum() __A = total_area_intersect / total_area_union __A = total_area_intersect / total_area_label __A = np.nanmean(a_ ) __A = np.nanmean(a_ ) __A = all_acc __A = iou __A = acc if nan_to_num is not None: __A = {metric: np.nan_to_num(a_ , nan=a_ ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { "predictions": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), "references": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), } ) ,reference_urls=[ "https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py" ] ,) def UpperCamelCase_ ( self : int ,A : Optional[Any] ,A : Optional[Any] ,A : int ,A : bool ,A : Optional[int] = None ,A : Optional[Dict[int, int]] = None ,A : bool = False ,): __A = mean_iou( results=A ,gt_seg_maps=A ,num_labels=A ,ignore_index=A ,nan_to_num=A ,label_map=A ,reduce_labels=A ,) return iou_result
15
1
import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotConfig, is_flax_available from transformers.testing_utils import jax_device, require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html SCREAMING_SNAKE_CASE :str = 'platform' import jax import jax.numpy as jnp from transformers import BlenderbotTokenizer from transformers.models.blenderbot.modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, shift_tokens_right, ) def UpperCAmelCase ( a_ , a_ , a_=None , a_=None , a_=None , a_=None , a_=None , a_=None , ) -> Any: """simple docstring""" if attention_mask is None: __A = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: __A = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: __A = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: __A = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: __A = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class UpperCAmelCase : '''simple docstring''' def __init__( self : List[str] ,A : Tuple ,A : Tuple=13 ,A : List[Any]=7 ,A : Optional[int]=True ,A : List[Any]=False ,A : Optional[int]=99 ,A : Union[str, Any]=16 ,A : Union[str, Any]=2 ,A : Optional[int]=4 ,A : Union[str, Any]=4 ,A : str="gelu" ,A : Union[str, Any]=0.1 ,A : Any=0.1 ,A : Tuple=32 ,A : Any=2 ,A : Optional[int]=1 ,A : Union[str, Any]=0 ,A : Optional[int]=0.02 ,): __A = parent __A = batch_size __A = seq_length __A = is_training __A = use_labels __A = vocab_size __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = eos_token_id __A = pad_token_id __A = bos_token_id __A = initializer_range def UpperCamelCase_ ( self : int ): __A = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) ,3 ,self.vocab_size ) __A = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) ,dtype=np.intaa )) ,-1 ) __A = shift_tokens_right(A ,1 ,2 ) __A = BlenderbotConfig( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_id=self.eos_token_id ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,initializer_range=self.initializer_range ,use_cache=A ,) __A = prepare_blenderbot_inputs_dict(A ,A ,A ) return config, inputs_dict def UpperCamelCase_ ( self : Optional[Any] ): __A , __A = self.prepare_config_and_inputs() return config, inputs_dict def UpperCamelCase_ ( self : List[str] ,A : str ,A : Tuple ,A : Any ): __A = 20 __A = model_class_name(A ) __A = model.encode(inputs_dict["input_ids"] ) __A , __A = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) __A = model.init_cache(decoder_input_ids.shape[0] ,A ,A ) __A = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) ,dtype="i4" ) __A = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] ,(decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) ,) __A = model.decode( decoder_input_ids[:, :-1] ,A ,decoder_attention_mask=A ,past_key_values=A ,decoder_position_ids=A ,) __A = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] ,dtype="i4" ) __A = model.decode( decoder_input_ids[:, -1:] ,A ,decoder_attention_mask=A ,past_key_values=outputs_cache.past_key_values ,decoder_position_ids=A ,) __A = model.decode(A ,A ) __A = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 ,msg=f'''Max diff is {diff}''' ) def UpperCamelCase_ ( self : Dict ,A : Any ,A : int ,A : Optional[int] ): __A = 20 __A = model_class_name(A ) __A = model.encode(inputs_dict["input_ids"] ) __A , __A = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) __A = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] ,axis=-1 ,) __A = model.init_cache(decoder_input_ids.shape[0] ,A ,A ) __A = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] ,(decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) ,) __A = model.decode( decoder_input_ids[:, :-1] ,A ,decoder_attention_mask=A ,past_key_values=A ,decoder_position_ids=A ,) __A = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] ,dtype="i4" ) __A = model.decode( decoder_input_ids[:, -1:] ,A ,past_key_values=outputs_cache.past_key_values ,decoder_attention_mask=A ,decoder_position_ids=A ,) __A = model.decode(A ,A ,decoder_attention_mask=A ) __A = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 ,msg=f'''Max diff is {diff}''' ) @require_flax class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' snake_case_ = 99 def UpperCamelCase_ ( self : str ): __A = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] ,dtype=np.intaa ,) __A = input_ids.shape[0] __A = BlenderbotConfig( vocab_size=self.vocab_size ,d_model=24 ,encoder_layers=2 ,decoder_layers=2 ,encoder_attention_heads=2 ,decoder_attention_heads=2 ,encoder_ffn_dim=32 ,decoder_ffn_dim=32 ,max_position_embeddings=48 ,eos_token_id=2 ,pad_token_id=1 ,bos_token_id=0 ,) return config, input_ids, batch_size def UpperCamelCase_ ( self : Dict ): __A , __A , __A = self._get_config_and_data() __A = FlaxBlenderbotForConditionalGeneration(A ) __A = lm_model(input_ids=A ) __A = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs["logits"].shape ,A ) def UpperCamelCase_ ( self : Any ): __A = BlenderbotConfig( vocab_size=self.vocab_size ,d_model=14 ,encoder_layers=2 ,decoder_layers=2 ,encoder_attention_heads=2 ,decoder_attention_heads=2 ,encoder_ffn_dim=8 ,decoder_ffn_dim=8 ,max_position_embeddings=48 ,) __A = FlaxBlenderbotForConditionalGeneration(A ) __A = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] ,dtype=np.intaa ) __A = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] ,dtype=np.intaa ) __A = lm_model(input_ids=A ,decoder_input_ids=A ) __A = (*summary.shape, config.vocab_size) self.assertEqual(outputs["logits"].shape ,A ) def UpperCamelCase_ ( self : Any ): __A = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] ,dtype=np.intaa ) __A = shift_tokens_right(A ,1 ,2 ) __A = np.equal(A ,1 ).astype(np.floataa ).sum() __A = np.equal(A ,1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape ,input_ids.shape ) self.assertEqual(A ,n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] ,2 ).all() ) @require_flax class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase , __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = True snake_case_ = ( ( FlaxBlenderbotModel, FlaxBlenderbotForConditionalGeneration, ) if is_flax_available() else () ) snake_case_ = (FlaxBlenderbotForConditionalGeneration,) if is_flax_available() else () def UpperCamelCase_ ( self : str ): __A = FlaxBlenderbotModelTester(self ) def UpperCamelCase_ ( self : Optional[Any] ): __A , __A = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(A ,A ,A ) def UpperCamelCase_ ( self : List[Any] ): __A , __A = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(A ,A ,A ) def UpperCamelCase_ ( self : Optional[Any] ): __A , __A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __A = self._prepare_for_class(A ,A ) __A = model_class(A ) @jax.jit def encode_jitted(A : Any ,A : Optional[Any]=None ,**A : str ): return model.encode(input_ids=A ,attention_mask=A ) with self.subTest("JIT Enabled" ): __A = encode_jitted(**A ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __A = encode_jitted(**A ).to_tuple() self.assertEqual(len(A ) ,len(A ) ) for jitted_output, output in zip(A ,A ): self.assertEqual(jitted_output.shape ,output.shape ) def UpperCamelCase_ ( self : List[Any] ): __A , __A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __A = model_class(A ) __A = model.encode(inputs_dict["input_ids"] ,inputs_dict["attention_mask"] ) __A = { "decoder_input_ids": inputs_dict["decoder_input_ids"], "decoder_attention_mask": inputs_dict["decoder_attention_mask"], "encoder_outputs": encoder_outputs, } @jax.jit def decode_jitted(A : List[Any] ,A : Optional[int] ,A : Tuple ): return model.decode( decoder_input_ids=A ,decoder_attention_mask=A ,encoder_outputs=A ,) with self.subTest("JIT Enabled" ): __A = decode_jitted(**A ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __A = decode_jitted(**A ).to_tuple() self.assertEqual(len(A ) ,len(A ) ) for jitted_output, output in zip(A ,A ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def UpperCamelCase_ ( self : Tuple ): for model_class_name in self.all_model_classes: __A = model_class_name.from_pretrained("facebook/blenderbot-400M-distill" ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids __A = np.ones((1, 1) ) * model.config.eos_token_id __A = model(A ) self.assertIsNotNone(A ) @unittest.skipUnless(jax_device != "cpu" ,"3B test too slow on CPU." ) @slow def UpperCamelCase_ ( self : Tuple ): __A = {"num_beams": 1, "early_stopping": True, "min_length": 15, "max_length": 25} __A = {"skip_special_tokens": True, "clean_up_tokenization_spaces": True} __A = FlaxBlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-3B" ,from_pt=A ) __A = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-3B" ) __A = ["Sam"] __A = tokenizer(A ,return_tensors="jax" ) __A = model.generate(**A ,**A ) __A = "Sam is a great name. It means \"sun\" in Gaelic." __A = tokenizer.batch_decode(A ,**A ) assert generated_txt[0].strip() == tgt_text
15
import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE :List[str] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[str] = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Dict = { 'vocab_file': { 'AI-Sweden/gpt-sw3-126m': 'https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-350m': 'https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-1.6b': 'https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-6.7b': 'https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-20b': 'https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model', } } SCREAMING_SNAKE_CASE :Optional[Any] = { 'AI-Sweden/gpt-sw3-126m': 2048, 'AI-Sweden/gpt-sw3-350m': 2048, 'AI-Sweden/gpt-sw3-1.6b': 2048, 'AI-Sweden/gpt-sw3-6.7b': 2048, 'AI-Sweden/gpt-sw3-20b': 2048, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[int] ,A : Optional[Any] ,A : Optional[int]=False ,A : int=False ,A : Union[str, Any]=False ,A : int=None ,A : Optional[Any]=None ,A : Union[str, Any]=None ,A : Optional[Any]=None ,A : Optional[Dict[str, Any]] = None ,**A : Tuple ,): __A = {} if sp_model_kwargs is None else sp_model_kwargs __A = kwargs.get("name_or_path" ) if name_or_path is None: logger.warning( "name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b," " you are testing the model, this can safely be ignored" ) __A = "None" # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing __A = "<|endoftext|>" if eos_token is None else eos_token __A = "<unk>" if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: __A = unk_token if pad_token is None else pad_token __A = eos_token if bos_token is None else bos_token else: __A = "<pad>" if pad_token is None else pad_token __A = "<s>" if bos_token is None else bos_token super().__init__( do_lower_case=A ,remove_space=A ,keep_accents=A ,bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = do_lower_case __A = remove_space __A = keep_accents __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) # Used for whitespace normalization in input texts # fmt : off __A = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", "„"} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing __A = re.compile( f'''[{''.join(map(A ,list(range(0 ,9 ) ) + list(range(11 ,32 ) ) + list(range(1_27 ,1_60 ) ) + [1_60, 1_73, 82_03] ) )}]''' ) def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Optional[Any] ,A : Union[str, Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def UpperCamelCase_ ( self : List[str] ): return len(self.sp_model ) def UpperCamelCase_ ( self : int ,A : str ): __A = self.non_printing_characters_re.sub("" ,A ) # Normalize whitespaces __A = "".join([char if char not in self.whitespaces else " " for char in text] ) # NFC Unicode normalization __A = unicodedata.normalize("NFC" ,A ) return text def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,**A : Optional[int] ): __A = self.preprocess_text(A ) return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : int ): return self.sp_model.IdToPiece(A ) @staticmethod def UpperCamelCase_ ( A : str ): return out_string def UpperCamelCase_ ( self : str ,A : List[str] ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string def UpperCamelCase_ ( self : str ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : List[str] ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, List[str]] ,A : Union[str, bool] = False ): if isinstance(A ,A ): __A = self.preprocess_text(A ) __A = self.sp_model.encode(A ) else: __A = [self.preprocess_text(A ) for t in text] __A = self.sp_model.encode(A ) if return_tensors is True or return_tensors == "pt": __A = torch.tensor(A ) return token_ids def UpperCamelCase_ ( self : List[Any] ,A : Union[int, List[int]] ): return self.sp_model.decode(A ) def UpperCamelCase_ ( self : List[str] ,A : "Conversation" ): __A = [f'''User: {text}''' if is_user else f'''Bot: {text}''' for is_user, text in conversation.iter_texts()] __A = ( f'''{self.eos_token}{self.bos_token}''' + f'''{self.bos_token}'''.join(A ) + f'''{self.bos_token}Bot:''' ) return self.encode(text=A )
15
1
from __future__ import annotations import time SCREAMING_SNAKE_CASE :Dict = list[tuple[int, int]] SCREAMING_SNAKE_CASE :Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] SCREAMING_SNAKE_CASE :List[Any] = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right class UpperCAmelCase : '''simple docstring''' def __init__( self : List[Any] ,A : int ,A : int ,A : int ,A : int ,A : Node | None ): __A = pos_x __A = pos_y __A = (pos_y, pos_x) __A = goal_x __A = goal_y __A = parent class UpperCAmelCase : '''simple docstring''' def __init__( self : List[str] ,A : tuple[int, int] ,A : tuple[int, int] ): __A = Node(start[1] ,start[0] ,goal[1] ,goal[0] ,A ) __A = Node(goal[1] ,goal[0] ,goal[1] ,goal[0] ,A ) __A = [self.start] __A = False def UpperCamelCase_ ( self : Tuple ): while self.node_queue: __A = self.node_queue.pop(0 ) if current_node.pos == self.target.pos: __A = True return self.retrace_path(A ) __A = self.get_successors(A ) for node in successors: self.node_queue.append(A ) if not self.reached: return [self.start.pos] return None def UpperCamelCase_ ( self : List[str] ,A : Node ): __A = [] for action in delta: __A = parent.pos_x + action[1] __A = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(A ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node(A ,A ,self.target.pos_y ,self.target.pos_x ,A ) ) return successors def UpperCamelCase_ ( self : Optional[int] ,A : Node | None ): __A = node __A = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __A = current_node.parent path.reverse() return path class UpperCAmelCase : '''simple docstring''' def __init__( self : List[str] ,A : Tuple ,A : Union[str, Any] ): __A = BreadthFirstSearch(A ,A ) __A = BreadthFirstSearch(A ,A ) __A = False def UpperCamelCase_ ( self : Tuple ): while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue: __A = self.fwd_bfs.node_queue.pop(0 ) __A = self.bwd_bfs.node_queue.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: __A = True return self.retrace_bidirectional_path( A ,A ) __A = current_bwd_node __A = current_fwd_node __A = { self.fwd_bfs: self.fwd_bfs.get_successors(A ), self.bwd_bfs: self.bwd_bfs.get_successors(A ), } for bfs in [self.fwd_bfs, self.bwd_bfs]: for node in successors[bfs]: bfs.node_queue.append(A ) if not self.reached: return [self.fwd_bfs.start.pos] return None def UpperCamelCase_ ( self : Dict ,A : Node ,A : Node ): __A = self.fwd_bfs.retrace_path(A ) __A = self.bwd_bfs.retrace_path(A ) bwd_path.pop() bwd_path.reverse() __A = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] import doctest doctest.testmod() SCREAMING_SNAKE_CASE :Optional[int] = (0, 0) SCREAMING_SNAKE_CASE :List[Any] = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) SCREAMING_SNAKE_CASE :List[Any] = time.time() SCREAMING_SNAKE_CASE :List[str] = BreadthFirstSearch(init, goal) SCREAMING_SNAKE_CASE :str = bfs.search() SCREAMING_SNAKE_CASE :Optional[Any] = time.time() - start_bfs_time print('Unidirectional BFS computation time : ', bfs_time) SCREAMING_SNAKE_CASE :List[str] = time.time() SCREAMING_SNAKE_CASE :Dict = BidirectionalBreadthFirstSearch(init, goal) SCREAMING_SNAKE_CASE :Dict = bd_bfs.search() SCREAMING_SNAKE_CASE :Any = time.time() - start_bd_bfs_time print('Bidirectional BFS computation time : ', bd_bfs_time)
15
import numpy as np def UpperCAmelCase ( a_ , a_ , a_ = 1E-12 , a_ = 1_0_0 , ) -> tuple[float, np.ndarray]: """simple docstring""" assert np.shape(a_ )[0] == np.shape(a_ )[1] # Ensure proper dimensionality. assert np.shape(a_ )[0] == np.shape(a_ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(a_ ) == np.iscomplexobj(a_ ) __A = np.iscomplexobj(a_ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(a_ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. __A = False __A = 0 __A = 0 __A = 1E12 while not convergence: # Multiple matrix by the vector. __A = np.dot(a_ , a_ ) # Normalize the resulting output vector. __A = w / np.linalg.norm(a_ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) __A = vector.conj().T if is_complex else vector.T __A = np.dot(a_ , np.dot(a_ , a_ ) ) # Check convergence. __A = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: __A = True __A = lambda_ if is_complex: __A = np.real(lambda_ ) return lambda_, vector def UpperCAmelCase ( ) -> None: """simple docstring""" __A = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) __A = np.array([4_1, 4, 2_0] ) __A = real_input_matrix.astype(np.complexaaa ) __A = np.triu(1J * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T __A = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": __A = real_input_matrix __A = real_vector elif problem_type == "complex": __A = complex_input_matrix __A = complex_vector # Our implementation. __A , __A = power_iteration(a_ , a_ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). __A , __A = np.linalg.eigh(a_ ) # Last eigenvalue is the maximum one. __A = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. __A = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1E-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(a_ ) - np.abs(a_ ) ) <= 1E-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
15
1
import json import os import re import sys import urllib.request import requests from bsa import BeautifulSoup SCREAMING_SNAKE_CASE :Tuple = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' ' (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582' } def UpperCAmelCase ( a_ = "dhaka" , a_ = 5 ) -> int: """simple docstring""" __A = min(a_ , 5_0 ) # Prevent abuse! __A = { "q": query, "tbm": "isch", "hl": "en", "ijn": "0", } __A = requests.get("https://www.google.com/search" , params=a_ , headers=a_ ) __A = BeautifulSoup(html.text , "html.parser" ) __A = "".join( re.findall(r"AF_initDataCallback\(([^<]+)\);" , str(soup.select("script" ) ) ) ) __A = json.dumps(a_ ) __A = json.loads(a_ ) __A = re.findall( r"\[\"GRID_STATE0\",null,\[\[1,\[0,\".*?\",(.*),\"All\"," , a_ , ) if not matched_google_image_data: return 0 __A = re.sub( r"\[\"(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\",\d+,\d+\]" , "" , str(a_ ) , ) __A = re.findall( r"(?:'|,),\[\"(https:|http.*?)\",\d+,\d+\]" , a_ , ) for index, fixed_full_res_image in enumerate(a_ ): if index >= max_images: return index __A = bytes(a_ , "ascii" ).decode( "unicode-escape" ) __A = bytes(a_ , "ascii" ).decode( "unicode-escape" ) __A = urllib.request.build_opener() __A = [ ( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" " (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582", ) ] urllib.request.install_opener(a_ ) __A = F'''query_{query.replace(' ' , '_' )}''' if not os.path.exists(a_ ): os.makedirs(a_ ) urllib.request.urlretrieve( # noqa: S310 a_ , F'''{path_name}/original_size_img_{index}.jpg''' ) return index if __name__ == "__main__": try: SCREAMING_SNAKE_CASE :Dict = download_images_from_google_query(sys.argv[1]) print(f'''{image_count} images were downloaded to disk.''') except IndexError: print('Please provide a search term.') raise
15
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig SCREAMING_SNAKE_CASE :str = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE :str = 'RegNetConfig' # Base docstring SCREAMING_SNAKE_CASE :List[str] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Union[str, Any] = [1, 1088, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE :Optional[int] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Any = 'tabby, tabby cat' SCREAMING_SNAKE_CASE :Optional[int] = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : int ,A : int = 3 ,A : int = 1 ,A : int = 1 ,A : Optional[str] = "relu" ,**A : Dict ,): super().__init__(**A ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb __A = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=A ,strides=A ,padding="VALID" ,groups=A ,use_bias=A ,name="convolution" ,) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) __A = ACTaFN[activation] if activation is not None else tf.identity def UpperCamelCase_ ( self : List[Any] ,A : Any ): __A = self.convolution(self.padding(A ) ) __A = self.normalization(A ) __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : RegNetConfig ,**A : str ): super().__init__(**A ) __A = config.num_channels __A = TFRegNetConvLayer( out_channels=config.embedding_size ,kernel_size=3 ,stride=2 ,activation=config.hidden_act ,name="embedder" ,) def UpperCamelCase_ ( self : Tuple ,A : Optional[Any] ): __A = shape_list(A )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( "Make sure that the channel dimension of the pixel values match with the one set in the configuration." ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) __A = tf.transpose(A ,perm=(0, 2, 3, 1) ) __A = self.embedder(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Optional[int] ,A : int ,A : int = 2 ,**A : Tuple ): super().__init__(**A ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=1 ,strides=A ,use_bias=A ,name="convolution" ) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) def UpperCamelCase_ ( self : Union[str, Any] ,A : tf.Tensor ,A : bool = False ): return self.normalization(self.convolution(A ) ,training=A ) class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Dict ,A : int ,A : int ,**A : str ): super().__init__(**A ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) __A = [ tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="relu" ,name="attention.0" ), tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="sigmoid" ,name="attention.2" ), ] def UpperCamelCase_ ( self : Dict ,A : List[Any] ): # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] __A = self.pooler(A ) for layer_module in self.attention: __A = layer_module(A ) __A = hidden_state * pooled return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : Optional[int] ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.2" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : int ,A : Optional[int] ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : str ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetSELayer(A ,reduced_channels=int(round(in_channels / 4 ) ) ,name="layer.2" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.3" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : Dict ,A : Any ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 2 ,A : int = 2 ,**A : Optional[int] ): super().__init__(**A ) __A = TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer __A = [ # downsampling is done in the first layer with stride of 2 layer(A ,A ,A ,stride=A ,name="layers.0" ), *[layer(A ,A ,A ,name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCamelCase_ ( self : Any ,A : List[str] ): for layer_module in self.layers: __A = layer_module(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Any ,A : RegNetConfig ,**A : List[str] ): super().__init__(**A ) __A = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A ,config.embedding_size ,config.hidden_sizes[0] ,stride=2 if config.downsample_in_first_stage else 1 ,depth=config.depths[0] ,name="stages.0" ,) ) __A = zip(config.hidden_sizes ,config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A ,config.depths[1:] ) ): self.stages.append(TFRegNetStage(A ,A ,A ,depth=A ,name=f'''stages.{i+1}''' ) ) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor ,A : bool = False ,A : bool = True ): __A = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: __A = hidden_states + (hidden_state,) __A = stage_module(A ) if output_hidden_states: __A = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A ,hidden_states=A ) @keras_serializable class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' snake_case_ = RegNetConfig def __init__( self : int ,A : Optional[int] ,**A : Dict ): super().__init__(**A ) __A = config __A = TFRegNetEmbeddings(A ,name="embedder" ) __A = TFRegNetEncoder(A ,name="encoder" ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) @unpack_inputs def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : bool = False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.embedder(A ,training=A ) __A = self.encoder( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = encoder_outputs[0] __A = self.pooler(A ) # Change to NCHW output format have uniformity in the modules __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: __A = tuple([tf.transpose(A ,perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A ,pooler_output=A ,hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states ,) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = RegNetConfig snake_case_ = "regnet" snake_case_ = "pixel_values" @property def UpperCamelCase_ ( self : Optional[Any] ): return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24) ,dtype=tf.floataa )} SCREAMING_SNAKE_CASE :Dict = R'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE :Dict = R'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare RegNet model outputting raw features without any specific head on top." , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,*A : List[Any] ,**A : str ): super().__init__(A ,*A ,**A ) __A = TFRegNetMainLayer(A ,name="regnet" ) @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC ,output_type=A ,config_class=_CONFIG_FOR_DOC ,modality="vision" ,expected_output=_EXPECTED_OUTPUT_SHAPE ,) def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : int=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( pixel_values=A ,output_hidden_states=A ,return_dict=A ,training=A ,) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state ,pooler_output=outputs.pooler_output ,hidden_states=outputs.hidden_states ,) @add_start_docstrings( "\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[int] ,A : RegNetConfig ,*A : str ,**A : Tuple ): super().__init__(A ,*A ,**A ) __A = config.num_labels __A = TFRegNetMainLayer(A ,name="regnet" ) # classification head __A = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels ,name="classifier.1" ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT ,output_type=A ,config_class=_CONFIG_FOR_DOC ,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor = None ,A : tf.Tensor = None ,A : bool = None ,A : bool = None ,A : Union[str, Any]=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = outputs.pooler_output if return_dict else outputs[1] __A = self.classifier[0](A ) __A = self.classifier[1](A ) __A = None if labels is None else self.hf_compute_loss(labels=A ,logits=A ) if not return_dict: __A = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A ,logits=A ,hidden_states=outputs.hidden_states )
15
1
import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :int = {'vocab_file': 'vocab.txt', 'emoji_file': 'emoji.json'} SCREAMING_SNAKE_CASE :Dict = { 'vocab_file': { 'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt', }, 'emoji_file': { 'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json', }, } SCREAMING_SNAKE_CASE :Dict = { 'abeja/gpt-neox-japanese-2.7b': 2048, } def UpperCAmelCase ( a_ , a_ ) -> Any: """simple docstring""" with open(a_ , "r" , encoding="utf-8" ) as f: __A = json.loads(f.read() ) __A = collections.OrderedDict() __A = collections.OrderedDict() __A = collections.OrderedDict() with open(a_ , "r" , encoding="utf-8" ) as f: __A = f.readlines() __A = [[t.rstrip("\n" )] if (t == "," or "," not in t) else t.rstrip("\n" ).split("," ) for t in token] for idx, b in enumerate(a_ ): __A = b __A = idx for wd in b: __A = idx return vocab, raw_vocab, ids_to_tokens, emoji class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : str ,A : int ,A : Union[str, Any] ,A : List[str]="<|endoftext|>" ,A : int="<|endoftext|>" ,A : Any="<|startoftext|>" ,A : Dict="<|endoftext|>" ,A : Optional[Any]=False ,**A : str ,): super().__init__( unk_token=A ,pad_token=A ,bos_token=A ,eos_token=A ,do_clean_text=A ,**A ,) if not os.path.isfile(A ): raise ValueError( f'''Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained''' " model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`" ) if not os.path.isfile(A ): raise ValueError( f'''Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google''' " pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`" ) __A = do_clean_text __A , __A , __A , __A = load_vocab_and_emoji(A ,A ) __A = SubWordJapaneseTokenizer( vocab=self.vocab ,ids_to_tokens=self.ids_to_tokens ,emoji=self.emoji ) @property def UpperCamelCase_ ( self : Any ): # self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab return len(self.raw_vocab ) def UpperCamelCase_ ( self : List[Any] ): return dict(self.raw_vocab ,**self.added_tokens_encoder ) def UpperCamelCase_ ( self : Tuple ,A : Optional[int] ): return self.subword_tokenizer.tokenize(A ,clean=self.do_clean_text ) def UpperCamelCase_ ( self : List[Any] ,A : Optional[Any] ): return self.vocab.get(A ,self.vocab.get(self.unk_token ) ) def UpperCamelCase_ ( self : str ,A : List[str] ): return self.subword_tokenizer.convert_id_to_token(A ) def UpperCamelCase_ ( self : Optional[Any] ,A : str ): __A = "".join(A ).strip() return out_string def UpperCamelCase_ ( self : Optional[int] ,A : "Conversation" ): __A = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(A ,add_special_tokens=A ) + [self.eos_token_id] ) if len(A ) > self.model_max_length: __A = input_ids[-self.model_max_length :] return input_ids def UpperCamelCase_ ( self : Any ,A : str ,A : Optional[str] = None ): __A = 0 if os.path.isdir(A ): __A = os.path.join( A ,(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) __A = os.path.join( A ,(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["emoji_file"] ) else: __A = ( (filename_prefix + "-" if filename_prefix else "") + save_directory + VOCAB_FILES_NAMES["vocab_file"] ) __A = ( (filename_prefix + "-" if filename_prefix else "") + save_directory + VOCAB_FILES_NAMES["emoji_file"] ) with open(A ,"w" ,encoding="utf-8" ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( f'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.''' " Please check that the vocabulary is not corrupted!" ) __A = token_index writer.write(",".join(A ) + "\n" ) index += 1 with open(A ,"w" ,encoding="utf-8" ) as writer: json.dump(self.emoji ,A ) return vocab_file, emoji_file class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Union[str, Any] ,A : int ,A : Union[str, Any] ,A : Optional[Any] ): __A = vocab # same as swe __A = ids_to_tokens # same as bpe __A = emoji __A = np.max([len(A ) for w in self.vocab.keys()] ) __A = re.compile(R"(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)" ) __A = re.compile(R"[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*" ) __A = re.compile(R"[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}" ) __A = re.compile( R"([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*" ) __A = re.compile( R"(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*" ) __A = re.compile( R"((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*" ) __A = "─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿" __A = "▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟" __A = str.maketrans({k: "<BLOCK>" for k in keisen + blocks} ) def __len__( self : Union[str, Any] ): return len(self.ids_to_tokens ) def UpperCamelCase_ ( self : Union[str, Any] ,A : Any ): __A = self.content_repattera.sub("<URL>" ,A ) __A = self.content_repattera.sub("<EMAIL>" ,A ) __A = self.content_repattera.sub("<TEL>" ,A ) __A = self.content_repattera.sub("<DATE>" ,A ) __A = self.content_repattera.sub("<DATE>" ,A ) __A = self.content_repattera.sub("<PRICE>" ,A ) __A = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: __A = content.replace("<BLOCK><BLOCK>" ,"<BLOCK>" ) return content def UpperCamelCase_ ( self : str ,A : List[str] ,A : Optional[Any]=False ): __A = text.replace(" " ,"<SP>" ) __A = text.replace(" " ,"<SP>" ) __A = text.replace("\r\n" ,"<BR>" ) __A = text.replace("\n" ,"<BR>" ) __A = text.replace("\r" ,"<BR>" ) __A = text.replace("\t" ,"<TAB>" ) __A = text.replace("—" ,"ー" ) __A = text.replace("−" ,"ー" ) for k, v in self.emoji["emoji"].items(): if k in text: __A = text.replace(A ,A ) if clean: __A = self.clean_text(A ) def check_simbol(A : str ): __A = x.encode() if len(A ) == 1 and len(A ) == 2: __A = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0xc2a1 and c <= 0xc2bf) or (c >= 0xc780 and c <= 0xc783) or (c >= 0xcab9 and c <= 0xcbbf) or (c >= 0xcc80 and c <= 0xcda2) ): return True return False def checkuae(A : str ): __A = x.encode() if len(A ) == 1 and len(A ) == 3: __A = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0xe28080 and c <= 0xe2b07f: return True return False __A = 0 __A = [] while pos < len(A ): __A = min(len(A ) ,pos + self.maxlen + 1 ) if text[pos] == "<" else pos + 3 __A = [] # (token_id, token, pos) for e in range(A ,A ,-1 ): __A = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(A ) > 2: __A = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(A ) > 0: # the smallest token_id is adopted __A , __A , __A = sorted(A ,key=lambda A : x[0] )[0] result.append(A ) __A = e else: __A = pos + 1 __A = text[pos:end] if check_simbol(A ): result.append("<KIGOU>" ) elif checkuae(A ): result.append("<U2000U2BFF>" ) else: for i in wd.encode("utf-8" ): result.append("<|byte%d|>" % i ) __A = end return result def UpperCamelCase_ ( self : Optional[Any] ,A : List[str] ,A : Any="\n" ): __A = [] __A = [] __A = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(A ) > 0: words.append(bytearray(A ).decode("utf-8" ,errors="replace" ) ) __A = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji["emoji_inv"][word] ) elif word == "<SP>": words.append(" " ) elif word == "<BR>": words.append(A ) elif word == "<TAB>": words.append("\t" ) elif word == "<BLOCK>": words.append("▀" ) elif word == "<KIGOU>": words.append("ǀ" ) elif word == "<U2000U2BFF>": words.append("‖" ) else: words.append(A ) if len(A ) > 0: words.append(bytearray(A ).decode("utf-8" ,errors="replace" ) ) __A = "".join(A ) return text
15
import math def UpperCAmelCase ( a_ , a_ = 0 , a_ = 0 ) -> list: """simple docstring""" __A = end or len(a_ ) for i in range(a_ , a_ ): __A = i __A = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __A = array[temp_index - 1] temp_index -= 1 __A = temp_index_value return array def UpperCAmelCase ( a_ , a_ , a_ ) -> None: # Max Heap """simple docstring""" __A = index __A = 2 * index + 1 # Left Node __A = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __A = left_index if right_index < heap_size and array[largest] < array[right_index]: __A = right_index if largest != index: __A , __A = array[largest], array[index] heapify(a_ , a_ , a_ ) def UpperCAmelCase ( a_ ) -> list: """simple docstring""" __A = len(a_ ) for i in range(n // 2 , -1 , -1 ): heapify(a_ , a_ , a_ ) for i in range(n - 1 , 0 , -1 ): __A , __A = array[0], array[i] heapify(a_ , 0 , a_ ) return array def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" __A = low __A = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __A , __A = array[j], array[i] i += 1 def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) == 0: return array __A = 2 * math.ceil(math.loga(len(a_ ) ) ) __A = 1_6 return intro_sort(a_ , 0 , len(a_ ) , a_ , a_ ) def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ ) -> list: """simple docstring""" while end - start > size_threshold: if max_depth == 0: return heap_sort(a_ ) max_depth -= 1 __A = median_of_a(a_ , a_ , start + ((end - start) // 2) + 1 , end - 1 ) __A = partition(a_ , a_ , a_ , a_ ) intro_sort(a_ , a_ , a_ , a_ , a_ ) __A = p return insertion_sort(a_ , a_ , a_ ) if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma : ').strip() SCREAMING_SNAKE_CASE :str = [float(item) for item in user_input.split(',')] print(sort(unsorted))
15
1
import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = KandinskyVaaPriorPipeline snake_case_ = ["prompt"] snake_case_ = ["prompt", "negative_prompt"] snake_case_ = [ "num_images_per_prompt", "generator", "num_inference_steps", "latents", "negative_prompt", "guidance_scale", "output_type", "return_dict", ] snake_case_ = False @property def UpperCamelCase_ ( self : Optional[int] ): return 32 @property def UpperCamelCase_ ( self : int ): return 32 @property def UpperCamelCase_ ( self : Optional[int] ): return self.time_input_dim @property def UpperCamelCase_ ( self : int ): return self.time_input_dim * 4 @property def UpperCamelCase_ ( self : Optional[Any] ): return 1_00 @property def UpperCamelCase_ ( self : Optional[Any] ): __A = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) return tokenizer @property def UpperCamelCase_ ( self : Optional[int] ): torch.manual_seed(0 ) __A = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=self.text_embedder_hidden_size ,projection_dim=self.text_embedder_hidden_size ,intermediate_size=37 ,layer_norm_eps=1E-05 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=10_00 ,) return CLIPTextModelWithProjection(A ) @property def UpperCamelCase_ ( self : int ): torch.manual_seed(0 ) __A = { "num_attention_heads": 2, "attention_head_dim": 12, "embedding_dim": self.text_embedder_hidden_size, "num_layers": 1, } __A = PriorTransformer(**A ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 __A = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def UpperCamelCase_ ( self : List[Any] ): torch.manual_seed(0 ) __A = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size ,image_size=2_24 ,projection_dim=self.text_embedder_hidden_size ,intermediate_size=37 ,num_attention_heads=4 ,num_channels=3 ,num_hidden_layers=5 ,patch_size=14 ,) __A = CLIPVisionModelWithProjection(A ) return model @property def UpperCamelCase_ ( self : Tuple ): __A = CLIPImageProcessor( crop_size=2_24 ,do_center_crop=A ,do_normalize=A ,do_resize=A ,image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] ,image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] ,resample=3 ,size=2_24 ,) return image_processor def UpperCamelCase_ ( self : List[str] ): __A = self.dummy_prior __A = self.dummy_image_encoder __A = self.dummy_text_encoder __A = self.dummy_tokenizer __A = self.dummy_image_processor __A = UnCLIPScheduler( variance_type="fixed_small_log" ,prediction_type="sample" ,num_train_timesteps=10_00 ,clip_sample=A ,clip_sample_range=10.0 ,) __A = { "prior": prior, "image_encoder": image_encoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "scheduler": scheduler, "image_processor": image_processor, } return components def UpperCamelCase_ ( self : Dict ,A : int ,A : List[Any]=0 ): if str(A ).startswith("mps" ): __A = torch.manual_seed(A ) else: __A = torch.Generator(device=A ).manual_seed(A ) __A = { "prompt": "horse", "generator": generator, "guidance_scale": 4.0, "num_inference_steps": 2, "output_type": "np", } return inputs def UpperCamelCase_ ( self : Union[str, Any] ): __A = "cpu" __A = self.get_dummy_components() __A = self.pipeline_class(**A ) __A = pipe.to(A ) pipe.set_progress_bar_config(disable=A ) __A = pipe(**self.get_dummy_inputs(A ) ) __A = output.image_embeds __A = pipe( **self.get_dummy_inputs(A ) ,return_dict=A ,)[0] __A = image[0, -10:] __A = image_from_tuple[0, -10:] assert image.shape == (1, 32) __A = np.array( [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def UpperCamelCase_ ( self : Optional[Any] ): __A = torch_device == "cpu" __A = True __A = False self._test_inference_batch_single_identical( test_max_difference=A ,relax_max_difference=A ,test_mean_pixel_difference=A ,) @skip_mps def UpperCamelCase_ ( self : Optional[Any] ): __A = torch_device == "cpu" __A = False self._test_attention_slicing_forward_pass( test_max_difference=A ,test_mean_pixel_difference=A ,)
15
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE :Optional[int] = NewType('DataClass', Any) SCREAMING_SNAKE_CASE :int = NewType('DataClassType', Any) def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" if isinstance(a_ , a_ ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( F'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def UpperCAmelCase ( a_ ) -> Callable[[str], Any]: """simple docstring""" __A = {str(a_ ): choice for choice in choices} return lambda a_ : str_to_choice.get(a_ , a_ ) def UpperCAmelCase ( *, a_ = None , a_ = None , a_ = dataclasses.MISSING , a_ = dataclasses.MISSING , a_ = None , **a_ , ) -> dataclasses.Field: """simple docstring""" if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls __A = {} if aliases is not None: __A = aliases if help is not None: __A = help return dataclasses.field(metadata=a_ , default=a_ , default_factory=a_ , **a_ ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 def __init__( self : Union[str, Any] ,A : Union[DataClassType, Iterable[DataClassType]] ,**A : List[Any] ): # To make the default appear when using --help if "formatter_class" not in kwargs: __A = ArgumentDefaultsHelpFormatter super().__init__(**A ) if dataclasses.is_dataclass(A ): __A = [dataclass_types] __A = list(A ) for dtype in self.dataclass_types: self._add_dataclass_arguments(A ) @staticmethod def UpperCamelCase_ ( A : ArgumentParser ,A : dataclasses.Field ): __A = f'''--{field.name}''' __A = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type ,A ): raise RuntimeError( "Unresolved type detected, which should have been done with the help of " "`typing.get_type_hints` method by default" ) __A = kwargs.pop("aliases" ,[] ) if isinstance(A ,A ): __A = [aliases] __A = getattr(field.type ,"__origin__" ,field.type ) if origin_type is Union or (hasattr(A ,"UnionType" ) and isinstance(A ,types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(A ) not in field.type.__args__ ): raise ValueError( "Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because" " the argument parser only supports one type per argument." f''' Problem encountered in field \'{field.name}\'.''' ) if type(A ) not in field.type.__args__: # filter `str` in Union __A = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] __A = getattr(field.type ,"__origin__" ,field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) __A = ( field.type.__args__[0] if isinstance(A ,field.type.__args__[1] ) else field.type.__args__[1] ) __A = getattr(field.type ,"__origin__" ,field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) __A = {} if origin_type is Literal or (isinstance(field.type ,A ) and issubclass(field.type ,A )): if origin_type is Literal: __A = field.type.__args__ else: __A = [x.value for x in field.type] __A = make_choice_type_function(kwargs["choices"] ) if field.default is not dataclasses.MISSING: __A = field.default else: __A = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument __A = copy(A ) # Hack because type=bool in argparse does not behave as we want. __A = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. __A = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way __A = default # This tells argparse we accept 0 or 1 value after --field_name __A = "?" # This is the value that will get picked if we do --field_name (without value) __A = True elif isclass(A ) and issubclass(A ,A ): __A = field.type.__args__[0] __A = "+" if field.default_factory is not dataclasses.MISSING: __A = field.default_factory() elif field.default is dataclasses.MISSING: __A = True else: __A = field.type if field.default is not dataclasses.MISSING: __A = field.default elif field.default_factory is not dataclasses.MISSING: __A = field.default_factory() else: __A = True parser.add_argument(A ,*A ,**A ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): __A = False parser.add_argument(f'''--no_{field.name}''' ,action="store_false" ,dest=field.name ,**A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : DataClassType ): if hasattr(A ,"_argument_group_name" ): __A = self.add_argument_group(dtype._argument_group_name ) else: __A = self try: __A = get_type_hints(A ) except NameError: raise RuntimeError( f'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' "removing line of `from __future__ import annotations` which opts in Postponed " "Evaluation of Annotations (PEP 563)" ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(A ): __A = ".".join(map(A ,sys.version_info[:3] ) ) raise RuntimeError( f'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' "line of `from __future__ import annotations` which opts in union types as " "`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To " "support Python versions that lower than 3.10, you need to use " "`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of " "`X | None`." ) from ex raise for field in dataclasses.fields(A ): if not field.init: continue __A = type_hints[field.name] self._parse_dataclass_field(A ,A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : List[Any]=None ,A : List[Any]=False ,A : Optional[Any]=True ,A : Union[str, Any]=None ,A : Union[str, Any]=None ,): if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): __A = [] if args_filename: args_files.append(Path(A ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix(".args" ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values __A = ArgumentParser() args_file_parser.add_argument(A ,type=A ,action="append" ) # Use only remaining args for further parsing (remove the args_file_flag) __A , __A = args_file_parser.parse_known_args(args=A ) __A = vars(A ).get(args_file_flag.lstrip("-" ) ,A ) if cmd_args_file_paths: args_files.extend([Path(A ) for p in cmd_args_file_paths] ) __A = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last __A = file_args + args if args is not None else file_args + sys.argv[1:] __A , __A = self.parse_known_args(args=A ) __A = [] for dtype in self.dataclass_types: __A = {f.name for f in dataclasses.fields(A ) if f.init} __A = {k: v for k, v in vars(A ).items() if k in keys} for k in keys: delattr(A ,A ) __A = dtype(**A ) outputs.append(A ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(A ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(f'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def UpperCamelCase_ ( self : Dict ,A : Dict[str, Any] ,A : bool = False ): __A = set(args.keys() ) __A = [] for dtype in self.dataclass_types: __A = {f.name for f in dataclasses.fields(A ) if f.init} __A = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) __A = dtype(**A ) outputs.append(A ) if not allow_extra_keys and unused_keys: raise ValueError(f'''Some keys are not used by the HfArgumentParser: {sorted(A )}''' ) return tuple(A ) def UpperCamelCase_ ( self : List[str] ,A : str ,A : bool = False ): with open(Path(A ) ,encoding="utf-8" ) as open_json_file: __A = json.loads(open_json_file.read() ) __A = self.parse_dict(A ,allow_extra_keys=A ) return tuple(A ) def UpperCamelCase_ ( self : int ,A : str ,A : bool = False ): __A = self.parse_dict(yaml.safe_load(Path(A ).read_text() ) ,allow_extra_keys=A ) return tuple(A )
15
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def UpperCAmelCase ( a_=None ) -> Tuple: """simple docstring""" if subparsers is not None: __A = subparsers.add_parser("test" ) else: __A = argparse.ArgumentParser("Accelerate test command" ) parser.add_argument( "--config_file" , default=a_ , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=a_ ) return parser def UpperCAmelCase ( a_ ) -> Union[str, Any]: """simple docstring""" __A = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ["test_utils", "scripts", "test_script.py"] ) if args.config_file is None: __A = script_name else: __A = F'''--config_file={args.config_file} {script_name}''' __A = ["accelerate-launch"] + test_args.split() __A = execute_subprocess_async(a_ , env=os.environ.copy() ) if result.returncode == 0: print("Test is a success! You are ready for your distributed training!" ) def UpperCAmelCase ( ) -> Tuple: """simple docstring""" __A = test_command_parser() __A = parser.parse_args() test_command(a_ ) if __name__ == "__main__": main()
15
SCREAMING_SNAKE_CASE :Any = 256 # Modulus to hash a string SCREAMING_SNAKE_CASE :Union[str, Any] = 100_0003 def UpperCAmelCase ( a_ , a_ ) -> bool: """simple docstring""" __A = len(a_ ) __A = len(a_ ) if p_len > t_len: return False __A = 0 __A = 0 __A = 1 # Calculating the hash of pattern and substring of text for i in range(a_ ): __A = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus __A = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue __A = (modulus_power * alphabet_size) % modulus for i in range(0 , t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash __A = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def UpperCAmelCase ( ) -> None: """simple docstring""" __A = "abc1abc12" __A = "alskfjaldsabc1abc1abc12k23adsfabcabc" __A = "alskfjaldsk23adsfabcabc" assert rabin_karp(a_ , a_ ) and not rabin_karp(a_ , a_ ) # Test 2) __A = "ABABX" __A = "ABABZABABYABABX" assert rabin_karp(a_ , a_ ) # Test 3) __A = "AAAB" __A = "ABAAAAAB" assert rabin_karp(a_ , a_ ) # Test 4) __A = "abcdabcy" __A = "abcxabcdabxabcdabcdabcy" assert rabin_karp(a_ , a_ ) # Test 5) __A = "Lü" __A = "Lüsai" assert rabin_karp(a_ , a_ ) __A = "Lue" assert not rabin_karp(a_ , a_ ) print("Success." ) if __name__ == "__main__": test_rabin_karp()
15
1
import unittest from transformers import AlbertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, ) from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCAmelCase : '''simple docstring''' def __init__( self : List[Any] ,A : str ,A : List[str]=13 ,A : Any=7 ,A : Tuple=True ,A : Dict=True ,A : str=True ,A : str=True ,A : Any=99 ,A : Optional[int]=16 ,A : List[str]=36 ,A : str=6 ,A : Any=6 ,A : str=6 ,A : Any=37 ,A : Union[str, Any]="gelu" ,A : List[Any]=0.1 ,A : List[str]=0.1 ,A : Union[str, Any]=5_12 ,A : str=16 ,A : int=2 ,A : int=0.02 ,A : Any=3 ,A : str=4 ,A : int=None ,): __A = parent __A = batch_size __A = seq_length __A = is_training __A = use_input_mask __A = use_token_type_ids __A = use_labels __A = vocab_size __A = embedding_size __A = hidden_size __A = num_hidden_layers __A = num_hidden_groups __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = type_vocab_size __A = type_sequence_label_size __A = initializer_range __A = num_labels __A = num_choices __A = scope def UpperCamelCase_ ( self : Union[str, Any] ): __A = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) __A = None if self.use_input_mask: __A = random_attention_mask([self.batch_size, self.seq_length] ) __A = None if self.use_token_type_ids: __A = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) __A = None __A = None __A = None if self.use_labels: __A = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) __A = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) __A = ids_tensor([self.batch_size] ,self.num_choices ) __A = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase_ ( self : Tuple ): return AlbertConfig( 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 ,num_hidden_groups=self.num_hidden_groups ,) def UpperCamelCase_ ( self : str ,A : Union[str, Any] ,A : Union[str, Any] ,A : Any ,A : str ,A : Tuple ,A : List[Any] ,A : str ): __A = AlbertModel(config=A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,token_type_ids=A ) __A = model(A ,token_type_ids=A ) __A = model(A ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def UpperCamelCase_ ( self : Dict ,A : Union[str, Any] ,A : Dict ,A : Optional[int] ,A : int ,A : Tuple ,A : Optional[int] ,A : str ): __A = AlbertForPreTraining(config=A ) model.to(A ) model.eval() __A = model( A ,attention_mask=A ,token_type_ids=A ,labels=A ,sentence_order_label=A ,) self.parent.assertEqual(result.prediction_logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.sop_logits.shape ,(self.batch_size, config.num_labels) ) def UpperCamelCase_ ( self : Tuple ,A : Union[str, Any] ,A : Dict ,A : Dict ,A : Union[str, Any] ,A : List[str] ,A : List[str] ,A : List[str] ): __A = AlbertForMaskedLM(config=A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,token_type_ids=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase_ ( self : List[Any] ,A : Union[str, Any] ,A : Any ,A : Union[str, Any] ,A : Any ,A : Tuple ,A : str ,A : Optional[int] ): __A = AlbertForQuestionAnswering(config=A ) model.to(A ) model.eval() __A = model( A ,attention_mask=A ,token_type_ids=A ,start_positions=A ,end_positions=A ,) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def UpperCamelCase_ ( self : Any ,A : Dict ,A : Dict ,A : Dict ,A : Any ,A : Optional[Any] ,A : Any ,A : str ): __A = self.num_labels __A = AlbertForSequenceClassification(A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,token_type_ids=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def UpperCamelCase_ ( self : Any ,A : int ,A : Dict ,A : Dict ,A : Union[str, Any] ,A : List[str] ,A : str ,A : int ): __A = self.num_labels __A = AlbertForTokenClassification(config=A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,token_type_ids=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase_ ( self : Any ,A : Union[str, Any] ,A : Optional[Any] ,A : int ,A : Dict ,A : int ,A : List[Any] ,A : Any ): __A = self.num_choices __A = AlbertForMultipleChoice(config=A ) model.to(A ) model.eval() __A = input_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() __A = token_type_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() __A = input_mask.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() __A = model( A ,attention_mask=A ,token_type_ids=A ,labels=A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def UpperCamelCase_ ( self : Optional[int] ): __A = self.prepare_config_and_inputs() ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = config_and_inputs __A = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = ( ( AlbertModel, AlbertForPreTraining, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertForQuestionAnswering, ) if is_torch_available() else () ) snake_case_ = ( { "feature-extraction": AlbertModel, "fill-mask": AlbertForMaskedLM, "question-answering": AlbertForQuestionAnswering, "text-classification": AlbertForSequenceClassification, "token-classification": AlbertForTokenClassification, "zero-shot": AlbertForSequenceClassification, } if is_torch_available() else {} ) snake_case_ = True def UpperCamelCase_ ( self : Union[str, Any] ,A : List[str] ,A : Dict ,A : str=False ): __A = super()._prepare_for_class(A ,A ,return_labels=A ) if return_labels: if model_class in get_values(A ): __A = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) ,dtype=torch.long ,device=A ) __A = torch.zeros( self.model_tester.batch_size ,dtype=torch.long ,device=A ) return inputs_dict def UpperCamelCase_ ( self : Optional[int] ): __A = AlbertModelTester(self ) __A = ConfigTester(self ,config_class=A ,hidden_size=37 ) def UpperCamelCase_ ( self : Optional[Any] ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : Dict ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def UpperCamelCase_ ( self : List[Any] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*A ) def UpperCamelCase_ ( self : Union[str, Any] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A ) def UpperCamelCase_ ( self : Any ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A ) def UpperCamelCase_ ( self : str ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A ) def UpperCamelCase_ ( self : List[Any] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A ) def UpperCamelCase_ ( self : Optional[Any] ): __A = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: __A = type self.model_tester.create_and_check_model(*A ) @slow def UpperCamelCase_ ( self : Dict ): for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __A = AlbertModel.from_pretrained(A ) self.assertIsNotNone(A ) @require_torch class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def UpperCamelCase_ ( self : Optional[int] ): __A = AlbertModel.from_pretrained("albert-base-v2" ) __A = torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) __A = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __A = model(A ,attention_mask=A )[0] __A = torch.Size((1, 11, 7_68) ) self.assertEqual(output.shape ,A ) __A = torch.tensor( [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,A ,atol=1E-4 ) )
15
import argparse import json import os import torch from transformers.file_utils import has_file from diffusers import UNetaDConditionModel, UNetaDModel SCREAMING_SNAKE_CASE :Union[str, Any] = False SCREAMING_SNAKE_CASE :Any = True SCREAMING_SNAKE_CASE :Tuple = False if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser() parser.add_argument( '--repo_path', default=None, type=str, required=True, help='The config json file corresponding to the architecture.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') SCREAMING_SNAKE_CASE :Union[str, Any] = parser.parse_args() SCREAMING_SNAKE_CASE :Dict = { 'image_size': 'sample_size', 'num_res_blocks': 'layers_per_block', 'block_channels': 'block_out_channels', 'down_blocks': 'down_block_types', 'up_blocks': 'up_block_types', 'downscale_freq_shift': 'freq_shift', 'resnet_num_groups': 'norm_num_groups', 'resnet_act_fn': 'act_fn', 'resnet_eps': 'norm_eps', 'num_head_channels': 'attention_head_dim', } SCREAMING_SNAKE_CASE :Optional[int] = { 'time_steps': 'time_proj', 'mid': 'mid_block', 'downsample_blocks': 'down_blocks', 'upsample_blocks': 'up_blocks', } SCREAMING_SNAKE_CASE :int = '' if has_file(args.repo_path, 'config.json') else 'unet' with open(os.path.join(args.repo_path, subfolder, 'config.json'), 'r', encoding='utf-8') as reader: SCREAMING_SNAKE_CASE :Dict = reader.read() SCREAMING_SNAKE_CASE :List[str] = json.loads(text) if do_only_config: for key in config_parameters_to_change.keys(): config.pop(key, None) if has_file(args.repo_path, 'config.json'): SCREAMING_SNAKE_CASE :Optional[int] = UNetaDModel(**config) else: SCREAMING_SNAKE_CASE :Optional[Any] = UNetaDConditionModel if 'ldm-text2im-large-256' in args.repo_path else UNetaDModel SCREAMING_SNAKE_CASE :List[str] = class_name(**config) if do_only_config: model.save_config(os.path.join(args.repo_path, subfolder)) SCREAMING_SNAKE_CASE :List[str] = dict(model.config) if do_only_renaming: for key, value in config_parameters_to_change.items(): if key in config: SCREAMING_SNAKE_CASE :Optional[Any] = config[key] del config[key] SCREAMING_SNAKE_CASE :Optional[Any] = [k.replace('UNetRes', '') for k in config['down_block_types']] SCREAMING_SNAKE_CASE :List[Any] = [k.replace('UNetRes', '') for k in config['up_block_types']] if do_only_weights: SCREAMING_SNAKE_CASE :Tuple = torch.load(os.path.join(args.repo_path, subfolder, 'diffusion_pytorch_model.bin')) SCREAMING_SNAKE_CASE :Any = {} for param_key, param_value in state_dict.items(): if param_key.endswith('.op.bias') or param_key.endswith('.op.weight'): continue SCREAMING_SNAKE_CASE :List[str] = False for key, new_key in key_parameters_to_change.items(): if not has_changed and param_key.split('.')[0] == key: SCREAMING_SNAKE_CASE :List[Any] = param_value SCREAMING_SNAKE_CASE :str = True if not has_changed: SCREAMING_SNAKE_CASE :List[str] = param_value model.load_state_dict(new_state_dict) model.save_pretrained(os.path.join(args.repo_path, subfolder))
15
1
import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self : Dict ,A : List[Any] ,A : Dict ): return f'''gaussian_noise_s={seed}_shape={'_'.join([str(A ) for s in shape] )}.npy''' def UpperCamelCase_ ( self : Dict ): # clean up the VRAM after each test super().tearDown() gc.collect() def UpperCamelCase_ ( self : Union[str, Any] ,A : List[Any]=0 ,A : Any=(4, 4, 64, 64) ,A : Tuple=False ): __A = jnp.bfloataa if fpaa else jnp.floataa __A = jnp.array(load_hf_numpy(self.get_file_format(A ,A ) ) ,dtype=A ) return image def UpperCamelCase_ ( self : Any ,A : int=False ,A : Union[str, Any]="CompVis/stable-diffusion-v1-4" ): __A = jnp.bfloataa if fpaa else jnp.floataa __A = "bf16" if fpaa else None __A , __A = FlaxUNetaDConditionModel.from_pretrained( A ,subfolder="unet" ,dtype=A ,revision=A ) return model, params def UpperCamelCase_ ( self : str ,A : Union[str, Any]=0 ,A : int=(4, 77, 7_68) ,A : Dict=False ): __A = jnp.bfloataa if fpaa else jnp.floataa __A = jnp.array(load_hf_numpy(self.get_file_format(A ,A ) ) ,dtype=A ) return hidden_states @parameterized.expand( [ # fmt: off [83, 4, [-0.23_23, -0.13_04, 0.08_13, -0.30_93, -0.09_19, -0.15_71, -0.11_25, -0.58_06]], [17, 0.55, [-0.08_31, -0.24_43, 0.09_01, -0.09_19, 0.33_96, 0.01_03, -0.37_43, 0.07_01]], [8, 0.89, [-0.48_63, 0.08_59, 0.08_75, -0.16_58, 0.91_99, -0.01_14, 0.48_39, 0.46_39]], [3, 10_00, [-0.56_49, 0.24_02, -0.55_18, 0.12_48, 1.13_28, -0.24_43, -0.03_25, -1.00_78]], # fmt: on ] ) def UpperCamelCase_ ( self : int ,A : Dict ,A : Optional[Any] ,A : Optional[int] ): __A , __A = self.get_unet_model(model_id="CompVis/stable-diffusion-v1-4" ,fpaa=A ) __A = self.get_latents(A ,fpaa=A ) __A = self.get_encoder_hidden_states(A ,fpaa=A ) __A = model.apply( {"params": params} ,A ,jnp.array(A ,dtype=jnp.intaa ) ,encoder_hidden_states=A ,).sample assert sample.shape == latents.shape __A = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) ,dtype=jnp.floataa ) __A = jnp.array(A ,dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(A ,A ,atol=1E-2 ) @parameterized.expand( [ # fmt: off [83, 4, [0.15_14, 0.08_07, 0.16_24, 0.10_16, -0.18_96, 0.02_63, 0.06_77, 0.23_10]], [17, 0.55, [0.11_64, -0.02_16, 0.01_70, 0.15_89, -0.31_20, 0.10_05, -0.05_81, -0.14_58]], [8, 0.89, [-0.17_58, -0.01_69, 0.10_04, -0.14_11, 0.13_12, 0.11_03, -0.19_96, 0.21_39]], [3, 10_00, [0.12_14, 0.03_52, -0.07_31, -0.15_62, -0.09_94, -0.09_06, -0.23_40, -0.05_39]], # fmt: on ] ) def UpperCamelCase_ ( self : Any ,A : Union[str, Any] ,A : Optional[int] ,A : Any ): __A , __A = self.get_unet_model(model_id="stabilityai/stable-diffusion-2" ,fpaa=A ) __A = self.get_latents(A ,shape=(4, 4, 96, 96) ,fpaa=A ) __A = self.get_encoder_hidden_states(A ,shape=(4, 77, 10_24) ,fpaa=A ) __A = model.apply( {"params": params} ,A ,jnp.array(A ,dtype=jnp.intaa ) ,encoder_hidden_states=A ,).sample assert sample.shape == latents.shape __A = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) ,dtype=jnp.floataa ) __A = jnp.array(A ,dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(A ,A ,atol=1E-2 )
15
import argparse import math import traceback import dateutil.parser as date_parser import requests def UpperCAmelCase ( a_ ) -> str: """simple docstring""" __A = {} __A = job["started_at"] __A = job["completed_at"] __A = date_parser.parse(a_ ) __A = date_parser.parse(a_ ) __A = round((end_datetime - start_datetime).total_seconds() / 60.0 ) __A = start __A = end __A = duration_in_min return job_info def UpperCAmelCase ( a_ , a_=None ) -> str: """simple docstring""" __A = None if token is not None: __A = {"Accept": "application/vnd.github+json", "Authorization": F'''Bearer {token}'''} __A = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' __A = requests.get(a_ , headers=a_ ).json() __A = {} try: job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) __A = math.ceil((result["total_count"] - 1_0_0) / 1_0_0 ) for i in range(a_ ): __A = requests.get(url + F'''&page={i + 2}''' , headers=a_ ).json() job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) return job_time except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": SCREAMING_SNAKE_CASE :Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() SCREAMING_SNAKE_CASE :Union[str, Any] = get_job_time(args.workflow_run_id) SCREAMING_SNAKE_CASE :Optional[int] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f'''{k}: {v["duration"]}''')
15
1
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def UpperCAmelCase ( ) -> Dict: """simple docstring""" __A = ArgumentParser( description=( "PyTorch TPU distributed training launch " "helper utility that will spawn up " "multiple distributed processes" ) ) # Optional arguments for the launch helper parser.add_argument("--num_cores" , type=a_ , default=1 , help="Number of TPU cores to use (1 or 8)." ) # positional parser.add_argument( "training_script" , type=a_ , help=( "The full path to the single TPU training " "program/script to be launched in parallel, " "followed by all the arguments for the " "training script" ) , ) # rest from the training program parser.add_argument("training_script_args" , nargs=a_ ) return parser.parse_args() def UpperCAmelCase ( ) -> Tuple: """simple docstring""" __A = parse_args() # Import training_script as a module. __A = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) __A = script_fpath.stem __A = importlib.import_module(a_ ) # Patch sys.argv __A = [args.training_script] + args.training_script_args + ["--tpu_num_cores", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
15
import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" __A = args.pruning_method __A = args.threshold __A = args.model_name_or_path.rstrip("/" ) __A = args.target_model_path print(F'''Load fine-pruned model from {model_name_or_path}''' ) __A = torch.load(os.path.join(a_ , "pytorch_model.bin" ) ) __A = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: __A = tensor print(F'''Copied layer {name}''' ) elif "classifier" in name or "qa_output" in name: __A = tensor print(F'''Copied layer {name}''' ) elif "bias" in name: __A = tensor print(F'''Copied layer {name}''' ) else: if pruning_method == "magnitude": __A = MagnitudeBinarizer.apply(inputs=a_ , threshold=a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "topK": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A = TopKBinarizer.apply(a_ , a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A = ThresholdBinarizer.apply(a_ , a_ , a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "l0": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A , __A = -0.1, 1.1 __A = torch.sigmoid(a_ ) __A = s * (r - l) + l __A = s_bar.clamp(min=0.0 , max=1.0 ) __A = tensor * mask print(F'''Pruned layer {name}''' ) else: raise ValueError("Unknown pruning method" ) if target_model_path is None: __A = os.path.join( os.path.dirname(a_ ) , F'''bertarized_{os.path.basename(a_ )}''' ) if not os.path.isdir(a_ ): shutil.copytree(a_ , a_ ) print(F'''\nCreated folder {target_model_path}''' ) torch.save(a_ , os.path.join(a_ , "pytorch_model.bin" ) ) print("\nPruned model saved! See you later!" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser() parser.add_argument( '--pruning_method', choices=['l0', 'magnitude', 'topK', 'sigmoied_threshold'], type=str, required=True, help=( 'Pruning Method (l0 = L0 regularization, magnitude = Magnitude pruning, topK = Movement pruning,' ' sigmoied_threshold = Soft movement pruning)' ), ) parser.add_argument( '--threshold', type=float, required=False, help=( 'For `magnitude` and `topK`, it is the level of remaining weights (in %) in the fine-pruned model.' 'For `sigmoied_threshold`, it is the threshold \tau against which the (sigmoied) scores are compared.' 'Not needed for `l0`' ), ) parser.add_argument( '--model_name_or_path', type=str, required=True, help='Folder containing the model that was previously fine-pruned', ) parser.add_argument( '--target_model_path', default=None, type=str, required=False, help='Folder containing the model that was previously fine-pruned', ) SCREAMING_SNAKE_CASE :str = parser.parse_args() main(args)
15
1
def UpperCAmelCase ( a_ = 1_0_0_0 ) -> int: """simple docstring""" __A = 2**power __A = 0 while n: __A , __A = r + n % 1_0, n // 1_0 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
15
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :List[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :int = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Union[str, Any] = { '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' ), } } SCREAMING_SNAKE_CASE :int = { 'google/bigbird-roberta-base': 4096, 'google/bigbird-roberta-large': 4096, 'google/bigbird-base-trivia-itc': 4096, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] snake_case_ = [] def __init__( self : Any ,A : List[str] ,A : str="<unk>" ,A : int="<s>" ,A : Union[str, Any]="</s>" ,A : List[str]="<pad>" ,A : int="[SEP]" ,A : Optional[Any]="[MASK]" ,A : Tuple="[CLS]" ,A : Optional[Dict[str, Any]] = None ,**A : Any ,): __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else bos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else eos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else unk_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else pad_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else cls_token __A = 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 __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sep_token=A ,mask_token=A ,cls_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) @property def UpperCamelCase_ ( self : List[str] ): return self.sp_model.get_piece_size() def UpperCamelCase_ ( self : Optional[Any] ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : str ,A : Optional[Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Tuple ): return self.sp_model.piece_to_id(A ) def UpperCamelCase_ ( self : List[Any] ,A : Tuple ): __A = self.sp_model.IdToPiece(A ) return token def UpperCamelCase_ ( self : List[Any] ,A : int ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def UpperCamelCase_ ( self : Tuple ,A : List[int] ,A : bool = False ,A : bool = None ,A : bool = True ,**A : Union[str, Any] ,): __A = kwargs.pop("use_source_tokenizer" ,A ) __A = self.convert_ids_to_tokens(A ,skip_special_tokens=A ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __A = [] __A = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) __A = [] sub_texts.append(A ) else: current_sub_text.append(A ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: __A = re.sub(R" (\[(MASK|SEP)\])" ,R"\1" ," ".join(A ) ) else: __A = "".join(A ) __A = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __A = self.clean_up_tokenization(A ) return clean_text else: return text def UpperCamelCase_ ( self : str ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def UpperCamelCase_ ( self : Optional[int] ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=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 : Any ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
15
1
from __future__ import annotations def UpperCAmelCase ( a_ ) -> float: """simple docstring""" if not nums: raise ValueError("List is empty" ) return sum(a_ ) / len(a_ ) if __name__ == "__main__": import doctest doctest.testmod()
15
import PIL.Image import PIL.ImageOps from packaging import version from PIL import Image if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('9.1.0'): SCREAMING_SNAKE_CASE :Any = { 'linear': PIL.Image.Resampling.BILINEAR, 'bilinear': PIL.Image.Resampling.BILINEAR, 'bicubic': PIL.Image.Resampling.BICUBIC, 'lanczos': PIL.Image.Resampling.LANCZOS, 'nearest': PIL.Image.Resampling.NEAREST, } else: SCREAMING_SNAKE_CASE :int = { 'linear': PIL.Image.LINEAR, 'bilinear': PIL.Image.BILINEAR, 'bicubic': PIL.Image.BICUBIC, 'lanczos': PIL.Image.LANCZOS, 'nearest': PIL.Image.NEAREST, } def UpperCAmelCase ( a_ ) -> Optional[Any]: """simple docstring""" __A = (images / 2 + 0.5).clamp(0 , 1 ) __A = images.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __A = numpy_to_pil(a_ ) return images def UpperCAmelCase ( a_ ) -> int: """simple docstring""" if images.ndim == 3: __A = images[None, ...] __A = (images * 2_5_5).round().astype("uint8" ) if images.shape[-1] == 1: # special case for grayscale (single channel) images __A = [Image.fromarray(image.squeeze() , mode="L" ) for image in images] else: __A = [Image.fromarray(a_ ) for image in images] return pil_images
15
1
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "M-CLIP" def __init__( self : Any ,A : int=10_24 ,A : Dict=7_68 ,**A : Dict ): __A = transformerDimSize __A = imageDimSize super().__init__(**A ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = MCLIPConfig def __init__( self : Optional[Any] ,A : Dict ,*A : Optional[int] ,**A : Optional[Any] ): super().__init__(A ,*A ,**A ) __A = XLMRobertaModel(A ) __A = torch.nn.Linear( in_features=config.transformerDimensions ,out_features=config.numDims ) def UpperCamelCase_ ( self : str ,A : List[Any] ,A : str ): __A = self.transformer(input_ids=A ,attention_mask=A )[0] __A = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(A ), embs
15
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 SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[Any] = { 'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json', # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "yolos" def __init__( self : Any ,A : Optional[Any]=7_68 ,A : Dict=12 ,A : Any=12 ,A : str=30_72 ,A : Any="gelu" ,A : str=0.0 ,A : List[str]=0.0 ,A : Dict=0.02 ,A : int=1E-12 ,A : Tuple=[5_12, 8_64] ,A : List[Any]=16 ,A : str=3 ,A : str=True ,A : Any=1_00 ,A : Dict=True ,A : Dict=False ,A : Tuple=1 ,A : Union[str, Any]=5 ,A : Optional[Any]=2 ,A : Union[str, Any]=5 ,A : int=2 ,A : int=0.1 ,**A : List[str] ,): super().__init__(**A ) __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = initializer_range __A = layer_norm_eps __A = image_size __A = patch_size __A = num_channels __A = qkv_bias __A = num_detection_tokens __A = use_mid_position_embeddings __A = auxiliary_loss # Hungarian matcher __A = class_cost __A = bbox_cost __A = giou_cost # Loss coefficients __A = bbox_loss_coefficient __A = giou_loss_coefficient __A = eos_coefficient class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = version.parse("1.11" ) @property def UpperCamelCase_ ( self : str ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def UpperCamelCase_ ( self : List[Any] ): return 1E-4 @property def UpperCamelCase_ ( self : Optional[Any] ): return 12
15
1
import math def UpperCAmelCase ( a_ ) -> str: """simple docstring""" __A = 0 __A = 0 while num > 0: __A = num % 8 __A = octal + (remainder * math.floor(math.pow(1_0 , a_ ) )) counter += 1 __A = math.floor(num / 8 ) # basically /= 8 without remainder if any # This formatting removes trailing '.0' from `octal`. return F'''0o{int(a_ )}''' def UpperCAmelCase ( ) -> None: """simple docstring""" print("\n2 in octal is:" ) print(decimal_to_octal(2 ) ) # = 2 print("\n8 in octal is:" ) print(decimal_to_octal(8 ) ) # = 10 print("\n65 in octal is:" ) print(decimal_to_octal(6_5 ) ) # = 101 print("\n216 in octal is:" ) print(decimal_to_octal(2_1_6 ) ) # = 330 print("\n512 in octal is:" ) print(decimal_to_octal(5_1_2 ) ) # = 1000 print("\n" ) if __name__ == "__main__": main()
15
# Copyright 2021 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from packaging import version from .. import __version__ from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD from .doc import ( add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, copy_func, replace_return_docstrings, ) from .generic import ( ContextManagers, ExplicitEnum, ModelOutput, PaddingStrategy, TensorType, add_model_info_to_auto_map, cached_property, can_return_loss, expand_dims, find_labels, flatten_dict, infer_framework, is_jax_tensor, is_numpy_array, is_tensor, is_tf_symbolic_tensor, is_tf_tensor, is_torch_device, is_torch_dtype, is_torch_tensor, reshape, squeeze, strtobool, tensor_size, to_numpy, to_py_obj, transpose, working_or_temp_dir, ) from .hub import ( CLOUDFRONT_DISTRIB_PREFIX, DISABLE_TELEMETRY, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, EntryNotFoundError, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, cached_file, default_cache_path, define_sagemaker_information, download_url, extract_commit_hash, get_cached_models, get_file_from_repo, get_full_repo_name, has_file, http_user_agent, is_offline_mode, is_remote_url, move_cache, send_example_telemetry, try_to_load_from_cache, ) from .import_utils import ( ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, TORCH_FX_REQUIRED_VERSION, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, ccl_version, direct_transformers_import, get_torch_version, is_accelerate_available, is_apex_available, is_bitsandbytes_available, is_bsa_available, is_coloredlogs_available, is_cython_available, is_datasets_available, is_decord_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_jieba_available, is_jumanpp_available, is_kenlm_available, is_keras_nlp_available, is_librosa_available, is_natten_available, is_ninja_available, is_onnx_available, is_openai_available, is_optimum_available, is_pandas_available, is_peft_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytest_available, is_pytorch_quantization_available, is_rjieba_available, is_sacremoses_available, is_safetensors_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_sudachi_available, is_tensorflow_probability_available, is_tensorflow_text_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_bfaa_cpu_available, is_torch_bfaa_gpu_available, is_torch_compile_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_neuroncore_available, is_torch_tensorrt_fx_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_torchdistx_available, is_torchdynamo_available, is_torchvision_available, is_training_run_on_sagemaker, is_vision_available, requires_backends, torch_only_method, ) SCREAMING_SNAKE_CASE :List[str] = 'pytorch_model.bin' SCREAMING_SNAKE_CASE :str = 'pytorch_model.bin.index.json' SCREAMING_SNAKE_CASE :Optional[int] = 'adapter_config.json' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.bin' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.safetensors' SCREAMING_SNAKE_CASE :str = 'tf_model.h5' SCREAMING_SNAKE_CASE :List[Any] = 'tf_model.h5.index.json' SCREAMING_SNAKE_CASE :str = 'model.ckpt' SCREAMING_SNAKE_CASE :List[Any] = 'flax_model.msgpack' SCREAMING_SNAKE_CASE :Optional[int] = 'flax_model.msgpack.index.json' SCREAMING_SNAKE_CASE :Tuple = 'model.safetensors' SCREAMING_SNAKE_CASE :List[Any] = 'model.safetensors.index.json' SCREAMING_SNAKE_CASE :str = 'config.json' SCREAMING_SNAKE_CASE :int = 'preprocessor_config.json' SCREAMING_SNAKE_CASE :Optional[Any] = FEATURE_EXTRACTOR_NAME SCREAMING_SNAKE_CASE :Optional[int] = 'generation_config.json' SCREAMING_SNAKE_CASE :List[str] = 'modelcard.json' SCREAMING_SNAKE_CASE :Optional[int] = '▁' SCREAMING_SNAKE_CASE :Optional[Any] = SENTENCEPIECE_UNDERLINE # Kept for backward compatibility SCREAMING_SNAKE_CASE :str = [ [[0, 1, 0, 1], [1, 0, 0, 1]] ] * 2 # Needs to have 0s and 1s only since XLM uses it for langs too. SCREAMING_SNAKE_CASE :Optional[Any] = [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]] SCREAMING_SNAKE_CASE :List[Any] = [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]] def UpperCAmelCase ( a_ ) -> Dict: """simple docstring""" if version.parse(a_ ) < version.parse(a_ ): if "dev" in min_version: __A = ( "This example requires a source install from HuggingFace Transformers (see " "`https://huggingface.co/docs/transformers/installation#install-from-source`)," ) else: __A = F'''This example requires a minimum version of {min_version},''' error_message += F''' but the version found is {__version__}.\n''' raise ImportError( error_message + "Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other " "versions of HuggingFace Transformers." )
15
1
from operator import delitem, getitem, setitem import pytest from data_structures.hashing.hash_map import HashMap def UpperCAmelCase ( a_ ) -> Union[str, Any]: """simple docstring""" return getitem, k def UpperCAmelCase ( a_ , a_ ) -> Any: """simple docstring""" return setitem, k, v def UpperCAmelCase ( a_ ) -> Any: """simple docstring""" return delitem, k def UpperCAmelCase ( a_ , a_ , *a_ ) -> Any: """simple docstring""" try: return fun(a_ , *a_ ), None except Exception as e: return None, e SCREAMING_SNAKE_CASE :int = ( _set('key_a', 'val_a'), _set('key_b', 'val_b'), ) SCREAMING_SNAKE_CASE :int = [ _set('key_a', 'val_a'), _set('key_a', 'val_b'), ] SCREAMING_SNAKE_CASE :str = [ _set('key_a', 'val_a'), _set('key_b', 'val_b'), _del('key_a'), _del('key_b'), _set('key_a', 'val_a'), _del('key_a'), ] SCREAMING_SNAKE_CASE :List[str] = [ _get('key_a'), _del('key_a'), _set('key_a', 'val_a'), _del('key_a'), _del('key_a'), _get('key_a'), ] SCREAMING_SNAKE_CASE :str = [ *[_set(x, x) for x in range(5)], # guaranteed upsize ] SCREAMING_SNAKE_CASE :str = [ *[_set(x, x) for x in range(5)], # guaranteed upsize *[_del(x) for x in range(5)], _set('key_a', 'val_b'), ] @pytest.mark.parametrize( "operations" , ( pytest.param(_add_items , id="add items" ), pytest.param(_overwrite_items , id="overwrite items" ), pytest.param(_delete_items , id="delete items" ), pytest.param(_access_absent_items , id="access absent items" ), pytest.param(_add_with_resize_up , id="add with resize up" ), pytest.param(_add_with_resize_down , id="add with resize down" ), ) , ) def UpperCAmelCase ( a_ ) -> Any: """simple docstring""" __A = HashMap(initial_block_size=4 ) __A = {} for _, (fun, *args) in enumerate(a_ ): __A , __A = _run_operation(a_ , a_ , *a_ ) __A , __A = _run_operation(a_ , a_ , *a_ ) assert my_res == py_res assert str(a_ ) == str(a_ ) assert set(a_ ) == set(a_ ) assert len(a_ ) == len(a_ ) assert set(my.items() ) == set(py.items() ) def UpperCAmelCase ( ) -> List[str]: """simple docstring""" def is_public(a_ ) -> bool: return not name.startswith("_" ) __A = {name for name in dir({} ) if is_public(a_ )} __A = {name for name in dir(HashMap() ) if is_public(a_ )} assert dict_public_names > hash_public_names
15
def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" __A = [0] * len(a_ ) __A = [] __A = [1] * len(a_ ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(a_ ) ): if indegree[i] == 0: queue.append(a_ ) while queue: __A = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: __A = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(a_ ) print(max(a_ ) ) # Adjacency list of Graph SCREAMING_SNAKE_CASE :List[Any] = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
15
1
from dataclasses import dataclass from typing import Tuple import numpy as np import torch @dataclass class UpperCAmelCase : '''simple docstring''' snake_case_ = 42 # [batch_size x 3] snake_case_ = 42 # [batch_size x 3] snake_case_ = 42 # [batch_size x 3] snake_case_ = 42 # [batch_size x 3] snake_case_ = 42 snake_case_ = 42 snake_case_ = 42 snake_case_ = 42 snake_case_ = 42 def UpperCamelCase_ ( self : Optional[Any] ): assert self.x.shape[0] == self.y.shape[0] == self.z.shape[0] == self.origin.shape[0] assert self.x.shape[1] == self.y.shape[1] == self.z.shape[1] == self.origin.shape[1] == 3 assert len(self.x.shape ) == len(self.y.shape ) == len(self.z.shape ) == len(self.origin.shape ) == 2 def UpperCamelCase_ ( self : Optional[int] ): return torch.from_numpy(np.array([self.width, self.height] ,dtype=np.floataa ) ) def UpperCamelCase_ ( self : List[Any] ): return torch.from_numpy(np.array([self.x_fov, self.y_fov] ,dtype=np.floataa ) ) def UpperCamelCase_ ( self : Dict ): __A = torch.arange(self.height * self.width ) __A = torch.stack( [ pixel_indices % self.width, torch.div(A ,self.width ,rounding_mode="trunc" ), ] ,axis=1 ,) return coords @property def UpperCamelCase_ ( self : Any ): __A , *__A = self.shape __A = int(np.prod(A ) ) __A = self.get_image_coords() __A = torch.broadcast_to(coords.unsqueeze(0 ) ,[batch_size * inner_batch_size, *coords.shape] ) __A = self.get_camera_rays(A ) __A = rays.view(A ,inner_batch_size * self.height * self.width ,2 ,3 ) return rays def UpperCamelCase_ ( self : str ,A : torch.Tensor ): __A , *__A , __A = coords.shape assert n_coords == 2 assert batch_size == self.origin.shape[0] __A = coords.view(A ,-1 ,2 ) __A = self.resolution() __A = self.fov() __A = (flat.float() / (res - 1)) * 2 - 1 __A = fracs * torch.tan(fov / 2 ) __A = fracs.view(A ,-1 ,2 ) __A = ( self.z.view(A ,1 ,3 ) + self.x.view(A ,1 ,3 ) * fracs[:, :, :1] + self.y.view(A ,1 ,3 ) * fracs[:, :, 1:] ) __A = directions / directions.norm(dim=-1 ,keepdim=A ) __A = torch.stack( [ torch.broadcast_to(self.origin.view(A ,1 ,3 ) ,[batch_size, directions.shape[1], 3] ), directions, ] ,dim=2 ,) return rays.view(A ,*A ,2 ,3 ) def UpperCamelCase_ ( self : Optional[Any] ,A : int ,A : int ): assert width * self.height == height * self.width, "The aspect ratio should not change." return DifferentiableProjectiveCamera( origin=self.origin ,x=self.x ,y=self.y ,z=self.z ,width=A ,height=A ,x_fov=self.x_fov ,y_fov=self.y_fov ,) def UpperCAmelCase ( a_ ) -> DifferentiableProjectiveCamera: """simple docstring""" __A = [] __A = [] __A = [] __A = [] for theta in np.linspace(0 , 2 * np.pi , num=2_0 ): __A = np.array([np.sin(a_ ), np.cos(a_ ), -0.5] ) z /= np.sqrt(np.sum(z**2 ) ) __A = -z * 4 __A = np.array([np.cos(a_ ), -np.sin(a_ ), 0.0] ) __A = np.cross(a_ , a_ ) origins.append(a_ ) xs.append(a_ ) ys.append(a_ ) zs.append(a_ ) return DifferentiableProjectiveCamera( origin=torch.from_numpy(np.stack(a_ , axis=0 ) ).float() , x=torch.from_numpy(np.stack(a_ , axis=0 ) ).float() , y=torch.from_numpy(np.stack(a_ , axis=0 ) ).float() , z=torch.from_numpy(np.stack(a_ , axis=0 ) ).float() , width=a_ , height=a_ , x_fov=0.7 , y_fov=0.7 , shape=(1, len(a_ )) , )
15
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items() ) def UpperCAmelCase ( a_ , a_ ) -> Tuple: """simple docstring""" __A = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue __A = key.replace("heads.cmd.mim_head.cls.predictions" , "mmm_image_head" ) __A = key.replace("heads.cmd.mlm_head.cls.predictions" , "mmm_text_head" ) __A = key.replace("heads.cmd.itm_head.cls" , "itm_head" ) __A = key.replace("heads.cmd.itm_head.pooler" , "itm_head.pooler" ) __A = key.replace("heads.cmd.clip_head.logit_scale" , "flava.logit_scale" ) __A = key.replace("heads.fairseq_mlm.cls.predictions" , "mlm_head" ) __A = key.replace("heads.imagenet.mim_head.cls.predictions" , "mim_head" ) __A = key.replace("mm_text_projection" , "flava.text_to_mm_projection" ) __A = key.replace("mm_image_projection" , "flava.image_to_mm_projection" ) __A = key.replace("image_encoder.module" , "flava.image_model" ) __A = key.replace("text_encoder.module" , "flava.text_model" ) __A = key.replace("mm_encoder.module.encoder.cls_token" , "flava.multimodal_model.cls_token" ) __A = key.replace("mm_encoder.module" , "flava.multimodal_model" ) __A = key.replace("text_projection" , "flava.text_projection" ) __A = key.replace("image_projection" , "flava.image_projection" ) __A = value.float() for key, value in codebook_state_dict.items(): __A = value return upgrade @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_ , a_=None ) -> Tuple: """simple docstring""" if config_path is not None: __A = FlavaConfig.from_pretrained(a_ ) else: __A = FlavaConfig() __A = FlavaForPreTraining(a_ ).eval() __A = convert_dalle_checkpoint(a_ , a_ , save_checkpoint=a_ ) if os.path.exists(a_ ): __A = torch.load(a_ , map_location="cpu" ) else: __A = torch.hub.load_state_dict_from_url(a_ , map_location="cpu" ) __A = upgrade_state_dict(a_ , a_ ) hf_model.load_state_dict(a_ ) __A = hf_model.state_dict() __A = count_parameters(a_ ) __A = count_parameters(a_ ) + count_parameters(a_ ) assert torch.allclose(a_ , a_ , atol=1E-3 ) hf_model.save_pretrained(a_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Any = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to flava checkpoint') parser.add_argument('--codebook_path', default=None, type=str, help='Path to flava codebook checkpoint') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
15
1
SCREAMING_SNAKE_CASE :int = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def UpperCAmelCase ( ) -> None: """simple docstring""" __A = input("Enter message: " ) __A = input("Enter key [alphanumeric]: " ) __A = input("Encrypt/Decrypt [e/d]: " ) if mode.lower().startswith("e" ): __A = "encrypt" __A = encrypt_message(a_ , a_ ) elif mode.lower().startswith("d" ): __A = "decrypt" __A = decrypt_message(a_ , a_ ) print(F'''\n{mode.title()}ed message:''' ) print(a_ ) def UpperCAmelCase ( a_ , a_ ) -> str: """simple docstring""" return translate_message(a_ , a_ , "encrypt" ) def UpperCAmelCase ( a_ , a_ ) -> str: """simple docstring""" return translate_message(a_ , a_ , "decrypt" ) def UpperCAmelCase ( a_ , a_ , a_ ) -> str: """simple docstring""" __A = [] __A = 0 __A = key.upper() for symbol in message: __A = LETTERS.find(symbol.upper() ) if num != -1: if mode == "encrypt": num += LETTERS.find(key[key_index] ) elif mode == "decrypt": num -= LETTERS.find(key[key_index] ) num %= len(a_ ) if symbol.isupper(): translated.append(LETTERS[num] ) elif symbol.islower(): translated.append(LETTERS[num].lower() ) key_index += 1 if key_index == len(a_ ): __A = 0 else: translated.append(a_ ) return "".join(a_ ) if __name__ == "__main__": main()
15
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Optional[int] = {'vocab_file': 'sentencepiece.bpe.model'} SCREAMING_SNAKE_CASE :Tuple = { 'vocab_file': { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model', } } SCREAMING_SNAKE_CASE :List[Any] = { 'camembert-base': 512, } SCREAMING_SNAKE_CASE :List[str] = '▁' class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[Any] ,A : List[str] ,A : List[Any]="<s>" ,A : Tuple="</s>" ,A : Any="</s>" ,A : Optional[Any]="<s>" ,A : Tuple="<unk>" ,A : str="<pad>" ,A : int="<mask>" ,A : Optional[int]=["<s>NOTUSED", "</s>NOTUSED"] ,A : Optional[Dict[str, Any]] = None ,**A : Optional[Any] ,): # Mask token behave like a normal word, i.e. include the space before it __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,sep_token=A ,cls_token=A ,pad_token=A ,mask_token=A ,additional_special_tokens=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(A ) ) __A = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __A = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __A = len(self.fairseq_tokens_to_ids ) __A = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __A = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCamelCase_ ( self : int ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def UpperCamelCase_ ( self : Union[str, Any] ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase_ ( self : Dict ): return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCamelCase_ ( self : int ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Dict ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(A ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : Tuple ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCamelCase_ ( self : Optional[Any] ,A : Dict ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def __getstate__( self : Dict ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Union[str, Any] ,A : Any ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
15
1
import argparse from pathlib import Path import torch from packaging import version from torch.onnx import export from diffusers import AutoencoderKL SCREAMING_SNAKE_CASE :str = version.parse(version.parse(torch.__version__).base_version) < version.parse('1.11') def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ , a_ , a_ , a_=False , ) -> Optional[Any]: """simple docstring""" output_path.parent.mkdir(parents=a_ , exist_ok=a_ ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( a_ , a_ , f=output_path.as_posix() , input_names=a_ , output_names=a_ , dynamic_axes=a_ , do_constant_folding=a_ , use_external_data_format=a_ , enable_onnx_checker=a_ , opset_version=a_ , ) else: export( a_ , a_ , f=output_path.as_posix() , input_names=a_ , output_names=a_ , dynamic_axes=a_ , do_constant_folding=a_ , opset_version=a_ , ) @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_ , a_ = False ) -> Any: """simple docstring""" __A = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): __A = "cuda" elif fpaa and not torch.cuda.is_available(): raise ValueError("`float16` model export is only supported on GPUs with CUDA" ) else: __A = "cpu" __A = Path(a_ ) # VAE DECODER __A = AutoencoderKL.from_pretrained(model_path + "/vae" ) __A = vae_decoder.config.latent_channels # forward only through the decoder part __A = vae_decoder.decode onnx_export( a_ , model_args=( torch.randn(1 , a_ , 2_5 , 2_5 ).to(device=a_ , dtype=a_ ), False, ) , output_path=output_path / "vae_decoder" / "model.onnx" , ordered_input_names=["latent_sample", "return_dict"] , output_names=["sample"] , dynamic_axes={ "latent_sample": {0: "batch", 1: "channels", 2: "height", 3: "width"}, } , opset=a_ , ) del vae_decoder if __name__ == "__main__": SCREAMING_SNAKE_CASE :Optional[Any] = argparse.ArgumentParser() parser.add_argument( '--model_path', type=str, required=True, help='Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).', ) parser.add_argument('--output_path', type=str, required=True, help='Path to the output model.') parser.add_argument( '--opset', default=14, type=int, help='The version of the ONNX operator set to use.', ) parser.add_argument('--fp16', action='store_true', default=False, help='Export the models in `float16` mode') SCREAMING_SNAKE_CASE :int = parser.parse_args() print(args.output_path) convert_models(args.model_path, args.output_path, args.opset, args.fpaa) print('SD: Done: ONNX')
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return [tuple(a_ )] __A = [] def generate(a_ , a_ ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , a_ ) for i in range(k - 1 ): if k % 2 == 0: # k is even __A , __A = arr[k - 1], arr[i] else: # k is odd __A , __A = arr[k - 1], arr[0] generate(k - 1 , a_ ) generate(len(a_ ) , a_ ) return res if __name__ == "__main__": SCREAMING_SNAKE_CASE :int = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :Dict = [int(item) for item in user_input.split(',')] print(heaps(arr))
15
1
from typing import Optional, Tuple import jax import jax.numpy as jnp from flax import linen as nn from flax.core.frozen_dict import FrozenDict from transformers import CLIPConfig, FlaxPreTrainedModel from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule def UpperCAmelCase ( a_ , a_ , a_=1E-12 ) -> List[str]: """simple docstring""" __A = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(a_ , axis=1 ) , a_min=a_ ) ).T __A = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(a_ , axis=1 ) , a_min=a_ ) ).T return jnp.matmul(a_ , norm_emb_a.T ) class UpperCAmelCase ( nn.Module ): '''simple docstring''' snake_case_ = 42 snake_case_ = jnp.floataa def UpperCamelCase_ ( self : List[str] ): __A = FlaxCLIPVisionModule(self.config.vision_config ) __A = nn.Dense(self.config.projection_dim ,use_bias=A ,dtype=self.dtype ) __A = self.param("concept_embeds" ,jax.nn.initializers.ones ,(17, self.config.projection_dim) ) __A = self.param( "special_care_embeds" ,jax.nn.initializers.ones ,(3, self.config.projection_dim) ) __A = self.param("concept_embeds_weights" ,jax.nn.initializers.ones ,(17,) ) __A = self.param("special_care_embeds_weights" ,jax.nn.initializers.ones ,(3,) ) def __call__( self : Tuple ,A : Any ): __A = self.vision_model(A )[1] __A = self.visual_projection(A ) __A = jax_cosine_distance(A ,self.special_care_embeds ) __A = jax_cosine_distance(A ,self.concept_embeds ) # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign image inputs __A = 0.0 __A = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment __A = jnp.round(A ,3 ) __A = jnp.any(special_scores > 0 ,axis=1 ,keepdims=A ) # Use a lower threshold if an image has any special care concept __A = is_special_care * 0.01 __A = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment __A = jnp.round(A ,3 ) __A = jnp.any(concept_scores > 0 ,axis=1 ) return has_nsfw_concepts class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = CLIPConfig snake_case_ = "clip_input" snake_case_ = FlaxStableDiffusionSafetyCheckerModule def __init__( self : int ,A : CLIPConfig ,A : Optional[Tuple] = None ,A : int = 0 ,A : jnp.dtype = jnp.floataa ,A : bool = True ,**A : Tuple ,): if input_shape is None: __A = (1, 2_24, 2_24, 3) __A = self.module_class(config=A ,dtype=A ,**A ) super().__init__(A ,A ,input_shape=A ,seed=A ,dtype=A ,_do_init=_do_init ) def UpperCamelCase_ ( self : int ,A : jax.random.KeyArray ,A : Tuple ,A : FrozenDict = None ): # init input tensor __A = jax.random.normal(A ,A ) __A , __A = jax.random.split(A ) __A = {"params": params_rng, "dropout": dropout_rng} __A = self.module.init(A ,A )["params"] return random_params def __call__( self : Tuple ,A : Dict ,A : dict = None ,): __A = jnp.transpose(A ,(0, 2, 3, 1) ) return self.module.apply( {"params": params or self.params} ,jnp.array(A ,dtype=jnp.floataa ) ,rngs={} ,)
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return lst __A = 1 while i < len(a_ ): if lst[i - 1] <= lst[i]: i += 1 else: __A , __A = lst[i], lst[i - 1] i -= 1 if i == 0: __A = 1 return lst if __name__ == "__main__": SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :List[Any] = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
15
1
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 SCREAMING_SNAKE_CASE :List[str] = [ 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) SCREAMING_SNAKE_CASE :List[str] = logging.getLogger() def UpperCAmelCase ( ) -> List[str]: """simple docstring""" __A = argparse.ArgumentParser() parser.add_argument("-f" ) __A = parser.parse_args() return args.f def UpperCAmelCase ( a_ , a_="eval" ) -> Dict: """simple docstring""" __A = os.path.join(a_ , F'''{split}_results.json''' ) if os.path.exists(a_ ): with open(a_ , "r" ) as f: return json.load(a_ ) raise ValueError(F'''can\'t find {path}''' ) SCREAMING_SNAKE_CASE :Any = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def UpperCamelCase_ ( self : Any ): __A = self.get_auto_remove_tmp_dir() __A = 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(A ,"argv" ,A ): run_flax_glue.main() __A = get_results(A ) self.assertGreaterEqual(result["eval_accuracy"] ,0.75 ) @slow def UpperCamelCase_ ( self : List[str] ): __A = self.get_auto_remove_tmp_dir() __A = 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(A ,"argv" ,A ): run_clm_flax.main() __A = get_results(A ) self.assertLess(result["eval_perplexity"] ,1_00 ) @slow def UpperCamelCase_ ( self : int ): __A = self.get_auto_remove_tmp_dir() __A = 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(A ,"argv" ,A ): run_summarization_flax.main() __A = get_results(A ,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 : int ): __A = self.get_auto_remove_tmp_dir() __A = 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(A ,"argv" ,A ): run_mlm_flax.main() __A = get_results(A ) self.assertLess(result["eval_perplexity"] ,42 ) @slow def UpperCamelCase_ ( self : Any ): __A = self.get_auto_remove_tmp_dir() __A = 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(A ,"argv" ,A ): run_ta_mlm_flax.main() __A = get_results(A ) self.assertGreaterEqual(result["eval_accuracy"] ,0.42 ) @slow def UpperCamelCase_ ( self : Any ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu __A = 7 if get_gpu_count() > 1 else 2 __A = self.get_auto_remove_tmp_dir() __A = 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(A ,"argv" ,A ): run_flax_ner.main() __A = get_results(A ) self.assertGreaterEqual(result["eval_accuracy"] ,0.75 ) self.assertGreaterEqual(result["eval_f1"] ,0.3 ) @slow def UpperCamelCase_ ( self : int ): __A = self.get_auto_remove_tmp_dir() __A = 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(A ,"argv" ,A ): run_qa.main() __A = get_results(A ) self.assertGreaterEqual(result["eval_f1"] ,30 ) self.assertGreaterEqual(result["eval_exact"] ,30 )
15
from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 snake_case_ = 42 snake_case_ = None class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 2 @register_to_config def __init__( self : str ,A : float = 0.02 ,A : float = 1_00 ,A : float = 1.0_07 ,A : float = 80 ,A : float = 0.05 ,A : float = 50 ,): # standard deviation of the initial noise distribution __A = sigma_max # setable values __A = None __A = None __A = None # sigma(t_i) def UpperCamelCase_ ( self : str ,A : torch.FloatTensor ,A : Optional[int] = None ): return sample def UpperCamelCase_ ( self : Dict ,A : int ,A : Union[str, torch.device] = None ): __A = num_inference_steps __A = np.arange(0 ,self.num_inference_steps )[::-1].copy() __A = torch.from_numpy(A ).to(A ) __A = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] __A = torch.tensor(A ,dtype=torch.floataa ,device=A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : torch.FloatTensor ,A : float ,A : Optional[torch.Generator] = None ): if self.config.s_min <= sigma <= self.config.s_max: __A = min(self.config.s_churn / self.num_inference_steps ,2**0.5 - 1 ) else: __A = 0 # sample eps ~ N(0, S_noise^2 * I) __A = self.config.s_noise * randn_tensor(sample.shape ,generator=A ).to(sample.device ) __A = sigma + gamma * sigma __A = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def UpperCamelCase_ ( self : Dict ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_hat + sigma_hat * model_output __A = (sample_hat - pred_original_sample) / sigma_hat __A = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : Optional[int] ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_prev + sigma_prev * model_output __A = (sample_prev - pred_original_sample) / sigma_prev __A = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : List[Any] ,A : Dict ,A : List[str] ,A : str ): raise NotImplementedError()
15
1
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return [tuple(a_ )] __A = [] def generate(a_ , a_ ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , a_ ) for i in range(k - 1 ): if k % 2 == 0: # k is even __A , __A = arr[k - 1], arr[i] else: # k is odd __A , __A = arr[k - 1], arr[0] generate(k - 1 , a_ ) generate(len(a_ ) , a_ ) return res if __name__ == "__main__": SCREAMING_SNAKE_CASE :int = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :Dict = [int(item) for item in user_input.split(',')] print(heaps(arr))
15
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version SCREAMING_SNAKE_CASE :Union[str, Any] = get_logger(__name__) class UpperCAmelCase : '''simple docstring''' snake_case_ = "dummy_data" snake_case_ = "datasets" snake_case_ = False def __init__( self : Optional[int] ,A : str ,A : str ,A : Union[Version, str] ,A : Optional[str] = None ,A : bool = False ,A : bool = True ,A : Optional[List[Callable]] = None ,): __A = 0 __A = dataset_name __A = cache_dir __A = use_local_dummy_data __A = config # download_callbacks take a single url as input __A = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root __A = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general __A = str(A ) # to be downloaded __A = None __A = None @property def UpperCamelCase_ ( self : Union[str, Any] ): if self._dummy_file is None: __A = self.download_dummy_data() return self._dummy_file @property def UpperCamelCase_ ( self : Optional[Any] ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("dummy" ,self.config.name ,self.version_name ) # structure is dummy / version_name return os.path.join("dummy" ,self.version_name ) @property def UpperCamelCase_ ( self : List[Any] ): return os.path.join(self.dummy_data_folder ,"dummy_data.zip" ) def UpperCamelCase_ ( self : Tuple ): __A = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) __A = cached_path( A ,cache_dir=self.cache_dir ,extract_compressed_file=A ,force_extract=A ) return os.path.join(A ,self.dummy_file_name ) @property def UpperCamelCase_ ( self : str ): return os.path.join(self.datasets_scripts_dir ,self.dataset_name ,self.dummy_zip_file ) @property def UpperCamelCase_ ( self : Any ): if self._bucket_url is None: __A = hf_github_url(self.dataset_name ,self.dummy_zip_file.replace(os.sep ,"/" ) ) return self._bucket_url @property def UpperCamelCase_ ( self : Tuple ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep ,"/" ).split("/" )[:-1] ) def UpperCamelCase_ ( self : List[str] ,A : List[Any] ,*A : Dict ): if self.load_existing_dummy_data: # dummy data is downloaded and tested __A = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned __A = self.dummy_file_name # special case when data_url is a dict if isinstance(A ,A ): return self.create_dummy_data_dict(A ,A ) elif isinstance(A ,(list, tuple) ): return self.create_dummy_data_list(A ,A ) else: return self.create_dummy_data_single(A ,A ) def UpperCamelCase_ ( self : str ,A : List[Any] ,*A : List[Any] ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : List[str] ,A : List[str] ,A : Tuple ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : Any ,A : Any ,*A : Optional[Any] ,**A : List[str] ): return path def UpperCamelCase_ ( self : str ): return {} def UpperCamelCase_ ( self : int ,A : int ,A : Tuple ): __A = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(A ,A ): for single_url in single_urls: download_callback(A ) else: __A = single_urls download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(A ,A ): __A = [os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) for x in single_urls] else: __A = single_urls __A = os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) __A = value # make sure that values are unique if all(isinstance(A ,A ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique __A = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,A : str ): __A = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one __A = all(bool(re.findall("[0-9]{3,}-of-[0-9]{3,}" ,A ) ) for url in data_url ) __A = all( url.startswith("https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): __A = [data_url[0]] * len(A ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(single_url.split("/" )[-1] ) ) dummy_data_list.append(A ) return dummy_data_list def UpperCamelCase_ ( self : str ,A : List[Any] ,A : Optional[Any] ): for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(data_url.split("/" )[-1] ) ) if os.path.exists(A ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def UpperCamelCase_ ( self : int ): pass def UpperCamelCase_ ( self : Dict ): pass def UpperCamelCase_ ( self : Optional[Any] ,A : List[Any] ): def _iter_archive_members(A : Optional[Any] ): # this preserves the order of the members inside the ZIP archive __A = Path(self.dummy_file ).parent __A = path.relative_to(A ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: __A = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(A ) __A = Path(A ) __A = _iter_archive_members(A ) if self.use_local_dummy_data else path.rglob("*" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((".", "__") ): yield file_path.relative_to(A ).as_posix(), file_path.open("rb" ) def UpperCamelCase_ ( self : List[Any] ,A : Any ): if not isinstance(A ,A ): __A = [paths] for path in paths: if os.path.isfile(A ): if os.path.basename(A ).startswith((".", "__") ): return yield path else: for dirpath, dirnames, filenames in os.walk(A ): if os.path.basename(A ).startswith((".", "__") ): continue dirnames.sort() for filename in sorted(A ): if filename.startswith((".", "__") ): continue yield os.path.join(A ,A )
15
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Optional[int] = {'vocab_file': 'sentencepiece.bpe.model'} SCREAMING_SNAKE_CASE :Tuple = { 'vocab_file': { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model', } } SCREAMING_SNAKE_CASE :List[Any] = { 'camembert-base': 512, } SCREAMING_SNAKE_CASE :List[str] = '▁' class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[Any] ,A : List[str] ,A : List[Any]="<s>" ,A : Tuple="</s>" ,A : Any="</s>" ,A : Optional[Any]="<s>" ,A : Tuple="<unk>" ,A : str="<pad>" ,A : int="<mask>" ,A : Optional[int]=["<s>NOTUSED", "</s>NOTUSED"] ,A : Optional[Dict[str, Any]] = None ,**A : Optional[Any] ,): # Mask token behave like a normal word, i.e. include the space before it __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,sep_token=A ,cls_token=A ,pad_token=A ,mask_token=A ,additional_special_tokens=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(A ) ) __A = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __A = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __A = len(self.fairseq_tokens_to_ids ) __A = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __A = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCamelCase_ ( self : int ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def UpperCamelCase_ ( self : Union[str, Any] ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase_ ( self : Dict ): return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCamelCase_ ( self : int ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Dict ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(A ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : Tuple ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCamelCase_ ( self : Optional[Any] ,A : Dict ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def __getstate__( self : Dict ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Union[str, Any] ,A : Any ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
15
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available SCREAMING_SNAKE_CASE :List[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :List[str] = ['BartphoTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys SCREAMING_SNAKE_CASE :Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
15
1
from typing import Dict, Iterable, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = ["pixel_values"] def __init__( self : Dict ,A : bool = True ,A : Dict[str, int] = None ,A : PILImageResampling = PILImageResampling.BICUBIC ,A : bool = True ,A : Dict[str, int] = None ,A : bool = True ,A : Union[int, float] = 1 / 2_55 ,A : bool = True ,A : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN ,A : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD ,**A : Dict ,): super().__init__(**A ) __A = size if size is not None else {"shortest_edge": 2_24} __A = get_size_dict(A ,default_to_square=A ) __A = crop_size if crop_size is not None else {"height": 2_24, "width": 2_24} __A = get_size_dict(A ,param_name="crop_size" ) __A = do_resize __A = size __A = resample __A = do_center_crop __A = crop_size __A = do_rescale __A = rescale_factor __A = do_normalize __A = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN __A = image_std if image_std is not None else IMAGENET_DEFAULT_STD def UpperCamelCase_ ( self : Union[str, Any] ,A : np.ndarray ,A : Dict[str, int] ,A : PILImageResampling = PILImageResampling.BICUBIC ,A : Optional[Union[str, ChannelDimension]] = None ,**A : Optional[Any] ,): __A = get_size_dict(A ,default_to_square=A ) # size_dict is a dict with either keys "height" and "width" or "shortest_edge" if "shortest_edge" in size: __A = int((2_56 / 2_24) * size["shortest_edge"] ) __A = get_resize_output_image_size(A ,size=A ,default_to_square=A ) __A = {"height": output_size[0], "width": output_size[1]} if "height" not in size_dict or "width" not in size_dict: raise ValueError( f'''Size dict must have keys \'height\' and \'width\' or \'shortest_edge\'. Got {size_dict.keys()}''' ) return resize( A ,size=(size_dict["height"], size_dict["width"]) ,resample=A ,data_format=A ,**A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : np.ndarray ,A : Dict[str, int] ,A : Optional[Union[str, ChannelDimension]] = None ,**A : Tuple ,): __A = get_size_dict(A ) if "height" not in size or "width" not in size: raise ValueError(f'''Size dict must have keys \'height\' and \'width\'. Got {size.keys()}''' ) return center_crop(A ,size=(size["height"], size["width"]) ,data_format=A ,**A ) def UpperCamelCase_ ( self : Any ,A : np.ndarray ,A : Union[int, float] ,A : Optional[Union[str, ChannelDimension]] = None ,**A : str ,): return rescale(A ,scale=A ,data_format=A ,**A ) def UpperCamelCase_ ( self : List[Any] ,A : np.ndarray ,A : Union[float, List[float]] ,A : Union[float, List[float]] ,A : Optional[Union[str, ChannelDimension]] = None ,**A : str ,): return normalize(A ,mean=A ,std=A ,data_format=A ,**A ) def UpperCamelCase_ ( self : int ,A : ImageInput ,A : Optional[bool] = None ,A : Optional[Dict[str, int]] = None ,A : PILImageResampling = None ,A : Optional[bool] = None ,A : Optional[Dict[str, int]] = None ,A : Optional[bool] = None ,A : Optional[float] = None ,A : Optional[bool] = None ,A : Optional[Union[float, Iterable[float]]] = None ,A : Optional[Union[float, Iterable[float]]] = None ,A : Optional[TensorType] = None ,A : ChannelDimension = ChannelDimension.FIRST ,**A : str ,): __A = do_resize if do_resize is not None else self.do_resize __A = resample if resample is not None else self.resample __A = do_center_crop if do_center_crop is not None else self.do_center_crop __A = do_rescale if do_rescale is not None else self.do_rescale __A = rescale_factor if rescale_factor is not None else self.rescale_factor __A = do_normalize if do_normalize is not None else self.do_normalize __A = image_mean if image_mean is not None else self.image_mean __A = image_std if image_std is not None else self.image_std __A = size if size is not None else self.size __A = get_size_dict(A ,default_to_square=A ) __A = crop_size if crop_size is not None else self.crop_size __A = get_size_dict(A ,param_name="crop_size" ) __A = make_list_of_images(A ) if not valid_images(A ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # All transformations expect numpy arrays. __A = [to_numpy_array(A ) for image in images] if do_resize: __A = [self.resize(A ,A ,A ) for image in images] if do_center_crop: __A = [self.center_crop(A ,A ) for image in images] if do_rescale: __A = [self.rescale(A ,A ) for image in images] if do_normalize: __A = [self.normalize(A ,A ,A ) for image in images] __A = [to_channel_dimension_format(A ,A ) for image in images] __A = {"pixel_values": images} return BatchFeature(data=A ,tensor_type=A )
15
from typing import Dict, Optional import numpy as np import datasets SCREAMING_SNAKE_CASE :List[Any] = '\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n' SCREAMING_SNAKE_CASE :List[str] = '\nArgs:\n predictions (`List[ndarray]`):\n List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n references (`List[ndarray]`):\n List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n num_labels (`int`):\n Number of classes (categories).\n ignore_index (`int`):\n Index that will be ignored during evaluation.\n nan_to_num (`int`, *optional*):\n If specified, NaN values will be replaced by the number defined by the user.\n label_map (`dict`, *optional*):\n If specified, dictionary mapping old label indices to new label indices.\n reduce_labels (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n `Dict[str, float | ndarray]` comprising various elements:\n - *mean_iou* (`float`):\n Mean Intersection-over-Union (IoU averaged over all categories).\n - *mean_accuracy* (`float`):\n Mean accuracy (averaged over all categories).\n - *overall_accuracy* (`float`):\n Overall accuracy on all images.\n - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n Per category accuracy.\n - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n Per category IoU.\n\nExamples:\n\n >>> import numpy as np\n\n >>> mean_iou = datasets.load_metric("mean_iou")\n\n >>> # suppose one has 3 different segmentation maps predicted\n >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n >>> predicted = [predicted_1, predicted_2, predicted_3]\n >>> ground_truth = [actual_1, actual_2, actual_3]\n\n >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), \'per_category_accuracy\': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])}\n' SCREAMING_SNAKE_CASE :str = '\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}' def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Tuple: """simple docstring""" if label_map is not None: for old_id, new_id in label_map.items(): __A = new_id # turn into Numpy arrays __A = np.array(a_ ) __A = np.array(a_ ) if reduce_labels: __A = 2_5_5 __A = label - 1 __A = 2_5_5 __A = label != ignore_index __A = np.not_equal(a_ , a_ ) __A = pred_label[mask] __A = np.array(a_ )[mask] __A = pred_label[pred_label == label] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Union[str, Any]: """simple docstring""" __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(a_ , a_ ): __A , __A , __A , __A = intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = None , a_ = False , ) -> str: """simple docstring""" __A , __A , __A , __A = total_intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) # compute metrics __A = {} __A = total_area_intersect.sum() / total_area_label.sum() __A = total_area_intersect / total_area_union __A = total_area_intersect / total_area_label __A = np.nanmean(a_ ) __A = np.nanmean(a_ ) __A = all_acc __A = iou __A = acc if nan_to_num is not None: __A = {metric: np.nan_to_num(a_ , nan=a_ ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { "predictions": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), "references": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), } ) ,reference_urls=[ "https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py" ] ,) def UpperCamelCase_ ( self : int ,A : Optional[Any] ,A : Optional[Any] ,A : int ,A : bool ,A : Optional[int] = None ,A : Optional[Dict[int, int]] = None ,A : bool = False ,): __A = mean_iou( results=A ,gt_seg_maps=A ,num_labels=A ,ignore_index=A ,nan_to_num=A ,label_map=A ,reduce_labels=A ,) return iou_result
15
1
import enum import warnings from .. import MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING from ..utils import add_end_docstrings, is_tf_available from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf class UpperCAmelCase ( enum.Enum ): '''simple docstring''' snake_case_ = 0 snake_case_ = 1 snake_case_ = 2 @add_end_docstrings(__SCREAMING_SNAKE_CASE ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "\n In 1991, the remains of Russian Tsar Nicholas II and his family (except for Alexei and Maria) are discovered. The\n voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the remainder of the story. 1883 Western\n Siberia, a young Grigori Rasputin is asked by his father and a group of men to perform magic. Rasputin has a vision\n and denounces one of the men as a horse thief. Although his father initially slaps him for making such an\n accusation, Rasputin watches as the man is chased outside and beaten. Twenty years later, Rasputin sees a vision of\n the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous, with people, even a bishop,\n begging for his blessing. <eod> </s> <eos>\n " def __init__( self : List[str] ,*A : Optional[Any] ,**A : str ): super().__init__(*A ,**A ) self.check_model_type( TF_MODEL_FOR_CAUSAL_LM_MAPPING if self.framework == "tf" else MODEL_FOR_CAUSAL_LM_MAPPING ) if "prefix" not in self._preprocess_params: # This is very specific. The logic is quite complex and needs to be done # as a "default". # It also defines both some preprocess_kwargs and generate_kwargs # which is why we cannot put them in their respective methods. __A = None if self.model.config.prefix is not None: __A = self.model.config.prefix if prefix is None and self.model.__class__.__name__ in [ "XLNetLMHeadModel", "TransfoXLLMHeadModel", "TFXLNetLMHeadModel", "TFTransfoXLLMHeadModel", ]: # For XLNet and TransformerXL we add an article to the prompt to give more state to the model. __A = self.XL_PREFIX if prefix is not None: # Recalculate some generate_kwargs linked to prefix. __A , __A , __A = self._sanitize_parameters(prefix=A ,**self._forward_params ) __A = {**self._preprocess_params, **preprocess_params} __A = {**self._forward_params, **forward_params} def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, Any]=None ,A : int=None ,A : Union[str, Any]=None ,A : Optional[Any]=None ,A : Tuple=None ,A : int=None ,A : str=None ,A : Dict=None ,**A : Optional[Any] ,): __A = {} if prefix is not None: __A = prefix if prefix: __A = self.tokenizer( A ,padding=A ,add_special_tokens=A ,return_tensors=self.framework ) __A = prefix_inputs["input_ids"].shape[-1] if handle_long_generation is not None: if handle_long_generation not in {"hole"}: raise ValueError( f'''{handle_long_generation} is not a valid value for `handle_long_generation` parameter expected''' " [None, 'hole']" ) __A = handle_long_generation preprocess_params.update(A ) __A = generate_kwargs __A = {} if return_full_text is not None and return_type is None: if return_text is not None: raise ValueError("`return_text` is mutually exclusive with `return_full_text`" ) if return_tensors is not None: raise ValueError("`return_full_text` is mutually exclusive with `return_tensors`" ) __A = ReturnType.FULL_TEXT if return_full_text else ReturnType.NEW_TEXT if return_tensors is not None and return_type is None: if return_text is not None: raise ValueError("`return_text` is mutually exclusive with `return_tensors`" ) __A = ReturnType.TENSORS if return_type is not None: __A = return_type if clean_up_tokenization_spaces is not None: __A = clean_up_tokenization_spaces if stop_sequence is not None: __A = self.tokenizer.encode(A ,add_special_tokens=A ) if len(A ) > 1: warnings.warn( "Stopping on a multiple token sequence is not yet supported on transformers. The first token of" " the stop sequence will be used as the stop sequence string in the interim." ) __A = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def UpperCamelCase_ ( self : Union[str, Any] ,*A : Any ,**A : int ): # Parse arguments if self.model.__class__.__name__ in ["TransfoXLLMHeadModel"]: kwargs.update({"add_space_before_punct_symbol": True} ) return super()._parse_and_tokenize(*A ,**A ) def __call__( self : List[str] ,A : int ,**A : Dict ): return super().__call__(A ,**A ) def UpperCamelCase_ ( self : str ,A : Optional[int] ,A : Any="" ,A : Union[str, Any]=None ,**A : Any ): __A = self.tokenizer( prefix + prompt_text ,padding=A ,add_special_tokens=A ,return_tensors=self.framework ) __A = prompt_text if handle_long_generation == "hole": __A = inputs["input_ids"].shape[-1] if "max_new_tokens" in generate_kwargs: __A = generate_kwargs["max_new_tokens"] else: __A = generate_kwargs.get("max_length" ,self.model.config.max_length ) - cur_len if new_tokens < 0: raise ValueError("We cannot infer how many new tokens are expected" ) if cur_len + new_tokens > self.tokenizer.model_max_length: __A = self.tokenizer.model_max_length - new_tokens if keep_length <= 0: raise ValueError( "We cannot use `hole` to handle this generation the number of desired tokens exceeds the" " models max length" ) __A = inputs["input_ids"][:, -keep_length:] if "attention_mask" in inputs: __A = inputs["attention_mask"][:, -keep_length:] return inputs def UpperCamelCase_ ( self : int ,A : str ,**A : List[Any] ): __A = model_inputs["input_ids"] __A = model_inputs.get("attention_mask" ,A ) # Allow empty prompts if input_ids.shape[1] == 0: __A = None __A = None __A = 1 else: __A = input_ids.shape[0] __A = model_inputs.pop("prompt_text" ) # If there is a prefix, we may need to adjust the generation length. Do so without permanently modifying # generate_kwargs, as some of the parameterization may come from the initialization of the pipeline. __A = generate_kwargs.pop("prefix_length" ,0 ) if prefix_length > 0: __A = "max_new_tokens" in generate_kwargs or ( "generation_config" in generate_kwargs and generate_kwargs["generation_config"].max_new_tokens is not None ) if not has_max_new_tokens: __A = generate_kwargs.get("max_length" ) or self.model.config.max_length generate_kwargs["max_length"] += prefix_length __A = "min_new_tokens" in generate_kwargs or ( "generation_config" in generate_kwargs and generate_kwargs["generation_config"].min_new_tokens is not None ) if not has_min_new_tokens and "min_length" in generate_kwargs: generate_kwargs["min_length"] += prefix_length # BS x SL __A = self.model.generate(input_ids=A ,attention_mask=A ,**A ) __A = generated_sequence.shape[0] if self.framework == "pt": __A = generated_sequence.reshape(A ,out_b // in_b ,*generated_sequence.shape[1:] ) elif self.framework == "tf": __A = tf.reshape(A ,(in_b, out_b // in_b, *generated_sequence.shape[1:]) ) return {"generated_sequence": generated_sequence, "input_ids": input_ids, "prompt_text": prompt_text} def UpperCamelCase_ ( self : List[Any] ,A : Dict ,A : Optional[int]=ReturnType.FULL_TEXT ,A : Dict=True ): __A = model_outputs["generated_sequence"][0] __A = model_outputs["input_ids"] __A = model_outputs["prompt_text"] __A = generated_sequence.numpy().tolist() __A = [] for sequence in generated_sequence: if return_type == ReturnType.TENSORS: __A = {"generated_token_ids": sequence} elif return_type in {ReturnType.NEW_TEXT, ReturnType.FULL_TEXT}: # Decode text __A = self.tokenizer.decode( A ,skip_special_tokens=A ,clean_up_tokenization_spaces=A ,) # Remove PADDING prompt of the sequence if XLNet or Transfo-XL model is used if input_ids is None: __A = 0 else: __A = len( self.tokenizer.decode( input_ids[0] ,skip_special_tokens=A ,clean_up_tokenization_spaces=A ,) ) if return_type == ReturnType.FULL_TEXT: __A = prompt_text + text[prompt_length:] else: __A = text[prompt_length:] __A = {"generated_text": all_text} records.append(A ) return records
15
import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE :List[str] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[str] = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Dict = { 'vocab_file': { 'AI-Sweden/gpt-sw3-126m': 'https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-350m': 'https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-1.6b': 'https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-6.7b': 'https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-20b': 'https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model', } } SCREAMING_SNAKE_CASE :Optional[Any] = { 'AI-Sweden/gpt-sw3-126m': 2048, 'AI-Sweden/gpt-sw3-350m': 2048, 'AI-Sweden/gpt-sw3-1.6b': 2048, 'AI-Sweden/gpt-sw3-6.7b': 2048, 'AI-Sweden/gpt-sw3-20b': 2048, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[int] ,A : Optional[Any] ,A : Optional[int]=False ,A : int=False ,A : Union[str, Any]=False ,A : int=None ,A : Optional[Any]=None ,A : Union[str, Any]=None ,A : Optional[Any]=None ,A : Optional[Dict[str, Any]] = None ,**A : Tuple ,): __A = {} if sp_model_kwargs is None else sp_model_kwargs __A = kwargs.get("name_or_path" ) if name_or_path is None: logger.warning( "name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b," " you are testing the model, this can safely be ignored" ) __A = "None" # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing __A = "<|endoftext|>" if eos_token is None else eos_token __A = "<unk>" if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: __A = unk_token if pad_token is None else pad_token __A = eos_token if bos_token is None else bos_token else: __A = "<pad>" if pad_token is None else pad_token __A = "<s>" if bos_token is None else bos_token super().__init__( do_lower_case=A ,remove_space=A ,keep_accents=A ,bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = do_lower_case __A = remove_space __A = keep_accents __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) # Used for whitespace normalization in input texts # fmt : off __A = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", "„"} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing __A = re.compile( f'''[{''.join(map(A ,list(range(0 ,9 ) ) + list(range(11 ,32 ) ) + list(range(1_27 ,1_60 ) ) + [1_60, 1_73, 82_03] ) )}]''' ) def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Optional[Any] ,A : Union[str, Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def UpperCamelCase_ ( self : List[str] ): return len(self.sp_model ) def UpperCamelCase_ ( self : int ,A : str ): __A = self.non_printing_characters_re.sub("" ,A ) # Normalize whitespaces __A = "".join([char if char not in self.whitespaces else " " for char in text] ) # NFC Unicode normalization __A = unicodedata.normalize("NFC" ,A ) return text def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,**A : Optional[int] ): __A = self.preprocess_text(A ) return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : int ): return self.sp_model.IdToPiece(A ) @staticmethod def UpperCamelCase_ ( A : str ): return out_string def UpperCamelCase_ ( self : str ,A : List[str] ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string def UpperCamelCase_ ( self : str ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : List[str] ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, List[str]] ,A : Union[str, bool] = False ): if isinstance(A ,A ): __A = self.preprocess_text(A ) __A = self.sp_model.encode(A ) else: __A = [self.preprocess_text(A ) for t in text] __A = self.sp_model.encode(A ) if return_tensors is True or return_tensors == "pt": __A = torch.tensor(A ) return token_ids def UpperCamelCase_ ( self : List[Any] ,A : Union[int, List[int]] ): return self.sp_model.decode(A ) def UpperCamelCase_ ( self : List[str] ,A : "Conversation" ): __A = [f'''User: {text}''' if is_user else f'''Bot: {text}''' for is_user, text in conversation.iter_texts()] __A = ( f'''{self.eos_token}{self.bos_token}''' + f'''{self.bos_token}'''.join(A ) + f'''{self.bos_token}Bot:''' ) return self.encode(text=A )
15
1
from __future__ import annotations from collections import deque class UpperCAmelCase : '''simple docstring''' def __init__( self : Optional[Any] ,A : list[str] ): __A = [] self.adlist.append( {"value": "", "next_states": [], "fail_state": 0, "output": []} ) for keyword in keywords: self.add_keyword(A ) self.set_fail_transitions() def UpperCamelCase_ ( self : Dict ,A : int ,A : str ): for state in self.adlist[current_state]["next_states"]: if char == self.adlist[state]["value"]: return state return None def UpperCamelCase_ ( self : str ,A : str ): __A = 0 for character in keyword: __A = self.find_next_state(A ,A ) if next_state is None: self.adlist.append( { "value": character, "next_states": [], "fail_state": 0, "output": [], } ) self.adlist[current_state]["next_states"].append(len(self.adlist ) - 1 ) __A = len(self.adlist ) - 1 else: __A = next_state self.adlist[current_state]["output"].append(A ) def UpperCamelCase_ ( self : str ): __A = deque() for node in self.adlist[0]["next_states"]: q.append(A ) __A = 0 while q: __A = q.popleft() for child in self.adlist[r]["next_states"]: q.append(A ) __A = self.adlist[r]["fail_state"] while ( self.find_next_state(A ,self.adlist[child]["value"] ) is None and state != 0 ): __A = self.adlist[state]["fail_state"] __A = self.find_next_state( A ,self.adlist[child]["value"] ) if self.adlist[child]["fail_state"] is None: __A = 0 __A = ( self.adlist[child]["output"] + self.adlist[self.adlist[child]["fail_state"]]["output"] ) def UpperCamelCase_ ( self : Optional[int] ,A : str ): __A = {} # returns a dict with keywords and list of its occurrences __A = 0 for i in range(len(A ) ): while ( self.find_next_state(A ,string[i] ) is None and current_state != 0 ): __A = self.adlist[current_state]["fail_state"] __A = self.find_next_state(A ,string[i] ) if next_state is None: __A = 0 else: __A = next_state for key in self.adlist[current_state]["output"]: if key not in result: __A = [] result[key].append(i - len(A ) + 1 ) return result if __name__ == "__main__": import doctest doctest.testmod()
15
import numpy as np def UpperCAmelCase ( a_ , a_ , a_ = 1E-12 , a_ = 1_0_0 , ) -> tuple[float, np.ndarray]: """simple docstring""" assert np.shape(a_ )[0] == np.shape(a_ )[1] # Ensure proper dimensionality. assert np.shape(a_ )[0] == np.shape(a_ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(a_ ) == np.iscomplexobj(a_ ) __A = np.iscomplexobj(a_ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(a_ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. __A = False __A = 0 __A = 0 __A = 1E12 while not convergence: # Multiple matrix by the vector. __A = np.dot(a_ , a_ ) # Normalize the resulting output vector. __A = w / np.linalg.norm(a_ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) __A = vector.conj().T if is_complex else vector.T __A = np.dot(a_ , np.dot(a_ , a_ ) ) # Check convergence. __A = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: __A = True __A = lambda_ if is_complex: __A = np.real(lambda_ ) return lambda_, vector def UpperCAmelCase ( ) -> None: """simple docstring""" __A = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) __A = np.array([4_1, 4, 2_0] ) __A = real_input_matrix.astype(np.complexaaa ) __A = np.triu(1J * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T __A = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": __A = real_input_matrix __A = real_vector elif problem_type == "complex": __A = complex_input_matrix __A = complex_vector # Our implementation. __A , __A = power_iteration(a_ , a_ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). __A , __A = np.linalg.eigh(a_ ) # Last eigenvalue is the maximum one. __A = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. __A = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1E-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(a_ ) - np.abs(a_ ) ) <= 1E-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
15
1
from ..utils import DummyObject, requires_backends class UpperCAmelCase ( metaclass=__SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = ["torch", "scipy"] def __init__( self : List[Any] ,*A : Any ,**A : int ): requires_backends(self ,["torch", "scipy"] ) @classmethod def UpperCamelCase_ ( cls : str ,*A : List[Any] ,**A : Tuple ): requires_backends(cls ,["torch", "scipy"] ) @classmethod def UpperCamelCase_ ( cls : str ,*A : Optional[int] ,**A : Tuple ): requires_backends(cls ,["torch", "scipy"] )
15
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig SCREAMING_SNAKE_CASE :str = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE :str = 'RegNetConfig' # Base docstring SCREAMING_SNAKE_CASE :List[str] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Union[str, Any] = [1, 1088, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE :Optional[int] = 'facebook/regnet-y-040' SCREAMING_SNAKE_CASE :Any = 'tabby, tabby cat' SCREAMING_SNAKE_CASE :Optional[int] = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : int ,A : int = 3 ,A : int = 1 ,A : int = 1 ,A : Optional[str] = "relu" ,**A : Dict ,): super().__init__(**A ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb __A = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=A ,strides=A ,padding="VALID" ,groups=A ,use_bias=A ,name="convolution" ,) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) __A = ACTaFN[activation] if activation is not None else tf.identity def UpperCamelCase_ ( self : List[Any] ,A : Any ): __A = self.convolution(self.padding(A ) ) __A = self.normalization(A ) __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ,A : RegNetConfig ,**A : str ): super().__init__(**A ) __A = config.num_channels __A = TFRegNetConvLayer( out_channels=config.embedding_size ,kernel_size=3 ,stride=2 ,activation=config.hidden_act ,name="embedder" ,) def UpperCamelCase_ ( self : Tuple ,A : Optional[Any] ): __A = shape_list(A )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( "Make sure that the channel dimension of the pixel values match with the one set in the configuration." ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) __A = tf.transpose(A ,perm=(0, 2, 3, 1) ) __A = self.embedder(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Optional[int] ,A : int ,A : int = 2 ,**A : Tuple ): super().__init__(**A ) __A = tf.keras.layers.ConvaD( filters=A ,kernel_size=1 ,strides=A ,use_bias=A ,name="convolution" ) __A = tf.keras.layers.BatchNormalization(epsilon=1E-5 ,momentum=0.9 ,name="normalization" ) def UpperCamelCase_ ( self : Union[str, Any] ,A : tf.Tensor ,A : bool = False ): return self.normalization(self.convolution(A ) ,training=A ) class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Dict ,A : int ,A : int ,**A : str ): super().__init__(**A ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) __A = [ tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="relu" ,name="attention.0" ), tf.keras.layers.ConvaD(filters=A ,kernel_size=1 ,activation="sigmoid" ,name="attention.2" ), ] def UpperCamelCase_ ( self : Dict ,A : List[Any] ): # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] __A = self.pooler(A ) for layer_module in self.attention: __A = layer_module(A ) __A = hidden_state * pooled return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : Optional[int] ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.2" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : int ,A : Optional[int] ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,A : int ,A : int ,A : int = 1 ,**A : str ): super().__init__(**A ) __A = in_channels != out_channels or stride != 1 __A = max(1 ,out_channels // config.groups_width ) __A = ( TFRegNetShortCut(A ,stride=A ,name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" ,name="shortcut" ) ) __A = [ TFRegNetConvLayer(A ,kernel_size=1 ,activation=config.hidden_act ,name="layer.0" ), TFRegNetConvLayer( A ,stride=A ,groups=A ,activation=config.hidden_act ,name="layer.1" ), TFRegNetSELayer(A ,reduced_channels=int(round(in_channels / 4 ) ) ,name="layer.2" ), TFRegNetConvLayer(A ,kernel_size=1 ,activation=A ,name="layer.3" ), ] __A = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : Dict ,A : Any ): __A = hidden_state for layer_module in self.layers: __A = layer_module(A ) __A = self.shortcut(A ) hidden_state += residual __A = self.activation(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] ,A : RegNetConfig ,A : int ,A : int ,A : int = 2 ,A : int = 2 ,**A : Optional[int] ): super().__init__(**A ) __A = TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer __A = [ # downsampling is done in the first layer with stride of 2 layer(A ,A ,A ,stride=A ,name="layers.0" ), *[layer(A ,A ,A ,name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCamelCase_ ( self : Any ,A : List[str] ): for layer_module in self.layers: __A = layer_module(A ) return hidden_state class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Any ,A : RegNetConfig ,**A : List[str] ): super().__init__(**A ) __A = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A ,config.embedding_size ,config.hidden_sizes[0] ,stride=2 if config.downsample_in_first_stage else 1 ,depth=config.depths[0] ,name="stages.0" ,) ) __A = zip(config.hidden_sizes ,config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A ,config.depths[1:] ) ): self.stages.append(TFRegNetStage(A ,A ,A ,depth=A ,name=f'''stages.{i+1}''' ) ) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor ,A : bool = False ,A : bool = True ): __A = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: __A = hidden_states + (hidden_state,) __A = stage_module(A ) if output_hidden_states: __A = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A ,hidden_states=A ) @keras_serializable class UpperCAmelCase ( tf.keras.layers.Layer ): '''simple docstring''' snake_case_ = RegNetConfig def __init__( self : int ,A : Optional[int] ,**A : Dict ): super().__init__(**A ) __A = config __A = TFRegNetEmbeddings(A ,name="embedder" ) __A = TFRegNetEncoder(A ,name="encoder" ) __A = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A ,name="pooler" ) @unpack_inputs def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : bool = False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.embedder(A ,training=A ) __A = self.encoder( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = encoder_outputs[0] __A = self.pooler(A ) # Change to NCHW output format have uniformity in the modules __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) __A = tf.transpose(A ,perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: __A = tuple([tf.transpose(A ,perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A ,pooler_output=A ,hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states ,) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = RegNetConfig snake_case_ = "regnet" snake_case_ = "pixel_values" @property def UpperCamelCase_ ( self : Optional[Any] ): return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24) ,dtype=tf.floataa )} SCREAMING_SNAKE_CASE :Dict = R'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE :Dict = R'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare RegNet model outputting raw features without any specific head on top." , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] ,A : RegNetConfig ,*A : List[Any] ,**A : str ): super().__init__(A ,*A ,**A ) __A = TFRegNetMainLayer(A ,name="regnet" ) @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC ,output_type=A ,config_class=_CONFIG_FOR_DOC ,modality="vision" ,expected_output=_EXPECTED_OUTPUT_SHAPE ,) def UpperCamelCase_ ( self : Tuple ,A : tf.Tensor ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : int=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( pixel_values=A ,output_hidden_states=A ,return_dict=A ,training=A ,) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state ,pooler_output=outputs.pooler_output ,hidden_states=outputs.hidden_states ,) @add_start_docstrings( "\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , __SCREAMING_SNAKE_CASE , ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[int] ,A : RegNetConfig ,*A : str ,**A : Tuple ): super().__init__(A ,*A ,**A ) __A = config.num_labels __A = TFRegNetMainLayer(A ,name="regnet" ) # classification head __A = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels ,name="classifier.1" ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT ,output_type=A ,config_class=_CONFIG_FOR_DOC ,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,) def UpperCamelCase_ ( self : List[str] ,A : tf.Tensor = None ,A : tf.Tensor = None ,A : bool = None ,A : bool = None ,A : Union[str, Any]=False ,): __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet( A ,output_hidden_states=A ,return_dict=A ,training=A ) __A = outputs.pooler_output if return_dict else outputs[1] __A = self.classifier[0](A ) __A = self.classifier[1](A ) __A = None if labels is None else self.hf_compute_loss(labels=A ,logits=A ) if not return_dict: __A = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A ,logits=A ,hidden_states=outputs.hidden_states )
15
1
SCREAMING_SNAKE_CASE :int = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} SCREAMING_SNAKE_CASE :Optional[Any] = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def UpperCAmelCase ( a_ , a_ , a_ ) -> list[int]: """simple docstring""" __A = True __A = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(a_ , a_ , a_ ) order.append(a_ ) return order def UpperCAmelCase ( a_ , a_ , a_ ) -> list[int]: """simple docstring""" __A = True __A = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(a_ , a_ , a_ ) return component def UpperCAmelCase ( a_ ) -> list[list[int]]: """simple docstring""" __A = len(a_ ) * [False] __A = {vert: [] for vert in range(len(a_ ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(a_ ) __A = [] for i, was_visited in enumerate(a_ ): if not was_visited: order += topology_sort(a_ , a_ , a_ ) __A = [] __A = len(a_ ) * [False] for i in range(len(a_ ) ): __A = order[len(a_ ) - i - 1] if not visited[vert]: __A = find_components(a_ , a_ , a_ ) components_list.append(a_ ) return components_list
15
import math def UpperCAmelCase ( a_ , a_ = 0 , a_ = 0 ) -> list: """simple docstring""" __A = end or len(a_ ) for i in range(a_ , a_ ): __A = i __A = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __A = array[temp_index - 1] temp_index -= 1 __A = temp_index_value return array def UpperCAmelCase ( a_ , a_ , a_ ) -> None: # Max Heap """simple docstring""" __A = index __A = 2 * index + 1 # Left Node __A = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __A = left_index if right_index < heap_size and array[largest] < array[right_index]: __A = right_index if largest != index: __A , __A = array[largest], array[index] heapify(a_ , a_ , a_ ) def UpperCAmelCase ( a_ ) -> list: """simple docstring""" __A = len(a_ ) for i in range(n // 2 , -1 , -1 ): heapify(a_ , a_ , a_ ) for i in range(n - 1 , 0 , -1 ): __A , __A = array[0], array[i] heapify(a_ , 0 , a_ ) return array def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" __A = low __A = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __A , __A = array[j], array[i] i += 1 def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) == 0: return array __A = 2 * math.ceil(math.loga(len(a_ ) ) ) __A = 1_6 return intro_sort(a_ , 0 , len(a_ ) , a_ , a_ ) def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ ) -> list: """simple docstring""" while end - start > size_threshold: if max_depth == 0: return heap_sort(a_ ) max_depth -= 1 __A = median_of_a(a_ , a_ , start + ((end - start) // 2) + 1 , end - 1 ) __A = partition(a_ , a_ , a_ , a_ ) intro_sort(a_ , a_ , a_ , a_ , a_ ) __A = p return insertion_sort(a_ , a_ , a_ ) if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma : ').strip() SCREAMING_SNAKE_CASE :str = [float(item) for item in user_input.split(',')] print(sort(unsorted))
15
1
from __future__ import annotations from fractions import Fraction def UpperCAmelCase ( a_ , a_ ) -> bool: """simple docstring""" return ( num != den and num % 1_0 == den // 1_0 and (num // 1_0) / (den % 1_0) == num / den ) def UpperCAmelCase ( a_ ) -> list[str]: """simple docstring""" __A = [] __A = 1_1 __A = int("1" + "0" * digit_len ) for num in range(a_ , a_ ): while den <= 9_9: if (num != den) and (num % 1_0 == den // 1_0) and (den % 1_0 != 0): if is_digit_cancelling(a_ , a_ ): solutions.append(F'''{num}/{den}''' ) den += 1 num += 1 __A = 1_0 return solutions def UpperCAmelCase ( a_ = 2 ) -> int: """simple docstring""" __A = 1.0 for fraction in fraction_list(a_ ): __A = Fraction(a_ ) result *= frac.denominator / frac.numerator return int(a_ ) if __name__ == "__main__": print(solution())
15
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE :Optional[int] = NewType('DataClass', Any) SCREAMING_SNAKE_CASE :int = NewType('DataClassType', Any) def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" if isinstance(a_ , a_ ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( F'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def UpperCAmelCase ( a_ ) -> Callable[[str], Any]: """simple docstring""" __A = {str(a_ ): choice for choice in choices} return lambda a_ : str_to_choice.get(a_ , a_ ) def UpperCAmelCase ( *, a_ = None , a_ = None , a_ = dataclasses.MISSING , a_ = dataclasses.MISSING , a_ = None , **a_ , ) -> dataclasses.Field: """simple docstring""" if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls __A = {} if aliases is not None: __A = aliases if help is not None: __A = help return dataclasses.field(metadata=a_ , default=a_ , default_factory=a_ , **a_ ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 def __init__( self : Union[str, Any] ,A : Union[DataClassType, Iterable[DataClassType]] ,**A : List[Any] ): # To make the default appear when using --help if "formatter_class" not in kwargs: __A = ArgumentDefaultsHelpFormatter super().__init__(**A ) if dataclasses.is_dataclass(A ): __A = [dataclass_types] __A = list(A ) for dtype in self.dataclass_types: self._add_dataclass_arguments(A ) @staticmethod def UpperCamelCase_ ( A : ArgumentParser ,A : dataclasses.Field ): __A = f'''--{field.name}''' __A = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type ,A ): raise RuntimeError( "Unresolved type detected, which should have been done with the help of " "`typing.get_type_hints` method by default" ) __A = kwargs.pop("aliases" ,[] ) if isinstance(A ,A ): __A = [aliases] __A = getattr(field.type ,"__origin__" ,field.type ) if origin_type is Union or (hasattr(A ,"UnionType" ) and isinstance(A ,types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(A ) not in field.type.__args__ ): raise ValueError( "Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because" " the argument parser only supports one type per argument." f''' Problem encountered in field \'{field.name}\'.''' ) if type(A ) not in field.type.__args__: # filter `str` in Union __A = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] __A = getattr(field.type ,"__origin__" ,field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) __A = ( field.type.__args__[0] if isinstance(A ,field.type.__args__[1] ) else field.type.__args__[1] ) __A = getattr(field.type ,"__origin__" ,field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) __A = {} if origin_type is Literal or (isinstance(field.type ,A ) and issubclass(field.type ,A )): if origin_type is Literal: __A = field.type.__args__ else: __A = [x.value for x in field.type] __A = make_choice_type_function(kwargs["choices"] ) if field.default is not dataclasses.MISSING: __A = field.default else: __A = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument __A = copy(A ) # Hack because type=bool in argparse does not behave as we want. __A = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. __A = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way __A = default # This tells argparse we accept 0 or 1 value after --field_name __A = "?" # This is the value that will get picked if we do --field_name (without value) __A = True elif isclass(A ) and issubclass(A ,A ): __A = field.type.__args__[0] __A = "+" if field.default_factory is not dataclasses.MISSING: __A = field.default_factory() elif field.default is dataclasses.MISSING: __A = True else: __A = field.type if field.default is not dataclasses.MISSING: __A = field.default elif field.default_factory is not dataclasses.MISSING: __A = field.default_factory() else: __A = True parser.add_argument(A ,*A ,**A ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): __A = False parser.add_argument(f'''--no_{field.name}''' ,action="store_false" ,dest=field.name ,**A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : DataClassType ): if hasattr(A ,"_argument_group_name" ): __A = self.add_argument_group(dtype._argument_group_name ) else: __A = self try: __A = get_type_hints(A ) except NameError: raise RuntimeError( f'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' "removing line of `from __future__ import annotations` which opts in Postponed " "Evaluation of Annotations (PEP 563)" ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(A ): __A = ".".join(map(A ,sys.version_info[:3] ) ) raise RuntimeError( f'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' "line of `from __future__ import annotations` which opts in union types as " "`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To " "support Python versions that lower than 3.10, you need to use " "`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of " "`X | None`." ) from ex raise for field in dataclasses.fields(A ): if not field.init: continue __A = type_hints[field.name] self._parse_dataclass_field(A ,A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : List[Any]=None ,A : List[Any]=False ,A : Optional[Any]=True ,A : Union[str, Any]=None ,A : Union[str, Any]=None ,): if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): __A = [] if args_filename: args_files.append(Path(A ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix(".args" ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values __A = ArgumentParser() args_file_parser.add_argument(A ,type=A ,action="append" ) # Use only remaining args for further parsing (remove the args_file_flag) __A , __A = args_file_parser.parse_known_args(args=A ) __A = vars(A ).get(args_file_flag.lstrip("-" ) ,A ) if cmd_args_file_paths: args_files.extend([Path(A ) for p in cmd_args_file_paths] ) __A = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last __A = file_args + args if args is not None else file_args + sys.argv[1:] __A , __A = self.parse_known_args(args=A ) __A = [] for dtype in self.dataclass_types: __A = {f.name for f in dataclasses.fields(A ) if f.init} __A = {k: v for k, v in vars(A ).items() if k in keys} for k in keys: delattr(A ,A ) __A = dtype(**A ) outputs.append(A ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(A ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(f'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def UpperCamelCase_ ( self : Dict ,A : Dict[str, Any] ,A : bool = False ): __A = set(args.keys() ) __A = [] for dtype in self.dataclass_types: __A = {f.name for f in dataclasses.fields(A ) if f.init} __A = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) __A = dtype(**A ) outputs.append(A ) if not allow_extra_keys and unused_keys: raise ValueError(f'''Some keys are not used by the HfArgumentParser: {sorted(A )}''' ) return tuple(A ) def UpperCamelCase_ ( self : List[str] ,A : str ,A : bool = False ): with open(Path(A ) ,encoding="utf-8" ) as open_json_file: __A = json.loads(open_json_file.read() ) __A = self.parse_dict(A ,allow_extra_keys=A ) return tuple(A ) def UpperCamelCase_ ( self : int ,A : str ,A : bool = False ): __A = self.parse_dict(yaml.safe_load(Path(A ).read_text() ) ,allow_extra_keys=A ) return tuple(A )
15
1
import os import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers.models.realm.configuration_realm import RealmConfig from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def UpperCamelCase_ ( self : List[Any] ): __A = tempfile.mkdtemp() __A = 5 # Realm tok __A = [ "[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "test", "question", "this", "is", "the", "first", "second", "third", "fourth", "fifth", "record", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] __A = os.path.join(self.tmpdirname ,"realm_tokenizer" ) os.makedirs(A ,exist_ok=A ) __A = os.path.join(A ,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] ) ) __A = os.path.join(self.tmpdirname ,"realm_block_records" ) os.makedirs(A ,exist_ok=A ) def UpperCamelCase_ ( self : int ): return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname ,"realm_tokenizer" ) ) def UpperCamelCase_ ( self : Optional[Any] ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase_ ( self : int ): __A = RealmConfig(num_block_records=self.num_block_records ) return config def UpperCamelCase_ ( self : Union[str, Any] ): __A = Dataset.from_dict( { "id": ["0", "1"], "question": ["foo", "bar"], "answers": [["Foo", "Bar"], ["Bar"]], } ) return dataset def UpperCamelCase_ ( self : List[Any] ): __A = np.array( [ B"This is the first record", B"This is the second record", B"This is the third record", B"This is the fourth record", B"This is the fifth record", B"This is a longer longer longer record", ] ,dtype=A ,) return block_records def UpperCamelCase_ ( self : Tuple ): __A = RealmRetriever( block_records=self.get_dummy_block_records() ,tokenizer=self.get_tokenizer() ,) return retriever def UpperCamelCase_ ( self : Optional[int] ): __A = self.get_config() __A = self.get_dummy_retriever() __A = retriever.tokenizer __A = np.array([0, 3] ,dtype="long" ) __A = tokenizer(["Test question"] ).input_ids __A = tokenizer( ["the fourth"] ,add_special_tokens=A ,return_token_type_ids=A ,return_attention_mask=A ,).input_ids __A = config.reader_seq_len __A , __A , __A , __A = retriever( A ,A ,answer_ids=A ,max_length=A ,return_tensors="np" ) self.assertEqual(len(A ) ,2 ) self.assertEqual(len(A ) ,2 ) self.assertEqual(len(A ) ,2 ) self.assertEqual(concat_inputs.input_ids.shape ,(2, 10) ) self.assertEqual(concat_inputs.attention_mask.shape ,(2, 10) ) self.assertEqual(concat_inputs.token_type_ids.shape ,(2, 10) ) self.assertEqual(concat_inputs.special_tokens_mask.shape ,(2, 10) ) self.assertEqual( tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0] ) ,["[CLS]", "test", "question", "[SEP]", "this", "is", "the", "first", "record", "[SEP]"] ,) self.assertEqual( tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1] ) ,["[CLS]", "test", "question", "[SEP]", "this", "is", "the", "fourth", "record", "[SEP]"] ,) def UpperCamelCase_ ( self : Dict ): __A = self.get_config() __A = self.get_dummy_retriever() __A = retriever.tokenizer __A = np.array([0, 3, 5] ,dtype="long" ) __A = tokenizer(["Test question"] ).input_ids __A = tokenizer( ["the fourth", "longer longer"] ,add_special_tokens=A ,return_token_type_ids=A ,return_attention_mask=A ,).input_ids __A = config.reader_seq_len __A , __A , __A , __A = retriever( A ,A ,answer_ids=A ,max_length=A ,return_tensors="np" ) self.assertEqual([False, True, True] ,A ) self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]] ,A ) self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]] ,A ) def UpperCamelCase_ ( self : Tuple ): __A = self.get_dummy_retriever() retriever.save_pretrained(os.path.join(self.tmpdirname ,"realm_block_records" ) ) # Test local path __A = retriever.from_pretrained(os.path.join(self.tmpdirname ,"realm_block_records" ) ) self.assertEqual(retriever.block_records[0] ,B"This is the first record" ) # Test mocked remote path with patch("transformers.models.realm.retrieval_realm.hf_hub_download" ) as mock_hf_hub_download: __A = os.path.join( os.path.join(self.tmpdirname ,"realm_block_records" ) ,_REALM_BLOCK_RECORDS_FILENAME ) __A = RealmRetriever.from_pretrained("google/realm-cc-news-pretrained-openqa" ) self.assertEqual(retriever.block_records[0] ,B"This is the first record" )
15
SCREAMING_SNAKE_CASE :Any = 256 # Modulus to hash a string SCREAMING_SNAKE_CASE :Union[str, Any] = 100_0003 def UpperCAmelCase ( a_ , a_ ) -> bool: """simple docstring""" __A = len(a_ ) __A = len(a_ ) if p_len > t_len: return False __A = 0 __A = 0 __A = 1 # Calculating the hash of pattern and substring of text for i in range(a_ ): __A = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus __A = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue __A = (modulus_power * alphabet_size) % modulus for i in range(0 , t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash __A = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def UpperCAmelCase ( ) -> None: """simple docstring""" __A = "abc1abc12" __A = "alskfjaldsabc1abc1abc12k23adsfabcabc" __A = "alskfjaldsk23adsfabcabc" assert rabin_karp(a_ , a_ ) and not rabin_karp(a_ , a_ ) # Test 2) __A = "ABABX" __A = "ABABZABABYABABX" assert rabin_karp(a_ , a_ ) # Test 3) __A = "AAAB" __A = "ABAAAAAB" assert rabin_karp(a_ , a_ ) # Test 4) __A = "abcdabcy" __A = "abcxabcdabxabcdabcdabcy" assert rabin_karp(a_ , a_ ) # Test 5) __A = "Lü" __A = "Lüsai" assert rabin_karp(a_ , a_ ) __A = "Lue" assert not rabin_karp(a_ , a_ ) print("Success." ) if __name__ == "__main__": test_rabin_karp()
15
1
import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" __A = SwinConfig(image_size=1_9_2 ) if "base" in model_name: __A = 6 __A = 1_2_8 __A = (2, 2, 1_8, 2) __A = (4, 8, 1_6, 3_2) elif "large" in model_name: __A = 1_2 __A = 1_9_2 __A = (2, 2, 1_8, 2) __A = (6, 1_2, 2_4, 4_8) else: raise ValueError("Model not supported, only supports base and large variants" ) __A = window_size __A = embed_dim __A = depths __A = num_heads return config def UpperCAmelCase ( a_ ) -> int: """simple docstring""" if "encoder.mask_token" in name: __A = name.replace("encoder.mask_token" , "embeddings.mask_token" ) if "encoder.patch_embed.proj" in name: __A = name.replace("encoder.patch_embed.proj" , "embeddings.patch_embeddings.projection" ) if "encoder.patch_embed.norm" in name: __A = name.replace("encoder.patch_embed.norm" , "embeddings.norm" ) if "attn.proj" in name: __A = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name: __A = name.replace("attn" , "attention.self" ) if "norm1" in name: __A = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: __A = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: __A = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __A = name.replace("mlp.fc2" , "output.dense" ) if name == "encoder.norm.weight": __A = "layernorm.weight" if name == "encoder.norm.bias": __A = "layernorm.bias" if "decoder" in name: pass else: __A = "swin." + name return name def UpperCAmelCase ( a_ , a_ ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): __A = orig_state_dict.pop(a_ ) if "attn_mask" in key: pass elif "qkv" in key: __A = key.split("." ) __A = int(key_split[2] ) __A = int(key_split[4] ) __A = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: __A = val[:dim, :] __A = val[ dim : dim * 2, : ] __A = val[-dim:, :] else: __A = val[ :dim ] __A = val[ dim : dim * 2 ] __A = val[ -dim: ] else: __A = val return orig_state_dict def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" __A = torch.load(a_ , map_location="cpu" )["model"] __A = get_swin_config(a_ ) __A = SwinForMaskedImageModeling(a_ ) model.eval() __A = convert_state_dict(a_ , a_ ) model.load_state_dict(a_ ) __A = "http://images.cocodataset.org/val2017/000000039769.jpg" __A = ViTImageProcessor(size={"height": 1_9_2, "width": 1_9_2} ) __A = Image.open(requests.get(a_ , stream=a_ ).raw ) __A = image_processor(images=a_ , return_tensors="pt" ) with torch.no_grad(): __A = model(**a_ ).logits print(outputs.keys() ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(a_ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(a_ ) if push_to_hub: print(F'''Pushing model and image processor for {model_name} to hub''' ) model.push_to_hub(F'''microsoft/{model_name}''' ) image_processor.push_to_hub(F'''microsoft/{model_name}''' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :str = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='swin-base-simmim-window6-192', type=str, choices=['swin-base-simmim-window6-192', 'swin-large-simmim-window12-192'], help='Name of the Swin SimMIM model you\'d like to convert.', ) parser.add_argument( '--checkpoint_path', default='/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth', type=str, help='Path to the original PyTorch checkpoint (.pth file).', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) SCREAMING_SNAKE_CASE :Union[str, Any] = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
15
import argparse import json import os import torch from transformers.file_utils import has_file from diffusers import UNetaDConditionModel, UNetaDModel SCREAMING_SNAKE_CASE :Union[str, Any] = False SCREAMING_SNAKE_CASE :Any = True SCREAMING_SNAKE_CASE :Tuple = False if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser() parser.add_argument( '--repo_path', default=None, type=str, required=True, help='The config json file corresponding to the architecture.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') SCREAMING_SNAKE_CASE :Union[str, Any] = parser.parse_args() SCREAMING_SNAKE_CASE :Dict = { 'image_size': 'sample_size', 'num_res_blocks': 'layers_per_block', 'block_channels': 'block_out_channels', 'down_blocks': 'down_block_types', 'up_blocks': 'up_block_types', 'downscale_freq_shift': 'freq_shift', 'resnet_num_groups': 'norm_num_groups', 'resnet_act_fn': 'act_fn', 'resnet_eps': 'norm_eps', 'num_head_channels': 'attention_head_dim', } SCREAMING_SNAKE_CASE :Optional[int] = { 'time_steps': 'time_proj', 'mid': 'mid_block', 'downsample_blocks': 'down_blocks', 'upsample_blocks': 'up_blocks', } SCREAMING_SNAKE_CASE :int = '' if has_file(args.repo_path, 'config.json') else 'unet' with open(os.path.join(args.repo_path, subfolder, 'config.json'), 'r', encoding='utf-8') as reader: SCREAMING_SNAKE_CASE :Dict = reader.read() SCREAMING_SNAKE_CASE :List[str] = json.loads(text) if do_only_config: for key in config_parameters_to_change.keys(): config.pop(key, None) if has_file(args.repo_path, 'config.json'): SCREAMING_SNAKE_CASE :Optional[int] = UNetaDModel(**config) else: SCREAMING_SNAKE_CASE :Optional[Any] = UNetaDConditionModel if 'ldm-text2im-large-256' in args.repo_path else UNetaDModel SCREAMING_SNAKE_CASE :List[str] = class_name(**config) if do_only_config: model.save_config(os.path.join(args.repo_path, subfolder)) SCREAMING_SNAKE_CASE :List[str] = dict(model.config) if do_only_renaming: for key, value in config_parameters_to_change.items(): if key in config: SCREAMING_SNAKE_CASE :Optional[Any] = config[key] del config[key] SCREAMING_SNAKE_CASE :Optional[Any] = [k.replace('UNetRes', '') for k in config['down_block_types']] SCREAMING_SNAKE_CASE :List[Any] = [k.replace('UNetRes', '') for k in config['up_block_types']] if do_only_weights: SCREAMING_SNAKE_CASE :Tuple = torch.load(os.path.join(args.repo_path, subfolder, 'diffusion_pytorch_model.bin')) SCREAMING_SNAKE_CASE :Any = {} for param_key, param_value in state_dict.items(): if param_key.endswith('.op.bias') or param_key.endswith('.op.weight'): continue SCREAMING_SNAKE_CASE :List[str] = False for key, new_key in key_parameters_to_change.items(): if not has_changed and param_key.split('.')[0] == key: SCREAMING_SNAKE_CASE :List[Any] = param_value SCREAMING_SNAKE_CASE :str = True if not has_changed: SCREAMING_SNAKE_CASE :List[str] = param_value model.load_state_dict(new_state_dict) model.save_pretrained(os.path.join(args.repo_path, subfolder))
15
1
import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) @dataclass class UpperCAmelCase : '''simple docstring''' snake_case_ = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) snake_case_ = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) snake_case_ = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def UpperCamelCase_ ( self : Union[str, Any] ): __A = self.task_name.lower() class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "train" snake_case_ = "dev" snake_case_ = "test" class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 snake_case_ = 42 snake_case_ = 42 def __init__( self : Union[str, Any] ,A : GlueDataTrainingArguments ,A : PreTrainedTokenizerBase ,A : Optional[int] = None ,A : Union[str, Split] = Split.train ,A : Optional[str] = None ,): warnings.warn( "This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets " "library. You can have a look at this example script for pointers: " "https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py" ,A ,) __A = args __A = glue_processors[args.task_name]() __A = glue_output_modes[args.task_name] if isinstance(A ,A ): try: __A = Split[mode] except KeyError: raise KeyError("mode is not a valid split name" ) # Load data features from cache or dataset file __A = os.path.join( cache_dir if cache_dir is not None else args.data_dir ,f'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}''' ,) __A = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) __A , __A = label_list[2], label_list[1] __A = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. __A = cached_features_file + ".lock" with FileLock(A ): if os.path.exists(A ) and not args.overwrite_cache: __A = time.time() __A = torch.load(A ) logger.info( f'''Loading features from cached file {cached_features_file} [took %.3f s]''' ,time.time() - start ) else: logger.info(f'''Creating features from dataset file at {args.data_dir}''' ) if mode == Split.dev: __A = self.processor.get_dev_examples(args.data_dir ) elif mode == Split.test: __A = self.processor.get_test_examples(args.data_dir ) else: __A = self.processor.get_train_examples(args.data_dir ) if limit_length is not None: __A = examples[:limit_length] __A = glue_convert_examples_to_features( A ,A ,max_length=args.max_seq_length ,label_list=A ,output_mode=self.output_mode ,) __A = time.time() torch.save(self.features ,A ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__( self : int ): return len(self.features ) def __getitem__( self : Tuple ,A : List[Any] ): return self.features[i] def UpperCamelCase_ ( self : List[Any] ): return self.label_list
15
import argparse import math import traceback import dateutil.parser as date_parser import requests def UpperCAmelCase ( a_ ) -> str: """simple docstring""" __A = {} __A = job["started_at"] __A = job["completed_at"] __A = date_parser.parse(a_ ) __A = date_parser.parse(a_ ) __A = round((end_datetime - start_datetime).total_seconds() / 60.0 ) __A = start __A = end __A = duration_in_min return job_info def UpperCAmelCase ( a_ , a_=None ) -> str: """simple docstring""" __A = None if token is not None: __A = {"Accept": "application/vnd.github+json", "Authorization": F'''Bearer {token}'''} __A = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' __A = requests.get(a_ , headers=a_ ).json() __A = {} try: job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) __A = math.ceil((result["total_count"] - 1_0_0) / 1_0_0 ) for i in range(a_ ): __A = requests.get(url + F'''&page={i + 2}''' , headers=a_ ).json() job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) return job_time except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": SCREAMING_SNAKE_CASE :Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() SCREAMING_SNAKE_CASE :Union[str, Any] = get_job_time(args.workflow_run_id) SCREAMING_SNAKE_CASE :Optional[int] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f'''{k}: {v["duration"]}''')
15
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE :int = {'configuration_wavlm': ['WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WavLMConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :Dict = [ 'WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'WavLMForAudioFrameClassification', 'WavLMForCTC', 'WavLMForSequenceClassification', 'WavLMForXVector', 'WavLMModel', 'WavLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE :str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
15
import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" __A = args.pruning_method __A = args.threshold __A = args.model_name_or_path.rstrip("/" ) __A = args.target_model_path print(F'''Load fine-pruned model from {model_name_or_path}''' ) __A = torch.load(os.path.join(a_ , "pytorch_model.bin" ) ) __A = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: __A = tensor print(F'''Copied layer {name}''' ) elif "classifier" in name or "qa_output" in name: __A = tensor print(F'''Copied layer {name}''' ) elif "bias" in name: __A = tensor print(F'''Copied layer {name}''' ) else: if pruning_method == "magnitude": __A = MagnitudeBinarizer.apply(inputs=a_ , threshold=a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "topK": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A = TopKBinarizer.apply(a_ , a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A = ThresholdBinarizer.apply(a_ , a_ , a_ ) __A = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "l0": if "mask_scores" in name: continue __A = name[:-6] __A = model[F'''{prefix_}mask_scores'''] __A , __A = -0.1, 1.1 __A = torch.sigmoid(a_ ) __A = s * (r - l) + l __A = s_bar.clamp(min=0.0 , max=1.0 ) __A = tensor * mask print(F'''Pruned layer {name}''' ) else: raise ValueError("Unknown pruning method" ) if target_model_path is None: __A = os.path.join( os.path.dirname(a_ ) , F'''bertarized_{os.path.basename(a_ )}''' ) if not os.path.isdir(a_ ): shutil.copytree(a_ , a_ ) print(F'''\nCreated folder {target_model_path}''' ) torch.save(a_ , os.path.join(a_ , "pytorch_model.bin" ) ) print("\nPruned model saved! See you later!" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser() parser.add_argument( '--pruning_method', choices=['l0', 'magnitude', 'topK', 'sigmoied_threshold'], type=str, required=True, help=( 'Pruning Method (l0 = L0 regularization, magnitude = Magnitude pruning, topK = Movement pruning,' ' sigmoied_threshold = Soft movement pruning)' ), ) parser.add_argument( '--threshold', type=float, required=False, help=( 'For `magnitude` and `topK`, it is the level of remaining weights (in %) in the fine-pruned model.' 'For `sigmoied_threshold`, it is the threshold \tau against which the (sigmoied) scores are compared.' 'Not needed for `l0`' ), ) parser.add_argument( '--model_name_or_path', type=str, required=True, help='Folder containing the model that was previously fine-pruned', ) parser.add_argument( '--target_model_path', default=None, type=str, required=False, help='Folder containing the model that was previously fine-pruned', ) SCREAMING_SNAKE_CASE :str = parser.parse_args() main(args)
15
1
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging SCREAMING_SNAKE_CASE :List[str] = logging.get_logger(__name__) def UpperCAmelCase ( a_=None , a_=None ) -> Any: """simple docstring""" return field(default_factory=lambda: default , metadata=a_ ) @dataclass class UpperCAmelCase : '''simple docstring''' snake_case_ = list_field( default=[] , metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) } , ) snake_case_ = list_field( default=[8] , metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) snake_case_ = list_field( default=[8, 32, 128, 512] , metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"} , ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."} , ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."} , ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) snake_case_ = field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Use FP16 to accelerate inference."} ) snake_case_ = field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Benchmark training of model"} ) snake_case_ = field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Verbose memory tracing"} ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."} , ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" } , ) snake_case_ = field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Trace memory line by line"} ) snake_case_ = field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Save result to a CSV file"} ) snake_case_ = field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Save all print statements in a log file"} ) snake_case_ = field(default=__SCREAMING_SNAKE_CASE , metadata={"help": "Whether to print environment information"} ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) } , ) snake_case_ = field( default=F"""inference_time_{round(time() )}.csv""" , metadata={"help": "CSV filename used if saving time results to csv."} , ) snake_case_ = field( default=F"""inference_memory_{round(time() )}.csv""" , metadata={"help": "CSV filename used if saving memory results to csv."} , ) snake_case_ = field( default=F"""train_time_{round(time() )}.csv""" , metadata={"help": "CSV filename used if saving time results to csv for training."} , ) snake_case_ = field( default=F"""train_memory_{round(time() )}.csv""" , metadata={"help": "CSV filename used if saving memory results to csv for training."} , ) snake_case_ = field( default=F"""env_info_{round(time() )}.csv""" , metadata={"help": "CSV filename used if saving environment information."} , ) snake_case_ = field( default=F"""log_{round(time() )}.csv""" , metadata={"help": "Log filename used if print statements are saved in log."} , ) snake_case_ = field(default=3 , metadata={"help": "Times an experiment will be run."} ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) } , ) def UpperCamelCase_ ( self : List[str] ): warnings.warn( f'''The class {self.__class__} is deprecated. Hugging Face Benchmarking utils''' " are deprecated in general and it is advised to use external Benchmarking libraries " " to benchmark Transformer models." ,A ,) def UpperCamelCase_ ( self : Tuple ): return json.dumps(dataclasses.asdict(self ) ,indent=2 ) @property def UpperCamelCase_ ( self : Union[str, Any] ): if len(self.models ) <= 0: raise ValueError( "Please make sure you provide at least one model name / model identifier, *e.g.* `--models" " bert-base-cased` or `args.models = ['bert-base-cased']." ) return self.models @property def UpperCamelCase_ ( self : List[str] ): if not self.multi_process: return False elif self.is_tpu: logger.info("Multiprocessing is currently not possible on TPU." ) return False else: return True
15
import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :List[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :int = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Union[str, Any] = { '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' ), } } SCREAMING_SNAKE_CASE :int = { 'google/bigbird-roberta-base': 4096, 'google/bigbird-roberta-large': 4096, 'google/bigbird-base-trivia-itc': 4096, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] snake_case_ = [] def __init__( self : Any ,A : List[str] ,A : str="<unk>" ,A : int="<s>" ,A : Union[str, Any]="</s>" ,A : List[str]="<pad>" ,A : int="[SEP]" ,A : Optional[Any]="[MASK]" ,A : Tuple="[CLS]" ,A : Optional[Dict[str, Any]] = None ,**A : Any ,): __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else bos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else eos_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else unk_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else pad_token __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else cls_token __A = 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 __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sep_token=A ,mask_token=A ,cls_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) @property def UpperCamelCase_ ( self : List[str] ): return self.sp_model.get_piece_size() def UpperCamelCase_ ( self : Optional[Any] ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : str ,A : Optional[Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Tuple ): return self.sp_model.piece_to_id(A ) def UpperCamelCase_ ( self : List[Any] ,A : Tuple ): __A = self.sp_model.IdToPiece(A ) return token def UpperCamelCase_ ( self : List[Any] ,A : int ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def UpperCamelCase_ ( self : Tuple ,A : List[int] ,A : bool = False ,A : bool = None ,A : bool = True ,**A : Union[str, Any] ,): __A = kwargs.pop("use_source_tokenizer" ,A ) __A = self.convert_ids_to_tokens(A ,skip_special_tokens=A ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __A = [] __A = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) __A = [] sub_texts.append(A ) else: current_sub_text.append(A ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(A ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: __A = re.sub(R" (\[(MASK|SEP)\])" ,R"\1" ," ".join(A ) ) else: __A = "".join(A ) __A = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __A = self.clean_up_tokenization(A ) return clean_text else: return text def UpperCamelCase_ ( self : str ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def UpperCamelCase_ ( self : Optional[int] ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=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 : Any ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
15
1
def UpperCAmelCase ( a_ , a_ , a_ ) -> int: """simple docstring""" if exponent == 1: return base if exponent % 2 == 0: __A = _modexpt(a_ , exponent // 2 , a_ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(a_ , exponent - 1 , a_ )) % modulo_value def UpperCAmelCase ( a_ = 1_7_7_7 , a_ = 1_8_5_5 , a_ = 8 ) -> int: """simple docstring""" __A = base for _ in range(1 , a_ ): __A = _modexpt(a_ , a_ , 1_0**digits ) return result if __name__ == "__main__": print(f'''{solution() = }''')
15
import PIL.Image import PIL.ImageOps from packaging import version from PIL import Image if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('9.1.0'): SCREAMING_SNAKE_CASE :Any = { 'linear': PIL.Image.Resampling.BILINEAR, 'bilinear': PIL.Image.Resampling.BILINEAR, 'bicubic': PIL.Image.Resampling.BICUBIC, 'lanczos': PIL.Image.Resampling.LANCZOS, 'nearest': PIL.Image.Resampling.NEAREST, } else: SCREAMING_SNAKE_CASE :int = { 'linear': PIL.Image.LINEAR, 'bilinear': PIL.Image.BILINEAR, 'bicubic': PIL.Image.BICUBIC, 'lanczos': PIL.Image.LANCZOS, 'nearest': PIL.Image.NEAREST, } def UpperCAmelCase ( a_ ) -> Optional[Any]: """simple docstring""" __A = (images / 2 + 0.5).clamp(0 , 1 ) __A = images.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __A = numpy_to_pil(a_ ) return images def UpperCAmelCase ( a_ ) -> int: """simple docstring""" if images.ndim == 3: __A = images[None, ...] __A = (images * 2_5_5).round().astype("uint8" ) if images.shape[-1] == 1: # special case for grayscale (single channel) images __A = [Image.fromarray(image.squeeze() , mode="L" ) for image in images] else: __A = [Image.fromarray(a_ ) for image in images] return pil_images
15
1
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class UpperCAmelCase : '''simple docstring''' def __init__( self : List[Any] ,A : Optional[Any] ,A : Optional[Any]=2 ,A : Dict=3 ,A : Union[str, Any]=4 ,A : str=2 ,A : Any=7 ,A : List[str]=True ,A : List[str]=True ,A : Optional[Any]=True ,A : Optional[Any]=True ,A : Optional[Any]=99 ,A : str=36 ,A : Optional[Any]=2 ,A : List[str]=4 ,A : Optional[Any]=37 ,A : int="gelu" ,A : int=0.1 ,A : str=0.1 ,A : str=5_12 ,A : str=16 ,A : Tuple=2 ,A : Dict=0.02 ,A : str=6 ,A : Optional[Any]=6 ,A : Tuple=3 ,A : Optional[int]=4 ,A : int=None ,A : Dict=10_00 ,): __A = parent __A = batch_size __A = num_channels __A = image_size __A = patch_size __A = is_training __A = use_input_mask __A = use_token_type_ids __A = use_labels __A = vocab_size __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = type_vocab_size __A = type_sequence_label_size __A = initializer_range __A = coordinate_size __A = shape_size __A = num_labels __A = num_choices __A = scope __A = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) __A = text_seq_length __A = (image_size // patch_size) ** 2 + 1 __A = self.text_seq_length + self.image_seq_length def UpperCamelCase_ ( self : Dict ): __A = ids_tensor([self.batch_size, self.text_seq_length] ,self.vocab_size ) __A = ids_tensor([self.batch_size, self.text_seq_length, 4] ,self.range_bbox ) __A = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: __A = bbox[i, j, 3] __A = bbox[i, j, 1] __A = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: __A = bbox[i, j, 2] __A = bbox[i, j, 0] __A = tmp_coordinate __A = tf.constant(A ) __A = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __A = None if self.use_input_mask: __A = random_attention_mask([self.batch_size, self.text_seq_length] ) __A = None if self.use_token_type_ids: __A = ids_tensor([self.batch_size, self.text_seq_length] ,self.type_vocab_size ) __A = None __A = None if self.use_labels: __A = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) __A = ids_tensor([self.batch_size, self.text_seq_length] ,self.num_labels ) __A = LayoutLMvaConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,initializer_range=self.initializer_range ,coordinate_size=self.coordinate_size ,shape_size=self.shape_size ,input_size=self.image_size ,patch_size=self.patch_size ,) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def UpperCamelCase_ ( self : Any ,A : Optional[Any] ,A : List[str] ,A : Optional[Any] ,A : Any ,A : Optional[int] ,A : Union[str, Any] ): __A = TFLayoutLMvaModel(config=A ) # text + image __A = model(A ,pixel_values=A ,training=A ) __A = model( A ,bbox=A ,pixel_values=A ,attention_mask=A ,token_type_ids=A ,training=A ,) __A = model(A ,bbox=A ,pixel_values=A ,training=A ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) # text only __A = model(A ,training=A ) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.text_seq_length, self.hidden_size) ) # image only __A = model({"pixel_values": pixel_values} ,training=A ) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.image_seq_length, self.hidden_size) ) def UpperCamelCase_ ( self : Union[str, Any] ,A : Tuple ,A : List[Any] ,A : Tuple ,A : List[str] ,A : Tuple ,A : Union[str, Any] ,A : Tuple ): __A = self.num_labels __A = TFLayoutLMvaForSequenceClassification(config=A ) __A = model( A ,bbox=A ,pixel_values=A ,attention_mask=A ,token_type_ids=A ,labels=A ,training=A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def UpperCamelCase_ ( self : int ,A : Dict ,A : str ,A : Tuple ,A : Optional[Any] ,A : int ,A : Any ,A : Any ): __A = self.num_labels __A = TFLayoutLMvaForTokenClassification(config=A ) __A = model( A ,bbox=A ,pixel_values=A ,attention_mask=A ,token_type_ids=A ,labels=A ,training=A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.text_seq_length, self.num_labels) ) def UpperCamelCase_ ( self : Union[str, Any] ,A : int ,A : Tuple ,A : Optional[Any] ,A : List[Any] ,A : List[Any] ,A : str ,A : Tuple ): __A = 2 __A = TFLayoutLMvaForQuestionAnswering(config=A ) __A = model( A ,bbox=A ,pixel_values=A ,attention_mask=A ,token_type_ids=A ,start_positions=A ,end_positions=A ,training=A ,) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def UpperCamelCase_ ( self : int ): __A = self.prepare_config_and_inputs() ((__A) , (__A) , (__A) , (__A) , (__A) , (__A) , (__A) , (__A)) = config_and_inputs __A = { "input_ids": input_ids, "bbox": bbox, "pixel_values": pixel_values, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_tf class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) snake_case_ = ( {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel} if is_tf_available() else {} ) snake_case_ = False snake_case_ = False snake_case_ = False def UpperCamelCase_ ( self : int ,A : Optional[Any] ,A : List[str] ,A : Optional[int] ,A : str ,A : Tuple ): return True def UpperCamelCase_ ( self : Tuple ,A : int ,A : List[Any] ,A : str=False ): __A = copy.deepcopy(A ) if model_class in get_values(A ): __A = { k: tf.tile(tf.expand_dims(A ,1 ) ,(1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(A ,tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(A ): __A = tf.ones(self.model_tester.batch_size ,dtype=tf.intaa ) elif model_class in get_values(A ): __A = tf.zeros(self.model_tester.batch_size ,dtype=tf.intaa ) __A = tf.zeros(self.model_tester.batch_size ,dtype=tf.intaa ) elif model_class in get_values(A ): __A = tf.zeros(self.model_tester.batch_size ,dtype=tf.intaa ) elif model_class in get_values(A ): __A = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) ,dtype=tf.intaa ) return inputs_dict def UpperCamelCase_ ( self : str ): __A = TFLayoutLMvaModelTester(self ) __A = ConfigTester(self ,config_class=A ,hidden_size=37 ) def UpperCamelCase_ ( self : Union[str, Any] ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : Tuple ): __A , __A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __A = model_class(A ) if getattr(A ,"hf_compute_loss" ,A ): # The number of elements in the loss should be the same as the number of elements in the label __A = self._prepare_for_class(inputs_dict.copy() ,A ,return_labels=A ) __A = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() ,reverse=A )[0] ] __A = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs __A = self._prepare_for_class(inputs_dict.copy() ,A ,return_labels=A ) __A = prepared_for_class.pop("input_ids" ) __A = model(A ,**A )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions __A = self._prepare_for_class(inputs_dict.copy() ,A ,return_labels=A ) __A = prepared_for_class.pop("input_ids" ) if "labels" in prepared_for_class: __A = prepared_for_class["labels"].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: __A = -1_00 __A = tf.convert_to_tensor(A ) __A = model(A ,**A )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict __A = self._prepare_for_class(inputs_dict.copy() ,A ,return_labels=A ) __A = model(A )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple __A = self._prepare_for_class(inputs_dict.copy() ,A ,return_labels=A ) # Get keys that were added with the _prepare_for_class function __A = prepared_for_class.keys() - inputs_dict.keys() __A = inspect.signature(model.call ).parameters __A = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple __A = {0: "input_ids"} for label_key in label_keys: __A = signature_names.index(A ) __A = label_key __A = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple __A = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: __A = prepared_for_class[value] __A = tuple(A ) # Send to model __A = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def UpperCamelCase_ ( self : Tuple ): ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(A ,A ,A ,A ,A ,A ) def UpperCamelCase_ ( self : List[Any] ): ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: __A = type self.model_tester.create_and_check_model(A ,A ,A ,A ,A ,A ) def UpperCamelCase_ ( self : int ): ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( A ,A ,A ,A ,A ,A ,A ) def UpperCamelCase_ ( self : Dict ): ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( A ,A ,A ,A ,A ,A ,A ) def UpperCamelCase_ ( self : List[Any] ): ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( A ,A ,A ,A ,A ,A ,A ) @slow def UpperCamelCase_ ( self : str ): for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __A = TFLayoutLMvaModel.from_pretrained(A ) self.assertIsNotNone(A ) def UpperCAmelCase ( ) -> Any: """simple docstring""" __A = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def UpperCamelCase_ ( self : Dict ): return LayoutLMvaImageProcessor(apply_ocr=A ) if is_vision_available() else None @slow def UpperCamelCase_ ( self : str ): __A = TFLayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base" ) __A = self.default_image_processor __A = prepare_img() __A = image_processor(images=A ,return_tensors="tf" ).pixel_values __A = tf.constant([[1, 2]] ) __A = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) ,axis=0 ) # forward pass __A = model(input_ids=A ,bbox=A ,pixel_values=A ,training=A ) # verify the logits __A = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape ,A ) __A = tf.constant( [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] ,A ,atol=1E-4 ) )
15
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 SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[Any] = { 'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json', # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "yolos" def __init__( self : Any ,A : Optional[Any]=7_68 ,A : Dict=12 ,A : Any=12 ,A : str=30_72 ,A : Any="gelu" ,A : str=0.0 ,A : List[str]=0.0 ,A : Dict=0.02 ,A : int=1E-12 ,A : Tuple=[5_12, 8_64] ,A : List[Any]=16 ,A : str=3 ,A : str=True ,A : Any=1_00 ,A : Dict=True ,A : Dict=False ,A : Tuple=1 ,A : Union[str, Any]=5 ,A : Optional[Any]=2 ,A : Union[str, Any]=5 ,A : int=2 ,A : int=0.1 ,**A : List[str] ,): super().__init__(**A ) __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = initializer_range __A = layer_norm_eps __A = image_size __A = patch_size __A = num_channels __A = qkv_bias __A = num_detection_tokens __A = use_mid_position_embeddings __A = auxiliary_loss # Hungarian matcher __A = class_cost __A = bbox_cost __A = giou_cost # Loss coefficients __A = bbox_loss_coefficient __A = giou_loss_coefficient __A = eos_coefficient class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = version.parse("1.11" ) @property def UpperCamelCase_ ( self : str ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def UpperCamelCase_ ( self : List[Any] ): return 1E-4 @property def UpperCamelCase_ ( self : Optional[Any] ): return 12
15
1
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline SCREAMING_SNAKE_CASE :Tuple = argparse.ArgumentParser('Stable Diffusion script with intel optimization', add_help=False) parser.add_argument('--dpm', action='store_true', help='Enable DPMSolver or not') parser.add_argument('--steps', default=None, type=int, help='Num inference steps') SCREAMING_SNAKE_CASE :Union[str, Any] = parser.parse_args() SCREAMING_SNAKE_CASE :Optional[Any] = 'cpu' SCREAMING_SNAKE_CASE :Tuple = 'a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings' SCREAMING_SNAKE_CASE :Any = 'path-to-your-trained-model' SCREAMING_SNAKE_CASE :List[Any] = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: SCREAMING_SNAKE_CASE :Union[str, Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) SCREAMING_SNAKE_CASE :List[Any] = pipe.to(device) # to channels last SCREAMING_SNAKE_CASE :Optional[Any] = pipe.unet.to(memory_format=torch.channels_last) SCREAMING_SNAKE_CASE :Union[str, Any] = pipe.vae.to(memory_format=torch.channels_last) SCREAMING_SNAKE_CASE :str = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: SCREAMING_SNAKE_CASE :Optional[Any] = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex SCREAMING_SNAKE_CASE :Union[str, Any] = torch.randn(2, 4, 64, 64) SCREAMING_SNAKE_CASE :Union[str, Any] = torch.rand(1) * 999 SCREAMING_SNAKE_CASE :Any = torch.randn(2, 77, 768) SCREAMING_SNAKE_CASE :List[Any] = (sample, timestep, encoder_hidden_status) try: SCREAMING_SNAKE_CASE :int = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: SCREAMING_SNAKE_CASE :List[Any] = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) SCREAMING_SNAKE_CASE :Any = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) SCREAMING_SNAKE_CASE :Union[str, Any] = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: SCREAMING_SNAKE_CASE :Optional[Any] = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute SCREAMING_SNAKE_CASE :Tuple = 666 SCREAMING_SNAKE_CASE :Optional[int] = torch.Generator(device).manual_seed(seed) SCREAMING_SNAKE_CASE :Optional[Any] = {'generator': generator} if args.steps is not None: SCREAMING_SNAKE_CASE :str = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): SCREAMING_SNAKE_CASE :Optional[Any] = pipe(prompt, **generate_kwargs).images[0] # save image image.save('generated.png')
15
# Copyright 2021 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from packaging import version from .. import __version__ from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD from .doc import ( add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, copy_func, replace_return_docstrings, ) from .generic import ( ContextManagers, ExplicitEnum, ModelOutput, PaddingStrategy, TensorType, add_model_info_to_auto_map, cached_property, can_return_loss, expand_dims, find_labels, flatten_dict, infer_framework, is_jax_tensor, is_numpy_array, is_tensor, is_tf_symbolic_tensor, is_tf_tensor, is_torch_device, is_torch_dtype, is_torch_tensor, reshape, squeeze, strtobool, tensor_size, to_numpy, to_py_obj, transpose, working_or_temp_dir, ) from .hub import ( CLOUDFRONT_DISTRIB_PREFIX, DISABLE_TELEMETRY, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, EntryNotFoundError, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, cached_file, default_cache_path, define_sagemaker_information, download_url, extract_commit_hash, get_cached_models, get_file_from_repo, get_full_repo_name, has_file, http_user_agent, is_offline_mode, is_remote_url, move_cache, send_example_telemetry, try_to_load_from_cache, ) from .import_utils import ( ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, TORCH_FX_REQUIRED_VERSION, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, ccl_version, direct_transformers_import, get_torch_version, is_accelerate_available, is_apex_available, is_bitsandbytes_available, is_bsa_available, is_coloredlogs_available, is_cython_available, is_datasets_available, is_decord_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_jieba_available, is_jumanpp_available, is_kenlm_available, is_keras_nlp_available, is_librosa_available, is_natten_available, is_ninja_available, is_onnx_available, is_openai_available, is_optimum_available, is_pandas_available, is_peft_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytest_available, is_pytorch_quantization_available, is_rjieba_available, is_sacremoses_available, is_safetensors_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_sudachi_available, is_tensorflow_probability_available, is_tensorflow_text_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_bfaa_cpu_available, is_torch_bfaa_gpu_available, is_torch_compile_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_neuroncore_available, is_torch_tensorrt_fx_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_torchdistx_available, is_torchdynamo_available, is_torchvision_available, is_training_run_on_sagemaker, is_vision_available, requires_backends, torch_only_method, ) SCREAMING_SNAKE_CASE :List[str] = 'pytorch_model.bin' SCREAMING_SNAKE_CASE :str = 'pytorch_model.bin.index.json' SCREAMING_SNAKE_CASE :Optional[int] = 'adapter_config.json' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.bin' SCREAMING_SNAKE_CASE :Dict = 'adapter_model.safetensors' SCREAMING_SNAKE_CASE :str = 'tf_model.h5' SCREAMING_SNAKE_CASE :List[Any] = 'tf_model.h5.index.json' SCREAMING_SNAKE_CASE :str = 'model.ckpt' SCREAMING_SNAKE_CASE :List[Any] = 'flax_model.msgpack' SCREAMING_SNAKE_CASE :Optional[int] = 'flax_model.msgpack.index.json' SCREAMING_SNAKE_CASE :Tuple = 'model.safetensors' SCREAMING_SNAKE_CASE :List[Any] = 'model.safetensors.index.json' SCREAMING_SNAKE_CASE :str = 'config.json' SCREAMING_SNAKE_CASE :int = 'preprocessor_config.json' SCREAMING_SNAKE_CASE :Optional[Any] = FEATURE_EXTRACTOR_NAME SCREAMING_SNAKE_CASE :Optional[int] = 'generation_config.json' SCREAMING_SNAKE_CASE :List[str] = 'modelcard.json' SCREAMING_SNAKE_CASE :Optional[int] = '▁' SCREAMING_SNAKE_CASE :Optional[Any] = SENTENCEPIECE_UNDERLINE # Kept for backward compatibility SCREAMING_SNAKE_CASE :str = [ [[0, 1, 0, 1], [1, 0, 0, 1]] ] * 2 # Needs to have 0s and 1s only since XLM uses it for langs too. SCREAMING_SNAKE_CASE :Optional[Any] = [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]] SCREAMING_SNAKE_CASE :List[Any] = [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]] def UpperCAmelCase ( a_ ) -> Dict: """simple docstring""" if version.parse(a_ ) < version.parse(a_ ): if "dev" in min_version: __A = ( "This example requires a source install from HuggingFace Transformers (see " "`https://huggingface.co/docs/transformers/installation#install-from-source`)," ) else: __A = F'''This example requires a minimum version of {min_version},''' error_message += F''' but the version found is {__version__}.\n''' raise ImportError( error_message + "Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other " "versions of HuggingFace Transformers." )
15
1
import re import string import numpy as np import datasets SCREAMING_SNAKE_CASE :Optional[int] = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' SCREAMING_SNAKE_CASE :Tuple = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' SCREAMING_SNAKE_CASE :str = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Value("string" ,id="sequence" ), "references": datasets.Value("string" ,id="sequence" ), } ) ,reference_urls=[] ,) def UpperCamelCase_ ( self : List[Any] ,A : List[Any] ,A : List[Any] ,A : Any=None ,A : List[Any]=False ,A : Any=False ,A : Dict=False ,): if regexes_to_ignore is not None: for s in regexes_to_ignore: __A = np.array([re.sub(A ,"" ,A ) for x in predictions] ) __A = np.array([re.sub(A ,"" ,A ) for x in references] ) else: __A = np.asarray(A ) __A = np.asarray(A ) if ignore_case: __A = np.char.lower(A ) __A = np.char.lower(A ) if ignore_punctuation: __A = string.punctuation.maketrans("" ,"" ,string.punctuation ) __A = np.char.translate(A ,table=A ) __A = np.char.translate(A ,table=A ) if ignore_numbers: __A = string.digits.maketrans("" ,"" ,string.digits ) __A = np.char.translate(A ,table=A ) __A = np.char.translate(A ,table=A ) __A = predictions == references return {"exact_match": np.mean(A ) * 1_00}
15
def UpperCAmelCase ( a_ ) -> Optional[int]: """simple docstring""" __A = [0] * len(a_ ) __A = [] __A = [1] * len(a_ ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(a_ ) ): if indegree[i] == 0: queue.append(a_ ) while queue: __A = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: __A = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(a_ ) print(max(a_ ) ) # Adjacency list of Graph SCREAMING_SNAKE_CASE :List[Any] = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
15
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :Tuple = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Optional[Any] = {'vocab_file': 'sentencepiece.bpe.model'} SCREAMING_SNAKE_CASE :int = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, } SCREAMING_SNAKE_CASE :List[Any] = { 'moussaKam/mbarthez': 1024, 'moussaKam/barthez': 1024, 'moussaKam/barthez-orangesum-title': 1024, } SCREAMING_SNAKE_CASE :Dict = '▁' class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Any ,A : str ,A : str="<s>" ,A : str="</s>" ,A : Any="</s>" ,A : List[str]="<s>" ,A : Optional[Any]="<unk>" ,A : Tuple="<pad>" ,A : Optional[Any]="<mask>" ,A : Optional[Dict[str, Any]] = None ,**A : Any ,): # Mask token behave like a normal word, i.e. include the space before it __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,sep_token=A ,cls_token=A ,pad_token=A ,mask_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(A ) ) __A = {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3} __A = len(self.sp_model ) - 1 __A = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCamelCase_ ( self : Tuple ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase_ ( self : List[str] ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def UpperCamelCase_ ( self : List[str] ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase_ ( self : Union[str, Any] ): return len(self.sp_model ) def UpperCamelCase_ ( self : Tuple ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : str ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[Any] ,A : int ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] __A = self.sp_model.PieceToId(A ) return spm_id if spm_id else self.unk_token_id def UpperCamelCase_ ( self : Dict ,A : Dict ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(A ) def UpperCamelCase_ ( self : Any ,A : Optional[Any] ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def __getstate__( self : int ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : str ,A : Dict ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Tuple ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
15
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def UpperCAmelCase ( a_ ) -> List[str]: """simple docstring""" return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items() ) def UpperCAmelCase ( a_ , a_ ) -> Tuple: """simple docstring""" __A = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue __A = key.replace("heads.cmd.mim_head.cls.predictions" , "mmm_image_head" ) __A = key.replace("heads.cmd.mlm_head.cls.predictions" , "mmm_text_head" ) __A = key.replace("heads.cmd.itm_head.cls" , "itm_head" ) __A = key.replace("heads.cmd.itm_head.pooler" , "itm_head.pooler" ) __A = key.replace("heads.cmd.clip_head.logit_scale" , "flava.logit_scale" ) __A = key.replace("heads.fairseq_mlm.cls.predictions" , "mlm_head" ) __A = key.replace("heads.imagenet.mim_head.cls.predictions" , "mim_head" ) __A = key.replace("mm_text_projection" , "flava.text_to_mm_projection" ) __A = key.replace("mm_image_projection" , "flava.image_to_mm_projection" ) __A = key.replace("image_encoder.module" , "flava.image_model" ) __A = key.replace("text_encoder.module" , "flava.text_model" ) __A = key.replace("mm_encoder.module.encoder.cls_token" , "flava.multimodal_model.cls_token" ) __A = key.replace("mm_encoder.module" , "flava.multimodal_model" ) __A = key.replace("text_projection" , "flava.text_projection" ) __A = key.replace("image_projection" , "flava.image_projection" ) __A = value.float() for key, value in codebook_state_dict.items(): __A = value return upgrade @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_ , a_=None ) -> Tuple: """simple docstring""" if config_path is not None: __A = FlavaConfig.from_pretrained(a_ ) else: __A = FlavaConfig() __A = FlavaForPreTraining(a_ ).eval() __A = convert_dalle_checkpoint(a_ , a_ , save_checkpoint=a_ ) if os.path.exists(a_ ): __A = torch.load(a_ , map_location="cpu" ) else: __A = torch.hub.load_state_dict_from_url(a_ , map_location="cpu" ) __A = upgrade_state_dict(a_ , a_ ) hf_model.load_state_dict(a_ ) __A = hf_model.state_dict() __A = count_parameters(a_ ) __A = count_parameters(a_ ) + count_parameters(a_ ) assert torch.allclose(a_ , a_ , atol=1E-3 ) hf_model.save_pretrained(a_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE :Any = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to flava checkpoint') parser.add_argument('--codebook_path', default=None, type=str, help='Path to flava codebook checkpoint') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
15
1
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = LongformerTokenizer snake_case_ = True snake_case_ = LongformerTokenizerFast snake_case_ = True def UpperCamelCase_ ( self : Any ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __A = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] __A = dict(zip(A ,range(len(A ) ) ) ) __A = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] __A = {"unk_token": "<unk>"} __A = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) __A = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file ,"w" ,encoding="utf-8" ) as fp: fp.write(json.dumps(A ) + "\n" ) with open(self.merges_file ,"w" ,encoding="utf-8" ) as fp: fp.write("\n".join(A ) ) def UpperCamelCase_ ( self : Tuple ,**A : Dict ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname ,**A ) def UpperCamelCase_ ( self : int ,**A : Optional[Any] ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname ,**A ) def UpperCamelCase_ ( self : Any ,A : Tuple ): __A = "lower newer" __A = "lower newer" return input_text, output_text def UpperCamelCase_ ( self : Dict ): __A = self.tokenizer_class(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) __A = "lower newer" __A = ["l", "o", "w", "er", "\u0120", "n", "e", "w", "er"] __A = tokenizer.tokenize(A ) # , add_prefix_space=True) self.assertListEqual(A ,A ) __A = tokens + [tokenizer.unk_token] __A = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) ,A ) def UpperCamelCase_ ( self : Optional[Any] ): __A = self.get_tokenizer() self.assertListEqual(tokenizer.encode("Hello world!" ,add_special_tokens=A ) ,[0, 3_14_14, 2_32, 3_28, 2] ) self.assertListEqual( tokenizer.encode("Hello world! cécé herlolip 418" ,add_special_tokens=A ) ,[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2] ,) @slow def UpperCamelCase_ ( self : Union[str, Any] ): __A = self.tokenizer_class.from_pretrained("allenai/longformer-base-4096" ) __A = tokenizer.encode("sequence builders" ,add_special_tokens=A ) __A = tokenizer.encode("multi-sequence build" ,add_special_tokens=A ) __A = tokenizer.encode( "sequence builders" ,add_special_tokens=A ,add_prefix_space=A ) __A = tokenizer.encode( "sequence builders" ,"multi-sequence build" ,add_special_tokens=A ,add_prefix_space=A ) __A = tokenizer.build_inputs_with_special_tokens(A ) __A = tokenizer.build_inputs_with_special_tokens(A ,A ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def UpperCamelCase_ ( self : Optional[Any] ): __A = self.get_tokenizer() __A = "Encode this sequence." __A = tokenizer.byte_encoder[" ".encode("utf-8" )[0]] # Testing encoder arguments __A = tokenizer.encode(A ,add_special_tokens=A ,add_prefix_space=A ) __A = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(A ,A ) __A = tokenizer.encode(A ,add_special_tokens=A ,add_prefix_space=A ) __A = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(A ,A ) tokenizer.add_special_tokens({"bos_token": "<s>"} ) __A = tokenizer.encode(A ,add_special_tokens=A ) __A = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(A ,A ) # Testing spaces after special tokens __A = "<mask>" tokenizer.add_special_tokens( {"mask_token": AddedToken(A ,lstrip=A ,rstrip=A )} ) # mask token has a left space __A = tokenizer.convert_tokens_to_ids(A ) __A = "Encode <mask> sequence" __A = "Encode <mask>sequence" __A = tokenizer.encode(A ) __A = encoded.index(A ) __A = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(A ,A ) __A = tokenizer.encode(A ) __A = encoded.index(A ) __A = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(A ,A ) def UpperCamelCase_ ( self : int ): pass def UpperCamelCase_ ( self : str ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __A = self.rust_tokenizer_class.from_pretrained(A ,**A ) __A = self.tokenizer_class.from_pretrained(A ,**A ) __A = "A, <mask> AllenNLP sentence." __A = tokenizer_r.encode_plus(A ,add_special_tokens=A ,return_token_type_ids=A ) __A = tokenizer_p.encode_plus(A ,add_special_tokens=A ,return_token_type_ids=A ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r["token_type_ids"] ) ,sum(tokens_p["token_type_ids"] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) ,sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) ,) __A = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) __A = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p["input_ids"] ,[0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"] ,[0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual( A ,["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( A ,["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) def UpperCamelCase_ ( self : int ): for trim_offsets, add_prefix_space in itertools.product([True, False] ,repeat=2 ): __A = self.rust_tokenizer_class.from_pretrained( self.tmpdirname ,use_fast=A ,add_prefix_space=A ,trim_offsets=A ) __A = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) __A = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state["add_prefix_space"] ,A ) self.assertEqual(post_processor_state["add_prefix_space"] ,A ) self.assertEqual(post_processor_state["trim_offsets"] ,A ) def UpperCamelCase_ ( self : Any ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __A = "hello" # `hello` is a token in the vocabulary of `pretrained_name` __A = f'''{text_of_1_token} {text_of_1_token}''' __A = self.rust_tokenizer_class.from_pretrained( A ,use_fast=A ,add_prefix_space=A ,trim_offsets=A ) __A = tokenizer_r(A ,return_offsets_mapping=A ,add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] ,(0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] ,(len(A ) + 1, len(A ) + 1 + len(A )) ,) __A = self.rust_tokenizer_class.from_pretrained( A ,use_fast=A ,add_prefix_space=A ,trim_offsets=A ) __A = tokenizer_r(A ,return_offsets_mapping=A ,add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] ,(0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] ,(len(A ) + 1, len(A ) + 1 + len(A )) ,) __A = self.rust_tokenizer_class.from_pretrained( A ,use_fast=A ,add_prefix_space=A ,trim_offsets=A ) __A = tokenizer_r(A ,return_offsets_mapping=A ,add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] ,(0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] ,(len(A ), len(A ) + 1 + len(A )) ,) __A = self.rust_tokenizer_class.from_pretrained( A ,use_fast=A ,add_prefix_space=A ,trim_offsets=A ) __A = tokenizer_r(A ,return_offsets_mapping=A ,add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] ,(0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] ,(len(A ), len(A ) + 1 + len(A )) ,) __A = f''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) __A = self.rust_tokenizer_class.from_pretrained( A ,use_fast=A ,add_prefix_space=A ,trim_offsets=A ) __A = tokenizer_r(A ,return_offsets_mapping=A ,add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] ,(1, 1 + len(A )) ) self.assertEqual( encoding.offset_mapping[1] ,(1 + len(A ) + 1, 1 + len(A ) + 1 + len(A )) ,) __A = self.rust_tokenizer_class.from_pretrained( A ,use_fast=A ,add_prefix_space=A ,trim_offsets=A ) __A = tokenizer_r(A ,return_offsets_mapping=A ,add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] ,(0, 1 + len(A )) ) self.assertEqual( encoding.offset_mapping[1] ,(1 + len(A ), 1 + len(A ) + 1 + len(A )) ,) __A = self.rust_tokenizer_class.from_pretrained( A ,use_fast=A ,add_prefix_space=A ,trim_offsets=A ) __A = tokenizer_r(A ,return_offsets_mapping=A ,add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] ,(0, 1 + len(A )) ) self.assertEqual( encoding.offset_mapping[1] ,(1 + len(A ), 1 + len(A ) + 1 + len(A )) ,)
15
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE :Optional[int] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Optional[int] = {'vocab_file': 'sentencepiece.bpe.model'} SCREAMING_SNAKE_CASE :Tuple = { 'vocab_file': { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model', } } SCREAMING_SNAKE_CASE :List[Any] = { 'camembert-base': 512, } SCREAMING_SNAKE_CASE :List[str] = '▁' class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[Any] ,A : List[str] ,A : List[Any]="<s>" ,A : Tuple="</s>" ,A : Any="</s>" ,A : Optional[Any]="<s>" ,A : Tuple="<unk>" ,A : str="<pad>" ,A : int="<mask>" ,A : Optional[int]=["<s>NOTUSED", "</s>NOTUSED"] ,A : Optional[Dict[str, Any]] = None ,**A : Optional[Any] ,): # Mask token behave like a normal word, i.e. include the space before it __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A ,eos_token=A ,unk_token=A ,sep_token=A ,cls_token=A ,pad_token=A ,mask_token=A ,additional_special_tokens=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(A ) ) __A = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __A = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __A = len(self.fairseq_tokens_to_ids ) __A = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __A = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCamelCase_ ( self : int ,A : List[int] ,A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __A = [self.cls_token_id] __A = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase_ ( self : Dict ,A : List[int] ,A : Optional[List[int]] = None ,A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A ,token_ids_a=A ,already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def UpperCamelCase_ ( self : Union[str, Any] ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase_ ( self : Dict ): return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCamelCase_ ( self : int ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : List[str] ,A : Dict ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(A ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : Tuple ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCamelCase_ ( self : Optional[Any] ,A : Dict ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string.strip() def __getstate__( self : Dict ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Union[str, Any] ,A : Any ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : Any ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
15
1
import unittest from transformers import AlbertTokenizer, AlbertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE :Dict = get_tests_dir('fixtures/spiece.model') @require_sentencepiece @require_tokenizers class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = AlbertTokenizer snake_case_ = AlbertTokenizerFast snake_case_ = True snake_case_ = True snake_case_ = True def UpperCamelCase_ ( self : Union[str, Any] ): super().setUp() # We have a SentencePiece fixture for testing __A = AlbertTokenizer(A ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self : Tuple ,A : List[str] ): __A = "this is a test" __A = "this is a test" return input_text, output_text def UpperCamelCase_ ( self : List[Any] ): __A = "<pad>" __A = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A ) ,A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A ) ,A ) def UpperCamelCase_ ( self : Optional[Any] ): __A = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"<pad>" ) self.assertEqual(vocab_keys[1] ,"<unk>" ) self.assertEqual(vocab_keys[-1] ,"▁eloquent" ) self.assertEqual(len(A ) ,3_00_00 ) def UpperCamelCase_ ( self : str ): self.assertEqual(self.get_tokenizer().vocab_size ,3_00_00 ) def UpperCamelCase_ ( self : str ): if not self.test_rust_tokenizer: return __A = self.get_tokenizer() __A = self.get_rust_tokenizer() __A = "I was born in 92000, and this is falsé." __A = tokenizer.tokenize(A ) __A = rust_tokenizer.tokenize(A ) self.assertListEqual(A ,A ) __A = tokenizer.encode(A ,add_special_tokens=A ) __A = rust_tokenizer.encode(A ,add_special_tokens=A ) self.assertListEqual(A ,A ) __A = self.get_rust_tokenizer() __A = tokenizer.encode(A ) __A = rust_tokenizer.encode(A ) self.assertListEqual(A ,A ) def UpperCamelCase_ ( self : Any ): __A = AlbertTokenizer(A ,keep_accents=A ) __A = tokenizer.tokenize("This is a test" ) self.assertListEqual(A ,["▁this", "▁is", "▁a", "▁test"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) ,[48, 25, 21, 12_89] ) __A = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( A ,["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", "."] ) __A = tokenizer.convert_tokens_to_ids(A ) self.assertListEqual(A ,[31, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9] ) __A = tokenizer.convert_ids_to_tokens(A ) self.assertListEqual( A ,["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "."] ,) def UpperCamelCase_ ( self : List[str] ): __A = AlbertTokenizer(A ) __A = tokenizer.encode("sequence builders" ) __A = tokenizer.encode("multi-sequence build" ) __A = tokenizer.build_inputs_with_special_tokens(A ) __A = tokenizer.build_inputs_with_special_tokens(A ,A ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ] @slow def UpperCamelCase_ ( self : int ): # fmt: off __A = {"attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "input_ids": [[2, 2_19_70, 13, 5, 60_92, 1_67, 28, 71_03, 21_53, 6_73, 8, 70_28, 1_20_51, 18, 17, 71_03, 21_53, 6_73, 8, 35_15, 1_86_84, 8, 44_61, 6, 19_27, 2_97, 8, 1_20_60, 26_07, 18, 13, 5, 44_61, 15, 1_05_38, 38, 8, 1_35, 15, 8_22, 58, 15, 9_93, 1_03_63, 15, 14_60, 80_05, 44_61, 15, 9_93, 2_55, 23_28, 9, 9, 9, 6, 26, 11_12, 8_16, 32_60, 13, 5, 1_03, 23_77, 6, 17, 11_12, 8_16, 27_82, 13, 5, 1_03, 1_06_41, 6, 29, 84, 25_12, 24_30, 7_82, 1_86_84, 27_61, 19, 8_08, 24_30, 25_56, 17, 8_55, 14_80, 94_77, 40_91, 1_28, 1_17_12, 15, 71_03, 21_53, 6_73, 17, 2_48_83, 99_90, 9, 3], [2, 1_15_02, 25, 10_06, 20, 7_82, 8, 1_18_09, 8_55, 17_32, 1_93_93, 1_86_67, 37, 3_67, 2_10_18, 69, 18_54, 34, 1_18_60, 1_91_24, 27, 1_56, 2_25, 17, 1_93, 41_41, 19, 65, 91_24, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 14, 22_31, 8_86, 23_85, 1_76_59, 84, 14, 1_67_92, 19_52, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "token_type_ids": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=A ,model_name="albert-base-v2" ,revision="6b6560eaf5ff2e250b00c50f380c5389a9c2d82e" ,)
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return [tuple(a_ )] __A = [] def generate(a_ , a_ ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , a_ ) for i in range(k - 1 ): if k % 2 == 0: # k is even __A , __A = arr[k - 1], arr[i] else: # k is odd __A , __A = arr[k - 1], arr[0] generate(k - 1 , a_ ) generate(len(a_ ) , a_ ) return res if __name__ == "__main__": SCREAMING_SNAKE_CASE :int = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :Dict = [int(item) for item in user_input.split(',')] print(heaps(arr))
15
1
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = (KDPMaDiscreteScheduler,) snake_case_ = 10 def UpperCamelCase_ ( self : Optional[int] ,**A : int ): __A = { "num_train_timesteps": 11_00, "beta_start": 0.00_01, "beta_end": 0.02, "beta_schedule": "linear", } config.update(**A ) return config def UpperCamelCase_ ( self : Dict ): for timesteps in [10, 50, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=A ) def UpperCamelCase_ ( self : Dict ): for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01] ,[0.00_02, 0.0_02, 0.02] ): self.check_over_configs(beta_start=A ,beta_end=A ) def UpperCamelCase_ ( self : Tuple ): for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=A ) def UpperCamelCase_ ( self : int ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=A ) def UpperCamelCase_ ( self : str ): __A = self.scheduler_classes[0] __A = self.get_scheduler_config(prediction_type="v_prediction" ) __A = scheduler_class(**A ) scheduler.set_timesteps(self.num_inference_steps ) __A = self.dummy_model() __A = self.dummy_sample_deter * scheduler.init_noise_sigma __A = sample.to(A ) for i, t in enumerate(scheduler.timesteps ): __A = scheduler.scale_model_input(A ,A ) __A = model(A ,A ) __A = scheduler.step(A ,A ,A ) __A = output.prev_sample __A = torch.sum(torch.abs(A ) ) __A = torch.mean(torch.abs(A ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6934E-07 ) < 1E-2 assert abs(result_mean.item() - 6.1112E-10 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 4.693428650170972E-07 ) < 1E-2 assert abs(result_mean.item() - 0.00_02 ) < 1E-3 def UpperCamelCase_ ( self : Optional[Any] ): if torch_device == "mps": return __A = self.scheduler_classes[0] __A = self.get_scheduler_config() __A = scheduler_class(**A ) scheduler.set_timesteps(self.num_inference_steps ) __A = self.dummy_model() __A = self.dummy_sample_deter * scheduler.init_noise_sigma __A = sample.to(A ) for i, t in enumerate(scheduler.timesteps ): __A = scheduler.scale_model_input(A ,A ) __A = model(A ,A ) __A = scheduler.step(A ,A ,A ) __A = output.prev_sample __A = torch.sum(torch.abs(A ) ) __A = torch.mean(torch.abs(A ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.41_25 ) < 1E-2 assert abs(result_mean.item() - 0.02_66 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 20.41_25 ) < 1E-2 assert abs(result_mean.item() - 0.02_66 ) < 1E-3 def UpperCamelCase_ ( self : Dict ): if torch_device == "mps": return __A = self.scheduler_classes[0] __A = self.get_scheduler_config() __A = scheduler_class(**A ) scheduler.set_timesteps(self.num_inference_steps ,device=A ) __A = self.dummy_model() __A = self.dummy_sample_deter.to(A ) * scheduler.init_noise_sigma for t in scheduler.timesteps: __A = scheduler.scale_model_input(A ,A ) __A = model(A ,A ) __A = scheduler.step(A ,A ,A ) __A = output.prev_sample __A = torch.sum(torch.abs(A ) ) __A = torch.mean(torch.abs(A ) ) if str(A ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.41_25 ) < 1E-2 assert abs(result_mean.item() - 0.02_66 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 20.41_25 ) < 1E-2 assert abs(result_mean.item() - 0.02_66 ) < 1E-3
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return lst __A = 1 while i < len(a_ ): if lst[i - 1] <= lst[i]: i += 1 else: __A , __A = lst[i], lst[i - 1] i -= 1 if i == 0: __A = 1 return lst if __name__ == "__main__": SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :List[Any] = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
15
1
from __future__ import annotations from typing import Any def UpperCAmelCase ( a_ ) -> int: """simple docstring""" if not postfix_notation: return 0 __A = {"+", "-", "*", "/"} __A = [] for token in postfix_notation: if token in operations: __A , __A = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(a_ ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
15
from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 snake_case_ = 42 snake_case_ = None class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 2 @register_to_config def __init__( self : str ,A : float = 0.02 ,A : float = 1_00 ,A : float = 1.0_07 ,A : float = 80 ,A : float = 0.05 ,A : float = 50 ,): # standard deviation of the initial noise distribution __A = sigma_max # setable values __A = None __A = None __A = None # sigma(t_i) def UpperCamelCase_ ( self : str ,A : torch.FloatTensor ,A : Optional[int] = None ): return sample def UpperCamelCase_ ( self : Dict ,A : int ,A : Union[str, torch.device] = None ): __A = num_inference_steps __A = np.arange(0 ,self.num_inference_steps )[::-1].copy() __A = torch.from_numpy(A ).to(A ) __A = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in self.timesteps ] __A = torch.tensor(A ,dtype=torch.floataa ,device=A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : torch.FloatTensor ,A : float ,A : Optional[torch.Generator] = None ): if self.config.s_min <= sigma <= self.config.s_max: __A = min(self.config.s_churn / self.num_inference_steps ,2**0.5 - 1 ) else: __A = 0 # sample eps ~ N(0, S_noise^2 * I) __A = self.config.s_noise * randn_tensor(sample.shape ,generator=A ).to(sample.device ) __A = sigma + gamma * sigma __A = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def UpperCamelCase_ ( self : Dict ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_hat + sigma_hat * model_output __A = (sample_hat - pred_original_sample) / sigma_hat __A = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : Optional[int] ,A : torch.FloatTensor ,A : float ,A : float ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : torch.FloatTensor ,A : bool = True ,): __A = sample_prev + sigma_prev * model_output __A = (sample_prev - pred_original_sample) / sigma_prev __A = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative) return KarrasVeOutput( prev_sample=A ,derivative=A ,pred_original_sample=A ) def UpperCamelCase_ ( self : List[Any] ,A : Dict ,A : List[str] ,A : str ): raise NotImplementedError()
15
1
from typing import List import numpy as np def UpperCAmelCase ( a_ ) -> int: """simple docstring""" __A = {key: len(a_ ) for key, value in gen_kwargs.items() if isinstance(a_ , a_ )} if len(set(lists_lengths.values() ) ) > 1: raise RuntimeError( ( "Sharding is ambiguous for this dataset: " + "we found several data sources lists of different lengths, and we don't know over which list we should parallelize:\n" + "\n".join(F'''\t- key {key} has length {length}''' for key, length in lists_lengths.items() ) + "\nTo fix this, check the 'gen_kwargs' and make sure to use lists only for data sources, " + "and use tuples otherwise. In the end there should only be one single list, or several lists with the same length." ) ) __A = max(lists_lengths.values() , default=0 ) return max(1 , a_ ) def UpperCAmelCase ( a_ , a_ ) -> List[range]: """simple docstring""" __A = [] for group_idx in range(a_ ): __A = num_shards // max_num_jobs + (group_idx < (num_shards % max_num_jobs)) if num_shards_to_add == 0: break __A = shards_indices_per_group[-1].stop if shards_indices_per_group else 0 __A = range(a_ , start + num_shards_to_add ) shards_indices_per_group.append(a_ ) return shards_indices_per_group def UpperCAmelCase ( a_ , a_ ) -> List[dict]: """simple docstring""" __A = _number_of_shards_in_gen_kwargs(a_ ) if num_shards == 1: return [dict(a_ )] else: __A = _distribute_shards(num_shards=a_ , max_num_jobs=a_ ) return [ { key: [value[shard_idx] for shard_idx in shard_indices_per_group[group_idx]] if isinstance(a_ , a_ ) else value for key, value in gen_kwargs.items() } for group_idx in range(len(a_ ) ) ] def UpperCAmelCase ( a_ ) -> dict: """simple docstring""" return { key: [value for gen_kwargs in gen_kwargs_list for value in gen_kwargs[key]] if isinstance(gen_kwargs_list[0][key] , a_ ) else gen_kwargs_list[0][key] for key in gen_kwargs_list[0] } def UpperCAmelCase ( a_ , a_ ) -> dict: """simple docstring""" __A = {len(a_ ) for value in gen_kwargs.values() if isinstance(a_ , a_ )} __A = {} for size in list_sizes: __A = list(range(a_ ) ) rng.shuffle(indices_per_size[size] ) # Now let's copy the gen_kwargs and shuffle the lists based on their sizes __A = dict(a_ ) for key, value in shuffled_kwargs.items(): if isinstance(a_ , a_ ): __A = [value[i] for i in indices_per_size[len(a_ )]] return shuffled_kwargs
15
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version SCREAMING_SNAKE_CASE :Union[str, Any] = get_logger(__name__) class UpperCAmelCase : '''simple docstring''' snake_case_ = "dummy_data" snake_case_ = "datasets" snake_case_ = False def __init__( self : Optional[int] ,A : str ,A : str ,A : Union[Version, str] ,A : Optional[str] = None ,A : bool = False ,A : bool = True ,A : Optional[List[Callable]] = None ,): __A = 0 __A = dataset_name __A = cache_dir __A = use_local_dummy_data __A = config # download_callbacks take a single url as input __A = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root __A = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general __A = str(A ) # to be downloaded __A = None __A = None @property def UpperCamelCase_ ( self : Union[str, Any] ): if self._dummy_file is None: __A = self.download_dummy_data() return self._dummy_file @property def UpperCamelCase_ ( self : Optional[Any] ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("dummy" ,self.config.name ,self.version_name ) # structure is dummy / version_name return os.path.join("dummy" ,self.version_name ) @property def UpperCamelCase_ ( self : List[Any] ): return os.path.join(self.dummy_data_folder ,"dummy_data.zip" ) def UpperCamelCase_ ( self : Tuple ): __A = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) __A = cached_path( A ,cache_dir=self.cache_dir ,extract_compressed_file=A ,force_extract=A ) return os.path.join(A ,self.dummy_file_name ) @property def UpperCamelCase_ ( self : str ): return os.path.join(self.datasets_scripts_dir ,self.dataset_name ,self.dummy_zip_file ) @property def UpperCamelCase_ ( self : Any ): if self._bucket_url is None: __A = hf_github_url(self.dataset_name ,self.dummy_zip_file.replace(os.sep ,"/" ) ) return self._bucket_url @property def UpperCamelCase_ ( self : Tuple ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep ,"/" ).split("/" )[:-1] ) def UpperCamelCase_ ( self : List[str] ,A : List[Any] ,*A : Dict ): if self.load_existing_dummy_data: # dummy data is downloaded and tested __A = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned __A = self.dummy_file_name # special case when data_url is a dict if isinstance(A ,A ): return self.create_dummy_data_dict(A ,A ) elif isinstance(A ,(list, tuple) ): return self.create_dummy_data_list(A ,A ) else: return self.create_dummy_data_single(A ,A ) def UpperCamelCase_ ( self : str ,A : List[Any] ,*A : List[Any] ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : List[str] ,A : List[str] ,A : Tuple ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : Any ,A : Any ,*A : Optional[Any] ,**A : List[str] ): return path def UpperCamelCase_ ( self : str ): return {} def UpperCamelCase_ ( self : int ,A : int ,A : Tuple ): __A = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(A ,A ): for single_url in single_urls: download_callback(A ) else: __A = single_urls download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(A ,A ): __A = [os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) for x in single_urls] else: __A = single_urls __A = os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) __A = value # make sure that values are unique if all(isinstance(A ,A ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique __A = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,A : str ): __A = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one __A = all(bool(re.findall("[0-9]{3,}-of-[0-9]{3,}" ,A ) ) for url in data_url ) __A = all( url.startswith("https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): __A = [data_url[0]] * len(A ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(single_url.split("/" )[-1] ) ) dummy_data_list.append(A ) return dummy_data_list def UpperCamelCase_ ( self : str ,A : List[Any] ,A : Optional[Any] ): for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(data_url.split("/" )[-1] ) ) if os.path.exists(A ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def UpperCamelCase_ ( self : int ): pass def UpperCamelCase_ ( self : Dict ): pass def UpperCamelCase_ ( self : Optional[Any] ,A : List[Any] ): def _iter_archive_members(A : Optional[Any] ): # this preserves the order of the members inside the ZIP archive __A = Path(self.dummy_file ).parent __A = path.relative_to(A ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: __A = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(A ) __A = Path(A ) __A = _iter_archive_members(A ) if self.use_local_dummy_data else path.rglob("*" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((".", "__") ): yield file_path.relative_to(A ).as_posix(), file_path.open("rb" ) def UpperCamelCase_ ( self : List[Any] ,A : Any ): if not isinstance(A ,A ): __A = [paths] for path in paths: if os.path.isfile(A ): if os.path.basename(A ).startswith((".", "__") ): return yield path else: for dirpath, dirnames, filenames in os.walk(A ): if os.path.basename(A ).startswith((".", "__") ): continue dirnames.sort() for filename in sorted(A ): if filename.startswith((".", "__") ): continue yield os.path.join(A ,A )
15
1
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = ["image_processor", "tokenizer"] snake_case_ = "BridgeTowerImageProcessor" snake_case_ = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self : Any ,A : str ,A : int ): super().__init__(A ,A ) def __call__( self : Optional[int] ,A : Tuple ,A : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None ,A : bool = True ,A : Union[bool, str, PaddingStrategy] = False ,A : Union[bool, str, TruncationStrategy] = None ,A : Optional[int] = None ,A : int = 0 ,A : Optional[int] = None ,A : Optional[bool] = None ,A : Optional[bool] = None ,A : bool = False ,A : bool = False ,A : bool = False ,A : bool = False ,A : bool = True ,A : Optional[Union[str, TensorType]] = None ,**A : List[str] ,): __A = self.tokenizer( text=A ,add_special_tokens=A ,padding=A ,truncation=A ,max_length=A ,stride=A ,pad_to_multiple_of=A ,return_token_type_ids=A ,return_attention_mask=A ,return_overflowing_tokens=A ,return_special_tokens_mask=A ,return_offsets_mapping=A ,return_length=A ,verbose=A ,return_tensors=A ,**A ,) # add pixel_values + pixel_mask __A = self.image_processor( A ,return_tensors=A ,do_normalize=A ,do_center_crop=A ,**A ) encoding.update(A ) return encoding def UpperCamelCase_ ( self : Optional[Any] ,*A : Optional[Any] ,**A : str ): return self.tokenizer.batch_decode(*A ,**A ) def UpperCamelCase_ ( self : Tuple ,*A : str ,**A : Any ): return self.tokenizer.decode(*A ,**A ) @property def UpperCamelCase_ ( self : Tuple ): __A = self.tokenizer.model_input_names __A = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
15
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available SCREAMING_SNAKE_CASE :List[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :List[str] = ['BartphoTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys SCREAMING_SNAKE_CASE :Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
15
1
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Sequence, Value from .base import TaskTemplate @dataclass(frozen=__SCREAMING_SNAKE_CASE ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = field(default="question-answering-extractive" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case_ = Features({"question": Value("string" ), "context": Value("string" )} ) snake_case_ = Features( { "answers": Sequence( { "text": Value("string" ), "answer_start": Value("int32" ), } ) } ) snake_case_ = "question" snake_case_ = "context" snake_case_ = "answers" @property def UpperCamelCase_ ( self : Dict ): return {self.question_column: "question", self.context_column: "context", self.answers_column: "answers"}
15
from typing import Dict, Optional import numpy as np import datasets SCREAMING_SNAKE_CASE :List[Any] = '\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n' SCREAMING_SNAKE_CASE :List[str] = '\nArgs:\n predictions (`List[ndarray]`):\n List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n references (`List[ndarray]`):\n List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n num_labels (`int`):\n Number of classes (categories).\n ignore_index (`int`):\n Index that will be ignored during evaluation.\n nan_to_num (`int`, *optional*):\n If specified, NaN values will be replaced by the number defined by the user.\n label_map (`dict`, *optional*):\n If specified, dictionary mapping old label indices to new label indices.\n reduce_labels (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n `Dict[str, float | ndarray]` comprising various elements:\n - *mean_iou* (`float`):\n Mean Intersection-over-Union (IoU averaged over all categories).\n - *mean_accuracy* (`float`):\n Mean accuracy (averaged over all categories).\n - *overall_accuracy* (`float`):\n Overall accuracy on all images.\n - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n Per category accuracy.\n - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n Per category IoU.\n\nExamples:\n\n >>> import numpy as np\n\n >>> mean_iou = datasets.load_metric("mean_iou")\n\n >>> # suppose one has 3 different segmentation maps predicted\n >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n >>> predicted = [predicted_1, predicted_2, predicted_3]\n >>> ground_truth = [actual_1, actual_2, actual_3]\n\n >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), \'per_category_accuracy\': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])}\n' SCREAMING_SNAKE_CASE :str = '\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}' def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Tuple: """simple docstring""" if label_map is not None: for old_id, new_id in label_map.items(): __A = new_id # turn into Numpy arrays __A = np.array(a_ ) __A = np.array(a_ ) if reduce_labels: __A = 2_5_5 __A = label - 1 __A = 2_5_5 __A = label != ignore_index __A = np.not_equal(a_ , a_ ) __A = pred_label[mask] __A = np.array(a_ )[mask] __A = pred_label[pred_label == label] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = np.histogram(a_ , bins=a_ , range=(0, num_labels - 1) )[0] __A = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = False , ) -> Union[str, Any]: """simple docstring""" __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) __A = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(a_ , a_ ): __A , __A , __A , __A = intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ = None , a_ = None , a_ = False , ) -> str: """simple docstring""" __A , __A , __A , __A = total_intersect_and_union( a_ , a_ , a_ , a_ , a_ , a_ ) # compute metrics __A = {} __A = total_area_intersect.sum() / total_area_label.sum() __A = total_area_intersect / total_area_union __A = total_area_intersect / total_area_label __A = np.nanmean(a_ ) __A = np.nanmean(a_ ) __A = all_acc __A = iou __A = acc if nan_to_num is not None: __A = {metric: np.nan_to_num(a_ , nan=a_ ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { "predictions": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), "references": datasets.Sequence(datasets.Sequence(datasets.Value("uint16" ) ) ), } ) ,reference_urls=[ "https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py" ] ,) def UpperCamelCase_ ( self : int ,A : Optional[Any] ,A : Optional[Any] ,A : int ,A : bool ,A : Optional[int] = None ,A : Optional[Dict[int, int]] = None ,A : bool = False ,): __A = mean_iou( results=A ,gt_seg_maps=A ,num_labels=A ,ignore_index=A ,nan_to_num=A ,label_map=A ,reduce_labels=A ,) return iou_result
15
1
from maths.prime_check import is_prime def UpperCAmelCase ( a_ ) -> int: """simple docstring""" if not isinstance(a_ , a_ ): __A = F'''Input value of [number={number}] must be an integer''' raise TypeError(a_ ) if is_prime(a_ ) and is_prime(number + 2 ): return number + 2 else: return -1 if __name__ == "__main__": import doctest doctest.testmod()
15
import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE :List[str] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :List[str] = {'vocab_file': 'spiece.model'} SCREAMING_SNAKE_CASE :Dict = { 'vocab_file': { 'AI-Sweden/gpt-sw3-126m': 'https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-350m': 'https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-1.6b': 'https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-6.7b': 'https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model', 'AI-Sweden/gpt-sw3-20b': 'https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model', } } SCREAMING_SNAKE_CASE :Optional[Any] = { 'AI-Sweden/gpt-sw3-126m': 2048, 'AI-Sweden/gpt-sw3-350m': 2048, 'AI-Sweden/gpt-sw3-1.6b': 2048, 'AI-Sweden/gpt-sw3-6.7b': 2048, 'AI-Sweden/gpt-sw3-20b': 2048, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Optional[int] ,A : Optional[Any] ,A : Optional[int]=False ,A : int=False ,A : Union[str, Any]=False ,A : int=None ,A : Optional[Any]=None ,A : Union[str, Any]=None ,A : Optional[Any]=None ,A : Optional[Dict[str, Any]] = None ,**A : Tuple ,): __A = {} if sp_model_kwargs is None else sp_model_kwargs __A = kwargs.get("name_or_path" ) if name_or_path is None: logger.warning( "name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b," " you are testing the model, this can safely be ignored" ) __A = "None" # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing __A = "<|endoftext|>" if eos_token is None else eos_token __A = "<unk>" if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: __A = unk_token if pad_token is None else pad_token __A = eos_token if bos_token is None else bos_token else: __A = "<pad>" if pad_token is None else pad_token __A = "<s>" if bos_token is None else bos_token super().__init__( do_lower_case=A ,remove_space=A ,keep_accents=A ,bos_token=A ,eos_token=A ,unk_token=A ,pad_token=A ,sp_model_kwargs=self.sp_model_kwargs ,**A ,) __A = do_lower_case __A = remove_space __A = keep_accents __A = vocab_file __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) # Used for whitespace normalization in input texts # fmt : off __A = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", "„"} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing __A = re.compile( f'''[{''.join(map(A ,list(range(0 ,9 ) ) + list(range(11 ,32 ) ) + list(range(1_27 ,1_60 ) ) + [1_60, 1_73, 82_03] ) )}]''' ) def __getstate__( self : Optional[int] ): __A = self.__dict__.copy() __A = None return state def __setstate__( self : Optional[Any] ,A : Union[str, Any] ): __A = d # for backward compatibility if not hasattr(self ,"sp_model_kwargs" ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def UpperCamelCase_ ( self : List[str] ): return len(self.sp_model ) def UpperCamelCase_ ( self : int ,A : str ): __A = self.non_printing_characters_re.sub("" ,A ) # Normalize whitespaces __A = "".join([char if char not in self.whitespaces else " " for char in text] ) # NFC Unicode normalization __A = unicodedata.normalize("NFC" ,A ) return text def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,**A : Optional[int] ): __A = self.preprocess_text(A ) return self.sp_model.encode(A ,out_type=A ) def UpperCamelCase_ ( self : Any ,A : str ): return self.sp_model.PieceToId(A ) def UpperCamelCase_ ( self : Dict ,A : int ): return self.sp_model.IdToPiece(A ) @staticmethod def UpperCamelCase_ ( A : str ): return out_string def UpperCamelCase_ ( self : str ,A : List[str] ): __A = [] __A = "" __A = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token __A = True __A = [] else: current_sub_tokens.append(A ) __A = False out_string += self.sp_model.decode(A ) return out_string def UpperCamelCase_ ( self : str ): __A = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase_ ( self : List[str] ,A : str ,A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __A = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,A ) elif not os.path.isfile(self.vocab_file ): with open(A ,"wb" ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,) def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, List[str]] ,A : Union[str, bool] = False ): if isinstance(A ,A ): __A = self.preprocess_text(A ) __A = self.sp_model.encode(A ) else: __A = [self.preprocess_text(A ) for t in text] __A = self.sp_model.encode(A ) if return_tensors is True or return_tensors == "pt": __A = torch.tensor(A ) return token_ids def UpperCamelCase_ ( self : List[Any] ,A : Union[int, List[int]] ): return self.sp_model.decode(A ) def UpperCamelCase_ ( self : List[str] ,A : "Conversation" ): __A = [f'''User: {text}''' if is_user else f'''Bot: {text}''' for is_user, text in conversation.iter_texts()] __A = ( f'''{self.eos_token}{self.bos_token}''' + f'''{self.bos_token}'''.join(A ) + f'''{self.bos_token}Bot:''' ) return self.encode(text=A )
15
1