code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : Tuple = logging.get_logger(__name__) __A : int = { 'sayakpaul/vit-msn-base': 'https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[int] = 'vit_msn' def __init__( self :int ,_UpperCamelCase :str=7_6_8 ,_UpperCamelCase :int=1_2 ,_UpperCamelCase :Optional[Any]=1_2 ,_UpperCamelCase :str=3_0_7_2 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :Any=0.0 ,_UpperCamelCase :Dict=0.02 ,_UpperCamelCase :Tuple=1E-0_6 ,_UpperCamelCase :List[str]=2_2_4 ,_UpperCamelCase :int=1_6 ,_UpperCamelCase :Any=3 ,_UpperCamelCase :Tuple=True ,**_UpperCamelCase :Optional[Any] ,): super().__init__(**_UpperCamelCase ) snake_case_ : int = hidden_size snake_case_ : Tuple = num_hidden_layers snake_case_ : Tuple = num_attention_heads snake_case_ : Dict = intermediate_size snake_case_ : Dict = hidden_act snake_case_ : int = hidden_dropout_prob snake_case_ : List[str] = attention_probs_dropout_prob snake_case_ : Tuple = initializer_range snake_case_ : List[Any] = layer_norm_eps snake_case_ : Union[str, Any] = image_size snake_case_ : Dict = patch_size snake_case_ : Tuple = num_channels snake_case_ : str = qkv_bias
8
'''simple docstring''' from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig __A : Dict = { 'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json', 'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json', } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[int] = 'ernie_m' lowercase : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self :Optional[Any] ,_UpperCamelCase :int = 2_5_0_0_0_2 ,_UpperCamelCase :int = 7_6_8 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 3_0_7_2 ,_UpperCamelCase :str = "gelu" ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :int = 5_1_4 ,_UpperCamelCase :float = 0.02 ,_UpperCamelCase :int = 1 ,_UpperCamelCase :float = 1E-0_5 ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Optional[int]=0.0 ,**_UpperCamelCase :List[Any] ,): super().__init__(pad_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Optional[int] = vocab_size snake_case_ : Any = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : Union[str, Any] = num_attention_heads snake_case_ : Any = intermediate_size snake_case_ : Any = hidden_act snake_case_ : Tuple = hidden_dropout_prob snake_case_ : Union[str, Any] = attention_probs_dropout_prob snake_case_ : str = max_position_embeddings snake_case_ : int = initializer_range snake_case_ : Optional[Any] = layer_norm_eps snake_case_ : Union[str, Any] = classifier_dropout snake_case_ : Tuple = is_decoder snake_case_ : int = act_dropout
8
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __A : Any = { 'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/config.json', 'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/config.json', 'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/config.json', 'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json', 'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/config.json', 'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/config.json', 'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/config.json', 'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json', } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[Any] = 'albert' def __init__( self :int ,_UpperCamelCase :Tuple=3_0_0_0_0 ,_UpperCamelCase :Optional[int]=1_2_8 ,_UpperCamelCase :Dict=4_0_9_6 ,_UpperCamelCase :Tuple=1_2 ,_UpperCamelCase :List[str]=1 ,_UpperCamelCase :Dict=6_4 ,_UpperCamelCase :List[str]=1_6_3_8_4 ,_UpperCamelCase :Any=1 ,_UpperCamelCase :List[str]="gelu_new" ,_UpperCamelCase :int=0 ,_UpperCamelCase :Dict=0 ,_UpperCamelCase :Dict=5_1_2 ,_UpperCamelCase :Dict=2 ,_UpperCamelCase :Tuple=0.02 ,_UpperCamelCase :Dict=1E-1_2 ,_UpperCamelCase :List[str]=0.1 ,_UpperCamelCase :str="absolute" ,_UpperCamelCase :Optional[int]=0 ,_UpperCamelCase :List[str]=2 ,_UpperCamelCase :str=3 ,**_UpperCamelCase :Dict ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Union[str, Any] = vocab_size snake_case_ : Optional[Any] = embedding_size snake_case_ : int = hidden_size snake_case_ : Tuple = num_hidden_layers snake_case_ : Optional[int] = num_hidden_groups snake_case_ : Dict = num_attention_heads snake_case_ : Any = inner_group_num snake_case_ : Union[str, Any] = hidden_act snake_case_ : Any = intermediate_size snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : List[str] = attention_probs_dropout_prob snake_case_ : List[Any] = max_position_embeddings snake_case_ : Union[str, Any] = type_vocab_size snake_case_ : Any = initializer_range snake_case_ : Dict = layer_norm_eps snake_case_ : Optional[int] = classifier_dropout_prob snake_case_ : Optional[int] = position_embedding_type class __UpperCamelCase ( lowercase__ ): @property def a__ ( self :List[Any] ): if self.task == "multiple-choice": snake_case_ : Dict = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case_ : Union[str, Any] = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
8
'''simple docstring''' from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class __UpperCamelCase ( nn.Module ): def __init__( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = True ,_UpperCamelCase :str = "layer_norm" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Any = only_cross_attention snake_case_ : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" snake_case_ : Any = (num_embeds_ada_norm is not None) and norm_type == """ada_norm""" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F'''`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to''' F''' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.''' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: snake_case_ : Dict = AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ : str = AdaLayerNormZero(_UpperCamelCase ,_UpperCamelCase ) else: snake_case_ : List[Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=_UpperCamelCase ,) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. snake_case_ : str = ( AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,upcast_attention=_UpperCamelCase ,) # is self-attn if encoder_hidden_states is none else: snake_case_ : Any = None snake_case_ : Optional[Any] = None # 3. Feed-forward snake_case_ : List[str] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : Union[str, Any] = FeedForward(_UpperCamelCase ,dropout=_UpperCamelCase ,activation_fn=_UpperCamelCase ,final_dropout=_UpperCamelCase ) # let chunk size default to None snake_case_ : Optional[int] = None snake_case_ : Dict = 0 def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :int ): # Sets chunk feed-forward snake_case_ : Optional[Any] = chunk_size snake_case_ : Optional[Any] = dim def a__ ( self :List[str] ,_UpperCamelCase :torch.FloatTensor ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,_UpperCamelCase :Dict[str, Any] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = self.norma( _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=hidden_states.dtype ) else: snake_case_ : Optional[int] = self.norma(_UpperCamelCase ) snake_case_ : int = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case_ : Union[str, Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_msa.unsqueeze(1 ) * attn_output snake_case_ : Union[str, Any] = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case_ : Any = ( self.norma(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else self.norma(_UpperCamelCase ) ) snake_case_ : List[Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=_UpperCamelCase ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Tuple = attn_output + hidden_states # 3. Feed-forward snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Dict = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F'''`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.''' ) snake_case_ : Union[str, Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case_ : int = torch.cat( [self.ff(_UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase ,dim=self._chunk_dim )] ,dim=self._chunk_dim ,) else: snake_case_ : List[str] = self.ff(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case_ : Any = ff_output + hidden_states return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Dict ,_UpperCamelCase :int ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :int = 4 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Tuple = int(dim * mult ) snake_case_ : Optional[int] = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case_ : Any = GELU(_UpperCamelCase ,_UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case_ : Tuple = GELU(_UpperCamelCase ,_UpperCamelCase ,approximate="""tanh""" ) elif activation_fn == "geglu": snake_case_ : Dict = GEGLU(_UpperCamelCase ,_UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case_ : Optional[Any] = ApproximateGELU(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Dict = nn.ModuleList([] ) # project in self.net.append(_UpperCamelCase ) # project dropout self.net.append(nn.Dropout(_UpperCamelCase ) ) # project out self.net.append(nn.Linear(_UpperCamelCase ,_UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase ) ) def a__ ( self :Tuple ,_UpperCamelCase :Union[str, Any] ): for module in self.net: snake_case_ : Tuple = module(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :str = "none" ): super().__init__() snake_case_ : Union[str, Any] = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Optional[Any] = approximate def a__ ( self :str ,_UpperCamelCase :int ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ,approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ,approximate=self.approximate ).to(dtype=gate.dtype ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[Any] ): snake_case_ : Optional[Any] = self.proj(_UpperCamelCase ) snake_case_ : int = self.gelu(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :List[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : str = nn.Linear(_UpperCamelCase ,dim_out * 2 ) def a__ ( self :Dict ,_UpperCamelCase :List[str] ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def a__ ( self :Optional[Any] ,_UpperCamelCase :Optional[int] ): snake_case_ , snake_case_ : Dict = self.proj(_UpperCamelCase ).chunk(2 ,dim=-1 ) return hidden_states * self.gelu(_UpperCamelCase ) class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[int] ): snake_case_ : int = self.proj(_UpperCamelCase ) return x * torch.sigmoid(1.7_02 * x ) class __UpperCamelCase ( nn.Module ): def __init__( self :int ,_UpperCamelCase :str ,_UpperCamelCase :List[Any] ): super().__init__() snake_case_ : int = nn.Embedding(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = nn.SiLU() snake_case_ : Any = nn.Linear(_UpperCamelCase ,embedding_dim * 2 ) snake_case_ : Dict = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) def a__ ( self :int ,_UpperCamelCase :List[str] ,_UpperCamelCase :int ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ) ) ) snake_case_ , snake_case_ : Tuple = torch.chunk(_UpperCamelCase ,2 ) snake_case_ : Tuple = self.norm(_UpperCamelCase ) * (1 + scale) + shift return x class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :Tuple ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = CombinedTimestepLabelEmbeddings(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : int = nn.SiLU() snake_case_ : List[str] = nn.Linear(_UpperCamelCase ,6 * embedding_dim ,bias=_UpperCamelCase ) snake_case_ : str = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ,eps=1E-6 ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str=None ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=_UpperCamelCase ) ) ) snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Any = emb.chunk(6 ,dim=1 ) snake_case_ : str = self.norm(_UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[int] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :Optional[str] = None ,_UpperCamelCase :float = 1E-5 ): super().__init__() snake_case_ : Optional[int] = num_groups snake_case_ : List[Any] = eps if act_fn is None: snake_case_ : int = None else: snake_case_ : Dict = get_activation(_UpperCamelCase ) snake_case_ : Optional[int] = nn.Linear(_UpperCamelCase ,out_dim * 2 ) def a__ ( self :List[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :List[str] ): if self.act: snake_case_ : Any = self.act(_UpperCamelCase ) snake_case_ : Optional[int] = self.linear(_UpperCamelCase ) snake_case_ : Dict = emb[:, :, None, None] snake_case_ , snake_case_ : str = emb.chunk(2 ,dim=1 ) snake_case_ : str = F.group_norm(_UpperCamelCase ,self.num_groups ,eps=self.eps ) snake_case_ : List[str] = x * (1 + scale) + shift return x
8
1
'''simple docstring''' import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ConvNextConfig, UperNetConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import UperNetForSemanticSegmentation from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __UpperCamelCase : def __init__( self :Optional[int] ,_UpperCamelCase :Any ,_UpperCamelCase :int=1_3 ,_UpperCamelCase :Any=3_2 ,_UpperCamelCase :Any=3 ,_UpperCamelCase :str=4 ,_UpperCamelCase :List[str]=[1_0, 2_0, 3_0, 4_0] ,_UpperCamelCase :Dict=[2, 2, 3, 2] ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :Tuple=True ,_UpperCamelCase :Dict=3_7 ,_UpperCamelCase :List[str]="gelu" ,_UpperCamelCase :Tuple=1_0 ,_UpperCamelCase :List[str]=0.02 ,_UpperCamelCase :Tuple=["stage2", "stage3", "stage4"] ,_UpperCamelCase :str=3 ,_UpperCamelCase :List[Any]=None ,): snake_case_ : List[str] = parent snake_case_ : Union[str, Any] = batch_size snake_case_ : Tuple = image_size snake_case_ : Optional[int] = num_channels snake_case_ : List[str] = num_stages snake_case_ : List[Any] = hidden_sizes snake_case_ : Optional[Any] = depths snake_case_ : List[str] = is_training snake_case_ : str = use_labels snake_case_ : Any = intermediate_size snake_case_ : Dict = hidden_act snake_case_ : List[Any] = type_sequence_label_size snake_case_ : List[str] = initializer_range snake_case_ : Tuple = out_features snake_case_ : Dict = num_labels snake_case_ : List[str] = scope snake_case_ : Optional[Any] = num_stages def a__ ( self :Any ): snake_case_ : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ : Tuple = None if self.use_labels: snake_case_ : Any = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) snake_case_ : Optional[int] = self.get_config() return config, pixel_values, labels def a__ ( self :List[str] ): return ConvNextConfig( num_channels=self.num_channels ,num_stages=self.num_stages ,hidden_sizes=self.hidden_sizes ,depths=self.depths ,is_training=self.is_training ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,out_features=self.out_features ,) def a__ ( self :List[str] ): return UperNetConfig( backbone_config=self.get_backbone_config() ,hidden_size=5_1_2 ,pool_scales=[1, 2, 3, 6] ,use_auxiliary_head=_UpperCamelCase ,auxiliary_loss_weight=0.4 ,auxiliary_in_channels=4_0 ,auxiliary_channels=2_5_6 ,auxiliary_num_convs=1 ,auxiliary_concat_input=_UpperCamelCase ,loss_ignore_index=2_5_5 ,num_labels=self.num_labels ,) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Dict ,_UpperCamelCase :List[str] ): snake_case_ : Union[str, Any] = UperNetForSemanticSegmentation(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ : List[Any] = model(_UpperCamelCase ) self.parent.assertEqual( result.logits.shape ,(self.batch_size, self.num_labels, self.image_size, self.image_size) ) def a__ ( self :Dict ): snake_case_ : List[str] = self.prepare_config_and_inputs() ( ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ) : Union[str, Any] = config_and_inputs snake_case_ : List[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __UpperCamelCase ( lowercase__ , lowercase__ , unittest.TestCase ): lowercase : str = (UperNetForSemanticSegmentation,) if is_torch_available() else () lowercase : List[Any] = {'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {} lowercase : Optional[int] = False lowercase : Union[str, Any] = False lowercase : int = False lowercase : int = False lowercase : Dict = False lowercase : Any = False def a__ ( self :List[Any] ): snake_case_ : Tuple = UperNetModelTester(self ) snake_case_ : List[Any] = ConfigTester(self ,config_class=_UpperCamelCase ,has_text_modality=_UpperCamelCase ,hidden_size=3_7 ) def a__ ( self :Union[str, Any] ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def a__ ( self :List[Any] ): return def a__ ( self :Optional[Any] ): snake_case_ , snake_case_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ : List[Any] = model_class(_UpperCamelCase ) snake_case_ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ : Optional[Any] = [*signature.parameters.keys()] snake_case_ : Optional[Any] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] ,_UpperCamelCase ) def a__ ( self :Optional[Any] ): snake_case_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_UpperCamelCase ) @unittest.skip(reason="""UperNet does not use inputs_embeds""" ) def a__ ( self :List[str] ): pass @unittest.skip(reason="""UperNet does not support input and output embeddings""" ) def a__ ( self :Dict ): pass @unittest.skip(reason="""UperNet does not have a base model""" ) def a__ ( self :Union[str, Any] ): pass @unittest.skip(reason="""UperNet does not have a base model""" ) def a__ ( self :Union[str, Any] ): pass @require_torch_multi_gpu @unittest.skip(reason="""UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`""" ) def a__ ( self :str ): pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def a__ ( self :Union[str, Any] ): pass def a__ ( self :Optional[int] ): def check_hidden_states_output(_UpperCamelCase :Dict ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :str ): snake_case_ : Any = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): snake_case_ : List[Any] = model(**self._prepare_for_class(_UpperCamelCase ,_UpperCamelCase ) ) snake_case_ : Optional[Any] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states snake_case_ : Any = self.model_tester.num_stages self.assertEqual(len(_UpperCamelCase ) ,expected_num_stages + 1 ) # ConvNext's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) ,[self.model_tester.image_size // 4, self.model_tester.image_size // 4] ,) snake_case_ , snake_case_ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ : List[Any] = True check_hidden_states_output(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ : List[str] = True check_hidden_states_output(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :int ): snake_case_ , snake_case_ : int = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : str = _config_zero_init(_UpperCamelCase ) snake_case_ : str = _config_zero_init(configs_no_init.backbone_config ) for model_class in self.all_model_classes: snake_case_ : str = model_class(config=_UpperCamelCase ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() ,[0.0, 1.0] ,msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' ,) @unittest.skip(reason="""UperNet does not have tied weights""" ) def a__ ( self :Union[str, Any] ): pass @slow def a__ ( self :Tuple ): for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ : List[Any] = UperNetForSemanticSegmentation.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Tuple = hf_hub_download( repo_id="""hf-internal-testing/fixtures_ade20k""" , repo_type="""dataset""" , filename="""ADE_val_00000001.jpg""" ) snake_case_ : Optional[Any] = Image.open(lowerCamelCase_ ).convert("""RGB""" ) return image @require_torch @require_vision @slow class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :Optional[int] ): snake_case_ : Union[str, Any] = AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny""" ) snake_case_ : Optional[Any] = UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny""" ).to(_UpperCamelCase ) snake_case_ : Optional[int] = prepare_img() snake_case_ : int = processor(images=_UpperCamelCase ,return_tensors="""pt""" ).to(_UpperCamelCase ) with torch.no_grad(): snake_case_ : str = model(**_UpperCamelCase ) snake_case_ : List[str] = torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2) ) self.assertEqual(outputs.logits.shape ,_UpperCamelCase ) snake_case_ : Dict = torch.tensor( [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,_UpperCamelCase ,atol=1E-4 ) ) def a__ ( self :Optional[int] ): snake_case_ : int = AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny""" ) snake_case_ : List[Any] = UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny""" ).to(_UpperCamelCase ) snake_case_ : Optional[int] = prepare_img() snake_case_ : Tuple = processor(images=_UpperCamelCase ,return_tensors="""pt""" ).to(_UpperCamelCase ) with torch.no_grad(): snake_case_ : Optional[Any] = model(**_UpperCamelCase ) snake_case_ : Tuple = torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2) ) self.assertEqual(outputs.logits.shape ,_UpperCamelCase ) snake_case_ : Optional[int] = torch.tensor( [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,_UpperCamelCase ,atol=1E-4 ) )
8
'''simple docstring''' import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :str=True , lowerCamelCase_ :str="pt" ): '''simple docstring''' snake_case_ : Tuple = {"""add_prefix_space""": True} if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and not line.startswith(""" """ ) else {} snake_case_ : Union[str, Any] = padding_side return tokenizer( [line] , max_length=lowerCamelCase_ , padding="""max_length""" if pad_to_max_length else None , truncation=lowerCamelCase_ , return_tensors=lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , **lowerCamelCase_ , ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Any=None , ): '''simple docstring''' snake_case_ : Dict = input_ids.ne(lowerCamelCase_ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[Any] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any="train" ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :int=None ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :Optional[int]="" ,): super().__init__() snake_case_ : List[str] = Path(_UpperCamelCase ).joinpath(type_path + """.source""" ) snake_case_ : int = Path(_UpperCamelCase ).joinpath(type_path + """.target""" ) snake_case_ : Optional[int] = self.get_char_lens(self.src_file ) snake_case_ : List[str] = max_source_length snake_case_ : str = max_target_length assert min(self.src_lens ) > 0, F'''found empty line in {self.src_file}''' snake_case_ : str = tokenizer snake_case_ : str = prefix if n_obs is not None: snake_case_ : int = self.src_lens[:n_obs] snake_case_ : Tuple = src_lang snake_case_ : str = tgt_lang def __len__( self :Any ): return len(self.src_lens ) def __getitem__( self :List[str] ,_UpperCamelCase :Union[str, Any] ): snake_case_ : Optional[int] = index + 1 # linecache starts at 1 snake_case_ : Dict = self.prefix + linecache.getline(str(self.src_file ) ,_UpperCamelCase ).rstrip("""\n""" ) snake_case_ : List[Any] = linecache.getline(str(self.tgt_file ) ,_UpperCamelCase ).rstrip("""\n""" ) assert source_line, F'''empty source line for index {index}''' assert tgt_line, F'''empty tgt line for index {index}''' # Need to add eos token manually for T5 if isinstance(self.tokenizer ,_UpperCamelCase ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right snake_case_ : int = ( self.tokenizer.question_encoder if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer ) snake_case_ : Optional[int] = self.tokenizer.generator if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer snake_case_ : Optional[Any] = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_source_length ,"""right""" ) snake_case_ : Tuple = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_target_length ,"""right""" ) snake_case_ : int = source_inputs["""input_ids"""].squeeze() snake_case_ : str = target_inputs["""input_ids"""].squeeze() snake_case_ : Union[str, Any] = source_inputs["""attention_mask"""].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def a__ ( _UpperCamelCase :str ): return [len(_UpperCamelCase ) for x in Path(_UpperCamelCase ).open().readlines()] def a__ ( self :Optional[int] ,_UpperCamelCase :List[str] ): snake_case_ : Optional[Any] = torch.stack([x["""input_ids"""] for x in batch] ) snake_case_ : List[Any] = torch.stack([x["""attention_mask"""] for x in batch] ) snake_case_ : Union[str, Any] = torch.stack([x["""decoder_input_ids"""] for x in batch] ) snake_case_ : Optional[Any] = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Tuple = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Optional[int] = trim_batch(_UpperCamelCase ,_UpperCamelCase ) snake_case_ , snake_case_ : Dict = trim_batch(_UpperCamelCase ,_UpperCamelCase ,attention_mask=_UpperCamelCase ) snake_case_ : Optional[int] = { """input_ids""": source_ids, """attention_mask""": source_mask, """decoder_input_ids""": y, } return batch __A : List[Any] = getLogger(__name__) def UpperCAmelCase ( lowerCamelCase_ :List[List] ): '''simple docstring''' return list(itertools.chain.from_iterable(lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : int = get_git_info() save_json(lowerCamelCase_ , os.path.join(lowerCamelCase_ , """git_log.json""" ) ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int]=4 , **lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' with open(lowerCamelCase_ , """w""" ) as f: json.dump(lowerCamelCase_ , lowerCamelCase_ , indent=lowerCamelCase_ , **lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] ): '''simple docstring''' with open(lowerCamelCase_ ) as f: return json.load(lowerCamelCase_ ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Optional[Any] = git.Repo(search_parent_directories=lowerCamelCase_ ) snake_case_ : List[str] = { """repo_id""": str(lowerCamelCase_ ), """repo_sha""": str(repo.head.object.hexsha ), """repo_branch""": str(repo.active_branch ), """hostname""": str(socket.gethostname() ), } return repo_infos def UpperCAmelCase ( lowerCamelCase_ :Callable , lowerCamelCase_ :Iterable ): '''simple docstring''' return list(map(lowerCamelCase_ , lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , """wb""" ) as f: return pickle.dump(lowerCamelCase_ , lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' def remove_articles(lowerCamelCase_ :str ): return re.sub(R"""\b(a|an|the)\b""" , """ """ , lowerCamelCase_ ) def white_space_fix(lowerCamelCase_ :Optional[Any] ): return " ".join(text.split() ) def remove_punc(lowerCamelCase_ :Tuple ): snake_case_ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowerCamelCase_ :Optional[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase_ ) ) ) ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' snake_case_ : List[Any] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : Optional[int] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : List[Any] = Counter(lowerCamelCase_ ) & Counter(lowerCamelCase_ ) snake_case_ : Optional[Any] = sum(common.values() ) if num_same == 0: return 0 snake_case_ : Optional[Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Union[str, Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Optional[Any] = (2 * precision * recall) / (precision + recall) return fa def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' return normalize_answer(lowerCamelCase_ ) == normalize_answer(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] ): '''simple docstring''' assert len(lowerCamelCase_ ) == len(lowerCamelCase_ ) snake_case_ : Optional[int] = 0 for hypo, pred in zip(lowerCamelCase_ , lowerCamelCase_ ): em += exact_match_score(lowerCamelCase_ , lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: em /= len(lowerCamelCase_ ) return {"em": em} def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return model_prefix.startswith("""rag""" ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead snake_case_ : Optional[int] = """dropout_rate""" for p in extra_params: if getattr(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): if not hasattr(lowerCamelCase_ , lowerCamelCase_ ) and not hasattr(lowerCamelCase_ , equivalent_param[p] ): logger.info("""config doesn't have a `{}` attribute""".format(lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) continue snake_case_ : str = p if hasattr(lowerCamelCase_ , lowerCamelCase_ ) else equivalent_param[p] setattr(lowerCamelCase_ , lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) return hparams, config
8
1
'''simple docstring''' import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any]=7 ): '''simple docstring''' snake_case_ : Dict = None if token is not None: snake_case_ : Union[str, Any] = {"""Accept""": """application/vnd.github+json""", """Authorization""": F'''Bearer {token}'''} # The id of a workflow (not of a workflow run) snake_case_ : str = """636036""" snake_case_ : List[str] = F'''https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs''' # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F'''?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}''' snake_case_ : str = requests.get(lowerCamelCase_ , headers=lowerCamelCase_ ).json() return result["workflow_runs"] def UpperCAmelCase ( lowerCamelCase_ :List[str] ): '''simple docstring''' snake_case_ : Any = get_daily_ci_runs(lowerCamelCase_ ) snake_case_ : Optional[int] = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": snake_case_ : List[str] = workflow_run["""id"""] break return workflow_run_id def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Any , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : str = get_last_daily_ci_runs(lowerCamelCase_ ) if workflow_run_id is not None: snake_case_ : Tuple = get_artifacts_links(worflow_run_id=lowerCamelCase_ , token=lowerCamelCase_ ) for artifact_name in artifact_names: if artifact_name in artifacts_links: snake_case_ : Tuple = artifacts_links[artifact_name] download_artifact( artifact_name=lowerCamelCase_ , artifact_url=lowerCamelCase_ , output_dir=lowerCamelCase_ , token=lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Any ): '''simple docstring''' get_last_daily_ci_artifacts(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) snake_case_ : Tuple = {} for artifact_name in artifact_names: snake_case_ : Any = os.path.join(lowerCamelCase_ , F'''{artifact_name}.zip''' ) if os.path.isfile(lowerCamelCase_ ): snake_case_ : Dict = {} with zipfile.ZipFile(lowerCamelCase_ ) as z: for filename in z.namelist(): if not os.path.isdir(lowerCamelCase_ ): # read the file with z.open(lowerCamelCase_ ) as f: snake_case_ : Tuple = f.read().decode("""UTF-8""" ) return results
8
'''simple docstring''' import functools def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[str] = len(lowerCamelCase_ ) snake_case_ : Dict = len(lowerCamelCase_ ) @functools.cache def min_distance(lowerCamelCase_ :int , lowerCamelCase_ :int ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa snake_case_ : Union[str, Any] = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , lowerCamelCase_ ) , 1 + min_distance(lowerCamelCase_ , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
8
1
'''simple docstring''' import fire from utils import calculate_rouge, save_json def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Any=None , **lowerCamelCase_ :List[Any] ): '''simple docstring''' snake_case_ : int = [x.strip() for x in open(lowerCamelCase_ ).readlines()] snake_case_ : Dict = [x.strip() for x in open(lowerCamelCase_ ).readlines()][: len(lowerCamelCase_ )] snake_case_ : List[str] = calculate_rouge(lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ) if save_path is not None: save_json(lowerCamelCase_ , lowerCamelCase_ , indent=lowerCamelCase_ ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
8
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Any = tmp_path / """file.csv""" snake_case_ : Any = textwrap.dedent( """\ header1,header2 1,2 10,20 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[int] = tmp_path / """malformed_file.csv""" snake_case_ : int = textwrap.dedent( """\ header1,header2 1,2 10,20, """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : str = tmp_path / """csv_with_image.csv""" snake_case_ : int = textwrap.dedent( F'''\ image {image_file} ''' ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' snake_case_ : int = tmp_path / """csv_with_label.csv""" snake_case_ : Tuple = textwrap.dedent( """\ label good bad good """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = tmp_path / """csv_with_int_list.csv""" snake_case_ : str = textwrap.dedent( """\ int_list 1 2 3 4 5 6 7 8 9 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : int = Csv() snake_case_ : Optional[Any] = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(lowerCamelCase_ , match="""Error tokenizing data""" ): for _ in generator: pass assert any( record.levelname == """ERROR""" and """Failed to read file""" in record.message and os.path.basename(lowerCamelCase_ ) in record.message for record in caplog.records ) @require_pil def UpperCAmelCase ( lowerCamelCase_ :Tuple ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : Tuple = f.read().splitlines()[1] snake_case_ : str = Csv(encoding="""utf-8""" , features=Features({"""image""": Image()} ) ) snake_case_ : Tuple = csv._generate_tables([[csv_file_with_image]] ) snake_case_ : Optional[Any] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""image""" ).type == Image()() snake_case_ : List[str] = pa_table.to_pydict()["""image"""] assert generated_content == [{"path": image_file, "bytes": None}] def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : List[Any] = f.read().splitlines()[1:] snake_case_ : Union[str, Any] = Csv(encoding="""utf-8""" , features=Features({"""label""": ClassLabel(names=["""good""", """bad"""] )} ) ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_label]] ) snake_case_ : Optional[int] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""label""" ).type == ClassLabel(names=["""good""", """bad"""] )() snake_case_ : Union[str, Any] = pa_table.to_pydict()["""label"""] assert generated_content == [ClassLabel(names=["""good""", """bad"""] ).straint(lowerCamelCase_ ) for label in labels] def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : str = Csv(encoding="""utf-8""" , sep=""",""" , converters={"""int_list""": lambda lowerCamelCase_ : [int(lowerCamelCase_ ) for i in x.split()]} ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_int_list]] ) snake_case_ : Tuple = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field("""int_list""" ).type ) snake_case_ : Dict = pa_table.to_pydict()["""int_list"""] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
8
1
'''simple docstring''' from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __A : Tuple = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): lowercase : str = ['input_values', 'padding_mask'] def __init__( self :Optional[int] ,_UpperCamelCase :int = 1 ,_UpperCamelCase :int = 2_4_0_0_0 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :float = None ,_UpperCamelCase :float = None ,**_UpperCamelCase :List[Any] ,): super().__init__(feature_size=_UpperCamelCase ,sampling_rate=_UpperCamelCase ,padding_value=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Dict = chunk_length_s snake_case_ : str = overlap @property def a__ ( self :Any ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def a__ ( self :List[str] ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 ,int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self :Optional[Any] ,_UpperCamelCase :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,_UpperCamelCase :Optional[Union[bool, str, PaddingStrategy]] = None ,_UpperCamelCase :Optional[bool] = False ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :Optional[Union[str, TensorType]] = None ,_UpperCamelCase :Optional[int] = None ,): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) if padding and truncation: raise ValueError("""Both padding and truncation were set. Make sure you only set one.""" ) elif padding is None: # by default let's pad the inputs snake_case_ : Tuple = True snake_case_ : str = bool( isinstance(_UpperCamelCase ,(list, tuple) ) and (isinstance(raw_audio[0] ,(np.ndarray, tuple, list) )) ) if is_batched: snake_case_ : Any = [np.asarray(_UpperCamelCase ,dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_UpperCamelCase ,np.ndarray ): snake_case_ : Optional[int] = np.asarray(_UpperCamelCase ,dtype=np.floataa ) elif isinstance(_UpperCamelCase ,np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): snake_case_ : List[str] = raw_audio.astype(np.floataa ) # always return batch if not is_batched: snake_case_ : Optional[Any] = [np.asarray(_UpperCamelCase ).T] # verify inputs are valid for idx, example in enumerate(_UpperCamelCase ): if example.ndim > 2: raise ValueError(F'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(F'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(F'''Expected stereo audio but example has {example.shape[-1]} channels''' ) snake_case_ : Tuple = None snake_case_ : Optional[Any] = BatchFeature({"""input_values""": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: snake_case_ : Union[str, Any] = min(array.shape[0] for array in raw_audio ) snake_case_ : Dict = int(np.floor(max_length / self.chunk_stride ) ) snake_case_ : Union[str, Any] = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: snake_case_ : Any = max(array.shape[0] for array in raw_audio ) snake_case_ : List[Any] = int(np.ceil(max_length / self.chunk_stride ) ) snake_case_ : Any = (nb_step - 1) * self.chunk_stride + self.chunk_length snake_case_ : Union[str, Any] = """max_length""" else: snake_case_ : int = input_values # normal padding on batch if padded_inputs is None: snake_case_ : Optional[int] = self.pad( _UpperCamelCase ,max_length=_UpperCamelCase ,truncation=_UpperCamelCase ,padding=_UpperCamelCase ,return_attention_mask=_UpperCamelCase ,) if padding: snake_case_ : Tuple = padded_inputs.pop("""attention_mask""" ) snake_case_ : Optional[int] = [] for example in padded_inputs.pop("""input_values""" ): if self.feature_size == 1: snake_case_ : Dict = example[..., None] input_values.append(example.T ) snake_case_ : List[Any] = input_values if return_tensors is not None: snake_case_ : Tuple = padded_inputs.convert_to_tensors(_UpperCamelCase ) return padded_inputs
8
'''simple docstring''' import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple=None ): '''simple docstring''' # set parameter of one layer assert torch_layer.weight.shape == weight.shape, F'''{torch_layer} layer.weight does not match''' snake_case_ : Optional[Any] = nn.Parameter(lowerCamelCase_ ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F'''{torch_layer} layer.bias does not match''' snake_case_ : List[str] = nn.Parameter(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : Optional[Any] = np.asarray(weights[0] ) snake_case_ : int = np.asarray(weights[1] ) snake_case_ : Any = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : List[Any] = np.asarray(weights[0] ) snake_case_ : Optional[int] = np.asarray(weights[1] ) snake_case_ : Union[str, Any] = np.asarray(weights[2] ) snake_case_ : int = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' # layernorm 1 snake_case_ : str = weights[0][0][0] snake_case_ : int = np.asarray(layer_norm_a[0] ) snake_case_ : Optional[Any] = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # lsh weights + output snake_case_ : Tuple = weights[0][1] if len(lowerCamelCase_ ) < 4: set_layer_weights_in_torch_lsh(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) else: set_layer_weights_in_torch_local(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) # intermediate weighs snake_case_ : str = weights[2][0][1][2] # Chunked Feed Forward if len(lowerCamelCase_ ) == 4: snake_case_ : List[Any] = intermediate_weights[2] # layernorm 2 snake_case_ : Tuple = np.asarray(intermediate_weights[0][0] ) snake_case_ : Optional[Any] = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # intermediate dense snake_case_ : Any = np.asarray(intermediate_weights[1][0] ) snake_case_ : List[Any] = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) # intermediate out snake_case_ : List[Any] = np.asarray(intermediate_weights[4][0] ) snake_case_ : Union[str, Any] = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Any ): '''simple docstring''' # reformer model snake_case_ : Dict = torch_model.reformer # word embeds snake_case_ : List[Any] = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(lowerCamelCase_ ) , ) if isinstance(weights[3] , lowerCamelCase_ ): snake_case_ : Tuple = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): snake_case_ : Dict = np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), F'''{position_embeddings[emb_idx]} emb does not match''' snake_case_ : Optional[Any] = nn.Parameter(torch.tensor(lowerCamelCase_ ) ) snake_case_ : List[Any] = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( lowerCamelCase_ ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): snake_case_ : str = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # output layer norm snake_case_ : Optional[Any] = np.asarray(weights[7][0] ) snake_case_ : List[Any] = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # output embeddings snake_case_ : Optional[int] = np.asarray(weights[9][0] ) snake_case_ : Any = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # Initialise PyTorch model snake_case_ : List[str] = ReformerConfig.from_json_file(lowerCamelCase_ ) print(F'''Building PyTorch model from configuration: {config}''' ) snake_case_ : str = ReformerModelWithLMHead(lowerCamelCase_ ) with open(lowerCamelCase_ , """rb""" ) as f: snake_case_ : List[Any] = pickle.load(lowerCamelCase_ )["""weights"""] set_model_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , config.hidden_size ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , lowerCamelCase_ ) if __name__ == "__main__": __A : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--trax_model_pkl_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained Reformer model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __A : List[Any] = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
8
1
'''simple docstring''' import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def UpperCAmelCase ( *lowerCamelCase_ :Dict ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): snake_case_ : Union[str, Any] = list(lowerCamelCase_ ) for i in range(len(lowerCamelCase_ ) ): snake_case_ : int = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def UpperCAmelCase ( lowerCamelCase_ :Exception ): '''simple docstring''' snake_case_ : Any = [ """CUDA out of memory.""", # CUDA OOM """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""", # CUDNN SNAFU """DefaultCPUAllocator: can't allocate memory""", # CPU OOM ] if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def UpperCAmelCase ( lowerCamelCase_ :callable = None , lowerCamelCase_ :int = 1_28 ): '''simple docstring''' if function is None: return functools.partial(lowerCamelCase_ , starting_batch_size=lowerCamelCase_ ) snake_case_ : Union[str, Any] = starting_batch_size def decorator(*lowerCamelCase_ :List[Any] , **lowerCamelCase_ :List[str] ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() snake_case_ : Dict = list(inspect.signature(lowerCamelCase_ ).parameters.keys() ) # Guard against user error if len(lowerCamelCase_ ) < (len(lowerCamelCase_ ) + 1): snake_case_ : Any = """, """.join([F'''{arg}={value}''' for arg, value in zip(params[1:] , args[1:] )] ) raise TypeError( F'''Batch size was passed into `{function.__name__}` as the first argument when called.''' F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError("""No executable batch size found, reached zero.""" ) try: return function(lowerCamelCase_ , *lowerCamelCase_ , **lowerCamelCase_ ) except Exception as e: if should_reduce_batch_size(lowerCamelCase_ ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
8
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[Any] = logging.get_logger(__name__) __A : str = { 'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json', # See all CANINE models at https://huggingface.co/models?filter=canine } class __UpperCamelCase ( lowercase__ ): lowercase : List[Any] = 'canine' def __init__( self :Optional[int] ,_UpperCamelCase :Dict=7_6_8 ,_UpperCamelCase :Union[str, Any]=1_2 ,_UpperCamelCase :int=1_2 ,_UpperCamelCase :int=3_0_7_2 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :Any=0.1 ,_UpperCamelCase :int=0.1 ,_UpperCamelCase :Any=1_6_3_8_4 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :List[str]=0.02 ,_UpperCamelCase :Any=1E-1_2 ,_UpperCamelCase :Tuple=0 ,_UpperCamelCase :List[str]=0xE_0_0_0 ,_UpperCamelCase :Optional[Any]=0xE_0_0_1 ,_UpperCamelCase :str=4 ,_UpperCamelCase :Optional[int]=4 ,_UpperCamelCase :str=8 ,_UpperCamelCase :int=1_6_3_8_4 ,_UpperCamelCase :int=1_2_8 ,**_UpperCamelCase :str ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : List[str] = max_position_embeddings snake_case_ : Union[str, Any] = hidden_size snake_case_ : Dict = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : Tuple = intermediate_size snake_case_ : str = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : Optional[Any] = initializer_range snake_case_ : Optional[int] = type_vocab_size snake_case_ : List[str] = layer_norm_eps # Character config: snake_case_ : Any = downsampling_rate snake_case_ : List[str] = upsampling_kernel_size snake_case_ : int = num_hash_functions snake_case_ : Tuple = num_hash_buckets snake_case_ : Tuple = local_transformer_stride
8
1
'''simple docstring''' import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip __A : Dict = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def UpperCAmelCase ( lowerCamelCase_ :Tuple ): '''simple docstring''' if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :int ): '''simple docstring''' return max(metric_fn(lowerCamelCase_ , lowerCamelCase_ ) for gt in ground_truths ) def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : Dict = [line.strip() for line in open(lowerCamelCase_ , """r""" ).readlines()] snake_case_ : Optional[int] = [] if args.gold_data_mode == "qa": snake_case_ : str = pd.read_csv(lowerCamelCase_ , sep="""\t""" , header=lowerCamelCase_ ) for answer_list in data[1]: snake_case_ : Optional[int] = ast.literal_eval(lowerCamelCase_ ) answers.append(lowerCamelCase_ ) else: snake_case_ : Tuple = [line.strip() for line in open(lowerCamelCase_ , """r""" ).readlines()] snake_case_ : Union[str, Any] = [[reference] for reference in references] snake_case_ : int = 0 for prediction, ground_truths in zip(lowerCamelCase_ , lowerCamelCase_ ): total += 1 em += metric_max_over_ground_truths(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) fa += metric_max_over_ground_truths(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) snake_case_ : Dict = 100.0 * em / total snake_case_ : str = 100.0 * fa / total logger.info(F'''F1: {fa:.2f}''' ) logger.info(F'''EM: {em:.2f}''' ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : Tuple = args.k snake_case_ : str = [line.strip() for line in open(lowerCamelCase_ , """r""" ).readlines()] snake_case_ : Optional[Any] = [line.strip() for line in open(lowerCamelCase_ , """r""" ).readlines()] snake_case_ : Optional[int] = 0 for hypo, reference in zip(lowerCamelCase_ , lowerCamelCase_ ): snake_case_ : Optional[int] = set(hypo.split("""\t""" )[:k] ) snake_case_ : Tuple = set(reference.split("""\t""" ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k snake_case_ : int = 100.0 * em / total logger.info(F'''Precision@{k}: {em: .2f}''' ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :Any ): '''simple docstring''' def strip_title(lowerCamelCase_ :List[str] ): if title.startswith("""\"""" ): snake_case_ : List[Any] = title[1:] if title.endswith("""\"""" ): snake_case_ : Union[str, Any] = title[:-1] return title snake_case_ : Optional[Any] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( lowerCamelCase_ , return_tensors="""pt""" , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , )["""input_ids"""].to(args.device ) snake_case_ : Optional[int] = rag_model.rag.question_encoder(lowerCamelCase_ ) snake_case_ : List[str] = question_enc_outputs[0] snake_case_ : Optional[Any] = rag_model.retriever( lowerCamelCase_ , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors="""pt""" , ) snake_case_ : List[Any] = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) snake_case_ : List[str] = [] for docs in all_docs: snake_case_ : Any = [strip_title(lowerCamelCase_ ) for title in docs["""title"""]] provenance_strings.append("""\t""".join(lowerCamelCase_ ) ) return provenance_strings def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Tuple ): '''simple docstring''' with torch.no_grad(): snake_case_ : Optional[Any] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( lowerCamelCase_ , return_tensors="""pt""" , padding=lowerCamelCase_ , truncation=lowerCamelCase_ ) snake_case_ : List[str] = inputs_dict.input_ids.to(args.device ) snake_case_ : Optional[Any] = inputs_dict.attention_mask.to(args.device ) snake_case_ : Tuple = rag_model.generate( # rag_model overwrites generate lowerCamelCase_ , attention_mask=lowerCamelCase_ , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=lowerCamelCase_ , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) snake_case_ : List[str] = rag_model.retriever.generator_tokenizer.batch_decode(lowerCamelCase_ , skip_special_tokens=lowerCamelCase_ ) if args.print_predictions: for q, a in zip(lowerCamelCase_ , lowerCamelCase_ ): logger.info("""Q: {} - A: {}""".format(lowerCamelCase_ , lowerCamelCase_ ) ) return answers def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : int = argparse.ArgumentParser() parser.add_argument( """--model_type""" , choices=["""rag_sequence""", """rag_token""", """bart"""] , type=lowerCamelCase_ , help=( """RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the""" """ model_name_or_path""" ) , ) parser.add_argument( """--index_name""" , default=lowerCamelCase_ , choices=["""exact""", """compressed""", """legacy"""] , type=lowerCamelCase_ , help="""RAG model retriever type""" , ) parser.add_argument( """--index_path""" , default=lowerCamelCase_ , type=lowerCamelCase_ , help="""Path to the retrieval index""" , ) parser.add_argument("""--n_docs""" , default=5 , type=lowerCamelCase_ , help="""Number of retrieved docs""" ) parser.add_argument( """--model_name_or_path""" , default=lowerCamelCase_ , type=lowerCamelCase_ , required=lowerCamelCase_ , help="""Path to pretrained checkpoints or model identifier from huggingface.co/models""" , ) parser.add_argument( """--eval_mode""" , choices=["""e2e""", """retrieval"""] , default="""e2e""" , type=lowerCamelCase_ , help=( """Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates""" """ precision@k.""" ) , ) parser.add_argument("""--k""" , default=1 , type=lowerCamelCase_ , help="""k for the precision@k calculation""" ) parser.add_argument( """--evaluation_set""" , default=lowerCamelCase_ , type=lowerCamelCase_ , required=lowerCamelCase_ , help="""Path to a file containing evaluation samples""" , ) parser.add_argument( """--gold_data_path""" , default=lowerCamelCase_ , type=lowerCamelCase_ , required=lowerCamelCase_ , help="""Path to a tab-separated file with gold samples""" , ) parser.add_argument( """--gold_data_mode""" , default="""qa""" , type=lowerCamelCase_ , choices=["""qa""", """ans"""] , help=( """Format of the gold data file""" """qa - a single line in the following format: question [tab] answer_list""" """ans - a single line of the gold file contains the expected answer string""" ) , ) parser.add_argument( """--predictions_path""" , type=lowerCamelCase_ , default="""predictions.txt""" , help="""Name of the predictions file, to be stored in the checkpoints directory""" , ) parser.add_argument( """--eval_all_checkpoints""" , action="""store_true""" , help="""Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number""" , ) parser.add_argument( """--eval_batch_size""" , default=8 , type=lowerCamelCase_ , help="""Batch size per GPU/CPU for evaluation.""" , ) parser.add_argument( """--recalculate""" , help="""Recalculate predictions even if the prediction file exists""" , action="""store_true""" , ) parser.add_argument( """--num_beams""" , default=4 , type=lowerCamelCase_ , help="""Number of beams to be used when generating answers""" , ) parser.add_argument("""--min_length""" , default=1 , type=lowerCamelCase_ , help="""Min length of the generated answers""" ) parser.add_argument("""--max_length""" , default=50 , type=lowerCamelCase_ , help="""Max length of the generated answers""" ) parser.add_argument( """--print_predictions""" , action="""store_true""" , help="""If True, prints predictions while evaluating.""" , ) parser.add_argument( """--print_docs""" , action="""store_true""" , help="""If True, prints docs retried while generating.""" , ) snake_case_ : int = parser.parse_args() snake_case_ : Optional[int] = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) return args def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : str = {} if args.model_type is None: snake_case_ : Optional[int] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith("""rag""" ): snake_case_ : List[str] = RagTokenForGeneration if args.model_type == """rag_token""" else RagSequenceForGeneration snake_case_ : Union[str, Any] = args.n_docs if args.index_name is not None: snake_case_ : str = args.index_name if args.index_path is not None: snake_case_ : str = args.index_path else: snake_case_ : List[str] = BartForConditionalGeneration snake_case_ : Optional[Any] = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info("""Evaluate the following checkpoints: %s""" , lowerCamelCase_ ) snake_case_ : Union[str, Any] = get_scores if args.eval_mode == """e2e""" else get_precision_at_k snake_case_ : Dict = evaluate_batch_eae if args.eval_mode == """e2e""" else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info("""Calculating metrics based on an existing predictions file: {}""".format(args.predictions_path ) ) score_fn(lowerCamelCase_ , args.predictions_path , args.gold_data_path ) continue logger.info("""***** Running evaluation for {} *****""".format(lowerCamelCase_ ) ) logger.info(""" Batch size = %d""" , args.eval_batch_size ) logger.info(""" Predictions will be stored under {}""".format(args.predictions_path ) ) if args.model_type.startswith("""rag""" ): snake_case_ : int = RagRetriever.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) snake_case_ : int = model_class.from_pretrained(lowerCamelCase_ , retriever=lowerCamelCase_ , **lowerCamelCase_ ) model.retriever.init_retrieval() else: snake_case_ : List[Any] = model_class.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) model.to(args.device ) with open(args.evaluation_set , """r""" ) as eval_file, open(args.predictions_path , """w""" ) as preds_file: snake_case_ : List[Any] = [] for line in tqdm(lowerCamelCase_ ): questions.append(line.strip() ) if len(lowerCamelCase_ ) == args.eval_batch_size: snake_case_ : Tuple = evaluate_batch_fn(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) preds_file.write("""\n""".join(lowerCamelCase_ ) + """\n""" ) preds_file.flush() snake_case_ : List[Any] = [] if len(lowerCamelCase_ ) > 0: snake_case_ : Union[str, Any] = evaluate_batch_fn(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) preds_file.write("""\n""".join(lowerCamelCase_ ) ) preds_file.flush() score_fn(lowerCamelCase_ , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": __A : Dict = get_args() main(args)
8
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __A : Tuple = logging.get_logger(__name__) __A : List[Any] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } __A : str = { 'vocab_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json' }, 'merges_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt' }, 'tokenizer_config_file': { 'facebook/blenderbot_small-90M': ( 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json' ) }, } __A : Optional[Any] = { 'facebook/blenderbot_small-90M': 512, } class __UpperCamelCase ( lowercase__ ): lowercase : str = VOCAB_FILES_NAMES lowercase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : Dict = BlenderbotSmallTokenizer def __init__( self :str ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :Tuple="<|endoftext|>" ,_UpperCamelCase :int="<|endoftext|>" ,_UpperCamelCase :Dict="<|endoftext|>" ,_UpperCamelCase :Optional[Any]=False ,_UpperCamelCase :List[Any]=True ,**_UpperCamelCase :Any ,): super().__init__( ByteLevelBPETokenizer( vocab=_UpperCamelCase ,merges=_UpperCamelCase ,add_prefix_space=_UpperCamelCase ,trim_offsets=_UpperCamelCase ,) ,bos_token=_UpperCamelCase ,eos_token=_UpperCamelCase ,unk_token=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Any = add_prefix_space def a__ ( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any]=None ): snake_case_ : List[Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def a__ ( self :int ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): snake_case_ : int = [self.sep_token_id] snake_case_ : Tuple = [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]
8
1
'''simple docstring''' import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) __A : int = logging.getLogger() def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[Any] = argparse.ArgumentParser() parser.add_argument("""-f""" ) snake_case_ : int = parser.parse_args() return args.f def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[Any] = {} snake_case_ : Optional[Any] = os.path.join(lowerCamelCase_ , """all_results.json""" ) if os.path.exists(lowerCamelCase_ ): with open(lowerCamelCase_ , """r""" ) as f: snake_case_ : str = json.load(lowerCamelCase_ ) else: raise ValueError(F'''can\'t find {path}''' ) return results def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[str] = torch.cuda.is_available() and torch_device == """cuda""" return is_using_cuda and is_apex_available() __A : Any = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __UpperCamelCase ( lowercase__ ): @classmethod def a__ ( cls :Dict ): # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU snake_case_ : Optional[int] = tempfile.mkdtemp() snake_case_ : Any = os.path.join(cls.tmpdir ,"""default_config.yml""" ) write_basic_config(save_location=cls.configPath ) snake_case_ : List[Any] = ["""accelerate""", """launch""", """--config_file""", cls.configPath] @classmethod def a__ ( cls :int ): shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Optional[int] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.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 --seed=42 --checkpointing_steps epoch --with_tracking '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : Dict = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""glue_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking '''.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,1_0_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""clm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.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} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,4_2 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""mlm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu snake_case_ : Dict = 7 if get_gpu_count() > 1 else 2 snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : str = F''' {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.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} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertLess(result["""train_loss"""] ,0.5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""ner_no_trainer""" ) ) ) @unittest.skip(reason="""Fix me @muellerzr""" ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[str] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : Optional[int] = F''' {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.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} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result["""eval_f1"""] ,2_8 ) self.assertGreaterEqual(result["""eval_exact"""] ,2_8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""qa_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Union[str, Any] = F''' {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Union[str, Any] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""swag_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[Any] = F''' {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.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 --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : int = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_rouge1"""] ,1_0 ) self.assertGreaterEqual(result["""eval_rouge2"""] ,2 ) self.assertGreaterEqual(result["""eval_rougeL"""] ,7 ) self.assertGreaterEqual(result["""eval_rougeLsum"""] ,7 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""summarization_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : Tuple = self.get_auto_remove_tmp_dir() snake_case_ : Optional[Any] = F''' {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Any = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_bleu"""] ,3_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""translation_no_trainer""" ) ) ) @slow def a__ ( self :Optional[Any] ): snake_case_ : List[str] = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCamelCase ) snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_overall_accuracy"""] ,0.10 ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Any ): snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # The base model scores a 25% self.assertGreaterEqual(result["""eval_accuracy"""] ,0.6 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""step_1""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""image_classification_no_trainer""" ) ) )
8
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :list ): '''simple docstring''' if len(lowerCamelCase_ ) <= 1: return lst snake_case_ : Union[str, Any] = 1 while i < len(lowerCamelCase_ ): if lst[i - 1] <= lst[i]: i += 1 else: snake_case_ , snake_case_ : Union[str, Any] = lst[i], lst[i - 1] i -= 1 if i == 0: snake_case_ : int = 1 return lst if __name__ == "__main__": __A : Optional[int] = input('Enter numbers separated by a comma:\n').strip() __A : int = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
8
1
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :int = 10_00 ): '''simple docstring''' snake_case_ , snake_case_ : List[Any] = 1, 1 snake_case_ : Union[str, Any] = [] for i in range(1 , n + 1 ): snake_case_ : int = prev_numerator + 2 * prev_denominator snake_case_ : Any = prev_numerator + prev_denominator if len(str(lowerCamelCase_ ) ) > len(str(lowerCamelCase_ ) ): result.append(lowerCamelCase_ ) snake_case_ : Tuple = numerator snake_case_ : Optional[int] = denominator return len(lowerCamelCase_ ) if __name__ == "__main__": print(F'{solution() = }')
8
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __UpperCamelCase : def __init__( self :Any ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[int]=1_2 ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Optional[int]=True ,_UpperCamelCase :Union[str, Any]=True ,_UpperCamelCase :Dict=True ,_UpperCamelCase :Optional[int]=9_9 ,_UpperCamelCase :Dict=3_2 ,_UpperCamelCase :Union[str, Any]=3_2 ,_UpperCamelCase :Union[str, Any]=2 ,_UpperCamelCase :Optional[Any]=4 ,_UpperCamelCase :List[Any]=3_7 ,_UpperCamelCase :Tuple=0.1 ,_UpperCamelCase :Optional[int]=0.1 ,_UpperCamelCase :int=5_1_2 ,_UpperCamelCase :Tuple=0.02 ,_UpperCamelCase :Any=0 ,_UpperCamelCase :str=None ,): snake_case_ : str = parent snake_case_ : int = batch_size snake_case_ : Union[str, Any] = seq_length snake_case_ : List[Any] = is_training snake_case_ : Union[str, Any] = use_input_mask snake_case_ : List[str] = use_labels snake_case_ : int = vocab_size snake_case_ : Any = hidden_size snake_case_ : List[Any] = projection_dim snake_case_ : Dict = num_hidden_layers snake_case_ : Dict = num_attention_heads snake_case_ : str = intermediate_size snake_case_ : int = dropout snake_case_ : int = attention_dropout snake_case_ : Dict = max_position_embeddings snake_case_ : Union[str, Any] = initializer_range snake_case_ : Dict = scope snake_case_ : Union[str, Any] = bos_token_id def a__ ( self :Any ): snake_case_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case_ : Union[str, Any] = None if self.use_input_mask: snake_case_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: snake_case_ : int = input_mask.numpy() snake_case_ , snake_case_ : Tuple = input_mask.shape snake_case_ : Any = np.random.randint(1 ,seq_length - 1 ,size=(batch_size,) ) for batch_idx, start_index in enumerate(_UpperCamelCase ): snake_case_ : Optional[int] = 1 snake_case_ : List[str] = 0 snake_case_ : Tuple = self.get_config() return config, input_ids, tf.convert_to_tensor(_UpperCamelCase ) def a__ ( self :str ): return BlipTextConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,projection_dim=self.projection_dim ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,dropout=self.dropout ,attention_dropout=self.attention_dropout ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,bos_token_id=self.bos_token_id ,) def a__ ( self :List[Any] ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[int] ): snake_case_ : List[str] = TFBlipTextModel(config=_UpperCamelCase ) snake_case_ : List[Any] = model(_UpperCamelCase ,attention_mask=_UpperCamelCase ,training=_UpperCamelCase ) snake_case_ : Any = model(_UpperCamelCase ,training=_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def a__ ( self :List[str] ): snake_case_ : Union[str, Any] = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ : str = config_and_inputs snake_case_ : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Optional[Any] = (TFBlipTextModel,) if is_tf_available() else () lowercase : int = False lowercase : List[Any] = False lowercase : Dict = False def a__ ( self :List[Any] ): snake_case_ : List[str] = BlipTextModelTester(self ) snake_case_ : Tuple = ConfigTester(self ,config_class=_UpperCamelCase ,hidden_size=3_7 ) def a__ ( self :Union[str, Any] ): self.config_tester.run_common_tests() def a__ ( self :Union[str, Any] ): snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def a__ ( self :Tuple ): pass def a__ ( self :Tuple ): pass @unittest.skip(reason="""Blip does not use inputs_embeds""" ) def a__ ( self :Any ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :Tuple ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :List[Any] ): pass @slow def a__ ( self :Any ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ : Optional[Any] = TFBlipTextModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def a__ ( self :Dict ,_UpperCamelCase :Tuple=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCamelCase )
8
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, is_vision_available, ) __A : Dict = {'configuration_vit': ['VIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTConfig', 'ViTOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = ['ViTFeatureExtractor'] __A : Optional[Any] = ['ViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[int] = [ 'VIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTForImageClassification', 'ViTForMaskedImageModeling', 'ViTModel', 'ViTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[str] = [ 'TFViTForImageClassification', 'TFViTModel', 'TFViTPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Tuple = [ 'FlaxViTForImageClassification', 'FlaxViTModel', 'FlaxViTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_vit import ViTFeatureExtractor from .image_processing_vit import ViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit import ( VIT_PRETRAINED_MODEL_ARCHIVE_LIST, ViTForImageClassification, ViTForMaskedImageModeling, ViTModel, ViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel else: import sys __A : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : int = { 'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'], 'feature_extraction_whisper': ['WhisperFeatureExtractor'], 'processing_whisper': ['WhisperProcessor'], 'tokenization_whisper': ['WhisperTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FlaxWhisperForConditionalGeneration', 'FlaxWhisperModel', 'FlaxWhisperPreTrainedModel', 'FlaxWhisperForAudioClassification', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys __A : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
1
'''simple docstring''' from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig __A : Dict = { 'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json', 'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json', } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[int] = 'ernie_m' lowercase : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self :Optional[Any] ,_UpperCamelCase :int = 2_5_0_0_0_2 ,_UpperCamelCase :int = 7_6_8 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 3_0_7_2 ,_UpperCamelCase :str = "gelu" ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :int = 5_1_4 ,_UpperCamelCase :float = 0.02 ,_UpperCamelCase :int = 1 ,_UpperCamelCase :float = 1E-0_5 ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Optional[int]=0.0 ,**_UpperCamelCase :List[Any] ,): super().__init__(pad_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Optional[int] = vocab_size snake_case_ : Any = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : Union[str, Any] = num_attention_heads snake_case_ : Any = intermediate_size snake_case_ : Any = hidden_act snake_case_ : Tuple = hidden_dropout_prob snake_case_ : Union[str, Any] = attention_probs_dropout_prob snake_case_ : str = max_position_embeddings snake_case_ : int = initializer_range snake_case_ : Optional[Any] = layer_norm_eps snake_case_ : Union[str, Any] = classifier_dropout snake_case_ : Tuple = is_decoder snake_case_ : int = act_dropout
8
'''simple docstring''' import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor __A : Optional[int] = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[str] ,*_UpperCamelCase :str ,**_UpperCamelCase :Optional[int] ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" ,_UpperCamelCase ,) super().__init__(*_UpperCamelCase ,**_UpperCamelCase )
8
1
'''simple docstring''' import logging import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import librosa import torch from datasets import DatasetDict, load_dataset from packaging import version from torch import nn from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForPreTraining, is_apex_available, trainer_utils, ) from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'): __A : Tuple = True from torch.cuda.amp import autocast __A : str = logging.getLogger(__name__) @dataclass class __UpperCamelCase : lowercase : str = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) lowercase : Optional[str] = field( default=lowercase__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) lowercase : Optional[bool] = field( default=lowercase__ , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'} ) lowercase : Optional[bool] = field( default=lowercase__ , metadata={'help': 'Whether to log verbose messages or not.'} , ) lowercase : Optional[float] = field( default=2.0 , metadata={'help': 'Maximum temperature for gumbel softmax.'} ) lowercase : Optional[float] = field( default=0.5 , metadata={'help': 'Minimum temperature for gumbel softmax.'} ) lowercase : Optional[float] = field( default=0.9_9_9_9_9_5 , metadata={'help': 'Decay of gumbel temperature during training.'} ) def UpperCAmelCase ( lowerCamelCase_ :ModelArguments , lowerCamelCase_ :TrainingArguments ): '''simple docstring''' logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) snake_case_ : Dict = logging.WARNING if model_args.verbose_logging: snake_case_ : int = logging.DEBUG elif trainer_utils.is_main_process(training_args.local_rank ): snake_case_ : int = logging.INFO logger.setLevel(lowerCamelCase_ ) @dataclass class __UpperCamelCase : lowercase : str = field( default=lowercase__ , metadata={'help': 'The name of the dataset to use (via the datasets library).'} ) lowercase : Optional[str] = field( default=lowercase__ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) lowercase : Optional[str] = field( default='train' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) lowercase : Optional[str] = field( default='validation' , metadata={ 'help': ( 'The name of the validation data set split to use (via the datasets library). Defaults to \'validation\'' ) } , ) lowercase : Optional[str] = field( default='file' , metadata={'help': 'Column in the dataset that contains speech file path. Defaults to \'file\''} , ) lowercase : bool = field( default=lowercase__ , metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) lowercase : Optional[int] = field( default=1 , metadata={ 'help': 'The percentage of the train set used as validation set in case there\'s no validation split' } , ) lowercase : Optional[int] = field( default=lowercase__ , metadata={'help': 'The number of processes to use for the preprocessing.'} , ) lowercase : Optional[float] = field( default=2_0.0 , metadata={'help': 'Filter audio files that are longer than `max_duration_in_seconds` seconds'} ) @dataclass class __UpperCamelCase : lowercase : WavaVecaForPreTraining lowercase : WavaVecaFeatureExtractor lowercase : Union[bool, str] = "longest" lowercase : Optional[int] = None lowercase : Optional[int] = None def __call__( self :str ,_UpperCamelCase :List[Dict[str, Union[List[int], torch.Tensor]]] ): # reformat list to dict and set to pytorch format snake_case_ : int = self.feature_extractor.pad( _UpperCamelCase ,max_length=self.max_length ,padding=self.padding ,pad_to_multiple_of=self.pad_to_multiple_of ,return_tensors="""pt""" ,) snake_case_ : Tuple = self.model._get_feat_extract_output_lengths(batch["""input_values"""].shape[-1] ) snake_case_ : List[Any] = batch["""input_values"""].shape[0] # make sure that no loss is computed on padded inputs if batch["attention_mask"] is not None: # compute real output lengths according to convolution formula snake_case_ : int = self.model._get_feat_extract_output_lengths(batch["""attention_mask"""].sum(-1 ) ).to( torch.long ) snake_case_ : List[str] = torch.zeros( (batch_size, mask_indices_seq_length) ,dtype=torch.long ,device=batch["""input_values"""].device ) # these two operations makes sure that all values # before the output lengths indices are attended to snake_case_ : Tuple = 1 snake_case_ : Dict = attention_mask.flip([-1] ).cumsum(-1 ).flip([-1] ).bool() # sample randomly masked indices snake_case_ : str = _compute_mask_indices( (batch_size, mask_indices_seq_length) ,self.model.config.mask_time_prob ,self.model.config.mask_time_length ,attention_mask=_UpperCamelCase ,min_masks=2 ,) return batch class __UpperCamelCase ( lowercase__ ): def __init__( self :List[Any] ,*_UpperCamelCase :List[Any] ,_UpperCamelCase :Dict=1 ,_UpperCamelCase :Optional[int]=0 ,_UpperCamelCase :Optional[Any]=1.0 ,**_UpperCamelCase :Tuple ): super().__init__(*_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : List[str] = 0 snake_case_ : List[str] = max_gumbel_temp snake_case_ : Union[str, Any] = min_gumbel_temp snake_case_ : List[str] = gumbel_temp_decay def a__ ( self :int ,_UpperCamelCase :nn.Module ,_UpperCamelCase :Dict[str, Union[torch.Tensor, Any]] ): model.train() snake_case_ : Any = self._prepare_inputs(_UpperCamelCase ) if self.use_amp: with autocast(): snake_case_ : Tuple = self.compute_loss(_UpperCamelCase ,_UpperCamelCase ) else: snake_case_ : Optional[int] = self.compute_loss(_UpperCamelCase ,_UpperCamelCase ) if self.args.n_gpu > 1 or self.deepspeed: if model.module.config.ctc_loss_reduction == "mean": snake_case_ : Dict = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": snake_case_ : Union[str, Any] = loss.sum() / (inputs["""mask_time_indices"""]).sum() else: raise ValueError(F'''{model.config.ctc_loss_reduction} is not valid. Choose one of [\'mean\', \'sum\']''' ) if self.args.gradient_accumulation_steps > 1: snake_case_ : Optional[Any] = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(_UpperCamelCase ).backward() elif self.use_apex: with amp.scale_loss(_UpperCamelCase ,self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(_UpperCamelCase ) else: loss.backward() self.num_update_step += 1 # make sure gumbel softmax temperature is decayed if self.args.n_gpu > 1 or self.deepspeed: model.module.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step ,self.min_gumbel_temp ) ) else: model.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step ,self.min_gumbel_temp ) ) return loss.detach() def UpperCAmelCase ( ): '''simple docstring''' # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case_ : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) snake_case_ , snake_case_ , snake_case_ : str = parser.parse_args_into_dataclasses() configure_logger(lowerCamelCase_ , lowerCamelCase_ ) # Downloading and loading a dataset from the hub. snake_case_ : Optional[int] = load_dataset(data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) if "validation" not in datasets.keys(): # make sure only "validation" and "train" keys remain" snake_case_ : Tuple = DatasetDict() snake_case_ : Optional[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F'''{data_args.train_split_name}[:{data_args.validation_split_percentage}%]''' , cache_dir=model_args.cache_dir , ) snake_case_ : Optional[int] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F'''{data_args.train_split_name}[{data_args.validation_split_percentage}%:]''' , cache_dir=model_args.cache_dir , ) else: # make sure only "validation" and "train" keys remain" snake_case_ : List[Any] = DatasetDict() snake_case_ : Optional[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split="""validation""" , cache_dir=model_args.cache_dir , ) snake_case_ : List[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F'''{data_args.train_split_name}''' , cache_dir=model_args.cache_dir , ) # only normalized-inputs-training is supported snake_case_ : Optional[int] = WavaVecaFeatureExtractor.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , do_normalize=lowerCamelCase_ ) def prepare_dataset(lowerCamelCase_ :List[Any] ): # check that all files have the correct sampling rate snake_case_ , snake_case_ : str = librosa.load(batch[data_args.speech_file_column] , sr=feature_extractor.sampling_rate ) return batch # load audio files into numpy arrays snake_case_ : Union[str, Any] = datasets.map( lowerCamelCase_ , num_proc=data_args.preprocessing_num_workers , remove_columns=datasets["""train"""].column_names ) # filter audio files that are too long snake_case_ : Optional[Any] = vectorized_datasets.filter( lambda lowerCamelCase_ : len(data["""speech"""] ) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate ) ) def normalize(lowerCamelCase_ :Dict ): return feature_extractor(batch["""speech"""] , sampling_rate=feature_extractor.sampling_rate ) # normalize and transform to `BatchFeatures` snake_case_ : Optional[Any] = vectorized_datasets.map( lowerCamelCase_ , batched=lowerCamelCase_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , remove_columns=vectorized_datasets["""train"""].column_names , ) # pretraining is only supported for "newer" stable layer norm architecture # apply_spec_augment has to be True, mask_feature_prob has to be 0.0 snake_case_ : Any = WavaVecaConfig.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , gradient_checkpointing=training_args.gradient_checkpointing , ) if not config.do_stable_layer_norm or config.feat_extract_norm != "layer": raise ValueError( """PreTraining is only supported for ``config.do_stable_layer_norm=True`` and""" """ ``config.feat_extract_norm='layer'""" ) snake_case_ : Optional[Any] = WavaVecaForPreTraining(lowerCamelCase_ ) snake_case_ : List[Any] = DataCollatorForWavaVecaPretraining(model=lowerCamelCase_ , feature_extractor=lowerCamelCase_ ) snake_case_ : Optional[int] = WavaVecaPreTrainer( model=lowerCamelCase_ , data_collator=lowerCamelCase_ , args=lowerCamelCase_ , train_dataset=vectorized_datasets["""train"""] , eval_dataset=vectorized_datasets["""validation"""] , tokenizer=lowerCamelCase_ , max_gumbel_temp=model_args.max_gumbel_temperature , min_gumbel_temp=model_args.min_gumbel_temperature , gumbel_temp_decay=model_args.gumbel_temperature_decay , ) trainer.train() if __name__ == "__main__": main()
8
'''simple docstring''' import re def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[Any] = re.compile( R"""^(?:0|94|\+94|0{2}94)""" R"""7(0|1|2|4|5|6|7|8)""" R"""(-| |)""" R"""\d{7}$""" ) return bool(re.search(lowerCamelCase_ , lowerCamelCase_ ) ) if __name__ == "__main__": __A : int = '0094702343221' print(is_sri_lankan_phone_number(phone))
8
1
'''simple docstring''' from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class __UpperCamelCase : lowercase : int lowercase : int class __UpperCamelCase : def __init__( self :List[Any] ,_UpperCamelCase :int ): snake_case_ : list[list[Edge]] = [[] for _ in range(_UpperCamelCase )] snake_case_ : List[str] = size def __getitem__( self :Optional[Any] ,_UpperCamelCase :int ): return iter(self._graph[vertex] ) @property def a__ ( self :int ): return self._size def a__ ( self :Union[str, Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ): if weight not in (0, 1): raise ValueError("""Edge weight must be either 0 or 1.""" ) if to_vertex < 0 or to_vertex >= self.size: raise ValueError("""Vertex indexes must be in [0; size).""" ) self._graph[from_vertex].append(Edge(_UpperCamelCase ,_UpperCamelCase ) ) def a__ ( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ): snake_case_ : Optional[int] = deque([start_vertex] ) snake_case_ : list[int | None] = [None] * self.size snake_case_ : int = 0 while queue: snake_case_ : Optional[Any] = queue.popleft() snake_case_ : str = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: snake_case_ : int = current_distance + edge.weight snake_case_ : str = distances[edge.destination_vertex] if ( isinstance(_UpperCamelCase ,_UpperCamelCase ) and new_distance >= dest_vertex_distance ): continue snake_case_ : Union[str, Any] = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex ) else: queue.append(edge.destination_vertex ) if distances[finish_vertex] is None: raise ValueError("""No path from start_vertex to finish_vertex.""" ) return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
8
'''simple docstring''' from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import BaseOutput, is_torch_available, is_transformers_available @dataclass class __UpperCamelCase ( lowercase__ ): lowercase : Union[List[PIL.Image.Image], np.ndarray] lowercase : Optional[List[bool]] if is_transformers_available() and is_torch_available(): from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
8
1
'''simple docstring''' import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class __UpperCamelCase : @property def a__ ( self :Optional[Any] ): return self.get_dummy_input() @property def a__ ( self :int ): if self.block_type == "down": return (4, 3_2, 1_6, 1_6) elif self.block_type == "mid": return (4, 3_2, 3_2, 3_2) elif self.block_type == "up": return (4, 3_2, 6_4, 6_4) raise ValueError(F'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.''' ) def a__ ( self :List[Any] ,_UpperCamelCase :str=True ,_UpperCamelCase :int=False ,_UpperCamelCase :Any=False ,_UpperCamelCase :List[str]=False ,): snake_case_ : str = 4 snake_case_ : int = 3_2 snake_case_ : Optional[Any] = (3_2, 3_2) snake_case_ : Optional[int] = torch.manual_seed(0 ) snake_case_ : Any = torch.device(_UpperCamelCase ) snake_case_ : Optional[Any] = (batch_size, num_channels) + sizes snake_case_ : List[str] = randn_tensor(_UpperCamelCase ,generator=_UpperCamelCase ,device=_UpperCamelCase ) snake_case_ : Union[str, Any] = {"""hidden_states""": hidden_states} if include_temb: snake_case_ : Any = 1_2_8 snake_case_ : Union[str, Any] = randn_tensor((batch_size, temb_channels) ,generator=_UpperCamelCase ,device=_UpperCamelCase ) if include_res_hidden_states_tuple: snake_case_ : Dict = torch.manual_seed(1 ) snake_case_ : List[str] = (randn_tensor(_UpperCamelCase ,generator=_UpperCamelCase ,device=_UpperCamelCase ),) if include_encoder_hidden_states: snake_case_ : Dict = floats_tensor((batch_size, 3_2, 3_2) ).to(_UpperCamelCase ) if include_skip_sample: snake_case_ : Union[str, Any] = randn_tensor(((batch_size, 3) + sizes) ,generator=_UpperCamelCase ,device=_UpperCamelCase ) return dummy_input def a__ ( self :Optional[Any] ): snake_case_ : Optional[Any] = { """in_channels""": 3_2, """out_channels""": 3_2, """temb_channels""": 1_2_8, } if self.block_type == "up": snake_case_ : Any = 3_2 if self.block_type == "mid": init_dict.pop("""out_channels""" ) snake_case_ : Tuple = self.dummy_input return init_dict, inputs_dict def a__ ( self :Union[str, Any] ,_UpperCamelCase :int ): snake_case_ , snake_case_ : Union[str, Any] = self.prepare_init_args_and_inputs_for_common() snake_case_ : List[str] = self.block_class(**_UpperCamelCase ) unet_block.to(_UpperCamelCase ) unet_block.eval() with torch.no_grad(): snake_case_ : Optional[int] = unet_block(**_UpperCamelCase ) if isinstance(_UpperCamelCase ,_UpperCamelCase ): snake_case_ : str = output[0] self.assertEqual(output.shape ,self.output_shape ) snake_case_ : Tuple = output[0, -1, -3:, -3:] snake_case_ : Optional[Any] = torch.tensor(_UpperCamelCase ).to(_UpperCamelCase ) assert torch_all_close(output_slice.flatten() ,_UpperCamelCase ,atol=5E-3 ) @unittest.skipIf(torch_device == """mps""" ,"""Training is not supported in mps""" ) def a__ ( self :Optional[int] ): snake_case_ , snake_case_ : List[str] = self.prepare_init_args_and_inputs_for_common() snake_case_ : List[Any] = self.block_class(**_UpperCamelCase ) model.to(_UpperCamelCase ) model.train() snake_case_ : Union[str, Any] = model(**_UpperCamelCase ) if isinstance(_UpperCamelCase ,_UpperCamelCase ): snake_case_ : Tuple = output[0] snake_case_ : Optional[int] = torch.device(_UpperCamelCase ) snake_case_ : Any = randn_tensor(output.shape ,device=_UpperCamelCase ) snake_case_ : Union[str, Any] = torch.nn.functional.mse_loss(_UpperCamelCase ,_UpperCamelCase ) loss.backward()
8
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , unittest.TestCase ): lowercase : Dict = StableDiffusionInpaintPipeline lowercase : str = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS lowercase : Union[str, Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS lowercase : Dict = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowercase : Optional[int] = frozenset([] ) def a__ ( self :Any ): torch.manual_seed(0 ) snake_case_ : Optional[int] = UNetaDConditionModel( block_out_channels=(3_2, 6_4) ,layers_per_block=2 ,sample_size=3_2 ,in_channels=9 ,out_channels=4 ,down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,cross_attention_dim=3_2 ,attention_head_dim=(2, 4) ,use_linear_projection=_UpperCamelCase ,) snake_case_ : Tuple = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) torch.manual_seed(0 ) snake_case_ : List[str] = AutoencoderKL( block_out_channels=[3_2, 6_4] ,in_channels=3 ,out_channels=3 ,down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,latent_channels=4 ,sample_size=1_2_8 ,) torch.manual_seed(0 ) snake_case_ : Optional[int] = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=3_2 ,intermediate_size=3_7 ,layer_norm_eps=1E-0_5 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1_0_0_0 ,hidden_act="""gelu""" ,projection_dim=5_1_2 ,) snake_case_ : Tuple = CLIPTextModel(_UpperCamelCase ) snake_case_ : Optional[int] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) snake_case_ : str = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def a__ ( self :str ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Union[str, Any]=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched snake_case_ : List[Any] = floats_tensor((1, 3, 3_2, 3_2) ,rng=random.Random(_UpperCamelCase ) ).to(_UpperCamelCase ) snake_case_ : int = image.cpu().permute(0 ,2 ,3 ,1 )[0] snake_case_ : List[str] = Image.fromarray(np.uinta(_UpperCamelCase ) ).convert("""RGB""" ).resize((6_4, 6_4) ) snake_case_ : Optional[Any] = Image.fromarray(np.uinta(image + 4 ) ).convert("""RGB""" ).resize((6_4, 6_4) ) if str(_UpperCamelCase ).startswith("""mps""" ): snake_case_ : Optional[Any] = torch.manual_seed(_UpperCamelCase ) else: snake_case_ : Optional[int] = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) snake_case_ : int = { """prompt""": """A painting of a squirrel eating a burger""", """image""": init_image, """mask_image""": mask_image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def a__ ( self :Any ): snake_case_ : Union[str, Any] = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case_ : Optional[Any] = self.get_dummy_components() snake_case_ : Dict = StableDiffusionInpaintPipeline(**_UpperCamelCase ) snake_case_ : List[str] = sd_pipe.to(_UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ : Union[str, Any] = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ : Tuple = sd_pipe(**_UpperCamelCase ).images snake_case_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case_ : Dict = np.array([0.47_27, 0.57_35, 0.39_41, 0.54_46, 0.59_26, 0.43_94, 0.50_62, 0.46_54, 0.44_76] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def a__ ( self :Any ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :List[Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self :Tuple ): snake_case_ : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : List[str] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench.npy""" ) snake_case_ : str = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : Optional[Any] = StableDiffusionInpaintPipeline.from_pretrained(_UpperCamelCase ,safety_checker=_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() snake_case_ : Optional[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : List[str] = torch.manual_seed(0 ) snake_case_ : Dict = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""np""" ,) snake_case_ : Union[str, Any] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 9E-3 def a__ ( self :Tuple ): snake_case_ : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : List[str] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench_fp16.npy""" ) snake_case_ : Optional[int] = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : List[str] = StableDiffusionInpaintPipeline.from_pretrained( _UpperCamelCase ,torch_dtype=torch.floataa ,safety_checker=_UpperCamelCase ,) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() snake_case_ : Optional[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : List[Any] = torch.manual_seed(0 ) snake_case_ : Any = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""np""" ,) snake_case_ : List[str] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 5E-1 def a__ ( self :Union[str, Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case_ : Optional[int] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : int = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : Dict = PNDMScheduler.from_pretrained(_UpperCamelCase ,subfolder="""scheduler""" ) snake_case_ : List[Any] = StableDiffusionInpaintPipeline.from_pretrained( _UpperCamelCase ,safety_checker=_UpperCamelCase ,scheduler=_UpperCamelCase ,torch_dtype=torch.floataa ,) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case_ : List[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : Optional[int] = torch.manual_seed(0 ) snake_case_ : Tuple = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,num_inference_steps=2 ,output_type="""np""" ,) snake_case_ : Any = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 1_0**9
8
1
'''simple docstring''' 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 AutoImageProcessor, ViTImageProcessor 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_image_processing import CustomImageProcessor # noqa E402 __A : Optional[Any] = get_tests_dir('fixtures') class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :Union[str, Any] ): # A mock response for an HTTP head request to emulate server down snake_case_ : Union[str, Any] = mock.Mock() snake_case_ : Optional[int] = 5_0_0 snake_case_ : str = {} snake_case_ : str = HTTPError snake_case_ : int = {} # Download this model to make sure it's in the cache. snake_case_ : Optional[int] = ViTImageProcessor.from_pretrained("""hf-internal-testing/tiny-random-vit""" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("""requests.Session.request""" ,return_value=_UpperCamelCase ) as mock_head: snake_case_ : Optional[Any] = ViTImageProcessor.from_pretrained("""hf-internal-testing/tiny-random-vit""" ) # This check we did call the fake head request mock_head.assert_called() def a__ ( self :Dict ): # This test is for deprecated behavior and can be removed in v5 snake_case_ : Any = ViTImageProcessor.from_pretrained( """https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json""" ) def a__ ( self :str ): with self.assertRaises(_UpperCamelCase ): # config is in subfolder, the following should not work without specifying the subfolder snake_case_ : Optional[Any] = AutoImageProcessor.from_pretrained("""hf-internal-testing/stable-diffusion-all-variants""" ) snake_case_ : List[str] = AutoImageProcessor.from_pretrained( """hf-internal-testing/stable-diffusion-all-variants""" ,subfolder="""feature_extractor""" ) self.assertIsNotNone(_UpperCamelCase ) @is_staging_test class __UpperCamelCase ( unittest.TestCase ): @classmethod def a__ ( cls :Dict ): snake_case_ : List[str] = TOKEN HfFolder.save_token(_UpperCamelCase ) @classmethod def a__ ( cls :str ): try: delete_repo(token=cls._token ,repo_id="""test-image-processor""" ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id="""valid_org/test-image-processor-org""" ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id="""test-dynamic-image-processor""" ) except HTTPError: pass def a__ ( self :List[str] ): snake_case_ : List[str] = ViTImageProcessor.from_pretrained(_UpperCamelCase ) image_processor.push_to_hub("""test-image-processor""" ,use_auth_token=self._token ) snake_case_ : str = ViTImageProcessor.from_pretrained(F'''{USER}/test-image-processor''' ) for k, v in image_processor.__dict__.items(): self.assertEqual(_UpperCamelCase ,getattr(_UpperCamelCase ,_UpperCamelCase ) ) # Reset repo delete_repo(token=self._token ,repo_id="""test-image-processor""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained( _UpperCamelCase ,repo_id="""test-image-processor""" ,push_to_hub=_UpperCamelCase ,use_auth_token=self._token ) snake_case_ : List[Any] = ViTImageProcessor.from_pretrained(F'''{USER}/test-image-processor''' ) for k, v in image_processor.__dict__.items(): self.assertEqual(_UpperCamelCase ,getattr(_UpperCamelCase ,_UpperCamelCase ) ) def a__ ( self :List[str] ): snake_case_ : Optional[int] = ViTImageProcessor.from_pretrained(_UpperCamelCase ) image_processor.push_to_hub("""valid_org/test-image-processor""" ,use_auth_token=self._token ) snake_case_ : Optional[int] = ViTImageProcessor.from_pretrained("""valid_org/test-image-processor""" ) for k, v in image_processor.__dict__.items(): self.assertEqual(_UpperCamelCase ,getattr(_UpperCamelCase ,_UpperCamelCase ) ) # Reset repo delete_repo(token=self._token ,repo_id="""valid_org/test-image-processor""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained( _UpperCamelCase ,repo_id="""valid_org/test-image-processor-org""" ,push_to_hub=_UpperCamelCase ,use_auth_token=self._token ) snake_case_ : List[Any] = ViTImageProcessor.from_pretrained("""valid_org/test-image-processor-org""" ) for k, v in image_processor.__dict__.items(): self.assertEqual(_UpperCamelCase ,getattr(_UpperCamelCase ,_UpperCamelCase ) ) def a__ ( self :Tuple ): CustomImageProcessor.register_for_auto_class() snake_case_ : Tuple = CustomImageProcessor.from_pretrained(_UpperCamelCase ) image_processor.push_to_hub("""test-dynamic-image-processor""" ,use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( image_processor.auto_map ,{"""AutoImageProcessor""": """custom_image_processing.CustomImageProcessor"""} ,) snake_case_ : List[str] = AutoImageProcessor.from_pretrained( F'''{USER}/test-dynamic-image-processor''' ,trust_remote_code=_UpperCamelCase ) # Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module self.assertEqual(new_image_processor.__class__.__name__ ,"""CustomImageProcessor""" )
8
'''simple docstring''' import collections import os import re from pathlib import Path __A : Dict = 'src/transformers' # Matches is_xxx_available() __A : Dict = re.compile(r'is\_([a-z_]*)_available()') # Catches a one-line _import_struct = {xxx} __A : Any = re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __A : Tuple = re.compile(r'\s+"\S*":\s+\[([^\]]*)\]') # Catches a line if not is_foo_available __A : Optional[Any] = re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)') # Catches a line _import_struct["bla"].append("foo") __A : Optional[int] = re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __A : List[Any] = re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]') # Catches a line with an object between quotes and a comma: "MyModel", __A : Union[str, Any] = re.compile(r'^\s+"([^"]+)",') # Catches a line with objects between brackets only: ["foo", "bar"], __A : int = re.compile(r'^\s+\[([^\]]+)\]') # Catches a line with from foo import bar, bla, boo __A : int = re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') # Catches a line with try: __A : List[Any] = re.compile(r'^\s*try:') # Catches a line with else: __A : Any = re.compile(r'^\s*else:') def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' if _re_test_backend.search(lowerCamelCase_ ) is None: return None snake_case_ : Tuple = [b[0] for b in _re_backend.findall(lowerCamelCase_ )] backends.sort() return "_and_".join(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] ): '''simple docstring''' with open(lowerCamelCase_ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: snake_case_ : str = f.readlines() snake_case_ : List[Any] = 0 while line_index < len(lowerCamelCase_ ) and not lines[line_index].startswith("""_import_structure = {""" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(lowerCamelCase_ ): return None # First grab the objects without a specific backend in _import_structure snake_case_ : Union[str, Any] = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: snake_case_ : str = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(lowerCamelCase_ ): snake_case_ : Optional[int] = _re_one_line_import_struct.search(lowerCamelCase_ ).groups()[0] snake_case_ : Union[str, Any] = re.findall(R"""\[([^\]]+)\]""" , lowerCamelCase_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue snake_case_ : Any = _re_import_struct_key_value.search(lowerCamelCase_ ) if single_line_import_search is not None: snake_case_ : Optional[int] = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(""", """ ) if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) line_index += 1 snake_case_ : Union[str, Any] = {"""none""": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("""if TYPE_CHECKING""" ): # If the line is an if not is_backend_available, we grab all objects associated. snake_case_ : List[str] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : Tuple = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Dict = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): snake_case_ : List[Any] = lines[line_index] if _re_import_struct_add_one.search(lowerCamelCase_ ) is not None: objects.append(_re_import_struct_add_one.search(lowerCamelCase_ ).groups()[0] ) elif _re_import_struct_add_many.search(lowerCamelCase_ ) is not None: snake_case_ : Optional[int] = _re_import_struct_add_many.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : List[str] = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_between_brackets.search(lowerCamelCase_ ) is not None: snake_case_ : List[str] = _re_between_brackets.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : Any = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_quote_object.search(lowerCamelCase_ ) is not None: objects.append(_re_quote_object.search(lowerCamelCase_ ).groups()[0] ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) elif line.startswith(""" """ * 12 + """\"""" ): objects.append(line[13:-3] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend snake_case_ : List[Any] = [] while ( line_index < len(lowerCamelCase_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): snake_case_ : Union[str, Any] = lines[line_index] snake_case_ : Union[str, Any] = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 snake_case_ : Dict = {"""none""": objects} # Let's continue with backend-specific objects while line_index < len(lowerCamelCase_ ): # If the line is an if is_backend_available, we grab all objects associated. snake_case_ : Optional[Any] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : str = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Any = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): snake_case_ : Dict = lines[line_index] snake_case_ : Any = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 12 ): objects.append(line[12:-2] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 return import_dict_objects, type_hint_objects def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :List[str] ): '''simple docstring''' def find_duplicates(lowerCamelCase_ :Union[str, Any] ): return [k for k, v in collections.Counter(lowerCamelCase_ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] snake_case_ : Optional[int] = [] for key in import_dict_objects.keys(): snake_case_ : int = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) snake_case_ : List[str] = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): snake_case_ : str = """base imports""" if key == """none""" else F'''{key} backend''' errors.append(F'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Tuple = [] for root, _, files in os.walk(lowerCamelCase_ ): if "__init__.py" in files: snake_case_ : Any = os.path.join(lowerCamelCase_ , """__init__.py""" ) snake_case_ : Dict = parse_init(lowerCamelCase_ ) if objects is not None: snake_case_ : Any = analyze_results(*lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: snake_case_ : Tuple = F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append("""\n""".join(lowerCamelCase_ ) ) if len(lowerCamelCase_ ) > 0: raise ValueError("""\n\n""".join(lowerCamelCase_ ) ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Union[str, Any] = [] for path, directories, files in os.walk(lowerCamelCase_ ): for folder in directories: # Ignore private modules if folder.startswith("""_""" ): directories.remove(lowerCamelCase_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(lowerCamelCase_ ) / folder).glob("""*.py""" ) ) ) == 0: continue snake_case_ : Tuple = str((Path(lowerCamelCase_ ) / folder).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(os.path.sep , """.""" ) submodules.append(lowerCamelCase_ ) for fname in files: if fname == "__init__.py": continue snake_case_ : Dict = str((Path(lowerCamelCase_ ) / fname).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(lowerCamelCase_ ) return submodules __A : List[Any] = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', 'models.esm.openfold_utils', ] def UpperCAmelCase ( ): '''simple docstring''' # This is to make sure the transformers module imported is the one in the repo. from transformers.utils import direct_transformers_import snake_case_ : Union[str, Any] = direct_transformers_import(lowerCamelCase_ ) snake_case_ : List[str] = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(lowerCamelCase_ , """__init__.py""" ) , """r""" ) as f: snake_case_ : str = f.read() import_structure_keys.update(set(re.findall(R"""import_structure\[\"([^\"]*)\"\]""" , lowerCamelCase_ ) ) ) snake_case_ : Dict = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(lowerCamelCase_ ) > 0: snake_case_ : str = """\n""".join(F'''- {module}''' for module in module_not_registered ) raise ValueError( """The following submodules are not properly registed in the main init of Transformers:\n""" F'''{list_of_modules}\n''' """Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.""" ) if __name__ == "__main__": check_all_inits() check_submodules()
8
1
'''simple docstring''' import argparse import json import os import fairseq import torch from fairseq.data import Dictionary # Register SEW's fairseq modules from sew_asapp import tasks # noqa: F401 from transformers import ( SEWConfig, SEWForCTC, SEWModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() __A : Any = logging.get_logger(__name__) __A : int = { 'post_extract_proj': 'feature_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.upsample.0': 'encoder.upsample.projection', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'layer_norm', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' for attribute in key.split(""".""" ): snake_case_ : Any = getattr(lowerCamelCase_ , lowerCamelCase_ ) if weight_type is not None: snake_case_ : Tuple = getattr(lowerCamelCase_ , lowerCamelCase_ ).shape else: snake_case_ : Union[str, Any] = 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": snake_case_ : Union[str, Any] = value elif weight_type == "weight_g": snake_case_ : Dict = value elif weight_type == "weight_v": snake_case_ : int = value elif weight_type == "bias": snake_case_ : List[str] = value else: snake_case_ : List[Any] = value logger.info(F'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :str , lowerCamelCase_ :Optional[int] ): '''simple docstring''' snake_case_ : Optional[int] = [] snake_case_ : List[str] = fairseq_model.state_dict() snake_case_ : str = hf_model.sew.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): snake_case_ : Tuple = False if "conv_layers" in name: load_conv_layer( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , hf_model.config.feat_extract_norm == """group""" , ) snake_case_ : Tuple = True else: for key, mapped_key in MAPPING.items(): snake_case_ : Optional[int] = """sew.""" + 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]: snake_case_ : Optional[Any] = True if "*" in mapped_key: snake_case_ : Tuple = name.split(lowerCamelCase_ )[0].split(""".""" )[-2] snake_case_ : str = mapped_key.replace("""*""" , lowerCamelCase_ ) if "weight_g" in name: snake_case_ : Optional[Any] = """weight_g""" elif "weight_v" in name: snake_case_ : Tuple = """weight_v""" elif "weight" in name: snake_case_ : Union[str, Any] = """weight""" elif "bias" in name: snake_case_ : Union[str, Any] = """bias""" else: snake_case_ : int = None set_recursively(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) continue if not is_used: unused_weights.append(lowerCamelCase_ ) logger.warning(F'''Unused weights: {unused_weights}''' ) def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : str = full_name.split("""conv_layers.""" )[-1] snake_case_ : str = name.split(""".""" ) snake_case_ : Union[str, Any] = int(items[0] ) snake_case_ : Any = 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.''' ) snake_case_ : Optional[Any] = 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.''' ) snake_case_ : List[Any] = 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." ) snake_case_ : Dict = 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.''' ) snake_case_ : List[str] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :List[Any] ): '''simple docstring''' snake_case_ : Union[str, Any] = SEWConfig() if is_finetuned: snake_case_ : Optional[int] = model.wav_encoder.wav_model.cfg else: snake_case_ : int = model.cfg snake_case_ : List[Any] = fs_config.conv_bias snake_case_ : str = eval(fs_config.conv_feature_layers ) snake_case_ : Dict = [x[0] for x in conv_layers] snake_case_ : Tuple = [x[1] for x in conv_layers] snake_case_ : Any = [x[2] for x in conv_layers] snake_case_ : Tuple = """gelu""" snake_case_ : int = """layer""" if fs_config.extractor_mode == """layer_norm""" else """group""" snake_case_ : List[Any] = 0.0 snake_case_ : Any = fs_config.activation_fn.name snake_case_ : Union[str, Any] = fs_config.encoder_embed_dim snake_case_ : Tuple = 0.02 snake_case_ : str = fs_config.encoder_ffn_embed_dim snake_case_ : List[str] = 1E-5 snake_case_ : Tuple = fs_config.encoder_layerdrop snake_case_ : Tuple = fs_config.encoder_attention_heads snake_case_ : str = fs_config.conv_pos_groups snake_case_ : List[Any] = fs_config.conv_pos snake_case_ : Optional[int] = len(lowerCamelCase_ ) snake_case_ : Tuple = fs_config.encoder_layers snake_case_ : str = fs_config.squeeze_factor # take care of any params that are overridden by the Wav2VecCtc model if is_finetuned: snake_case_ : int = model.cfg snake_case_ : Optional[Any] = fs_config.final_dropout snake_case_ : List[Any] = fs_config.layerdrop snake_case_ : List[str] = fs_config.activation_dropout snake_case_ : int = fs_config.mask_prob > 0 or fs_config.mask_channel_prob > 0 snake_case_ : int = fs_config.attention_dropout snake_case_ : Tuple = fs_config.dropout_input snake_case_ : List[str] = fs_config.dropout snake_case_ : Optional[Any] = fs_config.mask_channel_length snake_case_ : Dict = fs_config.mask_channel_prob snake_case_ : Dict = fs_config.mask_length snake_case_ : Optional[int] = fs_config.mask_prob snake_case_ : Any = """Wav2Vec2FeatureExtractor""" snake_case_ : Tuple = """Wav2Vec2CTCTokenizer""" return config @torch.no_grad() def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Dict=None , lowerCamelCase_ :List[str]=True ): '''simple docstring''' if is_finetuned: snake_case_ , snake_case_ , snake_case_ : Any = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) else: snake_case_ , snake_case_ , snake_case_ : Optional[int] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) if config_path is not None: snake_case_ : Tuple = SEWConfig.from_pretrained(lowerCamelCase_ ) else: snake_case_ : Any = convert_config(model[0] , lowerCamelCase_ ) snake_case_ : str = model[0].eval() snake_case_ : int = True if config.feat_extract_norm == """layer""" else False snake_case_ : str = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , ) if is_finetuned: if dict_path: snake_case_ : List[Any] = Dictionary.load(lowerCamelCase_ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq snake_case_ : str = target_dict.pad_index snake_case_ : Tuple = target_dict.bos_index snake_case_ : Tuple = target_dict.pad_index snake_case_ : Union[str, Any] = target_dict.bos_index snake_case_ : str = target_dict.eos_index snake_case_ : Optional[Any] = len(target_dict.symbols ) snake_case_ : List[str] = os.path.join(lowerCamelCase_ , """vocab.json""" ) if not os.path.isdir(lowerCamelCase_ ): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(lowerCamelCase_ ) ) return os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) with open(lowerCamelCase_ , """w""" , encoding="""utf-8""" ) as vocab_handle: json.dump(target_dict.indices , lowerCamelCase_ ) snake_case_ : Optional[Any] = WavaVecaCTCTokenizer( lowerCamelCase_ , 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=lowerCamelCase_ , ) snake_case_ : Any = WavaVecaProcessor(feature_extractor=lowerCamelCase_ , tokenizer=lowerCamelCase_ ) processor.save_pretrained(lowerCamelCase_ ) snake_case_ : Union[str, Any] = SEWForCTC(lowerCamelCase_ ) else: snake_case_ : Any = SEWModel(lowerCamelCase_ ) feature_extractor.save_pretrained(lowerCamelCase_ ) recursively_load_weights(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) hf_model.save_pretrained(lowerCamelCase_ ) if __name__ == "__main__": __A : Tuple = 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( '--is_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) __A : Union[str, Any] = parser.parse_args() convert_sew_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, args.is_finetuned )
8
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __UpperCamelCase ( unittest.TestCase ): def __init__( self :List[Any] ,_UpperCamelCase :List[str] ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Union[str, Any]=3 ,_UpperCamelCase :Any=1_8 ,_UpperCamelCase :Optional[Any]=3_0 ,_UpperCamelCase :List[str]=4_0_0 ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :List[Any]=True ,): snake_case_ : List[str] = size if size is not None else {"""height""": 1_8, """width""": 1_8} snake_case_ : Union[str, Any] = parent snake_case_ : str = batch_size snake_case_ : List[Any] = num_channels snake_case_ : Tuple = image_size snake_case_ : int = min_resolution snake_case_ : int = max_resolution snake_case_ : Union[str, Any] = do_resize snake_case_ : Optional[Any] = size snake_case_ : Any = apply_ocr def a__ ( self :Union[str, Any] ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Tuple = LayoutLMvaImageProcessor if is_pytesseract_available() else None def a__ ( self :List[Any] ): snake_case_ : Union[str, Any] = LayoutLMvaImageProcessingTester(self ) @property def a__ ( self :int ): return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self :Any ): snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase ,"""do_resize""" ) ) self.assertTrue(hasattr(_UpperCamelCase ,"""size""" ) ) self.assertTrue(hasattr(_UpperCamelCase ,"""apply_ocr""" ) ) def a__ ( self :int ): snake_case_ : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 1_8, """width""": 1_8} ) snake_case_ : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict ,size=4_2 ) self.assertEqual(image_processor.size ,{"""height""": 4_2, """width""": 4_2} ) def a__ ( self :Optional[Any] ): pass def a__ ( self :Union[str, Any] ): # Initialize image_processing snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ : List[str] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,Image.Image ) # Test not batched input snake_case_ : List[str] = image_processing(image_inputs[0] ,return_tensors="""pt""" ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) self.assertIsInstance(encoding.words ,_UpperCamelCase ) self.assertIsInstance(encoding.boxes ,_UpperCamelCase ) # Test batched snake_case_ : List[Any] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :Tuple ): # Initialize image_processing snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ,numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,np.ndarray ) # Test not batched input snake_case_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Any = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :Optional[Any] ): # Initialize image_processing snake_case_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ : Optional[int] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ,torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,torch.Tensor ) # Test not batched input snake_case_ : Tuple = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Union[str, Any] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :List[Any] ): # with apply_OCR = True snake_case_ : Any = LayoutLMvaImageProcessor() from datasets import load_dataset snake_case_ : List[Any] = load_dataset("""hf-internal-testing/fixtures_docvqa""" ,split="""test""" ) snake_case_ : str = Image.open(ds[0]["""file"""] ).convert("""RGB""" ) snake_case_ : Dict = image_processing(_UpperCamelCase ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 2_2_4, 2_2_4) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 snake_case_ : Tuple = [["""11:14""", """to""", """11:39""", """a.m""", """11:39""", """to""", """11:44""", """a.m.""", """11:44""", """a.m.""", """to""", """12:25""", """p.m.""", """12:25""", """to""", """12:58""", """p.m.""", """12:58""", """to""", """4:00""", """p.m.""", """2:00""", """to""", """5:00""", """p.m.""", """Coffee""", """Break""", """Coffee""", """will""", """be""", """served""", """for""", """men""", """and""", """women""", """in""", """the""", """lobby""", """adjacent""", """to""", """exhibit""", """area.""", """Please""", """move""", """into""", """exhibit""", """area.""", """(Exhibits""", """Open)""", """TRRF""", """GENERAL""", """SESSION""", """(PART""", """|)""", """Presiding:""", """Lee""", """A.""", """Waller""", """TRRF""", """Vice""", """President""", """“Introductory""", """Remarks”""", """Lee""", """A.""", """Waller,""", """TRRF""", """Vice""", """Presi-""", """dent""", """Individual""", """Interviews""", """with""", """TRRF""", """Public""", """Board""", """Members""", """and""", """Sci-""", """entific""", """Advisory""", """Council""", """Mem-""", """bers""", """Conducted""", """by""", """TRRF""", """Treasurer""", """Philip""", """G.""", """Kuehn""", """to""", """get""", """answers""", """which""", """the""", """public""", """refrigerated""", """warehousing""", """industry""", """is""", """looking""", """for.""", """Plus""", """questions""", """from""", """the""", """floor.""", """Dr.""", """Emil""", """M.""", """Mrak,""", """University""", """of""", """Cal-""", """ifornia,""", """Chairman,""", """TRRF""", """Board;""", """Sam""", """R.""", """Cecil,""", """University""", """of""", """Georgia""", """College""", """of""", """Agriculture;""", """Dr.""", """Stanley""", """Charm,""", """Tufts""", """University""", """School""", """of""", """Medicine;""", """Dr.""", """Robert""", """H.""", """Cotton,""", """ITT""", """Continental""", """Baking""", """Company;""", """Dr.""", """Owen""", """Fennema,""", """University""", """of""", """Wis-""", """consin;""", """Dr.""", """Robert""", """E.""", """Hardenburg,""", """USDA.""", """Questions""", """and""", """Answers""", """Exhibits""", """Open""", """Capt.""", """Jack""", """Stoney""", """Room""", """TRRF""", """Scientific""", """Advisory""", """Council""", """Meeting""", """Ballroom""", """Foyer"""]] # noqa: E231 snake_case_ : Any = [[[1_4_1, 5_7, 2_1_4, 6_9], [2_2_8, 5_8, 2_5_2, 6_9], [1_4_1, 7_5, 2_1_6, 8_8], [2_3_0, 7_9, 2_8_0, 8_8], [1_4_2, 2_6_0, 2_1_8, 2_7_3], [2_3_0, 2_6_1, 2_5_5, 2_7_3], [1_4_3, 2_7_9, 2_1_8, 2_9_0], [2_3_1, 2_8_2, 2_9_0, 2_9_1], [1_4_3, 3_4_2, 2_1_8, 3_5_4], [2_3_1, 3_4_5, 2_8_9, 3_5_5], [2_0_2, 3_6_2, 2_2_7, 3_7_3], [1_4_3, 3_7_9, 2_2_0, 3_9_2], [2_3_1, 3_8_2, 2_9_1, 3_9_4], [1_4_4, 7_1_4, 2_2_0, 7_2_6], [2_3_1, 7_1_5, 2_5_6, 7_2_6], [1_4_4, 7_3_2, 2_2_0, 7_4_5], [2_3_2, 7_3_6, 2_9_1, 7_4_7], [1_4_4, 7_6_9, 2_1_8, 7_8_2], [2_3_1, 7_7_0, 2_5_6, 7_8_2], [1_4_1, 7_8_8, 2_0_2, 8_0_1], [2_1_5, 7_9_1, 2_7_4, 8_0_4], [1_4_3, 8_2_6, 2_0_4, 8_3_8], [2_1_5, 8_2_6, 2_4_0, 8_3_8], [1_4_2, 8_4_4, 2_0_2, 8_5_7], [2_1_5, 8_4_7, 2_7_4, 8_5_9], [3_3_4, 5_7, 4_2_7, 6_9], [4_4_0, 5_7, 5_2_2, 6_9], [3_6_9, 7_5, 4_6_1, 8_8], [4_6_9, 7_5, 5_1_6, 8_8], [5_2_8, 7_6, 5_6_2, 8_8], [5_7_0, 7_6, 6_6_7, 8_8], [6_7_5, 7_5, 7_1_1, 8_7], [7_2_1, 7_9, 7_7_8, 8_8], [7_8_9, 7_5, 8_4_0, 8_8], [3_6_9, 9_7, 4_7_0, 1_0_7], [4_8_4, 9_4, 5_0_7, 1_0_6], [5_1_8, 9_4, 5_6_2, 1_0_7], [5_7_6, 9_4, 6_5_5, 1_1_0], [6_6_8, 9_4, 7_9_2, 1_0_9], [8_0_4, 9_5, 8_2_9, 1_0_7], [3_6_9, 1_1_3, 4_6_5, 1_2_5], [4_7_7, 1_1_6, 5_4_7, 1_2_5], [5_6_2, 1_1_3, 6_5_8, 1_2_5], [6_7_1, 1_1_6, 7_4_8, 1_2_5], [7_6_1, 1_1_3, 8_1_1, 1_2_5], [3_6_9, 1_3_1, 4_6_5, 1_4_3], [4_7_7, 1_3_3, 5_4_8, 1_4_3], [5_6_3, 1_3_0, 6_9_8, 1_4_5], [7_1_0, 1_3_0, 8_0_2, 1_4_6], [3_3_6, 1_7_1, 4_1_2, 1_8_3], [4_2_3, 1_7_1, 5_7_2, 1_8_3], [5_8_2, 1_7_0, 7_1_6, 1_8_4], [7_2_8, 1_7_1, 8_1_7, 1_8_7], [8_2_9, 1_7_1, 8_4_4, 1_8_6], [3_3_8, 1_9_7, 4_8_2, 2_1_2], [5_0_7, 1_9_6, 5_5_7, 2_0_9], [5_6_9, 1_9_6, 5_9_5, 2_0_8], [6_1_0, 1_9_6, 7_0_2, 2_0_9], [5_0_5, 2_1_4, 5_8_3, 2_2_6], [5_9_5, 2_1_4, 6_5_6, 2_2_7], [6_7_0, 2_1_5, 8_0_7, 2_2_7], [3_3_5, 2_5_9, 5_4_3, 2_7_4], [5_5_6, 2_5_9, 7_0_8, 2_7_2], [3_7_2, 2_7_9, 4_2_2, 2_9_1], [4_3_5, 2_7_9, 4_6_0, 2_9_1], [4_7_4, 2_7_9, 5_7_4, 2_9_2], [5_8_7, 2_7_8, 6_6_4, 2_9_1], [6_7_6, 2_7_8, 7_3_8, 2_9_1], [7_5_1, 2_7_9, 8_3_4, 2_9_1], [3_7_2, 2_9_8, 4_3_4, 3_1_0], [3_3_5, 3_4_1, 4_8_3, 3_5_4], [4_9_7, 3_4_1, 6_5_5, 3_5_4], [6_6_7, 3_4_1, 7_2_8, 3_5_4], [7_4_0, 3_4_1, 8_2_5, 3_5_4], [3_3_5, 3_6_0, 4_3_0, 3_7_2], [4_4_2, 3_6_0, 5_3_4, 3_7_2], [5_4_5, 3_5_9, 6_8_7, 3_7_2], [6_9_7, 3_6_0, 7_5_4, 3_7_2], [7_6_5, 3_6_0, 8_2_3, 3_7_3], [3_3_4, 3_7_8, 4_2_8, 3_9_1], [4_4_0, 3_7_8, 5_7_7, 3_9_4], [5_9_0, 3_7_8, 7_0_5, 3_9_1], [7_2_0, 3_7_8, 8_0_1, 3_9_1], [3_3_4, 3_9_7, 4_0_0, 4_0_9], [3_7_0, 4_1_6, 5_2_9, 4_2_9], [5_4_4, 4_1_6, 5_7_6, 4_3_2], [5_8_7, 4_1_6, 6_6_5, 4_2_8], [6_7_7, 4_1_6, 8_1_4, 4_2_9], [3_7_2, 4_3_5, 4_5_2, 4_5_0], [4_6_5, 4_3_4, 4_9_5, 4_4_7], [5_1_1, 4_3_4, 6_0_0, 4_4_7], [6_1_1, 4_3_6, 6_3_7, 4_4_7], [6_4_9, 4_3_6, 6_9_4, 4_5_1], [7_0_5, 4_3_8, 8_2_4, 4_4_7], [3_6_9, 4_5_3, 4_5_2, 4_6_6], [4_6_4, 4_5_4, 5_0_9, 4_6_6], [5_2_2, 4_5_3, 6_1_1, 4_6_9], [6_2_5, 4_5_3, 7_9_2, 4_6_9], [3_7_0, 4_7_2, 5_5_6, 4_8_8], [5_7_0, 4_7_2, 6_8_4, 4_8_7], [6_9_7, 4_7_2, 7_1_8, 4_8_5], [7_3_2, 4_7_2, 8_3_5, 4_8_8], [3_6_9, 4_9_0, 4_1_1, 5_0_3], [4_2_5, 4_9_0, 4_8_4, 5_0_3], [4_9_6, 4_9_0, 6_3_5, 5_0_6], [6_4_5, 4_9_0, 7_0_7, 5_0_3], [7_1_8, 4_9_1, 7_6_1, 5_0_3], [7_7_1, 4_9_0, 8_4_0, 5_0_3], [3_3_6, 5_1_0, 3_7_4, 5_2_1], [3_8_8, 5_1_0, 4_4_7, 5_2_2], [4_6_0, 5_1_0, 4_8_9, 5_2_1], [5_0_3, 5_1_0, 5_8_0, 5_2_2], [5_9_2, 5_0_9, 7_3_6, 5_2_5], [7_4_5, 5_0_9, 7_7_0, 5_2_2], [7_8_1, 5_0_9, 8_4_0, 5_2_2], [3_3_8, 5_2_8, 4_3_4, 5_4_1], [4_4_8, 5_2_8, 5_9_6, 5_4_1], [6_0_9, 5_2_7, 6_8_7, 5_4_0], [7_0_0, 5_2_8, 7_9_2, 5_4_1], [3_3_6, 5_4_6, 3_9_7, 5_5_9], [4_0_7, 5_4_6, 4_3_1, 5_5_9], [4_4_3, 5_4_6, 5_2_5, 5_6_0], [5_3_7, 5_4_6, 6_8_0, 5_6_2], [6_8_8, 5_4_6, 7_1_4, 5_5_9], [7_2_2, 5_4_6, 8_3_7, 5_6_2], [3_3_6, 5_6_5, 4_4_9, 5_8_1], [4_6_1, 5_6_5, 4_8_5, 5_7_7], [4_9_7, 5_6_5, 6_6_5, 5_8_1], [6_8_1, 5_6_5, 7_1_8, 5_7_7], [7_3_2, 5_6_5, 8_3_7, 5_8_0], [3_3_7, 5_8_4, 4_3_8, 5_9_7], [4_5_2, 5_8_3, 5_2_1, 5_9_6], [5_3_5, 5_8_4, 6_7_7, 5_9_9], [6_9_0, 5_8_3, 7_8_7, 5_9_6], [8_0_1, 5_8_3, 8_2_5, 5_9_6], [3_3_8, 6_0_2, 4_7_8, 6_1_5], [4_9_2, 6_0_2, 5_3_0, 6_1_4], [5_4_3, 6_0_2, 6_3_8, 6_1_5], [6_5_0, 6_0_2, 6_7_6, 6_1_4], [6_8_8, 6_0_2, 7_8_8, 6_1_5], [8_0_2, 6_0_2, 8_4_3, 6_1_4], [3_3_7, 6_2_1, 5_0_2, 6_3_3], [5_1_6, 6_2_1, 6_1_5, 6_3_7], [6_2_9, 6_2_1, 7_7_4, 6_3_6], [7_8_9, 6_2_1, 8_2_7, 6_3_3], [3_3_7, 6_3_9, 4_1_8, 6_5_2], [4_3_2, 6_4_0, 5_7_1, 6_5_3], [5_8_7, 6_3_9, 7_3_1, 6_5_5], [7_4_3, 6_3_9, 7_6_9, 6_5_2], [7_8_0, 6_3_9, 8_4_1, 6_5_2], [3_3_8, 6_5_8, 4_4_0, 6_7_3], [4_5_5, 6_5_8, 4_9_1, 6_7_0], [5_0_8, 6_5_8, 6_0_2, 6_7_1], [6_1_6, 6_5_8, 6_3_8, 6_7_0], [6_5_4, 6_5_8, 8_3_5, 6_7_4], [3_3_7, 6_7_7, 4_2_9, 6_8_9], [3_3_7, 7_1_4, 4_8_2, 7_2_6], [4_9_5, 7_1_4, 5_4_8, 7_2_6], [5_6_1, 7_1_4, 6_8_3, 7_2_6], [3_3_8, 7_7_0, 4_6_1, 7_8_2], [4_7_4, 7_6_9, 5_5_4, 7_8_5], [4_8_9, 7_8_8, 5_6_2, 8_0_3], [5_7_6, 7_8_8, 6_4_3, 8_0_1], [6_5_6, 7_8_7, 7_5_1, 8_0_4], [7_6_4, 7_8_8, 8_4_4, 8_0_1], [3_3_4, 8_2_5, 4_2_1, 8_3_8], [4_3_0, 8_2_4, 5_7_4, 8_3_8], [5_8_4, 8_2_4, 7_2_3, 8_4_1], [3_3_5, 8_4_4, 4_5_0, 8_5_7], [4_6_4, 8_4_3, 5_8_3, 8_6_0], [6_2_8, 8_6_2, 7_5_5, 8_7_5], [7_6_9, 8_6_1, 8_4_8, 8_7_8]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,_UpperCamelCase ) self.assertListEqual(encoding.boxes ,_UpperCamelCase ) # with apply_OCR = False snake_case_ : Dict = LayoutLMvaImageProcessor(apply_ocr=_UpperCamelCase ) snake_case_ : Optional[int] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 2_2_4, 2_2_4) )
8
1
'''simple docstring''' from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
8
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : List[Any] = generate_pascal_triangle(lowerCamelCase_ ) for row_idx in range(lowerCamelCase_ ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=""" """ ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=""" """ ) else: print(triangle[row_idx][col_idx] , end="""""" ) print() def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError("""The input value of 'num_rows' should be 'int'""" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( """The input value of 'num_rows' should be greater than or equal to 0""" ) snake_case_ : list[list[int]] = [] for current_row_idx in range(lowerCamelCase_ ): snake_case_ : List[str] = populate_current_row(lowerCamelCase_ , lowerCamelCase_ ) triangle.append(lowerCamelCase_ ) return triangle def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Union[str, Any] = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 snake_case_ , snake_case_ : Optional[Any] = 1, 1 for current_col_idx in range(1 , lowerCamelCase_ ): calculate_current_element( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) return current_row def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :list[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , ): '''simple docstring''' snake_case_ : Union[str, Any] = triangle[current_row_idx - 1][current_col_idx - 1] snake_case_ : List[Any] = triangle[current_row_idx - 1][current_col_idx] snake_case_ : Optional[int] = above_to_left_elt + above_to_right_elt def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError("""The input value of 'num_rows' should be 'int'""" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( """The input value of 'num_rows' should be greater than or equal to 0""" ) snake_case_ : list[list[int]] = [[1]] for row_index in range(1 , lowerCamelCase_ ): snake_case_ : Optional[Any] = [0] + result[-1] + [0] snake_case_ : Dict = row_index + 1 # Calculate the number of distinct elements in a row snake_case_ : Any = sum(divmod(lowerCamelCase_ , 2 ) ) snake_case_ : Tuple = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] snake_case_ : Optional[int] = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() snake_case_ : str = row_first_half + row_second_half result.append(lowerCamelCase_ ) return result def UpperCAmelCase ( ): '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(lowerCamelCase_ :Callable , lowerCamelCase_ :int ) -> None: snake_case_ : Dict = F'''{func.__name__}({value})''' snake_case_ : Dict = timeit(F'''__main__.{call}''' , setup="""import __main__""" ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(F'''{call:38} -- {timing:.4f} seconds''' ) for value in range(15 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(lowerCamelCase_ , lowerCamelCase_ ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
8
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : str = logging.get_logger(__name__) __A : List[str] = { 'google/switch-base-8': 'https://huggingface.co/google/switch-base-8/blob/main/config.json', } class __UpperCamelCase ( lowercase__ ): lowercase : List[Any] = 'switch_transformers' lowercase : Union[str, Any] = ['past_key_values'] lowercase : Optional[int] = {'hidden_size': 'd_model', 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers'} def __init__( self :List[Any] ,_UpperCamelCase :Any=3_2_1_2_8 ,_UpperCamelCase :Tuple=7_6_8 ,_UpperCamelCase :Optional[int]=6_4 ,_UpperCamelCase :Dict=2_0_4_8 ,_UpperCamelCase :List[str]=6_4 ,_UpperCamelCase :Dict=1_2 ,_UpperCamelCase :Dict=3 ,_UpperCamelCase :Optional[int]=1_2 ,_UpperCamelCase :List[Any]=3 ,_UpperCamelCase :Tuple=1_2 ,_UpperCamelCase :Any=8 ,_UpperCamelCase :int=False ,_UpperCamelCase :List[str]=0.01 ,_UpperCamelCase :Tuple="float32" ,_UpperCamelCase :Tuple=False ,_UpperCamelCase :Any=3_2 ,_UpperCamelCase :Union[str, Any]=1_2_8 ,_UpperCamelCase :Dict=0.1 ,_UpperCamelCase :Optional[Any]=1E-6 ,_UpperCamelCase :Tuple=0.0_01 ,_UpperCamelCase :Any=0.0_01 ,_UpperCamelCase :Dict=1.0 ,_UpperCamelCase :List[str]="relu" ,_UpperCamelCase :List[Any]=True ,_UpperCamelCase :Any=False ,_UpperCamelCase :str=True ,_UpperCamelCase :Union[str, Any]=0 ,_UpperCamelCase :List[Any]=1 ,**_UpperCamelCase :Dict ,): snake_case_ : int = vocab_size snake_case_ : Optional[int] = d_model snake_case_ : Tuple = d_kv snake_case_ : List[Any] = d_ff snake_case_ : Optional[int] = num_sparse_encoder_layers snake_case_ : List[Any] = num_layers snake_case_ : Optional[Any] = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry snake_case_ : Optional[int] = num_sparse_decoder_layers # This tells us, each how many encoder layer we'll have to set a sparse layer. if self.num_sparse_encoder_layers > 0: snake_case_ : Any = self.num_layers // self.num_sparse_encoder_layers else: snake_case_ : Optional[Any] = self.num_layers # HACK: this will create 0 sparse layers # This tells us, each how many encoder layer we'll have to set a sparse layer. if self.num_sparse_decoder_layers > 0: snake_case_ : Tuple = self.num_decoder_layers // self.num_sparse_decoder_layers else: snake_case_ : Optional[int] = self.num_decoder_layers # HACK: this will create 0 sparse layers snake_case_ : Union[str, Any] = num_heads snake_case_ : int = num_experts snake_case_ : Union[str, Any] = expert_capacity snake_case_ : Optional[int] = router_bias snake_case_ : int = router_jitter_noise if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(F'''`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}''' ) snake_case_ : Any = router_dtype snake_case_ : int = router_ignore_padding_tokens snake_case_ : Optional[Any] = relative_attention_num_buckets snake_case_ : Optional[int] = relative_attention_max_distance snake_case_ : Tuple = dropout_rate snake_case_ : Union[str, Any] = layer_norm_epsilon snake_case_ : List[str] = initializer_factor snake_case_ : int = feed_forward_proj snake_case_ : Optional[Any] = use_cache snake_case_ : Union[str, Any] = add_router_probs snake_case_ : int = router_z_loss_coef snake_case_ : Optional[int] = router_aux_loss_coef snake_case_ : Optional[Any] = self.feed_forward_proj.split("""-""" ) snake_case_ : Dict = act_info[-1] snake_case_ : Any = act_info[0] == """gated""" if len(_UpperCamelCase ) > 1 and act_info[0] != "gated" or len(_UpperCamelCase ) > 2: raise ValueError( F'''`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.''' """Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. """ """'gated-gelu' or 'relu'""" ) # for backwards compatibility if feed_forward_proj == "gated-gelu": snake_case_ : List[Any] = """gelu_new""" super().__init__( pad_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,is_encoder_decoder=_UpperCamelCase ,**_UpperCamelCase ,)
8
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class __UpperCamelCase ( unittest.TestCase ): @slow def a__ ( self :Dict ): snake_case_ : Optional[Any] = XLMRobertaModel.from_pretrained("""xlm-roberta-base""" ) snake_case_ : Optional[int] = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house snake_case_ : Tuple = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim snake_case_ : Dict = torch.tensor( [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): snake_case_ : Tuple = model(_UpperCamelCase )["""last_hidden_state"""].detach() self.assertEqual(output.shape ,_UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,_UpperCamelCase ,atol=1E-3 ) ) @slow def a__ ( self :Union[str, Any] ): snake_case_ : List[Any] = XLMRobertaModel.from_pretrained("""xlm-roberta-large""" ) snake_case_ : Dict = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house snake_case_ : List[Any] = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim snake_case_ : Any = torch.tensor( [[-0.06_99, -0.03_18, 0.07_05, -0.12_41, 0.09_99, -0.05_20, 0.10_04, -0.18_38, -0.47_04, 0.14_37, 0.08_21, 0.01_26]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): snake_case_ : str = model(_UpperCamelCase )["""last_hidden_state"""].detach() self.assertEqual(output.shape ,_UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,_UpperCamelCase ,atol=1E-3 ) )
8
1
'''simple docstring''' import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( """kwargs, expected""" , [ ({"""num_shards""": 0, """max_num_jobs""": 1}, []), ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10 )]), ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(lowerCamelCase_ , i + 1 ) for i in range(10 )]), ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1 )]), ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :List[Any] ): '''simple docstring''' snake_case_ : List[Any] = _distribute_shards(**lowerCamelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, max_num_jobs, expected""" , [ ({"""foo""": 0}, 10, [{"""foo""": 0}]), ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]), ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]), ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]), ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]), ] , ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Tuple = _split_gen_kwargs(lowerCamelCase_ , lowerCamelCase_ ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, expected""" , [ ({"""foo""": 0}, 1), ({"""shards""": [0]}, 1), ({"""shards""": [0, 1, 2, 3]}, 4), ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4), ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4), ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError), ] , ) def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :List[Any] ): '''simple docstring''' if expected is RuntimeError: with pytest.raises(lowerCamelCase_ ): _number_of_shards_in_gen_kwargs(lowerCamelCase_ ) else: snake_case_ : Dict = _number_of_shards_in_gen_kwargs(lowerCamelCase_ ) assert out == expected
8
'''simple docstring''' from __future__ import annotations from collections.abc import Callable def UpperCAmelCase ( lowerCamelCase_ :Callable[[int | float], int | float] , lowerCamelCase_ :int | float , lowerCamelCase_ :int | float , lowerCamelCase_ :int = 1_00 , ): '''simple docstring''' snake_case_ : Tuple = x_start snake_case_ : Optional[int] = fnc(lowerCamelCase_ ) snake_case_ : Optional[int] = 0.0 for _ in range(lowerCamelCase_ ): # Approximates small segments of curve as linear and solve # for trapezoidal area snake_case_ : int = (x_end - x_start) / steps + xa snake_case_ : Union[str, Any] = fnc(lowerCamelCase_ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step snake_case_ : Any = xa snake_case_ : str = fxa return area if __name__ == "__main__": def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return x**3 + x**2 print('f(x) = x^3 + x^2') print('The area between the curve, x = -5, x = 5 and the x axis is:') __A : List[str] = 10 while i <= 100_000: print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}') i *= 10
8
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : List[str] = {'configuration_opt': ['OPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'OPTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[Any] = [ 'OPT_PRETRAINED_MODEL_ARCHIVE_LIST', 'OPTForCausalLM', 'OPTModel', 'OPTPreTrainedModel', 'OPTForSequenceClassification', 'OPTForQuestionAnswering', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = ['TFOPTForCausalLM', 'TFOPTModel', 'TFOPTPreTrainedModel'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Tuple = [ 'FlaxOPTForCausalLM', 'FlaxOPTModel', 'FlaxOPTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys __A : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
'''simple docstring''' import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) __A : int = logging.getLogger() def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[Any] = argparse.ArgumentParser() parser.add_argument("""-f""" ) snake_case_ : int = parser.parse_args() return args.f def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[Any] = {} snake_case_ : Optional[Any] = os.path.join(lowerCamelCase_ , """all_results.json""" ) if os.path.exists(lowerCamelCase_ ): with open(lowerCamelCase_ , """r""" ) as f: snake_case_ : str = json.load(lowerCamelCase_ ) else: raise ValueError(F'''can\'t find {path}''' ) return results def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[str] = torch.cuda.is_available() and torch_device == """cuda""" return is_using_cuda and is_apex_available() __A : Any = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __UpperCamelCase ( lowercase__ ): @classmethod def a__ ( cls :Dict ): # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU snake_case_ : Optional[int] = tempfile.mkdtemp() snake_case_ : Any = os.path.join(cls.tmpdir ,"""default_config.yml""" ) write_basic_config(save_location=cls.configPath ) snake_case_ : List[Any] = ["""accelerate""", """launch""", """--config_file""", cls.configPath] @classmethod def a__ ( cls :int ): shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Optional[int] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.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 --seed=42 --checkpointing_steps epoch --with_tracking '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : Dict = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""glue_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking '''.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,1_0_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""clm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.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} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,4_2 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""mlm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu snake_case_ : Dict = 7 if get_gpu_count() > 1 else 2 snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : str = F''' {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.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} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertLess(result["""train_loss"""] ,0.5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""ner_no_trainer""" ) ) ) @unittest.skip(reason="""Fix me @muellerzr""" ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[str] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : Optional[int] = F''' {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.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} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result["""eval_f1"""] ,2_8 ) self.assertGreaterEqual(result["""eval_exact"""] ,2_8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""qa_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Union[str, Any] = F''' {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Union[str, Any] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""swag_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[Any] = F''' {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.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 --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : int = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_rouge1"""] ,1_0 ) self.assertGreaterEqual(result["""eval_rouge2"""] ,2 ) self.assertGreaterEqual(result["""eval_rougeL"""] ,7 ) self.assertGreaterEqual(result["""eval_rougeLsum"""] ,7 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""summarization_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : Tuple = self.get_auto_remove_tmp_dir() snake_case_ : Optional[Any] = F''' {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Any = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_bleu"""] ,3_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""translation_no_trainer""" ) ) ) @slow def a__ ( self :Optional[Any] ): snake_case_ : List[str] = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCamelCase ) snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_overall_accuracy"""] ,0.10 ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Any ): snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # The base model scores a 25% self.assertGreaterEqual(result["""eval_accuracy"""] ,0.6 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""step_1""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""image_classification_no_trainer""" ) ) )
8
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[str] = logging.get_logger(__name__) __A : Optional[Any] = { 'microsoft/biogpt': 'https://huggingface.co/microsoft/biogpt/resolve/main/config.json', # See all BioGPT models at https://huggingface.co/models?filter=biogpt } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[Any] = 'biogpt' def __init__( self :List[str] ,_UpperCamelCase :Any=4_2_3_8_4 ,_UpperCamelCase :int=1_0_2_4 ,_UpperCamelCase :List[str]=2_4 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :str=4_0_9_6 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :Optional[Any]=0.1 ,_UpperCamelCase :str=0.1 ,_UpperCamelCase :int=1_0_2_4 ,_UpperCamelCase :Tuple=0.02 ,_UpperCamelCase :List[str]=1E-1_2 ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :int=True ,_UpperCamelCase :Optional[Any]=0.0 ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :str=1 ,_UpperCamelCase :Tuple=0 ,_UpperCamelCase :Optional[Any]=2 ,**_UpperCamelCase :Dict ,): snake_case_ : Tuple = vocab_size snake_case_ : Dict = max_position_embeddings snake_case_ : int = hidden_size snake_case_ : List[Any] = num_hidden_layers snake_case_ : Any = num_attention_heads snake_case_ : str = intermediate_size snake_case_ : Optional[int] = hidden_act snake_case_ : Optional[Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : List[str] = initializer_range snake_case_ : Optional[Any] = layer_norm_eps snake_case_ : Optional[Any] = scale_embedding snake_case_ : Union[str, Any] = use_cache snake_case_ : List[str] = layerdrop snake_case_ : Tuple = activation_dropout super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase )
8
'''simple docstring''' from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __A : Tuple = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): lowercase : str = ['input_values', 'padding_mask'] def __init__( self :Optional[int] ,_UpperCamelCase :int = 1 ,_UpperCamelCase :int = 2_4_0_0_0 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :float = None ,_UpperCamelCase :float = None ,**_UpperCamelCase :List[Any] ,): super().__init__(feature_size=_UpperCamelCase ,sampling_rate=_UpperCamelCase ,padding_value=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Dict = chunk_length_s snake_case_ : str = overlap @property def a__ ( self :Any ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def a__ ( self :List[str] ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 ,int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self :Optional[Any] ,_UpperCamelCase :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,_UpperCamelCase :Optional[Union[bool, str, PaddingStrategy]] = None ,_UpperCamelCase :Optional[bool] = False ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :Optional[Union[str, TensorType]] = None ,_UpperCamelCase :Optional[int] = None ,): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) if padding and truncation: raise ValueError("""Both padding and truncation were set. Make sure you only set one.""" ) elif padding is None: # by default let's pad the inputs snake_case_ : Tuple = True snake_case_ : str = bool( isinstance(_UpperCamelCase ,(list, tuple) ) and (isinstance(raw_audio[0] ,(np.ndarray, tuple, list) )) ) if is_batched: snake_case_ : Any = [np.asarray(_UpperCamelCase ,dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_UpperCamelCase ,np.ndarray ): snake_case_ : Optional[int] = np.asarray(_UpperCamelCase ,dtype=np.floataa ) elif isinstance(_UpperCamelCase ,np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): snake_case_ : List[str] = raw_audio.astype(np.floataa ) # always return batch if not is_batched: snake_case_ : Optional[Any] = [np.asarray(_UpperCamelCase ).T] # verify inputs are valid for idx, example in enumerate(_UpperCamelCase ): if example.ndim > 2: raise ValueError(F'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(F'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(F'''Expected stereo audio but example has {example.shape[-1]} channels''' ) snake_case_ : Tuple = None snake_case_ : Optional[Any] = BatchFeature({"""input_values""": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: snake_case_ : Union[str, Any] = min(array.shape[0] for array in raw_audio ) snake_case_ : Dict = int(np.floor(max_length / self.chunk_stride ) ) snake_case_ : Union[str, Any] = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: snake_case_ : Any = max(array.shape[0] for array in raw_audio ) snake_case_ : List[Any] = int(np.ceil(max_length / self.chunk_stride ) ) snake_case_ : Any = (nb_step - 1) * self.chunk_stride + self.chunk_length snake_case_ : Union[str, Any] = """max_length""" else: snake_case_ : int = input_values # normal padding on batch if padded_inputs is None: snake_case_ : Optional[int] = self.pad( _UpperCamelCase ,max_length=_UpperCamelCase ,truncation=_UpperCamelCase ,padding=_UpperCamelCase ,return_attention_mask=_UpperCamelCase ,) if padding: snake_case_ : Tuple = padded_inputs.pop("""attention_mask""" ) snake_case_ : Optional[int] = [] for example in padded_inputs.pop("""input_values""" ): if self.feature_size == 1: snake_case_ : Dict = example[..., None] input_values.append(example.T ) snake_case_ : List[Any] = input_values if return_tensors is not None: snake_case_ : Tuple = padded_inputs.convert_to_tensors(_UpperCamelCase ) return padded_inputs
8
1
'''simple docstring''' import warnings from transformers import AutoTokenizer from transformers.utils import is_torch_available from transformers.utils.generic import ExplicitEnum from ...processing_utils import ProcessorMixin if is_torch_available(): import torch class __UpperCamelCase ( lowercase__ ): lowercase : Optional[Any] = 'char' lowercase : Optional[Any] = 'bpe' lowercase : List[str] = 'wp' __A : Optional[int] = (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE) class __UpperCamelCase ( lowercase__ ): lowercase : Tuple = ['image_processor', 'char_tokenizer'] lowercase : Optional[int] = 'ViTImageProcessor' lowercase : Tuple = 'MgpstrTokenizer' def __init__( self :Dict ,_UpperCamelCase :Tuple=None ,_UpperCamelCase :str=None ,**_UpperCamelCase :Union[str, Any] ): snake_case_ : Any = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" ,_UpperCamelCase ,) snake_case_ : Any = kwargs.pop("""feature_extractor""" ) snake_case_ : Union[str, Any] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) snake_case_ : List[Any] = tokenizer snake_case_ : List[str] = AutoTokenizer.from_pretrained("""gpt2""" ) snake_case_ : Optional[int] = AutoTokenizer.from_pretrained("""bert-base-uncased""" ) super().__init__(_UpperCamelCase ,_UpperCamelCase ) def __call__( self :str ,_UpperCamelCase :Any=None ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :str=None ,**_UpperCamelCase :Optional[int] ): if images is None and text is None: raise ValueError("""You need to specify either an `images` or `text` input to process.""" ) if images is not None: snake_case_ : Union[str, Any] = self.image_processor(_UpperCamelCase ,return_tensors=_UpperCamelCase ,**_UpperCamelCase ) if text is not None: snake_case_ : Union[str, Any] = self.char_tokenizer(_UpperCamelCase ,return_tensors=_UpperCamelCase ,**_UpperCamelCase ) if text is None: return inputs elif images is None: return encodings else: snake_case_ : Optional[int] = encodings["""input_ids"""] return inputs def a__ ( self :Union[str, Any] ,_UpperCamelCase :Union[str, Any] ): snake_case_ , snake_case_ , snake_case_ : List[Any] = sequences snake_case_ : Any = char_preds.size(0 ) snake_case_ , snake_case_ : int = self._decode_helper(_UpperCamelCase ,"""char""" ) snake_case_ , snake_case_ : Dict = self._decode_helper(_UpperCamelCase ,"""bpe""" ) snake_case_ , snake_case_ : Optional[Any] = self._decode_helper(_UpperCamelCase ,"""wp""" ) snake_case_ : Optional[int] = [] snake_case_ : Optional[Any] = [] for i in range(_UpperCamelCase ): snake_case_ : Optional[Any] = [char_scores[i], bpe_scores[i], wp_scores[i]] snake_case_ : Any = [char_strs[i], bpe_strs[i], wp_strs[i]] snake_case_ : str = scores.index(max(_UpperCamelCase ) ) final_strs.append(strs[max_score_index] ) final_scores.append(scores[max_score_index] ) snake_case_ : Any = {} snake_case_ : Tuple = final_strs snake_case_ : List[Any] = final_scores snake_case_ : Any = char_strs snake_case_ : int = bpe_strs snake_case_ : Union[str, Any] = wp_strs return out def a__ ( self :Union[str, Any] ,_UpperCamelCase :str ,_UpperCamelCase :Union[str, Any] ): if format == DecodeType.CHARACTER: snake_case_ : Optional[Any] = self.char_decode snake_case_ : int = 1 snake_case_ : str = """[s]""" elif format == DecodeType.BPE: snake_case_ : Optional[int] = self.bpe_decode snake_case_ : Optional[Any] = 2 snake_case_ : List[str] = """#""" elif format == DecodeType.WORDPIECE: snake_case_ : Optional[Any] = self.wp_decode snake_case_ : List[Any] = 1_0_2 snake_case_ : Dict = """[SEP]""" else: raise ValueError(F'''Format {format} is not supported.''' ) snake_case_ , snake_case_ : List[Any] = [], [] snake_case_ : Tuple = pred_logits.size(0 ) snake_case_ : Union[str, Any] = pred_logits.size(1 ) snake_case_ , snake_case_ : List[Any] = pred_logits.topk(1 ,dim=-1 ,largest=_UpperCamelCase ,sorted=_UpperCamelCase ) snake_case_ : Any = preds_index.view(-1 ,_UpperCamelCase )[:, 1:] snake_case_ : List[str] = decoder(_UpperCamelCase ) snake_case_ , snake_case_ : Tuple = torch.nn.functional.softmax(_UpperCamelCase ,dim=2 ).max(dim=2 ) snake_case_ : Optional[int] = preds_max_prob[:, 1:] for index in range(_UpperCamelCase ): snake_case_ : Any = preds_str[index].find(_UpperCamelCase ) snake_case_ : Tuple = preds_str[index][:pred_eos] snake_case_ : Optional[Any] = preds_index[index].cpu().tolist() snake_case_ : Union[str, Any] = pred_index.index(_UpperCamelCase ) if eos_token in pred_index else -1 snake_case_ : Optional[Any] = preds_max_prob[index][: pred_eos_index + 1] snake_case_ : Optional[Any] = pred_max_prob.cumprod(dim=0 )[-1] if pred_max_prob.nelement() != 0 else 0.0 dec_strs.append(_UpperCamelCase ) conf_scores.append(_UpperCamelCase ) return dec_strs, conf_scores def a__ ( self :Any ,_UpperCamelCase :List[Any] ): snake_case_ : List[Any] = [seq.replace(""" """ ,"""""" ) for seq in self.char_tokenizer.batch_decode(_UpperCamelCase )] return decode_strs def a__ ( self :List[str] ,_UpperCamelCase :Dict ): return self.bpe_tokenizer.batch_decode(_UpperCamelCase ) def a__ ( self :str ,_UpperCamelCase :List[Any] ): snake_case_ : int = [seq.replace(""" """ ,"""""" ) for seq in self.wp_tokenizer.batch_decode(_UpperCamelCase )] return decode_strs
8
'''simple docstring''' from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig __A : Dict = { 'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json', 'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json', } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[int] = 'ernie_m' lowercase : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self :Optional[Any] ,_UpperCamelCase :int = 2_5_0_0_0_2 ,_UpperCamelCase :int = 7_6_8 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 3_0_7_2 ,_UpperCamelCase :str = "gelu" ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :int = 5_1_4 ,_UpperCamelCase :float = 0.02 ,_UpperCamelCase :int = 1 ,_UpperCamelCase :float = 1E-0_5 ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Optional[int]=0.0 ,**_UpperCamelCase :List[Any] ,): super().__init__(pad_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Optional[int] = vocab_size snake_case_ : Any = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : Union[str, Any] = num_attention_heads snake_case_ : Any = intermediate_size snake_case_ : Any = hidden_act snake_case_ : Tuple = hidden_dropout_prob snake_case_ : Union[str, Any] = attention_probs_dropout_prob snake_case_ : str = max_position_embeddings snake_case_ : int = initializer_range snake_case_ : Optional[Any] = layer_norm_eps snake_case_ : Union[str, Any] = classifier_dropout snake_case_ : Tuple = is_decoder snake_case_ : int = act_dropout
8
1
'''simple docstring''' from __future__ import annotations from math import pi from typing import Protocol import matplotlib.pyplot as plt import numpy as np class __UpperCamelCase ( lowercase__ ): def a__ ( self :List[Any] ,_UpperCamelCase :float ): return 0.0 def UpperCAmelCase ( lowerCamelCase_ :np.ndarray , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Dict = min([-20, np.min(fft_results[1 : samplerate // 2 - 1] )] ) snake_case_ : List[str] = max([20, np.max(fft_results[1 : samplerate // 2 - 1] )] ) return lowest, highest def UpperCAmelCase ( lowerCamelCase_ :FilterType , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Union[str, Any] = 5_12 snake_case_ : Any = [1] + [0] * (size - 1) snake_case_ : Optional[int] = [filter_type.process(lowerCamelCase_ ) for item in inputs] snake_case_ : str = [0] * (samplerate - size) # zero-padding outputs += filler snake_case_ : Optional[int] = np.abs(np.fft.fft(lowerCamelCase_ ) ) snake_case_ : int = 20 * np.logaa(lowerCamelCase_ ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel("""Frequency (Hz)""" ) plt.xscale("""log""" ) # Display within reasonable bounds snake_case_ : Optional[int] = get_bounds(lowerCamelCase_ , lowerCamelCase_ ) plt.ylim(max([-80, bounds[0]] ) , min([80, bounds[1]] ) ) plt.ylabel("""Gain (dB)""" ) plt.plot(lowerCamelCase_ ) plt.show() def UpperCAmelCase ( lowerCamelCase_ :FilterType , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : int = 5_12 snake_case_ : List[Any] = [1] + [0] * (size - 1) snake_case_ : Dict = [filter_type.process(lowerCamelCase_ ) for item in inputs] snake_case_ : Dict = [0] * (samplerate - size) # zero-padding outputs += filler snake_case_ : Optional[int] = np.angle(np.fft.fft(lowerCamelCase_ ) ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel("""Frequency (Hz)""" ) plt.xscale("""log""" ) plt.ylim(-2 * pi , 2 * pi ) plt.ylabel("""Phase shift (Radians)""" ) plt.plot(np.unwrap(lowerCamelCase_ , -2 * pi ) ) plt.show()
8
'''simple docstring''' from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class __UpperCamelCase ( nn.Module ): def __init__( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = True ,_UpperCamelCase :str = "layer_norm" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Any = only_cross_attention snake_case_ : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" snake_case_ : Any = (num_embeds_ada_norm is not None) and norm_type == """ada_norm""" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F'''`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to''' F''' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.''' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: snake_case_ : Dict = AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ : str = AdaLayerNormZero(_UpperCamelCase ,_UpperCamelCase ) else: snake_case_ : List[Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=_UpperCamelCase ,) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. snake_case_ : str = ( AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,upcast_attention=_UpperCamelCase ,) # is self-attn if encoder_hidden_states is none else: snake_case_ : Any = None snake_case_ : Optional[Any] = None # 3. Feed-forward snake_case_ : List[str] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : Union[str, Any] = FeedForward(_UpperCamelCase ,dropout=_UpperCamelCase ,activation_fn=_UpperCamelCase ,final_dropout=_UpperCamelCase ) # let chunk size default to None snake_case_ : Optional[int] = None snake_case_ : Dict = 0 def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :int ): # Sets chunk feed-forward snake_case_ : Optional[Any] = chunk_size snake_case_ : Optional[Any] = dim def a__ ( self :List[str] ,_UpperCamelCase :torch.FloatTensor ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,_UpperCamelCase :Dict[str, Any] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = self.norma( _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=hidden_states.dtype ) else: snake_case_ : Optional[int] = self.norma(_UpperCamelCase ) snake_case_ : int = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case_ : Union[str, Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_msa.unsqueeze(1 ) * attn_output snake_case_ : Union[str, Any] = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case_ : Any = ( self.norma(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else self.norma(_UpperCamelCase ) ) snake_case_ : List[Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=_UpperCamelCase ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Tuple = attn_output + hidden_states # 3. Feed-forward snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Dict = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F'''`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.''' ) snake_case_ : Union[str, Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case_ : int = torch.cat( [self.ff(_UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase ,dim=self._chunk_dim )] ,dim=self._chunk_dim ,) else: snake_case_ : List[str] = self.ff(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case_ : Any = ff_output + hidden_states return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Dict ,_UpperCamelCase :int ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :int = 4 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Tuple = int(dim * mult ) snake_case_ : Optional[int] = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case_ : Any = GELU(_UpperCamelCase ,_UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case_ : Tuple = GELU(_UpperCamelCase ,_UpperCamelCase ,approximate="""tanh""" ) elif activation_fn == "geglu": snake_case_ : Dict = GEGLU(_UpperCamelCase ,_UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case_ : Optional[Any] = ApproximateGELU(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Dict = nn.ModuleList([] ) # project in self.net.append(_UpperCamelCase ) # project dropout self.net.append(nn.Dropout(_UpperCamelCase ) ) # project out self.net.append(nn.Linear(_UpperCamelCase ,_UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase ) ) def a__ ( self :Tuple ,_UpperCamelCase :Union[str, Any] ): for module in self.net: snake_case_ : Tuple = module(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :str = "none" ): super().__init__() snake_case_ : Union[str, Any] = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Optional[Any] = approximate def a__ ( self :str ,_UpperCamelCase :int ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ,approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ,approximate=self.approximate ).to(dtype=gate.dtype ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[Any] ): snake_case_ : Optional[Any] = self.proj(_UpperCamelCase ) snake_case_ : int = self.gelu(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :List[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : str = nn.Linear(_UpperCamelCase ,dim_out * 2 ) def a__ ( self :Dict ,_UpperCamelCase :List[str] ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def a__ ( self :Optional[Any] ,_UpperCamelCase :Optional[int] ): snake_case_ , snake_case_ : Dict = self.proj(_UpperCamelCase ).chunk(2 ,dim=-1 ) return hidden_states * self.gelu(_UpperCamelCase ) class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[int] ): snake_case_ : int = self.proj(_UpperCamelCase ) return x * torch.sigmoid(1.7_02 * x ) class __UpperCamelCase ( nn.Module ): def __init__( self :int ,_UpperCamelCase :str ,_UpperCamelCase :List[Any] ): super().__init__() snake_case_ : int = nn.Embedding(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = nn.SiLU() snake_case_ : Any = nn.Linear(_UpperCamelCase ,embedding_dim * 2 ) snake_case_ : Dict = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) def a__ ( self :int ,_UpperCamelCase :List[str] ,_UpperCamelCase :int ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ) ) ) snake_case_ , snake_case_ : Tuple = torch.chunk(_UpperCamelCase ,2 ) snake_case_ : Tuple = self.norm(_UpperCamelCase ) * (1 + scale) + shift return x class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :Tuple ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = CombinedTimestepLabelEmbeddings(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : int = nn.SiLU() snake_case_ : List[str] = nn.Linear(_UpperCamelCase ,6 * embedding_dim ,bias=_UpperCamelCase ) snake_case_ : str = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ,eps=1E-6 ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str=None ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=_UpperCamelCase ) ) ) snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Any = emb.chunk(6 ,dim=1 ) snake_case_ : str = self.norm(_UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[int] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :Optional[str] = None ,_UpperCamelCase :float = 1E-5 ): super().__init__() snake_case_ : Optional[int] = num_groups snake_case_ : List[Any] = eps if act_fn is None: snake_case_ : int = None else: snake_case_ : Dict = get_activation(_UpperCamelCase ) snake_case_ : Optional[int] = nn.Linear(_UpperCamelCase ,out_dim * 2 ) def a__ ( self :List[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :List[str] ): if self.act: snake_case_ : Any = self.act(_UpperCamelCase ) snake_case_ : Optional[int] = self.linear(_UpperCamelCase ) snake_case_ : Dict = emb[:, :, None, None] snake_case_ , snake_case_ : str = emb.chunk(2 ,dim=1 ) snake_case_ : str = F.group_norm(_UpperCamelCase ,self.num_groups ,eps=self.eps ) snake_case_ : List[str] = x * (1 + scale) + shift return x
8
1
'''simple docstring''' import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def UpperCAmelCase ( lowerCamelCase_ :List[str] ): '''simple docstring''' snake_case_ : List[Any] = SwinConfig() snake_case_ : Union[str, Any] = swin_name.split("""_""" ) snake_case_ : Tuple = name_split[1] snake_case_ : int = int(name_split[4] ) snake_case_ : Optional[Any] = int(name_split[3][-1] ) if model_size == "tiny": snake_case_ : Optional[Any] = 96 snake_case_ : Tuple = (2, 2, 6, 2) snake_case_ : str = (3, 6, 12, 24) elif model_size == "small": snake_case_ : Optional[Any] = 96 snake_case_ : Optional[Any] = (2, 2, 18, 2) snake_case_ : Any = (3, 6, 12, 24) elif model_size == "base": snake_case_ : List[Any] = 1_28 snake_case_ : str = (2, 2, 18, 2) snake_case_ : List[str] = (4, 8, 16, 32) else: snake_case_ : Union[str, Any] = 1_92 snake_case_ : Optional[int] = (2, 2, 18, 2) snake_case_ : Optional[Any] = (6, 12, 24, 48) if "in22k" in swin_name: snake_case_ : List[str] = 2_18_41 else: snake_case_ : Any = 10_00 snake_case_ : Optional[Any] = """huggingface/label-files""" snake_case_ : Optional[Any] = """imagenet-1k-id2label.json""" snake_case_ : Tuple = json.load(open(hf_hub_download(lowerCamelCase_ , lowerCamelCase_ , repo_type="""dataset""" ) , """r""" ) ) snake_case_ : str = {int(lowerCamelCase_ ): v for k, v in idalabel.items()} snake_case_ : Dict = idalabel snake_case_ : int = {v: k for k, v in idalabel.items()} snake_case_ : Union[str, Any] = img_size snake_case_ : Union[str, Any] = num_classes snake_case_ : List[Any] = embed_dim snake_case_ : str = depths snake_case_ : int = num_heads snake_case_ : List[str] = window_size return config def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' if "patch_embed.proj" in name: snake_case_ : Union[str, Any] = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: snake_case_ : Tuple = name.replace("""patch_embed.norm""" , """embeddings.norm""" ) if "layers" in name: snake_case_ : str = """encoder.""" + name if "attn.proj" in name: snake_case_ : List[str] = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: snake_case_ : Tuple = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: snake_case_ : Dict = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: snake_case_ : Dict = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: snake_case_ : Optional[int] = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: snake_case_ : int = name.replace("""mlp.fc2""" , """output.dense""" ) if name == "norm.weight": snake_case_ : List[str] = """layernorm.weight""" if name == "norm.bias": snake_case_ : Any = """layernorm.bias""" if "head" in name: snake_case_ : List[Any] = name.replace("""head""" , """classifier""" ) else: snake_case_ : str = """swin.""" + name return name def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :str ): '''simple docstring''' for key in orig_state_dict.copy().keys(): snake_case_ : Optional[int] = orig_state_dict.pop(lowerCamelCase_ ) if "mask" in key: continue elif "qkv" in key: snake_case_ : Union[str, Any] = key.split(""".""" ) snake_case_ : str = int(key_split[1] ) snake_case_ : Optional[int] = int(key_split[3] ) snake_case_ : List[Any] = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: snake_case_ : List[Any] = val[:dim, :] snake_case_ : List[str] = val[ dim : dim * 2, : ] snake_case_ : int = val[-dim:, :] else: snake_case_ : Dict = val[ :dim ] snake_case_ : List[str] = val[ dim : dim * 2 ] snake_case_ : str = val[ -dim: ] else: snake_case_ : Optional[Any] = val return orig_state_dict def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] ): '''simple docstring''' snake_case_ : int = timm.create_model(lowerCamelCase_ , pretrained=lowerCamelCase_ ) timm_model.eval() snake_case_ : Any = get_swin_config(lowerCamelCase_ ) snake_case_ : Any = SwinForImageClassification(lowerCamelCase_ ) model.eval() snake_case_ : Dict = convert_state_dict(timm_model.state_dict() , lowerCamelCase_ ) model.load_state_dict(lowerCamelCase_ ) snake_case_ : List[str] = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case_ : str = AutoImageProcessor.from_pretrained("""microsoft/{}""".format(swin_name.replace("""_""" , """-""" ) ) ) snake_case_ : int = Image.open(requests.get(lowerCamelCase_ , stream=lowerCamelCase_ ).raw ) snake_case_ : Any = image_processor(images=lowerCamelCase_ , return_tensors="""pt""" ) snake_case_ : List[str] = timm_model(inputs["""pixel_values"""] ) snake_case_ : List[str] = model(**lowerCamelCase_ ).logits assert torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1E-3 ) print(F'''Saving model {swin_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(lowerCamelCase_ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(lowerCamelCase_ ) if __name__ == "__main__": __A : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--swin_name', default='swin_tiny_patch4_window7_224', type=str, help='Name of the Swin 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.' ) __A : int = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
8
'''simple docstring''' import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :str=True , lowerCamelCase_ :str="pt" ): '''simple docstring''' snake_case_ : Tuple = {"""add_prefix_space""": True} if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and not line.startswith(""" """ ) else {} snake_case_ : Union[str, Any] = padding_side return tokenizer( [line] , max_length=lowerCamelCase_ , padding="""max_length""" if pad_to_max_length else None , truncation=lowerCamelCase_ , return_tensors=lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , **lowerCamelCase_ , ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Any=None , ): '''simple docstring''' snake_case_ : Dict = input_ids.ne(lowerCamelCase_ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[Any] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any="train" ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :int=None ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :Optional[int]="" ,): super().__init__() snake_case_ : List[str] = Path(_UpperCamelCase ).joinpath(type_path + """.source""" ) snake_case_ : int = Path(_UpperCamelCase ).joinpath(type_path + """.target""" ) snake_case_ : Optional[int] = self.get_char_lens(self.src_file ) snake_case_ : List[str] = max_source_length snake_case_ : str = max_target_length assert min(self.src_lens ) > 0, F'''found empty line in {self.src_file}''' snake_case_ : str = tokenizer snake_case_ : str = prefix if n_obs is not None: snake_case_ : int = self.src_lens[:n_obs] snake_case_ : Tuple = src_lang snake_case_ : str = tgt_lang def __len__( self :Any ): return len(self.src_lens ) def __getitem__( self :List[str] ,_UpperCamelCase :Union[str, Any] ): snake_case_ : Optional[int] = index + 1 # linecache starts at 1 snake_case_ : Dict = self.prefix + linecache.getline(str(self.src_file ) ,_UpperCamelCase ).rstrip("""\n""" ) snake_case_ : List[Any] = linecache.getline(str(self.tgt_file ) ,_UpperCamelCase ).rstrip("""\n""" ) assert source_line, F'''empty source line for index {index}''' assert tgt_line, F'''empty tgt line for index {index}''' # Need to add eos token manually for T5 if isinstance(self.tokenizer ,_UpperCamelCase ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right snake_case_ : int = ( self.tokenizer.question_encoder if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer ) snake_case_ : Optional[int] = self.tokenizer.generator if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer snake_case_ : Optional[Any] = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_source_length ,"""right""" ) snake_case_ : Tuple = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_target_length ,"""right""" ) snake_case_ : int = source_inputs["""input_ids"""].squeeze() snake_case_ : str = target_inputs["""input_ids"""].squeeze() snake_case_ : Union[str, Any] = source_inputs["""attention_mask"""].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def a__ ( _UpperCamelCase :str ): return [len(_UpperCamelCase ) for x in Path(_UpperCamelCase ).open().readlines()] def a__ ( self :Optional[int] ,_UpperCamelCase :List[str] ): snake_case_ : Optional[Any] = torch.stack([x["""input_ids"""] for x in batch] ) snake_case_ : List[Any] = torch.stack([x["""attention_mask"""] for x in batch] ) snake_case_ : Union[str, Any] = torch.stack([x["""decoder_input_ids"""] for x in batch] ) snake_case_ : Optional[Any] = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Tuple = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Optional[int] = trim_batch(_UpperCamelCase ,_UpperCamelCase ) snake_case_ , snake_case_ : Dict = trim_batch(_UpperCamelCase ,_UpperCamelCase ,attention_mask=_UpperCamelCase ) snake_case_ : Optional[int] = { """input_ids""": source_ids, """attention_mask""": source_mask, """decoder_input_ids""": y, } return batch __A : List[Any] = getLogger(__name__) def UpperCAmelCase ( lowerCamelCase_ :List[List] ): '''simple docstring''' return list(itertools.chain.from_iterable(lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : int = get_git_info() save_json(lowerCamelCase_ , os.path.join(lowerCamelCase_ , """git_log.json""" ) ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int]=4 , **lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' with open(lowerCamelCase_ , """w""" ) as f: json.dump(lowerCamelCase_ , lowerCamelCase_ , indent=lowerCamelCase_ , **lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] ): '''simple docstring''' with open(lowerCamelCase_ ) as f: return json.load(lowerCamelCase_ ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Optional[Any] = git.Repo(search_parent_directories=lowerCamelCase_ ) snake_case_ : List[str] = { """repo_id""": str(lowerCamelCase_ ), """repo_sha""": str(repo.head.object.hexsha ), """repo_branch""": str(repo.active_branch ), """hostname""": str(socket.gethostname() ), } return repo_infos def UpperCAmelCase ( lowerCamelCase_ :Callable , lowerCamelCase_ :Iterable ): '''simple docstring''' return list(map(lowerCamelCase_ , lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , """wb""" ) as f: return pickle.dump(lowerCamelCase_ , lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' def remove_articles(lowerCamelCase_ :str ): return re.sub(R"""\b(a|an|the)\b""" , """ """ , lowerCamelCase_ ) def white_space_fix(lowerCamelCase_ :Optional[Any] ): return " ".join(text.split() ) def remove_punc(lowerCamelCase_ :Tuple ): snake_case_ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowerCamelCase_ :Optional[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase_ ) ) ) ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' snake_case_ : List[Any] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : Optional[int] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : List[Any] = Counter(lowerCamelCase_ ) & Counter(lowerCamelCase_ ) snake_case_ : Optional[Any] = sum(common.values() ) if num_same == 0: return 0 snake_case_ : Optional[Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Union[str, Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Optional[Any] = (2 * precision * recall) / (precision + recall) return fa def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' return normalize_answer(lowerCamelCase_ ) == normalize_answer(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] ): '''simple docstring''' assert len(lowerCamelCase_ ) == len(lowerCamelCase_ ) snake_case_ : Optional[int] = 0 for hypo, pred in zip(lowerCamelCase_ , lowerCamelCase_ ): em += exact_match_score(lowerCamelCase_ , lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: em /= len(lowerCamelCase_ ) return {"em": em} def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return model_prefix.startswith("""rag""" ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead snake_case_ : Optional[int] = """dropout_rate""" for p in extra_params: if getattr(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): if not hasattr(lowerCamelCase_ , lowerCamelCase_ ) and not hasattr(lowerCamelCase_ , equivalent_param[p] ): logger.info("""config doesn't have a `{}` attribute""".format(lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) continue snake_case_ : str = p if hasattr(lowerCamelCase_ , lowerCamelCase_ ) else equivalent_param[p] setattr(lowerCamelCase_ , lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) return hparams, config
8
1
'''simple docstring''' import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer import diffusers from diffusers import ( AutoencoderKL, EulerDiscreteScheduler, StableDiffusionLatentUpscalePipeline, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.schedulers import KarrasDiffusionSchedulers from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : List[Any] = [tensor.shape for tensor in tensor_list] return all(shape == shapes[0] for shape in shapes[1:] ) class __UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , unittest.TestCase ): lowercase : Dict = StableDiffusionLatentUpscalePipeline lowercase : str = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { 'height', 'width', 'cross_attention_kwargs', 'negative_prompt_embeds', 'prompt_embeds', } lowercase : str = PipelineTesterMixin.required_optional_params - {'num_images_per_prompt'} lowercase : str = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS lowercase : str = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowercase : str = frozenset([] ) lowercase : Optional[int] = True @property def a__ ( self :int ): snake_case_ : List[str] = 1 snake_case_ : int = 4 snake_case_ : Optional[Any] = (1_6, 1_6) snake_case_ : List[Any] = floats_tensor((batch_size, num_channels) + sizes ,rng=random.Random(0 ) ).to(_UpperCamelCase ) return image def a__ ( self :Tuple ): torch.manual_seed(0 ) snake_case_ : Optional[Any] = UNetaDConditionModel( act_fn="""gelu""" ,attention_head_dim=8 ,norm_num_groups=_UpperCamelCase ,block_out_channels=[3_2, 3_2, 6_4, 6_4] ,time_cond_proj_dim=1_6_0 ,conv_in_kernel=1 ,conv_out_kernel=1 ,cross_attention_dim=3_2 ,down_block_types=( """KDownBlock2D""", """KCrossAttnDownBlock2D""", """KCrossAttnDownBlock2D""", """KCrossAttnDownBlock2D""", ) ,in_channels=8 ,mid_block_type=_UpperCamelCase ,only_cross_attention=_UpperCamelCase ,out_channels=5 ,resnet_time_scale_shift="""scale_shift""" ,time_embedding_type="""fourier""" ,timestep_post_act="""gelu""" ,up_block_types=("""KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KUpBlock2D""") ,) snake_case_ : List[Any] = AutoencoderKL( block_out_channels=[3_2, 3_2, 6_4, 6_4] ,in_channels=3 ,out_channels=3 ,down_block_types=[ """DownEncoderBlock2D""", """DownEncoderBlock2D""", """DownEncoderBlock2D""", """DownEncoderBlock2D""", ] ,up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,latent_channels=4 ,) snake_case_ : List[Any] = EulerDiscreteScheduler(prediction_type="""sample""" ) snake_case_ : Any = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=3_2 ,intermediate_size=3_7 ,layer_norm_eps=1E-0_5 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1_0_0_0 ,hidden_act="""quick_gelu""" ,projection_dim=5_1_2 ,) snake_case_ : Any = CLIPTextModel(_UpperCamelCase ) snake_case_ : Optional[int] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) snake_case_ : Optional[int] = { """unet""": model.eval(), """vae""": vae.eval(), """scheduler""": scheduler, """text_encoder""": text_encoder, """tokenizer""": tokenizer, } return components def a__ ( self :List[Any] ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :int=0 ): if str(_UpperCamelCase ).startswith("""mps""" ): snake_case_ : List[Any] = torch.manual_seed(_UpperCamelCase ) else: snake_case_ : Union[str, Any] = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) snake_case_ : Optional[Any] = { """prompt""": """A painting of a squirrel eating a burger""", """image""": self.dummy_image.cpu(), """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs def a__ ( self :List[str] ): snake_case_ : Any = """cpu""" snake_case_ : str = self.get_dummy_components() snake_case_ : str = self.pipeline_class(**_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ : List[Any] = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ : Optional[Any] = pipe(**_UpperCamelCase ).images snake_case_ : Dict = image[0, -3:, -3:, -1] self.assertEqual(image.shape ,(1, 2_5_6, 2_5_6, 3) ) snake_case_ : int = np.array( [0.47_22_24_12, 0.41_92_16_33, 0.44_71_74_34, 0.46_87_41_92, 0.42_58_82_58, 0.46_15_07_26, 0.4_67_75_34, 0.45_58_38_32, 0.48_57_90_55] ) snake_case_ : Optional[int] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_UpperCamelCase ,1E-3 ) def a__ ( self :int ): super().test_attention_slicing_forward_pass(expected_max_diff=7E-3 ) def a__ ( self :Optional[Any] ): super().test_cpu_offload_forward_pass(expected_max_diff=3E-3 ) def a__ ( self :List[Any] ): super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def a__ ( self :List[str] ): super().test_inference_batch_single_identical(expected_max_diff=7E-3 ) def a__ ( self :int ): super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3E-3 ) def a__ ( self :int ): super().test_save_load_local(expected_max_difference=3E-3 ) def a__ ( self :List[str] ): super().test_save_load_optional_components(expected_max_difference=3E-3 ) def a__ ( self :Tuple ): snake_case_ : Union[str, Any] = [ """DDIMScheduler""", """DDPMScheduler""", """PNDMScheduler""", """HeunDiscreteScheduler""", """EulerAncestralDiscreteScheduler""", """KDPM2DiscreteScheduler""", """KDPM2AncestralDiscreteScheduler""", """DPMSolverSDEScheduler""", ] snake_case_ : Dict = self.get_dummy_components() snake_case_ : Optional[Any] = self.pipeline_class(**_UpperCamelCase ) # make sure that PNDM does not need warm-up pipe.scheduler.register_to_config(skip_prk_steps=_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ : Any = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ : Any = 2 snake_case_ : str = [] for scheduler_enum in KarrasDiffusionSchedulers: if scheduler_enum.name in skip_schedulers: # no sigma schedulers are not supported # no schedulers continue snake_case_ : Union[str, Any] = getattr(_UpperCamelCase ,scheduler_enum.name ) snake_case_ : str = scheduler_cls.from_config(pipe.scheduler.config ) snake_case_ : Any = pipe(**_UpperCamelCase )[0] outputs.append(_UpperCamelCase ) assert check_same_shape(_UpperCamelCase ) @require_torch_gpu @slow class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :Optional[Any] ): super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self :Optional[Any] ): snake_case_ : Tuple = torch.manual_seed(3_3 ) snake_case_ : Optional[int] = StableDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4""" ,torch_dtype=torch.floataa ) pipe.to("""cuda""" ) snake_case_ : List[Any] = StableDiffusionLatentUpscalePipeline.from_pretrained( """stabilityai/sd-x2-latent-upscaler""" ,torch_dtype=torch.floataa ) upscaler.to("""cuda""" ) snake_case_ : Dict = """a photo of an astronaut high resolution, unreal engine, ultra realistic""" snake_case_ : str = pipe(_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""latent""" ).images snake_case_ : Union[str, Any] = upscaler( prompt=_UpperCamelCase ,image=_UpperCamelCase ,num_inference_steps=2_0 ,guidance_scale=0 ,generator=_UpperCamelCase ,output_type="""np""" ,).images[0] snake_case_ : str = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy""" ) assert np.abs((expected_image - image).mean() ) < 5E-2 def a__ ( self :Dict ): snake_case_ : Union[str, Any] = torch.manual_seed(3_3 ) snake_case_ : Tuple = StableDiffusionLatentUpscalePipeline.from_pretrained( """stabilityai/sd-x2-latent-upscaler""" ,torch_dtype=torch.floataa ) upscaler.to("""cuda""" ) snake_case_ : Tuple = """the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas""" snake_case_ : Any = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png""" ) snake_case_ : List[str] = upscaler( prompt=_UpperCamelCase ,image=_UpperCamelCase ,num_inference_steps=2_0 ,guidance_scale=0 ,generator=_UpperCamelCase ,output_type="""np""" ,).images[0] snake_case_ : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy""" ) assert np.abs((expected_image - image).max() ) < 5E-2
8
'''simple docstring''' import functools def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[str] = len(lowerCamelCase_ ) snake_case_ : Dict = len(lowerCamelCase_ ) @functools.cache def min_distance(lowerCamelCase_ :int , lowerCamelCase_ :int ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa snake_case_ : Union[str, Any] = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , lowerCamelCase_ ) , 1 + min_distance(lowerCamelCase_ , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
8
1
'''simple docstring''' # # This a `torch.distributed` diagnostics script that checks that all GPUs in the cluster (one or # many nodes) can talk to each other via nccl and allocate gpu memory. # # To run first adjust the number of processes and nodes: # # python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # You may need to add --master_addr $MASTER_ADDR --master_port $MASTER_PORT if using a custom addr:port # # You can also use the rdzv API: --rdzv_endpoint $MASTER_ADDR:$MASTER_PORT --rdzv_backend c10d # # use torch.distributed.launch instead of torch.distributed.run for torch < 1.9 # # If you get a hanging in `barrier` calls you have some network issues, you may try to debug this with: # # NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # which should tell you what's going on behind the scenes. # # # This script can be run via `srun` in the SLURM environment as well. Here is a SLURM script that # runs on 2 nodes of 4 gpus per node: # # #SBATCH --job-name=test-nodes # name # #SBATCH --nodes=2 # nodes # #SBATCH --ntasks-per-node=1 # crucial - only 1 task per dist per node! # #SBATCH --cpus-per-task=10 # number of cores per tasks # #SBATCH --gres=gpu:4 # number of gpus # #SBATCH --time 0:05:00 # maximum execution time (HH:MM:SS) # #SBATCH --output=%x-%j.out # output file name # # GPUS_PER_NODE=4 # MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) # MASTER_PORT=6000 # # srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ # --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ # --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ # torch-distributed-gpu-test.py' # import fcntl import os import socket import torch import torch.distributed as dist def UpperCAmelCase ( *lowerCamelCase_ :str ): '''simple docstring''' with open(lowerCamelCase_ , """r""" ) as fh: fcntl.flock(lowerCamelCase_ , fcntl.LOCK_EX ) try: print(*lowerCamelCase_ ) finally: fcntl.flock(lowerCamelCase_ , fcntl.LOCK_UN ) __A : List[str] = int(os.environ['LOCAL_RANK']) torch.cuda.set_device(local_rank) __A : int = torch.device('cuda', local_rank) __A : Union[str, Any] = socket.gethostname() __A : str = F'[{hostname}-{local_rank}]' try: # test distributed dist.init_process_group('nccl') dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM) dist.barrier() # test cuda is available and can allocate memory torch.cuda.is_available() torch.ones(1).cuda(local_rank) # global rank __A : Dict = dist.get_rank() __A : List[str] = dist.get_world_size() printflock(F'{gpu} is OK (global rank: {rank}/{world_size})') dist.barrier() if rank == 0: printflock(F'pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}') except Exception: printflock(F'{gpu} is broken') raise
8
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Any = tmp_path / """file.csv""" snake_case_ : Any = textwrap.dedent( """\ header1,header2 1,2 10,20 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[int] = tmp_path / """malformed_file.csv""" snake_case_ : int = textwrap.dedent( """\ header1,header2 1,2 10,20, """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : str = tmp_path / """csv_with_image.csv""" snake_case_ : int = textwrap.dedent( F'''\ image {image_file} ''' ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' snake_case_ : int = tmp_path / """csv_with_label.csv""" snake_case_ : Tuple = textwrap.dedent( """\ label good bad good """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = tmp_path / """csv_with_int_list.csv""" snake_case_ : str = textwrap.dedent( """\ int_list 1 2 3 4 5 6 7 8 9 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : int = Csv() snake_case_ : Optional[Any] = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(lowerCamelCase_ , match="""Error tokenizing data""" ): for _ in generator: pass assert any( record.levelname == """ERROR""" and """Failed to read file""" in record.message and os.path.basename(lowerCamelCase_ ) in record.message for record in caplog.records ) @require_pil def UpperCAmelCase ( lowerCamelCase_ :Tuple ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : Tuple = f.read().splitlines()[1] snake_case_ : str = Csv(encoding="""utf-8""" , features=Features({"""image""": Image()} ) ) snake_case_ : Tuple = csv._generate_tables([[csv_file_with_image]] ) snake_case_ : Optional[Any] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""image""" ).type == Image()() snake_case_ : List[str] = pa_table.to_pydict()["""image"""] assert generated_content == [{"path": image_file, "bytes": None}] def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : List[Any] = f.read().splitlines()[1:] snake_case_ : Union[str, Any] = Csv(encoding="""utf-8""" , features=Features({"""label""": ClassLabel(names=["""good""", """bad"""] )} ) ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_label]] ) snake_case_ : Optional[int] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""label""" ).type == ClassLabel(names=["""good""", """bad"""] )() snake_case_ : Union[str, Any] = pa_table.to_pydict()["""label"""] assert generated_content == [ClassLabel(names=["""good""", """bad"""] ).straint(lowerCamelCase_ ) for label in labels] def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : str = Csv(encoding="""utf-8""" , sep=""",""" , converters={"""int_list""": lambda lowerCamelCase_ : [int(lowerCamelCase_ ) for i in x.split()]} ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_int_list]] ) snake_case_ : Tuple = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field("""int_list""" ).type ) snake_case_ : Dict = pa_table.to_pydict()["""int_list"""] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
8
1
'''simple docstring''' from statistics import mean import numpy as np def UpperCAmelCase ( lowerCamelCase_ :list , lowerCamelCase_ :list , lowerCamelCase_ :list , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Optional[Any] = 0 # Number of processes finished snake_case_ : Dict = 0 # Displays the finished process. # If it is 0, the performance is completed if it is 1, before the performance. snake_case_ : List[str] = [0] * no_of_process # List to include calculation results snake_case_ : int = [0] * no_of_process # Sort by arrival time. snake_case_ : Any = [burst_time[i] for i in np.argsort(lowerCamelCase_ )] snake_case_ : Tuple = [process_name[i] for i in np.argsort(lowerCamelCase_ )] arrival_time.sort() while no_of_process > finished_process_count: snake_case_ : Tuple = 0 while finished_process[i] == 1: i += 1 if current_time < arrival_time[i]: snake_case_ : List[str] = arrival_time[i] snake_case_ : Optional[int] = 0 # Index showing the location of the process being performed snake_case_ : Union[str, Any] = 0 # Saves the current response ratio. snake_case_ : Union[str, Any] = 0 for i in range(0 , lowerCamelCase_ ): if finished_process[i] == 0 and arrival_time[i] <= current_time: snake_case_ : Any = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[ i ] if response_ratio < temp: snake_case_ : Tuple = temp snake_case_ : Optional[Any] = i # Calculate the turn around time snake_case_ : List[Any] = current_time + burst_time[loc] - arrival_time[loc] current_time += burst_time[loc] # Indicates that the process has been performed. snake_case_ : Any = 1 # Increase finished_process_count by 1 finished_process_count += 1 return turn_around_time def UpperCAmelCase ( lowerCamelCase_ :list , lowerCamelCase_ :list , lowerCamelCase_ :list , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Union[str, Any] = [0] * no_of_process for i in range(0 , lowerCamelCase_ ): snake_case_ : Any = turn_around_time[i] - burst_time[i] return waiting_time if __name__ == "__main__": __A : Tuple = 5 __A : Dict = ['A', 'B', 'C', 'D', 'E'] __A : Dict = [1, 2, 3, 4, 5] __A : Dict = [1, 2, 3, 4, 5] __A : str = calculate_turn_around_time( process_name, arrival_time, burst_time, no_of_process ) __A : int = calculate_waiting_time( process_name, turn_around_time, burst_time, no_of_process ) print('Process name \tArrival time \tBurst time \tTurn around time \tWaiting time') for i in range(0, no_of_process): print( F'{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t' F'{turn_around_time[i]}\t\t\t{waiting_time[i]}' ) print(F'average waiting time : {mean(waiting_time):.5f}') print(F'average turn around time : {mean(turn_around_time):.5f}')
8
'''simple docstring''' import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple=None ): '''simple docstring''' # set parameter of one layer assert torch_layer.weight.shape == weight.shape, F'''{torch_layer} layer.weight does not match''' snake_case_ : Optional[Any] = nn.Parameter(lowerCamelCase_ ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F'''{torch_layer} layer.bias does not match''' snake_case_ : List[str] = nn.Parameter(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : Optional[Any] = np.asarray(weights[0] ) snake_case_ : int = np.asarray(weights[1] ) snake_case_ : Any = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : List[Any] = np.asarray(weights[0] ) snake_case_ : Optional[int] = np.asarray(weights[1] ) snake_case_ : Union[str, Any] = np.asarray(weights[2] ) snake_case_ : int = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' # layernorm 1 snake_case_ : str = weights[0][0][0] snake_case_ : int = np.asarray(layer_norm_a[0] ) snake_case_ : Optional[Any] = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # lsh weights + output snake_case_ : Tuple = weights[0][1] if len(lowerCamelCase_ ) < 4: set_layer_weights_in_torch_lsh(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) else: set_layer_weights_in_torch_local(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) # intermediate weighs snake_case_ : str = weights[2][0][1][2] # Chunked Feed Forward if len(lowerCamelCase_ ) == 4: snake_case_ : List[Any] = intermediate_weights[2] # layernorm 2 snake_case_ : Tuple = np.asarray(intermediate_weights[0][0] ) snake_case_ : Optional[Any] = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # intermediate dense snake_case_ : Any = np.asarray(intermediate_weights[1][0] ) snake_case_ : List[Any] = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) # intermediate out snake_case_ : List[Any] = np.asarray(intermediate_weights[4][0] ) snake_case_ : Union[str, Any] = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Any ): '''simple docstring''' # reformer model snake_case_ : Dict = torch_model.reformer # word embeds snake_case_ : List[Any] = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(lowerCamelCase_ ) , ) if isinstance(weights[3] , lowerCamelCase_ ): snake_case_ : Tuple = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): snake_case_ : Dict = np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), F'''{position_embeddings[emb_idx]} emb does not match''' snake_case_ : Optional[Any] = nn.Parameter(torch.tensor(lowerCamelCase_ ) ) snake_case_ : List[Any] = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( lowerCamelCase_ ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): snake_case_ : str = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # output layer norm snake_case_ : Optional[Any] = np.asarray(weights[7][0] ) snake_case_ : List[Any] = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # output embeddings snake_case_ : Optional[int] = np.asarray(weights[9][0] ) snake_case_ : Any = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # Initialise PyTorch model snake_case_ : List[str] = ReformerConfig.from_json_file(lowerCamelCase_ ) print(F'''Building PyTorch model from configuration: {config}''' ) snake_case_ : str = ReformerModelWithLMHead(lowerCamelCase_ ) with open(lowerCamelCase_ , """rb""" ) as f: snake_case_ : List[Any] = pickle.load(lowerCamelCase_ )["""weights"""] set_model_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , config.hidden_size ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , lowerCamelCase_ ) if __name__ == "__main__": __A : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--trax_model_pkl_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained Reformer model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __A : List[Any] = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
8
1
'''simple docstring''' from __future__ import annotations def UpperCAmelCase ( lowerCamelCase_ :list[int] ): # This function is recursive '''simple docstring''' snake_case_ : Tuple = len(lowerCamelCase_ ) # If the array contains only one element, we return it (it's the stop condition of # recursion) if array_length <= 1: return array # Else snake_case_ : Optional[Any] = array[0] snake_case_ : int = False snake_case_ : Tuple = 1 snake_case_ : list[int] = [] while not is_found and i < array_length: if array[i] < pivot: snake_case_ : Tuple = True snake_case_ : int = [element for element in array[i:] if element >= array[i]] snake_case_ : str = longest_subsequence(lowerCamelCase_ ) if len(lowerCamelCase_ ) > len(lowerCamelCase_ ): snake_case_ : Dict = temp_array else: i += 1 snake_case_ : List[str] = [element for element in array[1:] if element >= pivot] snake_case_ : Any = [pivot, *longest_subsequence(lowerCamelCase_ )] if len(lowerCamelCase_ ) > len(lowerCamelCase_ ): return temp_array else: return longest_subseq if __name__ == "__main__": import doctest doctest.testmod()
8
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[Any] = logging.get_logger(__name__) __A : str = { 'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json', # See all CANINE models at https://huggingface.co/models?filter=canine } class __UpperCamelCase ( lowercase__ ): lowercase : List[Any] = 'canine' def __init__( self :Optional[int] ,_UpperCamelCase :Dict=7_6_8 ,_UpperCamelCase :Union[str, Any]=1_2 ,_UpperCamelCase :int=1_2 ,_UpperCamelCase :int=3_0_7_2 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :Any=0.1 ,_UpperCamelCase :int=0.1 ,_UpperCamelCase :Any=1_6_3_8_4 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :List[str]=0.02 ,_UpperCamelCase :Any=1E-1_2 ,_UpperCamelCase :Tuple=0 ,_UpperCamelCase :List[str]=0xE_0_0_0 ,_UpperCamelCase :Optional[Any]=0xE_0_0_1 ,_UpperCamelCase :str=4 ,_UpperCamelCase :Optional[int]=4 ,_UpperCamelCase :str=8 ,_UpperCamelCase :int=1_6_3_8_4 ,_UpperCamelCase :int=1_2_8 ,**_UpperCamelCase :str ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : List[str] = max_position_embeddings snake_case_ : Union[str, Any] = hidden_size snake_case_ : Dict = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : Tuple = intermediate_size snake_case_ : str = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : Optional[Any] = initializer_range snake_case_ : Optional[int] = type_vocab_size snake_case_ : List[str] = layer_norm_eps # Character config: snake_case_ : Any = downsampling_rate snake_case_ : List[str] = upsampling_kernel_size snake_case_ : int = num_hash_functions snake_case_ : Tuple = num_hash_buckets snake_case_ : Tuple = local_transformer_stride
8
1
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class __UpperCamelCase ( lowercase__ ): lowercase : int = 0 lowercase : bool = False lowercase : float = 3.0 class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :Optional[Any] ): # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() ,{} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() ,{"""a""": 2} ) self.assertDictEqual(MockClass(a=2 ,b=_UpperCamelCase ).to_kwargs() ,{"""a""": 2, """b""": True} ) self.assertDictEqual(MockClass(a=2 ,c=2.25 ).to_kwargs() ,{"""a""": 2, """c""": 2.25} ) @require_cuda def a__ ( self :Optional[int] ): # If no defaults are changed, `to_kwargs` returns an empty dict. snake_case_ : List[Any] = GradScalerKwargs(init_scale=1_0_2_4 ,growth_factor=2 ) AcceleratorState._reset_state() snake_case_ : Union[str, Any] = Accelerator(mixed_precision="""fp16""" ,kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) snake_case_ : Optional[Any] = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale ,10_24.0 ) self.assertEqual(scaler._growth_factor ,2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor ,0.5 ) self.assertEqual(scaler._growth_interval ,2_0_0_0 ) self.assertEqual(scaler._enabled ,_UpperCamelCase ) @require_multi_gpu def a__ ( self :Union[str, Any] ): snake_case_ : Dict = ["""torchrun""", F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_UpperCamelCase ,env=os.environ.copy() ) if __name__ == "__main__": __A : Union[str, Any] = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) __A : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) __A : List[str] = torch.nn.Linear(100, 200) __A : str = accelerator.prepare(model) # Check the values changed in kwargs __A : str = '' __A : List[Any] = model.bucket_bytes_cap // (1_024 * 1_024) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
8
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __A : Tuple = logging.get_logger(__name__) __A : List[Any] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } __A : str = { 'vocab_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json' }, 'merges_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt' }, 'tokenizer_config_file': { 'facebook/blenderbot_small-90M': ( 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json' ) }, } __A : Optional[Any] = { 'facebook/blenderbot_small-90M': 512, } class __UpperCamelCase ( lowercase__ ): lowercase : str = VOCAB_FILES_NAMES lowercase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : Dict = BlenderbotSmallTokenizer def __init__( self :str ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :Tuple="<|endoftext|>" ,_UpperCamelCase :int="<|endoftext|>" ,_UpperCamelCase :Dict="<|endoftext|>" ,_UpperCamelCase :Optional[Any]=False ,_UpperCamelCase :List[Any]=True ,**_UpperCamelCase :Any ,): super().__init__( ByteLevelBPETokenizer( vocab=_UpperCamelCase ,merges=_UpperCamelCase ,add_prefix_space=_UpperCamelCase ,trim_offsets=_UpperCamelCase ,) ,bos_token=_UpperCamelCase ,eos_token=_UpperCamelCase ,unk_token=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Any = add_prefix_space def a__ ( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any]=None ): snake_case_ : List[Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def a__ ( self :int ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): snake_case_ : int = [self.sep_token_id] snake_case_ : Tuple = [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]
8
1
'''simple docstring''' import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __A : str = 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') __A : Optional[int] = parser.parse_args() __A : str = 'cpu' __A : int = 'a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings' __A : Tuple = 'path-to-your-trained-model' __A : Any = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __A : List[Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __A : Optional[Any] = pipe.to(device) # to channels last __A : List[str] = pipe.unet.to(memory_format=torch.channels_last) __A : List[Any] = pipe.vae.to(memory_format=torch.channels_last) __A : str = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __A : Union[str, Any] = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __A : str = torch.randn(2, 4, 64, 64) __A : int = torch.rand(1) * 999 __A : int = torch.randn(2, 77, 768) __A : Dict = (sample, timestep, encoder_hidden_status) try: __A : str = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __A : Optional[int] = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __A : Optional[Any] = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __A : str = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __A : List[Any] = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __A : List[str] = 666 __A : str = torch.Generator(device).manual_seed(seed) __A : int = {'generator': generator} if args.steps is not None: __A : Optional[int] = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __A : Union[str, Any] = pipe(prompt, **generate_kwargs).images[0] # save image image.save('generated.png')
8
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :list ): '''simple docstring''' if len(lowerCamelCase_ ) <= 1: return lst snake_case_ : Union[str, Any] = 1 while i < len(lowerCamelCase_ ): if lst[i - 1] <= lst[i]: i += 1 else: snake_case_ , snake_case_ : Union[str, Any] = lst[i], lst[i - 1] i -= 1 if i == 0: snake_case_ : int = 1 return lst if __name__ == "__main__": __A : Optional[int] = input('Enter numbers separated by a comma:\n').strip() __A : int = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
8
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __A : List[str] = { 'configuration_convnext': ['CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConvNextConfig', 'ConvNextOnnxConfig'] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ['ConvNextFeatureExtractor'] __A : Optional[int] = ['ConvNextImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[int] = [ 'CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST', 'ConvNextForImageClassification', 'ConvNextModel', 'ConvNextPreTrainedModel', 'ConvNextBackbone', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Dict = [ 'TFConvNextForImageClassification', 'TFConvNextModel', 'TFConvNextPreTrainedModel', ] if TYPE_CHECKING: from .configuration_convnext import CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvNextConfig, ConvNextOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_convnext import ConvNextFeatureExtractor from .image_processing_convnext import ConvNextImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convnext import ( CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvNextBackbone, ConvNextForImageClassification, ConvNextModel, ConvNextPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convnext import TFConvNextForImageClassification, TFConvNextModel, TFConvNextPreTrainedModel else: import sys __A : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure)
8
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __UpperCamelCase : def __init__( self :Any ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[int]=1_2 ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Optional[int]=True ,_UpperCamelCase :Union[str, Any]=True ,_UpperCamelCase :Dict=True ,_UpperCamelCase :Optional[int]=9_9 ,_UpperCamelCase :Dict=3_2 ,_UpperCamelCase :Union[str, Any]=3_2 ,_UpperCamelCase :Union[str, Any]=2 ,_UpperCamelCase :Optional[Any]=4 ,_UpperCamelCase :List[Any]=3_7 ,_UpperCamelCase :Tuple=0.1 ,_UpperCamelCase :Optional[int]=0.1 ,_UpperCamelCase :int=5_1_2 ,_UpperCamelCase :Tuple=0.02 ,_UpperCamelCase :Any=0 ,_UpperCamelCase :str=None ,): snake_case_ : str = parent snake_case_ : int = batch_size snake_case_ : Union[str, Any] = seq_length snake_case_ : List[Any] = is_training snake_case_ : Union[str, Any] = use_input_mask snake_case_ : List[str] = use_labels snake_case_ : int = vocab_size snake_case_ : Any = hidden_size snake_case_ : List[Any] = projection_dim snake_case_ : Dict = num_hidden_layers snake_case_ : Dict = num_attention_heads snake_case_ : str = intermediate_size snake_case_ : int = dropout snake_case_ : int = attention_dropout snake_case_ : Dict = max_position_embeddings snake_case_ : Union[str, Any] = initializer_range snake_case_ : Dict = scope snake_case_ : Union[str, Any] = bos_token_id def a__ ( self :Any ): snake_case_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case_ : Union[str, Any] = None if self.use_input_mask: snake_case_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: snake_case_ : int = input_mask.numpy() snake_case_ , snake_case_ : Tuple = input_mask.shape snake_case_ : Any = np.random.randint(1 ,seq_length - 1 ,size=(batch_size,) ) for batch_idx, start_index in enumerate(_UpperCamelCase ): snake_case_ : Optional[int] = 1 snake_case_ : List[str] = 0 snake_case_ : Tuple = self.get_config() return config, input_ids, tf.convert_to_tensor(_UpperCamelCase ) def a__ ( self :str ): return BlipTextConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,projection_dim=self.projection_dim ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,dropout=self.dropout ,attention_dropout=self.attention_dropout ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,bos_token_id=self.bos_token_id ,) def a__ ( self :List[Any] ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[int] ): snake_case_ : List[str] = TFBlipTextModel(config=_UpperCamelCase ) snake_case_ : List[Any] = model(_UpperCamelCase ,attention_mask=_UpperCamelCase ,training=_UpperCamelCase ) snake_case_ : Any = model(_UpperCamelCase ,training=_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def a__ ( self :List[str] ): snake_case_ : Union[str, Any] = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ : str = config_and_inputs snake_case_ : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Optional[Any] = (TFBlipTextModel,) if is_tf_available() else () lowercase : int = False lowercase : List[Any] = False lowercase : Dict = False def a__ ( self :List[Any] ): snake_case_ : List[str] = BlipTextModelTester(self ) snake_case_ : Tuple = ConfigTester(self ,config_class=_UpperCamelCase ,hidden_size=3_7 ) def a__ ( self :Union[str, Any] ): self.config_tester.run_common_tests() def a__ ( self :Union[str, Any] ): snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def a__ ( self :Tuple ): pass def a__ ( self :Tuple ): pass @unittest.skip(reason="""Blip does not use inputs_embeds""" ) def a__ ( self :Any ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :Tuple ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :List[Any] ): pass @slow def a__ ( self :Any ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ : Optional[Any] = TFBlipTextModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def a__ ( self :Dict ,_UpperCamelCase :Tuple=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCamelCase )
8
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : int = { 'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'], 'feature_extraction_whisper': ['WhisperFeatureExtractor'], 'processing_whisper': ['WhisperProcessor'], 'tokenization_whisper': ['WhisperTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FlaxWhisperForConditionalGeneration', 'FlaxWhisperModel', 'FlaxWhisperPreTrainedModel', 'FlaxWhisperForAudioClassification', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys __A : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : int = { 'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'], 'feature_extraction_whisper': ['WhisperFeatureExtractor'], 'processing_whisper': ['WhisperProcessor'], 'tokenization_whisper': ['WhisperTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FlaxWhisperForConditionalGeneration', 'FlaxWhisperModel', 'FlaxWhisperPreTrainedModel', 'FlaxWhisperForAudioClassification', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys __A : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
1
'''simple docstring''' import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :str=True , lowerCamelCase_ :str="pt" ): '''simple docstring''' snake_case_ : Tuple = {"""add_prefix_space""": True} if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and not line.startswith(""" """ ) else {} snake_case_ : Union[str, Any] = padding_side return tokenizer( [line] , max_length=lowerCamelCase_ , padding="""max_length""" if pad_to_max_length else None , truncation=lowerCamelCase_ , return_tensors=lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , **lowerCamelCase_ , ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Any=None , ): '''simple docstring''' snake_case_ : Dict = input_ids.ne(lowerCamelCase_ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[Any] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any="train" ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :int=None ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :Optional[int]="" ,): super().__init__() snake_case_ : List[str] = Path(_UpperCamelCase ).joinpath(type_path + """.source""" ) snake_case_ : int = Path(_UpperCamelCase ).joinpath(type_path + """.target""" ) snake_case_ : Optional[int] = self.get_char_lens(self.src_file ) snake_case_ : List[str] = max_source_length snake_case_ : str = max_target_length assert min(self.src_lens ) > 0, F'''found empty line in {self.src_file}''' snake_case_ : str = tokenizer snake_case_ : str = prefix if n_obs is not None: snake_case_ : int = self.src_lens[:n_obs] snake_case_ : Tuple = src_lang snake_case_ : str = tgt_lang def __len__( self :Any ): return len(self.src_lens ) def __getitem__( self :List[str] ,_UpperCamelCase :Union[str, Any] ): snake_case_ : Optional[int] = index + 1 # linecache starts at 1 snake_case_ : Dict = self.prefix + linecache.getline(str(self.src_file ) ,_UpperCamelCase ).rstrip("""\n""" ) snake_case_ : List[Any] = linecache.getline(str(self.tgt_file ) ,_UpperCamelCase ).rstrip("""\n""" ) assert source_line, F'''empty source line for index {index}''' assert tgt_line, F'''empty tgt line for index {index}''' # Need to add eos token manually for T5 if isinstance(self.tokenizer ,_UpperCamelCase ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right snake_case_ : int = ( self.tokenizer.question_encoder if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer ) snake_case_ : Optional[int] = self.tokenizer.generator if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer snake_case_ : Optional[Any] = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_source_length ,"""right""" ) snake_case_ : Tuple = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_target_length ,"""right""" ) snake_case_ : int = source_inputs["""input_ids"""].squeeze() snake_case_ : str = target_inputs["""input_ids"""].squeeze() snake_case_ : Union[str, Any] = source_inputs["""attention_mask"""].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def a__ ( _UpperCamelCase :str ): return [len(_UpperCamelCase ) for x in Path(_UpperCamelCase ).open().readlines()] def a__ ( self :Optional[int] ,_UpperCamelCase :List[str] ): snake_case_ : Optional[Any] = torch.stack([x["""input_ids"""] for x in batch] ) snake_case_ : List[Any] = torch.stack([x["""attention_mask"""] for x in batch] ) snake_case_ : Union[str, Any] = torch.stack([x["""decoder_input_ids"""] for x in batch] ) snake_case_ : Optional[Any] = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Tuple = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Optional[int] = trim_batch(_UpperCamelCase ,_UpperCamelCase ) snake_case_ , snake_case_ : Dict = trim_batch(_UpperCamelCase ,_UpperCamelCase ,attention_mask=_UpperCamelCase ) snake_case_ : Optional[int] = { """input_ids""": source_ids, """attention_mask""": source_mask, """decoder_input_ids""": y, } return batch __A : List[Any] = getLogger(__name__) def UpperCAmelCase ( lowerCamelCase_ :List[List] ): '''simple docstring''' return list(itertools.chain.from_iterable(lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : int = get_git_info() save_json(lowerCamelCase_ , os.path.join(lowerCamelCase_ , """git_log.json""" ) ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int]=4 , **lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' with open(lowerCamelCase_ , """w""" ) as f: json.dump(lowerCamelCase_ , lowerCamelCase_ , indent=lowerCamelCase_ , **lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] ): '''simple docstring''' with open(lowerCamelCase_ ) as f: return json.load(lowerCamelCase_ ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Optional[Any] = git.Repo(search_parent_directories=lowerCamelCase_ ) snake_case_ : List[str] = { """repo_id""": str(lowerCamelCase_ ), """repo_sha""": str(repo.head.object.hexsha ), """repo_branch""": str(repo.active_branch ), """hostname""": str(socket.gethostname() ), } return repo_infos def UpperCAmelCase ( lowerCamelCase_ :Callable , lowerCamelCase_ :Iterable ): '''simple docstring''' return list(map(lowerCamelCase_ , lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , """wb""" ) as f: return pickle.dump(lowerCamelCase_ , lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' def remove_articles(lowerCamelCase_ :str ): return re.sub(R"""\b(a|an|the)\b""" , """ """ , lowerCamelCase_ ) def white_space_fix(lowerCamelCase_ :Optional[Any] ): return " ".join(text.split() ) def remove_punc(lowerCamelCase_ :Tuple ): snake_case_ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowerCamelCase_ :Optional[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase_ ) ) ) ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' snake_case_ : List[Any] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : Optional[int] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : List[Any] = Counter(lowerCamelCase_ ) & Counter(lowerCamelCase_ ) snake_case_ : Optional[Any] = sum(common.values() ) if num_same == 0: return 0 snake_case_ : Optional[Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Union[str, Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Optional[Any] = (2 * precision * recall) / (precision + recall) return fa def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' return normalize_answer(lowerCamelCase_ ) == normalize_answer(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] ): '''simple docstring''' assert len(lowerCamelCase_ ) == len(lowerCamelCase_ ) snake_case_ : Optional[int] = 0 for hypo, pred in zip(lowerCamelCase_ , lowerCamelCase_ ): em += exact_match_score(lowerCamelCase_ , lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: em /= len(lowerCamelCase_ ) return {"em": em} def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return model_prefix.startswith("""rag""" ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead snake_case_ : Optional[int] = """dropout_rate""" for p in extra_params: if getattr(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): if not hasattr(lowerCamelCase_ , lowerCamelCase_ ) and not hasattr(lowerCamelCase_ , equivalent_param[p] ): logger.info("""config doesn't have a `{}` attribute""".format(lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) continue snake_case_ : str = p if hasattr(lowerCamelCase_ , lowerCamelCase_ ) else equivalent_param[p] setattr(lowerCamelCase_ , lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) return hparams, config
8
'''simple docstring''' import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor __A : Optional[int] = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[str] ,*_UpperCamelCase :str ,**_UpperCamelCase :Optional[int] ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" ,_UpperCamelCase ,) super().__init__(*_UpperCamelCase ,**_UpperCamelCase )
8
1
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType __A : Optional[Any] = logging.get_logger(__name__) __A : Tuple = { 'openai/imagegpt-small': '', 'openai/imagegpt-medium': '', 'openai/imagegpt-large': '', } class __UpperCamelCase ( lowercase__ ): lowercase : Any = 'imagegpt' lowercase : List[str] = ['past_key_values'] lowercase : str = { 'hidden_size': 'n_embd', 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self :Tuple ,_UpperCamelCase :Optional[int]=5_1_2 + 1 ,_UpperCamelCase :Tuple=3_2 * 3_2 ,_UpperCamelCase :Dict=5_1_2 ,_UpperCamelCase :List[Any]=2_4 ,_UpperCamelCase :Any=8 ,_UpperCamelCase :int=None ,_UpperCamelCase :int="quick_gelu" ,_UpperCamelCase :List[str]=0.1 ,_UpperCamelCase :int=0.1 ,_UpperCamelCase :Optional[Any]=0.1 ,_UpperCamelCase :Any=1E-5 ,_UpperCamelCase :Optional[int]=0.02 ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :List[str]=True ,_UpperCamelCase :Dict=False ,_UpperCamelCase :int=False ,_UpperCamelCase :Optional[int]=False ,**_UpperCamelCase :Any ,): snake_case_ : List[str] = vocab_size snake_case_ : Optional[int] = n_positions snake_case_ : Any = n_embd snake_case_ : Optional[int] = n_layer snake_case_ : Optional[int] = n_head snake_case_ : Tuple = n_inner snake_case_ : List[str] = activation_function snake_case_ : str = resid_pdrop snake_case_ : Optional[int] = embd_pdrop snake_case_ : Optional[Any] = attn_pdrop snake_case_ : Tuple = layer_norm_epsilon snake_case_ : Optional[Any] = initializer_range snake_case_ : List[Any] = scale_attn_weights snake_case_ : Tuple = use_cache snake_case_ : Dict = scale_attn_by_inverse_layer_idx snake_case_ : List[str] = reorder_and_upcast_attn snake_case_ : str = tie_word_embeddings super().__init__(tie_word_embeddings=_UpperCamelCase ,**_UpperCamelCase ) class __UpperCamelCase ( lowercase__ ): @property def a__ ( self :int ): return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ] ) def a__ ( self :Tuple ,_UpperCamelCase :"FeatureExtractionMixin" ,_UpperCamelCase :int = 1 ,_UpperCamelCase :int = -1 ,_UpperCamelCase :bool = False ,_UpperCamelCase :Optional["TensorType"] = None ,_UpperCamelCase :int = 3 ,_UpperCamelCase :int = 3_2 ,_UpperCamelCase :int = 3_2 ,): snake_case_ : Dict = self._generate_dummy_images(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ) snake_case_ : List[str] = dict(preprocessor(images=_UpperCamelCase ,return_tensors=_UpperCamelCase ) ) return inputs
8
'''simple docstring''' import re def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[Any] = re.compile( R"""^(?:0|94|\+94|0{2}94)""" R"""7(0|1|2|4|5|6|7|8)""" R"""(-| |)""" R"""\d{7}$""" ) return bool(re.search(lowerCamelCase_ , lowerCamelCase_ ) ) if __name__ == "__main__": __A : int = '0094702343221' print(is_sri_lankan_phone_number(phone))
8
1
'''simple docstring''' import json import os import unittest from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : int = XLMTokenizer lowercase : Tuple = False def a__ ( self :List[str] ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt 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>""", ] snake_case_ : str = dict(zip(_UpperCamelCase ,range(len(_UpperCamelCase ) ) ) ) snake_case_ : Optional[int] = ["""l o 123""", """lo w 1456""", """e r</w> 1789""", """"""] snake_case_ : int = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["""vocab_file"""] ) snake_case_ : str = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file ,"""w""" ) as fp: fp.write(json.dumps(_UpperCamelCase ) ) with open(self.merges_file ,"""w""" ) as fp: fp.write("""\n""".join(_UpperCamelCase ) ) def a__ ( self :Tuple ,_UpperCamelCase :List[str] ): snake_case_ : Union[str, Any] = """lower newer""" snake_case_ : int = """lower newer""" return input_text, output_text def a__ ( self :str ): snake_case_ : Tuple = XLMTokenizer(self.vocab_file ,self.merges_file ) snake_case_ : str = """lower""" snake_case_ : Union[str, Any] = ["""low""", """er</w>"""] snake_case_ : str = tokenizer.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = tokens + ["""<unk>"""] snake_case_ : List[Any] = [1_4, 1_5, 2_0] self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) ,_UpperCamelCase ) @slow def a__ ( self :Optional[int] ): snake_case_ : str = XLMTokenizer.from_pretrained("""xlm-mlm-en-2048""" ) snake_case_ : int = tokenizer.encode("""sequence builders""" ,add_special_tokens=_UpperCamelCase ) snake_case_ : List[Any] = tokenizer.encode("""multi-sequence build""" ,add_special_tokens=_UpperCamelCase ) snake_case_ : int = tokenizer.build_inputs_with_special_tokens(_UpperCamelCase ) snake_case_ : Tuple = tokenizer.build_inputs_with_special_tokens(_UpperCamelCase ,_UpperCamelCase ) assert encoded_sentence == [0] + text + [1] assert encoded_pair == [0] + text + [1] + text_a + [1]
8
'''simple docstring''' from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import BaseOutput, is_torch_available, is_transformers_available @dataclass class __UpperCamelCase ( lowercase__ ): lowercase : Union[List[PIL.Image.Image], np.ndarray] lowercase : Optional[List[bool]] if is_transformers_available() and is_torch_available(): from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
8
1
'''simple docstring''' import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, ByTaTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): __A : Optional[Any] = 'pt' elif is_tf_available(): __A : Optional[int] = 'tf' else: __A : List[Any] = 'jax' class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Any = ByTaTokenizer lowercase : Optional[int] = False def a__ ( self :List[str] ): super().setUp() snake_case_ : Tuple = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self :Tuple ): return ByTaTokenizer.from_pretrained("""google/byt5-small""" ) def a__ ( self :Any ,**_UpperCamelCase :Union[str, Any] ): return self.tokenizer_class.from_pretrained(self.tmpdirname ,**_UpperCamelCase ) def a__ ( self :int ,_UpperCamelCase :List[str] ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Union[str, Any]=2_0 ,_UpperCamelCase :str=5 ): # XXX The default common tokenizer tests assume that every ID is decodable on its own. # This assumption is invalid for ByT5 because single bytes might not be # valid utf-8 (byte 128 for instance). # Here we're overriding the smallest possible method to provide # a clean sequence without making the same assumption. snake_case_ : List[Any] = [] for i in range(len(_UpperCamelCase ) ): try: snake_case_ : Any = tokenizer.decode([i] ,clean_up_tokenization_spaces=_UpperCamelCase ) except UnicodeDecodeError: pass toks.append((i, tok) ) snake_case_ : Optional[int] = list(filter(lambda _UpperCamelCase : re.match(R"""^[ a-zA-Z]+$""" ,t[1] ) ,_UpperCamelCase ) ) snake_case_ : int = list(filter(lambda _UpperCamelCase : [t[0]] == tokenizer.encode(t[1] ,add_special_tokens=_UpperCamelCase ) ,_UpperCamelCase ) ) if max_length is not None and len(_UpperCamelCase ) > max_length: snake_case_ : Dict = toks[:max_length] if min_length is not None and len(_UpperCamelCase ) < min_length and len(_UpperCamelCase ) > 0: while len(_UpperCamelCase ) < min_length: snake_case_ : Any = toks + toks # toks_str = [t[1] for t in toks] snake_case_ : Tuple = [t[0] for t in toks] # Ensure consistency snake_case_ : Optional[Any] = tokenizer.decode(_UpperCamelCase ,clean_up_tokenization_spaces=_UpperCamelCase ) if " " not in output_txt and len(_UpperCamelCase ) > 1: snake_case_ : Dict = ( tokenizer.decode([toks_ids[0]] ,clean_up_tokenization_spaces=_UpperCamelCase ) + """ """ + tokenizer.decode(toks_ids[1:] ,clean_up_tokenization_spaces=_UpperCamelCase ) ) if with_prefix_space: snake_case_ : str = """ """ + output_txt snake_case_ : str = tokenizer.encode(_UpperCamelCase ,add_special_tokens=_UpperCamelCase ) return output_txt, output_ids def a__ ( self :Optional[Any] ): snake_case_ : int = self.ta_base_tokenizer snake_case_ : Dict = tokenizer(["""hi</s>""", """I went to the gym</s>""", """</s>"""] ) snake_case_ : Union[str, Any] = tokenizer(["""hi""", """I went to the gym""", """"""] ) self.assertListEqual(batch_with_eos_added["""input_ids"""] ,batch_without_eos_added["""input_ids"""] ) def a__ ( self :int ): snake_case_ : str = self.ta_base_tokenizer snake_case_ : List[Any] = """Unicode €.""" snake_case_ : Any = tokenizer(_UpperCamelCase ) snake_case_ : int = [8_8, 1_1_3, 1_0_8, 1_0_2, 1_1_4, 1_0_3, 1_0_4, 3_5, 2_2_9, 1_3_3, 1_7_5, 4_9, 1] self.assertEqual(encoded["""input_ids"""] ,_UpperCamelCase ) # decoding snake_case_ : Union[str, Any] = tokenizer.decode(_UpperCamelCase ) self.assertEqual(_UpperCamelCase ,"""Unicode €.</s>""" ) snake_case_ : Optional[int] = tokenizer("""e è é ê ë""" ) snake_case_ : List[Any] = [1_0_4, 3_5, 1_9_8, 1_7_1, 3_5, 1_9_8, 1_7_2, 3_5, 1_9_8, 1_7_3, 3_5, 1_9_8, 1_7_4, 1] self.assertEqual(encoded["""input_ids"""] ,_UpperCamelCase ) # decoding snake_case_ : Optional[int] = tokenizer.decode(_UpperCamelCase ) self.assertEqual(_UpperCamelCase ,"""e è é ê ë</s>""" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("""e è é ê ë""" ) ) ,"""e è é ê ë</s>""" ) def a__ ( self :Optional[Any] ): snake_case_ : List[Any] = self.ta_base_tokenizer snake_case_ : int = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] # fmt: off snake_case_ : List[Any] = [6_8, 3_5, 1_1_1, 1_1_4, 1_1_3, 1_0_6, 3_5, 1_1_5, 1_0_0, 1_1_7, 1_0_0, 1_0_6, 1_1_7, 1_0_0, 1_1_5, 1_0_7, 3_5, 1_0_5, 1_1_4, 1_1_7, 3_5, 1_1_8, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_0_8, 1_2_5, 1_0_0, 1_1_9, 1_0_8, 1_1_4, 1_1_3, 4_9, 1, 0] # fmt: on snake_case_ : Union[str, Any] = tokenizer(_UpperCamelCase ,padding=_UpperCamelCase ,return_tensors=_UpperCamelCase ) self.assertIsInstance(_UpperCamelCase ,_UpperCamelCase ) if FRAMEWORK != "jax": snake_case_ : List[str] = list(batch.input_ids.numpy()[0] ) else: snake_case_ : List[str] = list(batch.input_ids.tolist()[0] ) self.assertListEqual(_UpperCamelCase ,_UpperCamelCase ) self.assertEqual((2, 3_7) ,batch.input_ids.shape ) self.assertEqual((2, 3_7) ,batch.attention_mask.shape ) def a__ ( self :Dict ): snake_case_ : Any = self.ta_base_tokenizer snake_case_ : Union[str, Any] = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] snake_case_ : Dict = tokenizer(_UpperCamelCase ,padding=_UpperCamelCase ,return_tensors=_UpperCamelCase ) # check if input_ids are returned and no decoder_input_ids self.assertIn("""input_ids""" ,_UpperCamelCase ) self.assertIn("""attention_mask""" ,_UpperCamelCase ) self.assertNotIn("""decoder_input_ids""" ,_UpperCamelCase ) self.assertNotIn("""decoder_attention_mask""" ,_UpperCamelCase ) def a__ ( self :Optional[int] ): snake_case_ : Optional[Any] = self.ta_base_tokenizer snake_case_ : str = [ """Summary of the text.""", """Another summary.""", ] snake_case_ : int = tokenizer( text_target=_UpperCamelCase ,max_length=3_2 ,padding="""max_length""" ,truncation=_UpperCamelCase ,return_tensors=_UpperCamelCase ) self.assertEqual(3_2 ,targets["""input_ids"""].shape[1] ) def a__ ( self :Union[str, Any] ): snake_case_ : int = self.ta_base_tokenizer snake_case_ : int = ["""A long paragraph for summarization. </s>"""] snake_case_ : Any = ["""Summary of the text. </s>"""] # fmt: off snake_case_ : List[str] = [6_8, 3_5, 1_1_1, 1_1_4, 1_1_3, 1_0_6, 3_5, 1_1_5, 1_0_0, 1_1_7, 1_0_0, 1_0_6, 1_1_7, 1_0_0, 1_1_5, 1_0_7, 3_5, 1_0_5, 1_1_4, 1_1_7, 3_5, 1_1_8, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_0_8, 1_2_5, 1_0_0, 1_1_9, 1_0_8, 1_1_4, 1_1_3, 4_9, 3_5, 1] snake_case_ : List[str] = [8_6, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_2_4, 3_5, 1_1_4, 1_0_5, 3_5, 1_1_9, 1_0_7, 1_0_4, 3_5, 1_1_9, 1_0_4, 1_2_3, 1_1_9, 4_9, 3_5, 1] # fmt: on snake_case_ : Dict = tokenizer(_UpperCamelCase ,text_target=_UpperCamelCase ) self.assertEqual(_UpperCamelCase ,batch["""input_ids"""][0] ) self.assertEqual(_UpperCamelCase ,batch["""labels"""][0] ) def a__ ( self :int ): # safety check on max_len default value so we are sure the test works snake_case_ : int = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): self.assertNotEqual(tokenizer.model_max_length ,4_2 ) # Now let's start the test snake_case_ : Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc snake_case_ : str = tempfile.mkdtemp() snake_case_ : int = """ He is very happy, UNwant\u00E9d,running""" snake_case_ : str = tokenizer.encode(_UpperCamelCase ,add_special_tokens=_UpperCamelCase ) tokenizer.save_pretrained(_UpperCamelCase ) snake_case_ : List[str] = tokenizer.__class__.from_pretrained(_UpperCamelCase ) snake_case_ : Union[str, Any] = after_tokenizer.encode(_UpperCamelCase ,add_special_tokens=_UpperCamelCase ) self.assertListEqual(_UpperCamelCase ,_UpperCamelCase ) shutil.rmtree(_UpperCamelCase ) snake_case_ : Tuple = self.get_tokenizers(model_max_length=4_2 ) for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc snake_case_ : Optional[int] = tempfile.mkdtemp() snake_case_ : Dict = """ He is very happy, UNwant\u00E9d,running""" tokenizer.add_tokens(["""bim""", """bambam"""] ) snake_case_ : Optional[int] = tokenizer.additional_special_tokens additional_special_tokens.append("""new_additional_special_token""" ) tokenizer.add_special_tokens({"""additional_special_tokens""": additional_special_tokens} ) snake_case_ : str = tokenizer.encode(_UpperCamelCase ,add_special_tokens=_UpperCamelCase ) tokenizer.save_pretrained(_UpperCamelCase ) snake_case_ : Optional[int] = tokenizer.__class__.from_pretrained(_UpperCamelCase ) snake_case_ : Optional[Any] = after_tokenizer.encode(_UpperCamelCase ,add_special_tokens=_UpperCamelCase ) self.assertListEqual(_UpperCamelCase ,_UpperCamelCase ) self.assertIn("""new_additional_special_token""" ,after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length ,4_2 ) snake_case_ : List[str] = tokenizer.__class__.from_pretrained(_UpperCamelCase ,model_max_length=4_3 ) self.assertEqual(tokenizer.model_max_length ,4_3 ) shutil.rmtree(_UpperCamelCase ) def a__ ( self :Union[str, Any] ): snake_case_ : Tuple = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(_UpperCamelCase ) with open(os.path.join(_UpperCamelCase ,"""special_tokens_map.json""" ) ,encoding="""utf-8""" ) as json_file: snake_case_ : str = json.load(_UpperCamelCase ) with open(os.path.join(_UpperCamelCase ,"""tokenizer_config.json""" ) ,encoding="""utf-8""" ) as json_file: snake_case_ : Any = json.load(_UpperCamelCase ) snake_case_ : Any = [F'''<extra_id_{i}>''' for i in range(1_2_5 )] snake_case_ : Optional[Any] = added_tokens_extra_ids + [ """an_additional_special_token""" ] snake_case_ : List[str] = added_tokens_extra_ids + [ """an_additional_special_token""" ] with open(os.path.join(_UpperCamelCase ,"""special_tokens_map.json""" ) ,"""w""" ,encoding="""utf-8""" ) as outfile: json.dump(_UpperCamelCase ,_UpperCamelCase ) with open(os.path.join(_UpperCamelCase ,"""tokenizer_config.json""" ) ,"""w""" ,encoding="""utf-8""" ) as outfile: json.dump(_UpperCamelCase ,_UpperCamelCase ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files snake_case_ : str = tokenizer_class.from_pretrained( _UpperCamelCase ,) self.assertIn( """an_additional_special_token""" ,tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( ["""an_additional_special_token"""] ,tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["""an_additional_special_token"""] ) ) ,) # Now we test that we can change the value of additional_special_tokens in the from_pretrained snake_case_ : List[Any] = added_tokens_extra_ids + [AddedToken("""a_new_additional_special_token""" ,lstrip=_UpperCamelCase )] snake_case_ : Optional[int] = tokenizer_class.from_pretrained( _UpperCamelCase ,additional_special_tokens=_UpperCamelCase ,) self.assertIn("""a_new_additional_special_token""" ,tokenizer.additional_special_tokens ) self.assertEqual( ["""a_new_additional_special_token"""] ,tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(["""a_new_additional_special_token"""] ) ) ,) def a__ ( self :int ): snake_case_ : Tuple = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(_UpperCamelCase ) snake_case_ : Optional[Any] = tokenizer_class.from_pretrained(_UpperCamelCase ) self.assertTrue(tokenizer.decode([2_5_5] ) == """""" ) def a__ ( self :Tuple ): pass def a__ ( self :Any ): pass def a__ ( self :List[Any] ): pass def a__ ( self :str ): pass def a__ ( self :Dict ): # The default common tokenizer tests uses invalid tokens for ByT5 that can only accept one-character strings # and special added tokens as tokens snake_case_ : List[Any] = self.get_tokenizers(fast=_UpperCamelCase ,do_lower_case=_UpperCamelCase ) for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): snake_case_ : Tuple = ["""t""", """h""", """i""", """s""", """ """, """i""", """s""", """ """, """a""", """ """, """t""", """e""", """x""", """t""", """</s>"""] snake_case_ : Optional[int] = tokenizer.convert_tokens_to_string(_UpperCamelCase ) self.assertIsInstance(_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :Any ): snake_case_ : Any = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): snake_case_ : List[Any] = [ """bos_token""", """eos_token""", """unk_token""", """sep_token""", """pad_token""", """cls_token""", """mask_token""", ] snake_case_ : Tuple = 0 snake_case_ : int = tokenizer.convert_ids_to_tokens( _UpperCamelCase ,skip_special_tokens=_UpperCamelCase ) for attr in attributes_list: setattr(_UpperCamelCase ,attr + """_id""" ,_UpperCamelCase ) self.assertEqual(getattr(_UpperCamelCase ,_UpperCamelCase ) ,_UpperCamelCase ) self.assertEqual(getattr(_UpperCamelCase ,attr + """_id""" ) ,_UpperCamelCase ) setattr(_UpperCamelCase ,attr + """_id""" ,_UpperCamelCase ) self.assertEqual(getattr(_UpperCamelCase ,_UpperCamelCase ) ,_UpperCamelCase ) self.assertEqual(getattr(_UpperCamelCase ,attr + """_id""" ) ,_UpperCamelCase ) setattr(_UpperCamelCase ,"""additional_special_tokens_ids""" ,[] ) self.assertListEqual(getattr(_UpperCamelCase ,"""additional_special_tokens""" ) ,[] ) self.assertListEqual(getattr(_UpperCamelCase ,"""additional_special_tokens_ids""" ) ,[] ) setattr(_UpperCamelCase ,"""additional_special_tokens_ids""" ,[token_id_to_test_setters] ) self.assertListEqual(getattr(_UpperCamelCase ,"""additional_special_tokens""" ) ,[token_to_test_setters] ) self.assertListEqual(getattr(_UpperCamelCase ,"""additional_special_tokens_ids""" ) ,[token_id_to_test_setters] )
8
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , unittest.TestCase ): lowercase : Dict = StableDiffusionInpaintPipeline lowercase : str = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS lowercase : Union[str, Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS lowercase : Dict = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowercase : Optional[int] = frozenset([] ) def a__ ( self :Any ): torch.manual_seed(0 ) snake_case_ : Optional[int] = UNetaDConditionModel( block_out_channels=(3_2, 6_4) ,layers_per_block=2 ,sample_size=3_2 ,in_channels=9 ,out_channels=4 ,down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,cross_attention_dim=3_2 ,attention_head_dim=(2, 4) ,use_linear_projection=_UpperCamelCase ,) snake_case_ : Tuple = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) torch.manual_seed(0 ) snake_case_ : List[str] = AutoencoderKL( block_out_channels=[3_2, 6_4] ,in_channels=3 ,out_channels=3 ,down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,latent_channels=4 ,sample_size=1_2_8 ,) torch.manual_seed(0 ) snake_case_ : Optional[int] = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=3_2 ,intermediate_size=3_7 ,layer_norm_eps=1E-0_5 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1_0_0_0 ,hidden_act="""gelu""" ,projection_dim=5_1_2 ,) snake_case_ : Tuple = CLIPTextModel(_UpperCamelCase ) snake_case_ : Optional[int] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) snake_case_ : str = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def a__ ( self :str ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Union[str, Any]=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched snake_case_ : List[Any] = floats_tensor((1, 3, 3_2, 3_2) ,rng=random.Random(_UpperCamelCase ) ).to(_UpperCamelCase ) snake_case_ : int = image.cpu().permute(0 ,2 ,3 ,1 )[0] snake_case_ : List[str] = Image.fromarray(np.uinta(_UpperCamelCase ) ).convert("""RGB""" ).resize((6_4, 6_4) ) snake_case_ : Optional[Any] = Image.fromarray(np.uinta(image + 4 ) ).convert("""RGB""" ).resize((6_4, 6_4) ) if str(_UpperCamelCase ).startswith("""mps""" ): snake_case_ : Optional[Any] = torch.manual_seed(_UpperCamelCase ) else: snake_case_ : Optional[int] = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) snake_case_ : int = { """prompt""": """A painting of a squirrel eating a burger""", """image""": init_image, """mask_image""": mask_image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def a__ ( self :Any ): snake_case_ : Union[str, Any] = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case_ : Optional[Any] = self.get_dummy_components() snake_case_ : Dict = StableDiffusionInpaintPipeline(**_UpperCamelCase ) snake_case_ : List[str] = sd_pipe.to(_UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ : Union[str, Any] = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ : Tuple = sd_pipe(**_UpperCamelCase ).images snake_case_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case_ : Dict = np.array([0.47_27, 0.57_35, 0.39_41, 0.54_46, 0.59_26, 0.43_94, 0.50_62, 0.46_54, 0.44_76] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def a__ ( self :Any ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :List[Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self :Tuple ): snake_case_ : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : List[str] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench.npy""" ) snake_case_ : str = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : Optional[Any] = StableDiffusionInpaintPipeline.from_pretrained(_UpperCamelCase ,safety_checker=_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() snake_case_ : Optional[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : List[str] = torch.manual_seed(0 ) snake_case_ : Dict = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""np""" ,) snake_case_ : Union[str, Any] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 9E-3 def a__ ( self :Tuple ): snake_case_ : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : List[str] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench_fp16.npy""" ) snake_case_ : Optional[int] = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : List[str] = StableDiffusionInpaintPipeline.from_pretrained( _UpperCamelCase ,torch_dtype=torch.floataa ,safety_checker=_UpperCamelCase ,) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() snake_case_ : Optional[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : List[Any] = torch.manual_seed(0 ) snake_case_ : Any = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""np""" ,) snake_case_ : List[str] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 5E-1 def a__ ( self :Union[str, Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case_ : Optional[int] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : int = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : Dict = PNDMScheduler.from_pretrained(_UpperCamelCase ,subfolder="""scheduler""" ) snake_case_ : List[Any] = StableDiffusionInpaintPipeline.from_pretrained( _UpperCamelCase ,safety_checker=_UpperCamelCase ,scheduler=_UpperCamelCase ,torch_dtype=torch.floataa ,) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case_ : List[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : Optional[int] = torch.manual_seed(0 ) snake_case_ : Tuple = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,num_inference_steps=2 ,output_type="""np""" ,) snake_case_ : Any = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 1_0**9
8
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __A : Tuple = { 'configuration_blip_2': [ 'BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Blip2Config', 'Blip2QFormerConfig', 'Blip2VisionConfig', ], 'processing_blip_2': ['Blip2Processor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[Any] = [ 'BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST', 'Blip2Model', 'Blip2QFormerModel', 'Blip2PreTrainedModel', 'Blip2ForConditionalGeneration', 'Blip2VisionModel', ] if TYPE_CHECKING: from .configuration_blip_a import ( BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipaConfig, BlipaQFormerConfig, BlipaVisionConfig, ) from .processing_blip_a import BlipaProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip_a import ( BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST, BlipaForConditionalGeneration, BlipaModel, BlipaPreTrainedModel, BlipaQFormerModel, BlipaVisionModel, ) else: import sys __A : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
'''simple docstring''' import collections import os import re from pathlib import Path __A : Dict = 'src/transformers' # Matches is_xxx_available() __A : Dict = re.compile(r'is\_([a-z_]*)_available()') # Catches a one-line _import_struct = {xxx} __A : Any = re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __A : Tuple = re.compile(r'\s+"\S*":\s+\[([^\]]*)\]') # Catches a line if not is_foo_available __A : Optional[Any] = re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)') # Catches a line _import_struct["bla"].append("foo") __A : Optional[int] = re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __A : List[Any] = re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]') # Catches a line with an object between quotes and a comma: "MyModel", __A : Union[str, Any] = re.compile(r'^\s+"([^"]+)",') # Catches a line with objects between brackets only: ["foo", "bar"], __A : int = re.compile(r'^\s+\[([^\]]+)\]') # Catches a line with from foo import bar, bla, boo __A : int = re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') # Catches a line with try: __A : List[Any] = re.compile(r'^\s*try:') # Catches a line with else: __A : Any = re.compile(r'^\s*else:') def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' if _re_test_backend.search(lowerCamelCase_ ) is None: return None snake_case_ : Tuple = [b[0] for b in _re_backend.findall(lowerCamelCase_ )] backends.sort() return "_and_".join(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] ): '''simple docstring''' with open(lowerCamelCase_ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: snake_case_ : str = f.readlines() snake_case_ : List[Any] = 0 while line_index < len(lowerCamelCase_ ) and not lines[line_index].startswith("""_import_structure = {""" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(lowerCamelCase_ ): return None # First grab the objects without a specific backend in _import_structure snake_case_ : Union[str, Any] = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: snake_case_ : str = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(lowerCamelCase_ ): snake_case_ : Optional[int] = _re_one_line_import_struct.search(lowerCamelCase_ ).groups()[0] snake_case_ : Union[str, Any] = re.findall(R"""\[([^\]]+)\]""" , lowerCamelCase_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue snake_case_ : Any = _re_import_struct_key_value.search(lowerCamelCase_ ) if single_line_import_search is not None: snake_case_ : Optional[int] = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(""", """ ) if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) line_index += 1 snake_case_ : Union[str, Any] = {"""none""": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("""if TYPE_CHECKING""" ): # If the line is an if not is_backend_available, we grab all objects associated. snake_case_ : List[str] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : Tuple = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Dict = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): snake_case_ : List[Any] = lines[line_index] if _re_import_struct_add_one.search(lowerCamelCase_ ) is not None: objects.append(_re_import_struct_add_one.search(lowerCamelCase_ ).groups()[0] ) elif _re_import_struct_add_many.search(lowerCamelCase_ ) is not None: snake_case_ : Optional[int] = _re_import_struct_add_many.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : List[str] = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_between_brackets.search(lowerCamelCase_ ) is not None: snake_case_ : List[str] = _re_between_brackets.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : Any = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_quote_object.search(lowerCamelCase_ ) is not None: objects.append(_re_quote_object.search(lowerCamelCase_ ).groups()[0] ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) elif line.startswith(""" """ * 12 + """\"""" ): objects.append(line[13:-3] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend snake_case_ : List[Any] = [] while ( line_index < len(lowerCamelCase_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): snake_case_ : Union[str, Any] = lines[line_index] snake_case_ : Union[str, Any] = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 snake_case_ : Dict = {"""none""": objects} # Let's continue with backend-specific objects while line_index < len(lowerCamelCase_ ): # If the line is an if is_backend_available, we grab all objects associated. snake_case_ : Optional[Any] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : str = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Any = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): snake_case_ : Dict = lines[line_index] snake_case_ : Any = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 12 ): objects.append(line[12:-2] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 return import_dict_objects, type_hint_objects def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :List[str] ): '''simple docstring''' def find_duplicates(lowerCamelCase_ :Union[str, Any] ): return [k for k, v in collections.Counter(lowerCamelCase_ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] snake_case_ : Optional[int] = [] for key in import_dict_objects.keys(): snake_case_ : int = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) snake_case_ : List[str] = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): snake_case_ : str = """base imports""" if key == """none""" else F'''{key} backend''' errors.append(F'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Tuple = [] for root, _, files in os.walk(lowerCamelCase_ ): if "__init__.py" in files: snake_case_ : Any = os.path.join(lowerCamelCase_ , """__init__.py""" ) snake_case_ : Dict = parse_init(lowerCamelCase_ ) if objects is not None: snake_case_ : Any = analyze_results(*lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: snake_case_ : Tuple = F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append("""\n""".join(lowerCamelCase_ ) ) if len(lowerCamelCase_ ) > 0: raise ValueError("""\n\n""".join(lowerCamelCase_ ) ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Union[str, Any] = [] for path, directories, files in os.walk(lowerCamelCase_ ): for folder in directories: # Ignore private modules if folder.startswith("""_""" ): directories.remove(lowerCamelCase_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(lowerCamelCase_ ) / folder).glob("""*.py""" ) ) ) == 0: continue snake_case_ : Tuple = str((Path(lowerCamelCase_ ) / folder).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(os.path.sep , """.""" ) submodules.append(lowerCamelCase_ ) for fname in files: if fname == "__init__.py": continue snake_case_ : Dict = str((Path(lowerCamelCase_ ) / fname).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(lowerCamelCase_ ) return submodules __A : List[Any] = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', 'models.esm.openfold_utils', ] def UpperCAmelCase ( ): '''simple docstring''' # This is to make sure the transformers module imported is the one in the repo. from transformers.utils import direct_transformers_import snake_case_ : Union[str, Any] = direct_transformers_import(lowerCamelCase_ ) snake_case_ : List[str] = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(lowerCamelCase_ , """__init__.py""" ) , """r""" ) as f: snake_case_ : str = f.read() import_structure_keys.update(set(re.findall(R"""import_structure\[\"([^\"]*)\"\]""" , lowerCamelCase_ ) ) ) snake_case_ : Dict = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(lowerCamelCase_ ) > 0: snake_case_ : str = """\n""".join(F'''- {module}''' for module in module_not_registered ) raise ValueError( """The following submodules are not properly registed in the main init of Transformers:\n""" F'''{list_of_modules}\n''' """Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.""" ) if __name__ == "__main__": check_all_inits() check_submodules()
8
1
'''simple docstring''' from __future__ import annotations import os from collections.abc import Mapping __A : Union[str, Any] = tuple[int, int] class __UpperCamelCase : def __init__( self :Dict ,_UpperCamelCase :set[int] ,_UpperCamelCase :Mapping[EdgeT, int] ): snake_case_ : set[int] = vertices snake_case_ : dict[EdgeT, int] = { (min(_UpperCamelCase ), max(_UpperCamelCase )): weight for edge, weight in edges.items() } def a__ ( self :Union[str, Any] ,_UpperCamelCase :EdgeT ,_UpperCamelCase :int ): self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) snake_case_ : Optional[Any] = weight def a__ ( self :str ): snake_case_ : Graph = Graph({min(self.vertices )} ,{} ) snake_case_ : EdgeT snake_case_ : int snake_case_ : EdgeT snake_case_ : int while len(subgraph.vertices ) < len(self.vertices ): snake_case_ : Tuple = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: snake_case_ : Optional[Any] = edge snake_case_ : str = weight subgraph.add_edge(_UpperCamelCase ,_UpperCamelCase ) return subgraph def UpperCAmelCase ( lowerCamelCase_ :str = "p107_network.txt" ): '''simple docstring''' snake_case_ : str = os.path.abspath(os.path.dirname(lowerCamelCase_ ) ) snake_case_ : str = os.path.join(lowerCamelCase_ , lowerCamelCase_ ) snake_case_ : dict[EdgeT, int] = {} snake_case_ : list[str] snake_case_ : int snake_case_ : int with open(lowerCamelCase_ ) as f: snake_case_ : str = f.read().strip().split("""\n""" ) snake_case_ : Tuple = [line.split(""",""" ) for line in data] for edgea in range(1 , len(lowerCamelCase_ ) ): for edgea in range(lowerCamelCase_ ): if adjaceny_matrix[edgea][edgea] != "-": snake_case_ : str = int(adjaceny_matrix[edgea][edgea] ) snake_case_ : Graph = Graph(set(range(len(lowerCamelCase_ ) ) ) , lowerCamelCase_ ) snake_case_ : Graph = graph.prims_algorithm() snake_case_ : int = sum(graph.edges.values() ) snake_case_ : int = sum(subgraph.edges.values() ) return initial_total - optimal_total if __name__ == "__main__": print(F'{solution() = }')
8
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __UpperCamelCase ( unittest.TestCase ): def __init__( self :List[Any] ,_UpperCamelCase :List[str] ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Union[str, Any]=3 ,_UpperCamelCase :Any=1_8 ,_UpperCamelCase :Optional[Any]=3_0 ,_UpperCamelCase :List[str]=4_0_0 ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :List[Any]=True ,): snake_case_ : List[str] = size if size is not None else {"""height""": 1_8, """width""": 1_8} snake_case_ : Union[str, Any] = parent snake_case_ : str = batch_size snake_case_ : List[Any] = num_channels snake_case_ : Tuple = image_size snake_case_ : int = min_resolution snake_case_ : int = max_resolution snake_case_ : Union[str, Any] = do_resize snake_case_ : Optional[Any] = size snake_case_ : Any = apply_ocr def a__ ( self :Union[str, Any] ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Tuple = LayoutLMvaImageProcessor if is_pytesseract_available() else None def a__ ( self :List[Any] ): snake_case_ : Union[str, Any] = LayoutLMvaImageProcessingTester(self ) @property def a__ ( self :int ): return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self :Any ): snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase ,"""do_resize""" ) ) self.assertTrue(hasattr(_UpperCamelCase ,"""size""" ) ) self.assertTrue(hasattr(_UpperCamelCase ,"""apply_ocr""" ) ) def a__ ( self :int ): snake_case_ : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 1_8, """width""": 1_8} ) snake_case_ : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict ,size=4_2 ) self.assertEqual(image_processor.size ,{"""height""": 4_2, """width""": 4_2} ) def a__ ( self :Optional[Any] ): pass def a__ ( self :Union[str, Any] ): # Initialize image_processing snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ : List[str] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,Image.Image ) # Test not batched input snake_case_ : List[str] = image_processing(image_inputs[0] ,return_tensors="""pt""" ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) self.assertIsInstance(encoding.words ,_UpperCamelCase ) self.assertIsInstance(encoding.boxes ,_UpperCamelCase ) # Test batched snake_case_ : List[Any] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :Tuple ): # Initialize image_processing snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ,numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,np.ndarray ) # Test not batched input snake_case_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Any = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :Optional[Any] ): # Initialize image_processing snake_case_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ : Optional[int] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ,torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,torch.Tensor ) # Test not batched input snake_case_ : Tuple = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Union[str, Any] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :List[Any] ): # with apply_OCR = True snake_case_ : Any = LayoutLMvaImageProcessor() from datasets import load_dataset snake_case_ : List[Any] = load_dataset("""hf-internal-testing/fixtures_docvqa""" ,split="""test""" ) snake_case_ : str = Image.open(ds[0]["""file"""] ).convert("""RGB""" ) snake_case_ : Dict = image_processing(_UpperCamelCase ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 2_2_4, 2_2_4) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 snake_case_ : Tuple = [["""11:14""", """to""", """11:39""", """a.m""", """11:39""", """to""", """11:44""", """a.m.""", """11:44""", """a.m.""", """to""", """12:25""", """p.m.""", """12:25""", """to""", """12:58""", """p.m.""", """12:58""", """to""", """4:00""", """p.m.""", """2:00""", """to""", """5:00""", """p.m.""", """Coffee""", """Break""", """Coffee""", """will""", """be""", """served""", """for""", """men""", """and""", """women""", """in""", """the""", """lobby""", """adjacent""", """to""", """exhibit""", """area.""", """Please""", """move""", """into""", """exhibit""", """area.""", """(Exhibits""", """Open)""", """TRRF""", """GENERAL""", """SESSION""", """(PART""", """|)""", """Presiding:""", """Lee""", """A.""", """Waller""", """TRRF""", """Vice""", """President""", """“Introductory""", """Remarks”""", """Lee""", """A.""", """Waller,""", """TRRF""", """Vice""", """Presi-""", """dent""", """Individual""", """Interviews""", """with""", """TRRF""", """Public""", """Board""", """Members""", """and""", """Sci-""", """entific""", """Advisory""", """Council""", """Mem-""", """bers""", """Conducted""", """by""", """TRRF""", """Treasurer""", """Philip""", """G.""", """Kuehn""", """to""", """get""", """answers""", """which""", """the""", """public""", """refrigerated""", """warehousing""", """industry""", """is""", """looking""", """for.""", """Plus""", """questions""", """from""", """the""", """floor.""", """Dr.""", """Emil""", """M.""", """Mrak,""", """University""", """of""", """Cal-""", """ifornia,""", """Chairman,""", """TRRF""", """Board;""", """Sam""", """R.""", """Cecil,""", """University""", """of""", """Georgia""", """College""", """of""", """Agriculture;""", """Dr.""", """Stanley""", """Charm,""", """Tufts""", """University""", """School""", """of""", """Medicine;""", """Dr.""", """Robert""", """H.""", """Cotton,""", """ITT""", """Continental""", """Baking""", """Company;""", """Dr.""", """Owen""", """Fennema,""", """University""", """of""", """Wis-""", """consin;""", """Dr.""", """Robert""", """E.""", """Hardenburg,""", """USDA.""", """Questions""", """and""", """Answers""", """Exhibits""", """Open""", """Capt.""", """Jack""", """Stoney""", """Room""", """TRRF""", """Scientific""", """Advisory""", """Council""", """Meeting""", """Ballroom""", """Foyer"""]] # noqa: E231 snake_case_ : Any = [[[1_4_1, 5_7, 2_1_4, 6_9], [2_2_8, 5_8, 2_5_2, 6_9], [1_4_1, 7_5, 2_1_6, 8_8], [2_3_0, 7_9, 2_8_0, 8_8], [1_4_2, 2_6_0, 2_1_8, 2_7_3], [2_3_0, 2_6_1, 2_5_5, 2_7_3], [1_4_3, 2_7_9, 2_1_8, 2_9_0], [2_3_1, 2_8_2, 2_9_0, 2_9_1], [1_4_3, 3_4_2, 2_1_8, 3_5_4], [2_3_1, 3_4_5, 2_8_9, 3_5_5], [2_0_2, 3_6_2, 2_2_7, 3_7_3], [1_4_3, 3_7_9, 2_2_0, 3_9_2], [2_3_1, 3_8_2, 2_9_1, 3_9_4], [1_4_4, 7_1_4, 2_2_0, 7_2_6], [2_3_1, 7_1_5, 2_5_6, 7_2_6], [1_4_4, 7_3_2, 2_2_0, 7_4_5], [2_3_2, 7_3_6, 2_9_1, 7_4_7], [1_4_4, 7_6_9, 2_1_8, 7_8_2], [2_3_1, 7_7_0, 2_5_6, 7_8_2], [1_4_1, 7_8_8, 2_0_2, 8_0_1], [2_1_5, 7_9_1, 2_7_4, 8_0_4], [1_4_3, 8_2_6, 2_0_4, 8_3_8], [2_1_5, 8_2_6, 2_4_0, 8_3_8], [1_4_2, 8_4_4, 2_0_2, 8_5_7], [2_1_5, 8_4_7, 2_7_4, 8_5_9], [3_3_4, 5_7, 4_2_7, 6_9], [4_4_0, 5_7, 5_2_2, 6_9], [3_6_9, 7_5, 4_6_1, 8_8], [4_6_9, 7_5, 5_1_6, 8_8], [5_2_8, 7_6, 5_6_2, 8_8], [5_7_0, 7_6, 6_6_7, 8_8], [6_7_5, 7_5, 7_1_1, 8_7], [7_2_1, 7_9, 7_7_8, 8_8], [7_8_9, 7_5, 8_4_0, 8_8], [3_6_9, 9_7, 4_7_0, 1_0_7], [4_8_4, 9_4, 5_0_7, 1_0_6], [5_1_8, 9_4, 5_6_2, 1_0_7], [5_7_6, 9_4, 6_5_5, 1_1_0], [6_6_8, 9_4, 7_9_2, 1_0_9], [8_0_4, 9_5, 8_2_9, 1_0_7], [3_6_9, 1_1_3, 4_6_5, 1_2_5], [4_7_7, 1_1_6, 5_4_7, 1_2_5], [5_6_2, 1_1_3, 6_5_8, 1_2_5], [6_7_1, 1_1_6, 7_4_8, 1_2_5], [7_6_1, 1_1_3, 8_1_1, 1_2_5], [3_6_9, 1_3_1, 4_6_5, 1_4_3], [4_7_7, 1_3_3, 5_4_8, 1_4_3], [5_6_3, 1_3_0, 6_9_8, 1_4_5], [7_1_0, 1_3_0, 8_0_2, 1_4_6], [3_3_6, 1_7_1, 4_1_2, 1_8_3], [4_2_3, 1_7_1, 5_7_2, 1_8_3], [5_8_2, 1_7_0, 7_1_6, 1_8_4], [7_2_8, 1_7_1, 8_1_7, 1_8_7], [8_2_9, 1_7_1, 8_4_4, 1_8_6], [3_3_8, 1_9_7, 4_8_2, 2_1_2], [5_0_7, 1_9_6, 5_5_7, 2_0_9], [5_6_9, 1_9_6, 5_9_5, 2_0_8], [6_1_0, 1_9_6, 7_0_2, 2_0_9], [5_0_5, 2_1_4, 5_8_3, 2_2_6], [5_9_5, 2_1_4, 6_5_6, 2_2_7], [6_7_0, 2_1_5, 8_0_7, 2_2_7], [3_3_5, 2_5_9, 5_4_3, 2_7_4], [5_5_6, 2_5_9, 7_0_8, 2_7_2], [3_7_2, 2_7_9, 4_2_2, 2_9_1], [4_3_5, 2_7_9, 4_6_0, 2_9_1], [4_7_4, 2_7_9, 5_7_4, 2_9_2], [5_8_7, 2_7_8, 6_6_4, 2_9_1], [6_7_6, 2_7_8, 7_3_8, 2_9_1], [7_5_1, 2_7_9, 8_3_4, 2_9_1], [3_7_2, 2_9_8, 4_3_4, 3_1_0], [3_3_5, 3_4_1, 4_8_3, 3_5_4], [4_9_7, 3_4_1, 6_5_5, 3_5_4], [6_6_7, 3_4_1, 7_2_8, 3_5_4], [7_4_0, 3_4_1, 8_2_5, 3_5_4], [3_3_5, 3_6_0, 4_3_0, 3_7_2], [4_4_2, 3_6_0, 5_3_4, 3_7_2], [5_4_5, 3_5_9, 6_8_7, 3_7_2], [6_9_7, 3_6_0, 7_5_4, 3_7_2], [7_6_5, 3_6_0, 8_2_3, 3_7_3], [3_3_4, 3_7_8, 4_2_8, 3_9_1], [4_4_0, 3_7_8, 5_7_7, 3_9_4], [5_9_0, 3_7_8, 7_0_5, 3_9_1], [7_2_0, 3_7_8, 8_0_1, 3_9_1], [3_3_4, 3_9_7, 4_0_0, 4_0_9], [3_7_0, 4_1_6, 5_2_9, 4_2_9], [5_4_4, 4_1_6, 5_7_6, 4_3_2], [5_8_7, 4_1_6, 6_6_5, 4_2_8], [6_7_7, 4_1_6, 8_1_4, 4_2_9], [3_7_2, 4_3_5, 4_5_2, 4_5_0], [4_6_5, 4_3_4, 4_9_5, 4_4_7], [5_1_1, 4_3_4, 6_0_0, 4_4_7], [6_1_1, 4_3_6, 6_3_7, 4_4_7], [6_4_9, 4_3_6, 6_9_4, 4_5_1], [7_0_5, 4_3_8, 8_2_4, 4_4_7], [3_6_9, 4_5_3, 4_5_2, 4_6_6], [4_6_4, 4_5_4, 5_0_9, 4_6_6], [5_2_2, 4_5_3, 6_1_1, 4_6_9], [6_2_5, 4_5_3, 7_9_2, 4_6_9], [3_7_0, 4_7_2, 5_5_6, 4_8_8], [5_7_0, 4_7_2, 6_8_4, 4_8_7], [6_9_7, 4_7_2, 7_1_8, 4_8_5], [7_3_2, 4_7_2, 8_3_5, 4_8_8], [3_6_9, 4_9_0, 4_1_1, 5_0_3], [4_2_5, 4_9_0, 4_8_4, 5_0_3], [4_9_6, 4_9_0, 6_3_5, 5_0_6], [6_4_5, 4_9_0, 7_0_7, 5_0_3], [7_1_8, 4_9_1, 7_6_1, 5_0_3], [7_7_1, 4_9_0, 8_4_0, 5_0_3], [3_3_6, 5_1_0, 3_7_4, 5_2_1], [3_8_8, 5_1_0, 4_4_7, 5_2_2], [4_6_0, 5_1_0, 4_8_9, 5_2_1], [5_0_3, 5_1_0, 5_8_0, 5_2_2], [5_9_2, 5_0_9, 7_3_6, 5_2_5], [7_4_5, 5_0_9, 7_7_0, 5_2_2], [7_8_1, 5_0_9, 8_4_0, 5_2_2], [3_3_8, 5_2_8, 4_3_4, 5_4_1], [4_4_8, 5_2_8, 5_9_6, 5_4_1], [6_0_9, 5_2_7, 6_8_7, 5_4_0], [7_0_0, 5_2_8, 7_9_2, 5_4_1], [3_3_6, 5_4_6, 3_9_7, 5_5_9], [4_0_7, 5_4_6, 4_3_1, 5_5_9], [4_4_3, 5_4_6, 5_2_5, 5_6_0], [5_3_7, 5_4_6, 6_8_0, 5_6_2], [6_8_8, 5_4_6, 7_1_4, 5_5_9], [7_2_2, 5_4_6, 8_3_7, 5_6_2], [3_3_6, 5_6_5, 4_4_9, 5_8_1], [4_6_1, 5_6_5, 4_8_5, 5_7_7], [4_9_7, 5_6_5, 6_6_5, 5_8_1], [6_8_1, 5_6_5, 7_1_8, 5_7_7], [7_3_2, 5_6_5, 8_3_7, 5_8_0], [3_3_7, 5_8_4, 4_3_8, 5_9_7], [4_5_2, 5_8_3, 5_2_1, 5_9_6], [5_3_5, 5_8_4, 6_7_7, 5_9_9], [6_9_0, 5_8_3, 7_8_7, 5_9_6], [8_0_1, 5_8_3, 8_2_5, 5_9_6], [3_3_8, 6_0_2, 4_7_8, 6_1_5], [4_9_2, 6_0_2, 5_3_0, 6_1_4], [5_4_3, 6_0_2, 6_3_8, 6_1_5], [6_5_0, 6_0_2, 6_7_6, 6_1_4], [6_8_8, 6_0_2, 7_8_8, 6_1_5], [8_0_2, 6_0_2, 8_4_3, 6_1_4], [3_3_7, 6_2_1, 5_0_2, 6_3_3], [5_1_6, 6_2_1, 6_1_5, 6_3_7], [6_2_9, 6_2_1, 7_7_4, 6_3_6], [7_8_9, 6_2_1, 8_2_7, 6_3_3], [3_3_7, 6_3_9, 4_1_8, 6_5_2], [4_3_2, 6_4_0, 5_7_1, 6_5_3], [5_8_7, 6_3_9, 7_3_1, 6_5_5], [7_4_3, 6_3_9, 7_6_9, 6_5_2], [7_8_0, 6_3_9, 8_4_1, 6_5_2], [3_3_8, 6_5_8, 4_4_0, 6_7_3], [4_5_5, 6_5_8, 4_9_1, 6_7_0], [5_0_8, 6_5_8, 6_0_2, 6_7_1], [6_1_6, 6_5_8, 6_3_8, 6_7_0], [6_5_4, 6_5_8, 8_3_5, 6_7_4], [3_3_7, 6_7_7, 4_2_9, 6_8_9], [3_3_7, 7_1_4, 4_8_2, 7_2_6], [4_9_5, 7_1_4, 5_4_8, 7_2_6], [5_6_1, 7_1_4, 6_8_3, 7_2_6], [3_3_8, 7_7_0, 4_6_1, 7_8_2], [4_7_4, 7_6_9, 5_5_4, 7_8_5], [4_8_9, 7_8_8, 5_6_2, 8_0_3], [5_7_6, 7_8_8, 6_4_3, 8_0_1], [6_5_6, 7_8_7, 7_5_1, 8_0_4], [7_6_4, 7_8_8, 8_4_4, 8_0_1], [3_3_4, 8_2_5, 4_2_1, 8_3_8], [4_3_0, 8_2_4, 5_7_4, 8_3_8], [5_8_4, 8_2_4, 7_2_3, 8_4_1], [3_3_5, 8_4_4, 4_5_0, 8_5_7], [4_6_4, 8_4_3, 5_8_3, 8_6_0], [6_2_8, 8_6_2, 7_5_5, 8_7_5], [7_6_9, 8_6_1, 8_4_8, 8_7_8]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,_UpperCamelCase ) self.assertListEqual(encoding.boxes ,_UpperCamelCase ) # with apply_OCR = False snake_case_ : Dict = LayoutLMvaImageProcessor(apply_ocr=_UpperCamelCase ) snake_case_ : Optional[int] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 2_2_4, 2_2_4) )
8
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[str] = logging.get_logger(__name__) __A : List[str] = { 'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/config.json', 'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/config.json' # See all FNet models at https://huggingface.co/models?filter=fnet } class __UpperCamelCase ( lowercase__ ): lowercase : Dict = 'fnet' def __init__( self :List[Any] ,_UpperCamelCase :Optional[Any]=3_2_0_0_0 ,_UpperCamelCase :List[Any]=7_6_8 ,_UpperCamelCase :List[Any]=1_2 ,_UpperCamelCase :List[Any]=3_0_7_2 ,_UpperCamelCase :List[str]="gelu_new" ,_UpperCamelCase :str=0.1 ,_UpperCamelCase :List[Any]=5_1_2 ,_UpperCamelCase :Union[str, Any]=4 ,_UpperCamelCase :Optional[Any]=0.02 ,_UpperCamelCase :int=1E-1_2 ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :int=5_1_2 ,_UpperCamelCase :int=3 ,_UpperCamelCase :Any=1 ,_UpperCamelCase :Tuple=2 ,**_UpperCamelCase :List[str] ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Any = vocab_size snake_case_ : Tuple = max_position_embeddings snake_case_ : Dict = hidden_size snake_case_ : List[str] = num_hidden_layers snake_case_ : Optional[int] = intermediate_size snake_case_ : List[Any] = hidden_act snake_case_ : Dict = hidden_dropout_prob snake_case_ : Optional[Any] = initializer_range snake_case_ : Any = type_vocab_size snake_case_ : Optional[int] = layer_norm_eps snake_case_ : Union[str, Any] = use_tpu_fourier_optimizations snake_case_ : Any = tpu_short_seq_length
8
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : List[Any] = generate_pascal_triangle(lowerCamelCase_ ) for row_idx in range(lowerCamelCase_ ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=""" """ ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=""" """ ) else: print(triangle[row_idx][col_idx] , end="""""" ) print() def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError("""The input value of 'num_rows' should be 'int'""" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( """The input value of 'num_rows' should be greater than or equal to 0""" ) snake_case_ : list[list[int]] = [] for current_row_idx in range(lowerCamelCase_ ): snake_case_ : List[str] = populate_current_row(lowerCamelCase_ , lowerCamelCase_ ) triangle.append(lowerCamelCase_ ) return triangle def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Union[str, Any] = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 snake_case_ , snake_case_ : Optional[Any] = 1, 1 for current_col_idx in range(1 , lowerCamelCase_ ): calculate_current_element( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) return current_row def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :list[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , ): '''simple docstring''' snake_case_ : Union[str, Any] = triangle[current_row_idx - 1][current_col_idx - 1] snake_case_ : List[Any] = triangle[current_row_idx - 1][current_col_idx] snake_case_ : Optional[int] = above_to_left_elt + above_to_right_elt def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError("""The input value of 'num_rows' should be 'int'""" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( """The input value of 'num_rows' should be greater than or equal to 0""" ) snake_case_ : list[list[int]] = [[1]] for row_index in range(1 , lowerCamelCase_ ): snake_case_ : Optional[Any] = [0] + result[-1] + [0] snake_case_ : Dict = row_index + 1 # Calculate the number of distinct elements in a row snake_case_ : Any = sum(divmod(lowerCamelCase_ , 2 ) ) snake_case_ : Tuple = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] snake_case_ : Optional[int] = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() snake_case_ : str = row_first_half + row_second_half result.append(lowerCamelCase_ ) return result def UpperCAmelCase ( ): '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(lowerCamelCase_ :Callable , lowerCamelCase_ :int ) -> None: snake_case_ : Dict = F'''{func.__name__}({value})''' snake_case_ : Dict = timeit(F'''__main__.{call}''' , setup="""import __main__""" ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(F'''{call:38} -- {timing:.4f} seconds''' ) for value in range(15 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(lowerCamelCase_ , lowerCamelCase_ ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
8
1
'''simple docstring''' import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import numpy as np import pytest from datasets.arrow_dataset import Dataset from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex from .utils import require_elasticsearch, require_faiss __A : Tuple = pytest.mark.integration @require_faiss class __UpperCamelCase ( SCREAMING_SNAKE_CASE_ ): def a__ ( self :Any ): snake_case_ : Dict = Dataset.from_dict({"""filename""": ["""my_name-train""" + """_""" + str(__a ) for x in np.arange(3_0 ).tolist()]} ) return dset def a__ ( self :Union[str, Any] ): import faiss snake_case_ : Dataset = self._create_dummy_dataset() snake_case_ : Dict = dset.map( lambda _UpperCamelCase ,_UpperCamelCase : {"vecs": i * np.ones(5 ,dtype=np.floataa )} ,with_indices=__a ,keep_in_memory=__a ) snake_case_ : List[Any] = dset.add_faiss_index("""vecs""" ,batch_size=1_0_0 ,metric_type=faiss.METRIC_INNER_PRODUCT ) snake_case_ : Any = dset.get_nearest_examples("""vecs""" ,np.ones(5 ,dtype=np.floataa ) ) self.assertEqual(examples["""filename"""][0] ,"""my_name-train_29""" ) dset.drop_index("""vecs""" ) def a__ ( self :Tuple ): import faiss snake_case_ : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((3_0, 5) ) * np.arange(3_0 ).reshape(-1 ,1 ) ,index_name="""vecs""" ,batch_size=1_0_0 ,metric_type=faiss.METRIC_INNER_PRODUCT ,) snake_case_ : Any = dset.get_nearest_examples("""vecs""" ,np.ones(5 ,dtype=np.floataa ) ) self.assertEqual(examples["""filename"""][0] ,"""my_name-train_29""" ) def a__ ( self :List[Any] ): import faiss snake_case_ : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((3_0, 5) ) * np.arange(3_0 ).reshape(-1 ,1 ) ,index_name="""vecs""" ,metric_type=faiss.METRIC_INNER_PRODUCT ,) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=__a ) as tmp_file: dset.save_faiss_index("""vecs""" ,tmp_file.name ) dset.load_faiss_index("""vecs2""" ,tmp_file.name ) os.unlink(tmp_file.name ) snake_case_ : str = dset.get_nearest_examples("""vecs2""" ,np.ones(5 ,dtype=np.floataa ) ) self.assertEqual(examples["""filename"""][0] ,"""my_name-train_29""" ) def a__ ( self :Union[str, Any] ): snake_case_ : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((3_0, 5) ) * np.arange(3_0 ).reshape(-1 ,1 ) ,index_name="""vecs""" ) dset.drop_index("""vecs""" ) self.assertRaises(__a ,partial(dset.get_nearest_examples ,"""vecs2""" ,np.ones(5 ,dtype=np.floataa ) ) ) def a__ ( self :List[str] ): from elasticsearch import Elasticsearch snake_case_ : Dataset = self._create_dummy_dataset() with patch("""elasticsearch.Elasticsearch.search""" ) as mocked_search, patch( """elasticsearch.client.IndicesClient.create""" ) as mocked_index_create, patch("""elasticsearch.helpers.streaming_bulk""" ) as mocked_bulk: snake_case_ : Any = {'acknowledged': True} mocked_bulk.return_value([(True, None)] * 3_0 ) snake_case_ : Dict = {'hits': {'hits': [{'_score': 1, '_id': 2_9}]}} snake_case_ : Union[str, Any] = Elasticsearch() dset.add_elasticsearch_index("""filename""" ,es_client=__a ) snake_case_ : str = dset.get_nearest_examples("""filename""" ,"""my_name-train_29""" ) self.assertEqual(examples["""filename"""][0] ,"""my_name-train_29""" ) @require_faiss class __UpperCamelCase ( SCREAMING_SNAKE_CASE_ ): def a__ ( self :str ): import faiss snake_case_ : int = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) # add vectors index.add_vectors(np.eye(5 ,dtype=np.floataa ) ) self.assertIsNotNone(index.faiss_index ) self.assertEqual(index.faiss_index.ntotal ,5 ) index.add_vectors(np.zeros((5, 5) ,dtype=np.floataa ) ) self.assertEqual(index.faiss_index.ntotal ,1_0 ) # single query snake_case_ : Dict = np.zeros(5 ,dtype=np.floataa ) snake_case_ : List[str] = 1 snake_case_ : List[Any] = index.search(__a ) self.assertRaises(__a ,index.search ,query.reshape(-1 ,1 ) ) self.assertGreater(scores[0] ,0 ) self.assertEqual(indices[0] ,1 ) # batched queries snake_case_ : List[str] = np.eye(5 ,dtype=np.floataa )[::-1] snake_case_ : Dict = index.search_batch(__a ) self.assertRaises(__a ,index.search_batch ,queries[0] ) snake_case_ : Any = [scores[0] for scores in total_scores] snake_case_ : List[Any] = [indices[0] for indices in total_indices] self.assertGreater(np.min(__a ) ,0 ) self.assertListEqual([4, 3, 2, 1, 0] ,__a ) def a__ ( self :int ): import faiss snake_case_ : int = FaissIndex(string_factory="""Flat""" ) index.add_vectors(np.eye(5 ,dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index ,faiss.IndexFlat ) snake_case_ : List[str] = FaissIndex(string_factory="""LSH""" ) index.add_vectors(np.eye(5 ,dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index ,faiss.IndexLSH ) with self.assertRaises(__a ): snake_case_ : Dict = FaissIndex(string_factory="""Flat""" ,custom_index=faiss.IndexFlat(5 ) ) def a__ ( self :str ): import faiss snake_case_ : Tuple = faiss.IndexFlat(5 ) snake_case_ : List[Any] = FaissIndex(custom_index=__a ) index.add_vectors(np.eye(5 ,dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index ,faiss.IndexFlat ) def a__ ( self :List[Any] ): import faiss snake_case_ : Optional[Any] = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 ,dtype=np.floataa ) ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=__a ) as tmp_file: index.save(tmp_file.name ) snake_case_ : List[Any] = FaissIndex.load(tmp_file.name ) os.unlink(tmp_file.name ) snake_case_ : List[Any] = np.zeros(5 ,dtype=np.floataa ) snake_case_ : Any = 1 snake_case_ : int = index.search(__a ) self.assertGreater(scores[0] ,0 ) self.assertEqual(indices[0] ,1 ) @require_faiss def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' import faiss snake_case_ : int = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) snake_case_ : Dict = 'index.faiss' snake_case_ : Any = F'''mock://{index_name}''' index.save(_UpperCAmelCase , storage_options=mockfs.storage_options ) snake_case_ : Any = FaissIndex.load(_UpperCAmelCase , storage_options=mockfs.storage_options ) snake_case_ : Any = np.zeros(5 , dtype=np.floataa ) snake_case_ : Any = 1 snake_case_ : Tuple = index.search(_UpperCAmelCase ) assert scores[0] > 0 assert indices[0] == 1 @require_elasticsearch class __UpperCamelCase ( SCREAMING_SNAKE_CASE_ ): def a__ ( self :List[str] ): from elasticsearch import Elasticsearch with patch("""elasticsearch.Elasticsearch.search""" ) as mocked_search, patch( """elasticsearch.client.IndicesClient.create""" ) as mocked_index_create, patch("""elasticsearch.helpers.streaming_bulk""" ) as mocked_bulk: snake_case_ : int = Elasticsearch() snake_case_ : Dict = {'acknowledged': True} snake_case_ : List[Any] = ElasticSearchIndex(es_client=__a ) mocked_bulk.return_value([(True, None)] * 3 ) index.add_documents(["""foo""", """bar""", """foobar"""] ) # single query snake_case_ : Optional[Any] = 'foo' snake_case_ : int = {'hits': {'hits': [{'_score': 1, '_id': 0}]}} snake_case_ : List[Any] = index.search(__a ) self.assertEqual(scores[0] ,1 ) self.assertEqual(indices[0] ,0 ) # single query with timeout snake_case_ : Dict = 'foo' snake_case_ : Dict = {'hits': {'hits': [{'_score': 1, '_id': 0}]}} snake_case_ : Optional[Any] = index.search(__a ,request_timeout=3_0 ) self.assertEqual(scores[0] ,1 ) self.assertEqual(indices[0] ,0 ) # batched queries snake_case_ : List[Any] = ['foo', 'bar', 'foobar'] snake_case_ : str = {'hits': {'hits': [{'_score': 1, '_id': 1}]}} snake_case_ : Any = index.search_batch(__a ) snake_case_ : Any = [scores[0] for scores in total_scores] snake_case_ : Tuple = [indices[0] for indices in total_indices] self.assertGreater(np.min(__a ) ,0 ) self.assertListEqual([1, 1, 1] ,__a ) # batched queries with timeout snake_case_ : Tuple = ['foo', 'bar', 'foobar'] snake_case_ : List[Any] = {'hits': {'hits': [{'_score': 1, '_id': 1}]}} snake_case_ : int = index.search_batch(__a ,request_timeout=3_0 ) snake_case_ : Any = [scores[0] for scores in total_scores] snake_case_ : Dict = [indices[0] for indices in total_indices] self.assertGreater(np.min(__a ) ,0 ) self.assertListEqual([1, 1, 1] ,__a )
350
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class __UpperCamelCase ( unittest.TestCase ): @slow def a__ ( self :Dict ): snake_case_ : Optional[Any] = XLMRobertaModel.from_pretrained("""xlm-roberta-base""" ) snake_case_ : Optional[int] = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house snake_case_ : Tuple = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim snake_case_ : Dict = torch.tensor( [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): snake_case_ : Tuple = model(_UpperCamelCase )["""last_hidden_state"""].detach() self.assertEqual(output.shape ,_UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,_UpperCamelCase ,atol=1E-3 ) ) @slow def a__ ( self :Union[str, Any] ): snake_case_ : List[Any] = XLMRobertaModel.from_pretrained("""xlm-roberta-large""" ) snake_case_ : Dict = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house snake_case_ : List[Any] = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim snake_case_ : Any = torch.tensor( [[-0.06_99, -0.03_18, 0.07_05, -0.12_41, 0.09_99, -0.05_20, 0.10_04, -0.18_38, -0.47_04, 0.14_37, 0.08_21, 0.01_26]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): snake_case_ : str = model(_UpperCamelCase )["""last_hidden_state"""].detach() self.assertEqual(output.shape ,_UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,_UpperCamelCase ,atol=1E-3 ) )
8
0
'''simple docstring''' __A : List[Any] = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} __A : Union[str, Any] = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def UpperCAmelCase ( lowerCamelCase_ :dict[int, list[int]] , lowerCamelCase_ :int , lowerCamelCase_ :list[bool] ): '''simple docstring''' snake_case_ : Dict = True snake_case_ : Tuple = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(lowercase__ , lowercase__ , lowercase__ ) order.append(lowercase__ ) return order def UpperCAmelCase ( lowerCamelCase_ :dict[int, list[int]] , lowerCamelCase_ :int , lowerCamelCase_ :list[bool] ): '''simple docstring''' snake_case_ : Union[str, Any] = True snake_case_ : List[str] = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(lowercase__ , lowercase__ , lowercase__ ) return component def UpperCAmelCase ( lowerCamelCase_ :dict[int, list[int]] ): '''simple docstring''' snake_case_ : int = len(lowercase__ ) * [False] snake_case_ : dict[int, list[int]] = {vert: [] for vert in range(len(lowercase__ ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(lowercase__ ) snake_case_ : str = [] for i, was_visited in enumerate(lowercase__ ): if not was_visited: order += topology_sort(lowercase__ , lowercase__ , lowercase__ ) snake_case_ : Union[str, Any] = [] snake_case_ : Union[str, Any] = len(lowercase__ ) * [False] for i in range(len(lowercase__ ) ): snake_case_ : Optional[Any] = order[len(lowercase__ ) - i - 1] if not visited[vert]: snake_case_ : Optional[Any] = find_components(lowercase__ , lowercase__ , lowercase__ ) components_list.append(lowercase__ ) return components_list
351
'''simple docstring''' from __future__ import annotations from collections.abc import Callable def UpperCAmelCase ( lowerCamelCase_ :Callable[[int | float], int | float] , lowerCamelCase_ :int | float , lowerCamelCase_ :int | float , lowerCamelCase_ :int = 1_00 , ): '''simple docstring''' snake_case_ : Tuple = x_start snake_case_ : Optional[int] = fnc(lowerCamelCase_ ) snake_case_ : Optional[int] = 0.0 for _ in range(lowerCamelCase_ ): # Approximates small segments of curve as linear and solve # for trapezoidal area snake_case_ : int = (x_end - x_start) / steps + xa snake_case_ : Union[str, Any] = fnc(lowerCamelCase_ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step snake_case_ : Any = xa snake_case_ : str = fxa return area if __name__ == "__main__": def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return x**3 + x**2 print('f(x) = x^3 + x^2') print('The area between the curve, x = -5, x = 5 and the x axis is:') __A : List[str] = 10 while i <= 100_000: print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}') i *= 10
8
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __A : Optional[Any] = logging.get_logger(__name__) __A : List[str] = { 'microsoft/beit-base-patch16-224-pt22k': ( 'https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json' ), # See all BEiT models at https://huggingface.co/models?filter=beit } class __UpperCamelCase ( _UpperCAmelCase ): lowercase : Optional[Any] = "beit" def __init__( self :Any ,_UpperCamelCase :Dict=8_1_9_2 ,_UpperCamelCase :Tuple=7_6_8 ,_UpperCamelCase :str=1_2 ,_UpperCamelCase :str=1_2 ,_UpperCamelCase :Optional[int]=3_0_7_2 ,_UpperCamelCase :str="gelu" ,_UpperCamelCase :Any=0.0 ,_UpperCamelCase :Optional[Any]=0.0 ,_UpperCamelCase :Union[str, Any]=0.02 ,_UpperCamelCase :List[Any]=1E-1_2 ,_UpperCamelCase :Optional[int]=2_2_4 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :Union[str, Any]=3 ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Any=False ,_UpperCamelCase :Any=False ,_UpperCamelCase :Optional[int]=False ,_UpperCamelCase :Any=0.1 ,_UpperCamelCase :Union[str, Any]=0.1 ,_UpperCamelCase :List[str]=True ,_UpperCamelCase :Dict=[3, 5, 7, 1_1] ,_UpperCamelCase :str=[1, 2, 3, 6] ,_UpperCamelCase :str=True ,_UpperCamelCase :str=0.4 ,_UpperCamelCase :Any=2_5_6 ,_UpperCamelCase :List[str]=1 ,_UpperCamelCase :Optional[Any]=False ,_UpperCamelCase :Tuple=2_5_5 ,**_UpperCamelCase :Dict ,): super().__init__(**_UpperCAmelCase ) snake_case_ : Union[str, Any] = vocab_size snake_case_ : List[Any] = hidden_size snake_case_ : Optional[int] = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : int = intermediate_size snake_case_ : List[str] = hidden_act snake_case_ : List[Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : List[str] = initializer_range snake_case_ : Optional[int] = layer_norm_eps snake_case_ : int = image_size snake_case_ : Tuple = patch_size snake_case_ : int = num_channels snake_case_ : Optional[Any] = use_mask_token snake_case_ : List[Any] = use_absolute_position_embeddings snake_case_ : Optional[int] = use_relative_position_bias snake_case_ : Optional[int] = use_shared_relative_position_bias snake_case_ : Optional[Any] = layer_scale_init_value snake_case_ : Union[str, Any] = drop_path_rate snake_case_ : Tuple = use_mean_pooling # decode head attributes (semantic segmentation) snake_case_ : Tuple = out_indices snake_case_ : Optional[int] = pool_scales # auxiliary head attributes (semantic segmentation) snake_case_ : List[str] = use_auxiliary_head snake_case_ : Optional[Any] = auxiliary_loss_weight snake_case_ : str = auxiliary_channels snake_case_ : List[str] = auxiliary_num_convs snake_case_ : Tuple = auxiliary_concat_input snake_case_ : Dict = semantic_loss_ignore_index class __UpperCamelCase ( _UpperCAmelCase ): lowercase : Dict = version.parse('1.11' ) @property def a__ ( self :Dict ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def a__ ( self :Dict ): return 1E-4
352
'''simple docstring''' import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) __A : int = logging.getLogger() def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[Any] = argparse.ArgumentParser() parser.add_argument("""-f""" ) snake_case_ : int = parser.parse_args() return args.f def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[Any] = {} snake_case_ : Optional[Any] = os.path.join(lowerCamelCase_ , """all_results.json""" ) if os.path.exists(lowerCamelCase_ ): with open(lowerCamelCase_ , """r""" ) as f: snake_case_ : str = json.load(lowerCamelCase_ ) else: raise ValueError(F'''can\'t find {path}''' ) return results def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[str] = torch.cuda.is_available() and torch_device == """cuda""" return is_using_cuda and is_apex_available() __A : Any = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __UpperCamelCase ( lowercase__ ): @classmethod def a__ ( cls :Dict ): # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU snake_case_ : Optional[int] = tempfile.mkdtemp() snake_case_ : Any = os.path.join(cls.tmpdir ,"""default_config.yml""" ) write_basic_config(save_location=cls.configPath ) snake_case_ : List[Any] = ["""accelerate""", """launch""", """--config_file""", cls.configPath] @classmethod def a__ ( cls :int ): shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Optional[int] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.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 --seed=42 --checkpointing_steps epoch --with_tracking '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : Dict = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""glue_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking '''.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,1_0_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""clm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.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} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,4_2 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""mlm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu snake_case_ : Dict = 7 if get_gpu_count() > 1 else 2 snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : str = F''' {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.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} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertLess(result["""train_loss"""] ,0.5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""ner_no_trainer""" ) ) ) @unittest.skip(reason="""Fix me @muellerzr""" ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[str] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : Optional[int] = F''' {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.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} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result["""eval_f1"""] ,2_8 ) self.assertGreaterEqual(result["""eval_exact"""] ,2_8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""qa_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Union[str, Any] = F''' {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Union[str, Any] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""swag_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[Any] = F''' {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.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 --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : int = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_rouge1"""] ,1_0 ) self.assertGreaterEqual(result["""eval_rouge2"""] ,2 ) self.assertGreaterEqual(result["""eval_rougeL"""] ,7 ) self.assertGreaterEqual(result["""eval_rougeLsum"""] ,7 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""summarization_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : Tuple = self.get_auto_remove_tmp_dir() snake_case_ : Optional[Any] = F''' {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Any = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_bleu"""] ,3_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""translation_no_trainer""" ) ) ) @slow def a__ ( self :Optional[Any] ): snake_case_ : List[str] = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCamelCase ) snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_overall_accuracy"""] ,0.10 ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Any ): snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # The base model scores a 25% self.assertGreaterEqual(result["""eval_accuracy"""] ,0.6 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""step_1""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""image_classification_no_trainer""" ) ) )
8
0
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :list[int] , lowerCamelCase_ :list[int] , lowerCamelCase_ :int ): '''simple docstring''' return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__lowerCAmelCase ) ) def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :int , lowerCamelCase_ :list[int] , lowerCamelCase_ :int ): '''simple docstring''' # Base Case if index == len(__lowerCAmelCase ): return True # Recursive Step for i in range(__lowerCAmelCase ): if valid_coloring(graph[index] , __lowerCAmelCase , __lowerCAmelCase ): # Color current vertex snake_case_ : Optional[int] = i # Validate coloring if util_color(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , index + 1 ): return True # Backtrack snake_case_ : Union[str, Any] = -1 return False def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : str = [-1] * len(__lowerCAmelCase ) if util_color(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , 0 ): return colored_vertices return []
353
'''simple docstring''' from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __A : Tuple = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): lowercase : str = ['input_values', 'padding_mask'] def __init__( self :Optional[int] ,_UpperCamelCase :int = 1 ,_UpperCamelCase :int = 2_4_0_0_0 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :float = None ,_UpperCamelCase :float = None ,**_UpperCamelCase :List[Any] ,): super().__init__(feature_size=_UpperCamelCase ,sampling_rate=_UpperCamelCase ,padding_value=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Dict = chunk_length_s snake_case_ : str = overlap @property def a__ ( self :Any ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def a__ ( self :List[str] ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 ,int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self :Optional[Any] ,_UpperCamelCase :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,_UpperCamelCase :Optional[Union[bool, str, PaddingStrategy]] = None ,_UpperCamelCase :Optional[bool] = False ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :Optional[Union[str, TensorType]] = None ,_UpperCamelCase :Optional[int] = None ,): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) if padding and truncation: raise ValueError("""Both padding and truncation were set. Make sure you only set one.""" ) elif padding is None: # by default let's pad the inputs snake_case_ : Tuple = True snake_case_ : str = bool( isinstance(_UpperCamelCase ,(list, tuple) ) and (isinstance(raw_audio[0] ,(np.ndarray, tuple, list) )) ) if is_batched: snake_case_ : Any = [np.asarray(_UpperCamelCase ,dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_UpperCamelCase ,np.ndarray ): snake_case_ : Optional[int] = np.asarray(_UpperCamelCase ,dtype=np.floataa ) elif isinstance(_UpperCamelCase ,np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): snake_case_ : List[str] = raw_audio.astype(np.floataa ) # always return batch if not is_batched: snake_case_ : Optional[Any] = [np.asarray(_UpperCamelCase ).T] # verify inputs are valid for idx, example in enumerate(_UpperCamelCase ): if example.ndim > 2: raise ValueError(F'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(F'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(F'''Expected stereo audio but example has {example.shape[-1]} channels''' ) snake_case_ : Tuple = None snake_case_ : Optional[Any] = BatchFeature({"""input_values""": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: snake_case_ : Union[str, Any] = min(array.shape[0] for array in raw_audio ) snake_case_ : Dict = int(np.floor(max_length / self.chunk_stride ) ) snake_case_ : Union[str, Any] = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: snake_case_ : Any = max(array.shape[0] for array in raw_audio ) snake_case_ : List[Any] = int(np.ceil(max_length / self.chunk_stride ) ) snake_case_ : Any = (nb_step - 1) * self.chunk_stride + self.chunk_length snake_case_ : Union[str, Any] = """max_length""" else: snake_case_ : int = input_values # normal padding on batch if padded_inputs is None: snake_case_ : Optional[int] = self.pad( _UpperCamelCase ,max_length=_UpperCamelCase ,truncation=_UpperCamelCase ,padding=_UpperCamelCase ,return_attention_mask=_UpperCamelCase ,) if padding: snake_case_ : Tuple = padded_inputs.pop("""attention_mask""" ) snake_case_ : Optional[int] = [] for example in padded_inputs.pop("""input_values""" ): if self.feature_size == 1: snake_case_ : Dict = example[..., None] input_values.append(example.T ) snake_case_ : List[Any] = input_values if return_tensors is not None: snake_case_ : Tuple = padded_inputs.convert_to_tensors(_UpperCamelCase ) return padded_inputs
8
0
'''simple docstring''' from collections.abc import Callable import numpy as np def UpperCAmelCase ( lowerCamelCase_ :Callable , lowerCamelCase_ :float , lowerCamelCase_ :float , lowerCamelCase_ :float , lowerCamelCase_ :float ): '''simple docstring''' snake_case_ : str = int(np.ceil((x_end - xa) / step_size ) ) snake_case_ : List[str] = np.zeros((n + 1,) ) snake_case_ : List[str] = ya snake_case_ : Union[str, Any] = xa for k in range(lowerCamelCase__ ): snake_case_ : Union[str, Any] = y[k] + step_size * ode_func(lowerCamelCase__ , y[k] ) snake_case_ : Optional[Any] = y[k] + ( (step_size / 2) * (ode_func(lowerCamelCase__ , y[k] ) + ode_func(x + step_size , lowerCamelCase__ )) ) x += step_size return y if __name__ == "__main__": import doctest doctest.testmod()
354
'''simple docstring''' from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig __A : Dict = { 'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json', 'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json', } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[int] = 'ernie_m' lowercase : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self :Optional[Any] ,_UpperCamelCase :int = 2_5_0_0_0_2 ,_UpperCamelCase :int = 7_6_8 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 3_0_7_2 ,_UpperCamelCase :str = "gelu" ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :int = 5_1_4 ,_UpperCamelCase :float = 0.02 ,_UpperCamelCase :int = 1 ,_UpperCamelCase :float = 1E-0_5 ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Optional[int]=0.0 ,**_UpperCamelCase :List[Any] ,): super().__init__(pad_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Optional[int] = vocab_size snake_case_ : Any = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : Union[str, Any] = num_attention_heads snake_case_ : Any = intermediate_size snake_case_ : Any = hidden_act snake_case_ : Tuple = hidden_dropout_prob snake_case_ : Union[str, Any] = attention_probs_dropout_prob snake_case_ : str = max_position_embeddings snake_case_ : int = initializer_range snake_case_ : Optional[Any] = layer_norm_eps snake_case_ : Union[str, Any] = classifier_dropout snake_case_ : Tuple = is_decoder snake_case_ : int = act_dropout
8
0
'''simple docstring''' from __future__ import annotations def UpperCAmelCase ( lowerCamelCase_ :list[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :int ): '''simple docstring''' if (direction == 1 and array[indexa] > array[indexa]) or ( direction == 0 and array[indexa] < array[indexa] ): snake_case_ : Tuple = array[indexa], array[indexa] def UpperCAmelCase ( lowerCamelCase_ :list[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :int ): '''simple docstring''' if length > 1: snake_case_ : str = int(length / 2 ) for i in range(__a , low + middle ): comp_and_swap(__a , __a , i + middle , __a ) bitonic_merge(__a , __a , __a , __a ) bitonic_merge(__a , low + middle , __a , __a ) def UpperCAmelCase ( lowerCamelCase_ :list[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :int ): '''simple docstring''' if length > 1: snake_case_ : str = int(length / 2 ) bitonic_sort(__a , __a , __a , 1 ) bitonic_sort(__a , low + middle , __a , 0 ) bitonic_merge(__a , __a , __a , __a ) if __name__ == "__main__": __A : int = input('Enter numbers separated by a comma:\n').strip() __A : Dict = [int(item.strip()) for item in user_input.split(',')] bitonic_sort(unsorted, 0, len(unsorted), 1) print('\nSorted array in ascending order is: ', end='') print(*unsorted, sep=', ') bitonic_merge(unsorted, 0, len(unsorted), 0) print('Sorted array in descending order is: ', end='') print(*unsorted, sep=', ')
355
'''simple docstring''' from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class __UpperCamelCase ( nn.Module ): def __init__( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = True ,_UpperCamelCase :str = "layer_norm" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Any = only_cross_attention snake_case_ : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" snake_case_ : Any = (num_embeds_ada_norm is not None) and norm_type == """ada_norm""" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F'''`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to''' F''' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.''' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: snake_case_ : Dict = AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ : str = AdaLayerNormZero(_UpperCamelCase ,_UpperCamelCase ) else: snake_case_ : List[Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=_UpperCamelCase ,) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. snake_case_ : str = ( AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,upcast_attention=_UpperCamelCase ,) # is self-attn if encoder_hidden_states is none else: snake_case_ : Any = None snake_case_ : Optional[Any] = None # 3. Feed-forward snake_case_ : List[str] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : Union[str, Any] = FeedForward(_UpperCamelCase ,dropout=_UpperCamelCase ,activation_fn=_UpperCamelCase ,final_dropout=_UpperCamelCase ) # let chunk size default to None snake_case_ : Optional[int] = None snake_case_ : Dict = 0 def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :int ): # Sets chunk feed-forward snake_case_ : Optional[Any] = chunk_size snake_case_ : Optional[Any] = dim def a__ ( self :List[str] ,_UpperCamelCase :torch.FloatTensor ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,_UpperCamelCase :Dict[str, Any] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = self.norma( _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=hidden_states.dtype ) else: snake_case_ : Optional[int] = self.norma(_UpperCamelCase ) snake_case_ : int = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case_ : Union[str, Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_msa.unsqueeze(1 ) * attn_output snake_case_ : Union[str, Any] = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case_ : Any = ( self.norma(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else self.norma(_UpperCamelCase ) ) snake_case_ : List[Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=_UpperCamelCase ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Tuple = attn_output + hidden_states # 3. Feed-forward snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Dict = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F'''`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.''' ) snake_case_ : Union[str, Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case_ : int = torch.cat( [self.ff(_UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase ,dim=self._chunk_dim )] ,dim=self._chunk_dim ,) else: snake_case_ : List[str] = self.ff(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case_ : Any = ff_output + hidden_states return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Dict ,_UpperCamelCase :int ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :int = 4 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Tuple = int(dim * mult ) snake_case_ : Optional[int] = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case_ : Any = GELU(_UpperCamelCase ,_UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case_ : Tuple = GELU(_UpperCamelCase ,_UpperCamelCase ,approximate="""tanh""" ) elif activation_fn == "geglu": snake_case_ : Dict = GEGLU(_UpperCamelCase ,_UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case_ : Optional[Any] = ApproximateGELU(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Dict = nn.ModuleList([] ) # project in self.net.append(_UpperCamelCase ) # project dropout self.net.append(nn.Dropout(_UpperCamelCase ) ) # project out self.net.append(nn.Linear(_UpperCamelCase ,_UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase ) ) def a__ ( self :Tuple ,_UpperCamelCase :Union[str, Any] ): for module in self.net: snake_case_ : Tuple = module(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :str = "none" ): super().__init__() snake_case_ : Union[str, Any] = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Optional[Any] = approximate def a__ ( self :str ,_UpperCamelCase :int ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ,approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ,approximate=self.approximate ).to(dtype=gate.dtype ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[Any] ): snake_case_ : Optional[Any] = self.proj(_UpperCamelCase ) snake_case_ : int = self.gelu(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :List[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : str = nn.Linear(_UpperCamelCase ,dim_out * 2 ) def a__ ( self :Dict ,_UpperCamelCase :List[str] ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def a__ ( self :Optional[Any] ,_UpperCamelCase :Optional[int] ): snake_case_ , snake_case_ : Dict = self.proj(_UpperCamelCase ).chunk(2 ,dim=-1 ) return hidden_states * self.gelu(_UpperCamelCase ) class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[int] ): snake_case_ : int = self.proj(_UpperCamelCase ) return x * torch.sigmoid(1.7_02 * x ) class __UpperCamelCase ( nn.Module ): def __init__( self :int ,_UpperCamelCase :str ,_UpperCamelCase :List[Any] ): super().__init__() snake_case_ : int = nn.Embedding(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = nn.SiLU() snake_case_ : Any = nn.Linear(_UpperCamelCase ,embedding_dim * 2 ) snake_case_ : Dict = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) def a__ ( self :int ,_UpperCamelCase :List[str] ,_UpperCamelCase :int ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ) ) ) snake_case_ , snake_case_ : Tuple = torch.chunk(_UpperCamelCase ,2 ) snake_case_ : Tuple = self.norm(_UpperCamelCase ) * (1 + scale) + shift return x class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :Tuple ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = CombinedTimestepLabelEmbeddings(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : int = nn.SiLU() snake_case_ : List[str] = nn.Linear(_UpperCamelCase ,6 * embedding_dim ,bias=_UpperCamelCase ) snake_case_ : str = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ,eps=1E-6 ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str=None ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=_UpperCamelCase ) ) ) snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Any = emb.chunk(6 ,dim=1 ) snake_case_ : str = self.norm(_UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[int] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :Optional[str] = None ,_UpperCamelCase :float = 1E-5 ): super().__init__() snake_case_ : Optional[int] = num_groups snake_case_ : List[Any] = eps if act_fn is None: snake_case_ : int = None else: snake_case_ : Dict = get_activation(_UpperCamelCase ) snake_case_ : Optional[int] = nn.Linear(_UpperCamelCase ,out_dim * 2 ) def a__ ( self :List[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :List[str] ): if self.act: snake_case_ : Any = self.act(_UpperCamelCase ) snake_case_ : Optional[int] = self.linear(_UpperCamelCase ) snake_case_ : Dict = emb[:, :, None, None] snake_case_ , snake_case_ : str = emb.chunk(2 ,dim=1 ) snake_case_ : str = F.group_norm(_UpperCamelCase ,self.num_groups ,eps=self.eps ) snake_case_ : List[str] = x * (1 + scale) + shift return x
8
0
'''simple docstring''' import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Dict , lowerCamelCase_ :Union[str, Any]=10_24 , lowerCamelCase_ :Any=10_24 , lowerCamelCase_ :Dict=False , **lowerCamelCase_ :List[str] ): '''simple docstring''' snake_case_ : Union[str, Any] = AutoTokenizer.from_pretrained(__snake_case ) snake_case_ : Any = SeqaSeqDataset(__snake_case , __snake_case , __snake_case , __snake_case , type_path="""train""" , **__snake_case ) snake_case_ : Union[str, Any] = tok.pad_token_id def get_lens(lowerCamelCase_ :List[str] ): snake_case_ : Dict = tqdm( DataLoader(__snake_case , batch_size=5_12 , num_workers=8 , shuffle=__snake_case , collate_fn=ds.collate_fn ) , desc=str(ds.len_file ) , ) snake_case_ : Optional[Any] = [] for batch in dl: snake_case_ : Tuple = batch["""input_ids"""].ne(__snake_case ).sum(1 ).tolist() snake_case_ : Any = batch["""labels"""].ne(__snake_case ).sum(1 ).tolist() if consider_target: for src, tgt in zip(__snake_case , __snake_case ): max_lens.append(max(__snake_case , __snake_case ) ) else: max_lens.extend(__snake_case ) return max_lens snake_case_ : Dict = get_lens(__snake_case ) snake_case_ : Any = SeqaSeqDataset(__snake_case , __snake_case , __snake_case , __snake_case , type_path="""val""" , **__snake_case ) snake_case_ : Tuple = get_lens(__snake_case ) pickle_save(__snake_case , train_ds.len_file ) pickle_save(__snake_case , val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
356
'''simple docstring''' import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :str=True , lowerCamelCase_ :str="pt" ): '''simple docstring''' snake_case_ : Tuple = {"""add_prefix_space""": True} if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and not line.startswith(""" """ ) else {} snake_case_ : Union[str, Any] = padding_side return tokenizer( [line] , max_length=lowerCamelCase_ , padding="""max_length""" if pad_to_max_length else None , truncation=lowerCamelCase_ , return_tensors=lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , **lowerCamelCase_ , ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Any=None , ): '''simple docstring''' snake_case_ : Dict = input_ids.ne(lowerCamelCase_ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[Any] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any="train" ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :int=None ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :Optional[int]="" ,): super().__init__() snake_case_ : List[str] = Path(_UpperCamelCase ).joinpath(type_path + """.source""" ) snake_case_ : int = Path(_UpperCamelCase ).joinpath(type_path + """.target""" ) snake_case_ : Optional[int] = self.get_char_lens(self.src_file ) snake_case_ : List[str] = max_source_length snake_case_ : str = max_target_length assert min(self.src_lens ) > 0, F'''found empty line in {self.src_file}''' snake_case_ : str = tokenizer snake_case_ : str = prefix if n_obs is not None: snake_case_ : int = self.src_lens[:n_obs] snake_case_ : Tuple = src_lang snake_case_ : str = tgt_lang def __len__( self :Any ): return len(self.src_lens ) def __getitem__( self :List[str] ,_UpperCamelCase :Union[str, Any] ): snake_case_ : Optional[int] = index + 1 # linecache starts at 1 snake_case_ : Dict = self.prefix + linecache.getline(str(self.src_file ) ,_UpperCamelCase ).rstrip("""\n""" ) snake_case_ : List[Any] = linecache.getline(str(self.tgt_file ) ,_UpperCamelCase ).rstrip("""\n""" ) assert source_line, F'''empty source line for index {index}''' assert tgt_line, F'''empty tgt line for index {index}''' # Need to add eos token manually for T5 if isinstance(self.tokenizer ,_UpperCamelCase ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right snake_case_ : int = ( self.tokenizer.question_encoder if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer ) snake_case_ : Optional[int] = self.tokenizer.generator if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer snake_case_ : Optional[Any] = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_source_length ,"""right""" ) snake_case_ : Tuple = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_target_length ,"""right""" ) snake_case_ : int = source_inputs["""input_ids"""].squeeze() snake_case_ : str = target_inputs["""input_ids"""].squeeze() snake_case_ : Union[str, Any] = source_inputs["""attention_mask"""].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def a__ ( _UpperCamelCase :str ): return [len(_UpperCamelCase ) for x in Path(_UpperCamelCase ).open().readlines()] def a__ ( self :Optional[int] ,_UpperCamelCase :List[str] ): snake_case_ : Optional[Any] = torch.stack([x["""input_ids"""] for x in batch] ) snake_case_ : List[Any] = torch.stack([x["""attention_mask"""] for x in batch] ) snake_case_ : Union[str, Any] = torch.stack([x["""decoder_input_ids"""] for x in batch] ) snake_case_ : Optional[Any] = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Tuple = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Optional[int] = trim_batch(_UpperCamelCase ,_UpperCamelCase ) snake_case_ , snake_case_ : Dict = trim_batch(_UpperCamelCase ,_UpperCamelCase ,attention_mask=_UpperCamelCase ) snake_case_ : Optional[int] = { """input_ids""": source_ids, """attention_mask""": source_mask, """decoder_input_ids""": y, } return batch __A : List[Any] = getLogger(__name__) def UpperCAmelCase ( lowerCamelCase_ :List[List] ): '''simple docstring''' return list(itertools.chain.from_iterable(lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : int = get_git_info() save_json(lowerCamelCase_ , os.path.join(lowerCamelCase_ , """git_log.json""" ) ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int]=4 , **lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' with open(lowerCamelCase_ , """w""" ) as f: json.dump(lowerCamelCase_ , lowerCamelCase_ , indent=lowerCamelCase_ , **lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] ): '''simple docstring''' with open(lowerCamelCase_ ) as f: return json.load(lowerCamelCase_ ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Optional[Any] = git.Repo(search_parent_directories=lowerCamelCase_ ) snake_case_ : List[str] = { """repo_id""": str(lowerCamelCase_ ), """repo_sha""": str(repo.head.object.hexsha ), """repo_branch""": str(repo.active_branch ), """hostname""": str(socket.gethostname() ), } return repo_infos def UpperCAmelCase ( lowerCamelCase_ :Callable , lowerCamelCase_ :Iterable ): '''simple docstring''' return list(map(lowerCamelCase_ , lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , """wb""" ) as f: return pickle.dump(lowerCamelCase_ , lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' def remove_articles(lowerCamelCase_ :str ): return re.sub(R"""\b(a|an|the)\b""" , """ """ , lowerCamelCase_ ) def white_space_fix(lowerCamelCase_ :Optional[Any] ): return " ".join(text.split() ) def remove_punc(lowerCamelCase_ :Tuple ): snake_case_ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowerCamelCase_ :Optional[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase_ ) ) ) ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' snake_case_ : List[Any] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : Optional[int] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : List[Any] = Counter(lowerCamelCase_ ) & Counter(lowerCamelCase_ ) snake_case_ : Optional[Any] = sum(common.values() ) if num_same == 0: return 0 snake_case_ : Optional[Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Union[str, Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Optional[Any] = (2 * precision * recall) / (precision + recall) return fa def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' return normalize_answer(lowerCamelCase_ ) == normalize_answer(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] ): '''simple docstring''' assert len(lowerCamelCase_ ) == len(lowerCamelCase_ ) snake_case_ : Optional[int] = 0 for hypo, pred in zip(lowerCamelCase_ , lowerCamelCase_ ): em += exact_match_score(lowerCamelCase_ , lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: em /= len(lowerCamelCase_ ) return {"em": em} def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return model_prefix.startswith("""rag""" ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead snake_case_ : Optional[int] = """dropout_rate""" for p in extra_params: if getattr(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): if not hasattr(lowerCamelCase_ , lowerCamelCase_ ) and not hasattr(lowerCamelCase_ , equivalent_param[p] ): logger.info("""config doesn't have a `{}` attribute""".format(lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) continue snake_case_ : str = p if hasattr(lowerCamelCase_ , lowerCamelCase_ ) else equivalent_param[p] setattr(lowerCamelCase_ , lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) return hparams, config
8
0
'''simple docstring''' 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 __A : Union[str, Any] = logging.get_logger(__name__) __A : Dict = "▁" __A : str = {"vocab_file": "sentencepiece.bpe.model", "monolingual_vocab_file": "dict.txt"} __A : List[Any] = { "vocab_file": { "vinai/bartpho-syllable": "https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model", }, "monolingual_vocab_file": { "vinai/bartpho-syllable": "https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt", }, } __A : Dict = {"vinai/bartpho-syllable": 1_024} class __UpperCamelCase ( lowercase__ ): lowercase : Union[str, Any] = VOCAB_FILES_NAMES lowercase : Any = PRETRAINED_VOCAB_FILES_MAP lowercase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : Any = ['''input_ids''', '''attention_mask'''] def __init__( self :Optional[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Dict ,_UpperCamelCase :List[Any]="<s>" ,_UpperCamelCase :Tuple="</s>" ,_UpperCamelCase :Dict="</s>" ,_UpperCamelCase :List[str]="<s>" ,_UpperCamelCase :Dict="<unk>" ,_UpperCamelCase :Any="<pad>" ,_UpperCamelCase :Union[str, Any]="<mask>" ,_UpperCamelCase :Optional[Dict[str, Any]] = None ,**_UpperCamelCase :Optional[int] ,): snake_case_ : List[str] = AddedToken(_snake_case ,lstrip=_snake_case ,rstrip=_snake_case ) if isinstance(_snake_case ,_snake_case ) else mask_token snake_case_ : Any = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_snake_case ,eos_token=_snake_case ,unk_token=_snake_case ,sep_token=_snake_case ,cls_token=_snake_case ,pad_token=_snake_case ,mask_token=_snake_case ,sp_model_kwargs=self.sp_model_kwargs ,**_snake_case ,) snake_case_ : Union[str, Any] = vocab_file snake_case_ : Optional[Any] = monolingual_vocab_file snake_case_ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_snake_case ) ) # Load the reduced vocab # Keep order of special tokens for backward compatibility snake_case_ : Union[str, Any] = {} snake_case_ : Any = 0 for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]: if str(_snake_case ) not in self.fairseq_tokens_to_ids: snake_case_ : Any = cnt cnt += 1 with open(_snake_case ,"""r""" ,encoding="""utf-8""" ) as f: for line in f.readlines(): snake_case_ : List[str] = line.strip().split()[0] snake_case_ : Any = len(self.fairseq_tokens_to_ids ) if str(_snake_case ) not in self.fairseq_tokens_to_ids: snake_case_ : Optional[int] = len(self.fairseq_tokens_to_ids ) snake_case_ : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self :List[Any] ): snake_case_ : Any = self.__dict__.copy() snake_case_ : Any = None snake_case_ : Tuple = self.sp_model.serialized_model_proto() return state def __setstate__( self :Optional[Any] ,_UpperCamelCase :List[str] ): snake_case_ : Dict = d # for backward compatibility if not hasattr(self ,"""sp_model_kwargs""" ): snake_case_ : Dict = {} snake_case_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def a__ ( self :Any ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] snake_case_ : Union[str, Any] = [self.cls_token_id] snake_case_ : str = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def a__ ( self :Any ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ,_UpperCamelCase :bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_snake_case ,token_ids_a=_snake_case ,already_has_special_tokens=_snake_case ) if token_ids_a is None: return [1] + ([0] * len(_snake_case )) + [1] return [1] + ([0] * len(_snake_case )) + [1, 1] + ([0] * len(_snake_case )) + [1] def a__ ( self :Dict ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): snake_case_ : Union[str, Any] = [self.sep_token_id] snake_case_ : Any = [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 a__ ( self :List[str] ): return len(self.fairseq_ids_to_tokens ) def a__ ( self :Union[str, Any] ): snake_case_ : str = {self.convert_ids_to_tokens(_snake_case ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def a__ ( self :str ,_UpperCamelCase :str ): return self.sp_model.encode(_snake_case ,out_type=_snake_case ) def a__ ( self :str ,_UpperCamelCase :Tuple ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] else: return self.unk_token_id def a__ ( self :Dict ,_UpperCamelCase :Union[str, Any] ): return self.fairseq_ids_to_tokens[index] def a__ ( self :str ,_UpperCamelCase :Union[str, Any] ): snake_case_ : str = """""".join(_snake_case ).replace(_snake_case ,""" """ ).strip() return out_string def a__ ( self :Any ,_UpperCamelCase :str ,_UpperCamelCase :Optional[str] = None ): if not os.path.isdir(_snake_case ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return snake_case_ : int = os.path.join( _snake_case ,(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) snake_case_ : List[str] = os.path.join( _snake_case ,(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""monolingual_vocab_file"""] ,) if os.path.abspath(self.vocab_file ) != os.path.abspath(_snake_case ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,_snake_case ) elif not os.path.isfile(self.vocab_file ): with open(_snake_case ,"""wb""" ) as fi: snake_case_ : Tuple = self.sp_model.serialized_model_proto() fi.write(_snake_case ) if os.path.abspath(self.monolingual_vocab_file ) != os.path.abspath( _snake_case ) and os.path.isfile(self.monolingual_vocab_file ): copyfile(self.monolingual_vocab_file ,_snake_case ) elif not os.path.isfile(self.monolingual_vocab_file ): with open(_snake_case ,"""w""" ,encoding="""utf-8""" ) as fp: for token in self.fairseq_tokens_to_ids: if token not in self.all_special_tokens: fp.write(F'''{str(_snake_case )} \n''' ) return out_vocab_file, out_monolingual_vocab_file
357
'''simple docstring''' import functools def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[str] = len(lowerCamelCase_ ) snake_case_ : Dict = len(lowerCamelCase_ ) @functools.cache def min_distance(lowerCamelCase_ :int , lowerCamelCase_ :int ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa snake_case_ : Union[str, Any] = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , lowerCamelCase_ ) , 1 + min_distance(lowerCamelCase_ , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
8
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __A : str = logging.get_logger(__name__) __A : str = { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/config.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/config.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/config.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/config.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json""", """roberta-large-openai-detector""": """https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json""", } class __UpperCamelCase ( __A ): lowercase : str = 'roberta' def __init__( self :List[Any] ,_UpperCamelCase :Optional[Any]=5_0_2_6_5 ,_UpperCamelCase :Dict=7_6_8 ,_UpperCamelCase :Dict=1_2 ,_UpperCamelCase :List[str]=1_2 ,_UpperCamelCase :Dict=3_0_7_2 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :Optional[int]=0.1 ,_UpperCamelCase :Optional[int]=0.1 ,_UpperCamelCase :List[str]=5_1_2 ,_UpperCamelCase :List[str]=2 ,_UpperCamelCase :str=0.02 ,_UpperCamelCase :Tuple=1E-1_2 ,_UpperCamelCase :List[str]=1 ,_UpperCamelCase :Any=0 ,_UpperCamelCase :Optional[int]=2 ,_UpperCamelCase :List[str]="absolute" ,_UpperCamelCase :str=True ,_UpperCamelCase :Optional[int]=None ,**_UpperCamelCase :Dict ,): super().__init__(pad_token_id=__lowercase ,bos_token_id=__lowercase ,eos_token_id=__lowercase ,**__lowercase ) snake_case_ : Any = vocab_size snake_case_ : Optional[int] = hidden_size snake_case_ : Dict = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : List[Any] = hidden_act snake_case_ : Optional[Any] = intermediate_size snake_case_ : Optional[int] = hidden_dropout_prob snake_case_ : Tuple = attention_probs_dropout_prob snake_case_ : List[Any] = max_position_embeddings snake_case_ : str = type_vocab_size snake_case_ : str = initializer_range snake_case_ : Optional[int] = layer_norm_eps snake_case_ : Optional[int] = position_embedding_type snake_case_ : List[str] = use_cache snake_case_ : Optional[Any] = classifier_dropout class __UpperCamelCase ( __A ): @property def a__ ( self :int ): if self.task == "multiple-choice": snake_case_ : Tuple = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case_ : Tuple = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
358
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Any = tmp_path / """file.csv""" snake_case_ : Any = textwrap.dedent( """\ header1,header2 1,2 10,20 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[int] = tmp_path / """malformed_file.csv""" snake_case_ : int = textwrap.dedent( """\ header1,header2 1,2 10,20, """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : str = tmp_path / """csv_with_image.csv""" snake_case_ : int = textwrap.dedent( F'''\ image {image_file} ''' ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' snake_case_ : int = tmp_path / """csv_with_label.csv""" snake_case_ : Tuple = textwrap.dedent( """\ label good bad good """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = tmp_path / """csv_with_int_list.csv""" snake_case_ : str = textwrap.dedent( """\ int_list 1 2 3 4 5 6 7 8 9 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : int = Csv() snake_case_ : Optional[Any] = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(lowerCamelCase_ , match="""Error tokenizing data""" ): for _ in generator: pass assert any( record.levelname == """ERROR""" and """Failed to read file""" in record.message and os.path.basename(lowerCamelCase_ ) in record.message for record in caplog.records ) @require_pil def UpperCAmelCase ( lowerCamelCase_ :Tuple ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : Tuple = f.read().splitlines()[1] snake_case_ : str = Csv(encoding="""utf-8""" , features=Features({"""image""": Image()} ) ) snake_case_ : Tuple = csv._generate_tables([[csv_file_with_image]] ) snake_case_ : Optional[Any] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""image""" ).type == Image()() snake_case_ : List[str] = pa_table.to_pydict()["""image"""] assert generated_content == [{"path": image_file, "bytes": None}] def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : List[Any] = f.read().splitlines()[1:] snake_case_ : Union[str, Any] = Csv(encoding="""utf-8""" , features=Features({"""label""": ClassLabel(names=["""good""", """bad"""] )} ) ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_label]] ) snake_case_ : Optional[int] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""label""" ).type == ClassLabel(names=["""good""", """bad"""] )() snake_case_ : Union[str, Any] = pa_table.to_pydict()["""label"""] assert generated_content == [ClassLabel(names=["""good""", """bad"""] ).straint(lowerCamelCase_ ) for label in labels] def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : str = Csv(encoding="""utf-8""" , sep=""",""" , converters={"""int_list""": lambda lowerCamelCase_ : [int(lowerCamelCase_ ) for i in x.split()]} ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_int_list]] ) snake_case_ : Tuple = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field("""int_list""" ).type ) snake_case_ : Dict = pa_table.to_pydict()["""int_list"""] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
8
0
from arguments import InitializationArguments from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, HfArgumentParser # Configuration __A : Tuple = HfArgumentParser(InitializationArguments) __A : Optional[int] = parser.parse_args() # Load codeparrot tokenizer trained for Python code tokenization __A : Tuple = AutoTokenizer.from_pretrained(args.tokenizer_name) # Config: "scale_attn_by_layer_idx" and "reorder_and_upcast_attn" are Mistral stability tweaks __A : Optional[Any] = { 'vocab_size': len(tokenizer), 'scale_attn_by_inverse_layer_idx': True, 'reorder_and_upcast_attn': True, } # Load model config (GPT-2 large in this case) __A : List[Any] = AutoConfig.from_pretrained(args.config_name, **config_kwargs) # Initialize new model with config __A : str = AutoModelForCausalLM.from_config(config) # Save model to the hub model.save_pretrained(args.model_name, push_to_hub=args.push_to_hub)
359
'''simple docstring''' import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple=None ): '''simple docstring''' # set parameter of one layer assert torch_layer.weight.shape == weight.shape, F'''{torch_layer} layer.weight does not match''' snake_case_ : Optional[Any] = nn.Parameter(lowerCamelCase_ ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F'''{torch_layer} layer.bias does not match''' snake_case_ : List[str] = nn.Parameter(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : Optional[Any] = np.asarray(weights[0] ) snake_case_ : int = np.asarray(weights[1] ) snake_case_ : Any = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : List[Any] = np.asarray(weights[0] ) snake_case_ : Optional[int] = np.asarray(weights[1] ) snake_case_ : Union[str, Any] = np.asarray(weights[2] ) snake_case_ : int = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' # layernorm 1 snake_case_ : str = weights[0][0][0] snake_case_ : int = np.asarray(layer_norm_a[0] ) snake_case_ : Optional[Any] = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # lsh weights + output snake_case_ : Tuple = weights[0][1] if len(lowerCamelCase_ ) < 4: set_layer_weights_in_torch_lsh(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) else: set_layer_weights_in_torch_local(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) # intermediate weighs snake_case_ : str = weights[2][0][1][2] # Chunked Feed Forward if len(lowerCamelCase_ ) == 4: snake_case_ : List[Any] = intermediate_weights[2] # layernorm 2 snake_case_ : Tuple = np.asarray(intermediate_weights[0][0] ) snake_case_ : Optional[Any] = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # intermediate dense snake_case_ : Any = np.asarray(intermediate_weights[1][0] ) snake_case_ : List[Any] = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) # intermediate out snake_case_ : List[Any] = np.asarray(intermediate_weights[4][0] ) snake_case_ : Union[str, Any] = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Any ): '''simple docstring''' # reformer model snake_case_ : Dict = torch_model.reformer # word embeds snake_case_ : List[Any] = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(lowerCamelCase_ ) , ) if isinstance(weights[3] , lowerCamelCase_ ): snake_case_ : Tuple = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): snake_case_ : Dict = np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), F'''{position_embeddings[emb_idx]} emb does not match''' snake_case_ : Optional[Any] = nn.Parameter(torch.tensor(lowerCamelCase_ ) ) snake_case_ : List[Any] = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( lowerCamelCase_ ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): snake_case_ : str = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # output layer norm snake_case_ : Optional[Any] = np.asarray(weights[7][0] ) snake_case_ : List[Any] = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # output embeddings snake_case_ : Optional[int] = np.asarray(weights[9][0] ) snake_case_ : Any = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # Initialise PyTorch model snake_case_ : List[str] = ReformerConfig.from_json_file(lowerCamelCase_ ) print(F'''Building PyTorch model from configuration: {config}''' ) snake_case_ : str = ReformerModelWithLMHead(lowerCamelCase_ ) with open(lowerCamelCase_ , """rb""" ) as f: snake_case_ : List[Any] = pickle.load(lowerCamelCase_ )["""weights"""] set_model_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , config.hidden_size ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , lowerCamelCase_ ) if __name__ == "__main__": __A : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--trax_model_pkl_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained Reformer model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __A : List[Any] = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
8
0
'''simple docstring''' from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging __A : List[Any] = logging.get_logger(__name__) __A : int = { 'EleutherAI/gpt-j-6B': 'https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json', # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class __UpperCamelCase ( __SCREAMING_SNAKE_CASE ): lowercase : Optional[int] = 'gptj' lowercase : Dict = { 'max_position_embeddings': 'n_positions', 'hidden_size': 'n_embd', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self :Tuple ,_UpperCamelCase :Optional[int]=5_0_4_0_0 ,_UpperCamelCase :Dict=2_0_4_8 ,_UpperCamelCase :Union[str, Any]=4_0_9_6 ,_UpperCamelCase :Union[str, Any]=2_8 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :int=6_4 ,_UpperCamelCase :Dict=None ,_UpperCamelCase :Tuple="gelu_new" ,_UpperCamelCase :Optional[int]=0.0 ,_UpperCamelCase :List[Any]=0.0 ,_UpperCamelCase :List[str]=0.0 ,_UpperCamelCase :Dict=1E-5 ,_UpperCamelCase :List[Any]=0.02 ,_UpperCamelCase :Any=True ,_UpperCamelCase :Dict=5_0_2_5_6 ,_UpperCamelCase :List[str]=5_0_2_5_6 ,_UpperCamelCase :Union[str, Any]=False ,**_UpperCamelCase :Union[str, Any] ,): snake_case_ : Optional[int] = vocab_size snake_case_ : Dict = n_positions snake_case_ : List[Any] = n_embd snake_case_ : Union[str, Any] = n_layer snake_case_ : Dict = n_head snake_case_ : Tuple = n_inner snake_case_ : int = rotary_dim snake_case_ : Optional[int] = activation_function snake_case_ : Tuple = resid_pdrop snake_case_ : Tuple = embd_pdrop snake_case_ : str = attn_pdrop snake_case_ : List[str] = layer_norm_epsilon snake_case_ : List[Any] = initializer_range snake_case_ : int = use_cache snake_case_ : str = bos_token_id snake_case_ : Optional[Any] = eos_token_id super().__init__( bos_token_id=_a ,eos_token_id=_a ,tie_word_embeddings=_a ,**_a ) class __UpperCamelCase ( __SCREAMING_SNAKE_CASE ): def __init__( self :Optional[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[Any] = "default" ,_UpperCamelCase :Optional[Any] = None ,_UpperCamelCase :List[Any] = False ,): super().__init__(_a ,task=_a ,patching_specs=_a ,use_past=_a ) if not getattr(self._config ,"""pad_token_id""" ,_a ): # TODO: how to do that better? snake_case_ : Any = 0 @property def a__ ( self :Any ): snake_case_ : List[Any] = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(_a ,direction="""inputs""" ) snake_case_ : str = {0: """batch""", 1: """past_sequence + sequence"""} else: snake_case_ : str = {0: """batch""", 1: """sequence"""} return common_inputs @property def a__ ( self :Any ): return self._config.n_layer @property def a__ ( self :List[Any] ): return self._config.n_head def a__ ( self :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Dict = -1 ,_UpperCamelCase :Optional[Any] = -1 ,_UpperCamelCase :str = False ,_UpperCamelCase :Optional[int] = None ,): snake_case_ : List[str] = super(_a ,self ).generate_dummy_inputs( _a ,batch_size=_a ,seq_length=_a ,is_pair=_a ,framework=_a ) # We need to order the input in the way they appears in the forward() snake_case_ : Union[str, Any] = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch snake_case_ , snake_case_ : List[Any] = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values snake_case_ : Optional[Any] = seqlen + 2 snake_case_ : Any = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) snake_case_ : Any = [ (torch.zeros(_a ), torch.zeros(_a )) for _ in range(self.num_layers ) ] snake_case_ : List[Any] = common_inputs["""attention_mask"""] if self.use_past: snake_case_ : Optional[Any] = ordered_inputs["""attention_mask"""].dtype snake_case_ : Any = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(_a ,_a ,dtype=_a )] ,dim=1 ) return ordered_inputs @property def a__ ( self :Tuple ): return 1_3
360
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[Any] = logging.get_logger(__name__) __A : str = { 'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json', # See all CANINE models at https://huggingface.co/models?filter=canine } class __UpperCamelCase ( lowercase__ ): lowercase : List[Any] = 'canine' def __init__( self :Optional[int] ,_UpperCamelCase :Dict=7_6_8 ,_UpperCamelCase :Union[str, Any]=1_2 ,_UpperCamelCase :int=1_2 ,_UpperCamelCase :int=3_0_7_2 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :Any=0.1 ,_UpperCamelCase :int=0.1 ,_UpperCamelCase :Any=1_6_3_8_4 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :List[str]=0.02 ,_UpperCamelCase :Any=1E-1_2 ,_UpperCamelCase :Tuple=0 ,_UpperCamelCase :List[str]=0xE_0_0_0 ,_UpperCamelCase :Optional[Any]=0xE_0_0_1 ,_UpperCamelCase :str=4 ,_UpperCamelCase :Optional[int]=4 ,_UpperCamelCase :str=8 ,_UpperCamelCase :int=1_6_3_8_4 ,_UpperCamelCase :int=1_2_8 ,**_UpperCamelCase :str ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : List[str] = max_position_embeddings snake_case_ : Union[str, Any] = hidden_size snake_case_ : Dict = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : Tuple = intermediate_size snake_case_ : str = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : Optional[Any] = initializer_range snake_case_ : Optional[int] = type_vocab_size snake_case_ : List[str] = layer_norm_eps # Character config: snake_case_ : Any = downsampling_rate snake_case_ : List[str] = upsampling_kernel_size snake_case_ : int = num_hash_functions snake_case_ : Tuple = num_hash_buckets snake_case_ : Tuple = local_transformer_stride
8
0
'''simple docstring''' import csv import tweepy # Twitter API credentials __A : List[str] = '' __A : Optional[Any] = '' __A : int = '' __A : List[str] = '' def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] ): '''simple docstring''' # authorize twitter, initialize tweepy snake_case_ : int = tweepy.OAuthHandler(lowercase__ , lowercase__ ) auth.set_access_token(lowercase__ , lowercase__ ) snake_case_ : Optional[Any] = tweepy.API(lowercase__ ) # initialize a list to hold all the tweepy Tweets snake_case_ : Optional[int] = [] # make initial request for most recent tweets (200 is the maximum allowed count) snake_case_ : List[str] = api.user_timeline(screen_name=lowercase__ , count=2_00 ) # save most recent tweets alltweets.extend(lowercase__ ) # save the id of the oldest tweet less one snake_case_ : List[Any] = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(lowercase__ ) > 0: print(F'''getting tweets before {oldest}''' ) # all subsequent requests use the max_id param to prevent duplicates snake_case_ : List[str] = api.user_timeline( screen_name=lowercase__ , count=2_00 , max_id=lowercase__ ) # save most recent tweets alltweets.extend(lowercase__ ) # update the id of the oldest tweet less one snake_case_ : Optional[Any] = alltweets[-1].id - 1 print(F'''...{len(lowercase__ )} tweets downloaded so far''' ) # transform the tweepy tweets into a 2D array that will populate the csv snake_case_ : Any = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(F'''new_{screen_name}_tweets.csv''' , """w""" ) as f: snake_case_ : Tuple = csv.writer(lowercase__ ) writer.writerow(["""id""", """created_at""", """text"""] ) writer.writerows(lowercase__ ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets('FirePing32')
361
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __A : Tuple = logging.get_logger(__name__) __A : List[Any] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } __A : str = { 'vocab_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json' }, 'merges_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt' }, 'tokenizer_config_file': { 'facebook/blenderbot_small-90M': ( 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json' ) }, } __A : Optional[Any] = { 'facebook/blenderbot_small-90M': 512, } class __UpperCamelCase ( lowercase__ ): lowercase : str = VOCAB_FILES_NAMES lowercase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : Dict = BlenderbotSmallTokenizer def __init__( self :str ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :Tuple="<|endoftext|>" ,_UpperCamelCase :int="<|endoftext|>" ,_UpperCamelCase :Dict="<|endoftext|>" ,_UpperCamelCase :Optional[Any]=False ,_UpperCamelCase :List[Any]=True ,**_UpperCamelCase :Any ,): super().__init__( ByteLevelBPETokenizer( vocab=_UpperCamelCase ,merges=_UpperCamelCase ,add_prefix_space=_UpperCamelCase ,trim_offsets=_UpperCamelCase ,) ,bos_token=_UpperCamelCase ,eos_token=_UpperCamelCase ,unk_token=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Any = add_prefix_space def a__ ( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any]=None ): snake_case_ : List[Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def a__ ( self :int ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): snake_case_ : int = [self.sep_token_id] snake_case_ : Tuple = [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]
8
0
'''simple docstring''' import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging __A : Any = logging.get_logger(__name__) __A : Union[str, Any] = {'vocab_file': 'spiece.model'} __A : int = { 'vocab_file': { 'xlnet-base-cased': 'https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model', 'xlnet-large-cased': 'https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model', } } __A : str = { 'xlnet-base-cased': None, 'xlnet-large-cased': None, } # Segments (not really needed) __A : Optional[Any] = 0 __A : Tuple = 1 __A : Any = 2 __A : int = 3 __A : Tuple = 4 class __UpperCamelCase ( __lowercase ): lowercase : List[str] = VOCAB_FILES_NAMES lowercase : int = PRETRAINED_VOCAB_FILES_MAP lowercase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : int = '''left''' def __init__( self :int ,_UpperCamelCase :str ,_UpperCamelCase :int=False ,_UpperCamelCase :int=True ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Any="<s>" ,_UpperCamelCase :List[str]="</s>" ,_UpperCamelCase :int="<unk>" ,_UpperCamelCase :Dict="<sep>" ,_UpperCamelCase :str="<pad>" ,_UpperCamelCase :Tuple="<cls>" ,_UpperCamelCase :Optional[Any]="<mask>" ,_UpperCamelCase :List[str]=["<eop>", "<eod>"] ,_UpperCamelCase :Optional[Dict[str, Any]] = None ,**_UpperCamelCase :Dict ,): snake_case_ : Tuple = AddedToken(_UpperCamelCase ,lstrip=_UpperCamelCase ,rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase ,_UpperCamelCase ) else mask_token snake_case_ : Union[str, Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_UpperCamelCase ,remove_space=_UpperCamelCase ,keep_accents=_UpperCamelCase ,bos_token=_UpperCamelCase ,eos_token=_UpperCamelCase ,unk_token=_UpperCamelCase ,sep_token=_UpperCamelCase ,pad_token=_UpperCamelCase ,cls_token=_UpperCamelCase ,mask_token=_UpperCamelCase ,additional_special_tokens=_UpperCamelCase ,sp_model_kwargs=self.sp_model_kwargs ,**_UpperCamelCase ,) snake_case_ : Tuple = 3 snake_case_ : int = do_lower_case snake_case_ : Optional[int] = remove_space snake_case_ : Optional[int] = keep_accents snake_case_ : List[Any] = vocab_file snake_case_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_UpperCamelCase ) @property def a__ ( self :Tuple ): return len(self.sp_model ) def a__ ( self :List[Any] ): snake_case_ : Any = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self :Tuple ): snake_case_ : Optional[Any] = self.__dict__.copy() snake_case_ : List[Any] = None return state def __setstate__( self :Union[str, Any] ,_UpperCamelCase :Optional[int] ): snake_case_ : Union[str, Any] = d # for backward compatibility if not hasattr(self ,"""sp_model_kwargs""" ): snake_case_ : int = {} snake_case_ : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def a__ ( self :Dict ,_UpperCamelCase :str ): if self.remove_space: snake_case_ : Optional[int] = """ """.join(inputs.strip().split() ) else: snake_case_ : List[str] = inputs snake_case_ : Optional[Any] = outputs.replace("""``""" ,"""\"""" ).replace("""''""" ,"""\"""" ) if not self.keep_accents: snake_case_ : int = unicodedata.normalize("""NFKD""" ,_UpperCamelCase ) snake_case_ : int = """""".join([c for c in outputs if not unicodedata.combining(_UpperCamelCase )] ) if self.do_lower_case: snake_case_ : Optional[Any] = outputs.lower() return outputs def a__ ( self :List[Any] ,_UpperCamelCase :str ): snake_case_ : List[Any] = self.preprocess_text(_UpperCamelCase ) snake_case_ : Dict = self.sp_model.encode(_UpperCamelCase ,out_type=_UpperCamelCase ) snake_case_ : Optional[Any] = [] for piece in pieces: if len(_UpperCamelCase ) > 1 and piece[-1] == str(""",""" ) and piece[-2].isdigit(): snake_case_ : Any = self.sp_model.EncodeAsPieces(piece[:-1].replace(_UpperCamelCase ,"""""" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: snake_case_ : List[str] = cur_pieces[1:] else: snake_case_ : Any = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_UpperCamelCase ) else: new_pieces.append(_UpperCamelCase ) return new_pieces def a__ ( self :Tuple ,_UpperCamelCase :str ): return self.sp_model.PieceToId(_UpperCamelCase ) def a__ ( self :Any ,_UpperCamelCase :Optional[int] ): return self.sp_model.IdToPiece(_UpperCamelCase ) def a__ ( self :str ,_UpperCamelCase :Optional[Any] ): snake_case_ : Optional[int] = """""".join(_UpperCamelCase ).replace(_UpperCamelCase ,""" """ ).strip() return out_string def a__ ( self :List[Any] ,_UpperCamelCase :List[int] ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = None ,_UpperCamelCase :bool = True ,**_UpperCamelCase :Optional[int] ,): snake_case_ : Union[str, Any] = kwargs.pop("""use_source_tokenizer""" ,_UpperCamelCase ) snake_case_ : Any = self.convert_ids_to_tokens(_UpperCamelCase ,skip_special_tokens=_UpperCamelCase ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 snake_case_ : Dict = [] snake_case_ : Any = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) snake_case_ : Optional[int] = [] sub_texts.append(_UpperCamelCase ) else: current_sub_text.append(_UpperCamelCase ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_UpperCamelCase ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens snake_case_ : Tuple = """""".join(_UpperCamelCase ) snake_case_ : Optional[Any] = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: snake_case_ : Dict = self.clean_up_tokenization(_UpperCamelCase ) return clean_text else: return text def a__ ( self :List[Any] ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): snake_case_ : Union[str, Any] = [self.sep_token_id] snake_case_ : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def a__ ( self :Union[str, Any] ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ,_UpperCamelCase :bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase ,token_ids_a=_UpperCamelCase ,already_has_special_tokens=_UpperCamelCase ) if token_ids_a is not None: return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1, 1] return ([0] * len(_UpperCamelCase )) + [1, 1] def a__ ( self :int ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): snake_case_ : Any = [self.sep_token_id] snake_case_ : Optional[int] = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def a__ ( self :Union[str, Any] ,_UpperCamelCase :str ,_UpperCamelCase :Optional[str] = None ): if not os.path.isdir(_UpperCamelCase ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return snake_case_ : int = os.path.join( _UpperCamelCase ,(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,_UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase ,"""wb""" ) as fi: snake_case_ : Dict = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,)
362
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :list ): '''simple docstring''' if len(lowerCamelCase_ ) <= 1: return lst snake_case_ : Union[str, Any] = 1 while i < len(lowerCamelCase_ ): if lst[i - 1] <= lst[i]: i += 1 else: snake_case_ , snake_case_ : Union[str, Any] = lst[i], lst[i - 1] i -= 1 if i == 0: snake_case_ : int = 1 return lst if __name__ == "__main__": __A : Optional[int] = input('Enter numbers separated by a comma:\n').strip() __A : int = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
8
0
'''simple docstring''' import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline __A : Any = datasets.utils.logging.get_logger(__name__) @dataclass class __UpperCamelCase ( datasets.BuilderConfig ): lowercase : Optional[datasets.Features] = None lowercase : str = "utf-8" lowercase : Optional[str] = None lowercase : Optional[str] = None lowercase : bool = True # deprecated lowercase : Optional[int] = None # deprecated lowercase : int = 1_0 << 2_0 # 10MB lowercase : Optional[bool] = None class __UpperCamelCase ( datasets.ArrowBasedBuilder ): lowercase : Dict = JsonConfig def a__ ( self :Dict ): if self.config.block_size is not None: logger.warning("""The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead""" ) snake_case_ : int = self.config.block_size if self.config.use_threads is not True: logger.warning( """The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.""" ) if self.config.newlines_in_values is not None: raise ValueError("""The JSON loader parameter `newlines_in_values` is no longer supported""" ) return datasets.DatasetInfo(features=self.config.features ) def a__ ( self :str ,_UpperCamelCase :Tuple ): 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}''' ) snake_case_ : Tuple = dl_manager.download_and_extract(self.config.data_files ) if isinstance(lowercase_ ,(str, list, tuple) ): snake_case_ : Any = data_files if isinstance(lowercase_ ,lowercase_ ): snake_case_ : List[str] = [files] snake_case_ : int = [dl_manager.iter_files(lowercase_ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN ,gen_kwargs={"""files""": files} )] snake_case_ : Optional[int] = [] for split_name, files in data_files.items(): if isinstance(lowercase_ ,lowercase_ ): snake_case_ : Dict = [files] snake_case_ : Any = [dl_manager.iter_files(lowercase_ ) for file in files] splits.append(datasets.SplitGenerator(name=lowercase_ ,gen_kwargs={"""files""": files} ) ) return splits def a__ ( self :List[Any] ,_UpperCamelCase :List[str] ): if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): snake_case_ : Union[str, Any] = self.config.features.arrow_schema.field(lowercase_ ).type snake_case_ : Union[str, Any] = pa_table.append_column(lowercase_ ,pa.array([None] * len(lowercase_ ) ,type=lowercase_ ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example snake_case_ : Optional[Any] = table_cast(lowercase_ ,self.config.features.arrow_schema ) return pa_table def a__ ( self :int ,_UpperCamelCase :Union[str, Any] ): for file_idx, file in enumerate(itertools.chain.from_iterable(lowercase_ ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(lowercase_ ,encoding=self.config.encoding ,errors=self.config.encoding_errors ) as f: snake_case_ : List[str] = json.load(lowercase_ ) # We keep only the field we are interested in snake_case_ : str = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(lowercase_ ,(list, tuple) ): snake_case_ : Dict = set().union(*[row.keys() for row in dataset] ) snake_case_ : str = {col: [row.get(lowercase_ ) for row in dataset] for col in keys} else: snake_case_ : Optional[Any] = dataset snake_case_ : str = pa.Table.from_pydict(lowercase_ ) yield file_idx, self._cast_table(lowercase_ ) # If the file has one json object per line else: with open(lowercase_ ,"""rb""" ) as f: snake_case_ : Dict = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small snake_case_ : Any = max(self.config.chunksize // 3_2 ,1_6 << 1_0 ) snake_case_ : Any = ( self.config.encoding_errors if self.config.encoding_errors is not None else """strict""" ) while True: snake_case_ : str = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(lowercase_ ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": snake_case_ : Union[str, Any] = batch.decode(self.config.encoding ,errors=lowercase_ ).encode("""utf-8""" ) try: while True: try: snake_case_ : List[Any] = paj.read_json( io.BytesIO(lowercase_ ) ,read_options=paj.ReadOptions(block_size=lowercase_ ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(lowercase_ ,pa.ArrowInvalid ) and "straddling" not in str(lowercase_ ) or block_size > len(lowercase_ ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F'''Batch of {len(lowercase_ )} bytes couldn\'t be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.''' ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( lowercase_ ,encoding=self.config.encoding ,errors=self.config.encoding_errors ) as f: snake_case_ : Union[str, Any] = json.load(lowercase_ ) except json.JSONDecodeError: logger.error(F'''Failed to read file \'{file}\' with error {type(lowercase_ )}: {e}''' ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(lowercase_ ,lowercase_ ): # list is the only sequence type supported in JSON try: snake_case_ : Tuple = set().union(*[row.keys() for row in dataset] ) snake_case_ : Dict = {col: [row.get(lowercase_ ) for row in dataset] for col in keys} snake_case_ : Optional[int] = pa.Table.from_pydict(lowercase_ ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F'''Failed to read file \'{file}\' with error {type(lowercase_ )}: {e}''' ) raise ValueError(F'''Not able to read records in the JSON file at {file}.''' ) from None yield file_idx, self._cast_table(lowercase_ ) break else: logger.error(F'''Failed to read file \'{file}\' with error {type(lowercase_ )}: {e}''' ) raise ValueError( F'''Not able to read records in the JSON file at {file}. ''' F'''You should probably indicate the field of the JSON file containing your records. ''' F'''This JSON file contain the following fields: {str(list(dataset.keys() ) )}. ''' F'''Select the correct one and provide it as `field=\'XXX\'` to the dataset loading method. ''' ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(lowercase_ ) batch_idx += 1
363
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __UpperCamelCase : def __init__( self :Any ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[int]=1_2 ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Optional[int]=True ,_UpperCamelCase :Union[str, Any]=True ,_UpperCamelCase :Dict=True ,_UpperCamelCase :Optional[int]=9_9 ,_UpperCamelCase :Dict=3_2 ,_UpperCamelCase :Union[str, Any]=3_2 ,_UpperCamelCase :Union[str, Any]=2 ,_UpperCamelCase :Optional[Any]=4 ,_UpperCamelCase :List[Any]=3_7 ,_UpperCamelCase :Tuple=0.1 ,_UpperCamelCase :Optional[int]=0.1 ,_UpperCamelCase :int=5_1_2 ,_UpperCamelCase :Tuple=0.02 ,_UpperCamelCase :Any=0 ,_UpperCamelCase :str=None ,): snake_case_ : str = parent snake_case_ : int = batch_size snake_case_ : Union[str, Any] = seq_length snake_case_ : List[Any] = is_training snake_case_ : Union[str, Any] = use_input_mask snake_case_ : List[str] = use_labels snake_case_ : int = vocab_size snake_case_ : Any = hidden_size snake_case_ : List[Any] = projection_dim snake_case_ : Dict = num_hidden_layers snake_case_ : Dict = num_attention_heads snake_case_ : str = intermediate_size snake_case_ : int = dropout snake_case_ : int = attention_dropout snake_case_ : Dict = max_position_embeddings snake_case_ : Union[str, Any] = initializer_range snake_case_ : Dict = scope snake_case_ : Union[str, Any] = bos_token_id def a__ ( self :Any ): snake_case_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case_ : Union[str, Any] = None if self.use_input_mask: snake_case_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: snake_case_ : int = input_mask.numpy() snake_case_ , snake_case_ : Tuple = input_mask.shape snake_case_ : Any = np.random.randint(1 ,seq_length - 1 ,size=(batch_size,) ) for batch_idx, start_index in enumerate(_UpperCamelCase ): snake_case_ : Optional[int] = 1 snake_case_ : List[str] = 0 snake_case_ : Tuple = self.get_config() return config, input_ids, tf.convert_to_tensor(_UpperCamelCase ) def a__ ( self :str ): return BlipTextConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,projection_dim=self.projection_dim ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,dropout=self.dropout ,attention_dropout=self.attention_dropout ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,bos_token_id=self.bos_token_id ,) def a__ ( self :List[Any] ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[int] ): snake_case_ : List[str] = TFBlipTextModel(config=_UpperCamelCase ) snake_case_ : List[Any] = model(_UpperCamelCase ,attention_mask=_UpperCamelCase ,training=_UpperCamelCase ) snake_case_ : Any = model(_UpperCamelCase ,training=_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def a__ ( self :List[str] ): snake_case_ : Union[str, Any] = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ : str = config_and_inputs snake_case_ : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Optional[Any] = (TFBlipTextModel,) if is_tf_available() else () lowercase : int = False lowercase : List[Any] = False lowercase : Dict = False def a__ ( self :List[Any] ): snake_case_ : List[str] = BlipTextModelTester(self ) snake_case_ : Tuple = ConfigTester(self ,config_class=_UpperCamelCase ,hidden_size=3_7 ) def a__ ( self :Union[str, Any] ): self.config_tester.run_common_tests() def a__ ( self :Union[str, Any] ): snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def a__ ( self :Tuple ): pass def a__ ( self :Tuple ): pass @unittest.skip(reason="""Blip does not use inputs_embeds""" ) def a__ ( self :Any ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :Tuple ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :List[Any] ): pass @slow def a__ ( self :Any ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ : Optional[Any] = TFBlipTextModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def a__ ( self :Dict ,_UpperCamelCase :Tuple=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCamelCase )
8
0
'''simple docstring''' # 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 ( lowerCamelCase_ :Dict=None ): '''simple docstring''' if subparsers is not None: snake_case_ : Union[str, Any] = subparsers.add_parser("""test""" ) else: snake_case_ : List[str] = 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 ( lowerCamelCase_ :List[Any] ): '''simple docstring''' snake_case_ : Dict = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ["""test_utils""", """scripts""", """test_script.py"""] ) if args.config_file is None: snake_case_ : List[Any] = script_name else: snake_case_ : Union[str, Any] = F'''--config_file={args.config_file} {script_name}''' snake_case_ : str = ['''accelerate-launch'''] + test_args.split() snake_case_ : str = 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 ( ): '''simple docstring''' snake_case_ : Optional[int] = test_command_parser() snake_case_ : List[Any] = parser.parse_args() test_command(__a ) if __name__ == "__main__": main()
364
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : int = { 'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'], 'feature_extraction_whisper': ['WhisperFeatureExtractor'], 'processing_whisper': ['WhisperProcessor'], 'tokenization_whisper': ['WhisperTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FlaxWhisperForConditionalGeneration', 'FlaxWhisperModel', 'FlaxWhisperPreTrainedModel', 'FlaxWhisperForAudioClassification', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys __A : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
0
'''simple docstring''' import warnings from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __UpperCamelCase ( __SCREAMING_SNAKE_CASE ): lowercase : Dict = ["image_processor", "tokenizer"] lowercase : List[str] = "FlavaImageProcessor" lowercase : Dict = ("BertTokenizer", "BertTokenizerFast") def __init__( self :List[Any] ,_UpperCamelCase :int=None ,_UpperCamelCase :int=None ,**_UpperCamelCase :Any ): snake_case_ : Dict = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" ,__UpperCAmelCase ,) snake_case_ : List[Any] = kwargs.pop("""feature_extractor""" ) snake_case_ : List[Any] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(__UpperCAmelCase ,__UpperCAmelCase ) snake_case_ : Dict = self.image_processor def __call__( self :List[str] ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :int = None ,_UpperCamelCase :Union[str, Any] = True ,_UpperCamelCase :Optional[int] = False ,_UpperCamelCase :Tuple = False ,_UpperCamelCase :List[str] = None ,_UpperCamelCase :List[str] = 0 ,_UpperCamelCase :List[str] = None ,_UpperCamelCase :int = None ,_UpperCamelCase :Dict = None ,_UpperCamelCase :Union[str, Any] = None ,_UpperCamelCase :int = None ,_UpperCamelCase :Any = False ,_UpperCamelCase :Union[str, Any] = False ,_UpperCamelCase :int = False ,_UpperCamelCase :List[str] = False ,_UpperCamelCase :Optional[int] = True ,_UpperCamelCase :List[Any] = None ,**_UpperCamelCase :List[Any] ,): if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: snake_case_ : Optional[Any] = self.tokenizer( text=__UpperCAmelCase ,add_special_tokens=__UpperCAmelCase ,padding=__UpperCAmelCase ,truncation=__UpperCAmelCase ,max_length=__UpperCAmelCase ,stride=__UpperCAmelCase ,pad_to_multiple_of=__UpperCAmelCase ,return_token_type_ids=__UpperCAmelCase ,return_attention_mask=__UpperCAmelCase ,return_overflowing_tokens=__UpperCAmelCase ,return_special_tokens_mask=__UpperCAmelCase ,return_offsets_mapping=__UpperCAmelCase ,return_length=__UpperCAmelCase ,verbose=__UpperCAmelCase ,return_tensors=__UpperCAmelCase ,**__UpperCAmelCase ,) if images is not None: snake_case_ : List[Any] = self.image_processor( __UpperCAmelCase ,return_image_mask=__UpperCAmelCase ,return_codebook_pixels=__UpperCAmelCase ,return_tensors=__UpperCAmelCase ,**__UpperCAmelCase ,) if text is not None and images is not None: encoding.update(__UpperCAmelCase ) return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCAmelCase ) ,tensor_type=__UpperCAmelCase ) def a__ ( self :str ,*_UpperCamelCase :Optional[int] ,**_UpperCamelCase :Union[str, Any] ): return self.tokenizer.batch_decode(*__UpperCAmelCase ,**__UpperCAmelCase ) def a__ ( self :Optional[Any] ,*_UpperCamelCase :Optional[int] ,**_UpperCamelCase :int ): return self.tokenizer.decode(*__UpperCAmelCase ,**__UpperCAmelCase ) @property def a__ ( self :Optional[int] ): snake_case_ : Tuple = self.tokenizer.model_input_names snake_case_ : Union[str, Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def a__ ( self :Union[str, Any] ): warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" ,__UpperCAmelCase ,) return self.image_processor_class @property def a__ ( self :int ): warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" ,__UpperCAmelCase ,) return self.image_processor
365
'''simple docstring''' import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor __A : Optional[int] = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[str] ,*_UpperCamelCase :str ,**_UpperCamelCase :Optional[int] ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" ,_UpperCamelCase ,) super().__init__(*_UpperCamelCase ,**_UpperCamelCase )
8
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class __UpperCamelCase ( metaclass=lowercase__ ): lowercase : Optional[int] = ['''keras_nlp'''] def __init__( self :Tuple ,*_UpperCamelCase :str ,**_UpperCamelCase :int ): requires_backends(self ,["""keras_nlp"""] )
366
'''simple docstring''' import re def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[Any] = re.compile( R"""^(?:0|94|\+94|0{2}94)""" R"""7(0|1|2|4|5|6|7|8)""" R"""(-| |)""" R"""\d{7}$""" ) return bool(re.search(lowerCamelCase_ , lowerCamelCase_ ) ) if __name__ == "__main__": __A : int = '0094702343221' print(is_sri_lankan_phone_number(phone))
8
0
'''simple docstring''' from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Sequence, Value from .base import TaskTemplate @dataclass(frozen=_UpperCamelCase ) class __UpperCamelCase ( _UpperCamelCase ): lowercase : str = field(default='question-answering-extractive' , metadata={'include_in_asdict_even_if_is_default': True} ) lowercase : ClassVar[Features] = Features({'question': Value('string' ), 'context': Value('string' )} ) lowercase : ClassVar[Features] = Features( { 'answers': Sequence( { 'text': Value('string' ), 'answer_start': Value('int32' ), } ) } ) lowercase : str = "question" lowercase : str = "context" lowercase : str = "answers" @property def a__ ( self :Union[str, Any] ): return {self.question_column: "question", self.context_column: "context", self.answers_column: "answers"}
367
'''simple docstring''' from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import BaseOutput, is_torch_available, is_transformers_available @dataclass class __UpperCamelCase ( lowercase__ ): lowercase : Union[List[PIL.Image.Image], np.ndarray] lowercase : Optional[List[bool]] if is_transformers_available() and is_torch_available(): from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
8
0
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not all(char in """01""" for char in bin_string ): raise ValueError("""Non-binary value was passed to the function""" ) if not bin_string: raise ValueError("""Empty string was passed to the function""" ) snake_case_ : List[Any] = '''''' while len(a__ ) % 3 != 0: snake_case_ : Optional[int] = '''0''' + bin_string snake_case_ : Dict = [ bin_string[index : index + 3] for index in range(len(a__ ) ) if index % 3 == 0 ] for bin_group in bin_string_in_3_list: snake_case_ : Optional[Any] = 0 for index, val in enumerate(a__ ): oct_val += int(2 ** (2 - index) * int(a__ ) ) oct_string += str(a__ ) return oct_string if __name__ == "__main__": from doctest import testmod testmod()
368
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , unittest.TestCase ): lowercase : Dict = StableDiffusionInpaintPipeline lowercase : str = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS lowercase : Union[str, Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS lowercase : Dict = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowercase : Optional[int] = frozenset([] ) def a__ ( self :Any ): torch.manual_seed(0 ) snake_case_ : Optional[int] = UNetaDConditionModel( block_out_channels=(3_2, 6_4) ,layers_per_block=2 ,sample_size=3_2 ,in_channels=9 ,out_channels=4 ,down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,cross_attention_dim=3_2 ,attention_head_dim=(2, 4) ,use_linear_projection=_UpperCamelCase ,) snake_case_ : Tuple = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) torch.manual_seed(0 ) snake_case_ : List[str] = AutoencoderKL( block_out_channels=[3_2, 6_4] ,in_channels=3 ,out_channels=3 ,down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,latent_channels=4 ,sample_size=1_2_8 ,) torch.manual_seed(0 ) snake_case_ : Optional[int] = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=3_2 ,intermediate_size=3_7 ,layer_norm_eps=1E-0_5 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1_0_0_0 ,hidden_act="""gelu""" ,projection_dim=5_1_2 ,) snake_case_ : Tuple = CLIPTextModel(_UpperCamelCase ) snake_case_ : Optional[int] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) snake_case_ : str = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def a__ ( self :str ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Union[str, Any]=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched snake_case_ : List[Any] = floats_tensor((1, 3, 3_2, 3_2) ,rng=random.Random(_UpperCamelCase ) ).to(_UpperCamelCase ) snake_case_ : int = image.cpu().permute(0 ,2 ,3 ,1 )[0] snake_case_ : List[str] = Image.fromarray(np.uinta(_UpperCamelCase ) ).convert("""RGB""" ).resize((6_4, 6_4) ) snake_case_ : Optional[Any] = Image.fromarray(np.uinta(image + 4 ) ).convert("""RGB""" ).resize((6_4, 6_4) ) if str(_UpperCamelCase ).startswith("""mps""" ): snake_case_ : Optional[Any] = torch.manual_seed(_UpperCamelCase ) else: snake_case_ : Optional[int] = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) snake_case_ : int = { """prompt""": """A painting of a squirrel eating a burger""", """image""": init_image, """mask_image""": mask_image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def a__ ( self :Any ): snake_case_ : Union[str, Any] = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case_ : Optional[Any] = self.get_dummy_components() snake_case_ : Dict = StableDiffusionInpaintPipeline(**_UpperCamelCase ) snake_case_ : List[str] = sd_pipe.to(_UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ : Union[str, Any] = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ : Tuple = sd_pipe(**_UpperCamelCase ).images snake_case_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case_ : Dict = np.array([0.47_27, 0.57_35, 0.39_41, 0.54_46, 0.59_26, 0.43_94, 0.50_62, 0.46_54, 0.44_76] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def a__ ( self :Any ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :List[Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self :Tuple ): snake_case_ : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : List[str] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench.npy""" ) snake_case_ : str = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : Optional[Any] = StableDiffusionInpaintPipeline.from_pretrained(_UpperCamelCase ,safety_checker=_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() snake_case_ : Optional[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : List[str] = torch.manual_seed(0 ) snake_case_ : Dict = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""np""" ,) snake_case_ : Union[str, Any] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 9E-3 def a__ ( self :Tuple ): snake_case_ : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : List[str] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench_fp16.npy""" ) snake_case_ : Optional[int] = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : List[str] = StableDiffusionInpaintPipeline.from_pretrained( _UpperCamelCase ,torch_dtype=torch.floataa ,safety_checker=_UpperCamelCase ,) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() snake_case_ : Optional[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : List[Any] = torch.manual_seed(0 ) snake_case_ : Any = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""np""" ,) snake_case_ : List[str] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 5E-1 def a__ ( self :Union[str, Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case_ : Optional[int] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : int = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : Dict = PNDMScheduler.from_pretrained(_UpperCamelCase ,subfolder="""scheduler""" ) snake_case_ : List[Any] = StableDiffusionInpaintPipeline.from_pretrained( _UpperCamelCase ,safety_checker=_UpperCamelCase ,scheduler=_UpperCamelCase ,torch_dtype=torch.floataa ,) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case_ : List[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : Optional[int] = torch.manual_seed(0 ) snake_case_ : Tuple = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,num_inference_steps=2 ,output_type="""np""" ,) snake_case_ : Any = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 1_0**9
8
0
'''simple docstring''' import logging import os from typing import List, TextIO, Union from conllu import parse_incr from utils_ner import InputExample, Split, TokenClassificationTask __A : str = logging.getLogger(__name__) class __UpperCamelCase ( __UpperCamelCase ): def __init__( self :Union[str, Any] ,_UpperCamelCase :Optional[Any]=-1 ): # in NER datasets, the last column is usually reserved for NER label snake_case_ : Tuple = label_idx def a__ ( self :Optional[Any] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Union[Split, str] ): if isinstance(_lowerCAmelCase ,_lowerCAmelCase ): snake_case_ : Optional[int] = mode.value snake_case_ : Optional[Any] = os.path.join(_lowerCAmelCase ,F'''{mode}.txt''' ) snake_case_ : int = 1 snake_case_ : Optional[int] = [] with open(_lowerCAmelCase ,encoding="""utf-8""" ) as f: snake_case_ : Union[str, Any] = [] snake_case_ : int = [] for line in f: if line.startswith("""-DOCSTART-""" ) or line == "" or line == "\n": if words: examples.append(InputExample(guid=F'''{mode}-{guid_index}''' ,words=_lowerCAmelCase ,labels=_lowerCAmelCase ) ) guid_index += 1 snake_case_ : Optional[int] = [] snake_case_ : Dict = [] else: snake_case_ : Any = line.split(""" """ ) words.append(splits[0] ) if len(_lowerCAmelCase ) > 1: labels.append(splits[self.label_idx].replace("""\n""" ,"""""" ) ) else: # Examples could have no label for mode = "test" labels.append("""O""" ) if words: examples.append(InputExample(guid=F'''{mode}-{guid_index}''' ,words=_lowerCAmelCase ,labels=_lowerCAmelCase ) ) return examples def a__ ( self :int ,_UpperCamelCase :TextIO ,_UpperCamelCase :TextIO ,_UpperCamelCase :List ): snake_case_ : List[str] = 0 for line in test_input_reader: if line.startswith("""-DOCSTART-""" ) or line == "" or line == "\n": writer.write(_lowerCAmelCase ) if not preds_list[example_id]: example_id += 1 elif preds_list[example_id]: snake_case_ : List[Any] = line.split()[0] + """ """ + preds_list[example_id].pop(0 ) + """\n""" writer.write(_lowerCAmelCase ) else: logger.warning("""Maximum sequence length exceeded: No prediction for '%s'.""" ,line.split()[0] ) def a__ ( self :int ,_UpperCamelCase :str ): if path: with open(_lowerCAmelCase ,"""r""" ) as f: snake_case_ : Dict = f.read().splitlines() if "O" not in labels: snake_case_ : str = ["""O"""] + labels return labels else: return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"] class __UpperCamelCase ( __UpperCamelCase ): def __init__( self :List[Any] ): # in CONLL2003 dataset chunk column is second-to-last super().__init__(label_idx=-2 ) def a__ ( self :List[str] ,_UpperCamelCase :str ): if path: with open(_lowerCAmelCase ,"""r""" ) as f: snake_case_ : Tuple = f.read().splitlines() if "O" not in labels: snake_case_ : int = ["""O"""] + labels return labels else: return [ "O", "B-ADVP", "B-INTJ", "B-LST", "B-PRT", "B-NP", "B-SBAR", "B-VP", "B-ADJP", "B-CONJP", "B-PP", "I-ADVP", "I-INTJ", "I-LST", "I-PRT", "I-NP", "I-SBAR", "I-VP", "I-ADJP", "I-CONJP", "I-PP", ] class __UpperCamelCase ( __UpperCamelCase ): def a__ ( self :str ,_UpperCamelCase :List[str] ,_UpperCamelCase :Union[Split, str] ): if isinstance(_lowerCAmelCase ,_lowerCAmelCase ): snake_case_ : Optional[int] = mode.value snake_case_ : Any = os.path.join(_lowerCAmelCase ,F'''{mode}.txt''' ) snake_case_ : List[Any] = 1 snake_case_ : Any = [] with open(_lowerCAmelCase ,encoding="""utf-8""" ) as f: for sentence in parse_incr(_lowerCAmelCase ): snake_case_ : int = [] snake_case_ : str = [] for token in sentence: words.append(token["""form"""] ) labels.append(token["""upos"""] ) assert len(_lowerCAmelCase ) == len(_lowerCAmelCase ) if words: examples.append(InputExample(guid=F'''{mode}-{guid_index}''' ,words=_lowerCAmelCase ,labels=_lowerCAmelCase ) ) guid_index += 1 return examples def a__ ( self :Any ,_UpperCamelCase :TextIO ,_UpperCamelCase :TextIO ,_UpperCamelCase :List ): snake_case_ : List[Any] = 0 for sentence in parse_incr(_lowerCAmelCase ): snake_case_ : str = preds_list[example_id] snake_case_ : List[Any] = """""" for token in sentence: out += F'''{token["form"]} ({token["upos"]}|{s_p.pop(0 )}) ''' out += "\n" writer.write(_lowerCAmelCase ) example_id += 1 def a__ ( self :Optional[Any] ,_UpperCamelCase :str ): if path: with open(_lowerCAmelCase ,"""r""" ) as f: return f.read().splitlines() else: return [ "ADJ", "ADP", "ADV", "AUX", "CCONJ", "DET", "INTJ", "NOUN", "NUM", "PART", "PRON", "PROPN", "PUNCT", "SCONJ", "SYM", "VERB", "X", ]
369
'''simple docstring''' import collections import os import re from pathlib import Path __A : Dict = 'src/transformers' # Matches is_xxx_available() __A : Dict = re.compile(r'is\_([a-z_]*)_available()') # Catches a one-line _import_struct = {xxx} __A : Any = re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __A : Tuple = re.compile(r'\s+"\S*":\s+\[([^\]]*)\]') # Catches a line if not is_foo_available __A : Optional[Any] = re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)') # Catches a line _import_struct["bla"].append("foo") __A : Optional[int] = re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __A : List[Any] = re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]') # Catches a line with an object between quotes and a comma: "MyModel", __A : Union[str, Any] = re.compile(r'^\s+"([^"]+)",') # Catches a line with objects between brackets only: ["foo", "bar"], __A : int = re.compile(r'^\s+\[([^\]]+)\]') # Catches a line with from foo import bar, bla, boo __A : int = re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') # Catches a line with try: __A : List[Any] = re.compile(r'^\s*try:') # Catches a line with else: __A : Any = re.compile(r'^\s*else:') def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' if _re_test_backend.search(lowerCamelCase_ ) is None: return None snake_case_ : Tuple = [b[0] for b in _re_backend.findall(lowerCamelCase_ )] backends.sort() return "_and_".join(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] ): '''simple docstring''' with open(lowerCamelCase_ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: snake_case_ : str = f.readlines() snake_case_ : List[Any] = 0 while line_index < len(lowerCamelCase_ ) and not lines[line_index].startswith("""_import_structure = {""" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(lowerCamelCase_ ): return None # First grab the objects without a specific backend in _import_structure snake_case_ : Union[str, Any] = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: snake_case_ : str = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(lowerCamelCase_ ): snake_case_ : Optional[int] = _re_one_line_import_struct.search(lowerCamelCase_ ).groups()[0] snake_case_ : Union[str, Any] = re.findall(R"""\[([^\]]+)\]""" , lowerCamelCase_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue snake_case_ : Any = _re_import_struct_key_value.search(lowerCamelCase_ ) if single_line_import_search is not None: snake_case_ : Optional[int] = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(""", """ ) if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) line_index += 1 snake_case_ : Union[str, Any] = {"""none""": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("""if TYPE_CHECKING""" ): # If the line is an if not is_backend_available, we grab all objects associated. snake_case_ : List[str] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : Tuple = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Dict = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): snake_case_ : List[Any] = lines[line_index] if _re_import_struct_add_one.search(lowerCamelCase_ ) is not None: objects.append(_re_import_struct_add_one.search(lowerCamelCase_ ).groups()[0] ) elif _re_import_struct_add_many.search(lowerCamelCase_ ) is not None: snake_case_ : Optional[int] = _re_import_struct_add_many.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : List[str] = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_between_brackets.search(lowerCamelCase_ ) is not None: snake_case_ : List[str] = _re_between_brackets.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : Any = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_quote_object.search(lowerCamelCase_ ) is not None: objects.append(_re_quote_object.search(lowerCamelCase_ ).groups()[0] ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) elif line.startswith(""" """ * 12 + """\"""" ): objects.append(line[13:-3] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend snake_case_ : List[Any] = [] while ( line_index < len(lowerCamelCase_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): snake_case_ : Union[str, Any] = lines[line_index] snake_case_ : Union[str, Any] = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 snake_case_ : Dict = {"""none""": objects} # Let's continue with backend-specific objects while line_index < len(lowerCamelCase_ ): # If the line is an if is_backend_available, we grab all objects associated. snake_case_ : Optional[Any] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : str = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Any = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): snake_case_ : Dict = lines[line_index] snake_case_ : Any = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 12 ): objects.append(line[12:-2] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 return import_dict_objects, type_hint_objects def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :List[str] ): '''simple docstring''' def find_duplicates(lowerCamelCase_ :Union[str, Any] ): return [k for k, v in collections.Counter(lowerCamelCase_ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] snake_case_ : Optional[int] = [] for key in import_dict_objects.keys(): snake_case_ : int = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) snake_case_ : List[str] = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): snake_case_ : str = """base imports""" if key == """none""" else F'''{key} backend''' errors.append(F'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Tuple = [] for root, _, files in os.walk(lowerCamelCase_ ): if "__init__.py" in files: snake_case_ : Any = os.path.join(lowerCamelCase_ , """__init__.py""" ) snake_case_ : Dict = parse_init(lowerCamelCase_ ) if objects is not None: snake_case_ : Any = analyze_results(*lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: snake_case_ : Tuple = F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append("""\n""".join(lowerCamelCase_ ) ) if len(lowerCamelCase_ ) > 0: raise ValueError("""\n\n""".join(lowerCamelCase_ ) ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Union[str, Any] = [] for path, directories, files in os.walk(lowerCamelCase_ ): for folder in directories: # Ignore private modules if folder.startswith("""_""" ): directories.remove(lowerCamelCase_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(lowerCamelCase_ ) / folder).glob("""*.py""" ) ) ) == 0: continue snake_case_ : Tuple = str((Path(lowerCamelCase_ ) / folder).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(os.path.sep , """.""" ) submodules.append(lowerCamelCase_ ) for fname in files: if fname == "__init__.py": continue snake_case_ : Dict = str((Path(lowerCamelCase_ ) / fname).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(lowerCamelCase_ ) return submodules __A : List[Any] = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', 'models.esm.openfold_utils', ] def UpperCAmelCase ( ): '''simple docstring''' # This is to make sure the transformers module imported is the one in the repo. from transformers.utils import direct_transformers_import snake_case_ : Union[str, Any] = direct_transformers_import(lowerCamelCase_ ) snake_case_ : List[str] = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(lowerCamelCase_ , """__init__.py""" ) , """r""" ) as f: snake_case_ : str = f.read() import_structure_keys.update(set(re.findall(R"""import_structure\[\"([^\"]*)\"\]""" , lowerCamelCase_ ) ) ) snake_case_ : Dict = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(lowerCamelCase_ ) > 0: snake_case_ : str = """\n""".join(F'''- {module}''' for module in module_not_registered ) raise ValueError( """The following submodules are not properly registed in the main init of Transformers:\n""" F'''{list_of_modules}\n''' """Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.""" ) if __name__ == "__main__": check_all_inits() check_submodules()
8
0
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("""iterations must be defined as integers""" ) if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or not number >= 1: raise ValueError( """starting number must be and integer and be more than 0""" ) if not iterations >= 1: raise ValueError("""Iterations must be done more than 0 times to play FizzBuzz""" ) snake_case_ : Dict = """""" while number <= iterations: if number % 3 == 0: out += "Fizz" if number % 5 == 0: out += "Buzz" if 0 not in (number % 3, number % 5): out += str(UpperCAmelCase__ ) # print(out) number += 1 out += " " return out if __name__ == "__main__": import doctest doctest.testmod()
370
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __UpperCamelCase ( unittest.TestCase ): def __init__( self :List[Any] ,_UpperCamelCase :List[str] ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Union[str, Any]=3 ,_UpperCamelCase :Any=1_8 ,_UpperCamelCase :Optional[Any]=3_0 ,_UpperCamelCase :List[str]=4_0_0 ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :List[Any]=True ,): snake_case_ : List[str] = size if size is not None else {"""height""": 1_8, """width""": 1_8} snake_case_ : Union[str, Any] = parent snake_case_ : str = batch_size snake_case_ : List[Any] = num_channels snake_case_ : Tuple = image_size snake_case_ : int = min_resolution snake_case_ : int = max_resolution snake_case_ : Union[str, Any] = do_resize snake_case_ : Optional[Any] = size snake_case_ : Any = apply_ocr def a__ ( self :Union[str, Any] ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Tuple = LayoutLMvaImageProcessor if is_pytesseract_available() else None def a__ ( self :List[Any] ): snake_case_ : Union[str, Any] = LayoutLMvaImageProcessingTester(self ) @property def a__ ( self :int ): return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self :Any ): snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase ,"""do_resize""" ) ) self.assertTrue(hasattr(_UpperCamelCase ,"""size""" ) ) self.assertTrue(hasattr(_UpperCamelCase ,"""apply_ocr""" ) ) def a__ ( self :int ): snake_case_ : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 1_8, """width""": 1_8} ) snake_case_ : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict ,size=4_2 ) self.assertEqual(image_processor.size ,{"""height""": 4_2, """width""": 4_2} ) def a__ ( self :Optional[Any] ): pass def a__ ( self :Union[str, Any] ): # Initialize image_processing snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ : List[str] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,Image.Image ) # Test not batched input snake_case_ : List[str] = image_processing(image_inputs[0] ,return_tensors="""pt""" ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) self.assertIsInstance(encoding.words ,_UpperCamelCase ) self.assertIsInstance(encoding.boxes ,_UpperCamelCase ) # Test batched snake_case_ : List[Any] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :Tuple ): # Initialize image_processing snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ,numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,np.ndarray ) # Test not batched input snake_case_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Any = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :Optional[Any] ): # Initialize image_processing snake_case_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ : Optional[int] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ,torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,torch.Tensor ) # Test not batched input snake_case_ : Tuple = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Union[str, Any] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :List[Any] ): # with apply_OCR = True snake_case_ : Any = LayoutLMvaImageProcessor() from datasets import load_dataset snake_case_ : List[Any] = load_dataset("""hf-internal-testing/fixtures_docvqa""" ,split="""test""" ) snake_case_ : str = Image.open(ds[0]["""file"""] ).convert("""RGB""" ) snake_case_ : Dict = image_processing(_UpperCamelCase ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 2_2_4, 2_2_4) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 snake_case_ : Tuple = [["""11:14""", """to""", """11:39""", """a.m""", """11:39""", """to""", """11:44""", """a.m.""", """11:44""", """a.m.""", """to""", """12:25""", """p.m.""", """12:25""", """to""", """12:58""", """p.m.""", """12:58""", """to""", """4:00""", """p.m.""", """2:00""", """to""", """5:00""", """p.m.""", """Coffee""", """Break""", """Coffee""", """will""", """be""", """served""", """for""", """men""", """and""", """women""", """in""", """the""", """lobby""", """adjacent""", """to""", """exhibit""", """area.""", """Please""", """move""", """into""", """exhibit""", """area.""", """(Exhibits""", """Open)""", """TRRF""", """GENERAL""", """SESSION""", """(PART""", """|)""", """Presiding:""", """Lee""", """A.""", """Waller""", """TRRF""", """Vice""", """President""", """“Introductory""", """Remarks”""", """Lee""", """A.""", """Waller,""", """TRRF""", """Vice""", """Presi-""", """dent""", """Individual""", """Interviews""", """with""", """TRRF""", """Public""", """Board""", """Members""", """and""", """Sci-""", """entific""", """Advisory""", """Council""", """Mem-""", """bers""", """Conducted""", """by""", """TRRF""", """Treasurer""", """Philip""", """G.""", """Kuehn""", """to""", """get""", """answers""", """which""", """the""", """public""", """refrigerated""", """warehousing""", """industry""", """is""", """looking""", """for.""", """Plus""", """questions""", """from""", """the""", """floor.""", """Dr.""", """Emil""", """M.""", """Mrak,""", """University""", """of""", """Cal-""", """ifornia,""", """Chairman,""", """TRRF""", """Board;""", """Sam""", """R.""", """Cecil,""", """University""", """of""", """Georgia""", """College""", """of""", """Agriculture;""", """Dr.""", """Stanley""", """Charm,""", """Tufts""", """University""", """School""", """of""", """Medicine;""", """Dr.""", """Robert""", """H.""", """Cotton,""", """ITT""", """Continental""", """Baking""", """Company;""", """Dr.""", """Owen""", """Fennema,""", """University""", """of""", """Wis-""", """consin;""", """Dr.""", """Robert""", """E.""", """Hardenburg,""", """USDA.""", """Questions""", """and""", """Answers""", """Exhibits""", """Open""", """Capt.""", """Jack""", """Stoney""", """Room""", """TRRF""", """Scientific""", """Advisory""", """Council""", """Meeting""", """Ballroom""", """Foyer"""]] # noqa: E231 snake_case_ : Any = [[[1_4_1, 5_7, 2_1_4, 6_9], [2_2_8, 5_8, 2_5_2, 6_9], [1_4_1, 7_5, 2_1_6, 8_8], [2_3_0, 7_9, 2_8_0, 8_8], [1_4_2, 2_6_0, 2_1_8, 2_7_3], [2_3_0, 2_6_1, 2_5_5, 2_7_3], [1_4_3, 2_7_9, 2_1_8, 2_9_0], [2_3_1, 2_8_2, 2_9_0, 2_9_1], [1_4_3, 3_4_2, 2_1_8, 3_5_4], [2_3_1, 3_4_5, 2_8_9, 3_5_5], [2_0_2, 3_6_2, 2_2_7, 3_7_3], [1_4_3, 3_7_9, 2_2_0, 3_9_2], [2_3_1, 3_8_2, 2_9_1, 3_9_4], [1_4_4, 7_1_4, 2_2_0, 7_2_6], [2_3_1, 7_1_5, 2_5_6, 7_2_6], [1_4_4, 7_3_2, 2_2_0, 7_4_5], [2_3_2, 7_3_6, 2_9_1, 7_4_7], [1_4_4, 7_6_9, 2_1_8, 7_8_2], [2_3_1, 7_7_0, 2_5_6, 7_8_2], [1_4_1, 7_8_8, 2_0_2, 8_0_1], [2_1_5, 7_9_1, 2_7_4, 8_0_4], [1_4_3, 8_2_6, 2_0_4, 8_3_8], [2_1_5, 8_2_6, 2_4_0, 8_3_8], [1_4_2, 8_4_4, 2_0_2, 8_5_7], [2_1_5, 8_4_7, 2_7_4, 8_5_9], [3_3_4, 5_7, 4_2_7, 6_9], [4_4_0, 5_7, 5_2_2, 6_9], [3_6_9, 7_5, 4_6_1, 8_8], [4_6_9, 7_5, 5_1_6, 8_8], [5_2_8, 7_6, 5_6_2, 8_8], [5_7_0, 7_6, 6_6_7, 8_8], [6_7_5, 7_5, 7_1_1, 8_7], [7_2_1, 7_9, 7_7_8, 8_8], [7_8_9, 7_5, 8_4_0, 8_8], [3_6_9, 9_7, 4_7_0, 1_0_7], [4_8_4, 9_4, 5_0_7, 1_0_6], [5_1_8, 9_4, 5_6_2, 1_0_7], [5_7_6, 9_4, 6_5_5, 1_1_0], [6_6_8, 9_4, 7_9_2, 1_0_9], [8_0_4, 9_5, 8_2_9, 1_0_7], [3_6_9, 1_1_3, 4_6_5, 1_2_5], [4_7_7, 1_1_6, 5_4_7, 1_2_5], [5_6_2, 1_1_3, 6_5_8, 1_2_5], [6_7_1, 1_1_6, 7_4_8, 1_2_5], [7_6_1, 1_1_3, 8_1_1, 1_2_5], [3_6_9, 1_3_1, 4_6_5, 1_4_3], [4_7_7, 1_3_3, 5_4_8, 1_4_3], [5_6_3, 1_3_0, 6_9_8, 1_4_5], [7_1_0, 1_3_0, 8_0_2, 1_4_6], [3_3_6, 1_7_1, 4_1_2, 1_8_3], [4_2_3, 1_7_1, 5_7_2, 1_8_3], [5_8_2, 1_7_0, 7_1_6, 1_8_4], [7_2_8, 1_7_1, 8_1_7, 1_8_7], [8_2_9, 1_7_1, 8_4_4, 1_8_6], [3_3_8, 1_9_7, 4_8_2, 2_1_2], [5_0_7, 1_9_6, 5_5_7, 2_0_9], [5_6_9, 1_9_6, 5_9_5, 2_0_8], [6_1_0, 1_9_6, 7_0_2, 2_0_9], [5_0_5, 2_1_4, 5_8_3, 2_2_6], [5_9_5, 2_1_4, 6_5_6, 2_2_7], [6_7_0, 2_1_5, 8_0_7, 2_2_7], [3_3_5, 2_5_9, 5_4_3, 2_7_4], [5_5_6, 2_5_9, 7_0_8, 2_7_2], [3_7_2, 2_7_9, 4_2_2, 2_9_1], [4_3_5, 2_7_9, 4_6_0, 2_9_1], [4_7_4, 2_7_9, 5_7_4, 2_9_2], [5_8_7, 2_7_8, 6_6_4, 2_9_1], [6_7_6, 2_7_8, 7_3_8, 2_9_1], [7_5_1, 2_7_9, 8_3_4, 2_9_1], [3_7_2, 2_9_8, 4_3_4, 3_1_0], [3_3_5, 3_4_1, 4_8_3, 3_5_4], [4_9_7, 3_4_1, 6_5_5, 3_5_4], [6_6_7, 3_4_1, 7_2_8, 3_5_4], [7_4_0, 3_4_1, 8_2_5, 3_5_4], [3_3_5, 3_6_0, 4_3_0, 3_7_2], [4_4_2, 3_6_0, 5_3_4, 3_7_2], [5_4_5, 3_5_9, 6_8_7, 3_7_2], [6_9_7, 3_6_0, 7_5_4, 3_7_2], [7_6_5, 3_6_0, 8_2_3, 3_7_3], [3_3_4, 3_7_8, 4_2_8, 3_9_1], [4_4_0, 3_7_8, 5_7_7, 3_9_4], [5_9_0, 3_7_8, 7_0_5, 3_9_1], [7_2_0, 3_7_8, 8_0_1, 3_9_1], [3_3_4, 3_9_7, 4_0_0, 4_0_9], [3_7_0, 4_1_6, 5_2_9, 4_2_9], [5_4_4, 4_1_6, 5_7_6, 4_3_2], [5_8_7, 4_1_6, 6_6_5, 4_2_8], [6_7_7, 4_1_6, 8_1_4, 4_2_9], [3_7_2, 4_3_5, 4_5_2, 4_5_0], [4_6_5, 4_3_4, 4_9_5, 4_4_7], [5_1_1, 4_3_4, 6_0_0, 4_4_7], [6_1_1, 4_3_6, 6_3_7, 4_4_7], [6_4_9, 4_3_6, 6_9_4, 4_5_1], [7_0_5, 4_3_8, 8_2_4, 4_4_7], [3_6_9, 4_5_3, 4_5_2, 4_6_6], [4_6_4, 4_5_4, 5_0_9, 4_6_6], [5_2_2, 4_5_3, 6_1_1, 4_6_9], [6_2_5, 4_5_3, 7_9_2, 4_6_9], [3_7_0, 4_7_2, 5_5_6, 4_8_8], [5_7_0, 4_7_2, 6_8_4, 4_8_7], [6_9_7, 4_7_2, 7_1_8, 4_8_5], [7_3_2, 4_7_2, 8_3_5, 4_8_8], [3_6_9, 4_9_0, 4_1_1, 5_0_3], [4_2_5, 4_9_0, 4_8_4, 5_0_3], [4_9_6, 4_9_0, 6_3_5, 5_0_6], [6_4_5, 4_9_0, 7_0_7, 5_0_3], [7_1_8, 4_9_1, 7_6_1, 5_0_3], [7_7_1, 4_9_0, 8_4_0, 5_0_3], [3_3_6, 5_1_0, 3_7_4, 5_2_1], [3_8_8, 5_1_0, 4_4_7, 5_2_2], [4_6_0, 5_1_0, 4_8_9, 5_2_1], [5_0_3, 5_1_0, 5_8_0, 5_2_2], [5_9_2, 5_0_9, 7_3_6, 5_2_5], [7_4_5, 5_0_9, 7_7_0, 5_2_2], [7_8_1, 5_0_9, 8_4_0, 5_2_2], [3_3_8, 5_2_8, 4_3_4, 5_4_1], [4_4_8, 5_2_8, 5_9_6, 5_4_1], [6_0_9, 5_2_7, 6_8_7, 5_4_0], [7_0_0, 5_2_8, 7_9_2, 5_4_1], [3_3_6, 5_4_6, 3_9_7, 5_5_9], [4_0_7, 5_4_6, 4_3_1, 5_5_9], [4_4_3, 5_4_6, 5_2_5, 5_6_0], [5_3_7, 5_4_6, 6_8_0, 5_6_2], [6_8_8, 5_4_6, 7_1_4, 5_5_9], [7_2_2, 5_4_6, 8_3_7, 5_6_2], [3_3_6, 5_6_5, 4_4_9, 5_8_1], [4_6_1, 5_6_5, 4_8_5, 5_7_7], [4_9_7, 5_6_5, 6_6_5, 5_8_1], [6_8_1, 5_6_5, 7_1_8, 5_7_7], [7_3_2, 5_6_5, 8_3_7, 5_8_0], [3_3_7, 5_8_4, 4_3_8, 5_9_7], [4_5_2, 5_8_3, 5_2_1, 5_9_6], [5_3_5, 5_8_4, 6_7_7, 5_9_9], [6_9_0, 5_8_3, 7_8_7, 5_9_6], [8_0_1, 5_8_3, 8_2_5, 5_9_6], [3_3_8, 6_0_2, 4_7_8, 6_1_5], [4_9_2, 6_0_2, 5_3_0, 6_1_4], [5_4_3, 6_0_2, 6_3_8, 6_1_5], [6_5_0, 6_0_2, 6_7_6, 6_1_4], [6_8_8, 6_0_2, 7_8_8, 6_1_5], [8_0_2, 6_0_2, 8_4_3, 6_1_4], [3_3_7, 6_2_1, 5_0_2, 6_3_3], [5_1_6, 6_2_1, 6_1_5, 6_3_7], [6_2_9, 6_2_1, 7_7_4, 6_3_6], [7_8_9, 6_2_1, 8_2_7, 6_3_3], [3_3_7, 6_3_9, 4_1_8, 6_5_2], [4_3_2, 6_4_0, 5_7_1, 6_5_3], [5_8_7, 6_3_9, 7_3_1, 6_5_5], [7_4_3, 6_3_9, 7_6_9, 6_5_2], [7_8_0, 6_3_9, 8_4_1, 6_5_2], [3_3_8, 6_5_8, 4_4_0, 6_7_3], [4_5_5, 6_5_8, 4_9_1, 6_7_0], [5_0_8, 6_5_8, 6_0_2, 6_7_1], [6_1_6, 6_5_8, 6_3_8, 6_7_0], [6_5_4, 6_5_8, 8_3_5, 6_7_4], [3_3_7, 6_7_7, 4_2_9, 6_8_9], [3_3_7, 7_1_4, 4_8_2, 7_2_6], [4_9_5, 7_1_4, 5_4_8, 7_2_6], [5_6_1, 7_1_4, 6_8_3, 7_2_6], [3_3_8, 7_7_0, 4_6_1, 7_8_2], [4_7_4, 7_6_9, 5_5_4, 7_8_5], [4_8_9, 7_8_8, 5_6_2, 8_0_3], [5_7_6, 7_8_8, 6_4_3, 8_0_1], [6_5_6, 7_8_7, 7_5_1, 8_0_4], [7_6_4, 7_8_8, 8_4_4, 8_0_1], [3_3_4, 8_2_5, 4_2_1, 8_3_8], [4_3_0, 8_2_4, 5_7_4, 8_3_8], [5_8_4, 8_2_4, 7_2_3, 8_4_1], [3_3_5, 8_4_4, 4_5_0, 8_5_7], [4_6_4, 8_4_3, 5_8_3, 8_6_0], [6_2_8, 8_6_2, 7_5_5, 8_7_5], [7_6_9, 8_6_1, 8_4_8, 8_7_8]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,_UpperCamelCase ) self.assertListEqual(encoding.boxes ,_UpperCamelCase ) # with apply_OCR = False snake_case_ : Dict = LayoutLMvaImageProcessor(apply_ocr=_UpperCamelCase ) snake_case_ : Optional[int] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 2_2_4, 2_2_4) )
8
0
'''simple docstring''' import unittest from transformers import PegasusTokenizer, PegasusTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __A : Dict = get_tests_dir('fixtures/test_sentencepiece_no_bos.model') @require_sentencepiece @require_tokenizers class __UpperCamelCase ( __lowercase , unittest.TestCase ): lowercase : Union[str, Any] = PegasusTokenizer lowercase : Dict = PegasusTokenizerFast lowercase : Tuple = True lowercase : List[Any] = True def a__ ( self :Union[str, Any] ): super().setUp() # We have a SentencePiece fixture for testing snake_case_ : Tuple = PegasusTokenizer(_a ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self :str ): return PegasusTokenizer.from_pretrained("""google/pegasus-large""" ) def a__ ( self :List[str] ,**_UpperCamelCase :List[str] ): return PegasusTokenizer.from_pretrained(self.tmpdirname ,**_a ) def a__ ( self :int ,_UpperCamelCase :Union[str, Any] ): return ("This is a test", "This is a test") def a__ ( self :List[str] ): snake_case_ : Optional[Any] = """</s>""" snake_case_ : int = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) ,_a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) ,_a ) def a__ ( self :int ): snake_case_ : str = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"""<pad>""" ) self.assertEqual(vocab_keys[1] ,"""</s>""" ) self.assertEqual(vocab_keys[-1] ,"""v""" ) self.assertEqual(len(_a ) ,1_1_0_3 ) def a__ ( self :Union[str, Any] ): self.assertEqual(self.get_tokenizer().vocab_size ,1_1_0_3 ) def a__ ( self :int ): snake_case_ : Union[str, Any] = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) snake_case_ : Any = self.tokenizer_class.from_pretrained(self.tmpdirname ) snake_case_ : Any = ( """Let's see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important""" """ </s> <pad> <pad> <pad>""" ) snake_case_ : Optional[int] = rust_tokenizer([raw_input_str] ,return_tensors=_a ,add_special_tokens=_a ).input_ids[0] snake_case_ : Any = py_tokenizer([raw_input_str] ,return_tensors=_a ,add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a ,_a ) def a__ ( self :Union[str, Any] ): snake_case_ : Dict = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word snake_case_ : List[Any] = """<mask_1> To ensure a <mask_2> flow of bank resolutions.""" snake_case_ : List[str] = [2, 4_1_3, 6_1_5, 1_1_4, 3, 1_9_7_1, 1_1_3, 1_6_7_9, 1_0_7_1_0, 1_0_7, 1] snake_case_ : Dict = tokenizer([raw_input_str] ,return_tensors=_a ).input_ids[0] self.assertListEqual(_a ,_a ) def a__ ( self :str ): snake_case_ : str = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6_1_0_3 assert tokenizer.pad_token_id == 0 assert tokenizer.eos_token_id == 1 assert tokenizer.offset == 1_0_3 assert tokenizer.unk_token_id == tokenizer.offset + 2 == 1_0_5 assert tokenizer.unk_token == "<unk>" assert tokenizer.model_max_length == 1_0_2_4 snake_case_ : Dict = """To ensure a smooth flow of bank resolutions.""" snake_case_ : List[str] = [4_1_3, 6_1_5, 1_1_4, 2_2_9_1, 1_9_7_1, 1_1_3, 1_6_7_9, 1_0_7_1_0, 1_0_7, 1] snake_case_ : List[str] = tokenizer([raw_input_str] ,return_tensors=_a ).input_ids[0] self.assertListEqual(_a ,_a ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def a__ ( self :str ): snake_case_ : Optional[int] = ["""This is going to be way too long.""" * 1_5_0, """short example"""] snake_case_ : Optional[int] = ["""not super long but more than 5 tokens""", """tiny"""] snake_case_ : Optional[int] = self._large_tokenizer(_a ,padding=_a ,truncation=_a ,return_tensors="""pt""" ) snake_case_ : Optional[int] = self._large_tokenizer( text_target=_a ,max_length=5 ,padding=_a ,truncation=_a ,return_tensors="""pt""" ) assert batch.input_ids.shape == (2, 1_0_2_4) assert batch.attention_mask.shape == (2, 1_0_2_4) assert targets["input_ids"].shape == (2, 5) assert len(_a ) == 2 # input_ids, attention_mask. @slow def a__ ( self :Optional[Any] ): # fmt: off snake_case_ : Dict = {"""input_ids""": [[3_8_9_7_9, 1_4_3, 1_8_4_8_5, 6_0_6, 1_3_0, 2_6_6_6_9, 8_7_6_8_6, 1_2_1, 5_4_1_8_9, 1_1_2_9, 1_1_1, 2_6_6_6_9, 8_7_6_8_6, 1_2_1, 9_1_1_4, 1_4_7_8_7, 1_2_1, 1_3_2_4_9, 1_5_8, 5_9_2, 9_5_6, 1_2_1, 1_4_6_2_1, 3_1_5_7_6, 1_4_3, 6_2_6_1_3, 1_0_8, 9_6_8_8, 9_3_0, 4_3_4_3_0, 1_1_5_6_2, 6_2_6_1_3, 3_0_4, 1_0_8, 1_1_4_4_3, 8_9_7, 1_0_8, 9_3_1_4, 1_7_4_1_5, 6_3_3_9_9, 1_0_8, 1_1_4_4_3, 7_6_1_4, 1_8_3_1_6, 1_1_8, 4_2_8_4, 7_1_4_8, 1_2_4_3_0, 1_4_3, 1_4_0_0, 2_5_7_0_3, 1_5_8, 1_1_1, 4_2_8_4, 7_1_4_8, 1_1_7_7_2, 1_4_3, 2_1_2_9_7, 1_0_6_4, 1_5_8, 1_2_2, 2_0_4, 3_5_0_6, 1_7_5_4, 1_1_3_3, 1_4_7_8_7, 1_5_8_1, 1_1_5, 3_3_2_2_4, 4_4_8_2, 1_1_1, 1_3_5_5, 1_1_0, 2_9_1_7_3, 3_1_7, 5_0_8_3_3, 1_0_8, 2_0_1_4_7, 9_4_6_6_5, 1_1_1, 7_7_1_9_8, 1_0_7, 1], [1_1_0, 6_2_6_1_3, 1_1_7, 6_3_8, 1_1_2, 1_1_3_3, 1_2_1, 2_0_0_9_8, 1_3_5_5, 7_9_0_5_0, 1_3_8_7_2, 1_3_5, 1_5_9_6, 5_3_5_4_1, 1_3_5_2, 1_4_1, 1_3_0_3_9, 5_5_4_2, 1_2_4, 3_0_2, 5_1_8, 1_1_1, 2_6_8, 2_9_5_6, 1_1_5, 1_4_9, 4_4_2_7, 1_0_7, 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], [1_3_9, 1_2_3_5, 2_7_9_9, 1_8_2_8_9, 1_7_7_8_0, 2_0_4, 1_0_9, 9_4_7_4, 1_2_9_6, 1_0_7, 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]], """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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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="""google/bigbird-pegasus-large-arxiv""" ,revision="""ba85d0851d708441f91440d509690f1ab6353415""" ,) @require_sentencepiece @require_tokenizers class __UpperCamelCase ( __lowercase , unittest.TestCase ): lowercase : Optional[Any] = PegasusTokenizer lowercase : int = PegasusTokenizerFast lowercase : Optional[Any] = True lowercase : Optional[int] = True def a__ ( self :str ): super().setUp() # We have a SentencePiece fixture for testing snake_case_ : Tuple = PegasusTokenizer(_a ,offset=0 ,mask_token_sent=_a ,mask_token="""[MASK]""" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self :str ): return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv""" ) def a__ ( self :Optional[Any] ,**_UpperCamelCase :Optional[int] ): return PegasusTokenizer.from_pretrained(self.tmpdirname ,**_a ) def a__ ( self :List[Any] ,_UpperCamelCase :int ): return ("This is a test", "This is a test") def a__ ( self :List[Any] ): snake_case_ : List[str] = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) snake_case_ : str = self.tokenizer_class.from_pretrained(self.tmpdirname ) snake_case_ : Tuple = ( """Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>""" """ <pad> <pad> <pad>""" ) snake_case_ : Optional[int] = rust_tokenizer([raw_input_str] ,return_tensors=_a ,add_special_tokens=_a ).input_ids[0] snake_case_ : Any = py_tokenizer([raw_input_str] ,return_tensors=_a ,add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a ,_a ) @require_torch def a__ ( self :Optional[Any] ): snake_case_ : Optional[int] = ["""This is going to be way too long.""" * 1_0_0_0, """short example"""] snake_case_ : List[Any] = ["""not super long but more than 5 tokens""", """tiny"""] snake_case_ : int = self._large_tokenizer(_a ,padding=_a ,truncation=_a ,return_tensors="""pt""" ) snake_case_ : int = self._large_tokenizer( text_target=_a ,max_length=5 ,padding=_a ,truncation=_a ,return_tensors="""pt""" ) assert batch.input_ids.shape == (2, 4_0_9_6) assert batch.attention_mask.shape == (2, 4_0_9_6) assert targets["input_ids"].shape == (2, 5) assert len(_a ) == 2 # input_ids, attention_mask. def a__ ( self :List[str] ): snake_case_ : List[Any] = ( """This is an example string that is used to test the original TF implementation against the HF""" """ implementation""" ) snake_case_ : Dict = self._large_tokenizer(_a ).input_ids self.assertListEqual( _a ,[1_8_2, 1_1_7, 1_4_2, 5_8_7, 4_2_1_1, 1_2_0, 1_1_7, 2_6_3, 1_1_2, 8_0_4, 1_0_9, 8_5_6, 2_5_0_1_6, 3_1_3_7, 4_6_4, 1_0_9, 2_6_9_5_5, 3_1_3_7, 1] ,)
371
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : List[Any] = generate_pascal_triangle(lowerCamelCase_ ) for row_idx in range(lowerCamelCase_ ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=""" """ ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=""" """ ) else: print(triangle[row_idx][col_idx] , end="""""" ) print() def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError("""The input value of 'num_rows' should be 'int'""" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( """The input value of 'num_rows' should be greater than or equal to 0""" ) snake_case_ : list[list[int]] = [] for current_row_idx in range(lowerCamelCase_ ): snake_case_ : List[str] = populate_current_row(lowerCamelCase_ , lowerCamelCase_ ) triangle.append(lowerCamelCase_ ) return triangle def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Union[str, Any] = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 snake_case_ , snake_case_ : Optional[Any] = 1, 1 for current_col_idx in range(1 , lowerCamelCase_ ): calculate_current_element( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) return current_row def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :list[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , ): '''simple docstring''' snake_case_ : Union[str, Any] = triangle[current_row_idx - 1][current_col_idx - 1] snake_case_ : List[Any] = triangle[current_row_idx - 1][current_col_idx] snake_case_ : Optional[int] = above_to_left_elt + above_to_right_elt def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError("""The input value of 'num_rows' should be 'int'""" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( """The input value of 'num_rows' should be greater than or equal to 0""" ) snake_case_ : list[list[int]] = [[1]] for row_index in range(1 , lowerCamelCase_ ): snake_case_ : Optional[Any] = [0] + result[-1] + [0] snake_case_ : Dict = row_index + 1 # Calculate the number of distinct elements in a row snake_case_ : Any = sum(divmod(lowerCamelCase_ , 2 ) ) snake_case_ : Tuple = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] snake_case_ : Optional[int] = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() snake_case_ : str = row_first_half + row_second_half result.append(lowerCamelCase_ ) return result def UpperCAmelCase ( ): '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(lowerCamelCase_ :Callable , lowerCamelCase_ :int ) -> None: snake_case_ : Dict = F'''{func.__name__}({value})''' snake_case_ : Dict = timeit(F'''__main__.{call}''' , setup="""import __main__""" ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(F'''{call:38} -- {timing:.4f} seconds''' ) for value in range(15 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(lowerCamelCase_ , lowerCamelCase_ ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
8
0
'''simple docstring''' import io import json import fsspec import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.json import JsonDatasetReader, JsonDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def UpperCAmelCase ( lowerCamelCase_ :Tuple , lowerCamelCase_ :List[Any] ): '''simple docstring''' assert isinstance(__lowerCamelCase , __lowerCamelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Union[str, Any] = tmp_path / "cache" snake_case_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): snake_case_ : str = JsonDatasetReader(__lowerCamelCase , cache_dir=__lowerCamelCase , keep_in_memory=__lowerCamelCase ).read() _check_json_dataset(__lowerCamelCase , __lowerCamelCase ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict ): '''simple docstring''' snake_case_ : Optional[Any] = tmp_path / "cache" snake_case_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} snake_case_ : int = features.copy() if features else default_expected_features snake_case_ : Optional[Any] = ( Features({feature: Value(__lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) snake_case_ : Any = JsonDatasetReader(__lowerCamelCase , features=__lowerCamelCase , cache_dir=__lowerCamelCase ).read() _check_json_dataset(__lowerCamelCase , __lowerCamelCase ) @pytest.mark.parametrize( """features""" , [ None, {"""col_3""": """float64""", """col_1""": """string""", """col_2""": """int64"""}, ] , ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] ): '''simple docstring''' snake_case_ : List[str] = tmp_path / "cache" snake_case_ : Dict = {"col_3": "float64", "col_1": "string", "col_2": "int64"} snake_case_ : Any = features.copy() if features else default_expected_features snake_case_ : Dict = ( Features({feature: Value(__lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) snake_case_ : Dict = JsonDatasetReader(__lowerCamelCase , features=__lowerCamelCase , cache_dir=__lowerCamelCase ).read() assert isinstance(__lowerCamelCase , __lowerCamelCase ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_3", "col_1", "col_2"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[Any] ): '''simple docstring''' # jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"} snake_case_ : str = {"col_2": "int64", "col_3": "float64", "col_1": "string"} snake_case_ : str = features.copy() snake_case_ : Union[str, Any] = ( Features({feature: Value(__lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) snake_case_ : Any = tmp_path / "cache" snake_case_ : str = JsonDatasetReader(__lowerCamelCase , features=__lowerCamelCase , cache_dir=__lowerCamelCase ).read() assert isinstance(__lowerCamelCase , __lowerCamelCase ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_2", "col_3", "col_1"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] ): '''simple docstring''' snake_case_ : Dict = tmp_path / "cache" snake_case_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} snake_case_ : Dict = JsonDatasetReader(__lowerCamelCase , cache_dir=__lowerCamelCase , split=__lowerCamelCase ).read() _check_json_dataset(__lowerCamelCase , __lowerCamelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("""path_type""" , [str, list] ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] ): '''simple docstring''' if issubclass(__lowerCamelCase , __lowerCamelCase ): snake_case_ : Dict = jsonl_path elif issubclass(__lowerCamelCase , __lowerCamelCase ): snake_case_ : List[Any] = [jsonl_path] snake_case_ : str = tmp_path / "cache" snake_case_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} snake_case_ : str = JsonDatasetReader(__lowerCamelCase , cache_dir=__lowerCamelCase ).read() _check_json_dataset(__lowerCamelCase , __lowerCamelCase ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[Any]=("train",) ): '''simple docstring''' assert isinstance(__lowerCamelCase , __lowerCamelCase ) for split in splits: snake_case_ : List[str] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :List[Any] ): '''simple docstring''' snake_case_ : Tuple = tmp_path / "cache" snake_case_ : Optional[Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): snake_case_ : List[str] = JsonDatasetReader({"""train""": jsonl_path} , cache_dir=__lowerCamelCase , keep_in_memory=__lowerCamelCase ).read() _check_json_datasetdict(__lowerCamelCase , __lowerCamelCase ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : int = tmp_path / "cache" snake_case_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} snake_case_ : Any = features.copy() if features else default_expected_features snake_case_ : Union[str, Any] = ( Features({feature: Value(__lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) snake_case_ : Optional[Any] = JsonDatasetReader({"""train""": jsonl_path} , features=__lowerCamelCase , cache_dir=__lowerCamelCase ).read() _check_json_datasetdict(__lowerCamelCase , __lowerCamelCase ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Any ): '''simple docstring''' if split: snake_case_ : Dict = {split: jsonl_path} else: snake_case_ : Any = "train" snake_case_ : Union[str, Any] = {"train": jsonl_path, "test": jsonl_path} snake_case_ : Any = tmp_path / "cache" snake_case_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} snake_case_ : Dict = JsonDatasetReader(__lowerCamelCase , cache_dir=__lowerCamelCase ).read() _check_json_datasetdict(__lowerCamelCase , __lowerCamelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def UpperCAmelCase ( lowerCamelCase_ :Tuple ): '''simple docstring''' return json.load(__lowerCamelCase ) def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' return [json.loads(__lowerCamelCase ) for line in buffer] class __UpperCamelCase : @pytest.mark.parametrize("""lines, load_json_function""" ,[(True, load_json_lines), (False, load_json)] ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :str ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Tuple ): with io.BytesIO() as buffer: JsonDatasetWriter(lowercase_ ,lowercase_ ,lines=lowercase_ ).write() buffer.seek(0 ) snake_case_ : Dict = load_json_function(lowercase_ ) assert isinstance(lowercase_ ,lowercase_ ) assert isinstance(exported_content[0] ,lowercase_ ) assert len(lowercase_ ) == 1_0 @pytest.mark.parametrize( """orient, container, keys, len_at""" ,[ ("""records""", list, {"""tokens""", """labels""", """answers""", """id"""}, None), ("""split""", dict, {"""columns""", """data"""}, """data"""), ("""index""", dict, set("""0123456789""" ), None), ("""columns""", dict, {"""tokens""", """labels""", """answers""", """id"""}, """tokens"""), ("""values""", list, None, None), ("""table""", dict, {"""schema""", """data"""}, """data"""), ] ,) def a__ ( self :str ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Optional[Any] ): with io.BytesIO() as buffer: JsonDatasetWriter(lowercase_ ,lowercase_ ,lines=lowercase_ ,orient=lowercase_ ).write() buffer.seek(0 ) snake_case_ : Optional[Any] = load_json(lowercase_ ) assert isinstance(lowercase_ ,lowercase_ ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(lowercase_ ,"""keys""" ) and not hasattr(exported_content[0] ,"""keys""" ) if len_at: assert len(exported_content[len_at] ) == 1_0 else: assert len(lowercase_ ) == 1_0 @pytest.mark.parametrize("""lines, load_json_function""" ,[(True, load_json_lines), (False, load_json)] ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :List[str] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :List[str] ): with io.BytesIO() as buffer: JsonDatasetWriter(lowercase_ ,lowercase_ ,lines=lowercase_ ,num_proc=2 ).write() buffer.seek(0 ) snake_case_ : Optional[Any] = load_json_function(lowercase_ ) assert isinstance(lowercase_ ,lowercase_ ) assert isinstance(exported_content[0] ,lowercase_ ) assert len(lowercase_ ) == 1_0 @pytest.mark.parametrize( """orient, container, keys, len_at""" ,[ ("""records""", list, {"""tokens""", """labels""", """answers""", """id"""}, None), ("""split""", dict, {"""columns""", """data"""}, """data"""), ("""index""", dict, set("""0123456789""" ), None), ("""columns""", dict, {"""tokens""", """labels""", """answers""", """id"""}, """tokens"""), ("""values""", list, None, None), ("""table""", dict, {"""schema""", """data"""}, """data"""), ] ,) def a__ ( self :Tuple ,_UpperCamelCase :int ,_UpperCamelCase :Dict ,_UpperCamelCase :Dict ,_UpperCamelCase :Any ,_UpperCamelCase :Union[str, Any] ): with io.BytesIO() as buffer: JsonDatasetWriter(lowercase_ ,lowercase_ ,lines=lowercase_ ,orient=lowercase_ ,num_proc=2 ).write() buffer.seek(0 ) snake_case_ : List[Any] = load_json(lowercase_ ) assert isinstance(lowercase_ ,lowercase_ ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(lowercase_ ,"""keys""" ) and not hasattr(exported_content[0] ,"""keys""" ) if len_at: assert len(exported_content[len_at] ) == 1_0 else: assert len(lowercase_ ) == 1_0 def a__ ( self :List[Any] ,_UpperCamelCase :str ): with pytest.raises(lowercase_ ): with io.BytesIO() as buffer: JsonDatasetWriter(lowercase_ ,lowercase_ ,num_proc=0 ) @pytest.mark.parametrize("""compression, extension""" ,[("""gzip""", """gz"""), ("""bz2""", """bz2"""), ("""xz""", """xz""")] ) def a__ ( self :Optional[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Dict ,_UpperCamelCase :Dict ,_UpperCamelCase :int ,_UpperCamelCase :Tuple ): snake_case_ : List[Any] = tmp_path_factory.mktemp("""data""" ) / F'''test.json.{extension}''' snake_case_ : str = str(shared_datadir / F'''test_file.json.{extension}''' ) JsonDatasetWriter(lowercase_ ,lowercase_ ,compression=lowercase_ ).write() with fsspec.open(lowercase_ ,"""rb""" ,compression="""infer""" ) as f: snake_case_ : List[str] = f.read() with fsspec.open(lowercase_ ,"""rb""" ,compression="""infer""" ) as f: snake_case_ : Dict = f.read() assert exported_content == original_content
350
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class __UpperCamelCase ( unittest.TestCase ): @slow def a__ ( self :Dict ): snake_case_ : Optional[Any] = XLMRobertaModel.from_pretrained("""xlm-roberta-base""" ) snake_case_ : Optional[int] = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house snake_case_ : Tuple = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim snake_case_ : Dict = torch.tensor( [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): snake_case_ : Tuple = model(_UpperCamelCase )["""last_hidden_state"""].detach() self.assertEqual(output.shape ,_UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,_UpperCamelCase ,atol=1E-3 ) ) @slow def a__ ( self :Union[str, Any] ): snake_case_ : List[Any] = XLMRobertaModel.from_pretrained("""xlm-roberta-large""" ) snake_case_ : Dict = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house snake_case_ : List[Any] = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim snake_case_ : Any = torch.tensor( [[-0.06_99, -0.03_18, 0.07_05, -0.12_41, 0.09_99, -0.05_20, 0.10_04, -0.18_38, -0.47_04, 0.14_37, 0.08_21, 0.01_26]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): snake_case_ : str = model(_UpperCamelCase )["""last_hidden_state"""].detach() self.assertEqual(output.shape ,_UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,_UpperCamelCase ,atol=1E-3 ) )
8
0
'''simple docstring''' import os from glob import glob import imageio import torch import torchvision import wandb from img_processing import custom_to_pil, loop_post_process, preprocess, preprocess_vqgan from loaders import load_vqgan from PIL import Image from torch import nn from transformers import CLIPModel, CLIPTokenizerFast from utils import get_device, get_timestamp, show_pil class __UpperCamelCase : def __init__( self :Optional[Any] ,_UpperCamelCase :Optional[Any] = "cpu" ,_UpperCamelCase :Dict = "openai/clip-vit-large-patch14" ): snake_case_ : Union[str, Any] = device snake_case_ : int = CLIPTokenizerFast.from_pretrained(a_ ) snake_case_ : Any = [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] snake_case_ : Optional[Any] = [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] snake_case_ : int = torchvision.transforms.Normalize(self.image_mean ,self.image_std ) snake_case_ : List[Any] = torchvision.transforms.Resize(2_2_4 ) snake_case_ : str = torchvision.transforms.CenterCrop(2_2_4 ) def a__ ( self :Tuple ,_UpperCamelCase :int ): snake_case_ : Dict = self.resize(a_ ) snake_case_ : Optional[int] = self.center_crop(a_ ) snake_case_ : List[str] = self.normalize(a_ ) return images def __call__( self :Dict ,_UpperCamelCase :str=None ,_UpperCamelCase :Optional[int]=None ,**_UpperCamelCase :Tuple ): snake_case_ : str = self.tokenizer(text=a_ ,**a_ ) snake_case_ : Dict = self.preprocess_img(a_ ) snake_case_ : Dict = {key: value.to(self.device ) for (key, value) in encoding.items()} return encoding class __UpperCamelCase ( nn.Module ): def __init__( self :str ,_UpperCamelCase :Optional[int]=1_0 ,_UpperCamelCase :List[Any]=0.01 ,_UpperCamelCase :Any=None ,_UpperCamelCase :int=None ,_UpperCamelCase :int=None ,_UpperCamelCase :int=None ,_UpperCamelCase :Any=None ,_UpperCamelCase :str=None ,_UpperCamelCase :Any=False ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :int="image" ,_UpperCamelCase :List[str]=True ,_UpperCamelCase :int=False ,_UpperCamelCase :Union[str, Any]=False ,_UpperCamelCase :str=False ,): super().__init__() snake_case_ : Union[str, Any] = None snake_case_ : List[Any] = device if device else get_device() if vqgan: snake_case_ : List[str] = vqgan else: snake_case_ : Optional[Any] = load_vqgan(self.device ,conf_path=a_ ,ckpt_path=a_ ) self.vqgan.eval() if clip: snake_case_ : List[Any] = clip else: snake_case_ : Optional[int] = CLIPModel.from_pretrained("""openai/clip-vit-base-patch32""" ) self.clip.to(self.device ) snake_case_ : Optional[int] = ProcessorGradientFlow(device=self.device ) snake_case_ : Optional[int] = iterations snake_case_ : Any = lr snake_case_ : Optional[Any] = log snake_case_ : int = make_grid snake_case_ : List[str] = return_val snake_case_ : int = quantize snake_case_ : Optional[Any] = self.vqgan.decoder.z_shape def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :Optional[Any]=5 ,_UpperCamelCase :int=True ): snake_case_ : Union[str, Any] = [] if output_path is None: snake_case_ : str = '''./animation.gif''' if input_path is None: snake_case_ : Any = self.save_path snake_case_ : Optional[Any] = sorted(glob(input_path + """/*""" ) ) if not len(a_ ): raise ValueError( """No images found in save path, aborting (did you pass save_intermediate=True to the generate""" """ function?)""" ) if len(a_ ) == 1: print("""Only one image found in save path, (did you pass save_intermediate=True to the generate function?)""" ) snake_case_ : Optional[int] = total_duration / len(a_ ) snake_case_ : int = [frame_duration] * len(a_ ) if extend_frames: snake_case_ : Dict = 1.5 snake_case_ : List[Any] = 3 for file_name in paths: if file_name.endswith(""".png""" ): images.append(imageio.imread(a_ ) ) imageio.mimsave(a_ ,a_ ,duration=a_ ) print(F'''gif saved to {output_path}''' ) def a__ ( self :List[Any] ,_UpperCamelCase :List[str]=None ,_UpperCamelCase :Union[str, Any]=None ): if not (path or img): raise ValueError("""Input either path or tensor""" ) if img is not None: raise NotImplementedError snake_case_ : Dict = preprocess(Image.open(a_ ) ,target_image_size=2_5_6 ).to(self.device ) snake_case_ : Tuple = preprocess_vqgan(a_ ) snake_case_ : Union[str, Any] = self.vqgan.encode(a_ ) return z def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int] ): snake_case_ : Any = self.latent.detach().requires_grad_() snake_case_ : str = base_latent + transform_vector if self.quantize: snake_case_ : str = self.vqgan.quantize(a_ ) else: snake_case_ : Tuple = trans_latent return self.vqgan.decode(a_ ) def a__ ( self :Dict ,_UpperCamelCase :Tuple ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any]=None ): snake_case_ : List[str] = self.clip_preprocessor(text=a_ ,images=a_ ,return_tensors="""pt""" ,padding=a_ ) snake_case_ : Dict = self.clip(**a_ ) snake_case_ : Optional[Any] = clip_outputs.logits_per_image if weights is not None: snake_case_ : Dict = similarity_logits * weights return similarity_logits.sum() def a__ ( self :Dict ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Tuple ,_UpperCamelCase :Any ): snake_case_ : str = self._get_clip_similarity(pos_prompts["""prompts"""] ,a_ ,weights=(1 / pos_prompts["""weights"""]) ) if neg_prompts: snake_case_ : Any = self._get_clip_similarity(neg_prompts["""prompts"""] ,a_ ,weights=neg_prompts["""weights"""] ) else: snake_case_ : Optional[Any] = torch.tensor([1] ,device=self.device ) snake_case_ : int = -torch.log(a_ ) + torch.log(a_ ) return loss def a__ ( self :Dict ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any ): snake_case_ : int = torch.randn_like(self.latent ,requires_grad=a_ ,device=self.device ) snake_case_ : List[str] = torch.optim.Adam([vector] ,lr=self.lr ) for i in range(self.iterations ): optim.zero_grad() snake_case_ : Optional[int] = self._add_vector(a_ ) snake_case_ : Tuple = loop_post_process(a_ ) snake_case_ : Tuple = self._get_CLIP_loss(a_ ,a_ ,a_ ) print("""CLIP loss""" ,a_ ) if self.log: wandb.log({"""CLIP Loss""": clip_loss} ) clip_loss.backward(retain_graph=a_ ) optim.step() if self.return_val == "image": yield custom_to_pil(transformed_img[0] ) else: yield vector def a__ ( self :str ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Dict ,_UpperCamelCase :Optional[Any] ): wandb.init(reinit=a_ ,project="""face-editor""" ) wandb.config.update({"""Positive Prompts""": positive_prompts} ) wandb.config.update({"""Negative Prompts""": negative_prompts} ) wandb.config.update({"""lr""": self.lr, """iterations""": self.iterations} ) if image_path: snake_case_ : Optional[int] = Image.open(a_ ) snake_case_ : Any = image.resize((2_5_6, 2_5_6) ) wandb.log("""Original Image""" ,wandb.Image(a_ ) ) def a__ ( self :Optional[int] ,_UpperCamelCase :Dict ): if not prompts: return [] snake_case_ : Dict = [] snake_case_ : List[Any] = [] if isinstance(a_ ,a_ ): snake_case_ : List[str] = [prompt.strip() for prompt in prompts.split("""|""" )] for prompt in prompts: if isinstance(a_ ,(tuple, list) ): snake_case_ : int = prompt[0] snake_case_ : str = float(prompt[1] ) elif ":" in prompt: snake_case_ : Any = prompt.split(""":""" ) snake_case_ : str = float(a_ ) else: snake_case_ : Optional[int] = prompt snake_case_ : str = 1.0 processed_prompts.append(a_ ) weights.append(a_ ) return { "prompts": processed_prompts, "weights": torch.tensor(a_ ,device=self.device ), } def a__ ( self :Union[str, Any] ,_UpperCamelCase :str ,_UpperCamelCase :str=None ,_UpperCamelCase :int=None ,_UpperCamelCase :Dict=True ,_UpperCamelCase :Any=False ,_UpperCamelCase :Union[str, Any]=True ,_UpperCamelCase :str=True ,_UpperCamelCase :Any=None ,): if image_path: snake_case_ : Optional[Any] = self._get_latent(a_ ) else: snake_case_ : Union[str, Any] = torch.randn(self.latent_dim ,device=self.device ) if self.log: self._init_logging(a_ ,a_ ,a_ ) assert pos_prompts, "You must provide at least one positive prompt." snake_case_ : Optional[int] = self.process_prompts(a_ ) snake_case_ : Union[str, Any] = self.process_prompts(a_ ) if save_final and save_path is None: snake_case_ : Union[str, Any] = os.path.join("""./outputs/""" ,"""_""".join(pos_prompts["""prompts"""] ) ) if not os.path.exists(a_ ): os.makedirs(a_ ) else: snake_case_ : List[Any] = save_path + '''_''' + get_timestamp() os.makedirs(a_ ) snake_case_ : List[str] = save_path snake_case_ : int = self.vqgan.decode(self.latent )[0] if show_intermediate: print("""Original Image""" ) show_pil(custom_to_pil(a_ ) ) snake_case_ : Any = loop_post_process(a_ ) for iter, transformed_img in enumerate(self._optimize_CLIP(a_ ,a_ ,a_ ) ): if show_intermediate: show_pil(a_ ) if save_intermediate: transformed_img.save(os.path.join(self.save_path ,F'''iter_{iter:03d}.png''' ) ) if self.log: wandb.log({"""Image""": wandb.Image(a_ )} ) if show_final: show_pil(a_ ) if save_final: transformed_img.save(os.path.join(self.save_path ,F'''iter_{iter:03d}_final.png''' ) )
351
'''simple docstring''' from __future__ import annotations from collections.abc import Callable def UpperCAmelCase ( lowerCamelCase_ :Callable[[int | float], int | float] , lowerCamelCase_ :int | float , lowerCamelCase_ :int | float , lowerCamelCase_ :int = 1_00 , ): '''simple docstring''' snake_case_ : Tuple = x_start snake_case_ : Optional[int] = fnc(lowerCamelCase_ ) snake_case_ : Optional[int] = 0.0 for _ in range(lowerCamelCase_ ): # Approximates small segments of curve as linear and solve # for trapezoidal area snake_case_ : int = (x_end - x_start) / steps + xa snake_case_ : Union[str, Any] = fnc(lowerCamelCase_ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step snake_case_ : Any = xa snake_case_ : str = fxa return area if __name__ == "__main__": def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return x**3 + x**2 print('f(x) = x^3 + x^2') print('The area between the curve, x = -5, x = 5 and the x axis is:') __A : List[str] = 10 while i <= 100_000: print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}') i *= 10
8
0
'''simple docstring''' import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging __A : List[str] = logging.get_logger(__name__) # pylint: disable=invalid-name class __UpperCamelCase ( SCREAMING_SNAKE_CASE_ ): def __init__( self :Any ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str ,_UpperCamelCase :List[Any] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :List[str] ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Tuple ,): super().__init__() self.register_modules( vae=snake_case__ ,text_encoder=snake_case__ ,tokenizer=snake_case__ ,unet=snake_case__ ,scheduler=snake_case__ ,safety_checker=snake_case__ ,feature_extractor=snake_case__ ,) def a__ ( self :Optional[int] ,_UpperCamelCase :Tuple = "auto" ): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory snake_case_ : Optional[Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(snake_case__ ) def a__ ( self :Union[str, Any] ): self.enable_attention_slicing(snake_case__ ) @torch.no_grad() def __call__( self :int ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Optional[int] = 5_1_2 ,_UpperCamelCase :Any = 5_1_2 ,_UpperCamelCase :int = 5_0 ,_UpperCamelCase :List[Any] = 7.5 ,_UpperCamelCase :Union[str, Any] = None ,_UpperCamelCase :List[str] = 1 ,_UpperCamelCase :Dict = 0.0 ,_UpperCamelCase :List[str] = None ,_UpperCamelCase :List[str] = None ,_UpperCamelCase :Union[str, Any] = "pil" ,_UpperCamelCase :List[str] = True ,_UpperCamelCase :Tuple = None ,_UpperCamelCase :str = 1 ,_UpperCamelCase :Optional[int] = None ,**_UpperCamelCase :Optional[int] ,): if isinstance(snake_case__ ,snake_case__ ): snake_case_ : Tuple = 1 elif isinstance(snake_case__ ,snake_case__ ): snake_case_ : List[str] = len(snake_case__ ) else: raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ ,snake_case__ ) or callback_steps <= 0) ): raise ValueError( F'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' F''' {type(snake_case__ )}.''' ) # get prompt text embeddings snake_case_ : Union[str, Any] = self.tokenizer( snake_case__ ,padding="""max_length""" ,max_length=self.tokenizer.model_max_length ,return_tensors="""pt""" ,) snake_case_ : List[Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: snake_case_ : Union[str, Any] = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( """The following part of your input was truncated because CLIP can only handle sequences up to""" F''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) snake_case_ : List[str] = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: snake_case_ : Optional[Any] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method snake_case_ : Any = text_embeddings.shape snake_case_ : int = text_embeddings.repeat(1 ,snake_case__ ,1 ) snake_case_ : Union[str, Any] = text_embeddings.view(bs_embed * num_images_per_prompt ,snake_case__ ,-1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. snake_case_ : Tuple = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: snake_case_ : List[str] if negative_prompt is None: snake_case_ : Dict = [''] elif type(snake_case__ ) is not type(snake_case__ ): raise TypeError( F'''`negative_prompt` should be the same type to `prompt`, but got {type(snake_case__ )} !=''' F''' {type(snake_case__ )}.''' ) elif isinstance(snake_case__ ,snake_case__ ): snake_case_ : List[str] = [negative_prompt] elif batch_size != len(snake_case__ ): raise ValueError( F'''`negative_prompt`: {negative_prompt} has batch size {len(snake_case__ )}, but `prompt`:''' F''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches''' """ the batch size of `prompt`.""" ) else: snake_case_ : Optional[Any] = negative_prompt snake_case_ : Tuple = text_input_ids.shape[-1] snake_case_ : Optional[Any] = self.tokenizer( snake_case__ ,padding="""max_length""" ,max_length=snake_case__ ,truncation=snake_case__ ,return_tensors="""pt""" ,) snake_case_ : Optional[int] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method snake_case_ : Dict = uncond_embeddings.shape[1] snake_case_ : Any = uncond_embeddings.repeat(snake_case__ ,snake_case__ ,1 ) snake_case_ : List[Any] = uncond_embeddings.view(batch_size * num_images_per_prompt ,snake_case__ ,-1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes snake_case_ : List[str] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. snake_case_ : Tuple = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) snake_case_ : Any = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 6_4, 6_4) snake_case_ : Optional[int] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps snake_case_ : Tuple = torch.randn( snake_case__ ,generator=snake_case__ ,device="""cpu""" ,dtype=snake_case__ ).to(self.device ) snake_case_ : Optional[Any] = torch.randn(snake_case__ ,generator=snake_case__ ,device="""cpu""" ,dtype=snake_case__ ).to( self.device ) else: snake_case_ : List[str] = torch.randn( snake_case__ ,generator=snake_case__ ,device=self.device ,dtype=snake_case__ ) snake_case_ : List[str] = torch.randn(snake_case__ ,generator=snake_case__ ,device=self.device ,dtype=snake_case__ ) else: if latents_reference.shape != latents_shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) snake_case_ : str = latents_reference.to(self.device ) snake_case_ : Dict = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images snake_case_ : Dict = (latents_shape[3] - latents_shape_reference[3]) // 2 snake_case_ : Tuple = (latents_shape[2] - latents_shape_reference[2]) // 2 snake_case_ : Union[str, Any] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx snake_case_ : Tuple = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy snake_case_ : int = 0 if dx < 0 else dx snake_case_ : str = 0 if dy < 0 else dy snake_case_ : Tuple = max(-dx ,0 ) snake_case_ : str = max(-dy ,0 ) # import pdb # pdb.set_trace() snake_case_ : Union[str, Any] = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(snake_case__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand snake_case_ : Dict = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler snake_case_ : Dict = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] snake_case_ : Optional[int] = 'eta' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) snake_case_ : Optional[int] = {} if accepts_eta: snake_case_ : Optional[Any] = eta for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the latents if we are doing classifier free guidance snake_case_ : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents snake_case_ : List[str] = self.scheduler.scale_model_input(snake_case__ ,snake_case__ ) # predict the noise residual snake_case_ : Union[str, Any] = self.unet(snake_case__ ,snake_case__ ,encoder_hidden_states=snake_case__ ).sample # perform guidance if do_classifier_free_guidance: snake_case_ : List[Any] = noise_pred.chunk(2 ) snake_case_ : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 snake_case_ : List[Any] = self.scheduler.step(snake_case__ ,snake_case__ ,snake_case__ ,**snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ ,snake_case__ ,snake_case__ ) snake_case_ : Any = 1 / 0.1_82_15 * latents snake_case_ : Optional[int] = self.vae.decode(snake_case__ ).sample snake_case_ : List[Any] = (image / 2 + 0.5).clamp(0 ,1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 snake_case_ : Any = image.cpu().permute(0 ,2 ,3 ,1 ).float().numpy() if self.safety_checker is not None: snake_case_ : Union[str, Any] = self.feature_extractor(self.numpy_to_pil(snake_case__ ) ,return_tensors="""pt""" ).to( self.device ) snake_case_ : Dict = self.safety_checker( images=snake_case__ ,clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: snake_case_ : Dict = None if output_type == "pil": snake_case_ : str = self.numpy_to_pil(snake_case__ ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=snake_case__ ,nsfw_content_detected=snake_case__ )
352
'''simple docstring''' import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) __A : int = logging.getLogger() def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[Any] = argparse.ArgumentParser() parser.add_argument("""-f""" ) snake_case_ : int = parser.parse_args() return args.f def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[Any] = {} snake_case_ : Optional[Any] = os.path.join(lowerCamelCase_ , """all_results.json""" ) if os.path.exists(lowerCamelCase_ ): with open(lowerCamelCase_ , """r""" ) as f: snake_case_ : str = json.load(lowerCamelCase_ ) else: raise ValueError(F'''can\'t find {path}''' ) return results def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[str] = torch.cuda.is_available() and torch_device == """cuda""" return is_using_cuda and is_apex_available() __A : Any = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __UpperCamelCase ( lowercase__ ): @classmethod def a__ ( cls :Dict ): # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU snake_case_ : Optional[int] = tempfile.mkdtemp() snake_case_ : Any = os.path.join(cls.tmpdir ,"""default_config.yml""" ) write_basic_config(save_location=cls.configPath ) snake_case_ : List[Any] = ["""accelerate""", """launch""", """--config_file""", cls.configPath] @classmethod def a__ ( cls :int ): shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Optional[int] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.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 --seed=42 --checkpointing_steps epoch --with_tracking '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : Dict = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""glue_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking '''.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,1_0_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""clm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.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} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,4_2 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""mlm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu snake_case_ : Dict = 7 if get_gpu_count() > 1 else 2 snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : str = F''' {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.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} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertLess(result["""train_loss"""] ,0.5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""ner_no_trainer""" ) ) ) @unittest.skip(reason="""Fix me @muellerzr""" ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[str] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : Optional[int] = F''' {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.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} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result["""eval_f1"""] ,2_8 ) self.assertGreaterEqual(result["""eval_exact"""] ,2_8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""qa_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Union[str, Any] = F''' {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Union[str, Any] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""swag_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[Any] = F''' {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.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 --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : int = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_rouge1"""] ,1_0 ) self.assertGreaterEqual(result["""eval_rouge2"""] ,2 ) self.assertGreaterEqual(result["""eval_rougeL"""] ,7 ) self.assertGreaterEqual(result["""eval_rougeLsum"""] ,7 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""summarization_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : Tuple = self.get_auto_remove_tmp_dir() snake_case_ : Optional[Any] = F''' {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Any = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_bleu"""] ,3_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""translation_no_trainer""" ) ) ) @slow def a__ ( self :Optional[Any] ): snake_case_ : List[str] = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCamelCase ) snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_overall_accuracy"""] ,0.10 ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Any ): snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # The base model scores a 25% self.assertGreaterEqual(result["""eval_accuracy"""] ,0.6 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""step_1""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""image_classification_no_trainer""" ) ) )
8
0
'''simple docstring''' # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __A : int = { 'configuration_xmod': [ 'XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XmodConfig', 'XmodOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[Any] = [ 'XMOD_PRETRAINED_MODEL_ARCHIVE_LIST', 'XmodForCausalLM', 'XmodForMaskedLM', 'XmodForMultipleChoice', 'XmodForQuestionAnswering', 'XmodForSequenceClassification', 'XmodForTokenClassification', 'XmodModel', 'XmodPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys __A : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
353
'''simple docstring''' from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __A : Tuple = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): lowercase : str = ['input_values', 'padding_mask'] def __init__( self :Optional[int] ,_UpperCamelCase :int = 1 ,_UpperCamelCase :int = 2_4_0_0_0 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :float = None ,_UpperCamelCase :float = None ,**_UpperCamelCase :List[Any] ,): super().__init__(feature_size=_UpperCamelCase ,sampling_rate=_UpperCamelCase ,padding_value=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Dict = chunk_length_s snake_case_ : str = overlap @property def a__ ( self :Any ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def a__ ( self :List[str] ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 ,int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self :Optional[Any] ,_UpperCamelCase :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,_UpperCamelCase :Optional[Union[bool, str, PaddingStrategy]] = None ,_UpperCamelCase :Optional[bool] = False ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :Optional[Union[str, TensorType]] = None ,_UpperCamelCase :Optional[int] = None ,): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) if padding and truncation: raise ValueError("""Both padding and truncation were set. Make sure you only set one.""" ) elif padding is None: # by default let's pad the inputs snake_case_ : Tuple = True snake_case_ : str = bool( isinstance(_UpperCamelCase ,(list, tuple) ) and (isinstance(raw_audio[0] ,(np.ndarray, tuple, list) )) ) if is_batched: snake_case_ : Any = [np.asarray(_UpperCamelCase ,dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_UpperCamelCase ,np.ndarray ): snake_case_ : Optional[int] = np.asarray(_UpperCamelCase ,dtype=np.floataa ) elif isinstance(_UpperCamelCase ,np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): snake_case_ : List[str] = raw_audio.astype(np.floataa ) # always return batch if not is_batched: snake_case_ : Optional[Any] = [np.asarray(_UpperCamelCase ).T] # verify inputs are valid for idx, example in enumerate(_UpperCamelCase ): if example.ndim > 2: raise ValueError(F'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(F'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(F'''Expected stereo audio but example has {example.shape[-1]} channels''' ) snake_case_ : Tuple = None snake_case_ : Optional[Any] = BatchFeature({"""input_values""": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: snake_case_ : Union[str, Any] = min(array.shape[0] for array in raw_audio ) snake_case_ : Dict = int(np.floor(max_length / self.chunk_stride ) ) snake_case_ : Union[str, Any] = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: snake_case_ : Any = max(array.shape[0] for array in raw_audio ) snake_case_ : List[Any] = int(np.ceil(max_length / self.chunk_stride ) ) snake_case_ : Any = (nb_step - 1) * self.chunk_stride + self.chunk_length snake_case_ : Union[str, Any] = """max_length""" else: snake_case_ : int = input_values # normal padding on batch if padded_inputs is None: snake_case_ : Optional[int] = self.pad( _UpperCamelCase ,max_length=_UpperCamelCase ,truncation=_UpperCamelCase ,padding=_UpperCamelCase ,return_attention_mask=_UpperCamelCase ,) if padding: snake_case_ : Tuple = padded_inputs.pop("""attention_mask""" ) snake_case_ : Optional[int] = [] for example in padded_inputs.pop("""input_values""" ): if self.feature_size == 1: snake_case_ : Dict = example[..., None] input_values.append(example.T ) snake_case_ : List[Any] = input_values if return_tensors is not None: snake_case_ : Tuple = padded_inputs.convert_to_tensors(_UpperCamelCase ) return padded_inputs
8
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_deit import DeiTImageProcessor __A : List[Any] = logging.get_logger(__name__) class __UpperCamelCase ( _UpperCAmelCase ): def __init__( self :int ,*_UpperCamelCase :str ,**_UpperCamelCase :Dict ): warnings.warn( """The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use DeiTImageProcessor instead.""" ,SCREAMING_SNAKE_CASE_ ,) super().__init__(*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
354
'''simple docstring''' from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig __A : Dict = { 'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json', 'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json', } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[int] = 'ernie_m' lowercase : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self :Optional[Any] ,_UpperCamelCase :int = 2_5_0_0_0_2 ,_UpperCamelCase :int = 7_6_8 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 3_0_7_2 ,_UpperCamelCase :str = "gelu" ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :int = 5_1_4 ,_UpperCamelCase :float = 0.02 ,_UpperCamelCase :int = 1 ,_UpperCamelCase :float = 1E-0_5 ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Optional[int]=0.0 ,**_UpperCamelCase :List[Any] ,): super().__init__(pad_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Optional[int] = vocab_size snake_case_ : Any = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : Union[str, Any] = num_attention_heads snake_case_ : Any = intermediate_size snake_case_ : Any = hidden_act snake_case_ : Tuple = hidden_dropout_prob snake_case_ : Union[str, Any] = attention_probs_dropout_prob snake_case_ : str = max_position_embeddings snake_case_ : int = initializer_range snake_case_ : Optional[Any] = layer_norm_eps snake_case_ : Union[str, Any] = classifier_dropout snake_case_ : Tuple = is_decoder snake_case_ : int = act_dropout
8
0
'''simple docstring''' import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] ): '''simple docstring''' snake_case_ : Any = args.pruning_method snake_case_ : List[Any] = args.threshold snake_case_ : Optional[Any] = args.model_name_or_path.rstrip("""/""" ) snake_case_ : Any = args.target_model_path print(F'''Load fine-pruned model from {model_name_or_path}''' ) snake_case_ : Any = torch.load(os.path.join(lowercase__ , """pytorch_model.bin""" ) ) snake_case_ : int = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: snake_case_ : Dict = tensor print(F'''Copied layer {name}''' ) elif "classifier" in name or "qa_output" in name: snake_case_ : int = tensor print(F'''Copied layer {name}''' ) elif "bias" in name: snake_case_ : Any = tensor print(F'''Copied layer {name}''' ) else: if pruning_method == "magnitude": snake_case_ : Tuple = MagnitudeBinarizer.apply(inputs=lowercase__ , threshold=lowercase__ ) snake_case_ : List[Any] = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "topK": if "mask_scores" in name: continue snake_case_ : int = name[:-6] snake_case_ : Any = model[F'''{prefix_}mask_scores'''] snake_case_ : str = TopKBinarizer.apply(lowercase__ , lowercase__ ) snake_case_ : int = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue snake_case_ : Tuple = name[:-6] snake_case_ : Any = model[F'''{prefix_}mask_scores'''] snake_case_ : Optional[Any] = ThresholdBinarizer.apply(lowercase__ , lowercase__ , lowercase__ ) snake_case_ : str = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "l0": if "mask_scores" in name: continue snake_case_ : Optional[Any] = name[:-6] snake_case_ : Optional[Any] = model[F'''{prefix_}mask_scores'''] snake_case_ : Optional[Any] = -0.1, 1.1 snake_case_ : Dict = torch.sigmoid(lowercase__ ) snake_case_ : Optional[int] = s * (r - l) + l snake_case_ : str = s_bar.clamp(min=0.0 , max=1.0 ) snake_case_ : str = tensor * mask print(F'''Pruned layer {name}''' ) else: raise ValueError("""Unknown pruning method""" ) if target_model_path is None: snake_case_ : Optional[int] = os.path.join( os.path.dirname(lowercase__ ) , F'''bertarized_{os.path.basename(lowercase__ )}''' ) if not os.path.isdir(lowercase__ ): shutil.copytree(lowercase__ , lowercase__ ) print(F'''\nCreated folder {target_model_path}''' ) torch.save(lowercase__ , os.path.join(lowercase__ , """pytorch_model.bin""" ) ) print("""\nPruned model saved! See you later!""" ) if __name__ == "__main__": __A : List[str] = 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', ) __A : Dict = parser.parse_args() main(args)
355
'''simple docstring''' from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class __UpperCamelCase ( nn.Module ): def __init__( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = True ,_UpperCamelCase :str = "layer_norm" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Any = only_cross_attention snake_case_ : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" snake_case_ : Any = (num_embeds_ada_norm is not None) and norm_type == """ada_norm""" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F'''`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to''' F''' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.''' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: snake_case_ : Dict = AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ : str = AdaLayerNormZero(_UpperCamelCase ,_UpperCamelCase ) else: snake_case_ : List[Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=_UpperCamelCase ,) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. snake_case_ : str = ( AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,upcast_attention=_UpperCamelCase ,) # is self-attn if encoder_hidden_states is none else: snake_case_ : Any = None snake_case_ : Optional[Any] = None # 3. Feed-forward snake_case_ : List[str] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : Union[str, Any] = FeedForward(_UpperCamelCase ,dropout=_UpperCamelCase ,activation_fn=_UpperCamelCase ,final_dropout=_UpperCamelCase ) # let chunk size default to None snake_case_ : Optional[int] = None snake_case_ : Dict = 0 def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :int ): # Sets chunk feed-forward snake_case_ : Optional[Any] = chunk_size snake_case_ : Optional[Any] = dim def a__ ( self :List[str] ,_UpperCamelCase :torch.FloatTensor ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,_UpperCamelCase :Dict[str, Any] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = self.norma( _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=hidden_states.dtype ) else: snake_case_ : Optional[int] = self.norma(_UpperCamelCase ) snake_case_ : int = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case_ : Union[str, Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_msa.unsqueeze(1 ) * attn_output snake_case_ : Union[str, Any] = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case_ : Any = ( self.norma(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else self.norma(_UpperCamelCase ) ) snake_case_ : List[Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=_UpperCamelCase ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Tuple = attn_output + hidden_states # 3. Feed-forward snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Dict = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F'''`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.''' ) snake_case_ : Union[str, Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case_ : int = torch.cat( [self.ff(_UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase ,dim=self._chunk_dim )] ,dim=self._chunk_dim ,) else: snake_case_ : List[str] = self.ff(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case_ : Any = ff_output + hidden_states return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Dict ,_UpperCamelCase :int ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :int = 4 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Tuple = int(dim * mult ) snake_case_ : Optional[int] = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case_ : Any = GELU(_UpperCamelCase ,_UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case_ : Tuple = GELU(_UpperCamelCase ,_UpperCamelCase ,approximate="""tanh""" ) elif activation_fn == "geglu": snake_case_ : Dict = GEGLU(_UpperCamelCase ,_UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case_ : Optional[Any] = ApproximateGELU(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Dict = nn.ModuleList([] ) # project in self.net.append(_UpperCamelCase ) # project dropout self.net.append(nn.Dropout(_UpperCamelCase ) ) # project out self.net.append(nn.Linear(_UpperCamelCase ,_UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase ) ) def a__ ( self :Tuple ,_UpperCamelCase :Union[str, Any] ): for module in self.net: snake_case_ : Tuple = module(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :str = "none" ): super().__init__() snake_case_ : Union[str, Any] = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Optional[Any] = approximate def a__ ( self :str ,_UpperCamelCase :int ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ,approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ,approximate=self.approximate ).to(dtype=gate.dtype ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[Any] ): snake_case_ : Optional[Any] = self.proj(_UpperCamelCase ) snake_case_ : int = self.gelu(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :List[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : str = nn.Linear(_UpperCamelCase ,dim_out * 2 ) def a__ ( self :Dict ,_UpperCamelCase :List[str] ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def a__ ( self :Optional[Any] ,_UpperCamelCase :Optional[int] ): snake_case_ , snake_case_ : Dict = self.proj(_UpperCamelCase ).chunk(2 ,dim=-1 ) return hidden_states * self.gelu(_UpperCamelCase ) class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[int] ): snake_case_ : int = self.proj(_UpperCamelCase ) return x * torch.sigmoid(1.7_02 * x ) class __UpperCamelCase ( nn.Module ): def __init__( self :int ,_UpperCamelCase :str ,_UpperCamelCase :List[Any] ): super().__init__() snake_case_ : int = nn.Embedding(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = nn.SiLU() snake_case_ : Any = nn.Linear(_UpperCamelCase ,embedding_dim * 2 ) snake_case_ : Dict = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) def a__ ( self :int ,_UpperCamelCase :List[str] ,_UpperCamelCase :int ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ) ) ) snake_case_ , snake_case_ : Tuple = torch.chunk(_UpperCamelCase ,2 ) snake_case_ : Tuple = self.norm(_UpperCamelCase ) * (1 + scale) + shift return x class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :Tuple ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = CombinedTimestepLabelEmbeddings(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : int = nn.SiLU() snake_case_ : List[str] = nn.Linear(_UpperCamelCase ,6 * embedding_dim ,bias=_UpperCamelCase ) snake_case_ : str = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ,eps=1E-6 ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str=None ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=_UpperCamelCase ) ) ) snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Any = emb.chunk(6 ,dim=1 ) snake_case_ : str = self.norm(_UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[int] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :Optional[str] = None ,_UpperCamelCase :float = 1E-5 ): super().__init__() snake_case_ : Optional[int] = num_groups snake_case_ : List[Any] = eps if act_fn is None: snake_case_ : int = None else: snake_case_ : Dict = get_activation(_UpperCamelCase ) snake_case_ : Optional[int] = nn.Linear(_UpperCamelCase ,out_dim * 2 ) def a__ ( self :List[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :List[str] ): if self.act: snake_case_ : Any = self.act(_UpperCamelCase ) snake_case_ : Optional[int] = self.linear(_UpperCamelCase ) snake_case_ : Dict = emb[:, :, None, None] snake_case_ , snake_case_ : str = emb.chunk(2 ,dim=1 ) snake_case_ : str = F.group_norm(_UpperCamelCase ,self.num_groups ,eps=self.eps ) snake_case_ : List[str] = x * (1 + scale) + shift return x
8
0
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Any ): '''simple docstring''' return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[Any]=0 ): '''simple docstring''' return sorted(A__ , key=lambda lowerCamelCase_ : x[column] ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int]=float("""inf""" ) ): '''simple docstring''' for i in range(points_counts - 1 ): for j in range(i + 1 , A__ ): snake_case_ : Optional[Any] = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: snake_case_ : Union[str, Any] = current_dis return min_dis def UpperCAmelCase ( lowerCamelCase_ :Tuple , lowerCamelCase_ :List[Any] , lowerCamelCase_ :int=float("""inf""" ) ): '''simple docstring''' for i in range(min(6 , points_counts - 1 ) , A__ ): for j in range(max(0 , i - 6 ) , A__ ): snake_case_ : Dict = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: snake_case_ : Tuple = current_dis return min_dis def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] ): '''simple docstring''' if points_counts <= 3: return dis_between_closest_pair(A__ , A__ ) # recursion snake_case_ : List[Any] = points_counts // 2 snake_case_ : Tuple = closest_pair_of_points_sqr( A__ , points_sorted_on_y[:mid] , A__ ) snake_case_ : Tuple = closest_pair_of_points_sqr( A__ , points_sorted_on_y[mid:] , points_counts - mid ) snake_case_ : Optional[Any] = min(A__ , A__ ) snake_case_ : Any = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(A__ ) snake_case_ : List[str] = dis_between_closest_in_strip( A__ , len(A__ ) , A__ ) return min(A__ , A__ ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any ): '''simple docstring''' snake_case_ : Union[str, Any] = column_based_sort(A__ , column=0 ) snake_case_ : Optional[int] = column_based_sort(A__ , column=1 ) return ( closest_pair_of_points_sqr( A__ , A__ , A__ ) ) ** 0.5 if __name__ == "__main__": __A : int = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print('Distance:', closest_pair_of_points(points, len(points)))
356
'''simple docstring''' import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :str=True , lowerCamelCase_ :str="pt" ): '''simple docstring''' snake_case_ : Tuple = {"""add_prefix_space""": True} if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and not line.startswith(""" """ ) else {} snake_case_ : Union[str, Any] = padding_side return tokenizer( [line] , max_length=lowerCamelCase_ , padding="""max_length""" if pad_to_max_length else None , truncation=lowerCamelCase_ , return_tensors=lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , **lowerCamelCase_ , ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Any=None , ): '''simple docstring''' snake_case_ : Dict = input_ids.ne(lowerCamelCase_ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[Any] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any="train" ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :int=None ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :Optional[int]="" ,): super().__init__() snake_case_ : List[str] = Path(_UpperCamelCase ).joinpath(type_path + """.source""" ) snake_case_ : int = Path(_UpperCamelCase ).joinpath(type_path + """.target""" ) snake_case_ : Optional[int] = self.get_char_lens(self.src_file ) snake_case_ : List[str] = max_source_length snake_case_ : str = max_target_length assert min(self.src_lens ) > 0, F'''found empty line in {self.src_file}''' snake_case_ : str = tokenizer snake_case_ : str = prefix if n_obs is not None: snake_case_ : int = self.src_lens[:n_obs] snake_case_ : Tuple = src_lang snake_case_ : str = tgt_lang def __len__( self :Any ): return len(self.src_lens ) def __getitem__( self :List[str] ,_UpperCamelCase :Union[str, Any] ): snake_case_ : Optional[int] = index + 1 # linecache starts at 1 snake_case_ : Dict = self.prefix + linecache.getline(str(self.src_file ) ,_UpperCamelCase ).rstrip("""\n""" ) snake_case_ : List[Any] = linecache.getline(str(self.tgt_file ) ,_UpperCamelCase ).rstrip("""\n""" ) assert source_line, F'''empty source line for index {index}''' assert tgt_line, F'''empty tgt line for index {index}''' # Need to add eos token manually for T5 if isinstance(self.tokenizer ,_UpperCamelCase ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right snake_case_ : int = ( self.tokenizer.question_encoder if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer ) snake_case_ : Optional[int] = self.tokenizer.generator if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer snake_case_ : Optional[Any] = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_source_length ,"""right""" ) snake_case_ : Tuple = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_target_length ,"""right""" ) snake_case_ : int = source_inputs["""input_ids"""].squeeze() snake_case_ : str = target_inputs["""input_ids"""].squeeze() snake_case_ : Union[str, Any] = source_inputs["""attention_mask"""].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def a__ ( _UpperCamelCase :str ): return [len(_UpperCamelCase ) for x in Path(_UpperCamelCase ).open().readlines()] def a__ ( self :Optional[int] ,_UpperCamelCase :List[str] ): snake_case_ : Optional[Any] = torch.stack([x["""input_ids"""] for x in batch] ) snake_case_ : List[Any] = torch.stack([x["""attention_mask"""] for x in batch] ) snake_case_ : Union[str, Any] = torch.stack([x["""decoder_input_ids"""] for x in batch] ) snake_case_ : Optional[Any] = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Tuple = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Optional[int] = trim_batch(_UpperCamelCase ,_UpperCamelCase ) snake_case_ , snake_case_ : Dict = trim_batch(_UpperCamelCase ,_UpperCamelCase ,attention_mask=_UpperCamelCase ) snake_case_ : Optional[int] = { """input_ids""": source_ids, """attention_mask""": source_mask, """decoder_input_ids""": y, } return batch __A : List[Any] = getLogger(__name__) def UpperCAmelCase ( lowerCamelCase_ :List[List] ): '''simple docstring''' return list(itertools.chain.from_iterable(lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : int = get_git_info() save_json(lowerCamelCase_ , os.path.join(lowerCamelCase_ , """git_log.json""" ) ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int]=4 , **lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' with open(lowerCamelCase_ , """w""" ) as f: json.dump(lowerCamelCase_ , lowerCamelCase_ , indent=lowerCamelCase_ , **lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] ): '''simple docstring''' with open(lowerCamelCase_ ) as f: return json.load(lowerCamelCase_ ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Optional[Any] = git.Repo(search_parent_directories=lowerCamelCase_ ) snake_case_ : List[str] = { """repo_id""": str(lowerCamelCase_ ), """repo_sha""": str(repo.head.object.hexsha ), """repo_branch""": str(repo.active_branch ), """hostname""": str(socket.gethostname() ), } return repo_infos def UpperCAmelCase ( lowerCamelCase_ :Callable , lowerCamelCase_ :Iterable ): '''simple docstring''' return list(map(lowerCamelCase_ , lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , """wb""" ) as f: return pickle.dump(lowerCamelCase_ , lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' def remove_articles(lowerCamelCase_ :str ): return re.sub(R"""\b(a|an|the)\b""" , """ """ , lowerCamelCase_ ) def white_space_fix(lowerCamelCase_ :Optional[Any] ): return " ".join(text.split() ) def remove_punc(lowerCamelCase_ :Tuple ): snake_case_ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowerCamelCase_ :Optional[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase_ ) ) ) ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' snake_case_ : List[Any] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : Optional[int] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : List[Any] = Counter(lowerCamelCase_ ) & Counter(lowerCamelCase_ ) snake_case_ : Optional[Any] = sum(common.values() ) if num_same == 0: return 0 snake_case_ : Optional[Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Union[str, Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Optional[Any] = (2 * precision * recall) / (precision + recall) return fa def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' return normalize_answer(lowerCamelCase_ ) == normalize_answer(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] ): '''simple docstring''' assert len(lowerCamelCase_ ) == len(lowerCamelCase_ ) snake_case_ : Optional[int] = 0 for hypo, pred in zip(lowerCamelCase_ , lowerCamelCase_ ): em += exact_match_score(lowerCamelCase_ , lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: em /= len(lowerCamelCase_ ) return {"em": em} def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return model_prefix.startswith("""rag""" ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead snake_case_ : Optional[int] = """dropout_rate""" for p in extra_params: if getattr(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): if not hasattr(lowerCamelCase_ , lowerCamelCase_ ) and not hasattr(lowerCamelCase_ , equivalent_param[p] ): logger.info("""config doesn't have a `{}` attribute""".format(lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) continue snake_case_ : str = p if hasattr(lowerCamelCase_ , lowerCamelCase_ ) else equivalent_param[p] setattr(lowerCamelCase_ , lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) return hparams, config
8
0
'''simple docstring''' import random import unittest from torch.utils.data import BatchSampler, DataLoader, IterableDataset from accelerate import Accelerator from accelerate.data_loader import ( BatchSamplerShard, DataLoaderDispatcher, DataLoaderShard, IterableDatasetShard, SkipBatchSampler, SkipDataLoader, skip_first_batches, ) class __UpperCamelCase ( lowerCAmelCase_ ): def __init__( self :Tuple ,_UpperCamelCase :Tuple=0.01 ,_UpperCamelCase :Dict=1_0_0_0 ): snake_case_ : Union[str, Any] = p_stop snake_case_ : Tuple = max_length def __iter__( self :int ): snake_case_ : Optional[Any] = 0 snake_case_ : str = False while not stop and count < self.max_length: yield count count += 1 snake_case_ : List[str] = random.random() < self.p_stop class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :Dict ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str=False ,_UpperCamelCase :Tuple=True ): snake_case_ : Optional[Any] = [ BatchSamplerShard(__lowerCAmelCase ,2 ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) for i in range(2 ) ] snake_case_ : List[Any] = [list(__lowerCAmelCase ) for batch_sampler_shard in batch_sampler_shards] if not split_batches: self.assertListEqual([len(__lowerCAmelCase ) for shard in batch_sampler_shards] ,[len(__lowerCAmelCase ) for e in expected] ) self.assertListEqual(__lowerCAmelCase ,__lowerCAmelCase ) def a__ ( self :Any ): # Check the shards when the dataset is a round multiple of total batch size. snake_case_ : List[Any] = BatchSampler(range(2_4 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : str = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4], [1_8, 1_9, 2_0]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7], [2_1, 2_2, 2_3]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) snake_case_ : str = BatchSampler(range(2_4 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. snake_case_ : int = BatchSampler(range(2_1 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : List[Any] = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4], [1_8, 1_9, 2_0]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7], [0, 1, 2]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) snake_case_ : int = BatchSampler(range(2_1 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : Optional[int] = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. snake_case_ : List[str] = BatchSampler(range(2_2 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : Union[str, Any] = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4], [1_8, 1_9, 2_0]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7], [2_1, 0, 1]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) snake_case_ : Any = BatchSampler(range(2_2 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : int = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. snake_case_ : Optional[int] = BatchSampler(range(2_0 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : Any = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4], [1_8, 1_9, 0]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7], [1, 2, 3]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) snake_case_ : int = BatchSampler(range(2_0 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : Dict = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) # Check the shards when the dataset is very small. snake_case_ : str = BatchSampler(range(2 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : List[str] = [[[0, 1, 0]], [[1, 0, 1]]] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) snake_case_ : Optional[Any] = BatchSampler(range(2 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : Union[str, Any] = [[], []] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ) def a__ ( self :Optional[Any] ): # Check the shards when the dataset is a round multiple of batch size. snake_case_ : Optional[int] = BatchSampler(range(2_4 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Optional[int] = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7], [2_0, 2_1]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9], [2_2, 2_3]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ) snake_case_ : str = BatchSampler(range(2_4 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size. snake_case_ : str = BatchSampler(range(2_2 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Union[str, Any] = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7], [2_0, 2_1]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9], [0, 1]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ) snake_case_ : Tuple = BatchSampler(range(2_2 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : str = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. snake_case_ : Optional[Any] = BatchSampler(range(2_1 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Optional[int] = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7], [2_0, 0]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9], [1, 2]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ) snake_case_ : Optional[Any] = BatchSampler(range(2_1 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : List[str] = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ) # Check the shards when the dataset is very small. snake_case_ : List[Any] = BatchSampler(range(2 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : List[Any] = [[[0, 1]], [[0, 1]]] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ) snake_case_ : Optional[int] = BatchSampler(range(2 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Optional[Any] = [[], []] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ) def a__ ( self :Union[str, Any] ): # Check the shards when the dataset is a round multiple of total batch size. snake_case_ : str = BatchSampler(range(2_4 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : List[str] = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4], [1_8, 1_9, 2_0]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7], [2_1, 2_2, 2_3]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : int = BatchSampler(range(2_4 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. snake_case_ : Any = BatchSampler(range(2_1 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : Optional[Any] = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4], [1_8, 1_9, 2_0]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : Tuple = BatchSampler(range(2_1 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : Union[str, Any] = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. snake_case_ : List[str] = BatchSampler(range(2_2 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : List[Any] = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4], [1_8, 1_9, 2_0]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7], [2_1]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : Optional[Any] = BatchSampler(range(2_2 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : Union[str, Any] = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. snake_case_ : str = BatchSampler(range(2_0 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : str = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4], [1_8, 1_9]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : Union[str, Any] = BatchSampler(range(2_0 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : str = [ [[0, 1, 2], [6, 7, 8], [1_2, 1_3, 1_4]], [[3, 4, 5], [9, 1_0, 1_1], [1_5, 1_6, 1_7]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) # Check the shards when the dataset is very small. snake_case_ : Any = BatchSampler(range(2 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : str = [[[0, 1]], []] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : int = BatchSampler(range(2 ) ,batch_size=3 ,drop_last=__lowerCAmelCase ) snake_case_ : List[str] = [[], []] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) def a__ ( self :List[Any] ): # Check the shards when the dataset is a round multiple of batch size. snake_case_ : Tuple = BatchSampler(range(2_4 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Optional[int] = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7], [2_0, 2_1]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9], [2_2, 2_3]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : Any = BatchSampler(range(2_4 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size. snake_case_ : List[Any] = BatchSampler(range(2_2 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Optional[int] = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7], [2_0, 2_1]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : Union[str, Any] = BatchSampler(range(2_2 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : List[Any] = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. snake_case_ : Tuple = BatchSampler(range(2_1 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : str = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7], [2_0]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : Any = BatchSampler(range(2_1 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Tuple = [ [[0, 1], [4, 5], [8, 9], [1_2, 1_3], [1_6, 1_7]], [[2, 3], [6, 7], [1_0, 1_1], [1_4, 1_5], [1_8, 1_9]], ] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) # Check the shards when the dataset is very small. snake_case_ : List[Any] = BatchSampler(range(2 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Optional[int] = [[[0, 1]], []] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) snake_case_ : Optional[Any] = BatchSampler(range(2 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Dict = [[], []] self.check_batch_sampler_shards(__lowerCAmelCase ,__lowerCAmelCase ,split_batches=__lowerCAmelCase ,even_batches=__lowerCAmelCase ) def a__ ( self :Tuple ): snake_case_ : List[Any] = [[0, 1, 2], [3, 4], [5, 6, 7, 8], [9, 1_0, 1_1], [1_2, 1_3]] snake_case_ : List[Any] = [BatchSamplerShard(__lowerCAmelCase ,2 ,__lowerCAmelCase ,even_batches=__lowerCAmelCase ) for i in range(2 )] self.assertEqual(len(batch_sampler_shards[0] ) ,3 ) self.assertEqual(len(batch_sampler_shards[1] ) ,2 ) self.assertListEqual(list(batch_sampler_shards[0] ) ,[[0, 1, 2], [5, 6, 7, 8], [1_2, 1_3]] ) self.assertListEqual(list(batch_sampler_shards[1] ) ,[[3, 4], [9, 1_0, 1_1]] ) def a__ ( self :List[Any] ,_UpperCamelCase :List[str] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Dict=False ,_UpperCamelCase :int=2 ,_UpperCamelCase :Tuple=False ): random.seed(__lowerCAmelCase ) snake_case_ : Dict = list(__lowerCAmelCase ) snake_case_ : int = [ IterableDatasetShard( __lowerCAmelCase ,batch_size=__lowerCAmelCase ,drop_last=__lowerCAmelCase ,num_processes=__lowerCAmelCase ,process_index=__lowerCAmelCase ,split_batches=__lowerCAmelCase ,) for i in range(__lowerCAmelCase ) ] snake_case_ : Optional[int] = [] for iterable_dataset_shard in iterable_dataset_shards: # Since our random iterable dataset will be... random... we need to use a seed to get reproducible results. random.seed(__lowerCAmelCase ) iterable_dataset_lists.append(list(__lowerCAmelCase ) ) snake_case_ : int = batch_size // num_processes if split_batches else batch_size # All iterable dataset shard should have the same length, a round multiple of shard_batch_size snake_case_ : Any = iterable_dataset_lists[0] for l in iterable_dataset_lists[1:]: self.assertEqual(len(__lowerCAmelCase ) ,len(__lowerCAmelCase ) ) self.assertTrue(len(__lowerCAmelCase ) % shard_batch_size == 0 ) snake_case_ : Optional[Any] = [] for idx in range(0 ,len(__lowerCAmelCase ) ,__lowerCAmelCase ): for l in iterable_dataset_lists: observed += l[idx : idx + shard_batch_size] if not drop_last: while len(__lowerCAmelCase ) < len(__lowerCAmelCase ): reference += reference self.assertListEqual(__lowerCAmelCase ,reference[: len(__lowerCAmelCase )] ) def a__ ( self :List[str] ): snake_case_ : List[Any] = 4_2 snake_case_ : int = RandomIterableDataset() self.check_iterable_dataset_shards(__lowerCAmelCase ,__lowerCAmelCase ,batch_size=4 ,drop_last=__lowerCAmelCase ,split_batches=__lowerCAmelCase ) self.check_iterable_dataset_shards(__lowerCAmelCase ,__lowerCAmelCase ,batch_size=4 ,drop_last=__lowerCAmelCase ,split_batches=__lowerCAmelCase ) self.check_iterable_dataset_shards(__lowerCAmelCase ,__lowerCAmelCase ,batch_size=4 ,drop_last=__lowerCAmelCase ,split_batches=__lowerCAmelCase ) self.check_iterable_dataset_shards(__lowerCAmelCase ,__lowerCAmelCase ,batch_size=4 ,drop_last=__lowerCAmelCase ,split_batches=__lowerCAmelCase ) # Edge case with a very small dataset snake_case_ : List[Any] = RandomIterableDataset(max_length=2 ) self.check_iterable_dataset_shards(__lowerCAmelCase ,__lowerCAmelCase ,batch_size=4 ,drop_last=__lowerCAmelCase ,split_batches=__lowerCAmelCase ) self.check_iterable_dataset_shards(__lowerCAmelCase ,__lowerCAmelCase ,batch_size=4 ,drop_last=__lowerCAmelCase ,split_batches=__lowerCAmelCase ) self.check_iterable_dataset_shards(__lowerCAmelCase ,__lowerCAmelCase ,batch_size=4 ,drop_last=__lowerCAmelCase ,split_batches=__lowerCAmelCase ) self.check_iterable_dataset_shards(__lowerCAmelCase ,__lowerCAmelCase ,batch_size=4 ,drop_last=__lowerCAmelCase ,split_batches=__lowerCAmelCase ) def a__ ( self :Union[str, Any] ): snake_case_ : List[Any] = BatchSampler(range(1_6 ) ,batch_size=4 ,drop_last=__lowerCAmelCase ) snake_case_ : Dict = SkipBatchSampler(__lowerCAmelCase ,2 ) self.assertListEqual(list(__lowerCAmelCase ) ,[[8, 9, 1_0, 1_1], [1_2, 1_3, 1_4, 1_5]] ) def a__ ( self :List[str] ): snake_case_ : Optional[Any] = SkipDataLoader(list(range(1_6 ) ) ,batch_size=4 ,skip_batches=2 ) self.assertListEqual([t.tolist() for t in dataloader] ,[[8, 9, 1_0, 1_1], [1_2, 1_3, 1_4, 1_5]] ) def a__ ( self :List[str] ): snake_case_ : Union[str, Any] = DataLoader(list(range(1_6 ) ) ,batch_size=4 ) snake_case_ : List[str] = skip_first_batches(__lowerCAmelCase ,num_batches=2 ) self.assertListEqual([t.tolist() for t in new_dataloader] ,[[8, 9, 1_0, 1_1], [1_2, 1_3, 1_4, 1_5]] ) def a__ ( self :List[Any] ): snake_case_ : int = DataLoaderShard(list(range(1_6 ) ) ,batch_size=4 ) for idx, _ in enumerate(__lowerCAmelCase ): self.assertEqual(dataloader.end_of_dataloader ,idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(__lowerCAmelCase ): self.assertEqual(dataloader.end_of_dataloader ,idx == 3 ) def a__ ( self :Optional[int] ): Accelerator() snake_case_ : Dict = DataLoaderDispatcher(range(1_6 ) ,batch_size=4 ) for idx, _ in enumerate(__lowerCAmelCase ): self.assertEqual(dataloader.end_of_dataloader ,idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(__lowerCAmelCase ): self.assertEqual(dataloader.end_of_dataloader ,idx == 3 )
357
'''simple docstring''' import functools def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[str] = len(lowerCamelCase_ ) snake_case_ : Dict = len(lowerCamelCase_ ) @functools.cache def min_distance(lowerCamelCase_ :int , lowerCamelCase_ :int ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa snake_case_ : Union[str, Any] = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , lowerCamelCase_ ) , 1 + min_distance(lowerCamelCase_ , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
8
0
'''simple docstring''' from __future__ import annotations class __UpperCamelCase : def __init__( self :Dict ,_UpperCamelCase :list[list[int]] ): snake_case_ : Optional[int] = TypeError( """Matrices must be formed from a list of zero or more lists containing at """ """least one and the same number of values, each of which must be of type """ """int or float.""" ) if len(a_ ) != 0: snake_case_ : Any = len(rows[0] ) if cols == 0: raise error for row in rows: if len(a_ ) != cols: raise error for value in row: if not isinstance(a_ ,(int, float) ): raise error snake_case_ : Optional[Any] = rows else: snake_case_ : Tuple = [] def a__ ( self :List[Any] ): return [[row[i] for row in self.rows] for i in range(len(self.rows[0] ) )] @property def a__ ( self :Any ): return len(self.rows ) @property def a__ ( self :List[str] ): return len(self.rows[0] ) @property def a__ ( self :List[str] ): return (self.num_rows, self.num_columns) @property def a__ ( self :str ): return self.order[0] == self.order[1] def a__ ( self :Tuple ): snake_case_ : Union[str, Any] = [ [0 if column_num != row_num else 1 for column_num in range(self.num_rows )] for row_num in range(self.num_rows ) ] return Matrix(a_ ) def a__ ( self :List[str] ): if not self.is_square: return 0 if self.order == (0, 0): return 1 if self.order == (1, 1): return int(self.rows[0][0] ) if self.order == (2, 2): return int( (self.rows[0][0] * self.rows[1][1]) - (self.rows[0][1] * self.rows[1][0]) ) else: return sum( self.rows[0][column] * self.cofactors().rows[0][column] for column in range(self.num_columns ) ) def a__ ( self :List[Any] ): return bool(self.determinant() ) def a__ ( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :int ): snake_case_ : List[str] = [ [ self.rows[other_row][other_column] for other_column in range(self.num_columns ) if other_column != column ] for other_row in range(self.num_rows ) if other_row != row ] return Matrix(a_ ).determinant() def a__ ( self :int ,_UpperCamelCase :int ,_UpperCamelCase :int ): if (row + column) % 2 == 0: return self.get_minor(a_ ,a_ ) return -1 * self.get_minor(a_ ,a_ ) def a__ ( self :Dict ): return Matrix( [ [self.get_minor(a_ ,a_ ) for column in range(self.num_columns )] for row in range(self.num_rows ) ] ) def a__ ( self :Tuple ): return Matrix( [ [ self.minors().rows[row][column] if (row + column) % 2 == 0 else self.minors().rows[row][column] * -1 for column in range(self.minors().num_columns ) ] for row in range(self.minors().num_rows ) ] ) def a__ ( self :Union[str, Any] ): snake_case_ : str = [ [self.cofactors().rows[column][row] for column in range(self.num_columns )] for row in range(self.num_rows ) ] return Matrix(a_ ) def a__ ( self :Union[str, Any] ): snake_case_ : Tuple = self.determinant() if not determinant: raise TypeError("""Only matrices with a non-zero determinant have an inverse""" ) return self.adjugate() * (1 / determinant) def __repr__( self :List[str] ): return str(self.rows ) def __str__( self :int ): if self.num_rows == 0: return "[]" if self.num_rows == 1: return "[[" + ". ".join(str(self.rows[0] ) ) + "]]" return ( "[" + "\n ".join( [ """[""" + """. """.join([str(a_ ) for value in row] ) + """.]""" for row in self.rows ] ) + "]" ) def a__ ( self :int ,_UpperCamelCase :list[int] ,_UpperCamelCase :int | None = None ): snake_case_ : Union[str, Any] = TypeError("""Row must be a list containing all ints and/or floats""" ) if not isinstance(a_ ,a_ ): raise type_error for value in row: if not isinstance(a_ ,(int, float) ): raise type_error if len(a_ ) != self.num_columns: raise ValueError( """Row must be equal in length to the other rows in the matrix""" ) if position is None: self.rows.append(a_ ) else: snake_case_ : str = self.rows[0:position] + [row] + self.rows[position:] def a__ ( self :str ,_UpperCamelCase :list[int] ,_UpperCamelCase :int | None = None ): snake_case_ : Union[str, Any] = TypeError( """Column must be a list containing all ints and/or floats""" ) if not isinstance(a_ ,a_ ): raise type_error for value in column: if not isinstance(a_ ,(int, float) ): raise type_error if len(a_ ) != self.num_rows: raise ValueError( """Column must be equal in length to the other columns in the matrix""" ) if position is None: snake_case_ : int = [self.rows[i] + [column[i]] for i in range(self.num_rows )] else: snake_case_ : str = [ self.rows[i][0:position] + [column[i]] + self.rows[i][position:] for i in range(self.num_rows ) ] def __eq__( self :int ,_UpperCamelCase :object ): if not isinstance(a_ ,a_ ): return NotImplemented return self.rows == other.rows def __ne__( self :int ,_UpperCamelCase :object ): return not self == other def __neg__( self :int ): return self * -1 def __add__( self :Optional[Any] ,_UpperCamelCase :Matrix ): if self.order != other.order: raise ValueError("""Addition requires matrices of the same order""" ) return Matrix( [ [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns )] for i in range(self.num_rows ) ] ) def __sub__( self :Any ,_UpperCamelCase :Matrix ): if self.order != other.order: raise ValueError("""Subtraction requires matrices of the same order""" ) return Matrix( [ [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns )] for i in range(self.num_rows ) ] ) def __mul__( self :Tuple ,_UpperCamelCase :Matrix | int | float ): if isinstance(a_ ,(int, float) ): return Matrix( [[int(element * other ) for element in row] for row in self.rows] ) elif isinstance(a_ ,a_ ): if self.num_columns != other.num_rows: raise ValueError( """The number of columns in the first matrix must """ """be equal to the number of rows in the second""" ) return Matrix( [ [Matrix.dot_product(a_ ,a_ ) for column in other.columns()] for row in self.rows ] ) else: raise TypeError( """A Matrix can only be multiplied by an int, float, or another matrix""" ) def __pow__( self :int ,_UpperCamelCase :int ): if not isinstance(a_ ,a_ ): raise TypeError("""A Matrix can only be raised to the power of an int""" ) if not self.is_square: raise ValueError("""Only square matrices can be raised to a power""" ) if other == 0: return self.identity() if other < 0: if self.is_invertable(): return self.inverse() ** (-other) raise ValueError( """Only invertable matrices can be raised to a negative power""" ) snake_case_ : Optional[Any] = self for _ in range(other - 1 ): result *= self return result @classmethod def a__ ( cls :str ,_UpperCamelCase :list[int] ,_UpperCamelCase :list[int] ): return sum(row[i] * column[i] for i in range(len(a_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
358
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Any = tmp_path / """file.csv""" snake_case_ : Any = textwrap.dedent( """\ header1,header2 1,2 10,20 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[int] = tmp_path / """malformed_file.csv""" snake_case_ : int = textwrap.dedent( """\ header1,header2 1,2 10,20, """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : str = tmp_path / """csv_with_image.csv""" snake_case_ : int = textwrap.dedent( F'''\ image {image_file} ''' ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' snake_case_ : int = tmp_path / """csv_with_label.csv""" snake_case_ : Tuple = textwrap.dedent( """\ label good bad good """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = tmp_path / """csv_with_int_list.csv""" snake_case_ : str = textwrap.dedent( """\ int_list 1 2 3 4 5 6 7 8 9 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : int = Csv() snake_case_ : Optional[Any] = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(lowerCamelCase_ , match="""Error tokenizing data""" ): for _ in generator: pass assert any( record.levelname == """ERROR""" and """Failed to read file""" in record.message and os.path.basename(lowerCamelCase_ ) in record.message for record in caplog.records ) @require_pil def UpperCAmelCase ( lowerCamelCase_ :Tuple ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : Tuple = f.read().splitlines()[1] snake_case_ : str = Csv(encoding="""utf-8""" , features=Features({"""image""": Image()} ) ) snake_case_ : Tuple = csv._generate_tables([[csv_file_with_image]] ) snake_case_ : Optional[Any] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""image""" ).type == Image()() snake_case_ : List[str] = pa_table.to_pydict()["""image"""] assert generated_content == [{"path": image_file, "bytes": None}] def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : List[Any] = f.read().splitlines()[1:] snake_case_ : Union[str, Any] = Csv(encoding="""utf-8""" , features=Features({"""label""": ClassLabel(names=["""good""", """bad"""] )} ) ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_label]] ) snake_case_ : Optional[int] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""label""" ).type == ClassLabel(names=["""good""", """bad"""] )() snake_case_ : Union[str, Any] = pa_table.to_pydict()["""label"""] assert generated_content == [ClassLabel(names=["""good""", """bad"""] ).straint(lowerCamelCase_ ) for label in labels] def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : str = Csv(encoding="""utf-8""" , sep=""",""" , converters={"""int_list""": lambda lowerCamelCase_ : [int(lowerCamelCase_ ) for i in x.split()]} ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_int_list]] ) snake_case_ : Tuple = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field("""int_list""" ).type ) snake_case_ : Dict = pa_table.to_pydict()["""int_list"""] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
8
0
import datasets from .evaluate import evaluate __A : Union[str, Any] = """\ @article{hendrycks2021cuad, title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review}, author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball}, journal={arXiv preprint arXiv:2103.06268}, year={2021} } """ __A : List[str] = """ This metric wrap the official scoring script for version 1 of the Contract Understanding Atticus Dataset (CUAD). Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510 commercial legal contracts that have been manually labeled to identify 41 categories of important clauses that lawyers look for when reviewing contracts in connection with corporate transactions. """ __A : Optional[int] = """ Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall). Args: predictions: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair as given in the references (see below) - 'prediction_text': list of possible texts for the answer, as a list of strings depending on a threshold on the confidence probability of each prediction. references: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair (see above), - 'answers': a Dict in the CUAD dataset format { 'text': list of possible texts for the answer, as a list of strings 'answer_start': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: 'exact_match': Exact match (the normalized answer exactly match the gold answer) 'f1': The F-score of predicted tokens versus the gold answer 'aupr': Area Under the Precision-Recall curve 'prec_at_80_recall': Precision at 80% recall 'prec_at_90_recall': Precision at 90% recall Examples: >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> cuad_metric = datasets.load_metric(\"cuad\") >>> results = cuad_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def a__ ( self :Tuple ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": { """id""": datasets.Value("""string""" ), """prediction_text""": datasets.features.Sequence(datasets.Value("""string""" ) ), }, """references""": { """id""": datasets.Value("""string""" ), """answers""": datasets.features.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), }, } ) ,codebase_urls=["""https://www.atticusprojectai.org/cuad"""] ,reference_urls=["""https://www.atticusprojectai.org/cuad"""] ,) def a__ ( self :List[str] ,_UpperCamelCase :str ,_UpperCamelCase :List[str] ): snake_case_ : Tuple = {prediction["""id"""]: prediction["""prediction_text"""] for prediction in predictions} snake_case_ : Optional[Any] = [ { """paragraphs""": [ { """qas""": [ { """answers""": [{"""text""": answer_text} for answer_text in ref["""answers"""]["""text"""]], """id""": ref["""id"""], } for ref in references ] } ] } ] snake_case_ : str = evaluate(dataset=__lowerCAmelCase ,predictions=__lowerCAmelCase ) return score
359
'''simple docstring''' import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple=None ): '''simple docstring''' # set parameter of one layer assert torch_layer.weight.shape == weight.shape, F'''{torch_layer} layer.weight does not match''' snake_case_ : Optional[Any] = nn.Parameter(lowerCamelCase_ ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F'''{torch_layer} layer.bias does not match''' snake_case_ : List[str] = nn.Parameter(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : Optional[Any] = np.asarray(weights[0] ) snake_case_ : int = np.asarray(weights[1] ) snake_case_ : Any = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : List[Any] = np.asarray(weights[0] ) snake_case_ : Optional[int] = np.asarray(weights[1] ) snake_case_ : Union[str, Any] = np.asarray(weights[2] ) snake_case_ : int = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' # layernorm 1 snake_case_ : str = weights[0][0][0] snake_case_ : int = np.asarray(layer_norm_a[0] ) snake_case_ : Optional[Any] = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # lsh weights + output snake_case_ : Tuple = weights[0][1] if len(lowerCamelCase_ ) < 4: set_layer_weights_in_torch_lsh(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) else: set_layer_weights_in_torch_local(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) # intermediate weighs snake_case_ : str = weights[2][0][1][2] # Chunked Feed Forward if len(lowerCamelCase_ ) == 4: snake_case_ : List[Any] = intermediate_weights[2] # layernorm 2 snake_case_ : Tuple = np.asarray(intermediate_weights[0][0] ) snake_case_ : Optional[Any] = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # intermediate dense snake_case_ : Any = np.asarray(intermediate_weights[1][0] ) snake_case_ : List[Any] = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) # intermediate out snake_case_ : List[Any] = np.asarray(intermediate_weights[4][0] ) snake_case_ : Union[str, Any] = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Any ): '''simple docstring''' # reformer model snake_case_ : Dict = torch_model.reformer # word embeds snake_case_ : List[Any] = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(lowerCamelCase_ ) , ) if isinstance(weights[3] , lowerCamelCase_ ): snake_case_ : Tuple = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): snake_case_ : Dict = np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), F'''{position_embeddings[emb_idx]} emb does not match''' snake_case_ : Optional[Any] = nn.Parameter(torch.tensor(lowerCamelCase_ ) ) snake_case_ : List[Any] = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( lowerCamelCase_ ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): snake_case_ : str = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # output layer norm snake_case_ : Optional[Any] = np.asarray(weights[7][0] ) snake_case_ : List[Any] = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # output embeddings snake_case_ : Optional[int] = np.asarray(weights[9][0] ) snake_case_ : Any = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # Initialise PyTorch model snake_case_ : List[str] = ReformerConfig.from_json_file(lowerCamelCase_ ) print(F'''Building PyTorch model from configuration: {config}''' ) snake_case_ : str = ReformerModelWithLMHead(lowerCamelCase_ ) with open(lowerCamelCase_ , """rb""" ) as f: snake_case_ : List[Any] = pickle.load(lowerCamelCase_ )["""weights"""] set_model_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , config.hidden_size ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , lowerCamelCase_ ) if __name__ == "__main__": __A : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--trax_model_pkl_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained Reformer model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __A : List[Any] = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
8
0
'''simple docstring''' import collections import importlib.util import os import re from pathlib import Path __A : Dict = 'src/transformers' # Matches is_xxx_available() __A : int = re.compile(r'is\_([a-z_]*)_available()') # Catches a one-line _import_struct = {xxx} __A : Optional[int] = re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __A : Union[str, Any] = re.compile(r'\s+"\S*":\s+\[([^\]]*)\]') # Catches a line if not is_foo_available __A : Dict = re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)') # Catches a line _import_struct["bla"].append("foo") __A : int = re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __A : Optional[int] = re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]') # Catches a line with an object between quotes and a comma: "MyModel", __A : Any = re.compile('^\s+"([^"]+)",') # Catches a line with objects between brackets only: ["foo", "bar"], __A : Optional[int] = re.compile('^\s+\[([^\]]+)\]') # Catches a line with from foo import bar, bla, boo __A : Any = re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') # Catches a line with try: __A : Optional[Any] = re.compile(r'^\s*try:') # Catches a line with else: __A : Union[str, Any] = re.compile(r'^\s*else:') def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' if _re_test_backend.search(a__ ) is None: return None snake_case_ : Tuple = [b[0] for b in _re_backend.findall(a__ )] backends.sort() return "_and_".join(a__ ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] ): '''simple docstring''' with open(a__ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: snake_case_ : List[str] = f.readlines() snake_case_ : Optional[Any] = 0 while line_index < len(a__ ) and not lines[line_index].startswith("""_import_structure = {""" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(a__ ): return None # First grab the objects without a specific backend in _import_structure snake_case_ : str = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: snake_case_ : Union[str, Any] = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(a__ ): snake_case_ : Dict = _re_one_line_import_struct.search(a__ ).groups()[0] snake_case_ : str = re.findall("""\[([^\]]+)\]""" , a__ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue snake_case_ : Optional[int] = _re_import_struct_key_value.search(a__ ) if single_line_import_search is not None: snake_case_ : Tuple = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(""", """ ) if len(a__ ) > 0] objects.extend(a__ ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) line_index += 1 snake_case_ : Any = {"""none""": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("""if TYPE_CHECKING""" ): # If the line is an if not is_backend_available, we grab all objects associated. snake_case_ : Optional[Any] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : List[str] = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Dict = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): snake_case_ : Union[str, Any] = lines[line_index] if _re_import_struct_add_one.search(a__ ) is not None: objects.append(_re_import_struct_add_one.search(a__ ).groups()[0] ) elif _re_import_struct_add_many.search(a__ ) is not None: snake_case_ : Any = _re_import_struct_add_many.search(a__ ).groups()[0].split(""", """ ) snake_case_ : Any = [obj[1:-1] for obj in imports if len(a__ ) > 0] objects.extend(a__ ) elif _re_between_brackets.search(a__ ) is not None: snake_case_ : List[Any] = _re_between_brackets.search(a__ ).groups()[0].split(""", """ ) snake_case_ : Optional[int] = [obj[1:-1] for obj in imports if len(a__ ) > 0] objects.extend(a__ ) elif _re_quote_object.search(a__ ) is not None: objects.append(_re_quote_object.search(a__ ).groups()[0] ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) elif line.startswith(""" """ * 12 + """\"""" ): objects.append(line[13:-3] ) line_index += 1 snake_case_ : Optional[Any] = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend snake_case_ : Optional[int] = [] while ( line_index < len(a__ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): snake_case_ : str = lines[line_index] snake_case_ : str = _re_import.search(a__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 snake_case_ : Tuple = {"""none""": objects} # Let's continue with backend-specific objects while line_index < len(a__ ): # If the line is an if is_backend_available, we grab all objects associated. snake_case_ : List[str] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : Optional[Any] = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : str = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): snake_case_ : Optional[Any] = lines[line_index] snake_case_ : Union[str, Any] = _re_import.search(a__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 12 ): objects.append(line[12:-2] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 return import_dict_objects, type_hint_objects def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :str ): '''simple docstring''' def find_duplicates(lowerCamelCase_ :Any ): return [k for k, v in collections.Counter(a__ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] snake_case_ : Tuple = [] for key in import_dict_objects.keys(): snake_case_ : Any = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) snake_case_ : str = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): snake_case_ : Union[str, Any] = """base imports""" if key == """none""" else F'''{key} backend''' errors.append(F'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Any = [] for root, _, files in os.walk(a__ ): if "__init__.py" in files: snake_case_ : List[str] = os.path.join(a__ , """__init__.py""" ) snake_case_ : int = parse_init(a__ ) if objects is not None: snake_case_ : Optional[int] = analyze_results(*a__ ) if len(a__ ) > 0: snake_case_ : Tuple = F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append("""\n""".join(a__ ) ) if len(a__ ) > 0: raise ValueError("""\n\n""".join(a__ ) ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : str = [] for path, directories, files in os.walk(a__ ): for folder in directories: # Ignore private modules if folder.startswith("""_""" ): directories.remove(a__ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(a__ ) / folder).glob("""*.py""" ) ) ) == 0: continue snake_case_ : Any = str((Path(a__ ) / folder).relative_to(a__ ) ) snake_case_ : Any = short_path.replace(os.path.sep , """.""" ) submodules.append(a__ ) for fname in files: if fname == "__init__.py": continue snake_case_ : int = str((Path(a__ ) / fname).relative_to(a__ ) ) snake_case_ : Optional[int] = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(a__ ) return submodules __A : Union[str, Any] = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', ] def UpperCAmelCase ( ): '''simple docstring''' # This is to make sure the transformers module imported is the one in the repo. snake_case_ : Union[str, Any] = importlib.util.spec_from_file_location( """transformers""" , os.path.join(a__ , """__init__.py""" ) , submodule_search_locations=[PATH_TO_TRANSFORMERS] , ) snake_case_ : Optional[int] = spec.loader.load_module() snake_case_ : Optional[Any] = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys() ] if len(a__ ) > 0: snake_case_ : Any = """\n""".join(F'''- {module}''' for module in module_not_registered ) raise ValueError( """The following submodules are not properly registered in the main init of Transformers:\n""" F'''{list_of_modules}\n''' """Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.""" ) if __name__ == "__main__": check_all_inits() check_submodules()
360
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[Any] = logging.get_logger(__name__) __A : str = { 'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json', # See all CANINE models at https://huggingface.co/models?filter=canine } class __UpperCamelCase ( lowercase__ ): lowercase : List[Any] = 'canine' def __init__( self :Optional[int] ,_UpperCamelCase :Dict=7_6_8 ,_UpperCamelCase :Union[str, Any]=1_2 ,_UpperCamelCase :int=1_2 ,_UpperCamelCase :int=3_0_7_2 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :Any=0.1 ,_UpperCamelCase :int=0.1 ,_UpperCamelCase :Any=1_6_3_8_4 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :List[str]=0.02 ,_UpperCamelCase :Any=1E-1_2 ,_UpperCamelCase :Tuple=0 ,_UpperCamelCase :List[str]=0xE_0_0_0 ,_UpperCamelCase :Optional[Any]=0xE_0_0_1 ,_UpperCamelCase :str=4 ,_UpperCamelCase :Optional[int]=4 ,_UpperCamelCase :str=8 ,_UpperCamelCase :int=1_6_3_8_4 ,_UpperCamelCase :int=1_2_8 ,**_UpperCamelCase :str ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : List[str] = max_position_embeddings snake_case_ : Union[str, Any] = hidden_size snake_case_ : Dict = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : Tuple = intermediate_size snake_case_ : str = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : Optional[Any] = initializer_range snake_case_ : Optional[int] = type_vocab_size snake_case_ : List[str] = layer_norm_eps # Character config: snake_case_ : Any = downsampling_rate snake_case_ : List[str] = upsampling_kernel_size snake_case_ : int = num_hash_functions snake_case_ : Tuple = num_hash_buckets snake_case_ : Tuple = local_transformer_stride
8
0
'''simple docstring''' import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class __UpperCamelCase ( __snake_case ): lowercase : Optional[int] = (PNDMScheduler,) lowercase : Optional[int] = (('num_inference_steps', 5_0),) def a__ ( self :List[Any] ,**_UpperCamelCase :List[Any] ): snake_case_ : Optional[Any] = { """num_train_timesteps""": 1_0_0_0, """beta_start""": 0.00_01, """beta_end""": 0.02, """beta_schedule""": """linear""", } config.update(**_UpperCamelCase ) return config def a__ ( self :Optional[int] ,_UpperCamelCase :Any=0 ,**_UpperCamelCase :Any ): snake_case_ : str = dict(self.forward_default_kwargs ) snake_case_ : str = kwargs.pop("""num_inference_steps""" ,_UpperCamelCase ) snake_case_ : List[Any] = self.dummy_sample snake_case_ : Tuple = 0.1 * sample snake_case_ : Optional[Any] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: snake_case_ : Optional[Any] = self.get_scheduler_config(**_UpperCamelCase ) snake_case_ : Tuple = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(_UpperCamelCase ) # copy over dummy past residuals snake_case_ : List[str] = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_UpperCamelCase ) snake_case_ : Optional[Any] = scheduler_class.from_pretrained(_UpperCamelCase ) new_scheduler.set_timesteps(_UpperCamelCase ) # copy over dummy past residuals snake_case_ : int = dummy_past_residuals[:] snake_case_ : Dict = scheduler.step_prk(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample snake_case_ : int = new_scheduler.step_prk(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" snake_case_ : int = scheduler.step_plms(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample snake_case_ : Dict = new_scheduler.step_plms(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def a__ ( self :int ): pass def a__ ( self :Any ,_UpperCamelCase :List[str]=0 ,**_UpperCamelCase :Union[str, Any] ): snake_case_ : Dict = dict(self.forward_default_kwargs ) snake_case_ : Any = kwargs.pop("""num_inference_steps""" ,_UpperCamelCase ) snake_case_ : List[str] = self.dummy_sample snake_case_ : List[Any] = 0.1 * sample snake_case_ : List[str] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: snake_case_ : Optional[int] = self.get_scheduler_config() snake_case_ : Any = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(_UpperCamelCase ) # copy over dummy past residuals (must be after setting timesteps) snake_case_ : List[str] = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_UpperCamelCase ) snake_case_ : str = scheduler_class.from_pretrained(_UpperCamelCase ) # copy over dummy past residuals new_scheduler.set_timesteps(_UpperCamelCase ) # copy over dummy past residual (must be after setting timesteps) snake_case_ : Union[str, Any] = dummy_past_residuals[:] snake_case_ : Optional[Any] = scheduler.step_prk(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample snake_case_ : List[Any] = new_scheduler.step_prk(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" snake_case_ : str = scheduler.step_plms(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample snake_case_ : int = new_scheduler.step_plms(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def a__ ( self :List[Any] ,**_UpperCamelCase :Optional[Any] ): snake_case_ : Dict = self.scheduler_classes[0] snake_case_ : int = self.get_scheduler_config(**_UpperCamelCase ) snake_case_ : Union[str, Any] = scheduler_class(**_UpperCamelCase ) snake_case_ : Optional[int] = 1_0 snake_case_ : Optional[int] = self.dummy_model() snake_case_ : Union[str, Any] = self.dummy_sample_deter scheduler.set_timesteps(_UpperCamelCase ) for i, t in enumerate(scheduler.prk_timesteps ): snake_case_ : int = model(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = scheduler.step_prk(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): snake_case_ : List[str] = model(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Any = scheduler.step_plms(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ).prev_sample return sample def a__ ( self :Optional[Any] ): snake_case_ : Any = dict(self.forward_default_kwargs ) snake_case_ : Optional[int] = kwargs.pop("""num_inference_steps""" ,_UpperCamelCase ) for scheduler_class in self.scheduler_classes: snake_case_ : List[Any] = self.get_scheduler_config() snake_case_ : Optional[Any] = scheduler_class(**_UpperCamelCase ) snake_case_ : List[str] = self.dummy_sample snake_case_ : Union[str, Any] = 0.1 * sample if num_inference_steps is not None and hasattr(_UpperCamelCase ,"""set_timesteps""" ): scheduler.set_timesteps(_UpperCamelCase ) elif num_inference_steps is not None and not hasattr(_UpperCamelCase ,"""set_timesteps""" ): snake_case_ : int = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) snake_case_ : List[str] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] snake_case_ : Tuple = dummy_past_residuals[:] snake_case_ : Tuple = scheduler.step_prk(_UpperCamelCase ,0 ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample snake_case_ : int = scheduler.step_prk(_UpperCamelCase ,1 ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample self.assertEqual(output_a.shape ,sample.shape ) self.assertEqual(output_a.shape ,output_a.shape ) snake_case_ : str = scheduler.step_plms(_UpperCamelCase ,0 ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample snake_case_ : List[str] = scheduler.step_plms(_UpperCamelCase ,1 ,_UpperCamelCase ,**_UpperCamelCase ).prev_sample self.assertEqual(output_a.shape ,sample.shape ) self.assertEqual(output_a.shape ,output_a.shape ) def a__ ( self :int ): for timesteps in [1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def a__ ( self :Tuple ): for steps_offset in [0, 1]: self.check_over_configs(steps_offset=_UpperCamelCase ) snake_case_ : int = self.scheduler_classes[0] snake_case_ : Tuple = self.get_scheduler_config(steps_offset=1 ) snake_case_ : Union[str, Any] = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(1_0 ) assert torch.equal( scheduler.timesteps ,torch.LongTensor( [9_0_1, 8_5_1, 8_5_1, 8_0_1, 8_0_1, 7_5_1, 7_5_1, 7_0_1, 7_0_1, 6_5_1, 6_5_1, 6_0_1, 6_0_1, 5_0_1, 4_0_1, 3_0_1, 2_0_1, 1_0_1, 1] ) ,) def a__ ( self :Dict ): for beta_start, beta_end in zip([0.00_01, 0.0_01] ,[0.0_02, 0.02] ): self.check_over_configs(beta_start=_UpperCamelCase ,beta_end=_UpperCamelCase ) def a__ ( self :List[str] ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def a__ ( self :Optional[int] ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def a__ ( self :Tuple ): for t in [1, 5, 1_0]: self.check_over_forward(time_step=_UpperCamelCase ) def a__ ( self :Any ): for t, num_inference_steps in zip([1, 5, 1_0] ,[1_0, 5_0, 1_0_0] ): self.check_over_forward(num_inference_steps=_UpperCamelCase ) def a__ ( self :Union[str, Any] ): # earlier version of set_timesteps() caused an error indexing alpha's with inference steps as power of 3 snake_case_ : int = 2_7 for scheduler_class in self.scheduler_classes: snake_case_ : Optional[Any] = self.dummy_sample snake_case_ : Tuple = 0.1 * sample snake_case_ : Tuple = self.get_scheduler_config() snake_case_ : Optional[Any] = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(_UpperCamelCase ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): snake_case_ : Optional[Any] = scheduler.step_prk(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ).prev_sample def a__ ( self :Union[str, Any] ): with self.assertRaises(_UpperCamelCase ): snake_case_ : Dict = self.scheduler_classes[0] snake_case_ : str = self.get_scheduler_config() snake_case_ : Any = scheduler_class(**_UpperCamelCase ) scheduler.step_plms(self.dummy_sample ,1 ,self.dummy_sample ).prev_sample def a__ ( self :List[str] ): snake_case_ : Any = self.full_loop() snake_case_ : Tuple = torch.sum(torch.abs(_UpperCamelCase ) ) snake_case_ : Dict = torch.mean(torch.abs(_UpperCamelCase ) ) assert abs(result_sum.item() - 1_98.13_18 ) < 1E-2 assert abs(result_mean.item() - 0.25_80 ) < 1E-3 def a__ ( self :List[Any] ): snake_case_ : Tuple = self.full_loop(prediction_type="""v_prediction""" ) snake_case_ : Tuple = torch.sum(torch.abs(_UpperCamelCase ) ) snake_case_ : Optional[int] = torch.mean(torch.abs(_UpperCamelCase ) ) assert abs(result_sum.item() - 67.39_86 ) < 1E-2 assert abs(result_mean.item() - 0.08_78 ) < 1E-3 def a__ ( self :List[str] ): # We specify different beta, so that the first alpha is 0.99 snake_case_ : Tuple = self.full_loop(set_alpha_to_one=_UpperCamelCase ,beta_start=0.01 ) snake_case_ : Optional[Any] = torch.sum(torch.abs(_UpperCamelCase ) ) snake_case_ : Tuple = torch.mean(torch.abs(_UpperCamelCase ) ) assert abs(result_sum.item() - 2_30.03_99 ) < 1E-2 assert abs(result_mean.item() - 0.29_95 ) < 1E-3 def a__ ( self :str ): # We specify different beta, so that the first alpha is 0.99 snake_case_ : Tuple = self.full_loop(set_alpha_to_one=_UpperCamelCase ,beta_start=0.01 ) snake_case_ : Union[str, Any] = torch.sum(torch.abs(_UpperCamelCase ) ) snake_case_ : List[Any] = torch.mean(torch.abs(_UpperCamelCase ) ) assert abs(result_sum.item() - 1_86.94_82 ) < 1E-2 assert abs(result_mean.item() - 0.24_34 ) < 1E-3
361
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __A : Tuple = logging.get_logger(__name__) __A : List[Any] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } __A : str = { 'vocab_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json' }, 'merges_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt' }, 'tokenizer_config_file': { 'facebook/blenderbot_small-90M': ( 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json' ) }, } __A : Optional[Any] = { 'facebook/blenderbot_small-90M': 512, } class __UpperCamelCase ( lowercase__ ): lowercase : str = VOCAB_FILES_NAMES lowercase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : Dict = BlenderbotSmallTokenizer def __init__( self :str ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :Tuple="<|endoftext|>" ,_UpperCamelCase :int="<|endoftext|>" ,_UpperCamelCase :Dict="<|endoftext|>" ,_UpperCamelCase :Optional[Any]=False ,_UpperCamelCase :List[Any]=True ,**_UpperCamelCase :Any ,): super().__init__( ByteLevelBPETokenizer( vocab=_UpperCamelCase ,merges=_UpperCamelCase ,add_prefix_space=_UpperCamelCase ,trim_offsets=_UpperCamelCase ,) ,bos_token=_UpperCamelCase ,eos_token=_UpperCamelCase ,unk_token=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Any = add_prefix_space def a__ ( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any]=None ): snake_case_ : List[Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def a__ ( self :int ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): snake_case_ : int = [self.sep_token_id] snake_case_ : Tuple = [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]
8
0
'''simple docstring''' import argparse import os from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_task_guides.py __A : Any = 'src/transformers' __A : List[str] = 'docs/source/en/tasks' def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str ): '''simple docstring''' with open(UpperCamelCase__ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: snake_case_ : Tuple = f.readlines() # Find the start prompt. snake_case_ : str = 0 while not lines[start_index].startswith(UpperCamelCase__ ): start_index += 1 start_index += 1 snake_case_ : Optional[int] = start_index while not lines[end_index].startswith(UpperCamelCase__ ): end_index += 1 end_index -= 1 while len(lines[start_index] ) <= 1: start_index += 1 while len(lines[end_index] ) <= 1: end_index -= 1 end_index += 1 return "".join(lines[start_index:end_index] ), start_index, end_index, lines # This is to make sure the transformers module imported is the one in the repo. __A : str = direct_transformers_import(TRANSFORMERS_PATH) __A : Any = { 'asr.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES, 'audio_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, 'language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, 'image_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, 'masked_language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES, 'multiple_choice.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES, 'object_detection.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, 'question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, 'semantic_segmentation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, 'sequence_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, 'summarization.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, 'token_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, 'translation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, 'video_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES, 'document_question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES, 'monocular_depth_estimation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, } # This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any # `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`). __A : Tuple = { 'summarization.md': ('nllb',), 'translation.md': ('nllb',), } def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Optional[int] = TASK_GUIDE_TO_MODELS[task_guide] snake_case_ : Tuple = SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(UpperCamelCase__ , set() ) snake_case_ : int = { code: name for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if (code in model_maping_names or code in special_model_types) } return ", ".join([F'''[{name}](../model_doc/{code})''' for code, name in model_names.items()] ) + "\n" def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :Any=False ): '''simple docstring''' snake_case_ , snake_case_ , snake_case_ , snake_case_ : List[Any] = _find_text_in_file( filename=os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , start_prompt="""<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->""" , end_prompt="""<!--End of the generated tip-->""" , ) snake_case_ : Optional[int] = get_model_list_for_task(UpperCamelCase__ ) if current_list != new_list: if overwrite: with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(lines[:start_index] + [new_list] + lines[end_index:] ) else: raise ValueError( F'''The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`''' """ to fix this.""" ) if __name__ == "__main__": __A : List[Any] = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') __A : List[Any] = parser.parse_args() for task_guide in TASK_GUIDE_TO_MODELS.keys(): check_model_list_for_task(task_guide, args.fix_and_overwrite)
362
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :list ): '''simple docstring''' if len(lowerCamelCase_ ) <= 1: return lst snake_case_ : Union[str, Any] = 1 while i < len(lowerCamelCase_ ): if lst[i - 1] <= lst[i]: i += 1 else: snake_case_ , snake_case_ : Union[str, Any] = lst[i], lst[i - 1] i -= 1 if i == 0: snake_case_ : int = 1 return lst if __name__ == "__main__": __A : Optional[int] = input('Enter numbers separated by a comma:\n').strip() __A : int = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
8
0
'''simple docstring''' import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __UpperCamelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase ): lowercase : Union[str, Any] = MgpstrTokenizer lowercase : Any = False lowercase : List[Any] = {} lowercase : Optional[int] = False def a__ ( self :Dict ): super().setUp() # fmt: off snake_case_ : List[Any] = ["[GO]", "[s]", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] # fmt: on snake_case_ : Any = dict(zip(_SCREAMING_SNAKE_CASE ,range(len(_SCREAMING_SNAKE_CASE ) ) ) ) snake_case_ : str = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file ,"""w""" ,encoding="""utf-8""" ) as fp: fp.write(json.dumps(_SCREAMING_SNAKE_CASE ) + """\n""" ) def a__ ( self :Optional[Any] ,**_UpperCamelCase :Optional[int] ): return MgpstrTokenizer.from_pretrained(self.tmpdirname ,**_SCREAMING_SNAKE_CASE ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Optional[int] ): snake_case_ : Any = "tester" snake_case_ : Optional[Any] = "tester" return input_text, output_text @unittest.skip("""MGP-STR always lower cases letters.""" ) def a__ ( self :List[str] ): pass def a__ ( self :List[Any] ): snake_case_ : str = self.get_tokenizers(do_lower_case=_SCREAMING_SNAKE_CASE ) for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): snake_case_ : Optional[int] = "[SPECIAL_TOKEN]" tokenizer.add_special_tokens({"""cls_token""": special_token} ) snake_case_ : Dict = tokenizer.encode([special_token] ,add_special_tokens=_SCREAMING_SNAKE_CASE ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) ,1 ) snake_case_ : str = tokenizer.decode(_SCREAMING_SNAKE_CASE ,skip_special_tokens=_SCREAMING_SNAKE_CASE ) self.assertTrue(special_token not in decoded ) def a__ ( self :str ): snake_case_ : Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): snake_case_ : str = self.get_input_output_texts(_SCREAMING_SNAKE_CASE ) snake_case_ : Tuple = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) snake_case_ : Tuple = tokenizer.convert_tokens_to_ids(_SCREAMING_SNAKE_CASE ) snake_case_ : Optional[int] = tokenizer.encode(_SCREAMING_SNAKE_CASE ,add_special_tokens=_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) snake_case_ : Optional[Any] = tokenizer.convert_ids_to_tokens(_SCREAMING_SNAKE_CASE ) self.assertNotEqual(len(_SCREAMING_SNAKE_CASE ) ,0 ) snake_case_ : Tuple = tokenizer.decode(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) self.assertEqual(text_a.replace(""" """ ,"""""" ) ,_SCREAMING_SNAKE_CASE ) @unittest.skip("""MGP-STR tokenizer only handles one sequence.""" ) def a__ ( self :List[Any] ): pass @unittest.skip("""inputs cannot be pretokenized in MgpstrTokenizer""" ) def a__ ( self :Optional[int] ): pass
363
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __UpperCamelCase : def __init__( self :Any ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[int]=1_2 ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Optional[int]=True ,_UpperCamelCase :Union[str, Any]=True ,_UpperCamelCase :Dict=True ,_UpperCamelCase :Optional[int]=9_9 ,_UpperCamelCase :Dict=3_2 ,_UpperCamelCase :Union[str, Any]=3_2 ,_UpperCamelCase :Union[str, Any]=2 ,_UpperCamelCase :Optional[Any]=4 ,_UpperCamelCase :List[Any]=3_7 ,_UpperCamelCase :Tuple=0.1 ,_UpperCamelCase :Optional[int]=0.1 ,_UpperCamelCase :int=5_1_2 ,_UpperCamelCase :Tuple=0.02 ,_UpperCamelCase :Any=0 ,_UpperCamelCase :str=None ,): snake_case_ : str = parent snake_case_ : int = batch_size snake_case_ : Union[str, Any] = seq_length snake_case_ : List[Any] = is_training snake_case_ : Union[str, Any] = use_input_mask snake_case_ : List[str] = use_labels snake_case_ : int = vocab_size snake_case_ : Any = hidden_size snake_case_ : List[Any] = projection_dim snake_case_ : Dict = num_hidden_layers snake_case_ : Dict = num_attention_heads snake_case_ : str = intermediate_size snake_case_ : int = dropout snake_case_ : int = attention_dropout snake_case_ : Dict = max_position_embeddings snake_case_ : Union[str, Any] = initializer_range snake_case_ : Dict = scope snake_case_ : Union[str, Any] = bos_token_id def a__ ( self :Any ): snake_case_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case_ : Union[str, Any] = None if self.use_input_mask: snake_case_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: snake_case_ : int = input_mask.numpy() snake_case_ , snake_case_ : Tuple = input_mask.shape snake_case_ : Any = np.random.randint(1 ,seq_length - 1 ,size=(batch_size,) ) for batch_idx, start_index in enumerate(_UpperCamelCase ): snake_case_ : Optional[int] = 1 snake_case_ : List[str] = 0 snake_case_ : Tuple = self.get_config() return config, input_ids, tf.convert_to_tensor(_UpperCamelCase ) def a__ ( self :str ): return BlipTextConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,projection_dim=self.projection_dim ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,dropout=self.dropout ,attention_dropout=self.attention_dropout ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,bos_token_id=self.bos_token_id ,) def a__ ( self :List[Any] ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[int] ): snake_case_ : List[str] = TFBlipTextModel(config=_UpperCamelCase ) snake_case_ : List[Any] = model(_UpperCamelCase ,attention_mask=_UpperCamelCase ,training=_UpperCamelCase ) snake_case_ : Any = model(_UpperCamelCase ,training=_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def a__ ( self :List[str] ): snake_case_ : Union[str, Any] = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ : str = config_and_inputs snake_case_ : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Optional[Any] = (TFBlipTextModel,) if is_tf_available() else () lowercase : int = False lowercase : List[Any] = False lowercase : Dict = False def a__ ( self :List[Any] ): snake_case_ : List[str] = BlipTextModelTester(self ) snake_case_ : Tuple = ConfigTester(self ,config_class=_UpperCamelCase ,hidden_size=3_7 ) def a__ ( self :Union[str, Any] ): self.config_tester.run_common_tests() def a__ ( self :Union[str, Any] ): snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def a__ ( self :Tuple ): pass def a__ ( self :Tuple ): pass @unittest.skip(reason="""Blip does not use inputs_embeds""" ) def a__ ( self :Any ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :Tuple ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :List[Any] ): pass @slow def a__ ( self :Any ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ : Optional[Any] = TFBlipTextModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def a__ ( self :Dict ,_UpperCamelCase :Tuple=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCamelCase )
8
0
'''simple docstring''' import gc import unittest import numpy as np import torch from torch.backends.cuda import sdp_kernel from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) from diffusers.utils import randn_tensor, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( snake_case__ , unittest.TestCase ): lowercase : int = ConsistencyModelPipeline lowercase : Tuple = UNCONDITIONAL_IMAGE_GENERATION_PARAMS lowercase : Optional[Any] = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS # Override required_optional_params to remove num_images_per_prompt lowercase : List[str] = frozenset( [ 'num_inference_steps', 'generator', 'latents', 'output_type', 'return_dict', 'callback', 'callback_steps', ] ) @property def a__ ( self :Dict ): snake_case_ : Any = UNetaDModel.from_pretrained( """diffusers/consistency-models-test""" ,subfolder="""test_unet""" ,) return unet @property def a__ ( self :str ): snake_case_ : Optional[int] = UNetaDModel.from_pretrained( """diffusers/consistency-models-test""" ,subfolder="""test_unet_class_cond""" ,) return unet def a__ ( self :List[str] ,_UpperCamelCase :int=False ): if class_cond: snake_case_ : str = self.dummy_cond_unet else: snake_case_ : List[Any] = self.dummy_uncond_unet # Default to CM multistep sampler snake_case_ : Dict = CMStochasticIterativeScheduler( num_train_timesteps=4_0 ,sigma_min=0.0_02 ,sigma_max=80.0 ,) snake_case_ : List[str] = { "unet": unet, "scheduler": scheduler, } return components def a__ ( self :int ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Optional[int]=0 ): if str(UpperCAmelCase_ ).startswith("""mps""" ): snake_case_ : Dict = torch.manual_seed(UpperCAmelCase_ ) else: snake_case_ : Any = torch.Generator(device=UpperCAmelCase_ ).manual_seed(UpperCAmelCase_ ) snake_case_ : Optional[Any] = { "batch_size": 1, "num_inference_steps": None, "timesteps": [2_2, 0], "generator": generator, "output_type": "np", } return inputs def a__ ( self :Union[str, Any] ): snake_case_ : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case_ : int = self.get_dummy_components() snake_case_ : Tuple = ConsistencyModelPipeline(**UpperCAmelCase_ ) snake_case_ : Tuple = pipe.to(UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ : Optional[int] = self.get_dummy_inputs(UpperCAmelCase_ ) snake_case_ : Dict = pipe(**UpperCAmelCase_ ).images assert image.shape == (1, 3_2, 3_2, 3) snake_case_ : List[str] = image[0, -3:, -3:, -1] snake_case_ : List[Any] = np.array([0.35_72, 0.62_73, 0.40_31, 0.39_61, 0.43_21, 0.57_30, 0.52_66, 0.47_80, 0.50_04] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def a__ ( self :Optional[int] ): snake_case_ : List[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case_ : int = self.get_dummy_components(class_cond=UpperCAmelCase_ ) snake_case_ : Dict = ConsistencyModelPipeline(**UpperCAmelCase_ ) snake_case_ : int = pipe.to(UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ : int = self.get_dummy_inputs(UpperCAmelCase_ ) snake_case_ : Union[str, Any] = 0 snake_case_ : Optional[int] = pipe(**UpperCAmelCase_ ).images assert image.shape == (1, 3_2, 3_2, 3) snake_case_ : int = image[0, -3:, -3:, -1] snake_case_ : List[str] = np.array([0.35_72, 0.62_73, 0.40_31, 0.39_61, 0.43_21, 0.57_30, 0.52_66, 0.47_80, 0.50_04] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def a__ ( self :Union[str, Any] ): snake_case_ : List[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case_ : int = self.get_dummy_components() snake_case_ : List[Any] = ConsistencyModelPipeline(**UpperCAmelCase_ ) snake_case_ : Optional[int] = pipe.to(UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ : Optional[int] = self.get_dummy_inputs(UpperCAmelCase_ ) snake_case_ : Dict = 1 snake_case_ : str = None snake_case_ : Any = pipe(**UpperCAmelCase_ ).images assert image.shape == (1, 3_2, 3_2, 3) snake_case_ : Any = image[0, -3:, -3:, -1] snake_case_ : int = np.array([0.50_04, 0.50_04, 0.49_94, 0.50_08, 0.49_76, 0.50_18, 0.49_90, 0.49_82, 0.49_87] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def a__ ( self :Any ): snake_case_ : Dict = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case_ : Dict = self.get_dummy_components(class_cond=UpperCAmelCase_ ) snake_case_ : List[str] = ConsistencyModelPipeline(**UpperCAmelCase_ ) snake_case_ : Any = pipe.to(UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ : Optional[int] = self.get_dummy_inputs(UpperCAmelCase_ ) snake_case_ : Tuple = 1 snake_case_ : List[str] = None snake_case_ : Dict = 0 snake_case_ : Optional[int] = pipe(**UpperCAmelCase_ ).images assert image.shape == (1, 3_2, 3_2, 3) snake_case_ : Any = image[0, -3:, -3:, -1] snake_case_ : List[Any] = np.array([0.50_04, 0.50_04, 0.49_94, 0.50_08, 0.49_76, 0.50_18, 0.49_90, 0.49_82, 0.49_87] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :int ): super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self :Tuple ,_UpperCamelCase :Optional[Any]=0 ,_UpperCamelCase :int=False ,_UpperCamelCase :List[Any]="cpu" ,_UpperCamelCase :List[Any]=torch.floataa ,_UpperCamelCase :int=(1, 3, 6_4, 6_4) ): snake_case_ : Any = torch.manual_seed(UpperCAmelCase_ ) snake_case_ : Tuple = { "num_inference_steps": None, "timesteps": [2_2, 0], "class_labels": 0, "generator": generator, "output_type": "np", } if get_fixed_latents: snake_case_ : Optional[Any] = self.get_fixed_latents(seed=UpperCAmelCase_ ,device=UpperCAmelCase_ ,dtype=UpperCAmelCase_ ,shape=UpperCAmelCase_ ) snake_case_ : Optional[int] = latents return inputs def a__ ( self :str ,_UpperCamelCase :Dict=0 ,_UpperCamelCase :List[Any]="cpu" ,_UpperCamelCase :Tuple=torch.floataa ,_UpperCamelCase :Dict=(1, 3, 6_4, 6_4) ): if type(UpperCAmelCase_ ) == str: snake_case_ : Optional[int] = torch.device(UpperCAmelCase_ ) snake_case_ : Union[str, Any] = torch.Generator(device=UpperCAmelCase_ ).manual_seed(UpperCAmelCase_ ) snake_case_ : int = randn_tensor(UpperCAmelCase_ ,generator=UpperCAmelCase_ ,device=UpperCAmelCase_ ,dtype=UpperCAmelCase_ ) return latents def a__ ( self :Dict ): snake_case_ : Optional[Any] = UNetaDModel.from_pretrained("""diffusers/consistency_models""" ,subfolder="""diffusers_cd_imagenet64_l2""" ) snake_case_ : Dict = CMStochasticIterativeScheduler( num_train_timesteps=4_0 ,sigma_min=0.0_02 ,sigma_max=80.0 ,) snake_case_ : Tuple = ConsistencyModelPipeline(unet=UpperCAmelCase_ ,scheduler=UpperCAmelCase_ ) pipe.to(torch_device=UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ : Dict = self.get_inputs() snake_case_ : Optional[int] = pipe(**UpperCAmelCase_ ).images assert image.shape == (1, 6_4, 6_4, 3) snake_case_ : List[str] = image[0, -3:, -3:, -1] snake_case_ : Optional[int] = np.array([0.08_88, 0.08_81, 0.06_66, 0.04_79, 0.02_92, 0.01_95, 0.02_01, 0.01_63, 0.02_54] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2 def a__ ( self :Optional[int] ): snake_case_ : Dict = UNetaDModel.from_pretrained("""diffusers/consistency_models""" ,subfolder="""diffusers_cd_imagenet64_l2""" ) snake_case_ : Tuple = CMStochasticIterativeScheduler( num_train_timesteps=4_0 ,sigma_min=0.0_02 ,sigma_max=80.0 ,) snake_case_ : Optional[Any] = ConsistencyModelPipeline(unet=UpperCAmelCase_ ,scheduler=UpperCAmelCase_ ) pipe.to(torch_device=UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ : Any = self.get_inputs() snake_case_ : List[Any] = 1 snake_case_ : Tuple = None snake_case_ : Union[str, Any] = pipe(**UpperCAmelCase_ ).images assert image.shape == (1, 6_4, 6_4, 3) snake_case_ : Dict = image[0, -3:, -3:, -1] snake_case_ : Optional[int] = np.array([0.03_40, 0.01_52, 0.00_63, 0.02_67, 0.02_21, 0.01_07, 0.04_16, 0.01_86, 0.02_17] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2 @require_torch_a def a__ ( self :Union[str, Any] ): snake_case_ : Tuple = UNetaDModel.from_pretrained("""diffusers/consistency_models""" ,subfolder="""diffusers_cd_imagenet64_l2""" ) snake_case_ : List[Any] = CMStochasticIterativeScheduler( num_train_timesteps=4_0 ,sigma_min=0.0_02 ,sigma_max=80.0 ,) snake_case_ : Tuple = ConsistencyModelPipeline(unet=UpperCAmelCase_ ,scheduler=UpperCAmelCase_ ) pipe.to(torch_device=UpperCAmelCase_ ,torch_dtype=torch.floataa ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ : Any = self.get_inputs(get_fixed_latents=UpperCAmelCase_ ,device=UpperCAmelCase_ ) # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=UpperCAmelCase_ ,enable_math=UpperCAmelCase_ ,enable_mem_efficient=UpperCAmelCase_ ): snake_case_ : List[str] = pipe(**UpperCAmelCase_ ).images assert image.shape == (1, 6_4, 6_4, 3) snake_case_ : List[str] = image[0, -3:, -3:, -1] snake_case_ : Optional[int] = np.array([0.18_75, 0.14_28, 0.12_89, 0.21_51, 0.20_92, 0.14_77, 0.18_77, 0.16_41, 0.13_53] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 @require_torch_a def a__ ( self :Optional[int] ): snake_case_ : Any = UNetaDModel.from_pretrained("""diffusers/consistency_models""" ,subfolder="""diffusers_cd_imagenet64_l2""" ) snake_case_ : int = CMStochasticIterativeScheduler( num_train_timesteps=4_0 ,sigma_min=0.0_02 ,sigma_max=80.0 ,) snake_case_ : List[Any] = ConsistencyModelPipeline(unet=UpperCAmelCase_ ,scheduler=UpperCAmelCase_ ) pipe.to(torch_device=UpperCAmelCase_ ,torch_dtype=torch.floataa ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ : Union[str, Any] = self.get_inputs(get_fixed_latents=UpperCAmelCase_ ,device=UpperCAmelCase_ ) snake_case_ : Tuple = 1 snake_case_ : List[str] = None # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=UpperCAmelCase_ ,enable_math=UpperCAmelCase_ ,enable_mem_efficient=UpperCAmelCase_ ): snake_case_ : Tuple = pipe(**UpperCAmelCase_ ).images assert image.shape == (1, 6_4, 6_4, 3) snake_case_ : Tuple = image[0, -3:, -3:, -1] snake_case_ : Tuple = np.array([0.16_63, 0.19_48, 0.22_75, 0.16_80, 0.12_04, 0.12_45, 0.18_58, 0.13_38, 0.20_95] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3
364
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : int = { 'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'], 'feature_extraction_whisper': ['WhisperFeatureExtractor'], 'processing_whisper': ['WhisperProcessor'], 'tokenization_whisper': ['WhisperTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FlaxWhisperForConditionalGeneration', 'FlaxWhisperModel', 'FlaxWhisperPreTrainedModel', 'FlaxWhisperForAudioClassification', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys __A : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
0
'''simple docstring''' def UpperCAmelCase_ ( lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Tuple = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
365
'''simple docstring''' import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor __A : Optional[int] = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[str] ,*_UpperCamelCase :str ,**_UpperCamelCase :Optional[int] ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" ,_UpperCamelCase ,) super().__init__(*_UpperCamelCase ,**_UpperCamelCase )
8
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) __A : str = { 'configuration_vision_encoder_decoder': ['VisionEncoderDecoderConfig', 'VisionEncoderDecoderOnnxConfig'] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[Any] = ['VisionEncoderDecoderModel'] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Tuple = ['TFVisionEncoderDecoderModel'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = ['FlaxVisionEncoderDecoderModel'] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys __A : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
366
'''simple docstring''' import re def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[Any] = re.compile( R"""^(?:0|94|\+94|0{2}94)""" R"""7(0|1|2|4|5|6|7|8)""" R"""(-| |)""" R"""\d{7}$""" ) return bool(re.search(lowerCamelCase_ , lowerCamelCase_ ) ) if __name__ == "__main__": __A : int = '0094702343221' print(is_sri_lankan_phone_number(phone))
8
0
'''simple docstring''' import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device __A : Optional[Any] = False class __UpperCamelCase ( unittest.TestCase ): pass @nightly @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :Union[str, Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self :int ): snake_case_ : int = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" ,torch_dtype=torch.floataa ) pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) snake_case_ : Optional[Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) snake_case_ : List[Any] = torch.manual_seed(0 ) snake_case_ : str = pipe.dual_guided( prompt="""first prompt""" ,image=_lowerCamelCase ,text_to_image_strength=0.75 ,generator=_lowerCamelCase ,guidance_scale=7.5 ,num_inference_steps=2 ,output_type="""numpy""" ,).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(_lowerCamelCase ) snake_case_ : Any = VersatileDiffusionPipeline.from_pretrained(_lowerCamelCase ,torch_dtype=torch.floataa ) pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) snake_case_ : Dict = generator.manual_seed(0 ) snake_case_ : str = pipe.dual_guided( prompt="""first prompt""" ,image=_lowerCamelCase ,text_to_image_strength=0.75 ,generator=_lowerCamelCase ,guidance_scale=7.5 ,num_inference_steps=2 ,output_type="""numpy""" ,).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def a__ ( self :str ): snake_case_ : List[Any] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" ,torch_dtype=torch.floataa ) pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) snake_case_ : int = """cyberpunk 2077""" snake_case_ : Optional[int] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) snake_case_ : str = torch.manual_seed(0 ) snake_case_ : int = pipe.dual_guided( prompt=_lowerCamelCase ,image=_lowerCamelCase ,text_to_image_strength=0.75 ,generator=_lowerCamelCase ,guidance_scale=7.5 ,num_inference_steps=5_0 ,output_type="""numpy""" ,).images snake_case_ : List[Any] = image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ : Optional[int] = np.array([0.14_48, 0.16_19, 0.17_41, 0.10_86, 0.11_47, 0.11_28, 0.11_99, 0.11_65, 0.10_01] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 snake_case_ : List[str] = """A painting of a squirrel eating a burger """ snake_case_ : Any = torch.manual_seed(0 ) snake_case_ : Any = pipe.text_to_image( prompt=_lowerCamelCase ,generator=_lowerCamelCase ,guidance_scale=7.5 ,num_inference_steps=5_0 ,output_type="""numpy""" ).images snake_case_ : Optional[Any] = image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ : Optional[Any] = np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 snake_case_ : Tuple = pipe.image_variation(_lowerCamelCase ,generator=_lowerCamelCase ,output_type="""numpy""" ).images snake_case_ : str = image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ : Union[str, Any] = np.array([0.30_76, 0.31_23, 0.32_84, 0.37_82, 0.37_70, 0.38_94, 0.42_97, 0.43_31, 0.44_56] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
367
'''simple docstring''' from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import BaseOutput, is_torch_available, is_transformers_available @dataclass class __UpperCamelCase ( lowercase__ ): lowercase : Union[List[PIL.Image.Image], np.ndarray] lowercase : Optional[List[bool]] if is_transformers_available() and is_torch_available(): from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
8
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) __A : Optional[Any] = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys __A : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
368
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , unittest.TestCase ): lowercase : Dict = StableDiffusionInpaintPipeline lowercase : str = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS lowercase : Union[str, Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS lowercase : Dict = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowercase : Optional[int] = frozenset([] ) def a__ ( self :Any ): torch.manual_seed(0 ) snake_case_ : Optional[int] = UNetaDConditionModel( block_out_channels=(3_2, 6_4) ,layers_per_block=2 ,sample_size=3_2 ,in_channels=9 ,out_channels=4 ,down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,cross_attention_dim=3_2 ,attention_head_dim=(2, 4) ,use_linear_projection=_UpperCamelCase ,) snake_case_ : Tuple = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) torch.manual_seed(0 ) snake_case_ : List[str] = AutoencoderKL( block_out_channels=[3_2, 6_4] ,in_channels=3 ,out_channels=3 ,down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,latent_channels=4 ,sample_size=1_2_8 ,) torch.manual_seed(0 ) snake_case_ : Optional[int] = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=3_2 ,intermediate_size=3_7 ,layer_norm_eps=1E-0_5 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1_0_0_0 ,hidden_act="""gelu""" ,projection_dim=5_1_2 ,) snake_case_ : Tuple = CLIPTextModel(_UpperCamelCase ) snake_case_ : Optional[int] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) snake_case_ : str = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def a__ ( self :str ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Union[str, Any]=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched snake_case_ : List[Any] = floats_tensor((1, 3, 3_2, 3_2) ,rng=random.Random(_UpperCamelCase ) ).to(_UpperCamelCase ) snake_case_ : int = image.cpu().permute(0 ,2 ,3 ,1 )[0] snake_case_ : List[str] = Image.fromarray(np.uinta(_UpperCamelCase ) ).convert("""RGB""" ).resize((6_4, 6_4) ) snake_case_ : Optional[Any] = Image.fromarray(np.uinta(image + 4 ) ).convert("""RGB""" ).resize((6_4, 6_4) ) if str(_UpperCamelCase ).startswith("""mps""" ): snake_case_ : Optional[Any] = torch.manual_seed(_UpperCamelCase ) else: snake_case_ : Optional[int] = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) snake_case_ : int = { """prompt""": """A painting of a squirrel eating a burger""", """image""": init_image, """mask_image""": mask_image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def a__ ( self :Any ): snake_case_ : Union[str, Any] = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case_ : Optional[Any] = self.get_dummy_components() snake_case_ : Dict = StableDiffusionInpaintPipeline(**_UpperCamelCase ) snake_case_ : List[str] = sd_pipe.to(_UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ : Union[str, Any] = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ : Tuple = sd_pipe(**_UpperCamelCase ).images snake_case_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case_ : Dict = np.array([0.47_27, 0.57_35, 0.39_41, 0.54_46, 0.59_26, 0.43_94, 0.50_62, 0.46_54, 0.44_76] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def a__ ( self :Any ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :List[Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self :Tuple ): snake_case_ : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : List[str] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench.npy""" ) snake_case_ : str = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : Optional[Any] = StableDiffusionInpaintPipeline.from_pretrained(_UpperCamelCase ,safety_checker=_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() snake_case_ : Optional[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : List[str] = torch.manual_seed(0 ) snake_case_ : Dict = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""np""" ,) snake_case_ : Union[str, Any] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 9E-3 def a__ ( self :Tuple ): snake_case_ : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : List[str] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench_fp16.npy""" ) snake_case_ : Optional[int] = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : List[str] = StableDiffusionInpaintPipeline.from_pretrained( _UpperCamelCase ,torch_dtype=torch.floataa ,safety_checker=_UpperCamelCase ,) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() snake_case_ : Optional[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : List[Any] = torch.manual_seed(0 ) snake_case_ : Any = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,output_type="""np""" ,) snake_case_ : List[str] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 5E-1 def a__ ( self :Union[str, Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case_ : Optional[int] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) snake_case_ : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) snake_case_ : int = """stabilityai/stable-diffusion-2-inpainting""" snake_case_ : Dict = PNDMScheduler.from_pretrained(_UpperCamelCase ,subfolder="""scheduler""" ) snake_case_ : List[Any] = StableDiffusionInpaintPipeline.from_pretrained( _UpperCamelCase ,safety_checker=_UpperCamelCase ,scheduler=_UpperCamelCase ,torch_dtype=torch.floataa ,) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case_ : List[Any] = """Face of a yellow cat, high resolution, sitting on a park bench""" snake_case_ : Optional[int] = torch.manual_seed(0 ) snake_case_ : Tuple = pipe( prompt=_UpperCamelCase ,image=_UpperCamelCase ,mask_image=_UpperCamelCase ,generator=_UpperCamelCase ,num_inference_steps=2 ,output_type="""np""" ,) snake_case_ : Any = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 1_0**9
8
0
'''simple docstring''' from math import pow def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :int , ): '''simple docstring''' if current_sum == needed_sum: # If the sum of the powers is equal to needed_sum, then we have a solution. solutions_count += 1 return current_sum, solutions_count snake_case_ : Any = int(pow(lowerCamelCase_ , lowerCamelCase_ ) ) if current_sum + i_to_n <= needed_sum: # If the sum of the powers is less than needed_sum, then continue adding powers. current_sum += i_to_n snake_case_ : int = backtrack( lowerCamelCase_ , lowerCamelCase_ , current_number + 1 , lowerCamelCase_ , lowerCamelCase_ ) current_sum -= i_to_n if i_to_n < needed_sum: # If the power of i is less than needed_sum, then try with the next power. snake_case_ : Union[str, Any] = backtrack( lowerCamelCase_ , lowerCamelCase_ , current_number + 1 , lowerCamelCase_ , lowerCamelCase_ ) return current_sum, solutions_count def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :int ): '''simple docstring''' if not (1 <= needed_sum <= 10_00 and 2 <= power <= 10): raise ValueError( """Invalid input\n""" """needed_sum must be between 1 and 1000, power between 2 and 10.""" ) return backtrack(lowerCamelCase_ , lowerCamelCase_ , 1 , 0 , 0 )[1] # Return the solutions_count if __name__ == "__main__": import doctest doctest.testmod()
369
'''simple docstring''' import collections import os import re from pathlib import Path __A : Dict = 'src/transformers' # Matches is_xxx_available() __A : Dict = re.compile(r'is\_([a-z_]*)_available()') # Catches a one-line _import_struct = {xxx} __A : Any = re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __A : Tuple = re.compile(r'\s+"\S*":\s+\[([^\]]*)\]') # Catches a line if not is_foo_available __A : Optional[Any] = re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)') # Catches a line _import_struct["bla"].append("foo") __A : Optional[int] = re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __A : List[Any] = re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]') # Catches a line with an object between quotes and a comma: "MyModel", __A : Union[str, Any] = re.compile(r'^\s+"([^"]+)",') # Catches a line with objects between brackets only: ["foo", "bar"], __A : int = re.compile(r'^\s+\[([^\]]+)\]') # Catches a line with from foo import bar, bla, boo __A : int = re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') # Catches a line with try: __A : List[Any] = re.compile(r'^\s*try:') # Catches a line with else: __A : Any = re.compile(r'^\s*else:') def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' if _re_test_backend.search(lowerCamelCase_ ) is None: return None snake_case_ : Tuple = [b[0] for b in _re_backend.findall(lowerCamelCase_ )] backends.sort() return "_and_".join(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] ): '''simple docstring''' with open(lowerCamelCase_ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: snake_case_ : str = f.readlines() snake_case_ : List[Any] = 0 while line_index < len(lowerCamelCase_ ) and not lines[line_index].startswith("""_import_structure = {""" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(lowerCamelCase_ ): return None # First grab the objects without a specific backend in _import_structure snake_case_ : Union[str, Any] = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: snake_case_ : str = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(lowerCamelCase_ ): snake_case_ : Optional[int] = _re_one_line_import_struct.search(lowerCamelCase_ ).groups()[0] snake_case_ : Union[str, Any] = re.findall(R"""\[([^\]]+)\]""" , lowerCamelCase_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue snake_case_ : Any = _re_import_struct_key_value.search(lowerCamelCase_ ) if single_line_import_search is not None: snake_case_ : Optional[int] = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(""", """ ) if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) line_index += 1 snake_case_ : Union[str, Any] = {"""none""": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("""if TYPE_CHECKING""" ): # If the line is an if not is_backend_available, we grab all objects associated. snake_case_ : List[str] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : Tuple = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Dict = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): snake_case_ : List[Any] = lines[line_index] if _re_import_struct_add_one.search(lowerCamelCase_ ) is not None: objects.append(_re_import_struct_add_one.search(lowerCamelCase_ ).groups()[0] ) elif _re_import_struct_add_many.search(lowerCamelCase_ ) is not None: snake_case_ : Optional[int] = _re_import_struct_add_many.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : List[str] = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_between_brackets.search(lowerCamelCase_ ) is not None: snake_case_ : List[str] = _re_between_brackets.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : Any = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_quote_object.search(lowerCamelCase_ ) is not None: objects.append(_re_quote_object.search(lowerCamelCase_ ).groups()[0] ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) elif line.startswith(""" """ * 12 + """\"""" ): objects.append(line[13:-3] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend snake_case_ : List[Any] = [] while ( line_index < len(lowerCamelCase_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): snake_case_ : Union[str, Any] = lines[line_index] snake_case_ : Union[str, Any] = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 snake_case_ : Dict = {"""none""": objects} # Let's continue with backend-specific objects while line_index < len(lowerCamelCase_ ): # If the line is an if is_backend_available, we grab all objects associated. snake_case_ : Optional[Any] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : str = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Any = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): snake_case_ : Dict = lines[line_index] snake_case_ : Any = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 12 ): objects.append(line[12:-2] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 return import_dict_objects, type_hint_objects def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :List[str] ): '''simple docstring''' def find_duplicates(lowerCamelCase_ :Union[str, Any] ): return [k for k, v in collections.Counter(lowerCamelCase_ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] snake_case_ : Optional[int] = [] for key in import_dict_objects.keys(): snake_case_ : int = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) snake_case_ : List[str] = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): snake_case_ : str = """base imports""" if key == """none""" else F'''{key} backend''' errors.append(F'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Tuple = [] for root, _, files in os.walk(lowerCamelCase_ ): if "__init__.py" in files: snake_case_ : Any = os.path.join(lowerCamelCase_ , """__init__.py""" ) snake_case_ : Dict = parse_init(lowerCamelCase_ ) if objects is not None: snake_case_ : Any = analyze_results(*lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: snake_case_ : Tuple = F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append("""\n""".join(lowerCamelCase_ ) ) if len(lowerCamelCase_ ) > 0: raise ValueError("""\n\n""".join(lowerCamelCase_ ) ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Union[str, Any] = [] for path, directories, files in os.walk(lowerCamelCase_ ): for folder in directories: # Ignore private modules if folder.startswith("""_""" ): directories.remove(lowerCamelCase_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(lowerCamelCase_ ) / folder).glob("""*.py""" ) ) ) == 0: continue snake_case_ : Tuple = str((Path(lowerCamelCase_ ) / folder).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(os.path.sep , """.""" ) submodules.append(lowerCamelCase_ ) for fname in files: if fname == "__init__.py": continue snake_case_ : Dict = str((Path(lowerCamelCase_ ) / fname).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(lowerCamelCase_ ) return submodules __A : List[Any] = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', 'models.esm.openfold_utils', ] def UpperCAmelCase ( ): '''simple docstring''' # This is to make sure the transformers module imported is the one in the repo. from transformers.utils import direct_transformers_import snake_case_ : Union[str, Any] = direct_transformers_import(lowerCamelCase_ ) snake_case_ : List[str] = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(lowerCamelCase_ , """__init__.py""" ) , """r""" ) as f: snake_case_ : str = f.read() import_structure_keys.update(set(re.findall(R"""import_structure\[\"([^\"]*)\"\]""" , lowerCamelCase_ ) ) ) snake_case_ : Dict = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(lowerCamelCase_ ) > 0: snake_case_ : str = """\n""".join(F'''- {module}''' for module in module_not_registered ) raise ValueError( """The following submodules are not properly registed in the main init of Transformers:\n""" F'''{list_of_modules}\n''' """Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.""" ) if __name__ == "__main__": check_all_inits() check_submodules()
8
0
'''simple docstring''' import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __UpperCamelCase : @staticmethod def a__ ( *_UpperCamelCase :List[str] ,**_UpperCamelCase :Optional[Any] ): pass @is_pipeline_test @require_vision class __UpperCamelCase ( unittest.TestCase ): @require_torch def a__ ( self :Dict ): snake_case_ : Optional[int] = pipeline( model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification""" ,) snake_case_ : List[str] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case_ : List[Any] = image_classifier(snake_case_ ,candidate_labels=["""a""", """b""", """c"""] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(snake_case_ ) ,[ [{"""score""": 0.3_33, """label""": """a"""}, {"""score""": 0.3_33, """label""": """b"""}, {"""score""": 0.3_33, """label""": """c"""}], [{"""score""": 0.3_33, """label""": """a"""}, {"""score""": 0.3_33, """label""": """c"""}, {"""score""": 0.3_33, """label""": """b"""}], ] ,) snake_case_ : Dict = image_classifier([image] * 5 ,candidate_labels=["""A""", """B""", """C"""] ,batch_size=2 ) self.assertEqual( nested_simplify(snake_case_ ) ,[ [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], ] ,) @require_tf def a__ ( self :Any ): snake_case_ : str = pipeline( model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification""" ,framework="""tf""" ) snake_case_ : Optional[int] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case_ : int = image_classifier(snake_case_ ,candidate_labels=["""a""", """b""", """c"""] ) self.assertEqual( nested_simplify(snake_case_ ) ,[{"""score""": 0.3_33, """label""": """a"""}, {"""score""": 0.3_33, """label""": """b"""}, {"""score""": 0.3_33, """label""": """c"""}] ,) snake_case_ : Optional[int] = image_classifier([image] * 5 ,candidate_labels=["""A""", """B""", """C"""] ,batch_size=2 ) self.assertEqual( nested_simplify(snake_case_ ) ,[ [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], [ {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, {"""score""": 0.3_33, """label""": ANY(snake_case_ )}, ], ] ,) @slow @require_torch def a__ ( self :Union[str, Any] ): snake_case_ : List[str] = pipeline( task="""zero-shot-image-classification""" ,model="""openai/clip-vit-base-patch32""" ,) # This is an image of 2 cats with remotes and no planes snake_case_ : Optional[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case_ : Union[str, Any] = image_classifier(snake_case_ ,candidate_labels=["""cat""", """plane""", """remote"""] ) self.assertEqual( nested_simplify(snake_case_ ) ,[ {"""score""": 0.5_11, """label""": """remote"""}, {"""score""": 0.4_85, """label""": """cat"""}, {"""score""": 0.0_04, """label""": """plane"""}, ] ,) snake_case_ : List[Any] = image_classifier([image] * 5 ,candidate_labels=["""cat""", """plane""", """remote"""] ,batch_size=2 ) self.assertEqual( nested_simplify(snake_case_ ) ,[ [ {"""score""": 0.5_11, """label""": """remote"""}, {"""score""": 0.4_85, """label""": """cat"""}, {"""score""": 0.0_04, """label""": """plane"""}, ], ] * 5 ,) @slow @require_tf def a__ ( self :Optional[Any] ): snake_case_ : Optional[Any] = pipeline( task="""zero-shot-image-classification""" ,model="""openai/clip-vit-base-patch32""" ,framework="""tf""" ) # This is an image of 2 cats with remotes and no planes snake_case_ : Dict = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case_ : Any = image_classifier(snake_case_ ,candidate_labels=["""cat""", """plane""", """remote"""] ) self.assertEqual( nested_simplify(snake_case_ ) ,[ {"""score""": 0.5_11, """label""": """remote"""}, {"""score""": 0.4_85, """label""": """cat"""}, {"""score""": 0.0_04, """label""": """plane"""}, ] ,) snake_case_ : Optional[int] = image_classifier([image] * 5 ,candidate_labels=["""cat""", """plane""", """remote"""] ,batch_size=2 ) self.assertEqual( nested_simplify(snake_case_ ) ,[ [ {"""score""": 0.5_11, """label""": """remote"""}, {"""score""": 0.4_85, """label""": """cat"""}, {"""score""": 0.0_04, """label""": """plane"""}, ], ] * 5 ,)
370
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __UpperCamelCase ( unittest.TestCase ): def __init__( self :List[Any] ,_UpperCamelCase :List[str] ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Union[str, Any]=3 ,_UpperCamelCase :Any=1_8 ,_UpperCamelCase :Optional[Any]=3_0 ,_UpperCamelCase :List[str]=4_0_0 ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :List[Any]=True ,): snake_case_ : List[str] = size if size is not None else {"""height""": 1_8, """width""": 1_8} snake_case_ : Union[str, Any] = parent snake_case_ : str = batch_size snake_case_ : List[Any] = num_channels snake_case_ : Tuple = image_size snake_case_ : int = min_resolution snake_case_ : int = max_resolution snake_case_ : Union[str, Any] = do_resize snake_case_ : Optional[Any] = size snake_case_ : Any = apply_ocr def a__ ( self :Union[str, Any] ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Tuple = LayoutLMvaImageProcessor if is_pytesseract_available() else None def a__ ( self :List[Any] ): snake_case_ : Union[str, Any] = LayoutLMvaImageProcessingTester(self ) @property def a__ ( self :int ): return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self :Any ): snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase ,"""do_resize""" ) ) self.assertTrue(hasattr(_UpperCamelCase ,"""size""" ) ) self.assertTrue(hasattr(_UpperCamelCase ,"""apply_ocr""" ) ) def a__ ( self :int ): snake_case_ : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 1_8, """width""": 1_8} ) snake_case_ : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict ,size=4_2 ) self.assertEqual(image_processor.size ,{"""height""": 4_2, """width""": 4_2} ) def a__ ( self :Optional[Any] ): pass def a__ ( self :Union[str, Any] ): # Initialize image_processing snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ : List[str] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,Image.Image ) # Test not batched input snake_case_ : List[str] = image_processing(image_inputs[0] ,return_tensors="""pt""" ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) self.assertIsInstance(encoding.words ,_UpperCamelCase ) self.assertIsInstance(encoding.boxes ,_UpperCamelCase ) # Test batched snake_case_ : List[Any] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :Tuple ): # Initialize image_processing snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ,numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,np.ndarray ) # Test not batched input snake_case_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Any = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :Optional[Any] ): # Initialize image_processing snake_case_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ : Optional[int] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=_UpperCamelCase ,torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase ,torch.Tensor ) # Test not batched input snake_case_ : Tuple = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Union[str, Any] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) def a__ ( self :List[Any] ): # with apply_OCR = True snake_case_ : Any = LayoutLMvaImageProcessor() from datasets import load_dataset snake_case_ : List[Any] = load_dataset("""hf-internal-testing/fixtures_docvqa""" ,split="""test""" ) snake_case_ : str = Image.open(ds[0]["""file"""] ).convert("""RGB""" ) snake_case_ : Dict = image_processing(_UpperCamelCase ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 2_2_4, 2_2_4) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 snake_case_ : Tuple = [["""11:14""", """to""", """11:39""", """a.m""", """11:39""", """to""", """11:44""", """a.m.""", """11:44""", """a.m.""", """to""", """12:25""", """p.m.""", """12:25""", """to""", """12:58""", """p.m.""", """12:58""", """to""", """4:00""", """p.m.""", """2:00""", """to""", """5:00""", """p.m.""", """Coffee""", """Break""", """Coffee""", """will""", """be""", """served""", """for""", """men""", """and""", """women""", """in""", """the""", """lobby""", """adjacent""", """to""", """exhibit""", """area.""", """Please""", """move""", """into""", """exhibit""", """area.""", """(Exhibits""", """Open)""", """TRRF""", """GENERAL""", """SESSION""", """(PART""", """|)""", """Presiding:""", """Lee""", """A.""", """Waller""", """TRRF""", """Vice""", """President""", """“Introductory""", """Remarks”""", """Lee""", """A.""", """Waller,""", """TRRF""", """Vice""", """Presi-""", """dent""", """Individual""", """Interviews""", """with""", """TRRF""", """Public""", """Board""", """Members""", """and""", """Sci-""", """entific""", """Advisory""", """Council""", """Mem-""", """bers""", """Conducted""", """by""", """TRRF""", """Treasurer""", """Philip""", """G.""", """Kuehn""", """to""", """get""", """answers""", """which""", """the""", """public""", """refrigerated""", """warehousing""", """industry""", """is""", """looking""", """for.""", """Plus""", """questions""", """from""", """the""", """floor.""", """Dr.""", """Emil""", """M.""", """Mrak,""", """University""", """of""", """Cal-""", """ifornia,""", """Chairman,""", """TRRF""", """Board;""", """Sam""", """R.""", """Cecil,""", """University""", """of""", """Georgia""", """College""", """of""", """Agriculture;""", """Dr.""", """Stanley""", """Charm,""", """Tufts""", """University""", """School""", """of""", """Medicine;""", """Dr.""", """Robert""", """H.""", """Cotton,""", """ITT""", """Continental""", """Baking""", """Company;""", """Dr.""", """Owen""", """Fennema,""", """University""", """of""", """Wis-""", """consin;""", """Dr.""", """Robert""", """E.""", """Hardenburg,""", """USDA.""", """Questions""", """and""", """Answers""", """Exhibits""", """Open""", """Capt.""", """Jack""", """Stoney""", """Room""", """TRRF""", """Scientific""", """Advisory""", """Council""", """Meeting""", """Ballroom""", """Foyer"""]] # noqa: E231 snake_case_ : Any = [[[1_4_1, 5_7, 2_1_4, 6_9], [2_2_8, 5_8, 2_5_2, 6_9], [1_4_1, 7_5, 2_1_6, 8_8], [2_3_0, 7_9, 2_8_0, 8_8], [1_4_2, 2_6_0, 2_1_8, 2_7_3], [2_3_0, 2_6_1, 2_5_5, 2_7_3], [1_4_3, 2_7_9, 2_1_8, 2_9_0], [2_3_1, 2_8_2, 2_9_0, 2_9_1], [1_4_3, 3_4_2, 2_1_8, 3_5_4], [2_3_1, 3_4_5, 2_8_9, 3_5_5], [2_0_2, 3_6_2, 2_2_7, 3_7_3], [1_4_3, 3_7_9, 2_2_0, 3_9_2], [2_3_1, 3_8_2, 2_9_1, 3_9_4], [1_4_4, 7_1_4, 2_2_0, 7_2_6], [2_3_1, 7_1_5, 2_5_6, 7_2_6], [1_4_4, 7_3_2, 2_2_0, 7_4_5], [2_3_2, 7_3_6, 2_9_1, 7_4_7], [1_4_4, 7_6_9, 2_1_8, 7_8_2], [2_3_1, 7_7_0, 2_5_6, 7_8_2], [1_4_1, 7_8_8, 2_0_2, 8_0_1], [2_1_5, 7_9_1, 2_7_4, 8_0_4], [1_4_3, 8_2_6, 2_0_4, 8_3_8], [2_1_5, 8_2_6, 2_4_0, 8_3_8], [1_4_2, 8_4_4, 2_0_2, 8_5_7], [2_1_5, 8_4_7, 2_7_4, 8_5_9], [3_3_4, 5_7, 4_2_7, 6_9], [4_4_0, 5_7, 5_2_2, 6_9], [3_6_9, 7_5, 4_6_1, 8_8], [4_6_9, 7_5, 5_1_6, 8_8], [5_2_8, 7_6, 5_6_2, 8_8], [5_7_0, 7_6, 6_6_7, 8_8], [6_7_5, 7_5, 7_1_1, 8_7], [7_2_1, 7_9, 7_7_8, 8_8], [7_8_9, 7_5, 8_4_0, 8_8], [3_6_9, 9_7, 4_7_0, 1_0_7], [4_8_4, 9_4, 5_0_7, 1_0_6], [5_1_8, 9_4, 5_6_2, 1_0_7], [5_7_6, 9_4, 6_5_5, 1_1_0], [6_6_8, 9_4, 7_9_2, 1_0_9], [8_0_4, 9_5, 8_2_9, 1_0_7], [3_6_9, 1_1_3, 4_6_5, 1_2_5], [4_7_7, 1_1_6, 5_4_7, 1_2_5], [5_6_2, 1_1_3, 6_5_8, 1_2_5], [6_7_1, 1_1_6, 7_4_8, 1_2_5], [7_6_1, 1_1_3, 8_1_1, 1_2_5], [3_6_9, 1_3_1, 4_6_5, 1_4_3], [4_7_7, 1_3_3, 5_4_8, 1_4_3], [5_6_3, 1_3_0, 6_9_8, 1_4_5], [7_1_0, 1_3_0, 8_0_2, 1_4_6], [3_3_6, 1_7_1, 4_1_2, 1_8_3], [4_2_3, 1_7_1, 5_7_2, 1_8_3], [5_8_2, 1_7_0, 7_1_6, 1_8_4], [7_2_8, 1_7_1, 8_1_7, 1_8_7], [8_2_9, 1_7_1, 8_4_4, 1_8_6], [3_3_8, 1_9_7, 4_8_2, 2_1_2], [5_0_7, 1_9_6, 5_5_7, 2_0_9], [5_6_9, 1_9_6, 5_9_5, 2_0_8], [6_1_0, 1_9_6, 7_0_2, 2_0_9], [5_0_5, 2_1_4, 5_8_3, 2_2_6], [5_9_5, 2_1_4, 6_5_6, 2_2_7], [6_7_0, 2_1_5, 8_0_7, 2_2_7], [3_3_5, 2_5_9, 5_4_3, 2_7_4], [5_5_6, 2_5_9, 7_0_8, 2_7_2], [3_7_2, 2_7_9, 4_2_2, 2_9_1], [4_3_5, 2_7_9, 4_6_0, 2_9_1], [4_7_4, 2_7_9, 5_7_4, 2_9_2], [5_8_7, 2_7_8, 6_6_4, 2_9_1], [6_7_6, 2_7_8, 7_3_8, 2_9_1], [7_5_1, 2_7_9, 8_3_4, 2_9_1], [3_7_2, 2_9_8, 4_3_4, 3_1_0], [3_3_5, 3_4_1, 4_8_3, 3_5_4], [4_9_7, 3_4_1, 6_5_5, 3_5_4], [6_6_7, 3_4_1, 7_2_8, 3_5_4], [7_4_0, 3_4_1, 8_2_5, 3_5_4], [3_3_5, 3_6_0, 4_3_0, 3_7_2], [4_4_2, 3_6_0, 5_3_4, 3_7_2], [5_4_5, 3_5_9, 6_8_7, 3_7_2], [6_9_7, 3_6_0, 7_5_4, 3_7_2], [7_6_5, 3_6_0, 8_2_3, 3_7_3], [3_3_4, 3_7_8, 4_2_8, 3_9_1], [4_4_0, 3_7_8, 5_7_7, 3_9_4], [5_9_0, 3_7_8, 7_0_5, 3_9_1], [7_2_0, 3_7_8, 8_0_1, 3_9_1], [3_3_4, 3_9_7, 4_0_0, 4_0_9], [3_7_0, 4_1_6, 5_2_9, 4_2_9], [5_4_4, 4_1_6, 5_7_6, 4_3_2], [5_8_7, 4_1_6, 6_6_5, 4_2_8], [6_7_7, 4_1_6, 8_1_4, 4_2_9], [3_7_2, 4_3_5, 4_5_2, 4_5_0], [4_6_5, 4_3_4, 4_9_5, 4_4_7], [5_1_1, 4_3_4, 6_0_0, 4_4_7], [6_1_1, 4_3_6, 6_3_7, 4_4_7], [6_4_9, 4_3_6, 6_9_4, 4_5_1], [7_0_5, 4_3_8, 8_2_4, 4_4_7], [3_6_9, 4_5_3, 4_5_2, 4_6_6], [4_6_4, 4_5_4, 5_0_9, 4_6_6], [5_2_2, 4_5_3, 6_1_1, 4_6_9], [6_2_5, 4_5_3, 7_9_2, 4_6_9], [3_7_0, 4_7_2, 5_5_6, 4_8_8], [5_7_0, 4_7_2, 6_8_4, 4_8_7], [6_9_7, 4_7_2, 7_1_8, 4_8_5], [7_3_2, 4_7_2, 8_3_5, 4_8_8], [3_6_9, 4_9_0, 4_1_1, 5_0_3], [4_2_5, 4_9_0, 4_8_4, 5_0_3], [4_9_6, 4_9_0, 6_3_5, 5_0_6], [6_4_5, 4_9_0, 7_0_7, 5_0_3], [7_1_8, 4_9_1, 7_6_1, 5_0_3], [7_7_1, 4_9_0, 8_4_0, 5_0_3], [3_3_6, 5_1_0, 3_7_4, 5_2_1], [3_8_8, 5_1_0, 4_4_7, 5_2_2], [4_6_0, 5_1_0, 4_8_9, 5_2_1], [5_0_3, 5_1_0, 5_8_0, 5_2_2], [5_9_2, 5_0_9, 7_3_6, 5_2_5], [7_4_5, 5_0_9, 7_7_0, 5_2_2], [7_8_1, 5_0_9, 8_4_0, 5_2_2], [3_3_8, 5_2_8, 4_3_4, 5_4_1], [4_4_8, 5_2_8, 5_9_6, 5_4_1], [6_0_9, 5_2_7, 6_8_7, 5_4_0], [7_0_0, 5_2_8, 7_9_2, 5_4_1], [3_3_6, 5_4_6, 3_9_7, 5_5_9], [4_0_7, 5_4_6, 4_3_1, 5_5_9], [4_4_3, 5_4_6, 5_2_5, 5_6_0], [5_3_7, 5_4_6, 6_8_0, 5_6_2], [6_8_8, 5_4_6, 7_1_4, 5_5_9], [7_2_2, 5_4_6, 8_3_7, 5_6_2], [3_3_6, 5_6_5, 4_4_9, 5_8_1], [4_6_1, 5_6_5, 4_8_5, 5_7_7], [4_9_7, 5_6_5, 6_6_5, 5_8_1], [6_8_1, 5_6_5, 7_1_8, 5_7_7], [7_3_2, 5_6_5, 8_3_7, 5_8_0], [3_3_7, 5_8_4, 4_3_8, 5_9_7], [4_5_2, 5_8_3, 5_2_1, 5_9_6], [5_3_5, 5_8_4, 6_7_7, 5_9_9], [6_9_0, 5_8_3, 7_8_7, 5_9_6], [8_0_1, 5_8_3, 8_2_5, 5_9_6], [3_3_8, 6_0_2, 4_7_8, 6_1_5], [4_9_2, 6_0_2, 5_3_0, 6_1_4], [5_4_3, 6_0_2, 6_3_8, 6_1_5], [6_5_0, 6_0_2, 6_7_6, 6_1_4], [6_8_8, 6_0_2, 7_8_8, 6_1_5], [8_0_2, 6_0_2, 8_4_3, 6_1_4], [3_3_7, 6_2_1, 5_0_2, 6_3_3], [5_1_6, 6_2_1, 6_1_5, 6_3_7], [6_2_9, 6_2_1, 7_7_4, 6_3_6], [7_8_9, 6_2_1, 8_2_7, 6_3_3], [3_3_7, 6_3_9, 4_1_8, 6_5_2], [4_3_2, 6_4_0, 5_7_1, 6_5_3], [5_8_7, 6_3_9, 7_3_1, 6_5_5], [7_4_3, 6_3_9, 7_6_9, 6_5_2], [7_8_0, 6_3_9, 8_4_1, 6_5_2], [3_3_8, 6_5_8, 4_4_0, 6_7_3], [4_5_5, 6_5_8, 4_9_1, 6_7_0], [5_0_8, 6_5_8, 6_0_2, 6_7_1], [6_1_6, 6_5_8, 6_3_8, 6_7_0], [6_5_4, 6_5_8, 8_3_5, 6_7_4], [3_3_7, 6_7_7, 4_2_9, 6_8_9], [3_3_7, 7_1_4, 4_8_2, 7_2_6], [4_9_5, 7_1_4, 5_4_8, 7_2_6], [5_6_1, 7_1_4, 6_8_3, 7_2_6], [3_3_8, 7_7_0, 4_6_1, 7_8_2], [4_7_4, 7_6_9, 5_5_4, 7_8_5], [4_8_9, 7_8_8, 5_6_2, 8_0_3], [5_7_6, 7_8_8, 6_4_3, 8_0_1], [6_5_6, 7_8_7, 7_5_1, 8_0_4], [7_6_4, 7_8_8, 8_4_4, 8_0_1], [3_3_4, 8_2_5, 4_2_1, 8_3_8], [4_3_0, 8_2_4, 5_7_4, 8_3_8], [5_8_4, 8_2_4, 7_2_3, 8_4_1], [3_3_5, 8_4_4, 4_5_0, 8_5_7], [4_6_4, 8_4_3, 5_8_3, 8_6_0], [6_2_8, 8_6_2, 7_5_5, 8_7_5], [7_6_9, 8_6_1, 8_4_8, 8_7_8]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,_UpperCamelCase ) self.assertListEqual(encoding.boxes ,_UpperCamelCase ) # with apply_OCR = False snake_case_ : Dict = LayoutLMvaImageProcessor(apply_ocr=_UpperCamelCase ) snake_case_ : Optional[int] = image_processing(_UpperCamelCase ,return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 2_2_4, 2_2_4) )
8
0
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' snake_case_ : Dict = [int(__lowerCAmelCase ) for i in ip_va_address.split(""".""" ) if i.isdigit()] return len(__lowerCAmelCase ) == 4 and all(0 <= int(__lowerCAmelCase ) <= 2_54 for octet in octets ) if __name__ == "__main__": __A : List[Any] = input().strip() __A : Tuple = 'valid' if is_ip_va_address_valid(ip) else 'invalid' print(F'{ip} is a {valid_or_invalid} IP v4 address.')
371
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : List[Any] = generate_pascal_triangle(lowerCamelCase_ ) for row_idx in range(lowerCamelCase_ ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=""" """ ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=""" """ ) else: print(triangle[row_idx][col_idx] , end="""""" ) print() def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError("""The input value of 'num_rows' should be 'int'""" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( """The input value of 'num_rows' should be greater than or equal to 0""" ) snake_case_ : list[list[int]] = [] for current_row_idx in range(lowerCamelCase_ ): snake_case_ : List[str] = populate_current_row(lowerCamelCase_ , lowerCamelCase_ ) triangle.append(lowerCamelCase_ ) return triangle def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : Union[str, Any] = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 snake_case_ , snake_case_ : Optional[Any] = 1, 1 for current_col_idx in range(1 , lowerCamelCase_ ): calculate_current_element( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) return current_row def UpperCAmelCase ( lowerCamelCase_ :list[list[int]] , lowerCamelCase_ :list[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , ): '''simple docstring''' snake_case_ : Union[str, Any] = triangle[current_row_idx - 1][current_col_idx - 1] snake_case_ : List[Any] = triangle[current_row_idx - 1][current_col_idx] snake_case_ : Optional[int] = above_to_left_elt + above_to_right_elt def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError("""The input value of 'num_rows' should be 'int'""" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( """The input value of 'num_rows' should be greater than or equal to 0""" ) snake_case_ : list[list[int]] = [[1]] for row_index in range(1 , lowerCamelCase_ ): snake_case_ : Optional[Any] = [0] + result[-1] + [0] snake_case_ : Dict = row_index + 1 # Calculate the number of distinct elements in a row snake_case_ : Any = sum(divmod(lowerCamelCase_ , 2 ) ) snake_case_ : Tuple = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] snake_case_ : Optional[int] = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() snake_case_ : str = row_first_half + row_second_half result.append(lowerCamelCase_ ) return result def UpperCAmelCase ( ): '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(lowerCamelCase_ :Callable , lowerCamelCase_ :int ) -> None: snake_case_ : Dict = F'''{func.__name__}({value})''' snake_case_ : Dict = timeit(F'''__main__.{call}''' , setup="""import __main__""" ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(F'''{call:38} -- {timing:.4f} seconds''' ) for value in range(15 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(lowerCamelCase_ , lowerCamelCase_ ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
8
0
'''simple docstring''' import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __UpperCamelCase : def __init__( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :List[Any]=1_4 ,_UpperCamelCase :Tuple=7 ,_UpperCamelCase :Dict=True ,_UpperCamelCase :str=True ,_UpperCamelCase :Union[str, Any]=True ,_UpperCamelCase :Dict=True ,_UpperCamelCase :Any=True ,_UpperCamelCase :Union[str, Any]=9_9 ,_UpperCamelCase :List[Any]=3_2 ,_UpperCamelCase :int=5 ,_UpperCamelCase :List[str]=4 ,_UpperCamelCase :Union[str, Any]=3_7 ,_UpperCamelCase :Any="gelu" ,_UpperCamelCase :Any=0.1 ,_UpperCamelCase :Optional[Any]=0.1 ,_UpperCamelCase :Union[str, Any]=5_1_2 ,_UpperCamelCase :Any=1_6 ,_UpperCamelCase :Dict=2 ,_UpperCamelCase :Dict=0.02 ,_UpperCamelCase :Union[str, Any]=3 ,_UpperCamelCase :List[Any]=4 ,_UpperCamelCase :Union[str, Any]=None ,): snake_case_ : Optional[int] = parent snake_case_ : Tuple = batch_size snake_case_ : List[str] = seq_length snake_case_ : Union[str, Any] = is_training snake_case_ : Dict = use_token_type_ids snake_case_ : Optional[int] = use_input_mask snake_case_ : Tuple = use_labels snake_case_ : str = use_mc_token_ids snake_case_ : Union[str, Any] = vocab_size snake_case_ : Optional[int] = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : int = intermediate_size snake_case_ : Tuple = hidden_act snake_case_ : Optional[int] = hidden_dropout_prob snake_case_ : Tuple = attention_probs_dropout_prob snake_case_ : str = max_position_embeddings snake_case_ : str = type_vocab_size snake_case_ : List[str] = type_sequence_label_size snake_case_ : List[Any] = initializer_range snake_case_ : Any = num_labels snake_case_ : Optional[Any] = num_choices snake_case_ : Optional[Any] = scope snake_case_ : List[Any] = self.vocab_size - 1 def a__ ( self :Optional[int] ): snake_case_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case_ : str = None if self.use_input_mask: snake_case_ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ : str = None if self.use_token_type_ids: snake_case_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) snake_case_ : Tuple = None if self.use_mc_token_ids: snake_case_ : Optional[Any] = ids_tensor([self.batch_size, self.num_choices] ,self.seq_length ) snake_case_ : Tuple = None snake_case_ : Dict = None snake_case_ : Dict = None if self.use_labels: snake_case_ : List[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) snake_case_ : List[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) snake_case_ : List[str] = ids_tensor([self.batch_size] ,self.num_choices ) snake_case_ : Tuple = self.get_config() snake_case_ : List[str] = ids_tensor([self.num_hidden_layers, self.num_attention_heads] ,2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def a__ ( self :Dict ): return CTRLConfig( vocab_size=self.vocab_size ,n_embd=self.hidden_size ,n_layer=self.num_hidden_layers ,n_head=self.num_attention_heads ,n_positions=self.max_position_embeddings ,pad_token_id=self.pad_token_id ,) def a__ ( self :Tuple ,_UpperCamelCase :Any ,_UpperCamelCase :str ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Optional[int] ,*_UpperCamelCase :Dict ): snake_case_ : Optional[int] = CTRLModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() model(_UpperCamelCase ,token_type_ids=_UpperCamelCase ,head_mask=_UpperCamelCase ) model(_UpperCamelCase ,token_type_ids=_UpperCamelCase ) snake_case_ : Optional[Any] = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) ,config.n_layer ) def a__ ( self :List[Any] ,_UpperCamelCase :Dict ,_UpperCamelCase :List[str] ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[int] ,*_UpperCamelCase :Tuple ): snake_case_ : Union[str, Any] = CTRLLMHeadModel(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ : List[Any] = model(_UpperCamelCase ,token_type_ids=_UpperCamelCase ,labels=_UpperCamelCase ) self.parent.assertEqual(result.loss.shape ,() ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def a__ ( self :int ): snake_case_ : Optional[Any] = self.prepare_config_and_inputs() ( snake_case_ ) : Any = config_and_inputs snake_case_ : str = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask} return config, inputs_dict def a__ ( self :Tuple ,_UpperCamelCase :Any ,_UpperCamelCase :int ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,*_UpperCamelCase :Any ): snake_case_ : Any = self.num_labels snake_case_ : Tuple = CTRLForSequenceClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ : List[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) snake_case_ : int = model(_UpperCamelCase ,token_type_ids=_UpperCamelCase ,labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) @require_torch class __UpperCamelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , unittest.TestCase ): lowercase : Optional[Any] = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () lowercase : Union[str, Any] = (CTRLLMHeadModel,) if is_torch_available() else () lowercase : Tuple = ( { 'feature-extraction': CTRLModel, 'text-classification': CTRLForSequenceClassification, 'text-generation': CTRLLMHeadModel, 'zero-shot': CTRLForSequenceClassification, } if is_torch_available() else {} ) lowercase : int = True lowercase : Any = False lowercase : Dict = False def a__ ( self :str ,_UpperCamelCase :Tuple ,_UpperCamelCase :List[str] ,_UpperCamelCase :Any ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Tuple ): if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def a__ ( self :Optional[int] ): snake_case_ : Tuple = CTRLModelTester(self ) snake_case_ : Optional[int] = ConfigTester(self ,config_class=_UpperCamelCase ,n_embd=3_7 ) def a__ ( self :List[str] ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def a__ ( self :List[Any] ): self.config_tester.run_common_tests() def a__ ( self :int ): snake_case_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_UpperCamelCase ) def a__ ( self :str ): snake_case_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_UpperCamelCase ) @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def a__ ( self :List[Any] ): pass @slow def a__ ( self :List[str] ): for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ : List[str] = CTRLModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) @unittest.skip("""The model doesn\'t support left padding""" ) # and it's not used enough to be worth fixing :) def a__ ( self :Tuple ): pass @require_torch class __UpperCamelCase ( unittest.TestCase ): def a__ ( self :str ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def a__ ( self :Optional[int] ): snake_case_ : Union[str, Any] = CTRLLMHeadModel.from_pretrained("""ctrl""" ) model.to(_UpperCamelCase ) snake_case_ : Tuple = torch.tensor( [[1_1_8_5_9, 0, 1_6_1_1, 8]] ,dtype=torch.long ,device=_UpperCamelCase ) # Legal the president is snake_case_ : List[str] = [ 1_1_8_5_9, 0, 1_6_1_1, 8, 5, 1_5_0, 2_6_4_4_9, 2, 1_9, 3_4_8, 4_6_9, 3, 2_5_9_5, 4_8, 2_0_7_4_0, 2_4_6_5_3_3, 2_4_6_5_3_3, 1_9, 3_0, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a snake_case_ : Optional[Any] = model.generate(_UpperCamelCase ,do_sample=_UpperCamelCase ) self.assertListEqual(output_ids[0].tolist() ,_UpperCamelCase )
350
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class __UpperCamelCase ( unittest.TestCase ): @slow def a__ ( self :Dict ): snake_case_ : Optional[Any] = XLMRobertaModel.from_pretrained("""xlm-roberta-base""" ) snake_case_ : Optional[int] = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house snake_case_ : Tuple = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim snake_case_ : Dict = torch.tensor( [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): snake_case_ : Tuple = model(_UpperCamelCase )["""last_hidden_state"""].detach() self.assertEqual(output.shape ,_UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,_UpperCamelCase ,atol=1E-3 ) ) @slow def a__ ( self :Union[str, Any] ): snake_case_ : List[Any] = XLMRobertaModel.from_pretrained("""xlm-roberta-large""" ) snake_case_ : Dict = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house snake_case_ : List[Any] = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim snake_case_ : Any = torch.tensor( [[-0.06_99, -0.03_18, 0.07_05, -0.12_41, 0.09_99, -0.05_20, 0.10_04, -0.18_38, -0.47_04, 0.14_37, 0.08_21, 0.01_26]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): snake_case_ : str = model(_UpperCamelCase )["""last_hidden_state"""].detach() self.assertEqual(output.shape ,_UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] ,_UpperCamelCase ,atol=1E-3 ) )
8
0
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import is_flaky, require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DonutImageProcessor class __UpperCamelCase ( unittest.TestCase ): def __init__( self :Optional[Any] ,_UpperCamelCase :Dict ,_UpperCamelCase :Any=7 ,_UpperCamelCase :Dict=3 ,_UpperCamelCase :Tuple=1_8 ,_UpperCamelCase :str=3_0 ,_UpperCamelCase :Optional[Any]=4_0_0 ,_UpperCamelCase :int=True ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :Optional[int]=True ,_UpperCamelCase :List[Any]=False ,_UpperCamelCase :int=True ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :Union[str, Any]=[0.5, 0.5, 0.5] ,_UpperCamelCase :Union[str, Any]=[0.5, 0.5, 0.5] ,): snake_case_ : int = parent snake_case_ : Optional[Any] = batch_size snake_case_ : List[Any] = num_channels snake_case_ : Tuple = image_size snake_case_ : Dict = min_resolution snake_case_ : Optional[int] = max_resolution snake_case_ : List[str] = do_resize snake_case_ : Union[str, Any] = size if size is not None else {"""height""": 1_8, """width""": 2_0} snake_case_ : List[Any] = do_thumbnail snake_case_ : List[str] = do_align_axis snake_case_ : str = do_pad snake_case_ : Optional[int] = do_normalize snake_case_ : Dict = image_mean snake_case_ : List[Any] = image_std def a__ ( self :Optional[Any] ): return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __UpperCamelCase ( lowerCAmelCase_ , unittest.TestCase ): lowercase : Optional[Any] = DonutImageProcessor if is_vision_available() else None def a__ ( self :int ): snake_case_ : Tuple = DonutImageProcessingTester(self ) @property def a__ ( self :List[Any] ): return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self :Any ): snake_case_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE ,"""do_resize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE ,"""size""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE ,"""do_thumbnail""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE ,"""do_align_long_axis""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE ,"""do_pad""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE ,"""do_normalize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE ,"""image_mean""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE ,"""image_std""" ) ) def a__ ( self :str ): snake_case_ : Dict = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{"""height""": 1_8, """width""": 2_0} ) snake_case_ : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ,size=4_2 ) self.assertEqual(image_processor.size ,{"""height""": 4_2, """width""": 4_2} ) # Previous config had dimensions in (width, height) order snake_case_ : str = self.image_processing_class.from_dict(self.image_processor_dict ,size=(4_2, 8_4) ) self.assertEqual(image_processor.size ,{"""height""": 8_4, """width""": 4_2} ) def a__ ( self :Any ): pass @is_flaky() def a__ ( self :Any ): snake_case_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ : Any = prepare_image_inputs(self.image_processor_tester ,equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE ,Image.Image ) # Test not batched input snake_case_ : str = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : Optional[Any] = image_processing(__SCREAMING_SNAKE_CASE ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) @is_flaky() def a__ ( self :List[Any] ): snake_case_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ : Any = prepare_image_inputs(self.image_processor_tester ,equal_resolution=__SCREAMING_SNAKE_CASE ,numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE ,np.ndarray ) # Test not batched input snake_case_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : List[str] = image_processing(__SCREAMING_SNAKE_CASE ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) @is_flaky() def a__ ( self :int ): snake_case_ : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ : Optional[int] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=__SCREAMING_SNAKE_CASE ,torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE ,torch.Tensor ) # Test not batched input snake_case_ : Optional[Any] = image_processing(image_inputs[0] ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,) # Test batched snake_case_ : str = image_processing(__SCREAMING_SNAKE_CASE ,return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) ,)
351
'''simple docstring''' from __future__ import annotations from collections.abc import Callable def UpperCAmelCase ( lowerCamelCase_ :Callable[[int | float], int | float] , lowerCamelCase_ :int | float , lowerCamelCase_ :int | float , lowerCamelCase_ :int = 1_00 , ): '''simple docstring''' snake_case_ : Tuple = x_start snake_case_ : Optional[int] = fnc(lowerCamelCase_ ) snake_case_ : Optional[int] = 0.0 for _ in range(lowerCamelCase_ ): # Approximates small segments of curve as linear and solve # for trapezoidal area snake_case_ : int = (x_end - x_start) / steps + xa snake_case_ : Union[str, Any] = fnc(lowerCamelCase_ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step snake_case_ : Any = xa snake_case_ : str = fxa return area if __name__ == "__main__": def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return x**3 + x**2 print('f(x) = x^3 + x^2') print('The area between the curve, x = -5, x = 5 and the x axis is:') __A : List[str] = 10 while i <= 100_000: print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}') i *= 10
8
0
'''simple docstring''' import os import time import numpy as np import onnxruntime as ort __A : List[Any] = '1' __A : Optional[int] = '0' __A : int = '1' __A : List[Any] = ort.SessionOptions() __A : Dict = ort.GraphOptimizationLevel.ORT_DISABLE_ALL print('Create inference session...') __A : List[Any] = ['TensorrtExecutionProvider', 'CUDAExecutionProvider'] __A : Optional[int] = ort.InferenceSession('model.onnx', sess_options=sess_opt, providers=execution_provider) __A : Union[str, Any] = ort.RunOptions() __A : Dict = 128 __A : Optional[Any] = 1 __A : Optional[Any] = np.ones((batch, sequence), dtype=np.intaa) __A : Dict = np.ones((batch, sequence), dtype=np.intaa) __A : List[str] = np.ones((batch, sequence), dtype=np.intaa) print('Warm up phase...') sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('Start inference...') __A : Dict = time.time() __A : Optional[Any] = 2_000 __A : Dict = {} for iter in range(max_iters): __A : Any = sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('Average Inference Time = {:.3f} ms'.format((time.time() - start_time) * 1_000 / max_iters))
352
'''simple docstring''' import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) __A : int = logging.getLogger() def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[Any] = argparse.ArgumentParser() parser.add_argument("""-f""" ) snake_case_ : int = parser.parse_args() return args.f def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[Any] = {} snake_case_ : Optional[Any] = os.path.join(lowerCamelCase_ , """all_results.json""" ) if os.path.exists(lowerCamelCase_ ): with open(lowerCamelCase_ , """r""" ) as f: snake_case_ : str = json.load(lowerCamelCase_ ) else: raise ValueError(F'''can\'t find {path}''' ) return results def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : List[str] = torch.cuda.is_available() and torch_device == """cuda""" return is_using_cuda and is_apex_available() __A : Any = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __UpperCamelCase ( lowercase__ ): @classmethod def a__ ( cls :Dict ): # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU snake_case_ : Optional[int] = tempfile.mkdtemp() snake_case_ : Any = os.path.join(cls.tmpdir ,"""default_config.yml""" ) write_basic_config(save_location=cls.configPath ) snake_case_ : List[Any] = ["""accelerate""", """launch""", """--config_file""", cls.configPath] @classmethod def a__ ( cls :int ): shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Optional[int] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.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 --seed=42 --checkpointing_steps epoch --with_tracking '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : Dict = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""glue_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking '''.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,1_0_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""clm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Tuple ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[str] = F''' {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.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} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertLess(result["""perplexity"""] ,4_2 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""mlm_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu snake_case_ : Dict = 7 if get_gpu_count() > 1 else 2 snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : str = F''' {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.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} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Optional[int] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.75 ) self.assertLess(result["""train_loss"""] ,0.5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""ner_no_trainer""" ) ) ) @unittest.skip(reason="""Fix me @muellerzr""" ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[str] ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : Optional[int] = F''' {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.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} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result["""eval_f1"""] ,2_8 ) self.assertGreaterEqual(result["""eval_exact"""] ,2_8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""qa_no_trainer""" ) ) ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :List[Any] ): snake_case_ : str = self.get_auto_remove_tmp_dir() snake_case_ : Union[str, Any] = F''' {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Union[str, Any] = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] ,0.8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""swag_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : List[Any] = self.get_auto_remove_tmp_dir() snake_case_ : List[Any] = F''' {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.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 --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : int = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_rouge1"""] ,1_0 ) self.assertGreaterEqual(result["""eval_rouge2"""] ,2 ) self.assertGreaterEqual(result["""eval_rougeL"""] ,7 ) self.assertGreaterEqual(result["""eval_rougeLsum"""] ,7 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""summarization_no_trainer""" ) ) ) @slow @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :int ): snake_case_ : Tuple = self.get_auto_remove_tmp_dir() snake_case_ : Optional[Any] = F''' {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) snake_case_ : Any = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_bleu"""] ,3_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""epoch_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""translation_no_trainer""" ) ) ) @slow def a__ ( self :Optional[Any] ): snake_case_ : List[str] = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCamelCase ) snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch '''.split() run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["""eval_overall_accuracy"""] ,0.10 ) @mock.patch.dict(os.environ ,{"""WANDB_MODE""": """offline"""} ) def a__ ( self :Any ): snake_case_ : Dict = self.get_auto_remove_tmp_dir() snake_case_ : Tuple = F''' {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 '''.split() if is_cuda_and_apex_available(): testargs.append("""--fp16""" ) run_command(self._launch_args + testargs ) snake_case_ : str = get_results(_UpperCamelCase ) # The base model scores a 25% self.assertGreaterEqual(result["""eval_accuracy"""] ,0.6 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""step_1""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase ,"""image_classification_no_trainer""" ) ) )
8
0
'''simple docstring''' import argparse from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase ( lowerCamelCase_ :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : Optional[int] = TaConfig.from_json_file(_lowerCAmelCase ) print(F'''Building PyTorch model from configuration: {config}''' ) snake_case_ : Optional[int] = TaForConditionalGeneration(_lowerCAmelCase ) # Load weights from tf checkpoint load_tf_weights_in_ta(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) model.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": __A : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained T5 model. \nThis specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __A : Optional[int] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
353
'''simple docstring''' from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __A : Tuple = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): lowercase : str = ['input_values', 'padding_mask'] def __init__( self :Optional[int] ,_UpperCamelCase :int = 1 ,_UpperCamelCase :int = 2_4_0_0_0 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :float = None ,_UpperCamelCase :float = None ,**_UpperCamelCase :List[Any] ,): super().__init__(feature_size=_UpperCamelCase ,sampling_rate=_UpperCamelCase ,padding_value=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Dict = chunk_length_s snake_case_ : str = overlap @property def a__ ( self :Any ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def a__ ( self :List[str] ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 ,int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self :Optional[Any] ,_UpperCamelCase :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,_UpperCamelCase :Optional[Union[bool, str, PaddingStrategy]] = None ,_UpperCamelCase :Optional[bool] = False ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :Optional[Union[str, TensorType]] = None ,_UpperCamelCase :Optional[int] = None ,): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) if padding and truncation: raise ValueError("""Both padding and truncation were set. Make sure you only set one.""" ) elif padding is None: # by default let's pad the inputs snake_case_ : Tuple = True snake_case_ : str = bool( isinstance(_UpperCamelCase ,(list, tuple) ) and (isinstance(raw_audio[0] ,(np.ndarray, tuple, list) )) ) if is_batched: snake_case_ : Any = [np.asarray(_UpperCamelCase ,dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_UpperCamelCase ,np.ndarray ): snake_case_ : Optional[int] = np.asarray(_UpperCamelCase ,dtype=np.floataa ) elif isinstance(_UpperCamelCase ,np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): snake_case_ : List[str] = raw_audio.astype(np.floataa ) # always return batch if not is_batched: snake_case_ : Optional[Any] = [np.asarray(_UpperCamelCase ).T] # verify inputs are valid for idx, example in enumerate(_UpperCamelCase ): if example.ndim > 2: raise ValueError(F'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(F'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(F'''Expected stereo audio but example has {example.shape[-1]} channels''' ) snake_case_ : Tuple = None snake_case_ : Optional[Any] = BatchFeature({"""input_values""": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: snake_case_ : Union[str, Any] = min(array.shape[0] for array in raw_audio ) snake_case_ : Dict = int(np.floor(max_length / self.chunk_stride ) ) snake_case_ : Union[str, Any] = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: snake_case_ : Any = max(array.shape[0] for array in raw_audio ) snake_case_ : List[Any] = int(np.ceil(max_length / self.chunk_stride ) ) snake_case_ : Any = (nb_step - 1) * self.chunk_stride + self.chunk_length snake_case_ : Union[str, Any] = """max_length""" else: snake_case_ : int = input_values # normal padding on batch if padded_inputs is None: snake_case_ : Optional[int] = self.pad( _UpperCamelCase ,max_length=_UpperCamelCase ,truncation=_UpperCamelCase ,padding=_UpperCamelCase ,return_attention_mask=_UpperCamelCase ,) if padding: snake_case_ : Tuple = padded_inputs.pop("""attention_mask""" ) snake_case_ : Optional[int] = [] for example in padded_inputs.pop("""input_values""" ): if self.feature_size == 1: snake_case_ : Dict = example[..., None] input_values.append(example.T ) snake_case_ : List[Any] = input_values if return_tensors is not None: snake_case_ : Tuple = padded_inputs.convert_to_tensors(_UpperCamelCase ) return padded_inputs
8
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __A : List[Any] = { 'configuration_xlm': ['XLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMConfig', 'XLMOnnxConfig'], 'tokenization_xlm': ['XLMTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = [ 'XLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMForMultipleChoice', 'XLMForQuestionAnswering', 'XLMForQuestionAnsweringSimple', 'XLMForSequenceClassification', 'XLMForTokenClassification', 'XLMModel', 'XLMPreTrainedModel', 'XLMWithLMHeadModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = [ 'TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFXLMForMultipleChoice', 'TFXLMForQuestionAnsweringSimple', 'TFXLMForSequenceClassification', 'TFXLMForTokenClassification', 'TFXLMMainLayer', 'TFXLMModel', 'TFXLMPreTrainedModel', 'TFXLMWithLMHeadModel', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys __A : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
354
'''simple docstring''' from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig __A : Dict = { 'susnato/ernie-m-base_pytorch': 'https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json', 'susnato/ernie-m-large_pytorch': 'https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json', } class __UpperCamelCase ( lowercase__ ): lowercase : Optional[int] = 'ernie_m' lowercase : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self :Optional[Any] ,_UpperCamelCase :int = 2_5_0_0_0_2 ,_UpperCamelCase :int = 7_6_8 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 3_0_7_2 ,_UpperCamelCase :str = "gelu" ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :int = 5_1_4 ,_UpperCamelCase :float = 0.02 ,_UpperCamelCase :int = 1 ,_UpperCamelCase :float = 1E-0_5 ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :List[str]=False ,_UpperCamelCase :Optional[int]=0.0 ,**_UpperCamelCase :List[Any] ,): super().__init__(pad_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Optional[int] = vocab_size snake_case_ : Any = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : Union[str, Any] = num_attention_heads snake_case_ : Any = intermediate_size snake_case_ : Any = hidden_act snake_case_ : Tuple = hidden_dropout_prob snake_case_ : Union[str, Any] = attention_probs_dropout_prob snake_case_ : str = max_position_embeddings snake_case_ : int = initializer_range snake_case_ : Optional[Any] = layer_norm_eps snake_case_ : Union[str, Any] = classifier_dropout snake_case_ : Tuple = is_decoder snake_case_ : int = act_dropout
8
0
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ): '''simple docstring''' snake_case_ : Union[str, Any] = 0 while b > 0: if b & 1: res += a a += a b >>= 1 return res def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[Any] ): '''simple docstring''' snake_case_ : int = 0 while b > 0: if b & 1: snake_case_ : Any = ((res % c) + (a % c)) % c a += a b >>= 1 return res
355
'''simple docstring''' from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class __UpperCamelCase ( nn.Module ): def __init__( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = True ,_UpperCamelCase :str = "layer_norm" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Any = only_cross_attention snake_case_ : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" snake_case_ : Any = (num_embeds_ada_norm is not None) and norm_type == """ada_norm""" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F'''`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to''' F''' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.''' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: snake_case_ : Dict = AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ : str = AdaLayerNormZero(_UpperCamelCase ,_UpperCamelCase ) else: snake_case_ : List[Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=_UpperCamelCase ,) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. snake_case_ : str = ( AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,upcast_attention=_UpperCamelCase ,) # is self-attn if encoder_hidden_states is none else: snake_case_ : Any = None snake_case_ : Optional[Any] = None # 3. Feed-forward snake_case_ : List[str] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : Union[str, Any] = FeedForward(_UpperCamelCase ,dropout=_UpperCamelCase ,activation_fn=_UpperCamelCase ,final_dropout=_UpperCamelCase ) # let chunk size default to None snake_case_ : Optional[int] = None snake_case_ : Dict = 0 def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :int ): # Sets chunk feed-forward snake_case_ : Optional[Any] = chunk_size snake_case_ : Optional[Any] = dim def a__ ( self :List[str] ,_UpperCamelCase :torch.FloatTensor ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,_UpperCamelCase :Dict[str, Any] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = self.norma( _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=hidden_states.dtype ) else: snake_case_ : Optional[int] = self.norma(_UpperCamelCase ) snake_case_ : int = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case_ : Union[str, Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_msa.unsqueeze(1 ) * attn_output snake_case_ : Union[str, Any] = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case_ : Any = ( self.norma(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else self.norma(_UpperCamelCase ) ) snake_case_ : List[Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=_UpperCamelCase ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Tuple = attn_output + hidden_states # 3. Feed-forward snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Dict = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F'''`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.''' ) snake_case_ : Union[str, Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case_ : int = torch.cat( [self.ff(_UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase ,dim=self._chunk_dim )] ,dim=self._chunk_dim ,) else: snake_case_ : List[str] = self.ff(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case_ : Any = ff_output + hidden_states return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Dict ,_UpperCamelCase :int ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :int = 4 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Tuple = int(dim * mult ) snake_case_ : Optional[int] = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case_ : Any = GELU(_UpperCamelCase ,_UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case_ : Tuple = GELU(_UpperCamelCase ,_UpperCamelCase ,approximate="""tanh""" ) elif activation_fn == "geglu": snake_case_ : Dict = GEGLU(_UpperCamelCase ,_UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case_ : Optional[Any] = ApproximateGELU(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Dict = nn.ModuleList([] ) # project in self.net.append(_UpperCamelCase ) # project dropout self.net.append(nn.Dropout(_UpperCamelCase ) ) # project out self.net.append(nn.Linear(_UpperCamelCase ,_UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase ) ) def a__ ( self :Tuple ,_UpperCamelCase :Union[str, Any] ): for module in self.net: snake_case_ : Tuple = module(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :str = "none" ): super().__init__() snake_case_ : Union[str, Any] = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Optional[Any] = approximate def a__ ( self :str ,_UpperCamelCase :int ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ,approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ,approximate=self.approximate ).to(dtype=gate.dtype ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[Any] ): snake_case_ : Optional[Any] = self.proj(_UpperCamelCase ) snake_case_ : int = self.gelu(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :List[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : str = nn.Linear(_UpperCamelCase ,dim_out * 2 ) def a__ ( self :Dict ,_UpperCamelCase :List[str] ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def a__ ( self :Optional[Any] ,_UpperCamelCase :Optional[int] ): snake_case_ , snake_case_ : Dict = self.proj(_UpperCamelCase ).chunk(2 ,dim=-1 ) return hidden_states * self.gelu(_UpperCamelCase ) class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[int] ): snake_case_ : int = self.proj(_UpperCamelCase ) return x * torch.sigmoid(1.7_02 * x ) class __UpperCamelCase ( nn.Module ): def __init__( self :int ,_UpperCamelCase :str ,_UpperCamelCase :List[Any] ): super().__init__() snake_case_ : int = nn.Embedding(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = nn.SiLU() snake_case_ : Any = nn.Linear(_UpperCamelCase ,embedding_dim * 2 ) snake_case_ : Dict = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) def a__ ( self :int ,_UpperCamelCase :List[str] ,_UpperCamelCase :int ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ) ) ) snake_case_ , snake_case_ : Tuple = torch.chunk(_UpperCamelCase ,2 ) snake_case_ : Tuple = self.norm(_UpperCamelCase ) * (1 + scale) + shift return x class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :Tuple ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = CombinedTimestepLabelEmbeddings(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : int = nn.SiLU() snake_case_ : List[str] = nn.Linear(_UpperCamelCase ,6 * embedding_dim ,bias=_UpperCamelCase ) snake_case_ : str = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ,eps=1E-6 ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str=None ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=_UpperCamelCase ) ) ) snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Any = emb.chunk(6 ,dim=1 ) snake_case_ : str = self.norm(_UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[int] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :Optional[str] = None ,_UpperCamelCase :float = 1E-5 ): super().__init__() snake_case_ : Optional[int] = num_groups snake_case_ : List[Any] = eps if act_fn is None: snake_case_ : int = None else: snake_case_ : Dict = get_activation(_UpperCamelCase ) snake_case_ : Optional[int] = nn.Linear(_UpperCamelCase ,out_dim * 2 ) def a__ ( self :List[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :List[str] ): if self.act: snake_case_ : Any = self.act(_UpperCamelCase ) snake_case_ : Optional[int] = self.linear(_UpperCamelCase ) snake_case_ : Dict = emb[:, :, None, None] snake_case_ , snake_case_ : str = emb.chunk(2 ,dim=1 ) snake_case_ : str = F.group_norm(_UpperCamelCase ,self.num_groups ,eps=self.eps ) snake_case_ : List[str] = x * (1 + scale) + shift return x
8
0
'''simple docstring''' import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' def wrapper(*lowerCamelCase_ :Dict , **lowerCamelCase_ :Dict ): snake_case_ : Optional[Any] = timeit.default_timer() snake_case_ : Dict = func(*lowerCamelCase_ , **lowerCamelCase_ ) snake_case_ : int = timeit.default_timer() - starttime return delta snake_case_ : Any = func.__name__ return wrapper def UpperCAmelCase ( lowerCamelCase_ :dict , lowerCamelCase_ :int=1_00 , lowerCamelCase_ :List[Any]=None ): '''simple docstring''' snake_case_ : Dict = [] snake_case_ : List[str] = seq_shapes or {} for i in range(lowerCamelCase_ ): snake_case_ : List[Any] = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(lowerCamelCase_ , _ArrayXD ): snake_case_ : Optional[int] = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(lowerCamelCase_ , datasets.Value ): if v.dtype == "string": snake_case_ : Any = '''The small grey turtle was surprisingly fast when challenged.''' else: snake_case_ : str = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(lowerCamelCase_ , datasets.Sequence ): while isinstance(lowerCamelCase_ , datasets.Sequence ): snake_case_ : Union[str, Any] = v.feature snake_case_ : Optional[Any] = seq_shapes[k] snake_case_ : int = np.random.rand(*lowerCamelCase_ ).astype(v.dtype ) snake_case_ : int = data dummy_data.append((i, example) ) return dummy_data def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any]=1_00 , lowerCamelCase_ :List[Any]=None ): '''simple docstring''' snake_case_ : Dict = generate_examples(lowerCamelCase_ , num_examples=lowerCamelCase_ , seq_shapes=lowerCamelCase_ ) with ArrowWriter(features=lowerCamelCase_ , path=lowerCamelCase_ ) as writer: for key, record in dummy_data: snake_case_ : Optional[Any] = features.encode_example(lowerCamelCase_ ) writer.write(lowerCamelCase_ ) snake_case_ : str = writer.finalize() if not num_final_examples == num_examples: raise ValueError( F'''Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.''' ) snake_case_ : Optional[Any] = datasets.Dataset.from_file(filename=lowerCamelCase_ , info=datasets.DatasetInfo(features=lowerCamelCase_ ) ) return dataset
356
'''simple docstring''' import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :str=True , lowerCamelCase_ :str="pt" ): '''simple docstring''' snake_case_ : Tuple = {"""add_prefix_space""": True} if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and not line.startswith(""" """ ) else {} snake_case_ : Union[str, Any] = padding_side return tokenizer( [line] , max_length=lowerCamelCase_ , padding="""max_length""" if pad_to_max_length else None , truncation=lowerCamelCase_ , return_tensors=lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , **lowerCamelCase_ , ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Any=None , ): '''simple docstring''' snake_case_ : Dict = input_ids.ne(lowerCamelCase_ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[Any] ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any="train" ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :int=None ,_UpperCamelCase :List[Any]=None ,_UpperCamelCase :Optional[int]="" ,): super().__init__() snake_case_ : List[str] = Path(_UpperCamelCase ).joinpath(type_path + """.source""" ) snake_case_ : int = Path(_UpperCamelCase ).joinpath(type_path + """.target""" ) snake_case_ : Optional[int] = self.get_char_lens(self.src_file ) snake_case_ : List[str] = max_source_length snake_case_ : str = max_target_length assert min(self.src_lens ) > 0, F'''found empty line in {self.src_file}''' snake_case_ : str = tokenizer snake_case_ : str = prefix if n_obs is not None: snake_case_ : int = self.src_lens[:n_obs] snake_case_ : Tuple = src_lang snake_case_ : str = tgt_lang def __len__( self :Any ): return len(self.src_lens ) def __getitem__( self :List[str] ,_UpperCamelCase :Union[str, Any] ): snake_case_ : Optional[int] = index + 1 # linecache starts at 1 snake_case_ : Dict = self.prefix + linecache.getline(str(self.src_file ) ,_UpperCamelCase ).rstrip("""\n""" ) snake_case_ : List[Any] = linecache.getline(str(self.tgt_file ) ,_UpperCamelCase ).rstrip("""\n""" ) assert source_line, F'''empty source line for index {index}''' assert tgt_line, F'''empty tgt line for index {index}''' # Need to add eos token manually for T5 if isinstance(self.tokenizer ,_UpperCamelCase ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right snake_case_ : int = ( self.tokenizer.question_encoder if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer ) snake_case_ : Optional[int] = self.tokenizer.generator if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer snake_case_ : Optional[Any] = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_source_length ,"""right""" ) snake_case_ : Tuple = encode_line(_UpperCamelCase ,_UpperCamelCase ,self.max_target_length ,"""right""" ) snake_case_ : int = source_inputs["""input_ids"""].squeeze() snake_case_ : str = target_inputs["""input_ids"""].squeeze() snake_case_ : Union[str, Any] = source_inputs["""attention_mask"""].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def a__ ( _UpperCamelCase :str ): return [len(_UpperCamelCase ) for x in Path(_UpperCamelCase ).open().readlines()] def a__ ( self :Optional[int] ,_UpperCamelCase :List[str] ): snake_case_ : Optional[Any] = torch.stack([x["""input_ids"""] for x in batch] ) snake_case_ : List[Any] = torch.stack([x["""attention_mask"""] for x in batch] ) snake_case_ : Union[str, Any] = torch.stack([x["""decoder_input_ids"""] for x in batch] ) snake_case_ : Optional[Any] = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Tuple = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer ,_UpperCamelCase ) else self.tokenizer.pad_token_id ) snake_case_ : Optional[int] = trim_batch(_UpperCamelCase ,_UpperCamelCase ) snake_case_ , snake_case_ : Dict = trim_batch(_UpperCamelCase ,_UpperCamelCase ,attention_mask=_UpperCamelCase ) snake_case_ : Optional[int] = { """input_ids""": source_ids, """attention_mask""": source_mask, """decoder_input_ids""": y, } return batch __A : List[Any] = getLogger(__name__) def UpperCAmelCase ( lowerCamelCase_ :List[List] ): '''simple docstring''' return list(itertools.chain.from_iterable(lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : int = get_git_info() save_json(lowerCamelCase_ , os.path.join(lowerCamelCase_ , """git_log.json""" ) ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int]=4 , **lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' with open(lowerCamelCase_ , """w""" ) as f: json.dump(lowerCamelCase_ , lowerCamelCase_ , indent=lowerCamelCase_ , **lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] ): '''simple docstring''' with open(lowerCamelCase_ ) as f: return json.load(lowerCamelCase_ ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Optional[Any] = git.Repo(search_parent_directories=lowerCamelCase_ ) snake_case_ : List[str] = { """repo_id""": str(lowerCamelCase_ ), """repo_sha""": str(repo.head.object.hexsha ), """repo_branch""": str(repo.active_branch ), """hostname""": str(socket.gethostname() ), } return repo_infos def UpperCAmelCase ( lowerCamelCase_ :Callable , lowerCamelCase_ :Iterable ): '''simple docstring''' return list(map(lowerCamelCase_ , lowerCamelCase_ ) ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , """wb""" ) as f: return pickle.dump(lowerCamelCase_ , lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Dict ): '''simple docstring''' def remove_articles(lowerCamelCase_ :str ): return re.sub(R"""\b(a|an|the)\b""" , """ """ , lowerCamelCase_ ) def white_space_fix(lowerCamelCase_ :Optional[Any] ): return " ".join(text.split() ) def remove_punc(lowerCamelCase_ :Tuple ): snake_case_ : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowerCamelCase_ :Optional[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase_ ) ) ) ) def UpperCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' snake_case_ : List[Any] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : Optional[int] = normalize_answer(lowerCamelCase_ ).split() snake_case_ : List[Any] = Counter(lowerCamelCase_ ) & Counter(lowerCamelCase_ ) snake_case_ : Optional[Any] = sum(common.values() ) if num_same == 0: return 0 snake_case_ : Optional[Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Union[str, Any] = 1.0 * num_same / len(lowerCamelCase_ ) snake_case_ : Optional[Any] = (2 * precision * recall) / (precision + recall) return fa def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' return normalize_answer(lowerCamelCase_ ) == normalize_answer(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] ): '''simple docstring''' assert len(lowerCamelCase_ ) == len(lowerCamelCase_ ) snake_case_ : Optional[int] = 0 for hypo, pred in zip(lowerCamelCase_ , lowerCamelCase_ ): em += exact_match_score(lowerCamelCase_ , lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: em /= len(lowerCamelCase_ ) return {"em": em} def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' return model_prefix.startswith("""rag""" ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead snake_case_ : Optional[int] = """dropout_rate""" for p in extra_params: if getattr(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): if not hasattr(lowerCamelCase_ , lowerCamelCase_ ) and not hasattr(lowerCamelCase_ , equivalent_param[p] ): logger.info("""config doesn't have a `{}` attribute""".format(lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) continue snake_case_ : str = p if hasattr(lowerCamelCase_ , lowerCamelCase_ ) else equivalent_param[p] setattr(lowerCamelCase_ , lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) return hparams, config
8
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __A : Optional[Any] = { 'configuration_mvp': ['MVP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MvpConfig', 'MvpOnnxConfig'], 'tokenization_mvp': ['MvpTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[str] = ['MvpTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Dict = [ 'MVP_PRETRAINED_MODEL_ARCHIVE_LIST', 'MvpForCausalLM', 'MvpForConditionalGeneration', 'MvpForQuestionAnswering', 'MvpForSequenceClassification', 'MvpModel', 'MvpPreTrainedModel', ] if TYPE_CHECKING: from .configuration_mvp import MVP_PRETRAINED_CONFIG_ARCHIVE_MAP, MvpConfig, MvpOnnxConfig from .tokenization_mvp import MvpTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mvp_fast import MvpTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mvp import ( MVP_PRETRAINED_MODEL_ARCHIVE_LIST, MvpForCausalLM, MvpForConditionalGeneration, MvpForQuestionAnswering, MvpForSequenceClassification, MvpModel, MvpPreTrainedModel, ) else: import sys __A : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
357
'''simple docstring''' import functools def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[str] = len(lowerCamelCase_ ) snake_case_ : Dict = len(lowerCamelCase_ ) @functools.cache def min_distance(lowerCamelCase_ :int , lowerCamelCase_ :int ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa snake_case_ : Union[str, Any] = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , lowerCamelCase_ ) , 1 + min_distance(lowerCamelCase_ , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
8
0
'''simple docstring''' import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class __UpperCamelCase ( pl.LightningModule ): def __init__( self :str ,_UpperCamelCase :Optional[Any] ): super().__init__() snake_case_ : Union[str, Any] = model snake_case_ : Tuple = 2 snake_case_ : List[str] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def a__ ( self :List[str] ): pass def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] ): '''simple docstring''' snake_case_ : List[str] = LongformerModel.from_pretrained(_UpperCAmelCase ) snake_case_ : int = LightningModel(_UpperCAmelCase ) snake_case_ : Union[str, Any] = torch.load(_UpperCAmelCase , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case_ : Any = LongformerForQuestionAnswering.from_pretrained(_UpperCAmelCase ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(_UpperCAmelCase ) print(F'''Conversion successful. Model saved under {pytorch_dump_folder_path}''' ) if __name__ == "__main__": __A : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '--longformer_model', default=None, type=str, required=True, help='model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.', ) parser.add_argument( '--longformer_question_answering_ckpt_path', default=None, type=str, required=True, help='Path the official PyTorch Lightning Checkpoint.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __A : str = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
358
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Any = tmp_path / """file.csv""" snake_case_ : Any = textwrap.dedent( """\ header1,header2 1,2 10,20 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : Optional[int] = tmp_path / """malformed_file.csv""" snake_case_ : int = textwrap.dedent( """\ header1,header2 1,2 10,20, """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int ): '''simple docstring''' snake_case_ : str = tmp_path / """csv_with_image.csv""" snake_case_ : int = textwrap.dedent( F'''\ image {image_file} ''' ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Any ): '''simple docstring''' snake_case_ : int = tmp_path / """csv_with_label.csv""" snake_case_ : Tuple = textwrap.dedent( """\ label good bad good """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = tmp_path / """csv_with_int_list.csv""" snake_case_ : str = textwrap.dedent( """\ int_list 1 2 3 4 5 6 7 8 9 """ ) with open(lowerCamelCase_ , """w""" ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :Tuple ): '''simple docstring''' snake_case_ : int = Csv() snake_case_ : Optional[Any] = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(lowerCamelCase_ , match="""Error tokenizing data""" ): for _ in generator: pass assert any( record.levelname == """ERROR""" and """Failed to read file""" in record.message and os.path.basename(lowerCamelCase_ ) in record.message for record in caplog.records ) @require_pil def UpperCAmelCase ( lowerCamelCase_ :Tuple ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : Tuple = f.read().splitlines()[1] snake_case_ : str = Csv(encoding="""utf-8""" , features=Features({"""image""": Image()} ) ) snake_case_ : Tuple = csv._generate_tables([[csv_file_with_image]] ) snake_case_ : Optional[Any] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""image""" ).type == Image()() snake_case_ : List[str] = pa_table.to_pydict()["""image"""] assert generated_content == [{"path": image_file, "bytes": None}] def UpperCAmelCase ( lowerCamelCase_ :int ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : List[Any] = f.read().splitlines()[1:] snake_case_ : Union[str, Any] = Csv(encoding="""utf-8""" , features=Features({"""label""": ClassLabel(names=["""good""", """bad"""] )} ) ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_label]] ) snake_case_ : Optional[int] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("""label""" ).type == ClassLabel(names=["""good""", """bad"""] )() snake_case_ : Union[str, Any] = pa_table.to_pydict()["""label"""] assert generated_content == [ClassLabel(names=["""good""", """bad"""] ).straint(lowerCamelCase_ ) for label in labels] def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] ): '''simple docstring''' snake_case_ : str = Csv(encoding="""utf-8""" , sep=""",""" , converters={"""int_list""": lambda lowerCamelCase_ : [int(lowerCamelCase_ ) for i in x.split()]} ) snake_case_ : Optional[Any] = csv._generate_tables([[csv_file_with_int_list]] ) snake_case_ : Tuple = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field("""int_list""" ).type ) snake_case_ : Dict = pa_table.to_pydict()["""int_list"""] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
8
0
import secrets from random import shuffle from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation def UpperCAmelCase ( lowerCamelCase_ :int = 8 ): '''simple docstring''' snake_case_ : Optional[Any] = ascii_letters + digits + punctuation return "".join(secrets.choice(a__ ) for _ in range(a__ ) ) def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :int ): '''simple docstring''' # Password Generator = full boot with random_number, random_letters, and # random_character FUNCTIONS # Put your code here... i -= len(a__ ) snake_case_ : Union[str, Any] = i // 3 snake_case_ : Optional[int] = i % 3 # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) snake_case_ : Optional[int] = ( chars_incl + random(a__ , quotient + remainder ) + random(a__ , a__ ) + random(a__ , a__ ) ) snake_case_ : Any = list(a__ ) shuffle(a__ ) return "".join(a__ ) # random is a generalised function for letters, characters and numbers def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :int ): '''simple docstring''' return "".join(secrets.choice(a__ ) for _ in range(a__ ) ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :Optional[Any] ): '''simple docstring''' pass # Put your code here... def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :Optional[int] ): '''simple docstring''' pass # Put your code here... def UpperCAmelCase ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str ): '''simple docstring''' pass # Put your code here... def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :int = 8 ): '''simple docstring''' if len(a__ ) < min_length: # Your Password must be at least 8 characters long return False snake_case_ : List[str] = any(char in ascii_uppercase for char in password ) snake_case_ : Any = any(char in ascii_lowercase for char in password ) snake_case_ : Optional[int] = any(char in digits for char in password ) snake_case_ : Optional[int] = any(char in punctuation for char in password ) return upper and lower and num and spec_char # Passwords should contain UPPERCASE, lowerase # numbers, and special characters def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Dict = int(input("""Please indicate the max length of your password: """ ).strip() ) snake_case_ : Dict = input( """Please indicate the characters that must be in your password: """ ).strip() print("""Password generated:""" , password_generator(a__ ) ) print( """Alternative Password generated:""" , alternative_password_generator(a__ , a__ ) , ) print("""[If you are thinking of using this passsword, You better save it.]""" ) if __name__ == "__main__": main()
359
'''simple docstring''' import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase ( lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple=None ): '''simple docstring''' # set parameter of one layer assert torch_layer.weight.shape == weight.shape, F'''{torch_layer} layer.weight does not match''' snake_case_ : Optional[Any] = nn.Parameter(lowerCamelCase_ ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F'''{torch_layer} layer.bias does not match''' snake_case_ : List[str] = nn.Parameter(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : Optional[Any] = np.asarray(weights[0] ) snake_case_ : int = np.asarray(weights[1] ) snake_case_ : Any = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[Any] ): '''simple docstring''' # set torch weights for 1-to-1 comparison snake_case_ : List[Any] = np.asarray(weights[0] ) snake_case_ : Optional[int] = np.asarray(weights[1] ) snake_case_ : Union[str, Any] = np.asarray(weights[2] ) snake_case_ : int = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.key , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.self_attention.value , torch.tensor(lowerCamelCase_ ).transpose(1 , 2 ).contiguous().view(-1 , lowerCamelCase_ ) , ) set_param( torch_layer.output.dense , torch.tensor(lowerCamelCase_ ).view(-1 , lowerCamelCase_ ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[int] ): '''simple docstring''' # layernorm 1 snake_case_ : str = weights[0][0][0] snake_case_ : int = np.asarray(layer_norm_a[0] ) snake_case_ : Optional[Any] = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # lsh weights + output snake_case_ : Tuple = weights[0][1] if len(lowerCamelCase_ ) < 4: set_layer_weights_in_torch_lsh(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) else: set_layer_weights_in_torch_local(lowerCamelCase_ , torch_block.attention , lowerCamelCase_ ) # intermediate weighs snake_case_ : str = weights[2][0][1][2] # Chunked Feed Forward if len(lowerCamelCase_ ) == 4: snake_case_ : List[Any] = intermediate_weights[2] # layernorm 2 snake_case_ : Tuple = np.asarray(intermediate_weights[0][0] ) snake_case_ : Optional[Any] = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # intermediate dense snake_case_ : Any = np.asarray(intermediate_weights[1][0] ) snake_case_ : List[Any] = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) # intermediate out snake_case_ : List[Any] = np.asarray(intermediate_weights[4][0] ) snake_case_ : Union[str, Any] = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Any ): '''simple docstring''' # reformer model snake_case_ : Dict = torch_model.reformer # word embeds snake_case_ : List[Any] = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(lowerCamelCase_ ) , ) if isinstance(weights[3] , lowerCamelCase_ ): snake_case_ : Tuple = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): snake_case_ : Dict = np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), F'''{position_embeddings[emb_idx]} emb does not match''' snake_case_ : Optional[Any] = nn.Parameter(torch.tensor(lowerCamelCase_ ) ) snake_case_ : List[Any] = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( lowerCamelCase_ ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): snake_case_ : str = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # output layer norm snake_case_ : Optional[Any] = np.asarray(weights[7][0] ) snake_case_ : List[Any] = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(lowerCamelCase_ ) , torch.tensor(lowerCamelCase_ ) , ) # output embeddings snake_case_ : Optional[int] = np.asarray(weights[9][0] ) snake_case_ : Any = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(lowerCamelCase_ ).transpose(0 , 1 ).contiguous() , torch.tensor(lowerCamelCase_ ) , ) def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] ): '''simple docstring''' # Initialise PyTorch model snake_case_ : List[str] = ReformerConfig.from_json_file(lowerCamelCase_ ) print(F'''Building PyTorch model from configuration: {config}''' ) snake_case_ : str = ReformerModelWithLMHead(lowerCamelCase_ ) with open(lowerCamelCase_ , """rb""" ) as f: snake_case_ : List[Any] = pickle.load(lowerCamelCase_ )["""weights"""] set_model_weights_in_torch(lowerCamelCase_ , lowerCamelCase_ , config.hidden_size ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , lowerCamelCase_ ) if __name__ == "__main__": __A : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--trax_model_pkl_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained Reformer model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __A : List[Any] = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
8
0
'''simple docstring''' import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __A : Any = 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') __A : List[str] = parser.parse_args() __A : Union[str, Any] = 'cpu' __A : int = 'a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings' __A : Tuple = 'path-to-your-trained-model' __A : Union[str, Any] = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __A : List[Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __A : Optional[Any] = pipe.to(device) # to channels last __A : int = pipe.unet.to(memory_format=torch.channels_last) __A : Optional[int] = pipe.vae.to(memory_format=torch.channels_last) __A : Optional[int] = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __A : Any = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __A : Tuple = torch.randn(2, 4, 64, 64) __A : Optional[int] = torch.rand(1) * 999 __A : List[str] = torch.randn(2, 77, 768) __A : Tuple = (sample, timestep, encoder_hidden_status) try: __A : List[Any] = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __A : Optional[Any] = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __A : Tuple = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __A : Optional[Any] = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __A : List[str] = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __A : Dict = 666 __A : Optional[Any] = torch.Generator(device).manual_seed(seed) __A : Union[str, Any] = {'generator': generator} if args.steps is not None: __A : int = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __A : Dict = pipe(prompt, **generate_kwargs).images[0] # save image image.save('generated.png')
360
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[Any] = logging.get_logger(__name__) __A : str = { 'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json', # See all CANINE models at https://huggingface.co/models?filter=canine } class __UpperCamelCase ( lowercase__ ): lowercase : List[Any] = 'canine' def __init__( self :Optional[int] ,_UpperCamelCase :Dict=7_6_8 ,_UpperCamelCase :Union[str, Any]=1_2 ,_UpperCamelCase :int=1_2 ,_UpperCamelCase :int=3_0_7_2 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :Any=0.1 ,_UpperCamelCase :int=0.1 ,_UpperCamelCase :Any=1_6_3_8_4 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :List[str]=0.02 ,_UpperCamelCase :Any=1E-1_2 ,_UpperCamelCase :Tuple=0 ,_UpperCamelCase :List[str]=0xE_0_0_0 ,_UpperCamelCase :Optional[Any]=0xE_0_0_1 ,_UpperCamelCase :str=4 ,_UpperCamelCase :Optional[int]=4 ,_UpperCamelCase :str=8 ,_UpperCamelCase :int=1_6_3_8_4 ,_UpperCamelCase :int=1_2_8 ,**_UpperCamelCase :str ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : List[str] = max_position_embeddings snake_case_ : Union[str, Any] = hidden_size snake_case_ : Dict = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : Tuple = intermediate_size snake_case_ : str = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : Optional[Any] = initializer_range snake_case_ : Optional[int] = type_vocab_size snake_case_ : List[str] = layer_norm_eps # Character config: snake_case_ : Any = downsampling_rate snake_case_ : List[str] = upsampling_kernel_size snake_case_ : int = num_hash_functions snake_case_ : Tuple = num_hash_buckets snake_case_ : Tuple = local_transformer_stride
8
0
'''simple docstring''' import re from filelock import FileLock try: import nltk __A : Dict = True except (ImportError, ModuleNotFoundError): __A : Optional[Any] = False if NLTK_AVAILABLE: with FileLock('.lock') as lock: nltk.download('punkt', quiet=True) def UpperCAmelCase ( lowerCamelCase_ :List[str] ): '''simple docstring''' re.sub("""<n>""" , """""" , __lowerCAmelCase ) # 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(__lowerCAmelCase ) )
361
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __A : Tuple = logging.get_logger(__name__) __A : List[Any] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } __A : str = { 'vocab_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json' }, 'merges_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt' }, 'tokenizer_config_file': { 'facebook/blenderbot_small-90M': ( 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json' ) }, } __A : Optional[Any] = { 'facebook/blenderbot_small-90M': 512, } class __UpperCamelCase ( lowercase__ ): lowercase : str = VOCAB_FILES_NAMES lowercase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : Dict = BlenderbotSmallTokenizer def __init__( self :str ,_UpperCamelCase :Optional[int]=None ,_UpperCamelCase :Union[str, Any]=None ,_UpperCamelCase :Tuple="<|endoftext|>" ,_UpperCamelCase :int="<|endoftext|>" ,_UpperCamelCase :Dict="<|endoftext|>" ,_UpperCamelCase :Optional[Any]=False ,_UpperCamelCase :List[Any]=True ,**_UpperCamelCase :Any ,): super().__init__( ByteLevelBPETokenizer( vocab=_UpperCamelCase ,merges=_UpperCamelCase ,add_prefix_space=_UpperCamelCase ,trim_offsets=_UpperCamelCase ,) ,bos_token=_UpperCamelCase ,eos_token=_UpperCamelCase ,unk_token=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Any = add_prefix_space def a__ ( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :Optional[Any]=None ): snake_case_ : List[Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def a__ ( self :int ,_UpperCamelCase :List[int] ,_UpperCamelCase :Optional[List[int]] = None ): snake_case_ : int = [self.sep_token_id] snake_case_ : Tuple = [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]
8
0
'''simple docstring''' from math import sqrt def UpperCAmelCase ( lowerCamelCase_ :Optional[int] ): '''simple docstring''' snake_case_ : Any = 0 for i in range(1 , int(sqrt(lowercase__ ) + 1 ) ): if n % i == 0 and i != sqrt(lowercase__ ): total += i + n // i elif i == sqrt(lowercase__ ): total += i return total - n def UpperCAmelCase ( lowerCamelCase_ :List[Any] = 1_00_00 ): '''simple docstring''' snake_case_ : int = sum( i for i in range(1 , lowercase__ ) if sum_of_divisors(sum_of_divisors(lowercase__ ) ) == i and sum_of_divisors(lowercase__ ) != i ) return total if __name__ == "__main__": print(solution(int(str(input()).strip())))
362
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :list ): '''simple docstring''' if len(lowerCamelCase_ ) <= 1: return lst snake_case_ : Union[str, Any] = 1 while i < len(lowerCamelCase_ ): if lst[i - 1] <= lst[i]: i += 1 else: snake_case_ , snake_case_ : Union[str, Any] = lst[i], lst[i - 1] i -= 1 if i == 0: snake_case_ : int = 1 return lst if __name__ == "__main__": __A : Optional[int] = input('Enter numbers separated by a comma:\n').strip() __A : int = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
8
0
'''simple docstring''' import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __UpperCamelCase ( __SCREAMING_SNAKE_CASE ): def a__ ( self :Any ): snake_case_ : Any = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,"""tf_padding""" ) ) self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE ,"""depth_multiplier""" ) ) class __UpperCamelCase : def __init__( self :Optional[int] ,_UpperCamelCase :Tuple ,_UpperCamelCase :str=1_3 ,_UpperCamelCase :Tuple=3 ,_UpperCamelCase :Optional[Any]=3_2 ,_UpperCamelCase :Optional[Any]=0.25 ,_UpperCamelCase :Union[str, Any]=8 ,_UpperCamelCase :Any=True ,_UpperCamelCase :Dict=1_0_2_4 ,_UpperCamelCase :Optional[int]=3_2 ,_UpperCamelCase :List[Any]="relu6" ,_UpperCamelCase :Dict=0.1 ,_UpperCamelCase :str=0.02 ,_UpperCamelCase :Optional[Any]=True ,_UpperCamelCase :List[Any]=True ,_UpperCamelCase :Union[str, Any]=1_0 ,_UpperCamelCase :Union[str, Any]=None ,): snake_case_ : Union[str, Any] = parent snake_case_ : Optional[Any] = batch_size snake_case_ : str = num_channels snake_case_ : Tuple = image_size snake_case_ : List[Any] = depth_multiplier snake_case_ : Any = min_depth snake_case_ : Any = tf_padding snake_case_ : Any = int(last_hidden_size * depth_multiplier ) snake_case_ : Optional[Any] = output_stride snake_case_ : Tuple = hidden_act snake_case_ : int = classifier_dropout_prob snake_case_ : Dict = use_labels snake_case_ : List[str] = is_training snake_case_ : Any = num_labels snake_case_ : Any = initializer_range snake_case_ : int = scope def a__ ( self :str ): snake_case_ : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ : Optional[Any] = None snake_case_ : List[str] = None if self.use_labels: snake_case_ : List[str] = ids_tensor([self.batch_size] ,self.num_labels ) snake_case_ : Union[str, Any] = ids_tensor([self.batch_size, self.image_size, self.image_size] ,self.num_labels ) snake_case_ : Union[str, Any] = self.get_config() return config, pixel_values, labels, pixel_labels def a__ ( self :Any ): return MobileNetVaConfig( num_channels=self.num_channels ,image_size=self.image_size ,depth_multiplier=self.depth_multiplier ,min_depth=self.min_depth ,tf_padding=self.tf_padding ,hidden_act=self.hidden_act ,classifier_dropout_prob=self.classifier_dropout_prob ,initializer_range=self.initializer_range ,) def a__ ( self :Any ,_UpperCamelCase :Tuple ,_UpperCamelCase :Any ,_UpperCamelCase :Any ,_UpperCamelCase :List[Any] ): snake_case_ : List[str] = MobileNetVaModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() snake_case_ : str = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.last_hidden_state.shape ,( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) ,) def a__ ( self :Any ,_UpperCamelCase :List[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :Dict ): snake_case_ : Optional[int] = self.num_labels snake_case_ : Optional[Any] = MobileNetVaForImageClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() snake_case_ : Optional[int] = model(_SCREAMING_SNAKE_CASE ,labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def a__ ( self :Union[str, Any] ): snake_case_ : Union[str, Any] = self.prepare_config_and_inputs() snake_case_ : List[str] = config_and_inputs snake_case_ : Union[str, Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __UpperCamelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): lowercase : List[str] = (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else () lowercase : List[Any] = ( {'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification} if is_torch_available() else {} ) lowercase : List[Any] = False lowercase : List[str] = False lowercase : int = False lowercase : Optional[int] = False def a__ ( self :str ): snake_case_ : List[Any] = MobileNetVaModelTester(self ) snake_case_ : Optional[int] = MobileNetVaConfigTester(self ,config_class=_SCREAMING_SNAKE_CASE ,has_text_modality=_SCREAMING_SNAKE_CASE ) def a__ ( self :Tuple ): self.config_tester.run_common_tests() @unittest.skip(reason="""MobileNetV1 does not use inputs_embeds""" ) def a__ ( self :Optional[Any] ): pass @unittest.skip(reason="""MobileNetV1 does not support input and output embeddings""" ) def a__ ( self :Optional[int] ): pass @unittest.skip(reason="""MobileNetV1 does not output attentions""" ) def a__ ( self :Any ): pass def a__ ( self :List[str] ): snake_case_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ : Union[str, Any] = model_class(_SCREAMING_SNAKE_CASE ) snake_case_ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ : Tuple = [*signature.parameters.keys()] snake_case_ : int = ["pixel_values"] self.assertListEqual(arg_names[:1] ,_SCREAMING_SNAKE_CASE ) def a__ ( self :Union[str, Any] ): snake_case_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def a__ ( self :Tuple ): def check_hidden_states_output(_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Dict ,_UpperCamelCase :Dict ): snake_case_ : Any = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): snake_case_ : Any = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) ) snake_case_ : Any = outputs.hidden_states snake_case_ : Optional[int] = 2_6 self.assertEqual(len(_SCREAMING_SNAKE_CASE ) ,_SCREAMING_SNAKE_CASE ) snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ : List[str] = True check_hidden_states_output(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ : Optional[int] = True check_hidden_states_output(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self :int ): snake_case_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE ) @slow def a__ ( self :Tuple ): for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ : Dict = MobileNetVaModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : str = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class __UpperCamelCase ( unittest.TestCase ): @cached_property def a__ ( self :int ): return ( MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v1_1.0_224""" ) if is_vision_available() else None ) @slow def a__ ( self :int ): snake_case_ : Any = MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v1_1.0_224""" ).to(_SCREAMING_SNAKE_CASE ) snake_case_ : Union[str, Any] = self.default_image_processor snake_case_ : List[Any] = prepare_img() snake_case_ : int = image_processor(images=_SCREAMING_SNAKE_CASE ,return_tensors="""pt""" ).to(_SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): snake_case_ : Optional[int] = model(**_SCREAMING_SNAKE_CASE ) # verify the logits snake_case_ : int = torch.Size((1, 1_0_0_1) ) self.assertEqual(outputs.logits.shape ,_SCREAMING_SNAKE_CASE ) snake_case_ : Dict = torch.tensor([-4.17_39, -1.12_33, 3.12_05] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,_SCREAMING_SNAKE_CASE ,atol=1E-4 ) )
363
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __UpperCamelCase : def __init__( self :Any ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[int]=1_2 ,_UpperCamelCase :Optional[Any]=7 ,_UpperCamelCase :Optional[int]=True ,_UpperCamelCase :Union[str, Any]=True ,_UpperCamelCase :Dict=True ,_UpperCamelCase :Optional[int]=9_9 ,_UpperCamelCase :Dict=3_2 ,_UpperCamelCase :Union[str, Any]=3_2 ,_UpperCamelCase :Union[str, Any]=2 ,_UpperCamelCase :Optional[Any]=4 ,_UpperCamelCase :List[Any]=3_7 ,_UpperCamelCase :Tuple=0.1 ,_UpperCamelCase :Optional[int]=0.1 ,_UpperCamelCase :int=5_1_2 ,_UpperCamelCase :Tuple=0.02 ,_UpperCamelCase :Any=0 ,_UpperCamelCase :str=None ,): snake_case_ : str = parent snake_case_ : int = batch_size snake_case_ : Union[str, Any] = seq_length snake_case_ : List[Any] = is_training snake_case_ : Union[str, Any] = use_input_mask snake_case_ : List[str] = use_labels snake_case_ : int = vocab_size snake_case_ : Any = hidden_size snake_case_ : List[Any] = projection_dim snake_case_ : Dict = num_hidden_layers snake_case_ : Dict = num_attention_heads snake_case_ : str = intermediate_size snake_case_ : int = dropout snake_case_ : int = attention_dropout snake_case_ : Dict = max_position_embeddings snake_case_ : Union[str, Any] = initializer_range snake_case_ : Dict = scope snake_case_ : Union[str, Any] = bos_token_id def a__ ( self :Any ): snake_case_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case_ : Union[str, Any] = None if self.use_input_mask: snake_case_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: snake_case_ : int = input_mask.numpy() snake_case_ , snake_case_ : Tuple = input_mask.shape snake_case_ : Any = np.random.randint(1 ,seq_length - 1 ,size=(batch_size,) ) for batch_idx, start_index in enumerate(_UpperCamelCase ): snake_case_ : Optional[int] = 1 snake_case_ : List[str] = 0 snake_case_ : Tuple = self.get_config() return config, input_ids, tf.convert_to_tensor(_UpperCamelCase ) def a__ ( self :str ): return BlipTextConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,projection_dim=self.projection_dim ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,dropout=self.dropout ,attention_dropout=self.attention_dropout ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,bos_token_id=self.bos_token_id ,) def a__ ( self :List[Any] ,_UpperCamelCase :Union[str, Any] ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[int] ): snake_case_ : List[str] = TFBlipTextModel(config=_UpperCamelCase ) snake_case_ : List[Any] = model(_UpperCamelCase ,attention_mask=_UpperCamelCase ,training=_UpperCamelCase ) snake_case_ : Any = model(_UpperCamelCase ,training=_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def a__ ( self :List[str] ): snake_case_ : Union[str, Any] = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ : str = config_and_inputs snake_case_ : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class __UpperCamelCase ( lowercase__ , unittest.TestCase ): lowercase : Optional[Any] = (TFBlipTextModel,) if is_tf_available() else () lowercase : int = False lowercase : List[Any] = False lowercase : Dict = False def a__ ( self :List[Any] ): snake_case_ : List[str] = BlipTextModelTester(self ) snake_case_ : Tuple = ConfigTester(self ,config_class=_UpperCamelCase ,hidden_size=3_7 ) def a__ ( self :Union[str, Any] ): self.config_tester.run_common_tests() def a__ ( self :Union[str, Any] ): snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def a__ ( self :Tuple ): pass def a__ ( self :Tuple ): pass @unittest.skip(reason="""Blip does not use inputs_embeds""" ) def a__ ( self :Any ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :Tuple ): pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def a__ ( self :List[Any] ): pass @slow def a__ ( self :Any ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ : Optional[Any] = TFBlipTextModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def a__ ( self :Dict ,_UpperCamelCase :Tuple=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCamelCase )
8
0
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase __A : Optional[int] = logging.get_logger(__name__) __A : Any = { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json', 'allenai/longformer-large-4096': 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json', 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json' ), } class __UpperCamelCase ( __snake_case ): lowercase : List[Any] = "longformer" def __init__( self :Optional[int] ,_UpperCamelCase :Union[List[int], int] = 5_1_2 ,_UpperCamelCase :int = 2 ,_UpperCamelCase :int = 1 ,_UpperCamelCase :int = 0 ,_UpperCamelCase :int = 2 ,_UpperCamelCase :int = 3_0_5_2_2 ,_UpperCamelCase :int = 7_6_8 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 1_2 ,_UpperCamelCase :int = 3_0_7_2 ,_UpperCamelCase :str = "gelu" ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :int = 5_1_2 ,_UpperCamelCase :int = 2 ,_UpperCamelCase :float = 0.02 ,_UpperCamelCase :float = 1E-1_2 ,_UpperCamelCase :bool = False ,**_UpperCamelCase :int ,): super().__init__(pad_token_id=lowerCamelCase_ ,**lowerCamelCase_ ) snake_case_ : Optional[Any] = attention_window snake_case_ : Optional[Any] = sep_token_id snake_case_ : int = bos_token_id snake_case_ : int = eos_token_id snake_case_ : Union[str, Any] = vocab_size snake_case_ : str = hidden_size snake_case_ : Optional[int] = num_hidden_layers snake_case_ : List[str] = num_attention_heads snake_case_ : List[str] = hidden_act snake_case_ : Tuple = intermediate_size snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Optional[Any] = attention_probs_dropout_prob snake_case_ : int = max_position_embeddings snake_case_ : Union[str, Any] = type_vocab_size snake_case_ : str = initializer_range snake_case_ : Union[str, Any] = layer_norm_eps snake_case_ : List[str] = onnx_export class __UpperCamelCase ( __snake_case ): def __init__( self :Dict ,_UpperCamelCase :"PretrainedConfig" ,_UpperCamelCase :str = "default" ,_UpperCamelCase :"List[PatchingSpec]" = None ): super().__init__(lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ) snake_case_ : Any = True @property def a__ ( self :Optional[Any] ): if self.task == "multiple-choice": snake_case_ : List[str] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case_ : str = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""global_attention_mask""", dynamic_axis), ] ) @property def a__ ( self :Any ): snake_case_ : Tuple = super().outputs if self.task == "default": snake_case_ : int = {0: """batch"""} return outputs @property def a__ ( self :List[str] ): return 1E-4 @property def a__ ( self :Any ): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset ,1_4 ) def a__ ( self :List[str] ,_UpperCamelCase :"PreTrainedTokenizerBase" ,_UpperCamelCase :int = -1 ,_UpperCamelCase :int = -1 ,_UpperCamelCase :bool = False ,_UpperCamelCase :Optional[TensorType] = None ,): snake_case_ : List[Any] = super().generate_dummy_inputs( preprocessor=lowerCamelCase_ ,batch_size=lowerCamelCase_ ,seq_length=lowerCamelCase_ ,is_pair=lowerCamelCase_ ,framework=lowerCamelCase_ ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly snake_case_ : str = torch.zeros_like(inputs["""input_ids"""] ) # make every second token global snake_case_ : Dict = 1 return inputs
364
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : int = { 'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'], 'feature_extraction_whisper': ['WhisperFeatureExtractor'], 'processing_whisper': ['WhisperProcessor'], 'tokenization_whisper': ['WhisperTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FlaxWhisperForConditionalGeneration', 'FlaxWhisperModel', 'FlaxWhisperPreTrainedModel', 'FlaxWhisperForAudioClassification', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys __A : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
8
0
'''simple docstring''' def UpperCAmelCase_ ( lowerCamelCase_ :str , lowerCamelCase_ :Dict ): '''simple docstring''' _validate_point(SCREAMING_SNAKE_CASE_ ) _validate_point(SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError("""Both points must be in the same n-dimensional space""" ) return float(sum(abs(a - b ) for a, b in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) ) def UpperCAmelCase_ ( lowerCamelCase_ :Optional[Any] ): '''simple docstring''' if point: if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): for item in point: if not isinstance(SCREAMING_SNAKE_CASE_ , (int, float) ): snake_case_ : str = ( """Expected a list of numbers as input, found """ F'''{type(SCREAMING_SNAKE_CASE_ ).__name__}''' ) raise TypeError(SCREAMING_SNAKE_CASE_ ) else: snake_case_ : int = F'''Expected a list of numbers as input, found {type(SCREAMING_SNAKE_CASE_ ).__name__}''' raise TypeError(SCREAMING_SNAKE_CASE_ ) else: raise ValueError("""Missing an input""" ) def UpperCAmelCase_ ( lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[Any] ): '''simple docstring''' _validate_point(SCREAMING_SNAKE_CASE_ ) _validate_point(SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError("""Both points must be in the same n-dimensional space""" ) return float(sum(abs(x - y ) for x, y in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
365
'''simple docstring''' import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor __A : Optional[int] = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): def __init__( self :List[str] ,*_UpperCamelCase :str ,**_UpperCamelCase :Optional[int] ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" ,_UpperCamelCase ,) super().__init__(*_UpperCamelCase ,**_UpperCamelCase )
8
0