code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class __snake_case ( a , a ): UpperCAmelCase__ : int = 1 @register_to_config def __init__( self : Optional[Any] , _snake_case : Tuple=2000 , _snake_case : Tuple=0.1 , _snake_case : Optional[int]=20 , _snake_case : Dict=1e-3): """simple docstring""" UpperCAmelCase_ = None UpperCAmelCase_ = None UpperCAmelCase_ = None def lowerCamelCase ( self : List[str] , _snake_case : Dict , _snake_case : List[str] = None): """simple docstring""" UpperCAmelCase_ = torch.linspace(1 , self.config.sampling_eps , _snake_case , device=_snake_case) def lowerCamelCase ( self : List[str] , _snake_case : Optional[int] , _snake_case : Any , _snake_case : List[str] , _snake_case : List[Any]=None): """simple docstring""" if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''') # TODO(Patrick) better comments + non-PyTorch # postprocess model score UpperCAmelCase_ = ( -0.2_5 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) UpperCAmelCase_ = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff)) UpperCAmelCase_ = std.flatten() while len(std.shape) < len(score.shape): UpperCAmelCase_ = std.unsqueeze(-1) UpperCAmelCase_ = -score / std # compute UpperCAmelCase_ = -1.0 / len(self.timesteps) UpperCAmelCase_ = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) UpperCAmelCase_ = beta_t.flatten() while len(beta_t.shape) < len(x.shape): UpperCAmelCase_ = beta_t.unsqueeze(-1) UpperCAmelCase_ = -0.5 * beta_t * x UpperCAmelCase_ = torch.sqrt(_snake_case) UpperCAmelCase_ = drift - diffusion**2 * score UpperCAmelCase_ = x + drift * dt # add noise UpperCAmelCase_ = randn_tensor(x.shape , layout=x.layout , generator=_snake_case , device=x.device , dtype=x.dtype) UpperCAmelCase_ = x_mean + diffusion * math.sqrt(-dt) * noise return x, x_mean def __len__( self : Optional[Any]): """simple docstring""" return self.config.num_train_timesteps
51
from math import isqrt def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" _lowercase =[True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __snake_case , __snake_case ): _lowercase =False return [i for i in range(2 , __snake_case ) if is_prime[i]] def UpperCAmelCase_ ( __snake_case = 10**8 ) -> int: """simple docstring""" _lowercase =calculate_prime_numbers(max_number // 2 ) _lowercase =0 _lowercase =0 _lowercase =len(__snake_case ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f'''{solution() = }''')
5
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a =logging.get_logger(__name__) a ={ """facebook/data2vec-vision-base-ft""": ( """https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json""" ), } class A_ ( SCREAMING_SNAKE_CASE ): _UpperCAmelCase : Optional[int] = '''data2vec-vision''' def __init__( self : List[str] ,SCREAMING_SNAKE_CASE__ : Any=7_6_8 ,SCREAMING_SNAKE_CASE__ : Optional[Any]=1_2 ,SCREAMING_SNAKE_CASE__ : Optional[Any]=1_2 ,SCREAMING_SNAKE_CASE__ : Optional[Any]=3_0_7_2 ,SCREAMING_SNAKE_CASE__ : Optional[int]="gelu" ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.0 ,SCREAMING_SNAKE_CASE__ : Optional[Any]=0.0 ,SCREAMING_SNAKE_CASE__ : Tuple=0.02 ,SCREAMING_SNAKE_CASE__ : List[str]=1E-12 ,SCREAMING_SNAKE_CASE__ : List[Any]=2_2_4 ,SCREAMING_SNAKE_CASE__ : Any=1_6 ,SCREAMING_SNAKE_CASE__ : str=3 ,SCREAMING_SNAKE_CASE__ : str=False ,SCREAMING_SNAKE_CASE__ : str=False ,SCREAMING_SNAKE_CASE__ : str=False ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=False ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.1 ,SCREAMING_SNAKE_CASE__ : List[Any]=0.1 ,SCREAMING_SNAKE_CASE__ : str=True ,SCREAMING_SNAKE_CASE__ : List[Any]=[3, 5, 7, 1_1] ,SCREAMING_SNAKE_CASE__ : List[str]=[1, 2, 3, 6] ,SCREAMING_SNAKE_CASE__ : Dict=True ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.4 ,SCREAMING_SNAKE_CASE__ : int=2_5_6 ,SCREAMING_SNAKE_CASE__ : Any=1 ,SCREAMING_SNAKE_CASE__ : Optional[int]=False ,SCREAMING_SNAKE_CASE__ : Tuple=2_5_5 ,**SCREAMING_SNAKE_CASE__ : Tuple ,): super().__init__(**SCREAMING_SNAKE_CASE__) __lowerCamelCase : Optional[int] = hidden_size __lowerCamelCase : List[Any] = num_hidden_layers __lowerCamelCase : Union[str, Any] = num_attention_heads __lowerCamelCase : List[Any] = intermediate_size __lowerCamelCase : List[Any] = hidden_act __lowerCamelCase : int = hidden_dropout_prob __lowerCamelCase : Dict = attention_probs_dropout_prob __lowerCamelCase : str = initializer_range __lowerCamelCase : str = layer_norm_eps __lowerCamelCase : Optional[Any] = image_size __lowerCamelCase : List[Any] = patch_size __lowerCamelCase : Optional[Any] = num_channels __lowerCamelCase : int = use_mask_token __lowerCamelCase : Tuple = use_absolute_position_embeddings __lowerCamelCase : Dict = use_relative_position_bias __lowerCamelCase : Optional[Any] = use_shared_relative_position_bias __lowerCamelCase : Optional[int] = layer_scale_init_value __lowerCamelCase : List[str] = drop_path_rate __lowerCamelCase : Optional[int] = use_mean_pooling # decode head attributes (semantic segmentation) __lowerCamelCase : Any = out_indices __lowerCamelCase : Any = pool_scales # auxiliary head attributes (semantic segmentation) __lowerCamelCase : Any = use_auxiliary_head __lowerCamelCase : int = auxiliary_loss_weight __lowerCamelCase : Dict = auxiliary_channels __lowerCamelCase : Optional[int] = auxiliary_num_convs __lowerCamelCase : List[Any] = auxiliary_concat_input __lowerCamelCase : Dict = semantic_loss_ignore_index class A_ ( SCREAMING_SNAKE_CASE ): _UpperCAmelCase : Dict = version.parse('''1.11''' ) @property def lowerCAmelCase ( self : Tuple): return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ]) @property def lowerCAmelCase ( self : Any): return 1E-4
73
UpperCAmelCase__ = { '''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''': '''--..''', '''1''': '''.----''', '''2''': '''..---''', '''3''': '''...--''', '''4''': '''....-''', '''5''': '''.....''', '''6''': '''-....''', '''7''': '''--...''', '''8''': '''---..''', '''9''': '''----.''', '''0''': '''-----''', '''&''': '''.-...''', '''@''': '''.--.-.''', ''':''': '''---...''', ''',''': '''--..--''', '''.''': '''.-.-.-''', '''\'''': '''.----.''', '''"''': '''.-..-.''', '''?''': '''..--..''', '''/''': '''-..-.''', '''=''': '''-...-''', '''+''': '''.-.-.''', '''-''': '''-....-''', '''(''': '''-.--.''', ''')''': '''-.--.-''', '''!''': '''-.-.--''', ''' ''': '''/''' } # Exclamation mark is not in ITU-R recommendation # fmt: on UpperCAmelCase__ = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _lowercase ='''Morse code here!''' print(__snake_case ) _lowercase =encrypt(__snake_case ) print(__snake_case ) _lowercase =decrypt(__snake_case ) print(__snake_case ) if __name__ == "__main__": main()
5
0
import sys from typing import Tuple import numpy as np import torch from PIL import Image from torch import nn from transformers.image_utils import PILImageResampling from utils import img_tensorize class __SCREAMING_SNAKE_CASE : def __init__( self : Union[str, Any] , A : List[str] , A : str=sys.maxsize ) ->str: lowerCamelCase__ : str = '''bilinear''' lowerCamelCase__ : Tuple = max_size lowerCamelCase__ : Optional[Any] = short_edge_length def __call__( self : Optional[int] , A : List[Any] ) ->int: lowerCamelCase__ : Optional[int] = [] for img in imgs: lowerCamelCase__ , lowerCamelCase__ : str = img.shape[:2] # later: provide list and randomly choose index for resize lowerCamelCase__ : Union[str, Any] = np.random.randint(self.short_edge_length[0] , self.short_edge_length[1] + 1 ) if size == 0: return img lowerCamelCase__ : List[str] = size * 1.0 / min(A , A ) if h < w: lowerCamelCase__ , lowerCamelCase__ : Any = size, scale * w else: lowerCamelCase__ , lowerCamelCase__ : List[Any] = scale * h, size if max(A , A ) > self.max_size: lowerCamelCase__ : List[Any] = self.max_size * 1.0 / max(A , A ) lowerCamelCase__ : List[str] = newh * scale lowerCamelCase__ : List[str] = neww * scale lowerCamelCase__ : Tuple = int(neww + 0.5 ) lowerCamelCase__ : Dict = int(newh + 0.5 ) if img.dtype == np.uinta: lowerCamelCase__ : Union[str, Any] = Image.fromarray(A ) lowerCamelCase__ : str = pil_image.resize((neww, newh) , PILImageResampling.BILINEAR ) lowerCamelCase__ : List[Any] = np.asarray(A ) else: lowerCamelCase__ : Union[str, Any] = img.permute(2 , 0 , 1 ).unsqueeze(0 ) # 3, 0, 1) # hw(c) -> nchw lowerCamelCase__ : Any = nn.functional.interpolate( A , (newh, neww) , mode=self.interp_method , align_corners=A ).squeeze(0 ) img_augs.append(A ) return img_augs class __SCREAMING_SNAKE_CASE : def __init__( self : str , A : List[str] ) ->Tuple: lowerCamelCase__ : Union[str, Any] = ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST] , cfg.INPUT.MAX_SIZE_TEST ) lowerCamelCase__ : Tuple = cfg.INPUT.FORMAT lowerCamelCase__ : List[Any] = cfg.SIZE_DIVISIBILITY lowerCamelCase__ : Any = cfg.PAD_VALUE lowerCamelCase__ : List[str] = cfg.INPUT.MAX_SIZE_TEST lowerCamelCase__ : List[Any] = cfg.MODEL.DEVICE lowerCamelCase__ : Tuple = torch.tensor(cfg.MODEL.PIXEL_STD ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) lowerCamelCase__ : Optional[int] = torch.tensor(cfg.MODEL.PIXEL_MEAN ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) lowerCamelCase__ : List[str] = lambda A : (x - self.pixel_mean) / self.pixel_std def __lowerCamelCase ( self : Dict , A : Union[str, Any] ) ->List[str]: lowerCamelCase__ : List[str] = tuple(max(A ) for s in zip(*[img.shape for img in images] ) ) lowerCamelCase__ : List[Any] = [im.shape[-2:] for im in images] lowerCamelCase__ : Union[str, Any] = [ nn.functional.pad( A , [0, max_size[-1] - size[1], 0, max_size[-2] - size[0]] , value=self.pad_value , ) for size, im in zip(A , A ) ] return torch.stack(A ), torch.tensor(A ) def __call__( self : str , A : int , A : List[str]=False ) ->str: with torch.no_grad(): if not isinstance(A , A ): lowerCamelCase__ : List[str] = [images] if single_image: assert len(A ) == 1 for i in range(len(A ) ): if isinstance(images[i] , torch.Tensor ): images.insert(A , images.pop(A ).to(self.device ).float() ) elif not isinstance(images[i] , torch.Tensor ): images.insert( A , torch.as_tensor(img_tensorize(images.pop(A ) , input_format=self.input_format ) ) .to(self.device ) .float() , ) # resize smallest edge lowerCamelCase__ : str = torch.tensor([im.shape[:2] for im in images] ) lowerCamelCase__ : str = self.aug(A ) # transpose images and convert to torch tensors # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images] # now normalize before pad to avoid useless arithmetic lowerCamelCase__ : Dict = [self.normalizer(A ) for x in images] # now pad them to do the following operations lowerCamelCase__ , lowerCamelCase__ : List[Any] = self.pad(A ) # Normalize if self.size_divisibility > 0: raise NotImplementedError() # pad lowerCamelCase__ : List[Any] = torch.true_divide(A , A ) if single_image: return images[0], sizes[0], scales_yx[0] else: return images, sizes, scales_yx def _a ( UpperCAmelCase , UpperCAmelCase ) -> Tuple: """simple docstring""" boxes[:, 0::2] *= scale_yx[:, 1] boxes[:, 1::2] *= scale_yx[:, 0] return boxes def _a ( UpperCAmelCase , UpperCAmelCase ) -> Optional[int]: """simple docstring""" assert torch.isfinite(__snake_case ).all(), "Box tensor contains infinite or NaN!" lowerCamelCase__ , lowerCamelCase__ : int = box_size tensor[:, 0].clamp_(min=0 , max=__snake_case ) tensor[:, 1].clamp_(min=0 , max=__snake_case ) tensor[:, 2].clamp_(min=0 , max=__snake_case ) tensor[:, 3].clamp_(min=0 , max=__snake_case )
142
from typing import Any def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> list: """simple docstring""" _validation( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) # Creates data structures and fill initial step _lowercase ={} _lowercase ={} for state in states_space: _lowercase =observations_space[0] _lowercase =( initial_probabilities[state] * emission_probabilities[state][observation] ) _lowercase =None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__snake_case ) ): _lowercase =observations_space[o] _lowercase =observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _lowercase =probability _lowercase =k_state # Update probabilities and pointers dicts _lowercase =( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _lowercase =arg_max # The final observation _lowercase =observations_space[len(__snake_case ) - 1] # argmax for given final observation _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =probabilities[(k_state, final_observation)] if probability > max_probability: _lowercase =probability _lowercase =k_state _lowercase =arg_max # Process pointers backwards _lowercase =last_state _lowercase =[] for o in range(len(__snake_case ) - 1 , -1 , -1 ): result.append(__snake_case ) _lowercase =pointers[previous, observations_space[o]] result.reverse() return result def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_not_empty( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) _validate_lists(__snake_case , __snake_case ) _validate_dicts( __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_list(__snake_case , '''observations_space''' ) _validate_list(__snake_case , '''states_space''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a list" raise ValueError(__snake_case ) else: for x in _object: if not isinstance(__snake_case , __snake_case ): _lowercase =F"{var_name} must be a list of strings" raise ValueError(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_dict(__snake_case , '''initial_probabilities''' , __snake_case ) _validate_nested_dict(__snake_case , '''transition_probabilities''' ) _validate_nested_dict(__snake_case , '''emission_probabilities''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_dict(_object , __snake_case , __snake_case ) for x in _object.values(): _validate_dict(__snake_case , __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case = False ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a dict" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object ): _lowercase =F"{var_name} all keys must be strings" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object.values() ): _lowercase ='''nested dictionary ''' if nested else '''''' _lowercase =F"{var_name} {nested_text}all values must be {value_type.__name__}" raise ValueError(__snake_case ) if __name__ == "__main__": from doctest import testmod testmod()
5
0
'''simple docstring''' from bisect import bisect from itertools import accumulate def __UpperCAmelCase ( a_: List[Any], a_: int, a_: List[str], a_: Union[str, Any] ): _UpperCAmelCase : List[str] = sorted(zip(__snake_case, __snake_case ), key=lambda a_ : x[0] / x[1], reverse=__snake_case ) _UpperCAmelCase , _UpperCAmelCase : List[str] = [i[0] for i in r], [i[1] for i in r] _UpperCAmelCase : Union[str, Any] = list(accumulate(__snake_case ) ) _UpperCAmelCase : List[Any] = bisect(__snake_case, __snake_case ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
145
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) # TODO Update this UpperCAmelCase__ = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''esm''' def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_2_6 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase="absolute" , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ) -> Tuple: super().__init__(pad_token_id=UpperCAmelCase , mask_token_id=UpperCAmelCase , **UpperCAmelCase ) _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =position_embedding_type _lowercase =use_cache _lowercase =emb_layer_norm_before _lowercase =token_dropout _lowercase =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) _lowercase =EsmFoldConfig() elif isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =EsmFoldConfig(**UpperCAmelCase ) _lowercase =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) _lowercase =get_default_vocab_list() else: _lowercase =vocab_list else: _lowercase =None _lowercase =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCAmelCase ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A (self ) -> List[str]: _lowercase =super().to_dict() if isinstance(self.esmfold_config , UpperCAmelCase ): _lowercase =self.esmfold_config.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> Union[str, Any]: if self.trunk is None: _lowercase =TrunkConfig() elif isinstance(self.trunk , UpperCAmelCase ): _lowercase =TrunkConfig(**self.trunk ) def __A (self ) -> Tuple: _lowercase =asdict(self ) _lowercase =self.trunk.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 48 SCREAMING_SNAKE_CASE__ = 1024 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> List[str]: if self.structure_module is None: _lowercase =StructureModuleConfig() elif isinstance(self.structure_module , UpperCAmelCase ): _lowercase =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) _lowercase =self.sequence_state_dim // self.sequence_head_width _lowercase =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(f"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __A (self ) -> Dict: _lowercase =asdict(self ) _lowercase =self.structure_module.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 384 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 16 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 12 SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 0.1 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 1 SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = 7 SCREAMING_SNAKE_CASE__ = 10 SCREAMING_SNAKE_CASE__ = 1E-8 SCREAMING_SNAKE_CASE__ = 1E5 def __A (self ) -> List[Any]: return asdict(self ) def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
5
0
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __snake_case ( _SCREAMING_SNAKE_CASE ,unittest.TestCase): """simple docstring""" lowercase = CTRLTokenizer lowercase = False lowercase = False def __lowercase ( self : Optional[int] ) -> int: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowerCAmelCase_ : Dict = ["""adapt""", """re@@""", """a@@""", """apt""", """c@@""", """t""", """<unk>"""] lowerCAmelCase_ : int = dict(zip(lowerCamelCase , range(len(lowerCamelCase ) ) ) ) lowerCAmelCase_ : Tuple = ["""#version: 0.2""", """a p""", """ap t</w>""", """r e""", """a d""", """ad apt</w>""", """"""] lowerCAmelCase_ : Dict = {"""unk_token""": """<unk>"""} lowerCAmelCase_ : Dict = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCAmelCase_ : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(lowerCamelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(lowerCamelCase ) ) def __lowercase ( self : List[str] , **lowerCamelCase : Any ) -> str: kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase ) def __lowercase ( self : str , lowerCamelCase : str ) -> Union[str, Any]: lowerCAmelCase_ : Dict = """adapt react readapt apt""" lowerCAmelCase_ : Any = """adapt react readapt apt""" return input_text, output_text def __lowercase ( self : int ) -> Dict: lowerCAmelCase_ : int = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) lowerCAmelCase_ : int = """adapt react readapt apt""" lowerCAmelCase_ : Dict = """adapt re@@ a@@ c@@ t re@@ adapt apt""".split() lowerCAmelCase_ : Tuple = tokenizer.tokenize(lowerCamelCase ) self.assertListEqual(lowerCamelCase , lowerCamelCase ) lowerCAmelCase_ : Optional[Any] = tokens + [tokenizer.unk_token] lowerCAmelCase_ : Dict = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase ) , lowerCamelCase )
120
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList UpperCAmelCase__ = ['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif'''] class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=1 ) -> Dict: _lowercase =tokenizer _lowercase =dataset _lowercase =len(UpperCAmelCase ) if n_tasks is None else n_tasks _lowercase =n_copies def __iter__(self ) -> Optional[Any]: _lowercase =[] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) _lowercase =self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =start_length _lowercase =eof_strings _lowercase =tokenizer def __call__(self , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: _lowercase =self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) _lowercase =[] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(UpperCAmelCase ) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =re.split('''(%s)''' % '''|'''.join(__snake_case ) , __snake_case ) # last string should be "" return "".join(string_list[:-2] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case=20 , **__snake_case ) -> Tuple: """simple docstring""" _lowercase =defaultdict(__snake_case ) # dict of list of generated tokens for step, batch in tqdm(enumerate(__snake_case ) ): with torch.no_grad(): _lowercase =batch['''ids'''].shape[-1] _lowercase =accelerator.unwrap_model(__snake_case ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=__snake_case , **__snake_case ) # each task is generated batch_size times _lowercase =batch['''task_id'''].repeat(__snake_case ) _lowercase =accelerator.pad_across_processes( __snake_case , dim=1 , pad_index=tokenizer.pad_token_id ) _lowercase , _lowercase =accelerator.gather((generated_tokens, generated_tasks) ) _lowercase =generated_tokens.cpu().numpy() _lowercase =generated_tasks.cpu().numpy() for task, generated_tokens in zip(__snake_case , __snake_case ): gen_token_dict[task].append(__snake_case ) _lowercase =[[] for _ in range(__snake_case )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: _lowercase =tokenizer.decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) code_gens[task].append(remove_last_block(__snake_case ) ) return code_gens def UpperCAmelCase_ ( ) -> str: """simple docstring""" _lowercase =HfArgumentParser(__snake_case ) _lowercase =parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric _lowercase =args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing _lowercase ='''false''' if args.num_workers is None: _lowercase =multiprocessing.cpu_count() # Use dataset load to feed to accelerate _lowercase =Accelerator() set_seed(args.seed , device_specific=__snake_case ) # Load model and tokenizer _lowercase =AutoTokenizer.from_pretrained(args.model_ckpt ) _lowercase =tokenizer.eos_token _lowercase =AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings _lowercase ={ '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , __snake_case , __snake_case )] ), } # Load evaluation dataset and metric _lowercase =load_dataset('''openai_humaneval''' ) _lowercase =load_metric('''code_eval''' ) _lowercase =args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) _lowercase =args.n_samples // args.batch_size _lowercase =TokenizedDataset(__snake_case , human_eval['''test'''] , n_copies=__snake_case , n_tasks=__snake_case ) # do not confuse args.batch_size, which is actually the num_return_sequences _lowercase =DataLoader(__snake_case , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: _lowercase =code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception _lowercase , _lowercase =accelerator.prepare(__snake_case , __snake_case ) _lowercase =complete_code( __snake_case , __snake_case , __snake_case , __snake_case , n_tasks=__snake_case , batch_size=args.batch_size , **__snake_case , ) if accelerator.is_main_process: _lowercase =[] for task in tqdm(range(__snake_case ) ): _lowercase =human_eval['''test'''][task]['''test'''] _lowercase =F"check({human_eval['test'][task]['entry_point']})" references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric _lowercase , _lowercase =code_eval_metric.compute( references=__snake_case , predictions=__snake_case , num_workers=args.num_workers ) print(F"Results: {pass_at_k}" ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(__snake_case , __snake_case ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
5
0
def A__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__) -> List[Any]: if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2)) * actual_power(__snake_case , int(b / 2)) else: return a * actual_power(__snake_case , int(b / 2)) * actual_power(__snake_case , int(b / 2)) def A__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__) -> float: if b < 0: return 1 / actual_power(__snake_case , __snake_case) return actual_power(__snake_case , __snake_case) if __name__ == "__main__": print(power(-2, -3))
111
UpperCAmelCase__ = 8.31_44_62 # Unit - J mol-1 K-1 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
5
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCAmelCase : Union[str, Any] = logging.get_logger(__name__) UpperCAmelCase : List[Any] = { """microsoft/focalnet-tiny""": """https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json""", } class __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__): _lowercase : List[Any] = """focalnet""" def __init__( self , lowerCAmelCase__=2_2_4 , lowerCAmelCase__=4 , lowerCAmelCase__=3 , lowerCAmelCase__=9_6 , lowerCAmelCase__=False , lowerCAmelCase__=[1_9_2, 3_8_4, 7_6_8, 7_6_8] , lowerCAmelCase__=[2, 2, 6, 2] , lowerCAmelCase__=[2, 2, 2, 2] , lowerCAmelCase__=[3, 3, 3, 3] , lowerCAmelCase__="gelu" , lowerCAmelCase__=4.0 , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.1 , lowerCAmelCase__=False , lowerCAmelCase__=1E-4 , lowerCAmelCase__=False , lowerCAmelCase__=False , lowerCAmelCase__=False , lowerCAmelCase__=0.02 , lowerCAmelCase__=1E-5 , lowerCAmelCase__=3_2 , lowerCAmelCase__=None , lowerCAmelCase__=None , **lowerCAmelCase__ , ) -> Dict: '''simple docstring''' super().__init__(**lowerCAmelCase__ ) a__ : List[Any] =image_size a__ : Optional[Any] =patch_size a__ : List[Any] =num_channels a__ : List[str] =embed_dim a__ : str =use_conv_embed a__ : List[str] =hidden_sizes a__ : int =depths a__ : Dict =focal_levels a__ : Union[str, Any] =focal_windows a__ : Union[str, Any] =hidden_act a__ : Union[str, Any] =mlp_ratio a__ : str =hidden_dropout_prob a__ : List[str] =drop_path_rate a__ : List[str] =use_layerscale a__ : List[str] =layerscale_value a__ : Dict =use_post_layernorm a__ : List[Any] =use_post_layernorm_in_modulation a__ : Dict =normalize_modulator a__ : Optional[int] =initializer_range a__ : Optional[Any] =layer_norm_eps a__ : Dict =encoder_stride a__ : List[Any] =["stem"] + [F'''stage{idx}''' for idx in range(1 , len(self.depths ) + 1 )] a__ , a__ : Union[str, Any] =get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
95
from __future__ import annotations from collections.abc import Callable UpperCAmelCase__ = list[list[float | int]] def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Matrix: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(size + 1 )] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for row in range(__snake_case ): for col in range(__snake_case ): _lowercase =matrix[row][col] _lowercase =vector[row][0] _lowercase =0 _lowercase =0 while row < size and col < size: # pivoting _lowercase =max((abs(augmented[rowa][col] ), rowa) for rowa in range(__snake_case , __snake_case ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: _lowercase , _lowercase =augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __snake_case ): _lowercase =augmented[rowa][col] / augmented[row][col] _lowercase =0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __snake_case ): for row in range(__snake_case ): _lowercase =augmented[row][col] / augmented[col][col] for cola in range(__snake_case , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__snake_case ) ] def UpperCAmelCase_ ( __snake_case ) -> Callable[[int], int]: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(__snake_case )] for _ in range(__snake_case )] _lowercase =[[0] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for x_val, y_val in enumerate(__snake_case ): for col in range(__snake_case ): _lowercase =(x_val + 1) ** (size - col - 1) _lowercase =y_val _lowercase =solve(__snake_case , __snake_case ) def interpolated_func(__snake_case ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__snake_case ) ) return interpolated_func def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def UpperCAmelCase_ ( __snake_case = question_function , __snake_case = 10 ) -> int: """simple docstring""" _lowercase =[func(__snake_case ) for x_val in range(1 , order + 1 )] _lowercase =[ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] _lowercase =0 _lowercase =42 _lowercase =42 for poly in polynomials: _lowercase =1 while func(__snake_case ) == poly(__snake_case ): x_val += 1 ret += poly(__snake_case ) return ret if __name__ == "__main__": print(f'''{solution() = }''')
5
0
from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class A_ ( snake_case__ ): _lowercase : Optional[Any] = 4_2 class A_ ( snake_case__ , snake_case__ ): @register_to_config def __init__( self : List[str] , UpperCAmelCase : Dict = 3 , UpperCAmelCase : Dict = 3 , UpperCAmelCase : List[Any] = ("DownEncoderBlock2D",) , UpperCAmelCase : Union[str, Any] = ("UpDecoderBlock2D",) , UpperCAmelCase : Optional[Any] = (6_4,) , UpperCAmelCase : Optional[Any] = 1 , UpperCAmelCase : List[Any] = "silu" , UpperCAmelCase : str = 3 , UpperCAmelCase : Optional[int] = 3_2 , UpperCAmelCase : int = 2_5_6 , UpperCAmelCase : Tuple = 3_2 , UpperCAmelCase : Optional[int] = None , UpperCAmelCase : Any = 0.18215 , UpperCAmelCase : str = "group" , ) -> Any: super().__init__() # pass init params to Encoder __lowerCAmelCase: List[str] = Encoder( in_channels=UpperCAmelCase , out_channels=UpperCAmelCase , down_block_types=UpperCAmelCase , block_out_channels=UpperCAmelCase , layers_per_block=UpperCAmelCase , act_fn=UpperCAmelCase , norm_num_groups=UpperCAmelCase , double_z=UpperCAmelCase , ) __lowerCAmelCase: Dict = vq_embed_dim if vq_embed_dim is not None else latent_channels __lowerCAmelCase: str = nn.Convad(UpperCAmelCase , UpperCAmelCase , 1 ) __lowerCAmelCase: Dict = VectorQuantizer(UpperCAmelCase , UpperCAmelCase , beta=0.25 , remap=UpperCAmelCase , sane_index_shape=UpperCAmelCase ) __lowerCAmelCase: int = nn.Convad(UpperCAmelCase , UpperCAmelCase , 1 ) # pass init params to Decoder __lowerCAmelCase: List[Any] = Decoder( in_channels=UpperCAmelCase , out_channels=UpperCAmelCase , up_block_types=UpperCAmelCase , block_out_channels=UpperCAmelCase , layers_per_block=UpperCAmelCase , act_fn=UpperCAmelCase , norm_num_groups=UpperCAmelCase , norm_type=UpperCAmelCase , ) @apply_forward_hook def UpperCAmelCase ( self : List[Any] , UpperCAmelCase : Any , UpperCAmelCase : Optional[int] = True ) -> VQEncoderOutput: __lowerCAmelCase: Optional[Any] = self.encoder(UpperCAmelCase ) __lowerCAmelCase: Optional[Any] = self.quant_conv(UpperCAmelCase ) if not return_dict: return (h,) return VQEncoderOutput(latents=UpperCAmelCase ) @apply_forward_hook def UpperCAmelCase ( self : Optional[int] , UpperCAmelCase : Any , UpperCAmelCase : Union[str, Any] = False , UpperCAmelCase : Union[str, Any] = True ) -> Union[DecoderOutput, torch.FloatTensor]: # also go through quantization layer if not force_not_quantize: __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase: List[Any] = self.quantize(UpperCAmelCase ) else: __lowerCAmelCase: str = h __lowerCAmelCase: Any = self.post_quant_conv(UpperCAmelCase ) __lowerCAmelCase: List[str] = self.decoder(UpperCAmelCase , quant if self.config.norm_type == 'spatial' else None ) if not return_dict: return (dec,) return DecoderOutput(sample=UpperCAmelCase ) def UpperCAmelCase ( self : Any , UpperCAmelCase : List[Any] , UpperCAmelCase : Dict = True ) -> Union[DecoderOutput, torch.FloatTensor]: __lowerCAmelCase: Optional[int] = sample __lowerCAmelCase: Optional[Any] = self.encode(UpperCAmelCase ).latents __lowerCAmelCase: Dict = self.decode(UpperCAmelCase ).sample if not return_dict: return (dec,) return DecoderOutput(sample=UpperCAmelCase )
322
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase__ = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''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: UpperCAmelCase__ = [ '''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 UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
0
import dataclasses import re import string from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple import numpy as np from . import residue_constants __snake_case : Union[str, Any] = Mapping[str, np.ndarray] __snake_case : Union[str, Any] = Mapping[str, Any] # Is a nested dict. __snake_case : List[str] = 0.01 @dataclasses.dataclass(frozen=a_ ) class A__: """simple docstring""" _A : List[str] = 42 # [num_res, num_atom_type, 3] # Amino-acid type for each residue represented as an integer between 0 and # 20, where 20 is 'X'. _A : str = 42 # [num_res] # Binary float mask to indicate presence of a particular atom. 1.0 if an atom # is present and 0.0 if not. This should be used for loss masking. _A : List[Any] = 42 # [num_res, num_atom_type] # Residue index as used in PDB. It is not necessarily continuous or 0-indexed. _A : List[Any] = 42 # [num_res] # B-factors, or temperature factors, of each residue (in sq. angstroms units), # representing the displacement of the residue from its ground truth mean # value. _A : Optional[int] = 42 # [num_res, num_atom_type] # Chain indices for multi-chain predictions _A : List[Any] = None # Optional remark about the protein. Included as a comment in output PDB # files _A : Optional[Any] = None # Templates used to generate this protein (prediction-only) _A : str = None # Chain corresponding to each parent _A : Dict = None def _UpperCAmelCase ( a__): '''simple docstring''' a_ : List[Any] = r"""(\[[A-Z]+\]\n)""" a_ : str = [tag.strip() for tag in re.split(__snake_case , __snake_case) if len(__snake_case) > 0] a_ : Union[str, Any] = zip(tags[0::2] , [l.split("""\n""") for l in tags[1::2]]) a_ : str = ["""N""", """CA""", """C"""] a_ : Dict = None a_ : Tuple = None a_ : int = None for g in groups: if "[PRIMARY]" == g[0]: a_ : List[Any] = g[1][0].strip() for i in range(len(__snake_case)): if seq[i] not in residue_constants.restypes: a_ : str = """X""" # FIXME: strings are immutable a_ : Union[str, Any] = np.array( [residue_constants.restype_order.get(__snake_case , residue_constants.restype_num) for res_symbol in seq]) elif "[TERTIARY]" == g[0]: a_ : Dict = [] for axis in range(3): tertiary.append(list(map(__snake_case , g[1][axis].split()))) a_ : Tuple = np.array(__snake_case) a_ : List[str] = np.zeros((len(tertiary[0]) // 3, residue_constants.atom_type_num, 3)).astype(np.floataa) for i, atom in enumerate(__snake_case): a_ : Optional[int] = np.transpose(tertiary_np[:, i::3]) atom_positions *= PICO_TO_ANGSTROM elif "[MASK]" == g[0]: a_ : Dict = np.array(list(map({"""-""": 0, """+""": 1}.get , g[1][0].strip()))) a_ : str = np.zeros( ( len(__snake_case), residue_constants.atom_type_num, )).astype(np.floataa) for i, atom in enumerate(__snake_case): a_ : Any = 1 atom_mask *= mask[..., None] assert aatype is not None return Protein( atom_positions=__snake_case , atom_mask=__snake_case , aatype=__snake_case , residue_index=np.arange(len(__snake_case)) , b_factors=__snake_case , ) def _UpperCAmelCase ( a__ , a__ = 0): '''simple docstring''' a_ : Union[str, Any] = [] a_ : Optional[Any] = prot.remark if remark is not None: pdb_headers.append(f'''REMARK {remark}''') a_ : Dict = prot.parents a_ : int = prot.parents_chain_index if parents is not None and parents_chain_index is not None: a_ : List[Any] = [p for i, p in zip(__snake_case , __snake_case) if i == chain_id] if parents is None or len(__snake_case) == 0: a_ : int = ["""N/A"""] pdb_headers.append(f'''PARENT {" ".join(__snake_case)}''') return pdb_headers def _UpperCAmelCase ( a__ , a__): '''simple docstring''' a_ : List[str] = [] a_ : Dict = pdb_str.split("""\n""") a_ : Tuple = prot.remark if remark is not None: out_pdb_lines.append(f'''REMARK {remark}''') a_ : Optional[int] = 4_2 if prot.parents is not None and len(prot.parents) > 0: a_ : Optional[int] = [] if prot.parents_chain_index is not None: a_ : Dict = {} for p, i in zip(prot.parents , prot.parents_chain_index): parent_dict.setdefault(str(__snake_case) , []) parent_dict[str(__snake_case)].append(__snake_case) a_ : str = max([int(__snake_case) for chain_idx in parent_dict]) for i in range(max_idx + 1): a_ : int = parent_dict.get(str(__snake_case) , ["""N/A"""]) parents_per_chain.append(__snake_case) else: parents_per_chain.append(list(prot.parents)) else: a_ : Tuple = [["""N/A"""]] def make_parent_line(a__) -> str: return f'''PARENT {" ".join(__snake_case)}''' out_pdb_lines.append(make_parent_line(parents_per_chain[0])) a_ : Tuple = 0 for i, l in enumerate(__snake_case): if "PARENT" not in l and "REMARK" not in l: out_pdb_lines.append(__snake_case) if "TER" in l and "END" not in lines[i + 1]: chain_counter += 1 if not chain_counter >= len(__snake_case): a_ : List[str] = parents_per_chain[chain_counter] else: a_ : Optional[int] = ["""N/A"""] out_pdb_lines.append(make_parent_line(__snake_case)) return "\n".join(__snake_case) def _UpperCAmelCase ( a__): '''simple docstring''' a_ : str = residue_constants.restypes + ["""X"""] def res_atoa(a__) -> str: return residue_constants.restype_atoa.get(restypes[r] , """UNK""") a_ : Any = residue_constants.atom_types a_ : Optional[int] = [] a_ : Dict = prot.atom_mask a_ : List[str] = prot.aatype a_ : Union[str, Any] = prot.atom_positions a_ : Tuple = prot.residue_index.astype(np.intaa) a_ : Dict = prot.b_factors a_ : Union[str, Any] = prot.chain_index if np.any(aatype > residue_constants.restype_num): raise ValueError("""Invalid aatypes.""") a_ : List[str] = get_pdb_headers(__snake_case) if len(__snake_case) > 0: pdb_lines.extend(__snake_case) a_ : Optional[Any] = aatype.shape[0] a_ : Any = 1 a_ : Tuple = 0 a_ : Optional[Any] = string.ascii_uppercase a_ : Any = None # Add all atom sites. for i in range(__snake_case): a_ : List[str] = res_atoa(aatype[i]) for atom_name, pos, mask, b_factor in zip(__snake_case , atom_positions[i] , atom_mask[i] , b_factors[i]): if mask < 0.5: continue a_ : List[str] = """ATOM""" a_ : List[str] = atom_name if len(__snake_case) == 4 else f''' {atom_name}''' a_ : List[str] = """""" a_ : str = """""" a_ : Optional[int] = 1.00 a_ : Any = atom_name[0] # Protein supports only C, N, O, S, this works. a_ : Optional[int] = """""" a_ : int = """A""" if chain_index is not None: a_ : Tuple = chain_tags[chain_index[i]] # PDB is a columnar format, every space matters here! a_ : Optional[int] = ( f'''{record_type:<6}{atom_index:>5} {name:<4}{alt_loc:>1}''' f'''{res_name_a:>3} {chain_tag:>1}''' f'''{residue_index[i]:>4}{insertion_code:>1} ''' f'''{pos[0]:>8.3f}{pos[1]:>8.3f}{pos[2]:>8.3f}''' f'''{occupancy:>6.2f}{b_factor:>6.2f} ''' f'''{element:>2}{charge:>2}''' ) pdb_lines.append(__snake_case) atom_index += 1 a_ : int = i == n - 1 if chain_index is not None: if i != n - 1 and chain_index[i + 1] != prev_chain_index: a_ : int = True a_ : List[str] = chain_index[i + 1] if should_terminate: # Close the chain. a_ : List[str] = """TER""" a_ : Dict = ( f'''{chain_end:<6}{atom_index:>5} {res_atoa(aatype[i]):>3} {chain_tag:>1}{residue_index[i]:>4}''' ) pdb_lines.append(__snake_case) atom_index += 1 if i != n - 1: # "prev" is a misnomer here. This happens at the beginning of # each new chain. pdb_lines.extend(get_pdb_headers(__snake_case , __snake_case)) pdb_lines.append("""END""") pdb_lines.append("""""") return "\n".join(__snake_case) def _UpperCAmelCase ( a__): '''simple docstring''' return residue_constants.STANDARD_ATOM_MASK[prot.aatype] def _UpperCAmelCase ( a__ , a__ , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , ): '''simple docstring''' return Protein( aatype=features["""aatype"""] , atom_positions=result["""final_atom_positions"""] , atom_mask=result["""final_atom_mask"""] , residue_index=features["""residue_index"""] + 1 , b_factors=b_factors if b_factors is not None else np.zeros_like(result["""final_atom_mask"""]) , chain_index=__snake_case , remark=__snake_case , parents=__snake_case , parents_chain_index=__snake_case , )
248
# 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 # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase__ = { '''configuration_efficientnet''': [ '''EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientNetConfig''', '''EfficientNetOnnxConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''EfficientNetImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientNetForImageClassification''', '''EfficientNetModel''', '''EfficientNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
5
0
from itertools import count def lowercase_ ( _A : Any = 50 ): """simple docstring""" lowerCamelCase__ : Any = [1] * min_block_length for n in count(__snake_case ): fill_count_functions.append(1 ) for block_length in range(__snake_case , n + 1 ): for block_start in range(n - block_length ): fill_count_functions[n] += fill_count_functions[ n - block_start - block_length - 1 ] fill_count_functions[n] += 1 if fill_count_functions[n] > 1000000: break return n if __name__ == "__main__": print(f'{solution() = }')
184
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
0
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a__ : Dict = logging.get_logger(__name__) a__ : Dict = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class a_ ( a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = 'wavlm' def __init__( self , _lowerCamelCase=32 , _lowerCamelCase=768 , _lowerCamelCase=12 , _lowerCamelCase=12 , _lowerCamelCase=3072 , _lowerCamelCase="gelu" , _lowerCamelCase=0.1 , _lowerCamelCase=0.1 , _lowerCamelCase=0.1 , _lowerCamelCase=0.0 , _lowerCamelCase=0.1 , _lowerCamelCase=0.1 , _lowerCamelCase=0.0_2 , _lowerCamelCase=1e-5 , _lowerCamelCase="group" , _lowerCamelCase="gelu" , _lowerCamelCase=(512, 512, 512, 512, 512, 512, 512) , _lowerCamelCase=(5, 2, 2, 2, 2, 2, 2) , _lowerCamelCase=(10, 3, 3, 3, 3, 2, 2) , _lowerCamelCase=False , _lowerCamelCase=128 , _lowerCamelCase=16 , _lowerCamelCase=320 , _lowerCamelCase=800 , _lowerCamelCase=False , _lowerCamelCase=True , _lowerCamelCase=0.0_5 , _lowerCamelCase=10 , _lowerCamelCase=2 , _lowerCamelCase=0.0 , _lowerCamelCase=10 , _lowerCamelCase=320 , _lowerCamelCase=2 , _lowerCamelCase=0.1 , _lowerCamelCase=100 , _lowerCamelCase=256 , _lowerCamelCase=256 , _lowerCamelCase=0.1 , _lowerCamelCase="mean" , _lowerCamelCase=False , _lowerCamelCase=False , _lowerCamelCase=256 , _lowerCamelCase=(512, 512, 512, 512, 1500) , _lowerCamelCase=(5, 3, 3, 1, 1) , _lowerCamelCase=(1, 2, 3, 1, 1) , _lowerCamelCase=512 , _lowerCamelCase=80 , _lowerCamelCase=0 , _lowerCamelCase=1 , _lowerCamelCase=2 , _lowerCamelCase=False , _lowerCamelCase=3 , _lowerCamelCase=2 , _lowerCamelCase=3 , _lowerCamelCase=None , **_lowerCamelCase , ) ->Optional[Any]: super().__init__(**_lowerCamelCase , pad_token_id=_lowerCamelCase , bos_token_id=_lowerCamelCase , eos_token_id=_lowerCamelCase ) SCREAMING_SNAKE_CASE : List[str] = hidden_size SCREAMING_SNAKE_CASE : Any = feat_extract_norm SCREAMING_SNAKE_CASE : Dict = feat_extract_activation SCREAMING_SNAKE_CASE : Union[str, Any] = list(_lowerCamelCase ) SCREAMING_SNAKE_CASE : List[Any] = list(_lowerCamelCase ) SCREAMING_SNAKE_CASE : Optional[int] = list(_lowerCamelCase ) SCREAMING_SNAKE_CASE : Any = conv_bias SCREAMING_SNAKE_CASE : Optional[int] = num_buckets SCREAMING_SNAKE_CASE : List[str] = max_bucket_distance SCREAMING_SNAKE_CASE : Dict = num_conv_pos_embeddings SCREAMING_SNAKE_CASE : Dict = num_conv_pos_embedding_groups SCREAMING_SNAKE_CASE : Optional[int] = len(self.conv_dim ) SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Dict = num_attention_heads SCREAMING_SNAKE_CASE : int = hidden_dropout SCREAMING_SNAKE_CASE : Dict = attention_dropout SCREAMING_SNAKE_CASE : Tuple = activation_dropout SCREAMING_SNAKE_CASE : Optional[Any] = feat_proj_dropout SCREAMING_SNAKE_CASE : Any = final_dropout SCREAMING_SNAKE_CASE : List[Any] = layerdrop SCREAMING_SNAKE_CASE : Tuple = layer_norm_eps SCREAMING_SNAKE_CASE : int = initializer_range SCREAMING_SNAKE_CASE : int = num_ctc_classes SCREAMING_SNAKE_CASE : Optional[int] = vocab_size SCREAMING_SNAKE_CASE : int = do_stable_layer_norm SCREAMING_SNAKE_CASE : List[str] = use_weighted_layer_sum SCREAMING_SNAKE_CASE : str = classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' F""" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,""" F""" `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 SCREAMING_SNAKE_CASE : List[Any] = apply_spec_augment SCREAMING_SNAKE_CASE : Tuple = mask_time_prob SCREAMING_SNAKE_CASE : str = mask_time_length SCREAMING_SNAKE_CASE : Union[str, Any] = mask_time_min_masks SCREAMING_SNAKE_CASE : Tuple = mask_feature_prob SCREAMING_SNAKE_CASE : Union[str, Any] = mask_feature_length # parameters for pretraining with codevector quantized representations SCREAMING_SNAKE_CASE : int = num_codevectors_per_group SCREAMING_SNAKE_CASE : Any = num_codevector_groups SCREAMING_SNAKE_CASE : Union[str, Any] = contrastive_logits_temperature SCREAMING_SNAKE_CASE : List[Any] = num_negatives SCREAMING_SNAKE_CASE : str = codevector_dim SCREAMING_SNAKE_CASE : Union[str, Any] = proj_codevector_dim SCREAMING_SNAKE_CASE : Dict = diversity_loss_weight # ctc loss SCREAMING_SNAKE_CASE : List[str] = ctc_loss_reduction SCREAMING_SNAKE_CASE : int = ctc_zero_infinity # adapter SCREAMING_SNAKE_CASE : Union[str, Any] = add_adapter SCREAMING_SNAKE_CASE : List[str] = adapter_kernel_size SCREAMING_SNAKE_CASE : Tuple = adapter_stride SCREAMING_SNAKE_CASE : Dict = num_adapter_layers SCREAMING_SNAKE_CASE : str = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. SCREAMING_SNAKE_CASE : Any = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. SCREAMING_SNAKE_CASE : Tuple = list(_lowerCamelCase ) SCREAMING_SNAKE_CASE : Optional[int] = list(_lowerCamelCase ) SCREAMING_SNAKE_CASE : int = list(_lowerCamelCase ) SCREAMING_SNAKE_CASE : Optional[int] = xvector_output_dim @property def __lowerCAmelCase ( self ) ->int: return functools.reduce(operator.mul , self.conv_stride , 1 )
313
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) else: return a * actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(__snake_case , __snake_case ) return actual_power(__snake_case , __snake_case ) if __name__ == "__main__": print(power(-2, -3))
5
0
import argparse import collections import os import re 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_table.py snake_case_ : Tuple = "src/transformers" snake_case_ : Union[str, Any] = "docs/source/en" snake_case_ : str = "." def A (__A : Union[str, Any] , __A : str , __A : Dict ) -> List[Any]: """simple docstring""" with open(__snake_case , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: UpperCAmelCase_ = f.readlines() # Find the start prompt. UpperCAmelCase_ = 0 while not lines[start_index].startswith(__snake_case ): start_index += 1 start_index += 1 UpperCAmelCase_ = start_index while not lines[end_index].startswith(__snake_case ): 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 # Add here suffixes that are used to identify models, separated by | snake_case_ : Dict = "Model|Encoder|Decoder|ForConditionalGeneration" # Regexes that match TF/Flax/PT model names. snake_case_ : Any = re.compile(r"TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)") snake_case_ : List[str] = re.compile(r"Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)") # Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes. snake_case_ : List[Any] = re.compile(r"(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)") # This is to make sure the transformers module imported is the one in the repo. snake_case_ : int = direct_transformers_import(TRANSFORMERS_PATH) def A (__A : List[str] ) -> Tuple: """simple docstring""" UpperCAmelCase_ = re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)''' , __snake_case ) return [m.group(0 ) for m in matches] def A (__A : Optional[Any] , __A : List[Any] ) -> str: """simple docstring""" UpperCAmelCase_ = 2 if text == '''✅''' or text == '''❌''' else len(__snake_case ) UpperCAmelCase_ = (width - text_length) // 2 UpperCAmelCase_ = width - text_length - left_indent return " " * left_indent + text + " " * right_indent def A () -> Optional[Any]: """simple docstring""" UpperCAmelCase_ = transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES UpperCAmelCase_ = { name: config_maping_names[code] for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if code in config_maping_names } UpperCAmelCase_ = {name: config.replace('''Config''' , '''''' ) for name, config in model_name_to_config.items()} # Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax. UpperCAmelCase_ = collections.defaultdict(__snake_case ) UpperCAmelCase_ = collections.defaultdict(__snake_case ) UpperCAmelCase_ = collections.defaultdict(__snake_case ) UpperCAmelCase_ = collections.defaultdict(__snake_case ) UpperCAmelCase_ = collections.defaultdict(__snake_case ) # Let's lookup through all transformers object (once). for attr_name in dir(__snake_case ): UpperCAmelCase_ = None if attr_name.endswith('''Tokenizer''' ): UpperCAmelCase_ = slow_tokenizers UpperCAmelCase_ = attr_name[:-9] elif attr_name.endswith('''TokenizerFast''' ): UpperCAmelCase_ = fast_tokenizers UpperCAmelCase_ = attr_name[:-13] elif _re_tf_models.match(__snake_case ) is not None: UpperCAmelCase_ = tf_models UpperCAmelCase_ = _re_tf_models.match(__snake_case ).groups()[0] elif _re_flax_models.match(__snake_case ) is not None: UpperCAmelCase_ = flax_models UpperCAmelCase_ = _re_flax_models.match(__snake_case ).groups()[0] elif _re_pt_models.match(__snake_case ) is not None: UpperCAmelCase_ = pt_models UpperCAmelCase_ = _re_pt_models.match(__snake_case ).groups()[0] if lookup_dict is not None: while len(__snake_case ) > 0: if attr_name in model_name_to_prefix.values(): UpperCAmelCase_ = True break # Try again after removing the last word in the name UpperCAmelCase_ = ''''''.join(camel_case_split(__snake_case )[:-1] ) # Let's build that table! UpperCAmelCase_ = list(model_name_to_config.keys() ) model_names.sort(key=str.lower ) UpperCAmelCase_ = ['''Model''', '''Tokenizer slow''', '''Tokenizer fast''', '''PyTorch support''', '''TensorFlow support''', '''Flax Support'''] # We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side). UpperCAmelCase_ = [len(__snake_case ) + 2 for c in columns] UpperCAmelCase_ = max([len(__snake_case ) for name in model_names] ) + 2 # Build the table per se UpperCAmelCase_ = '''|''' + '''|'''.join([_center_text(__snake_case , __snake_case ) for c, w in zip(__snake_case , __snake_case )] ) + '''|\n''' # Use ":-----:" format to center-aligned table cell texts table += "|" + "|".join([''':''' + '''-''' * (w - 2) + ''':''' for w in widths] ) + "|\n" UpperCAmelCase_ = {True: '''✅''', False: '''❌'''} for name in model_names: UpperCAmelCase_ = model_name_to_prefix[name] UpperCAmelCase_ = [ name, check[slow_tokenizers[prefix]], check[fast_tokenizers[prefix]], check[pt_models[prefix]], check[tf_models[prefix]], check[flax_models[prefix]], ] table += "|" + "|".join([_center_text(__snake_case , __snake_case ) for l, w in zip(__snake_case , __snake_case )] ) + "|\n" return table def A (__A : Optional[int]=False ) -> List[str]: """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = _find_text_in_file( filename=os.path.join(__snake_case , '''index.md''' ) , start_prompt='''<!--This table is updated automatically from the auto modules''' , end_prompt='''<!-- End table-->''' , ) UpperCAmelCase_ = get_model_table_from_auto_modules() if current_table != new_table: if overwrite: with open(os.path.join(__snake_case , '''index.md''' ) , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(lines[:start_index] + [new_table] + lines[end_index:] ) else: raise ValueError( '''The model table in the `index.md` has not been updated. Run `make fix-copies` to fix this.''' ) if __name__ == "__main__": snake_case_ : Tuple = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") snake_case_ : List[Any] = parser.parse_args() check_model_table(args.fix_and_overwrite)
51
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase = 1_6 , UpperCAmelCase = 8_8 , UpperCAmelCase = None , UpperCAmelCase = 1 , UpperCAmelCase = 0.0 , UpperCAmelCase = 3_2 , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "geglu" , UpperCAmelCase = None , ) -> Any: super().__init__() _lowercase =nn.ModuleList( [ TransformeraDModel( num_attention_heads=UpperCAmelCase , attention_head_dim=UpperCAmelCase , in_channels=UpperCAmelCase , num_layers=UpperCAmelCase , dropout=UpperCAmelCase , norm_num_groups=UpperCAmelCase , cross_attention_dim=UpperCAmelCase , attention_bias=UpperCAmelCase , sample_size=UpperCAmelCase , num_vector_embeds=UpperCAmelCase , activation_fn=UpperCAmelCase , num_embeds_ada_norm=UpperCAmelCase , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _lowercase =0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _lowercase =[7_7, 2_5_7] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _lowercase =[1, 0] def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = True , ) -> str: _lowercase =hidden_states _lowercase =[] _lowercase =0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _lowercase =encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _lowercase =self.transformer_index_for_condition[i] _lowercase =self.transformers[transformer_index]( UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , timestep=UpperCAmelCase , cross_attention_kwargs=UpperCAmelCase , return_dict=UpperCAmelCase , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _lowercase =encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _lowercase =output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=UpperCAmelCase )
5
0
a ={ """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""": """--..""", """1""": """.----""", """2""": """..---""", """3""": """...--""", """4""": """....-""", """5""": """.....""", """6""": """-....""", """7""": """--...""", """8""": """---..""", """9""": """----.""", """0""": """-----""", """&""": """.-...""", """@""": """.--.-.""", """:""": """---...""", """,""": """--..--""", """.""": """.-.-.-""", """\'""": """.----.""", """\"""": """.-..-.""", """?""": """..--..""", """/""": """-..-.""", """=""": """-...-""", """+""": """.-.-.""", """-""": """-....-""", """(""": """-.--.""", """)""": """-.--.-""", """!""": """-.-.--""", """ """: """/""" } # Exclamation mark is not in ITU-R recommendation # fmt: on a ={value: key for key, value in MORSE_CODE_DICT.items()} def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> str: return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> str: return "".join(REVERSE_DICT[char] for char in message.split() ) def SCREAMING_SNAKE_CASE__ ( ) -> None: __lowerCamelCase : Dict = 'Morse code here!' print(__snake_case ) __lowerCamelCase : List[Any] = encrypt(__snake_case ) print(__snake_case ) __lowerCamelCase : Union[str, Any] = decrypt(__snake_case ) print(__snake_case ) if __name__ == "__main__": main()
73
import heapq as hq import math from collections.abc import Iterator class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> Any: _lowercase =str(id_ ) _lowercase =None _lowercase =None _lowercase =[] _lowercase ={} # {vertex:distance} def __lt__(self , UpperCAmelCase ) -> List[str]: return self.key < other.key def __repr__(self ) -> str: return self.id def __A (self , UpperCAmelCase ) -> Dict: self.neighbors.append(UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =weight def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __snake_case ) graph[b - 1].add_edge(graph[a - 1] , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> list: """simple docstring""" _lowercase =[] for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =graph[:] while q: _lowercase =min(__snake_case ) q.remove(__snake_case ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] for i in range(1 , len(__snake_case ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Iterator[tuple]: """simple docstring""" for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =list(__snake_case ) hq.heapify(__snake_case ) while h: _lowercase =hq.heappop(__snake_case ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] hq.heapify(__snake_case ) for i in range(1 , len(__snake_case ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def UpperCAmelCase_ ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
5
0
class __SCREAMING_SNAKE_CASE : def __init__( self : Optional[Any] ) ->Tuple: lowerCamelCase__ : str = '''''' lowerCamelCase__ : int = '''''' lowerCamelCase__ : List[str] = [] def __lowerCamelCase ( self : Tuple , A : Optional[Any] , A : Dict ) ->int: if m == -1: return n + 1 elif n == -1: return m + 1 elif self.dp[m][n] > -1: return self.dp[m][n] else: if self.worda[m] == self.worda[n]: lowerCamelCase__ : List[str] = self.__min_dist_top_down_dp(m - 1 , n - 1 ) else: lowerCamelCase__ : str = self.__min_dist_top_down_dp(A , n - 1 ) lowerCamelCase__ : str = self.__min_dist_top_down_dp(m - 1 , A ) lowerCamelCase__ : List[Any] = self.__min_dist_top_down_dp(m - 1 , n - 1 ) lowerCamelCase__ : Any = 1 + min(A , A , A ) return self.dp[m][n] def __lowerCamelCase ( self : Dict , A : int , A : Any ) ->int: lowerCamelCase__ : Union[str, Any] = worda lowerCamelCase__ : Optional[Any] = worda lowerCamelCase__ : Tuple = [[-1 for _ in range(len(A ) )] for _ in range(len(A ) )] return self.__min_dist_top_down_dp(len(A ) - 1 , len(A ) - 1 ) def __lowerCamelCase ( self : List[Any] , A : List[Any] , A : Dict ) ->int: lowerCamelCase__ : Optional[Any] = worda lowerCamelCase__ : Dict = worda lowerCamelCase__ : Optional[Any] = len(A ) lowerCamelCase__ : Optional[Any] = len(A ) lowerCamelCase__ : Tuple = [[0 for _ in range(n + 1 )] for _ in range(m + 1 )] for i in range(m + 1 ): for j in range(n + 1 ): if i == 0: # first string is empty lowerCamelCase__ : int = j elif j == 0: # second string is empty lowerCamelCase__ : str = i elif worda[i - 1] == worda[j - 1]: # last characters are equal lowerCamelCase__ : List[Any] = self.dp[i - 1][j - 1] else: lowerCamelCase__ : int = self.dp[i][j - 1] lowerCamelCase__ : int = self.dp[i - 1][j] lowerCamelCase__ : str = self.dp[i - 1][j - 1] lowerCamelCase__ : Union[str, Any] = 1 + min(A , A , A ) return self.dp[m][n] if __name__ == "__main__": _A : str = EditDistance() print('****************** Testing Edit Distance DP Algorithm ******************') print() _A : int = input('Enter the first string: ').strip() _A : int = input('Enter the second string: ').strip() print() print(F'''The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}''') print(F'''The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}''') print() print('*************** End of Testing Edit Distance DP Algorithm ***************')
142
# flake8: noqa # Lint as: python3 UpperCAmelCase__ = [ '''VerificationMode''', '''Version''', '''disable_progress_bar''', '''enable_progress_bar''', '''is_progress_bar_enabled''', '''experimental''', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
5
0
'''simple docstring''' import argparse import torch from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert from transformers.utils import logging logging.set_verbosity_info() def __UpperCAmelCase ( a_: str, a_: List[str], a_: Dict ): _UpperCAmelCase : Union[str, Any] = LxmertConfig.from_json_file(__snake_case ) print(f"""Building PyTorch model from configuration: {config}""" ) _UpperCAmelCase : Optional[int] = LxmertForPreTraining(__snake_case ) # Load weights from tf checkpoint load_tf_weights_in_lxmert(__snake_case, __snake_case, __snake_case ) # Save pytorch-model print(f"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict(), __snake_case ) if __name__ == "__main__": __a = 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 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 = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
145
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''wavlm''' def __init__(self , UpperCAmelCase=3_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase="group" , UpperCAmelCase="gelu" , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase=False , UpperCAmelCase=1_2_8 , UpperCAmelCase=1_6 , UpperCAmelCase=3_2_0 , UpperCAmelCase=8_0_0 , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.05 , UpperCAmelCase=1_0 , UpperCAmelCase=2 , UpperCAmelCase=0.0 , UpperCAmelCase=1_0 , UpperCAmelCase=3_2_0 , UpperCAmelCase=2 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_0 , UpperCAmelCase=2_5_6 , UpperCAmelCase=2_5_6 , UpperCAmelCase=0.1 , UpperCAmelCase="mean" , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=2_5_6 , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase=(5, 3, 3, 1, 1) , UpperCAmelCase=(1, 2, 3, 1, 1) , UpperCAmelCase=5_1_2 , UpperCAmelCase=8_0 , UpperCAmelCase=0 , UpperCAmelCase=1 , UpperCAmelCase=2 , UpperCAmelCase=False , UpperCAmelCase=3 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase , pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase ) _lowercase =hidden_size _lowercase =feat_extract_norm _lowercase =feat_extract_activation _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =conv_bias _lowercase =num_buckets _lowercase =max_bucket_distance _lowercase =num_conv_pos_embeddings _lowercase =num_conv_pos_embedding_groups _lowercase =len(self.conv_dim ) _lowercase =num_hidden_layers _lowercase =intermediate_size _lowercase =hidden_act _lowercase =num_attention_heads _lowercase =hidden_dropout _lowercase =attention_dropout _lowercase =activation_dropout _lowercase =feat_proj_dropout _lowercase =final_dropout _lowercase =layerdrop _lowercase =layer_norm_eps _lowercase =initializer_range _lowercase =num_ctc_classes _lowercase =vocab_size _lowercase =do_stable_layer_norm _lowercase =use_weighted_layer_sum _lowercase =classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _lowercase =apply_spec_augment _lowercase =mask_time_prob _lowercase =mask_time_length _lowercase =mask_time_min_masks _lowercase =mask_feature_prob _lowercase =mask_feature_length # parameters for pretraining with codevector quantized representations _lowercase =num_codevectors_per_group _lowercase =num_codevector_groups _lowercase =contrastive_logits_temperature _lowercase =num_negatives _lowercase =codevector_dim _lowercase =proj_codevector_dim _lowercase =diversity_loss_weight # ctc loss _lowercase =ctc_loss_reduction _lowercase =ctc_zero_infinity # adapter _lowercase =add_adapter _lowercase =adapter_kernel_size _lowercase =adapter_stride _lowercase =num_adapter_layers _lowercase =output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _lowercase =classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =xvector_output_dim @property def __A (self ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
5
0
'''simple docstring''' import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( "The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion" ) __A : Dict = None __A : int = { "7B": 1_1008, "13B": 1_3824, "30B": 1_7920, "65B": 2_2016, "70B": 2_8672, } __A : List[Any] = { "7B": 1, "7Bf": 1, "13B": 2, "13Bf": 2, "30B": 4, "65B": 8, "70B": 8, "70Bf": 8, } def UpperCamelCase_ ( A__ : List[Any] , A__ : int=1 , A__ : Dict=2_56 ): '''simple docstring''' return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def UpperCamelCase_ ( A__ : Tuple ): '''simple docstring''' with open(__snake_case , """r""" ) as f: return json.load(__snake_case ) def UpperCamelCase_ ( A__ : Optional[Any] , A__ : int ): '''simple docstring''' with open(__snake_case , """w""" ) as f: json.dump(__snake_case , __snake_case ) def UpperCamelCase_ ( A__ : List[str] , A__ : Any , A__ : Dict , A__ : int=True ): '''simple docstring''' os.makedirs(__snake_case , exist_ok=__snake_case ) lowerCAmelCase_ : Union[str, Any] = os.path.join(__snake_case , """tmp""" ) os.makedirs(__snake_case , exist_ok=__snake_case ) lowerCAmelCase_ : int = read_json(os.path.join(__snake_case , """params.json""" ) ) lowerCAmelCase_ : int = NUM_SHARDS[model_size] lowerCAmelCase_ : List[str] = params["""n_layers"""] lowerCAmelCase_ : Optional[int] = params["""n_heads"""] lowerCAmelCase_ : Tuple = n_heads // num_shards lowerCAmelCase_ : int = params["""dim"""] lowerCAmelCase_ : Optional[Any] = dim // n_heads lowerCAmelCase_ : Tuple = 1_00_00.0 lowerCAmelCase_ : List[str] = 1.0 / (base ** (torch.arange(0 , __snake_case , 2 ).float() / dims_per_head)) if "n_kv_heads" in params: lowerCAmelCase_ : Any = params["""n_kv_heads"""] # for GQA / MQA lowerCAmelCase_ : str = n_heads_per_shard // num_key_value_heads lowerCAmelCase_ : int = dim // num_key_value_heads else: # compatibility with other checkpoints lowerCAmelCase_ : int = n_heads lowerCAmelCase_ : Optional[Any] = n_heads_per_shard lowerCAmelCase_ : int = dim # permute for sliced rotary def permute(A__ : int , A__ : List[Any]=n_heads , A__ : Any=dim , A__ : str=dim ): return w.view(__snake_case , dima // n_heads // 2 , 2 , __snake_case ).transpose(1 , 2 ).reshape(__snake_case , __snake_case ) print(f'Fetching all parameters from the checkpoint at {input_base_path}.' ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) lowerCAmelCase_ : List[str] = torch.load(os.path.join(__snake_case , """consolidated.00.pth""" ) , map_location="""cpu""" ) else: # Sharded lowerCAmelCase_ : Tuple = [ torch.load(os.path.join(__snake_case , f'consolidated.{i:02d}.pth' ) , map_location="""cpu""" ) for i in range(__snake_case ) ] lowerCAmelCase_ : int = 0 lowerCAmelCase_ : Any = {"""weight_map""": {}} for layer_i in range(__snake_case ): lowerCAmelCase_ : Any = f'pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin' if model_size == "7B": # Unsharded lowerCAmelCase_ : Optional[Any] = { f'model.layers.{layer_i}.self_attn.q_proj.weight': permute( loaded[f'layers.{layer_i}.attention.wq.weight'] ), f'model.layers.{layer_i}.self_attn.k_proj.weight': permute( loaded[f'layers.{layer_i}.attention.wk.weight'] ), f'model.layers.{layer_i}.self_attn.v_proj.weight': loaded[f'layers.{layer_i}.attention.wv.weight'], f'model.layers.{layer_i}.self_attn.o_proj.weight': loaded[f'layers.{layer_i}.attention.wo.weight'], f'model.layers.{layer_i}.mlp.gate_proj.weight': loaded[f'layers.{layer_i}.feed_forward.w1.weight'], f'model.layers.{layer_i}.mlp.down_proj.weight': loaded[f'layers.{layer_i}.feed_forward.w2.weight'], f'model.layers.{layer_i}.mlp.up_proj.weight': loaded[f'layers.{layer_i}.feed_forward.w3.weight'], f'model.layers.{layer_i}.input_layernorm.weight': loaded[f'layers.{layer_i}.attention_norm.weight'], f'model.layers.{layer_i}.post_attention_layernorm.weight': loaded[f'layers.{layer_i}.ffn_norm.weight'], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. lowerCAmelCase_ : List[Any] = { f'model.layers.{layer_i}.input_layernorm.weight': loaded[0][ f'layers.{layer_i}.attention_norm.weight' ].clone(), f'model.layers.{layer_i}.post_attention_layernorm.weight': loaded[0][ f'layers.{layer_i}.ffn_norm.weight' ].clone(), } lowerCAmelCase_ : Optional[Any] = permute( torch.cat( [ loaded[i][f'layers.{layer_i}.attention.wq.weight'].view(__snake_case , __snake_case , __snake_case ) for i in range(__snake_case ) ] , dim=0 , ).reshape(__snake_case , __snake_case ) ) lowerCAmelCase_ : Tuple = permute( torch.cat( [ loaded[i][f'layers.{layer_i}.attention.wk.weight'].view( __snake_case , __snake_case , __snake_case ) for i in range(__snake_case ) ] , dim=0 , ).reshape(__snake_case , __snake_case ) , __snake_case , __snake_case , __snake_case , ) lowerCAmelCase_ : List[Any] = torch.cat( [ loaded[i][f'layers.{layer_i}.attention.wv.weight'].view( __snake_case , __snake_case , __snake_case ) for i in range(__snake_case ) ] , dim=0 , ).reshape(__snake_case , __snake_case ) lowerCAmelCase_ : Optional[Any] = torch.cat( [loaded[i][f'layers.{layer_i}.attention.wo.weight'] for i in range(__snake_case )] , dim=1 ) lowerCAmelCase_ : List[str] = torch.cat( [loaded[i][f'layers.{layer_i}.feed_forward.w1.weight'] for i in range(__snake_case )] , dim=0 ) lowerCAmelCase_ : List[str] = torch.cat( [loaded[i][f'layers.{layer_i}.feed_forward.w2.weight'] for i in range(__snake_case )] , dim=1 ) lowerCAmelCase_ : Tuple = torch.cat( [loaded[i][f'layers.{layer_i}.feed_forward.w3.weight'] for i in range(__snake_case )] , dim=0 ) lowerCAmelCase_ : List[Any] = inv_freq for k, v in state_dict.items(): lowerCAmelCase_ : List[Any] = filename param_count += v.numel() torch.save(__snake_case , os.path.join(__snake_case , __snake_case ) ) lowerCAmelCase_ : int = f'pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin' if model_size == "7B": # Unsharded lowerCAmelCase_ : Any = { """model.embed_tokens.weight""": loaded["""tok_embeddings.weight"""], """model.norm.weight""": loaded["""norm.weight"""], """lm_head.weight""": loaded["""output.weight"""], } else: lowerCAmelCase_ : List[str] = { """model.norm.weight""": loaded[0]["""norm.weight"""], """model.embed_tokens.weight""": torch.cat( [loaded[i]["""tok_embeddings.weight"""] for i in range(__snake_case )] , dim=1 ), """lm_head.weight""": torch.cat([loaded[i]["""output.weight"""] for i in range(__snake_case )] , dim=0 ), } for k, v in state_dict.items(): lowerCAmelCase_ : Union[str, Any] = filename param_count += v.numel() torch.save(__snake_case , os.path.join(__snake_case , __snake_case ) ) # Write configs lowerCAmelCase_ : List[str] = {"""total_size""": param_count * 2} write_json(__snake_case , os.path.join(__snake_case , """pytorch_model.bin.index.json""" ) ) lowerCAmelCase_ : str = params["""ffn_dim_multiplier"""] if """ffn_dim_multiplier""" in params else 1 lowerCAmelCase_ : Optional[Any] = params["""multiple_of"""] if """multiple_of""" in params else 2_56 lowerCAmelCase_ : str = LlamaConfig( hidden_size=__snake_case , intermediate_size=compute_intermediate_size(__snake_case , __snake_case , __snake_case ) , num_attention_heads=params["""n_heads"""] , num_hidden_layers=params["""n_layers"""] , rms_norm_eps=params["""norm_eps"""] , num_key_value_heads=__snake_case , ) config.save_pretrained(__snake_case ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print("""Loading the checkpoint in a Llama model.""" ) lowerCAmelCase_ : List[str] = LlamaForCausalLM.from_pretrained(__snake_case , torch_dtype=torch.floataa , low_cpu_mem_usage=__snake_case ) # Avoid saving this as part of the config. del model.config._name_or_path print("""Saving in the Transformers format.""" ) model.save_pretrained(__snake_case , safe_serialization=__snake_case ) shutil.rmtree(__snake_case ) def UpperCamelCase_ ( A__ : str , A__ : str ): '''simple docstring''' lowerCAmelCase_ : Tuple = LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(f'Saving a {tokenizer_class.__name__} to {tokenizer_path}.' ) lowerCAmelCase_ : List[str] = tokenizer_class(__snake_case ) tokenizer.save_pretrained(__snake_case ) def UpperCamelCase_ ( ): '''simple docstring''' lowerCAmelCase_ : Dict = argparse.ArgumentParser() parser.add_argument( """--input_dir""" , help="""Location of LLaMA weights, which contains tokenizer.model and model folders""" , ) parser.add_argument( """--model_size""" , choices=["""7B""", """7Bf""", """13B""", """13Bf""", """30B""", """65B""", """70B""", """70Bf""", """tokenizer_only"""] , ) parser.add_argument( """--output_dir""" , help="""Location to write HF model and tokenizer""" , ) parser.add_argument("""--safe_serialization""" , type=__snake_case , help="""Whether or not to save using `safetensors`.""" ) lowerCAmelCase_ : Union[str, Any] = parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir , input_base_path=os.path.join(args.input_dir , args.model_size ) , model_size=args.model_size , safe_serialization=args.safe_serialization , ) lowerCAmelCase_ : Optional[int] = os.path.join(args.input_dir , """tokenizer.model""" ) write_tokenizer(args.output_dir , __snake_case ) if __name__ == "__main__": main()
120
import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class lowerCamelCase__ ( unittest.TestCase): def __A (self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() def __A (self ) -> Optional[Any]: _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _lowercase ='''xvjiarui/stable-diffusion-2-inpainting''' _lowercase , _lowercase =FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase , safety_checker=UpperCAmelCase ) _lowercase ='''Face of a yellow cat, high resolution, sitting on a park bench''' _lowercase =jax.random.PRNGKey(0 ) _lowercase =5_0 _lowercase =jax.device_count() _lowercase =num_samples * [prompt] _lowercase =num_samples * [init_image] _lowercase =num_samples * [mask_image] _lowercase , _lowercase , _lowercase =pipeline.prepare_inputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # shard inputs and rng _lowercase =replicate(UpperCAmelCase ) _lowercase =jax.random.split(UpperCAmelCase , jax.device_count() ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =pipeline( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , jit=UpperCAmelCase ) _lowercase =output.images.reshape(UpperCAmelCase , 5_1_2 , 5_1_2 , 3 ) _lowercase =images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] _lowercase =jnp.asarray(jax.device_get(image_slice.flatten() ) ) _lowercase =jnp.array( [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084] ) print(f"output_slice: {output_slice}" ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2
5
0
import os def A__ ( SCREAMING_SNAKE_CASE__ = "input.txt") -> int: with open(os.path.join(os.path.dirname(__snake_case) , __snake_case)) as input_file: __snake_case: Any = [ [int(__snake_case) for element in line.split(""",""")] for line in input_file.readlines() ] __snake_case: List[Any] = len(__snake_case) __snake_case: List[Any] = len(matrix[0]) __snake_case: Union[str, Any] = [[-1 for _ in range(__snake_case)] for _ in range(__snake_case)] for i in range(__snake_case): __snake_case: int = matrix[i][0] for j in range(1 , __snake_case): for i in range(__snake_case): __snake_case: Optional[Any] = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 , __snake_case): __snake_case: Any = min( minimal_path_sums[i][j] , minimal_path_sums[i - 1][j] + matrix[i][j]) for i in range(rows - 2 , -1 , -1): __snake_case: Optional[Any] = min( minimal_path_sums[i][j] , minimal_path_sums[i + 1][j] + matrix[i][j]) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums) if __name__ == "__main__": print(f'{solution() = }')
111
import comet # From: unbabel-comet import torch import datasets UpperCAmelCase__ = datasets.logging.get_logger(__name__) UpperCAmelCase__ = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' UpperCAmelCase__ = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' UpperCAmelCase__ = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): def __A (self ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A (self , UpperCAmelCase ) -> Dict: if self.config_name == "default": _lowercase =comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: _lowercase =comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=False ) -> int: if gpus is None: _lowercase =1 if torch.cuda.is_available() else 0 _lowercase ={'''src''': sources, '''mt''': predictions, '''ref''': references} _lowercase =[dict(zip(UpperCAmelCase , UpperCAmelCase ) ) for t in zip(*data.values() )] _lowercase , _lowercase =self.scorer.predict(UpperCAmelCase , gpus=UpperCAmelCase , progress_bar=UpperCAmelCase ) return {"mean_score": mean_score, "scores": scores}
5
0
import argparse import shutil import time from json import JSONDecodeError from logging import getLogger from pathlib import Path from typing import Dict, List import torch from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import ( SeqaSeqDataset, calculate_bleu, calculate_rouge, chunks, lmap, load_json, parse_numeric_n_bool_cl_kwargs, save_json, use_task_specific_params, write_txt_file, ) UpperCAmelCase : List[str] = getLogger(__name__) def _A ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Dict = 8 , SCREAMING_SNAKE_CASE : Tuple = 1_024 , SCREAMING_SNAKE_CASE : Any="val" , SCREAMING_SNAKE_CASE : Optional[int]=None , SCREAMING_SNAKE_CASE : Optional[Any]=False , SCREAMING_SNAKE_CASE : int="summarization" , SCREAMING_SNAKE_CASE : Optional[Any]=None , SCREAMING_SNAKE_CASE : List[Any]=1 , SCREAMING_SNAKE_CASE : Optional[int] = None , SCREAMING_SNAKE_CASE : Optional[Any]="" , **SCREAMING_SNAKE_CASE : int , ): """simple docstring""" a__ : List[str] =str(__snake_case ) assert local_rank is not None torch.distributed.init_process_group(backend="nccl" , rank=__snake_case ) a__ : List[str] =Path(__snake_case ) a__ : List[Any] =save_dir.joinpath(f'''rank_{local_rank}_output.json''' ) torch.cuda.set_device(__snake_case ) a__ : Union[str, Any] =AutoModelForSeqaSeqLM.from_pretrained(__snake_case ).cuda() if fpaa: a__ : Dict =model.half() # determine if we need to increase num_beams use_task_specific_params(__snake_case , __snake_case ) # update config with task specific params a__ : Dict =generate_kwargs.pop("num_beams" , model.config.num_beams ) # AttributeError risk? if num_return_sequences > num_beams: a__ : List[Any] =num_return_sequences a__ : Optional[Any] =AutoTokenizer.from_pretrained(__snake_case ) logger.info(f'''Inferred tokenizer type: {tokenizer.__class__}''' ) # if this is wrong, check config.model_type. if max_source_length is None: a__ : str =tokenizer.model_max_length if prefix is None: a__ : Optional[Any] =prefix or getattr(model.config , "prefix" , "" ) or "" a__ : Tuple =SeqaSeqDataset( __snake_case , __snake_case , __snake_case , max_target_length=1_024 , type_path=__snake_case , n_obs=__snake_case , prefix=__snake_case , **__snake_case , ) # I set shuffle=True for a more accurate progress bar. # If all the longest samples are first, the prog bar estimate is too high at the beginning. a__ : Tuple =ds.make_sortish_sampler(__snake_case , distributed=__snake_case , add_extra_examples=__snake_case , shuffle=__snake_case ) a__ : Union[str, Any] =DataLoader(__snake_case , sampler=__snake_case , batch_size=__snake_case , collate_fn=ds.collate_fn ) a__ : Optional[int] =[] for batch in tqdm(__snake_case ): a__ : List[str] =model.generate( input_ids=batch["input_ids"].to(model.device ) , attention_mask=batch["attention_mask"].to(model.device ) , num_return_sequences=__snake_case , num_beams=__snake_case , **__snake_case , ) a__ : Union[str, Any] =tokenizer.batch_decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) a__ : Any =batch["ids"] if num_return_sequences > 1: a__ : Any =chunks(__snake_case , __snake_case ) # batch size chunks, each of size num_return_seq for i, pred in enumerate(__snake_case ): results.append({"pred": pred, "id": ids[i].item()} ) save_json(__snake_case , __snake_case ) return results, sampler.num_replicas def _A ( ): """simple docstring""" a__ : Optional[Any] =argparse.ArgumentParser( epilog="Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate" ) parser.add_argument("--data_dir" , type=__snake_case , help="like cnn_dm/test.source" ) parser.add_argument( "--model_name" , type=__snake_case , help="like facebook/bart-large-cnn,t5-base, etc." , default="sshleifer/distilbart-xsum-12-3" , ) parser.add_argument("--save_dir" , type=__snake_case , help="where to save" , default="tmp_gen" ) parser.add_argument("--max_source_length" , type=__snake_case , default=__snake_case ) parser.add_argument( "--type_path" , type=__snake_case , default="test" , help="which subset to evaluate typically train/val/test" ) parser.add_argument("--task" , type=__snake_case , default="summarization" , help="used for task_specific_params + metrics" ) parser.add_argument("--bs" , type=__snake_case , default=8 , required=__snake_case , help="batch size" ) parser.add_argument( "--local_rank" , type=__snake_case , default=-1 , required=__snake_case , help="should be passed by distributed.launch" ) parser.add_argument( "--n_obs" , type=__snake_case , default=__snake_case , required=__snake_case , help="How many observations. Defaults to all." ) parser.add_argument( "--num_return_sequences" , type=__snake_case , default=1 , required=__snake_case , help="How many sequences to return" ) parser.add_argument( "--sync_timeout" , type=__snake_case , default=600 , required=__snake_case , help="How long should master process wait for other processes to finish." , ) parser.add_argument("--src_lang" , type=__snake_case , default=__snake_case , required=__snake_case ) parser.add_argument("--tgt_lang" , type=__snake_case , default=__snake_case , required=__snake_case ) parser.add_argument( "--prefix" , type=__snake_case , required=__snake_case , default=__snake_case , help="will be added to the begininng of src examples" ) parser.add_argument("--fp16" , action="store_true" ) parser.add_argument("--debug" , action="store_true" ) a__ : List[str] =time.time() a__ , a__ : Union[str, Any] =parser.parse_known_args() a__ : Optional[int] =parse_numeric_n_bool_cl_kwargs(__snake_case ) if generate_kwargs and args.local_rank <= 0: print(f'''parsed the following generate kwargs: {generate_kwargs}''' ) a__ : Optional[Any] =Path(args.save_dir + "_tmp" ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) # this handles locking. a__ : List[str] =list(json_save_dir.glob("rank_*.json" ) ) if intermediate_files: raise ValueError(f'''Found files at {json_save_dir} please move or remove them.''' ) # In theory, a node could finish and save before another node hits this. If this happens, we can address later. a__ : Optional[int] ={} if args.src_lang is not None: a__ : Union[str, Any] =args.src_lang if args.tgt_lang is not None: a__ : List[Any] =args.tgt_lang Path(args.save_dir ).mkdir(exist_ok=__snake_case ) a__ , a__ : Tuple =eval_data_dir( args.data_dir , __snake_case , args.model_name , type_path=args.type_path , bs=args.bs , fpaa=args.fpaa , task=args.task , local_rank=args.local_rank , n_obs=args.n_obs , max_source_length=args.max_source_length , num_return_sequences=args.num_return_sequences , prefix=args.prefix , dataset_kwargs=__snake_case , **__snake_case , ) if args.local_rank <= 0: a__ : List[Any] =Path(args.save_dir ) save_dir.mkdir(exist_ok=__snake_case ) a__ : Optional[Any] =gather_results_from_each_node(__snake_case , __snake_case , args.sync_timeout ) a__ : Tuple =combine_partial_results(__snake_case ) if args.num_return_sequences > 1: a__ : str =save_dir.joinpath("pseudolabel_results.json" ) print(f'''Saving aggregated results at {save_path}, intermediate in {json_save_dir}/''' ) save_json(__snake_case , __snake_case ) return a__ : List[Any] =Path(args.data_dir ).joinpath(args.type_path + ".target" ) with open(__snake_case ) as f: a__ : List[Any] =[x.rstrip() for x in f.readlines()][: len(__snake_case )] # Calculate metrics, save metrics, and save _generations.txt a__ : Dict ="translation" in args.task a__ : Tuple =calculate_bleu if calc_bleu else calculate_rouge a__ : Dict ="bleu" if calc_bleu else "rouge" a__ : List[str] =score_fn(__snake_case , __snake_case ) a__ : List[Any] =len(__snake_case ) a__ : str =time.time() - start_time a__ : Optional[int] =round(runtime / metrics["n_obs"] , 4 ) a__ : str =num_replicas # TODO(@stas00): add whatever metadata to metrics a__ : str =save_dir.joinpath(f'''{args.type_path}_{metric_name}.json''' ) save_json(__snake_case , __snake_case , indent=__snake_case ) print(__snake_case ) write_txt_file(__snake_case , save_dir.joinpath(f'''{args.type_path}_generations.txt''' ) ) if args.debug: write_txt_file(__snake_case , save_dir.joinpath(f'''{args.type_path}.target''' ) ) else: shutil.rmtree(__snake_case ) def _A ( SCREAMING_SNAKE_CASE : Optional[int] ): """simple docstring""" a__ : Dict =[] for partial_result in partial_results: records.extend(__snake_case ) a__ : Any =sorted(__snake_case , key=lambda SCREAMING_SNAKE_CASE : x["id"] ) a__ : Union[str, Any] =[x["pred"] for x in records] return preds def _A ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Union[str, Any] ): """simple docstring""" a__ : List[str] =time.time() logger.info("waiting for all nodes to finish" ) a__ : Dict =None while (time.time() - start_wait) < timeout: a__ : List[Any] =list(save_dir.glob("rank_*.json" ) ) if len(__snake_case ) < num_replicas: continue try: # make sure all json files are fully saved a__ : Optional[int] =lmap(__snake_case , __snake_case ) return json_data except JSONDecodeError: continue else: raise TimeoutError("Rank 0 gave up on waiting for other processes" ) # Unreachable if __name__ == "__main__": # Usage for MT: run_generate()
95
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 1 @register_to_config def __init__(self , UpperCAmelCase=2_0_0_0 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=1e-3 ) -> List[str]: _lowercase =None _lowercase =None _lowercase =None def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> str: _lowercase =torch.linspace(1 , self.config.sampling_eps , UpperCAmelCase , device=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> Optional[int]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _lowercase =( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _lowercase =torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _lowercase =std.flatten() while len(std.shape ) < len(score.shape ): _lowercase =std.unsqueeze(-1 ) _lowercase =-score / std # compute _lowercase =-1.0 / len(self.timesteps ) _lowercase =self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _lowercase =beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _lowercase =beta_t.unsqueeze(-1 ) _lowercase =-0.5 * beta_t * x _lowercase =torch.sqrt(UpperCAmelCase ) _lowercase =drift - diffusion**2 * score _lowercase =x + drift * dt # add noise _lowercase =randn_tensor(x.shape , layout=x.layout , generator=UpperCAmelCase , device=x.device , dtype=x.dtype ) _lowercase =x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__(self ) -> str: return self.config.num_train_timesteps
5
0
import unittest from transformers import BertGenerationConfig, 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, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import BertGenerationDecoder, BertGenerationEncoder class A_ : def __init__( self : Optional[int] , UpperCAmelCase : int , UpperCAmelCase : Any=1_3 , UpperCAmelCase : int=7 , UpperCAmelCase : Any=True , UpperCAmelCase : Optional[Any]=True , UpperCAmelCase : List[Any]=9_9 , UpperCAmelCase : List[str]=3_2 , UpperCAmelCase : Optional[Any]=5 , UpperCAmelCase : List[Any]=4 , UpperCAmelCase : Optional[Any]=3_7 , UpperCAmelCase : Dict="gelu" , UpperCAmelCase : List[Any]=0.1 , UpperCAmelCase : Any=0.1 , UpperCAmelCase : List[str]=5_0 , UpperCAmelCase : str=0.02 , UpperCAmelCase : Tuple=True , UpperCAmelCase : Tuple=None , ) -> str: __lowerCAmelCase: Optional[int] = parent __lowerCAmelCase: str = batch_size __lowerCAmelCase: Optional[Any] = seq_length __lowerCAmelCase: str = is_training __lowerCAmelCase: Union[str, Any] = use_input_mask __lowerCAmelCase: Any = vocab_size __lowerCAmelCase: Optional[Any] = hidden_size __lowerCAmelCase: Dict = num_hidden_layers __lowerCAmelCase: List[Any] = num_attention_heads __lowerCAmelCase: List[str] = intermediate_size __lowerCAmelCase: Union[str, Any] = hidden_act __lowerCAmelCase: List[str] = hidden_dropout_prob __lowerCAmelCase: Dict = attention_probs_dropout_prob __lowerCAmelCase: Optional[Any] = max_position_embeddings __lowerCAmelCase: Any = initializer_range __lowerCAmelCase: int = use_labels __lowerCAmelCase: Dict = scope def UpperCAmelCase ( self : Optional[Any] ) -> int: __lowerCAmelCase: Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCAmelCase: str = None if self.use_input_mask: __lowerCAmelCase: Dict = random_attention_mask([self.batch_size, self.seq_length] ) if self.use_labels: __lowerCAmelCase: List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCAmelCase: str = self.get_config() return config, input_ids, input_mask, token_labels def UpperCAmelCase ( self : Dict ) -> Any: return BertGenerationConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , ) def UpperCAmelCase ( self : Any ) -> Dict: ( ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ): Union[str, Any] = self.prepare_config_and_inputs() __lowerCAmelCase: List[Any] = True __lowerCAmelCase: Union[str, Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __lowerCAmelCase: Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, token_labels, encoder_hidden_states, encoder_attention_mask, ) def UpperCAmelCase ( self : Optional[int] , UpperCAmelCase : List[str] , UpperCAmelCase : int , UpperCAmelCase : Tuple , UpperCAmelCase : str , **UpperCAmelCase : List[str] , ) -> Tuple: __lowerCAmelCase: Optional[Any] = BertGenerationEncoder(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() __lowerCAmelCase: Any = model(UpperCAmelCase , attention_mask=UpperCAmelCase ) __lowerCAmelCase: Optional[Any] = model(UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase ( self : Tuple , UpperCAmelCase : List[str] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Optional[int] , UpperCAmelCase : List[str] , UpperCAmelCase : List[str] , UpperCAmelCase : List[Any] , **UpperCAmelCase : List[str] , ) -> List[str]: __lowerCAmelCase: Any = True __lowerCAmelCase: int = BertGenerationEncoder(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() __lowerCAmelCase: Dict = model( UpperCAmelCase , attention_mask=UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , encoder_attention_mask=UpperCAmelCase , ) __lowerCAmelCase: Dict = model( UpperCAmelCase , attention_mask=UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase ( self : List[Any] , UpperCAmelCase : Any , UpperCAmelCase : Tuple , UpperCAmelCase : int , UpperCAmelCase : List[Any] , UpperCAmelCase : Any , UpperCAmelCase : Optional[int] , **UpperCAmelCase : str , ) -> Dict: __lowerCAmelCase: Any = True __lowerCAmelCase: Dict = True __lowerCAmelCase: List[Any] = BertGenerationDecoder(config=UpperCAmelCase ).to(UpperCAmelCase ).eval() # first forward pass __lowerCAmelCase: Any = model( UpperCAmelCase , attention_mask=UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , encoder_attention_mask=UpperCAmelCase , use_cache=UpperCAmelCase , ) __lowerCAmelCase: str = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids __lowerCAmelCase: Dict = ids_tensor((self.batch_size, 3) , config.vocab_size ) __lowerCAmelCase: Any = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and __lowerCAmelCase: List[Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) __lowerCAmelCase: Optional[Any] = torch.cat([input_mask, next_mask] , dim=-1 ) __lowerCAmelCase: int = model( UpperCAmelCase , attention_mask=UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , encoder_attention_mask=UpperCAmelCase , output_hidden_states=UpperCAmelCase , )['hidden_states'][0] __lowerCAmelCase: Optional[int] = model( UpperCAmelCase , attention_mask=UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , encoder_attention_mask=UpperCAmelCase , past_key_values=UpperCAmelCase , output_hidden_states=UpperCAmelCase , )['hidden_states'][0] # select random slice __lowerCAmelCase: Union[str, Any] = ids_tensor((1,) , output_from_past.shape[-1] ).item() __lowerCAmelCase: Any = output_from_no_past[:, -3:, random_slice_idx].detach() __lowerCAmelCase: List[Any] = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(UpperCAmelCase , UpperCAmelCase , atol=1E-3 ) ) def UpperCAmelCase ( self : Union[str, Any] , UpperCAmelCase : Optional[int] , UpperCAmelCase : str , UpperCAmelCase : Any , UpperCAmelCase : List[str] , *UpperCAmelCase : List[str] , ) -> Optional[int]: __lowerCAmelCase: int = BertGenerationDecoder(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() __lowerCAmelCase: Optional[Any] = model(UpperCAmelCase , attention_mask=UpperCAmelCase , labels=UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase ( self : str ) -> Tuple: __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase: List[str] = self.prepare_config_and_inputs() __lowerCAmelCase: Union[str, Any] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A_ ( snake_case__ , snake_case__ , snake_case__ , unittest.TestCase ): _lowercase : Optional[int] = (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else () _lowercase : Tuple = (BertGenerationDecoder,) if is_torch_available() else () _lowercase : Optional[Any] = ( {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder} if is_torch_available() else {} ) def UpperCAmelCase ( self : List[str] ) -> Dict: __lowerCAmelCase: List[Any] = BertGenerationEncoderTester(self ) __lowerCAmelCase: Optional[int] = ConfigTester(self , config_class=UpperCAmelCase , hidden_size=3_7 ) def UpperCAmelCase ( self : Optional[Any] ) -> List[str]: self.config_tester.run_common_tests() def UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: __lowerCAmelCase: Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase ) def UpperCAmelCase ( self : Union[str, Any] ) -> List[Any]: __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase: int = self.model_tester.prepare_config_and_inputs() __lowerCAmelCase: Union[str, Any] = 'bert' self.model_tester.create_and_check_model(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def UpperCAmelCase ( self : Dict ) -> Union[str, Any]: __lowerCAmelCase: List[str] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*UpperCAmelCase ) def UpperCAmelCase ( self : Union[str, Any] ) -> List[str]: __lowerCAmelCase: List[Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_decoder_model_past_large_inputs(*UpperCAmelCase ) def UpperCAmelCase ( self : List[str] ) -> int: # This regression test was failing with PyTorch < 1.3 ( ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ): Optional[Any] = self.model_tester.prepare_config_and_inputs_for_decoder() __lowerCAmelCase: str = None self.model_tester.create_and_check_model_as_decoder( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , ) def UpperCAmelCase ( self : int ) -> str: __lowerCAmelCase: List[Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_for_causal_lm(*UpperCAmelCase ) @slow def UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: __lowerCAmelCase: str = BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) self.assertIsNotNone(UpperCAmelCase ) @require_torch class A_ ( unittest.TestCase ): @slow def UpperCAmelCase ( self : List[Any] ) -> List[str]: __lowerCAmelCase: List[str] = BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) __lowerCAmelCase: str = torch.tensor([[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_2_6, 3_8_9_9, 2_0_0_3, 1_0_1_4_0, 1_0_2]] ) with torch.no_grad(): __lowerCAmelCase: Union[str, Any] = model(UpperCAmelCase )[0] __lowerCAmelCase: List[Any] = torch.Size([1, 8, 1_0_2_4] ) self.assertEqual(output.shape , UpperCAmelCase ) __lowerCAmelCase: List[Any] = torch.tensor( [[[0.1775, 0.0083, -0.0321], [1.6002, 0.1287, 0.3912], [2.1473, 0.5791, 0.6066]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , UpperCAmelCase , atol=1E-4 ) ) @require_torch class A_ ( unittest.TestCase ): @slow def UpperCAmelCase ( self : Any ) -> Dict: __lowerCAmelCase: List[Any] = BertGenerationDecoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) __lowerCAmelCase: List[Any] = torch.tensor([[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_2_6, 3_8_9_9, 2_0_0_3, 1_0_1_4_0, 1_0_2]] ) with torch.no_grad(): __lowerCAmelCase: Dict = model(UpperCAmelCase )[0] __lowerCAmelCase: Optional[Any] = torch.Size([1, 8, 5_0_3_5_8] ) self.assertEqual(output.shape , UpperCAmelCase ) __lowerCAmelCase: Dict = torch.tensor( [[[-0.5788, -2.5994, -3.7054], [0.0438, 4.7997, 1.8795], [1.5862, 6.6409, 4.4638]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , UpperCAmelCase , atol=1E-4 ) )
322
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _lowercase =[144, 192, 240] _lowercase =[16, 32, 64, 96, 128, 160, 640] elif "mobilevit_xs" in mobilevit_name: _lowercase =[96, 120, 144] _lowercase =[16, 32, 48, 64, 80, 96, 384] elif "mobilevit_xxs" in mobilevit_name: _lowercase =[64, 80, 96] _lowercase =[16, 16, 24, 48, 64, 80, 320] _lowercase =0.05 _lowercase =2.0 if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =512 _lowercase =16 _lowercase =21 _lowercase ='''pascal-voc-id2label.json''' else: _lowercase =1000 _lowercase ='''imagenet-1k-id2label.json''' _lowercase ='''huggingface/label-files''' _lowercase =json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type='''dataset''' ) , '''r''' ) ) _lowercase ={int(__snake_case ): v for k, v in idalabel.items()} _lowercase =idalabel _lowercase ={v: k for k, v in idalabel.items()} return config def UpperCAmelCase_ ( __snake_case , __snake_case=False ) -> Tuple: """simple docstring""" for i in range(1 , 6 ): if F"layer_{i}." in name: _lowercase =name.replace(F"layer_{i}." , F"encoder.layer.{i - 1}." ) if "conv_1." in name: _lowercase =name.replace('''conv_1.''' , '''conv_stem.''' ) if ".block." in name: _lowercase =name.replace('''.block.''' , '''.''' ) if "exp_1x1" in name: _lowercase =name.replace('''exp_1x1''' , '''expand_1x1''' ) if "red_1x1" in name: _lowercase =name.replace('''red_1x1''' , '''reduce_1x1''' ) if ".local_rep.conv_3x3." in name: _lowercase =name.replace('''.local_rep.conv_3x3.''' , '''.conv_kxk.''' ) if ".local_rep.conv_1x1." in name: _lowercase =name.replace('''.local_rep.conv_1x1.''' , '''.conv_1x1.''' ) if ".norm." in name: _lowercase =name.replace('''.norm.''' , '''.normalization.''' ) if ".conv." in name: _lowercase =name.replace('''.conv.''' , '''.convolution.''' ) if ".conv_proj." in name: _lowercase =name.replace('''.conv_proj.''' , '''.conv_projection.''' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}.layer.{j}." ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}." ) if "expand_1x1" in name: _lowercase =name.replace('''expand_1x1''' , '''downsampling_layer.expand_1x1''' ) if "conv_3x3" in name: _lowercase =name.replace('''conv_3x3''' , '''downsampling_layer.conv_3x3''' ) if "reduce_1x1" in name: _lowercase =name.replace('''reduce_1x1''' , '''downsampling_layer.reduce_1x1''' ) for i in range(2 , 5 ): if F".global_rep.{i}.weight" in name: _lowercase =name.replace(F".global_rep.{i}.weight" , '''.layernorm.weight''' ) if F".global_rep.{i}.bias" in name: _lowercase =name.replace(F".global_rep.{i}.bias" , '''.layernorm.bias''' ) if ".global_rep." in name: _lowercase =name.replace('''.global_rep.''' , '''.transformer.''' ) if ".pre_norm_mha.0." in name: _lowercase =name.replace('''.pre_norm_mha.0.''' , '''.layernorm_before.''' ) if ".pre_norm_mha.1.out_proj." in name: _lowercase =name.replace('''.pre_norm_mha.1.out_proj.''' , '''.attention.output.dense.''' ) if ".pre_norm_ffn.0." in name: _lowercase =name.replace('''.pre_norm_ffn.0.''' , '''.layernorm_after.''' ) if ".pre_norm_ffn.1." in name: _lowercase =name.replace('''.pre_norm_ffn.1.''' , '''.intermediate.dense.''' ) if ".pre_norm_ffn.4." in name: _lowercase =name.replace('''.pre_norm_ffn.4.''' , '''.output.dense.''' ) if ".transformer." in name: _lowercase =name.replace('''.transformer.''' , '''.transformer.layer.''' ) if ".aspp_layer." in name: _lowercase =name.replace('''.aspp_layer.''' , '''.''' ) if ".aspp_pool." in name: _lowercase =name.replace('''.aspp_pool.''' , '''.''' ) if "seg_head." in name: _lowercase =name.replace('''seg_head.''' , '''segmentation_head.''' ) if "segmentation_head.classifier.classifier." in name: _lowercase =name.replace('''segmentation_head.classifier.classifier.''' , '''segmentation_head.classifier.''' ) if "classifier.fc." in name: _lowercase =name.replace('''classifier.fc.''' , '''classifier.''' ) elif (not base_model) and ("segmentation_head." not in name): _lowercase ='''mobilevit.''' + name return name def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=False ) -> Optional[Any]: """simple docstring""" if base_model: _lowercase ='''''' else: _lowercase ='''mobilevit.''' for key in orig_state_dict.copy().keys(): _lowercase =orig_state_dict.pop(__snake_case ) if key[:8] == "encoder.": _lowercase =key[8:] if "qkv" in key: _lowercase =key.split('''.''' ) _lowercase =int(key_split[0][6:] ) - 1 _lowercase =int(key_split[3] ) _lowercase =model.get_submodule(F"{model_prefix}encoder.layer.{layer_num}" ) _lowercase =layer.transformer.layer[transformer_num].attention.attention.all_head_size _lowercase =( F"{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention." ) if "weight" in key: _lowercase =val[:dim, :] _lowercase =val[dim : dim * 2, :] _lowercase =val[-dim:, :] else: _lowercase =val[:dim] _lowercase =val[dim : dim * 2] _lowercase =val[-dim:] else: _lowercase =val return orig_state_dict def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase ='''http://images.cocodataset.org/val2017/000000039769.jpg''' _lowercase =Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) return im @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=False ) -> int: """simple docstring""" _lowercase =get_mobilevit_config(__snake_case ) # load original state_dict _lowercase =torch.load(__snake_case , map_location='''cpu''' ) # load 🤗 model if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =MobileViTForSemanticSegmentation(__snake_case ).eval() else: _lowercase =MobileViTForImageClassification(__snake_case ).eval() _lowercase =convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) # Check outputs on an image, prepared by MobileViTImageProcessor _lowercase =MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _lowercase =image_processor(images=prepare_img() , return_tensors='''pt''' ) _lowercase =model(**__snake_case ) _lowercase =outputs.logits if mobilevit_name.startswith('''deeplabv3_''' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _lowercase =torch.tensor( [ [[6.20_65, 6.12_92, 6.20_70], [6.10_79, 6.12_54, 6.17_47], [6.00_42, 6.10_71, 6.10_34]], [[-6.92_53, -6.86_53, -7.03_98], [-7.32_18, -7.39_83, -7.36_70], [-7.19_61, -7.24_82, -7.15_69]], [[-4.47_23, -4.43_48, -4.37_69], [-5.36_29, -5.46_32, -5.45_98], [-5.15_87, -5.34_02, -5.50_59]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _lowercase =torch.tensor( [ [[5.44_49, 5.57_33, 5.63_14], [5.18_15, 5.39_30, 5.59_63], [5.16_56, 5.43_33, 5.48_53]], [[-9.44_23, -9.77_66, -9.67_14], [-9.15_81, -9.57_20, -9.55_19], [-9.10_06, -9.64_58, -9.57_03]], [[-7.77_21, -7.37_16, -7.15_83], [-8.45_99, -8.06_24, -7.79_44], [-8.41_72, -7.83_66, -7.50_25]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _lowercase =torch.tensor( [ [[6.98_11, 6.97_43, 7.31_23], [7.17_77, 7.19_31, 7.39_38], [7.56_33, 7.80_50, 7.89_01]], [[-10.55_36, -10.23_32, -10.29_24], [-10.23_36, -9.86_24, -9.59_64], [-10.88_40, -10.81_58, -10.66_59]], [[-3.49_38, -3.06_31, -2.86_20], [-3.42_05, -2.81_35, -2.68_75], [-3.41_79, -2.79_45, -2.87_50]], ] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3, :3, :3] , __snake_case , atol=1e-4 ) else: assert logits.shape == (1, 1000) if mobilevit_name == "mobilevit_s": _lowercase =torch.tensor([-0.98_66, 0.23_92, -1.12_41] ) elif mobilevit_name == "mobilevit_xs": _lowercase =torch.tensor([-2.47_61, -0.93_99, -1.95_87] ) elif mobilevit_name == "mobilevit_xxs": _lowercase =torch.tensor([-1.93_64, -1.23_27, -0.46_53] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) print(F"Saving model {mobilevit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__snake_case ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__snake_case ) if push_to_hub: _lowercase ={ '''mobilevit_s''': '''mobilevit-small''', '''mobilevit_xs''': '''mobilevit-x-small''', '''mobilevit_xxs''': '''mobilevit-xx-small''', '''deeplabv3_mobilevit_s''': '''deeplabv3-mobilevit-small''', '''deeplabv3_mobilevit_xs''': '''deeplabv3-mobilevit-x-small''', '''deeplabv3_mobilevit_xxs''': '''deeplabv3-mobilevit-xx-small''', } print('''Pushing to the hub...''' ) _lowercase =model_mapping[mobilevit_name] image_processor.push_to_hub(__snake_case , organization='''apple''' ) model.push_to_hub(__snake_case , organization='''apple''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--mobilevit_name''', default='''mobilevit_s''', type=str, help=( '''Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\',''' ''' \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'.''' ), ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) UpperCAmelCase__ = parser.parse_args() convert_movilevit_checkpoint( args.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
5
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_realm import RealmTokenizer __snake_case : Union[str, Any] = logging.get_logger(__name__) __snake_case : Any = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __snake_case : List[str] = { """vocab_file""": { """google/realm-cc-news-pretrained-embedder""": ( """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt""" ), """google/realm-cc-news-pretrained-encoder""": ( """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt""" ), """google/realm-cc-news-pretrained-scorer""": ( """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt""" ), """google/realm-cc-news-pretrained-openqa""": ( """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt""" ), """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt""", """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt""", """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt""", """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt""", }, """tokenizer_file""": { """google/realm-cc-news-pretrained-embedder""": ( """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont""" ), """google/realm-cc-news-pretrained-encoder""": ( """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json""" ), """google/realm-cc-news-pretrained-scorer""": ( """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json""" ), """google/realm-cc-news-pretrained-openqa""": ( """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json""" ), """google/realm-orqa-nq-openqa""": ( """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json""" ), """google/realm-orqa-nq-reader""": ( """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json""" ), """google/realm-orqa-wq-openqa""": ( """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json""" ), """google/realm-orqa-wq-reader""": ( """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json""" ), }, } __snake_case : Optional[Any] = { """google/realm-cc-news-pretrained-embedder""": 5_12, """google/realm-cc-news-pretrained-encoder""": 5_12, """google/realm-cc-news-pretrained-scorer""": 5_12, """google/realm-cc-news-pretrained-openqa""": 5_12, """google/realm-orqa-nq-openqa""": 5_12, """google/realm-orqa-nq-reader""": 5_12, """google/realm-orqa-wq-openqa""": 5_12, """google/realm-orqa-wq-reader""": 5_12, } __snake_case : Optional[Any] = { """google/realm-cc-news-pretrained-embedder""": {"""do_lower_case""": True}, """google/realm-cc-news-pretrained-encoder""": {"""do_lower_case""": True}, """google/realm-cc-news-pretrained-scorer""": {"""do_lower_case""": True}, """google/realm-cc-news-pretrained-openqa""": {"""do_lower_case""": True}, """google/realm-orqa-nq-openqa""": {"""do_lower_case""": True}, """google/realm-orqa-nq-reader""": {"""do_lower_case""": True}, """google/realm-orqa-wq-openqa""": {"""do_lower_case""": True}, """google/realm-orqa-wq-reader""": {"""do_lower_case""": True}, } class A__(a_ ): """simple docstring""" _A : str = VOCAB_FILES_NAMES _A : str = PRETRAINED_VOCAB_FILES_MAP _A : List[Any] = PRETRAINED_INIT_CONFIGURATION _A : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _A : List[str] = RealmTokenizer def __init__( self , _lowercase=None , _lowercase=None , _lowercase=True , _lowercase="[UNK]" , _lowercase="[SEP]" , _lowercase="[PAD]" , _lowercase="[CLS]" , _lowercase="[MASK]" , _lowercase=True , _lowercase=None , **_lowercase , ) -> Optional[Any]: super().__init__( _lowercase , tokenizer_file=_lowercase , do_lower_case=_lowercase , unk_token=_lowercase , sep_token=_lowercase , pad_token=_lowercase , cls_token=_lowercase , mask_token=_lowercase , tokenize_chinese_chars=_lowercase , strip_accents=_lowercase , **_lowercase , ) a_ : Tuple = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" , _lowercase ) != do_lower_case or normalizer_state.get("""strip_accents""" , _lowercase ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" , _lowercase ) != tokenize_chinese_chars ): a_ : Dict = getattr(_lowercase , normalizer_state.pop("""type""" ) ) a_ : List[Any] = do_lower_case a_ : List[str] = strip_accents a_ : Optional[int] = tokenize_chinese_chars a_ : Optional[Any] = normalizer_class(**_lowercase ) a_ : str = do_lower_case def UpperCamelCase__ ( self , _lowercase , **_lowercase ) -> Any: a_ : Dict = PaddingStrategy.MAX_LENGTH a_ : List[str] = text a_ : List[str] = kwargs.pop("""text_pair""" , _lowercase ) a_ : Optional[Any] = kwargs.pop("""return_tensors""" , _lowercase ) a_ : Optional[Any] = { """input_ids""": [], """attention_mask""": [], """token_type_ids""": [], } for idx, candidate_text in enumerate(_lowercase ): if batch_text_pair is not None: a_ : Optional[int] = batch_text_pair[idx] else: a_ : Union[str, Any] = None a_ : Any = super().__call__(_lowercase , _lowercase , return_tensors=_lowercase , **_lowercase ) a_ : List[str] = encoded_candidates.get("""input_ids""" ) a_ : str = encoded_candidates.get("""attention_mask""" ) a_ : Union[str, Any] = encoded_candidates.get("""token_type_ids""" ) if encoded_input_ids is not None: output_data["input_ids"].append(_lowercase ) if encoded_attention_mask is not None: output_data["attention_mask"].append(_lowercase ) if encoded_token_type_ids is not None: output_data["token_type_ids"].append(_lowercase ) a_ : Optional[Any] = {key: item for key, item in output_data.items() if len(_lowercase ) != 0} return BatchEncoding(_lowercase , tensor_type=_lowercase ) def UpperCamelCase__ ( self , _lowercase , _lowercase=None ) -> Dict: a_ : Tuple = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def UpperCamelCase__ ( self , _lowercase , _lowercase = None ) -> List[int]: a_ : Dict = [self.sep_token_id] a_ : List[str] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCamelCase__ ( self , _lowercase , _lowercase = None ) -> Tuple[str]: a_ : int = self._tokenizer.model.save(_lowercase , name=_lowercase ) return tuple(_lowercase )
248
import requests from bsa import BeautifulSoup def UpperCAmelCase_ ( __snake_case = "https://www.worldometers.info/coronavirus" ) -> dict: """simple docstring""" _lowercase =BeautifulSoup(requests.get(__snake_case ).text , '''html.parser''' ) _lowercase =soup.findAll('''h1''' ) _lowercase =soup.findAll('''div''' , {'''class''': '''maincounter-number'''} ) keys += soup.findAll('''span''' , {'''class''': '''panel-title'''} ) values += soup.findAll('''div''' , {'''class''': '''number-table-main'''} ) return {key.text.strip(): value.text.strip() for key, value in zip(__snake_case , __snake_case )} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
5
0
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 lowercase_ ( _A : Tuple , _A : Any , _A : int=None ): """simple docstring""" assert torch_layer.weight.shape == weight.shape, F"{torch_layer} layer.weight does not match" lowerCamelCase__ : List[str] = nn.Parameter(__snake_case ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F"{torch_layer} layer.bias does not match" lowerCamelCase__ : Tuple = nn.Parameter(__snake_case ) def lowercase_ ( _A : Dict , _A : Tuple , _A : List[Any] ): """simple docstring""" lowerCamelCase__ : Optional[Any] = np.asarray(weights[0] ) lowerCamelCase__ : Tuple = np.asarray(weights[1] ) lowerCamelCase__ : str = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.output.dense , torch.tensor(__snake_case ).view(-1 , __snake_case ).contiguous().transpose(0 , 1 ) , ) def lowercase_ ( _A : List[Any] , _A : List[Any] , _A : Optional[Any] ): """simple docstring""" lowerCamelCase__ : str = np.asarray(weights[0] ) lowerCamelCase__ : Optional[int] = np.asarray(weights[1] ) lowerCamelCase__ : Dict = np.asarray(weights[2] ) lowerCamelCase__ : List[Any] = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.self_attention.key , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.output.dense , torch.tensor(__snake_case ).view(-1 , __snake_case ).contiguous().transpose(0 , 1 ) , ) def lowercase_ ( _A : List[str] , _A : Any , _A : Optional[Any] ): """simple docstring""" lowerCamelCase__ : List[Any] = weights[0][0][0] lowerCamelCase__ : Tuple = np.asarray(layer_norm_a[0] ) lowerCamelCase__ : Any = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(__snake_case ) , torch.tensor(__snake_case ) , ) # lsh weights + output lowerCamelCase__ : Union[str, Any] = weights[0][1] if len(__snake_case ) < 4: set_layer_weights_in_torch_lsh(__snake_case , torch_block.attention , __snake_case ) else: set_layer_weights_in_torch_local(__snake_case , torch_block.attention , __snake_case ) # intermediate weighs lowerCamelCase__ : int = weights[2][0][1][2] # Chunked Feed Forward if len(__snake_case ) == 4: lowerCamelCase__ : Dict = intermediate_weights[2] # layernorm 2 lowerCamelCase__ : Any = np.asarray(intermediate_weights[0][0] ) lowerCamelCase__ : str = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(__snake_case ) , torch.tensor(__snake_case ) , ) # intermediate dense lowerCamelCase__ : Optional[int] = np.asarray(intermediate_weights[1][0] ) lowerCamelCase__ : Tuple = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(__snake_case ).transpose(0 , 1 ).contiguous() , torch.tensor(__snake_case ) , ) # intermediate out lowerCamelCase__ : List[Any] = np.asarray(intermediate_weights[4][0] ) lowerCamelCase__ : str = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(__snake_case ).transpose(0 , 1 ).contiguous() , torch.tensor(__snake_case ) , ) def lowercase_ ( _A : List[Any] , _A : Optional[Any] , _A : Optional[int] ): """simple docstring""" lowerCamelCase__ : List[Any] = torch_model.reformer # word embeds lowerCamelCase__ : Dict = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(__snake_case ) , ) if isinstance(weights[3] , __snake_case ): lowerCamelCase__ : Optional[Any] = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): lowerCamelCase__ : Optional[Any] = 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" lowerCamelCase__ : Any = nn.Parameter(torch.tensor(__snake_case ) ) lowerCamelCase__ : Any = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( __snake_case ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): lowerCamelCase__ : List[str] = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(__snake_case , __snake_case , __snake_case ) # output layer norm lowerCamelCase__ : List[Any] = np.asarray(weights[7][0] ) lowerCamelCase__ : Optional[Any] = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(__snake_case ) , torch.tensor(__snake_case ) , ) # output embeddings lowerCamelCase__ : Union[str, Any] = np.asarray(weights[9][0] ) lowerCamelCase__ : Optional[Any] = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(__snake_case ).transpose(0 , 1 ).contiguous() , torch.tensor(__snake_case ) , ) def lowercase_ ( _A : str , _A : int , _A : List[Any] ): """simple docstring""" lowerCamelCase__ : List[str] = ReformerConfig.from_json_file(__snake_case ) print(F"Building PyTorch model from configuration: {config}" ) lowerCamelCase__ : Optional[int] = ReformerModelWithLMHead(__snake_case ) with open(__snake_case , "rb" ) as f: lowerCamelCase__ : List[str] = pickle.load(__snake_case )["weights"] set_model_weights_in_torch(__snake_case , __snake_case , config.hidden_size ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) torch.save(model.state_dict() , __snake_case ) if __name__ == "__main__": A : Optional[Any] = 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 : int = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
184
from typing import TYPE_CHECKING from ..utils import _LazyModule UpperCAmelCase__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
0
from queue import Queue from typing import TYPE_CHECKING, Optional if TYPE_CHECKING: from ..models.auto import AutoTokenizer class a_ : """simple docstring""" def __lowerCAmelCase ( self , _lowerCamelCase ) ->Dict: raise NotImplementedError() def __lowerCAmelCase ( self ) ->Optional[int]: raise NotImplementedError() class a_ ( a__ ): """simple docstring""" def __init__( self , _lowerCamelCase , _lowerCamelCase = False , **_lowerCamelCase ) ->Any: SCREAMING_SNAKE_CASE : Tuple = tokenizer SCREAMING_SNAKE_CASE : Tuple = skip_prompt SCREAMING_SNAKE_CASE : int = decode_kwargs # variables used in the streaming process SCREAMING_SNAKE_CASE : List[str] = [] SCREAMING_SNAKE_CASE : Optional[Any] = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = True def __lowerCAmelCase ( self , _lowerCamelCase ) ->int: if len(value.shape ) > 1 and value.shape[0] > 1: raise ValueError('''TextStreamer only supports batch size 1''' ) elif len(value.shape ) > 1: SCREAMING_SNAKE_CASE : str = value[0] if self.skip_prompt and self.next_tokens_are_prompt: SCREAMING_SNAKE_CASE : Optional[int] = False return # Add the new token to the cache and decodes the entire thing. self.token_cache.extend(value.tolist() ) SCREAMING_SNAKE_CASE : str = self.tokenizer.decode(self.token_cache , **self.decode_kwargs ) # After the symbol for a new line, we flush the cache. if text.endswith('''\n''' ): SCREAMING_SNAKE_CASE : str = text[self.print_len :] SCREAMING_SNAKE_CASE : Any = [] SCREAMING_SNAKE_CASE : Any = 0 # If the last token is a CJK character, we print the characters. elif len(_lowerCamelCase ) > 0 and self._is_chinese_char(ord(text[-1] ) ): SCREAMING_SNAKE_CASE : Tuple = text[self.print_len :] self.print_len += len(_lowerCamelCase ) # Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words, # which may change with the subsequent token -- there are probably smarter ways to do this!) else: SCREAMING_SNAKE_CASE : Tuple = text[self.print_len : text.rfind(''' ''' ) + 1] self.print_len += len(_lowerCamelCase ) self.on_finalized_text(_lowerCamelCase ) def __lowerCAmelCase ( self ) ->Any: # Flush the cache, if it exists if len(self.token_cache ) > 0: SCREAMING_SNAKE_CASE : Any = self.tokenizer.decode(self.token_cache , **self.decode_kwargs ) SCREAMING_SNAKE_CASE : Any = text[self.print_len :] SCREAMING_SNAKE_CASE : Optional[int] = [] SCREAMING_SNAKE_CASE : List[Any] = 0 else: SCREAMING_SNAKE_CASE : Any = '''''' SCREAMING_SNAKE_CASE : List[Any] = True self.on_finalized_text(_lowerCamelCase , stream_end=_lowerCamelCase ) def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase = False ) ->Union[str, Any]: print(_lowerCamelCase , flush=_lowerCamelCase , end='''''' if not stream_end else None ) def __lowerCAmelCase ( self , _lowerCamelCase ) ->Optional[int]: # This defines a "chinese character" as anything in the CJK Unicode block: # https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) # # Note that the CJK Unicode block is NOT all Japanese and Korean characters, # despite its name. The modern Korean Hangul alphabet is a different block, # as is Japanese Hiragana and Katakana. Those alphabets are used to write # space-separated words, so they are not treated specially and handled # like the all of the other languages. if ( (cp >= 0x4_e00 and cp <= 0x9_fff) or (cp >= 0x3_400 and cp <= 0x4_dbf) # or (cp >= 0x20_000 and cp <= 0x2a_6df) # or (cp >= 0x2a_700 and cp <= 0x2b_73f) # or (cp >= 0x2b_740 and cp <= 0x2b_81f) # or (cp >= 0x2b_820 and cp <= 0x2c_eaf) # or (cp >= 0xf_900 and cp <= 0xf_aff) or (cp >= 0x2f_800 and cp <= 0x2f_a1f) # ): # return True return False class a_ ( a__ ): """simple docstring""" def __init__( self , _lowerCamelCase , _lowerCamelCase = False , _lowerCamelCase = None , **_lowerCamelCase ) ->List[str]: super().__init__(_lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ) SCREAMING_SNAKE_CASE : Optional[int] = Queue() SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : Optional[Any] = timeout def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase = False ) ->Any: self.text_queue.put(_lowerCamelCase , timeout=self.timeout ) if stream_end: self.text_queue.put(self.stop_signal , timeout=self.timeout ) def __iter__( self ) ->List[str]: return self def __lowerCAmelCase ( self ) ->Dict: SCREAMING_SNAKE_CASE : Any = self.text_queue.get(timeout=self.timeout ) if value == self.stop_signal: raise StopIteration() else: return value
313
def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" _lowercase =0 # if input_string is "aba" than new_input_string become "a|b|a" _lowercase ='''''' _lowercase ='''''' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__snake_case ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring _lowercase , _lowercase =0, 0 # length[i] shows the length of palindromic substring with center i _lowercase =[1 for i in range(len(__snake_case ) )] # for each character in new_string find corresponding palindromic string _lowercase =0 for j in range(len(__snake_case ) ): _lowercase =1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__snake_case ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 _lowercase =2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: _lowercase =j - k + 1 # noqa: E741 _lowercase =j + k - 1 # update max_length and start position if max_length < length[j]: _lowercase =length[j] _lowercase =j # create that string _lowercase =new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
5
0
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def A (__A : Union[str, Any] , __A : Dict , __A : Dict , __A : int , ) -> list[float]: """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = coefficient_matrix.shape UpperCAmelCase_ , UpperCAmelCase_ = constant_matrix.shape if rowsa != colsa: UpperCAmelCase_ = F"""Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}""" raise ValueError(__snake_case ) if colsa != 1: UpperCAmelCase_ = F"""Constant matrix must be nx1 but received {rowsa}x{colsa}""" raise ValueError(__snake_case ) if rowsa != rowsa: UpperCAmelCase_ = ( '''Coefficient and constant matrices dimensions must be nxn and nx1 but ''' F"""received {rowsa}x{colsa} and {rowsa}x{colsa}""" ) raise ValueError(__snake_case ) if len(__snake_case ) != rowsa: UpperCAmelCase_ = ( '''Number of initial values must be equal to number of rows in coefficient ''' F"""matrix but received {len(__snake_case )} and {rowsa}""" ) raise ValueError(__snake_case ) if iterations <= 0: raise ValueError('''Iterations must be at least 1''' ) UpperCAmelCase_ = np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) UpperCAmelCase_ , UpperCAmelCase_ = table.shape strictly_diagonally_dominant(__snake_case ) # Iterates the whole matrix for given number of times for _ in range(__snake_case ): UpperCAmelCase_ = [] for row in range(__snake_case ): UpperCAmelCase_ = 0 for col in range(__snake_case ): if col == row: UpperCAmelCase_ = table[row][col] elif col == cols - 1: UpperCAmelCase_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] UpperCAmelCase_ = (temp + val) / denom new_val.append(__snake_case ) UpperCAmelCase_ = new_val return [float(__snake_case ) for i in new_val] def A (__A : Union[str, Any] ) -> bool: """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = table.shape UpperCAmelCase_ = True for i in range(0 , __snake_case ): UpperCAmelCase_ = 0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError('''Coefficient matrix is not strictly diagonally dominant''' ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
51
from math import isqrt def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" _lowercase =[True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __snake_case , __snake_case ): _lowercase =False return [i for i in range(2 , __snake_case ) if is_prime[i]] def UpperCAmelCase_ ( __snake_case = 10**8 ) -> int: """simple docstring""" _lowercase =calculate_prime_numbers(max_number // 2 ) _lowercase =0 _lowercase =0 _lowercase =len(__snake_case ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f'''{solution() = }''')
5
0
import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class A_ ( SCREAMING_SNAKE_CASE , unittest.TestCase ): _UpperCAmelCase : List[Any] = ProphetNetTokenizer _UpperCAmelCase : Any = False def lowerCAmelCase ( self : Optional[int]): super().setUp() __lowerCamelCase : Union[str, Any] = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __lowerCamelCase : str = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file']) with open(self.vocab_file ,'w' ,encoding='utf-8') as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens])) def lowerCAmelCase ( self : Tuple ,SCREAMING_SNAKE_CASE__ : Tuple): __lowerCamelCase : Union[str, Any] = 'UNwant\u00E9d,running' __lowerCamelCase : Dict = 'unwanted, running' return input_text, output_text def lowerCAmelCase ( self : str): __lowerCamelCase : List[Any] = self.tokenizer_class(self.vocab_file) __lowerCamelCase : Dict = tokenizer.tokenize('UNwant\u00E9d,running') self.assertListEqual(SCREAMING_SNAKE_CASE__ ,['un', '##want', '##ed', ',', 'runn', '##ing']) self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__) ,[9, 6, 7, 1_2, 1_0, 1_1]) def lowerCAmelCase ( self : Any): __lowerCamelCase : Tuple = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz') ,['ah', '\u535A', '\u63A8', 'zz']) def lowerCAmelCase ( self : str): __lowerCamelCase : str = BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ') ,['hello', '!', 'how', 'are', 'you', '?']) self.assertListEqual(tokenizer.tokenize('H\u00E9llo') ,['hello']) def lowerCAmelCase ( self : Union[str, Any]): __lowerCamelCase : Tuple = BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__ ,strip_accents=SCREAMING_SNAKE_CASE__) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') ,['hällo', '!', 'how', 'are', 'you', '?']) self.assertListEqual(tokenizer.tokenize('H\u00E9llo') ,['h\u00E9llo']) def lowerCAmelCase ( self : Dict): __lowerCamelCase : Tuple = BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__ ,strip_accents=SCREAMING_SNAKE_CASE__) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') ,['hallo', '!', 'how', 'are', 'you', '?']) self.assertListEqual(tokenizer.tokenize('H\u00E9llo') ,['hello']) def lowerCAmelCase ( self : Optional[Any]): __lowerCamelCase : Any = BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') ,['hallo', '!', 'how', 'are', 'you', '?']) self.assertListEqual(tokenizer.tokenize('H\u00E9llo') ,['hello']) def lowerCAmelCase ( self : Optional[Any]): __lowerCamelCase : Optional[int] = BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ') ,['HeLLo', '!', 'how', 'Are', 'yoU', '?']) def lowerCAmelCase ( self : str): __lowerCamelCase : Union[str, Any] = BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__ ,strip_accents=SCREAMING_SNAKE_CASE__) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') ,['HäLLo', '!', 'how', 'Are', 'yoU', '?']) def lowerCAmelCase ( self : Tuple): __lowerCamelCase : Optional[Any] = BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__ ,strip_accents=SCREAMING_SNAKE_CASE__) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') ,['HaLLo', '!', 'how', 'Are', 'yoU', '?']) def lowerCAmelCase ( self : List[Any]): __lowerCamelCase : Optional[Any] = BasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__ ,never_split=['[UNK]']) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]') ,['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]']) def lowerCAmelCase ( self : Any): __lowerCamelCase : Any = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] __lowerCamelCase : List[Any] = {} for i, token in enumerate(SCREAMING_SNAKE_CASE__): __lowerCamelCase : List[str] = i __lowerCamelCase : Union[str, Any] = WordpieceTokenizer(vocab=SCREAMING_SNAKE_CASE__ ,unk_token='[UNK]') self.assertListEqual(tokenizer.tokenize('') ,[]) self.assertListEqual(tokenizer.tokenize('unwanted running') ,['un', '##want', '##ed', 'runn', '##ing']) self.assertListEqual(tokenizer.tokenize('unwantedX running') ,['[UNK]', 'runn', '##ing']) @require_torch def lowerCAmelCase ( self : Union[str, Any]): __lowerCamelCase : Tuple = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased') __lowerCamelCase : int = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] __lowerCamelCase : Tuple = [1_0_3_7, 2_1_4_6, 2_0_4_2_3, 2_0_0_5, 7_6_8_0, 7_8_4_9, 3_9_8_9, 1_0_1_2, 1_0_2] __lowerCamelCase : str = tokenizer(SCREAMING_SNAKE_CASE__ ,padding=SCREAMING_SNAKE_CASE__ ,return_tensors='pt') self.assertIsInstance(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__) __lowerCamelCase : List[str] = list(batch.input_ids.numpy()[0]) self.assertListEqual(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__) self.assertEqual((2, 9) ,batch.input_ids.shape) self.assertEqual((2, 9) ,batch.attention_mask.shape) def lowerCAmelCase ( self : str): self.assertTrue(_is_whitespace(' ')) self.assertTrue(_is_whitespace('\t')) self.assertTrue(_is_whitespace('\r')) self.assertTrue(_is_whitespace('\n')) self.assertTrue(_is_whitespace('\u00A0')) self.assertFalse(_is_whitespace('A')) self.assertFalse(_is_whitespace('-')) def lowerCAmelCase ( self : Union[str, Any]): self.assertTrue(_is_control('\u0005')) self.assertFalse(_is_control('A')) self.assertFalse(_is_control(' ')) self.assertFalse(_is_control('\t')) self.assertFalse(_is_control('\r')) def lowerCAmelCase ( self : List[str]): self.assertTrue(_is_punctuation('-')) self.assertTrue(_is_punctuation('$')) self.assertTrue(_is_punctuation('`')) self.assertTrue(_is_punctuation('.')) self.assertFalse(_is_punctuation('A')) self.assertFalse(_is_punctuation(' ')) @slow def lowerCAmelCase ( self : Optional[Any]): __lowerCamelCase : List[str] = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased') __lowerCamelCase : Optional[int] = tokenizer.encode('sequence builders' ,add_special_tokens=SCREAMING_SNAKE_CASE__) __lowerCamelCase : Optional[Any] = tokenizer.encode('multi-sequence build' ,add_special_tokens=SCREAMING_SNAKE_CASE__) __lowerCamelCase : Any = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE__) __lowerCamelCase : Any = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__) assert encoded_sentence == text + [1_0_2] assert encoded_pair == text + [1_0_2] + text_a + [1_0_2]
73
UpperCAmelCase__ = { '''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''': '''--..''', '''1''': '''.----''', '''2''': '''..---''', '''3''': '''...--''', '''4''': '''....-''', '''5''': '''.....''', '''6''': '''-....''', '''7''': '''--...''', '''8''': '''---..''', '''9''': '''----.''', '''0''': '''-----''', '''&''': '''.-...''', '''@''': '''.--.-.''', ''':''': '''---...''', ''',''': '''--..--''', '''.''': '''.-.-.-''', '''\'''': '''.----.''', '''"''': '''.-..-.''', '''?''': '''..--..''', '''/''': '''-..-.''', '''=''': '''-...-''', '''+''': '''.-.-.''', '''-''': '''-....-''', '''(''': '''-.--.''', ''')''': '''-.--.-''', '''!''': '''-.-.--''', ''' ''': '''/''' } # Exclamation mark is not in ITU-R recommendation # fmt: on UpperCAmelCase__ = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _lowercase ='''Morse code here!''' print(__snake_case ) _lowercase =encrypt(__snake_case ) print(__snake_case ) _lowercase =decrypt(__snake_case ) print(__snake_case ) if __name__ == "__main__": main()
5
0
_A : str = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} _A : Tuple = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> list[int]: """simple docstring""" lowerCamelCase__ : Tuple = True lowerCamelCase__ : Optional[int] = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(__snake_case , __snake_case , __snake_case ) order.append(__snake_case ) return order def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> list[int]: """simple docstring""" lowerCamelCase__ : Optional[int] = True lowerCamelCase__ : Optional[int] = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(__snake_case , __snake_case , __snake_case ) return component def _a ( UpperCAmelCase ) -> list[list[int]]: """simple docstring""" lowerCamelCase__ : Tuple = len(__snake_case ) * [False] lowerCamelCase__ : Tuple = {vert: [] for vert in range(len(__snake_case ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(__snake_case ) lowerCamelCase__ : Dict = [] for i, was_visited in enumerate(__snake_case ): if not was_visited: order += topology_sort(__snake_case , __snake_case , __snake_case ) lowerCamelCase__ : Any = [] lowerCamelCase__ : Optional[int] = len(__snake_case ) * [False] for i in range(len(__snake_case ) ): lowerCamelCase__ : Dict = order[len(__snake_case ) - i - 1] if not visited[vert]: lowerCamelCase__ : Union[str, Any] = find_components(__snake_case , __snake_case , __snake_case ) components_list.append(__snake_case ) return components_list
142
from typing import Any def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> list: """simple docstring""" _validation( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) # Creates data structures and fill initial step _lowercase ={} _lowercase ={} for state in states_space: _lowercase =observations_space[0] _lowercase =( initial_probabilities[state] * emission_probabilities[state][observation] ) _lowercase =None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__snake_case ) ): _lowercase =observations_space[o] _lowercase =observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _lowercase =probability _lowercase =k_state # Update probabilities and pointers dicts _lowercase =( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _lowercase =arg_max # The final observation _lowercase =observations_space[len(__snake_case ) - 1] # argmax for given final observation _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =probabilities[(k_state, final_observation)] if probability > max_probability: _lowercase =probability _lowercase =k_state _lowercase =arg_max # Process pointers backwards _lowercase =last_state _lowercase =[] for o in range(len(__snake_case ) - 1 , -1 , -1 ): result.append(__snake_case ) _lowercase =pointers[previous, observations_space[o]] result.reverse() return result def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_not_empty( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) _validate_lists(__snake_case , __snake_case ) _validate_dicts( __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_list(__snake_case , '''observations_space''' ) _validate_list(__snake_case , '''states_space''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a list" raise ValueError(__snake_case ) else: for x in _object: if not isinstance(__snake_case , __snake_case ): _lowercase =F"{var_name} must be a list of strings" raise ValueError(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_dict(__snake_case , '''initial_probabilities''' , __snake_case ) _validate_nested_dict(__snake_case , '''transition_probabilities''' ) _validate_nested_dict(__snake_case , '''emission_probabilities''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_dict(_object , __snake_case , __snake_case ) for x in _object.values(): _validate_dict(__snake_case , __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case = False ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a dict" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object ): _lowercase =F"{var_name} all keys must be strings" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object.values() ): _lowercase ='''nested dictionary ''' if nested else '''''' _lowercase =F"{var_name} {nested_text}all values must be {value_type.__name__}" raise ValueError(__snake_case ) if __name__ == "__main__": from doctest import testmod testmod()
5
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class A__ ( metaclass=UpperCamelCase ): """simple docstring""" UpperCamelCase_ : List[Any] = ['''flax''', '''transformers'''] def __init__( self : Dict , *lowerCAmelCase__ : str , **lowerCAmelCase__ : Union[str, Any] ) -> int: """simple docstring""" requires_backends(self , ["flax", "transformers"] ) @classmethod def _lowerCAmelCase ( cls : List[Any] , *lowerCAmelCase__ : int , **lowerCAmelCase__ : Union[str, Any] ) -> List[Any]: """simple docstring""" requires_backends(cls , ["flax", "transformers"] ) @classmethod def _lowerCAmelCase ( cls : Optional[int] , *lowerCAmelCase__ : Optional[int] , **lowerCAmelCase__ : Optional[int] ) -> Union[str, Any]: """simple docstring""" requires_backends(cls , ["flax", "transformers"] ) class A__ ( metaclass=UpperCamelCase ): """simple docstring""" UpperCamelCase_ : Dict = ['''flax''', '''transformers'''] def __init__( self : str , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Tuple ) -> Tuple: """simple docstring""" requires_backends(self , ["flax", "transformers"] ) @classmethod def _lowerCAmelCase ( cls : Union[str, Any] , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Dict ) -> List[str]: """simple docstring""" requires_backends(cls , ["flax", "transformers"] ) @classmethod def _lowerCAmelCase ( cls : List[str] , *lowerCAmelCase__ : str , **lowerCAmelCase__ : List[Any] ) -> List[Any]: """simple docstring""" requires_backends(cls , ["flax", "transformers"] ) class A__ ( metaclass=UpperCamelCase ): """simple docstring""" UpperCamelCase_ : Tuple = ['''flax''', '''transformers'''] def __init__( self : Any , *lowerCAmelCase__ : List[str] , **lowerCAmelCase__ : Tuple ) -> str: """simple docstring""" requires_backends(self , ["flax", "transformers"] ) @classmethod def _lowerCAmelCase ( cls : Any , *lowerCAmelCase__ : Tuple , **lowerCAmelCase__ : int ) -> str: """simple docstring""" requires_backends(cls , ["flax", "transformers"] ) @classmethod def _lowerCAmelCase ( cls : Optional[Any] , *lowerCAmelCase__ : str , **lowerCAmelCase__ : List[str] ) -> int: """simple docstring""" requires_backends(cls , ["flax", "transformers"] ) class A__ ( metaclass=UpperCamelCase ): """simple docstring""" UpperCamelCase_ : List[Any] = ['''flax''', '''transformers'''] def __init__( self : Optional[int] , *lowerCAmelCase__ : Dict , **lowerCAmelCase__ : Tuple ) -> Tuple: """simple docstring""" requires_backends(self , ["flax", "transformers"] ) @classmethod def _lowerCAmelCase ( cls : Optional[Any] , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Optional[int] ) -> int: """simple docstring""" requires_backends(cls , ["flax", "transformers"] ) @classmethod def _lowerCAmelCase ( cls : Tuple , *lowerCAmelCase__ : Optional[int] , **lowerCAmelCase__ : Tuple ) -> int: """simple docstring""" requires_backends(cls , ["flax", "transformers"] )
145
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) # TODO Update this UpperCAmelCase__ = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''esm''' def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_2_6 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase="absolute" , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ) -> Tuple: super().__init__(pad_token_id=UpperCAmelCase , mask_token_id=UpperCAmelCase , **UpperCAmelCase ) _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =position_embedding_type _lowercase =use_cache _lowercase =emb_layer_norm_before _lowercase =token_dropout _lowercase =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) _lowercase =EsmFoldConfig() elif isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =EsmFoldConfig(**UpperCAmelCase ) _lowercase =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) _lowercase =get_default_vocab_list() else: _lowercase =vocab_list else: _lowercase =None _lowercase =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCAmelCase ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A (self ) -> List[str]: _lowercase =super().to_dict() if isinstance(self.esmfold_config , UpperCAmelCase ): _lowercase =self.esmfold_config.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> Union[str, Any]: if self.trunk is None: _lowercase =TrunkConfig() elif isinstance(self.trunk , UpperCAmelCase ): _lowercase =TrunkConfig(**self.trunk ) def __A (self ) -> Tuple: _lowercase =asdict(self ) _lowercase =self.trunk.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 48 SCREAMING_SNAKE_CASE__ = 1024 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> List[str]: if self.structure_module is None: _lowercase =StructureModuleConfig() elif isinstance(self.structure_module , UpperCAmelCase ): _lowercase =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) _lowercase =self.sequence_state_dim // self.sequence_head_width _lowercase =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(f"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __A (self ) -> Dict: _lowercase =asdict(self ) _lowercase =self.structure_module.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 384 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 16 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 12 SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 0.1 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 1 SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = 7 SCREAMING_SNAKE_CASE__ = 10 SCREAMING_SNAKE_CASE__ = 1E-8 SCREAMING_SNAKE_CASE__ = 1E5 def __A (self ) -> List[Any]: return asdict(self ) def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
5
0
'''simple docstring''' def UpperCamelCase_ ( A__ : Union[str, Any] , A__ : List[str] ): '''simple docstring''' lowerCAmelCase_ : Any = len(__snake_case ) + 1 lowerCAmelCase_ : List[Any] = len(__snake_case ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. lowerCAmelCase_ : Tuple = [[0 for i in range(__snake_case )] for j in range(__snake_case )] # since string of zero length match pattern of zero length lowerCAmelCase_ : str = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , __snake_case ): lowerCAmelCase_ : str = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , __snake_case ): lowerCAmelCase_ : List[Any] = dp[0][j - 2] if pattern[j - 1] == """*""" else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , __snake_case ): for j in range(1 , __snake_case ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": lowerCAmelCase_ : str = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: lowerCAmelCase_ : List[str] = 1 elif pattern[j - 2] in (input_string[i - 1], "."): lowerCAmelCase_ : Optional[int] = dp[i - 1][j] else: lowerCAmelCase_ : Any = 0 else: lowerCAmelCase_ : Dict = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") __A : Union[str, Any] = "aab" __A : int = "c*a*b" # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(F'''{input_string} matches the given pattern {pattern}''') else: print(F'''{input_string} does not match with the given pattern {pattern}''')
120
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList UpperCAmelCase__ = ['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif'''] class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=1 ) -> Dict: _lowercase =tokenizer _lowercase =dataset _lowercase =len(UpperCAmelCase ) if n_tasks is None else n_tasks _lowercase =n_copies def __iter__(self ) -> Optional[Any]: _lowercase =[] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) _lowercase =self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =start_length _lowercase =eof_strings _lowercase =tokenizer def __call__(self , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: _lowercase =self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) _lowercase =[] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(UpperCAmelCase ) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =re.split('''(%s)''' % '''|'''.join(__snake_case ) , __snake_case ) # last string should be "" return "".join(string_list[:-2] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case=20 , **__snake_case ) -> Tuple: """simple docstring""" _lowercase =defaultdict(__snake_case ) # dict of list of generated tokens for step, batch in tqdm(enumerate(__snake_case ) ): with torch.no_grad(): _lowercase =batch['''ids'''].shape[-1] _lowercase =accelerator.unwrap_model(__snake_case ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=__snake_case , **__snake_case ) # each task is generated batch_size times _lowercase =batch['''task_id'''].repeat(__snake_case ) _lowercase =accelerator.pad_across_processes( __snake_case , dim=1 , pad_index=tokenizer.pad_token_id ) _lowercase , _lowercase =accelerator.gather((generated_tokens, generated_tasks) ) _lowercase =generated_tokens.cpu().numpy() _lowercase =generated_tasks.cpu().numpy() for task, generated_tokens in zip(__snake_case , __snake_case ): gen_token_dict[task].append(__snake_case ) _lowercase =[[] for _ in range(__snake_case )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: _lowercase =tokenizer.decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) code_gens[task].append(remove_last_block(__snake_case ) ) return code_gens def UpperCAmelCase_ ( ) -> str: """simple docstring""" _lowercase =HfArgumentParser(__snake_case ) _lowercase =parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric _lowercase =args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing _lowercase ='''false''' if args.num_workers is None: _lowercase =multiprocessing.cpu_count() # Use dataset load to feed to accelerate _lowercase =Accelerator() set_seed(args.seed , device_specific=__snake_case ) # Load model and tokenizer _lowercase =AutoTokenizer.from_pretrained(args.model_ckpt ) _lowercase =tokenizer.eos_token _lowercase =AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings _lowercase ={ '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , __snake_case , __snake_case )] ), } # Load evaluation dataset and metric _lowercase =load_dataset('''openai_humaneval''' ) _lowercase =load_metric('''code_eval''' ) _lowercase =args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) _lowercase =args.n_samples // args.batch_size _lowercase =TokenizedDataset(__snake_case , human_eval['''test'''] , n_copies=__snake_case , n_tasks=__snake_case ) # do not confuse args.batch_size, which is actually the num_return_sequences _lowercase =DataLoader(__snake_case , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: _lowercase =code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception _lowercase , _lowercase =accelerator.prepare(__snake_case , __snake_case ) _lowercase =complete_code( __snake_case , __snake_case , __snake_case , __snake_case , n_tasks=__snake_case , batch_size=args.batch_size , **__snake_case , ) if accelerator.is_main_process: _lowercase =[] for task in tqdm(range(__snake_case ) ): _lowercase =human_eval['''test'''][task]['''test'''] _lowercase =F"check({human_eval['test'][task]['entry_point']})" references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric _lowercase , _lowercase =code_eval_metric.compute( references=__snake_case , predictions=__snake_case , num_workers=args.num_workers ) print(F"Results: {pass_at_k}" ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(__snake_case , __snake_case ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
5
0
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConformerConfig, WavaVecaConformerForCTC, WavaVecaConformerForPreTraining, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() __UpperCAmelCase : Optional[int] = logging.get_logger(__name__) __UpperCAmelCase : List[Any] = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.linear_k": "encoder.layers.*.self_attn.linear_k", "self_attn.linear_v": "encoder.layers.*.self_attn.linear_v", "self_attn.linear_q": "encoder.layers.*.self_attn.linear_q", "self_attn.pos_bias_u": "encoder.layers.*.self_attn.pos_bias_u", "self_attn.pos_bias_v": "encoder.layers.*.self_attn.pos_bias_v", "self_attn.linear_out": "encoder.layers.*.self_attn.linear_out", "self_attn.linear_pos": "encoder.layers.*.self_attn.linear_pos", "self_attn.rotary_emb": "encoder.embed_positions", "self_attn_layer_norm": "encoder.layers.*.self_attn_layer_norm", "conv_module.pointwise_conv1": "encoder.layers.*.conv_module.pointwise_conv1", "conv_module.pointwise_conv2": "encoder.layers.*.conv_module.pointwise_conv2", "conv_module.depthwise_conv": "encoder.layers.*.conv_module.depthwise_conv", "conv_module.batch_norm": "encoder.layers.*.conv_module.batch_norm", "conv_module.layer_norm": "encoder.layers.*.conv_module.layer_norm", "ffn1.w_1": "encoder.layers.*.ffn1.intermediate_dense", "ffn1.w_2": "encoder.layers.*.ffn1.output_dense", "ffn1.layer_norm": "encoder.layers.*.ffn1_layer_norm", "ffn2.w_1": "encoder.layers.*.ffn2.intermediate_dense", "ffn2.w_2": "encoder.layers.*.ffn2.output_dense", "ffn2.layer_norm": "encoder.layers.*.ffn2_layer_norm", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } __UpperCAmelCase : Any = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def A__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__) -> Dict: for attribute in key.split("""."""): __snake_case: str = getattr(__snake_case , __snake_case) if weight_type is not None: __snake_case: Tuple = getattr(__snake_case , __snake_case).shape else: __snake_case: List[Any] = hf_pointer.shape if hf_shape != value.shape: raise ValueError( 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: int = value elif weight_type == "weight_g": __snake_case: Optional[int] = value elif weight_type == "weight_v": __snake_case: List[Any] = value elif weight_type == "bias": __snake_case: str = value elif weight_type == "running_mean": __snake_case: int = value elif weight_type == "running_var": __snake_case: int = value elif weight_type == "num_batches_tracked": __snake_case: List[str] = value elif weight_type == "inv_freq": __snake_case: Any = value else: __snake_case: Tuple = value logger.info(F'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.''') def A__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__) -> List[str]: __snake_case: Dict = [] __snake_case: str = fairseq_model.state_dict() __snake_case: int = hf_model.wavaveca_conformer.feature_extractor for name, value in fairseq_dict.items(): __snake_case: str = False if "conv_layers" in name: load_conv_layer( __snake_case , __snake_case , __snake_case , __snake_case , hf_model.config.feat_extract_norm == """group""" , ) __snake_case: Tuple = True else: for key, mapped_key in MAPPING.items(): __snake_case: Optional[Any] = """wav2vec2_conformer.""" + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("""w2v_model.""")[-1] == name.split(""".""")[0]: __snake_case: List[str] = True if "*" in mapped_key: __snake_case: Dict = name.split(__snake_case)[0].split(""".""")[-2] __snake_case: Optional[Any] = mapped_key.replace("""*""" , __snake_case) if "pos_bias_u" in name: __snake_case: Any = None elif "pos_bias_v" in name: __snake_case: int = None elif "weight_g" in name: __snake_case: str = """weight_g""" elif "weight_v" in name: __snake_case: int = """weight_v""" elif "bias" in name: __snake_case: List[Any] = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj __snake_case: int = """weight""" elif "running_mean" in name: __snake_case: List[str] = """running_mean""" elif "inv_freq" in name: __snake_case: List[Any] = """inv_freq""" elif "running_var" in name: __snake_case: Any = """running_var""" elif "num_batches_tracked" in name: __snake_case: List[Any] = """num_batches_tracked""" else: __snake_case: Optional[Any] = None set_recursively(__snake_case , __snake_case , __snake_case , __snake_case , __snake_case) continue if not is_used: unused_weights.append(__snake_case) logger.warning(F'''Unused weights: {unused_weights}''') def A__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__) -> str: __snake_case: Optional[Any] = full_name.split("""conv_layers.""")[-1] __snake_case: Optional[Any] = name.split(""".""") __snake_case: Tuple = int(items[0]) __snake_case: Union[str, Any] = int(items[1]) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''') __snake_case: Dict = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''') elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''') __snake_case: int = 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: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.''') __snake_case: Union[str, Any] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''') elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.''') __snake_case: Optional[int] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''') else: unused_weights.append(__snake_case) @torch.no_grad() def A__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=True) -> str: if config_path is not None: __snake_case: Optional[int] = WavaVecaConformerConfig.from_pretrained(__snake_case , hidden_act="""swish""") else: __snake_case: List[Any] = WavaVecaConformerConfig() if "rope" in checkpoint_path: __snake_case: int = """rotary""" if is_finetuned: if dict_path: __snake_case: List[str] = Dictionary.load(__snake_case) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __snake_case: int = target_dict.pad_index __snake_case: Optional[Any] = target_dict.bos_index __snake_case: Dict = target_dict.eos_index __snake_case: Optional[Any] = len(target_dict.symbols) __snake_case: int = os.path.join(__snake_case , """vocab.json""") if not os.path.isdir(__snake_case): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(__snake_case)) return os.makedirs(__snake_case , exist_ok=__snake_case) __snake_case: int = target_dict.indices # fairseq has the <pad> and <s> switched __snake_case: Dict = 0 __snake_case: Dict = 1 with open(__snake_case , """w""" , encoding="""utf-8""") as vocab_handle: json.dump(__snake_case , __snake_case) __snake_case: Optional[Any] = WavaVecaCTCTokenizer( __snake_case , 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=__snake_case , ) __snake_case: List[str] = True if config.feat_extract_norm == """layer""" else False __snake_case: List[str] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=__snake_case , return_attention_mask=__snake_case , ) __snake_case: str = WavaVecaProcessor(feature_extractor=__snake_case , tokenizer=__snake_case) processor.save_pretrained(__snake_case) __snake_case: List[str] = WavaVecaConformerForCTC(__snake_case) else: __snake_case: Union[str, Any] = WavaVecaConformerForPreTraining(__snake_case) 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: Optional[Any] = argparse.Namespace(task="""audio_pretraining""") __snake_case: Union[str, Any] = fairseq.tasks.setup_task(__snake_case) __snake_case , __snake_case , __snake_case: Union[str, Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=__snake_case) __snake_case: str = model[0].eval() recursively_load_weights(__snake_case , __snake_case , not is_finetuned) hf_wavavec.save_pretrained(__snake_case) if __name__ == "__main__": __UpperCAmelCase : Dict = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--not_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not" ) __UpperCAmelCase : int = parser.parse_args() convert_wavaveca_conformer_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
111
UpperCAmelCase__ = 8.31_44_62 # Unit - J mol-1 K-1 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
5
0
import unittest from transformers import MPNetConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) class __lowerCAmelCase : def __init__( self , lowerCAmelCase__ , lowerCAmelCase__=1_3 , lowerCAmelCase__=7 , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=False , lowerCAmelCase__=True , lowerCAmelCase__=9_9 , lowerCAmelCase__=6_4 , lowerCAmelCase__=5 , lowerCAmelCase__=4 , lowerCAmelCase__=6_4 , lowerCAmelCase__="gelu" , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.1 , lowerCAmelCase__=5_1_2 , lowerCAmelCase__=1_6 , lowerCAmelCase__=2 , lowerCAmelCase__=0.02 , lowerCAmelCase__=3 , lowerCAmelCase__=4 , lowerCAmelCase__=None , ) -> Union[str, Any]: '''simple docstring''' a__ : int =parent a__ : List[str] =batch_size a__ : int =seq_length a__ : int =is_training a__ : List[str] =use_input_mask a__ : List[str] =use_token_type_ids a__ : str =use_labels a__ : Union[str, Any] =vocab_size a__ : Dict =hidden_size a__ : Union[str, Any] =num_hidden_layers a__ : Tuple =num_attention_heads a__ : Any =intermediate_size a__ : Tuple =hidden_act a__ : str =hidden_dropout_prob a__ : List[str] =attention_probs_dropout_prob a__ : int =max_position_embeddings a__ : List[Any] =type_vocab_size a__ : List[str] =type_sequence_label_size a__ : Dict =initializer_range a__ : int =num_labels a__ : Any =num_choices a__ : str =scope def _lowercase ( self ) -> Tuple: '''simple docstring''' return MPNetConfig.from_pretrained("microsoft/mpnet-base" ) def _lowercase ( self ) -> Dict: '''simple docstring''' a__ : Tuple =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a__ : Optional[int] =None if self.use_input_mask: a__ : str =random_attention_mask([self.batch_size, self.seq_length] ) a__ : str =None a__ : Tuple =None a__ : int =None if self.use_labels: a__ : Any =ids_tensor([self.batch_size] , self.type_sequence_label_size ) a__ : Optional[Any] =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) a__ : int =ids_tensor([self.batch_size] , self.num_choices ) a__ : Dict =self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowercase ( self ) -> str: '''simple docstring''' return MPNetConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Optional[Any]: '''simple docstring''' a__ : List[Any] =MPNetModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a__ : Dict =model(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : int =model(lowerCAmelCase__ ) 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 _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Optional[Any]: '''simple docstring''' a__ : Any =MPNetForQuestionAnswering(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a__ : Dict =model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , start_positions=lowerCAmelCase__ , end_positions=lowerCAmelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Tuple: '''simple docstring''' a__ : int =self.num_labels a__ : Optional[int] =MPNetForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a__ : int =model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Dict: '''simple docstring''' a__ : Any =self.num_choices a__ : str =MPNetForMultipleChoice(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a__ : List[Any] =input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() a__ : int =input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() a__ : int =model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> int: '''simple docstring''' a__ : Optional[Any] =self.num_labels a__ : List[Any] =MPNetForTokenClassification(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a__ : Tuple =model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _lowercase ( self ) -> Tuple: '''simple docstring''' a__ : List[Any] =self.prepare_config_and_inputs() ((a__) , (a__) , (a__) , (a__) , (a__) , (a__)) : Any =config_and_inputs a__ : Tuple ={"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase): _lowercase : Optional[int] = ( ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) if is_torch_available() else () ) _lowercase : Tuple = ( { """feature-extraction""": MPNetModel, """fill-mask""": MPNetForMaskedLM, """question-answering""": MPNetForQuestionAnswering, """text-classification""": MPNetForSequenceClassification, """token-classification""": MPNetForTokenClassification, """zero-shot""": MPNetForSequenceClassification, } if is_torch_available() else {} ) _lowercase : Dict = False _lowercase : Optional[Any] = True def _lowercase ( self ) -> Optional[Any]: '''simple docstring''' a__ : Union[str, Any] =MPNetModelTester(self ) a__ : Any =ConfigTester(self , config_class=lowerCAmelCase__ , hidden_size=3_7 ) def _lowercase ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def _lowercase ( self ) -> Dict: '''simple docstring''' a__ : List[Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_model(*lowerCAmelCase__ ) def _lowercase ( self ) -> Tuple: '''simple docstring''' a__ : List[str] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCAmelCase__ ) def _lowercase ( self ) -> str: '''simple docstring''' a__ : Optional[Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCAmelCase__ ) def _lowercase ( self ) -> Union[str, Any]: '''simple docstring''' a__ : Tuple =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCAmelCase__ ) def _lowercase ( self ) -> Optional[Any]: '''simple docstring''' a__ : Optional[Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCAmelCase__ ) @require_torch class __lowerCAmelCase ( unittest.TestCase): @slow def _lowercase ( self ) -> str: '''simple docstring''' a__ : List[str] =MPNetModel.from_pretrained("microsoft/mpnet-base" ) a__ : List[Any] =torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] ) a__ : int =model(lowerCAmelCase__ )[0] a__ : Tuple =torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , lowerCAmelCase__ ) a__ : Optional[Any] =torch.tensor( [[[-0.05_50, 0.19_43, -0.07_40], [-0.05_62, 0.22_11, -0.05_79], [-0.04_37, 0.33_37, -0.06_41]]] ) # compare the actual values for a slice. self.assertTrue(torch.allclose(output[:, :3, :3] , lowerCAmelCase__ , atol=1E-4 ) )
95
from __future__ import annotations from collections.abc import Callable UpperCAmelCase__ = list[list[float | int]] def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Matrix: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(size + 1 )] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for row in range(__snake_case ): for col in range(__snake_case ): _lowercase =matrix[row][col] _lowercase =vector[row][0] _lowercase =0 _lowercase =0 while row < size and col < size: # pivoting _lowercase =max((abs(augmented[rowa][col] ), rowa) for rowa in range(__snake_case , __snake_case ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: _lowercase , _lowercase =augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __snake_case ): _lowercase =augmented[rowa][col] / augmented[row][col] _lowercase =0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __snake_case ): for row in range(__snake_case ): _lowercase =augmented[row][col] / augmented[col][col] for cola in range(__snake_case , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__snake_case ) ] def UpperCAmelCase_ ( __snake_case ) -> Callable[[int], int]: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(__snake_case )] for _ in range(__snake_case )] _lowercase =[[0] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for x_val, y_val in enumerate(__snake_case ): for col in range(__snake_case ): _lowercase =(x_val + 1) ** (size - col - 1) _lowercase =y_val _lowercase =solve(__snake_case , __snake_case ) def interpolated_func(__snake_case ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__snake_case ) ) return interpolated_func def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def UpperCAmelCase_ ( __snake_case = question_function , __snake_case = 10 ) -> int: """simple docstring""" _lowercase =[func(__snake_case ) for x_val in range(1 , order + 1 )] _lowercase =[ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] _lowercase =0 _lowercase =42 _lowercase =42 for poly in polynomials: _lowercase =1 while func(__snake_case ) == poly(__snake_case ): x_val += 1 ret += poly(__snake_case ) return ret if __name__ == "__main__": print(f'''{solution() = }''')
5
0
import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast _a = datasets.utils.logging.get_logger(__name__) @dataclass class A_ ( datasets.BuilderConfig ): _lowercase : Optional[Any] = 1_0_0_0_0 _lowercase : Optional[Any] = None _lowercase : Union[str, Any] = None class A_ ( datasets.ArrowBasedBuilder ): _lowercase : str = ParquetConfig def UpperCAmelCase ( self : Optional[Any] ) -> Union[str, Any]: return datasets.DatasetInfo(features=self.config.features ) def UpperCAmelCase ( self : List[str] , UpperCAmelCase : str ) -> List[str]: 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}''' ) __lowerCAmelCase: Dict = dl_manager.download_and_extract(self.config.data_files ) if isinstance(UpperCAmelCase , (str, list, tuple) ): __lowerCAmelCase: Dict = data_files if isinstance(UpperCAmelCase , UpperCAmelCase ): __lowerCAmelCase: Optional[int] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive __lowerCAmelCase: int = [dl_manager.iter_files(UpperCAmelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] __lowerCAmelCase: int = [] for split_name, files in data_files.items(): if isinstance(UpperCAmelCase , UpperCAmelCase ): __lowerCAmelCase: str = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive __lowerCAmelCase: List[str] = [dl_manager.iter_files(UpperCAmelCase ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(UpperCAmelCase ): with open(UpperCAmelCase , 'rb' ) as f: __lowerCAmelCase: Union[str, Any] = datasets.Features.from_arrow_schema(pq.read_schema(UpperCAmelCase ) ) break splits.append(datasets.SplitGenerator(name=UpperCAmelCase , gen_kwargs={'files': files} ) ) return splits def UpperCAmelCase ( self : Optional[int] , UpperCAmelCase : Dict ) -> pa.Table: if self.info.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example __lowerCAmelCase: List[Any] = table_cast(UpperCAmelCase , self.info.features.arrow_schema ) return pa_table def UpperCAmelCase ( self : Tuple , UpperCAmelCase : Any ) -> Tuple: __lowerCAmelCase: Union[str, Any] = self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( F'''Tried to load parquet data with columns \'{self.config.columns}\' with mismatching features \'{self.info.features}\'''' ) for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCAmelCase ) ): with open(UpperCAmelCase , 'rb' ) as f: __lowerCAmelCase: Optional[Any] = pq.ParquetFile(UpperCAmelCase ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size , columns=self.config.columns ) ): __lowerCAmelCase: Dict = pa.Table.from_batches([record_batch] ) # 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 F'''{file_idx}_{batch_idx}''', self._cast_table(UpperCAmelCase ) except ValueError as e: logger.error(F'''Failed to read file \'{file}\' with error {type(UpperCAmelCase )}: {e}''' ) raise
322
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase__ = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''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: UpperCAmelCase__ = [ '''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 UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
0
# 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 # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __snake_case : Union[str, Any] = { """configuration_efficientnet""": [ """EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """EfficientNetConfig""", """EfficientNetOnnxConfig""", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : Union[str, Any] = ["""EfficientNetImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : Tuple = [ """EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST""", """EfficientNetForImageClassification""", """EfficientNetModel""", """EfficientNetPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys __snake_case : List[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
248
# 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 # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase__ = { '''configuration_efficientnet''': [ '''EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientNetConfig''', '''EfficientNetOnnxConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''EfficientNetImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientNetForImageClassification''', '''EfficientNetModel''', '''EfficientNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
5
0
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : List[str] = "▁" A : Dict = {"vocab_file": "spiece.model"} A : Tuple = { "vocab_file": { "google/reformer-crime-and-punishment": ( "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model" ) } } A : Optional[int] = { "google/reformer-crime-and-punishment": 524288, } class _lowercase ( lowercase__): """simple docstring""" A__ = VOCAB_FILES_NAMES A__ = PRETRAINED_VOCAB_FILES_MAP A__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ = ["input_ids", "attention_mask"] def __init__( self : int , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : str="</s>" , __lowerCamelCase : Dict="<unk>" , __lowerCamelCase : Optional[int]=[] , __lowerCamelCase : Optional[int] = None , **__lowerCamelCase : List[str] , ): '''simple docstring''' lowerCamelCase__ : Optional[int] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=__lowerCamelCase , unk_token=__lowerCamelCase , additional_special_tokens=__lowerCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__lowerCamelCase , ) lowerCamelCase__ : Tuple = vocab_file lowerCamelCase__ : Union[str, Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__lowerCamelCase ) @property def lowerCAmelCase ( self : Dict ): '''simple docstring''' return self.sp_model.get_piece_size() def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = {self.convert_ids_to_tokens(__lowerCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : List[str] ): '''simple docstring''' lowerCamelCase__ : Any = self.__dict__.copy() lowerCamelCase__ : Optional[Any] = None return state def __setstate__( self : Tuple , __lowerCamelCase : List[Any] ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): lowerCamelCase__ : Union[str, Any] = {} lowerCamelCase__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowerCAmelCase ( self : Tuple , __lowerCamelCase : List[str] ): '''simple docstring''' return self.sp_model.encode(__lowerCamelCase , out_type=__lowerCamelCase ) def lowerCAmelCase ( self : Optional[int] , __lowerCamelCase : List[Any] ): '''simple docstring''' return self.sp_model.piece_to_id(__lowerCamelCase ) def lowerCAmelCase ( self : Dict , __lowerCamelCase : Optional[int] ): '''simple docstring''' if index < self.sp_model.get_piece_size(): lowerCamelCase__ : Union[str, Any] = self.sp_model.IdToPiece(__lowerCamelCase ) return token def lowerCAmelCase ( self : int , __lowerCamelCase : Any ): '''simple docstring''' lowerCamelCase__ : str = [] lowerCamelCase__ : List[str] = "" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__lowerCamelCase ) + token lowerCamelCase__ : List[str] = [] else: current_sub_tokens.append(__lowerCamelCase ) out_string += self.sp_model.decode(__lowerCamelCase ) return out_string.strip() def lowerCAmelCase ( self : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : List[Any] = None ): '''simple docstring''' if not os.path.isdir(__lowerCamelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return lowerCamelCase__ : Dict = os.path.join( __lowerCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __lowerCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(__lowerCamelCase , "wb" ) as fi: lowerCamelCase__ : Tuple = self.sp_model.serialized_model_proto() fi.write(__lowerCamelCase ) return (out_vocab_file,)
184
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
0
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList a__ : Any = ['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif'''] class a_ ( a__ ): """simple docstring""" def __init__( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=None , _lowerCamelCase=1 ) ->Dict: SCREAMING_SNAKE_CASE : int = tokenizer SCREAMING_SNAKE_CASE : str = dataset SCREAMING_SNAKE_CASE : int = len(_lowerCamelCase ) if n_tasks is None else n_tasks SCREAMING_SNAKE_CASE : Union[str, Any] = n_copies def __iter__( self ) ->Optional[Any]: SCREAMING_SNAKE_CASE : str = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) SCREAMING_SNAKE_CASE : List[str] = self.tokenizer(_lowerCamelCase , padding=_lowerCamelCase , return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class a_ ( a__ ): """simple docstring""" def __init__( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) ->Optional[Any]: SCREAMING_SNAKE_CASE : Optional[Any] = start_length SCREAMING_SNAKE_CASE : List[str] = eof_strings SCREAMING_SNAKE_CASE : Dict = tokenizer def __call__( self , _lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ) ->Dict: SCREAMING_SNAKE_CASE : Any = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) SCREAMING_SNAKE_CASE : Optional[int] = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(_lowerCamelCase ) def UpperCAmelCase_( a__ ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = re.split('''(%s)''' % '''|'''.join(__snake_case ) , __snake_case ) # last string should be "" return "".join(string_list[:-2] ) def UpperCAmelCase_( a__ , a__ , a__ , a__ , a__ , a__=20 , **a__ ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = defaultdict(__snake_case ) # dict of list of generated tokens for step, batch in tqdm(enumerate(__snake_case ) ): with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = batch['''ids'''].shape[-1] SCREAMING_SNAKE_CASE : Any = accelerator.unwrap_model(__snake_case ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=__snake_case , **__snake_case ) # each task is generated batch_size times SCREAMING_SNAKE_CASE : List[Any] = batch['''task_id'''].repeat(__snake_case ) SCREAMING_SNAKE_CASE : str = accelerator.pad_across_processes( __snake_case , dim=1 , pad_index=tokenizer.pad_token_id ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = accelerator.gather((generated_tokens, generated_tasks) ) SCREAMING_SNAKE_CASE : Tuple = generated_tokens.cpu().numpy() SCREAMING_SNAKE_CASE : int = generated_tasks.cpu().numpy() for task, generated_tokens in zip(__snake_case , __snake_case ): gen_token_dict[task].append(__snake_case ) SCREAMING_SNAKE_CASE : Union[str, Any] = [[] for _ in range(__snake_case )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: SCREAMING_SNAKE_CASE : List[Any] = tokenizer.decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) code_gens[task].append(remove_last_block(__snake_case ) ) return code_gens def UpperCAmelCase_( ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = HfArgumentParser(__snake_case ) SCREAMING_SNAKE_CASE : Dict = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric SCREAMING_SNAKE_CASE : Any = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing SCREAMING_SNAKE_CASE : int = '''false''' if args.num_workers is None: SCREAMING_SNAKE_CASE : Optional[int] = multiprocessing.cpu_count() # Use dataset load to feed to accelerate SCREAMING_SNAKE_CASE : Union[str, Any] = Accelerator() set_seed(args.seed , device_specific=__snake_case ) # Load model and tokenizer SCREAMING_SNAKE_CASE : Any = AutoTokenizer.from_pretrained(args.model_ckpt ) SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token SCREAMING_SNAKE_CASE : Dict = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings SCREAMING_SNAKE_CASE : str = { '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , __snake_case , __snake_case )] ), } # Load evaluation dataset and metric SCREAMING_SNAKE_CASE : Optional[Any] = load_dataset('''openai_humaneval''' ) SCREAMING_SNAKE_CASE : List[Any] = load_metric('''code_eval''' ) SCREAMING_SNAKE_CASE : Any = args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) SCREAMING_SNAKE_CASE : List[str] = args.n_samples // args.batch_size SCREAMING_SNAKE_CASE : Union[str, Any] = TokenizedDataset(__snake_case , human_eval['''test'''] , n_copies=__snake_case , n_tasks=__snake_case ) # do not confuse args.batch_size, which is actually the num_return_sequences SCREAMING_SNAKE_CASE : Optional[int] = DataLoader(__snake_case , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: SCREAMING_SNAKE_CASE : str = code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = accelerator.prepare(__snake_case , __snake_case ) SCREAMING_SNAKE_CASE : str = complete_code( __snake_case , __snake_case , __snake_case , __snake_case , n_tasks=__snake_case , batch_size=args.batch_size , **__snake_case , ) if accelerator.is_main_process: SCREAMING_SNAKE_CASE : Optional[Any] = [] for task in tqdm(range(__snake_case ) ): SCREAMING_SNAKE_CASE : Any = human_eval['''test'''][task]['''test'''] SCREAMING_SNAKE_CASE : List[Any] = F"""check({human_eval["test"][task]["entry_point"]})""" references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = code_eval_metric.compute( references=__snake_case , predictions=__snake_case , num_workers=args.num_workers ) print(F"""Results: {pass_at_k}""" ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(__snake_case , __snake_case ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
313
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) else: return a * actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(__snake_case , __snake_case ) return actual_power(__snake_case , __snake_case ) if __name__ == "__main__": print(power(-2, -3))
5
0
snake_case_ : Tuple = 8.314_462 # Unit - J mol-1 K-1 def A (__A : Any , __A : Optional[int] , __A : Any ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def A (__A : Dict , __A : List[str] , __A : int ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
51
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase = 1_6 , UpperCAmelCase = 8_8 , UpperCAmelCase = None , UpperCAmelCase = 1 , UpperCAmelCase = 0.0 , UpperCAmelCase = 3_2 , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "geglu" , UpperCAmelCase = None , ) -> Any: super().__init__() _lowercase =nn.ModuleList( [ TransformeraDModel( num_attention_heads=UpperCAmelCase , attention_head_dim=UpperCAmelCase , in_channels=UpperCAmelCase , num_layers=UpperCAmelCase , dropout=UpperCAmelCase , norm_num_groups=UpperCAmelCase , cross_attention_dim=UpperCAmelCase , attention_bias=UpperCAmelCase , sample_size=UpperCAmelCase , num_vector_embeds=UpperCAmelCase , activation_fn=UpperCAmelCase , num_embeds_ada_norm=UpperCAmelCase , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _lowercase =0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _lowercase =[7_7, 2_5_7] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _lowercase =[1, 0] def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = True , ) -> str: _lowercase =hidden_states _lowercase =[] _lowercase =0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _lowercase =encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _lowercase =self.transformer_index_for_condition[i] _lowercase =self.transformers[transformer_index]( UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , timestep=UpperCAmelCase , cross_attention_kwargs=UpperCAmelCase , return_dict=UpperCAmelCase , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _lowercase =encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _lowercase =output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=UpperCAmelCase )
5
0
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class A_ ( SCREAMING_SNAKE_CASE ): def __init__( self : Tuple ,SCREAMING_SNAKE_CASE__ : Union[str, Any] ,SCREAMING_SNAKE_CASE__ : Optional[Any] ,SCREAMING_SNAKE_CASE__ : List[Any] = None ,SCREAMING_SNAKE_CASE__ : List[Any] = None ,SCREAMING_SNAKE_CASE__ : Optional[int] = False ,**SCREAMING_SNAKE_CASE__ : List[str] ,): super().__init__(features=SCREAMING_SNAKE_CASE__ ,cache_dir=SCREAMING_SNAKE_CASE__ ,keep_in_memory=SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__) __lowerCamelCase : Optional[int] = Sql( cache_dir=SCREAMING_SNAKE_CASE__ ,features=SCREAMING_SNAKE_CASE__ ,sql=SCREAMING_SNAKE_CASE__ ,con=SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__ ,) def lowerCAmelCase ( self : Union[str, Any]): __lowerCamelCase : Any = None __lowerCamelCase : int = None __lowerCamelCase : Dict = None __lowerCamelCase : Optional[int] = None self.builder.download_and_prepare( download_config=SCREAMING_SNAKE_CASE__ ,download_mode=SCREAMING_SNAKE_CASE__ ,verification_mode=SCREAMING_SNAKE_CASE__ ,base_path=SCREAMING_SNAKE_CASE__ ,) # Build dataset for splits __lowerCamelCase : Tuple = self.builder.as_dataset( split='train' ,verification_mode=SCREAMING_SNAKE_CASE__ ,in_memory=self.keep_in_memory) return dataset class A_ : def __init__( self : List[str] ,SCREAMING_SNAKE_CASE__ : str ,SCREAMING_SNAKE_CASE__ : Any ,SCREAMING_SNAKE_CASE__ : List[str] ,SCREAMING_SNAKE_CASE__ : List[Any] = None ,SCREAMING_SNAKE_CASE__ : int = None ,**SCREAMING_SNAKE_CASE__ : List[str] ,): if num_proc is not None and num_proc <= 0: raise ValueError(F"num_proc {num_proc} must be an integer > 0.") __lowerCamelCase : Optional[Any] = dataset __lowerCamelCase : Union[str, Any] = name __lowerCamelCase : Optional[int] = con __lowerCamelCase : Optional[int] = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE __lowerCamelCase : Any = num_proc __lowerCamelCase : List[str] = to_sql_kwargs def lowerCAmelCase ( self : List[Any]): __lowerCamelCase : str = self.to_sql_kwargs.pop('sql' ,SCREAMING_SNAKE_CASE__) __lowerCamelCase : List[str] = self.to_sql_kwargs.pop('con' ,SCREAMING_SNAKE_CASE__) __lowerCamelCase : Union[str, Any] = self.to_sql_kwargs.pop('index' ,SCREAMING_SNAKE_CASE__) __lowerCamelCase : Dict = self._write(index=SCREAMING_SNAKE_CASE__ ,**self.to_sql_kwargs) return written def lowerCAmelCase ( self : List[Any] ,SCREAMING_SNAKE_CASE__ : List[str]): __lowerCamelCase , __lowerCamelCase , __lowerCamelCase : int = args __lowerCamelCase : Optional[Any] = {**to_sql_kwargs, 'if_exists': 'append'} if offset > 0 else to_sql_kwargs __lowerCamelCase : Union[str, Any] = query_table( table=self.dataset.data ,key=slice(SCREAMING_SNAKE_CASE__ ,offset + self.batch_size) ,indices=self.dataset._indices ,) __lowerCamelCase : Tuple = batch.to_pandas() __lowerCamelCase : int = df.to_sql(self.name ,self.con ,index=SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__) return num_rows or len(SCREAMING_SNAKE_CASE__) def lowerCAmelCase ( self : Any ,SCREAMING_SNAKE_CASE__ : str ,**SCREAMING_SNAKE_CASE__ : List[Any]): __lowerCamelCase : List[str] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 ,len(self.dataset) ,self.batch_size) ,unit='ba' ,disable=not logging.is_progress_bar_enabled() ,desc='Creating SQL from Arrow format' ,): written += self._batch_sql((offset, index, to_sql_kwargs)) else: __lowerCamelCase , __lowerCamelCase : Dict = len(self.dataset), self.batch_size with multiprocessing.Pool(self.num_proc) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql ,[(offset, index, to_sql_kwargs) for offset in range(0 ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__)] ,) ,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size ,unit='ba' ,disable=not logging.is_progress_bar_enabled() ,desc='Creating SQL from Arrow format' ,): written += num_rows return written
73
import heapq as hq import math from collections.abc import Iterator class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> Any: _lowercase =str(id_ ) _lowercase =None _lowercase =None _lowercase =[] _lowercase ={} # {vertex:distance} def __lt__(self , UpperCAmelCase ) -> List[str]: return self.key < other.key def __repr__(self ) -> str: return self.id def __A (self , UpperCAmelCase ) -> Dict: self.neighbors.append(UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =weight def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __snake_case ) graph[b - 1].add_edge(graph[a - 1] , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> list: """simple docstring""" _lowercase =[] for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =graph[:] while q: _lowercase =min(__snake_case ) q.remove(__snake_case ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] for i in range(1 , len(__snake_case ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Iterator[tuple]: """simple docstring""" for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =list(__snake_case ) hq.heapify(__snake_case ) while h: _lowercase =hq.heappop(__snake_case ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] hq.heapify(__snake_case ) for i in range(1 , len(__snake_case ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def UpperCAmelCase_ ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
5
0
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) _A : Optional[int] = logging.get_logger(__name__) # pylint: disable=invalid-name _A : str = '\n Examples:\n ```py\n >>> import torch\n >>> import numpy as np\n\n >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline\n >>> from transformers import pipeline\n >>> from diffusers.utils import load_image\n\n\n >>> def make_hint(image, depth_estimator):\n ... image = depth_estimator(image)["depth"]\n ... image = np.array(image)\n ... image = image[:, :, None]\n ... image = np.concatenate([image, image, image], axis=2)\n ... detected_map = torch.from_numpy(image).float() / 255.0\n ... hint = detected_map.permute(2, 0, 1)\n ... return hint\n\n\n >>> depth_estimator = pipeline("depth-estimation")\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16\n ... )\n >>> pipe_prior = pipe_prior.to("cuda")\n\n >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16\n ... )\n >>> pipe = pipe.to("cuda")\n\n\n >>> img = load_image(\n ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"\n ... "/kandinsky/cat.png"\n ... ).resize((768, 768))\n\n >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda")\n\n >>> prompt = "A robot, 4k photo"\n >>> negative_prior_prompt = "lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature"\n\n >>> generator = torch.Generator(device="cuda").manual_seed(43)\n\n >>> image_emb, zero_image_emb = pipe_prior(\n ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator\n ... ).to_tuple()\n\n >>> images = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... hint=hint,\n ... num_inference_steps=50,\n ... generator=generator,\n ... height=768,\n ... width=768,\n ... ).images\n\n >>> images[0].save("robot_cat.png")\n ```\n' def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=8 ) -> Union[str, Any]: """simple docstring""" lowerCamelCase__ : List[str] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 lowerCamelCase__ : Optional[Any] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): def __init__( self : int , A : List[Any] , A : int , A : List[Any] , ) ->Tuple: super().__init__() self.register_modules( unet=A , scheduler=A , movq=A , ) lowerCamelCase__ : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __lowerCamelCase ( self : Dict , A : Any , A : Any , A : Dict , A : Dict , A : Union[str, Any] , A : List[str] ) ->Optional[Any]: if latents is None: lowerCamelCase__ : Dict = randn_tensor(A , generator=A , device=A , dtype=A ) else: if latents.shape != shape: raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {shape}" ) lowerCamelCase__ : Optional[Any] = latents.to(A ) lowerCamelCase__ : Tuple = latents * scheduler.init_noise_sigma return latents def __lowerCamelCase ( self : List[Any] , A : int=0 ) ->Tuple: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) lowerCamelCase__ : int = torch.device(F"cuda:{gpu_id}" ) lowerCamelCase__ : Optional[int] = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(A , A ) def __lowerCamelCase ( self : Dict , A : Optional[Any]=0 ) ->str: if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) lowerCamelCase__ : Union[str, Any] = torch.device(F"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) lowerCamelCase__ : int = None for cpu_offloaded_model in [self.unet, self.movq]: lowerCamelCase__ , lowerCamelCase__ : Tuple = cpu_offload_with_hook(A , A , prev_module_hook=A ) # We'll offload the last model manually. lowerCamelCase__ : Optional[Any] = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __lowerCamelCase ( self : Tuple ) ->Union[str, Any]: if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(A , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(A ) def __call__( self : Dict , A : Optional[Any] , A : List[str] , A : List[str] , A : Tuple = 5_1_2 , A : Optional[int] = 5_1_2 , A : Tuple = 1_0_0 , A : int = 4.0 , A : List[str] = 1 , A : Dict = None , A : Tuple = None , A : Optional[Any] = "pil" , A : Dict = True , ) ->Any: lowerCamelCase__ : List[str] = self._execution_device lowerCamelCase__ : List[Any] = guidance_scale > 1.0 if isinstance(A , A ): lowerCamelCase__ : int = torch.cat(A , dim=0 ) if isinstance(A , A ): lowerCamelCase__ : str = torch.cat(A , dim=0 ) if isinstance(A , A ): lowerCamelCase__ : int = torch.cat(A , dim=0 ) lowerCamelCase__ : Union[str, Any] = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: lowerCamelCase__ : Any = image_embeds.repeat_interleave(A , dim=0 ) lowerCamelCase__ : Union[str, Any] = negative_image_embeds.repeat_interleave(A , dim=0 ) lowerCamelCase__ : Tuple = hint.repeat_interleave(A , dim=0 ) lowerCamelCase__ : List[str] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=A ) lowerCamelCase__ : str = torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=A ) self.scheduler.set_timesteps(A , device=A ) lowerCamelCase__ : List[Any] = self.scheduler.timesteps lowerCamelCase__ : Union[str, Any] = self.movq.config.latent_channels lowerCamelCase__ , lowerCamelCase__ : Tuple = downscale_height_and_width(A , A , self.movq_scale_factor ) # create initial latent lowerCamelCase__ : Tuple = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , A , A , A , self.scheduler , ) for i, t in enumerate(self.progress_bar(A ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase__ : Union[str, Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase__ : int = {'''image_embeds''': image_embeds, '''hint''': hint} lowerCamelCase__ : Tuple = self.unet( sample=A , timestep=A , encoder_hidden_states=A , added_cond_kwargs=A , return_dict=A , )[0] if do_classifier_free_guidance: lowerCamelCase__ , lowerCamelCase__ : str = noise_pred.split(latents.shape[1] , dim=1 ) lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] = noise_pred.chunk(2 ) lowerCamelCase__ , lowerCamelCase__ : List[Any] = variance_pred.chunk(2 ) lowerCamelCase__ : Dict = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) lowerCamelCase__ : Optional[Any] = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): lowerCamelCase__ , lowerCamelCase__ : Optional[Any] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 lowerCamelCase__ : List[str] = self.scheduler.step( A , A , A , generator=A , )[0] # post-processing lowerCamelCase__ : Union[str, Any] = self.movq.decode(A , force_not_quantize=A )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: lowerCamelCase__ : Tuple = image * 0.5 + 0.5 lowerCamelCase__ : Tuple = image.clamp(0 , 1 ) lowerCamelCase__ : Dict = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": lowerCamelCase__ : str = self.numpy_to_pil(A ) if not return_dict: return (image,) return ImagePipelineOutput(images=A )
142
# flake8: noqa # Lint as: python3 UpperCAmelCase__ = [ '''VerificationMode''', '''Version''', '''disable_progress_bar''', '''enable_progress_bar''', '''is_progress_bar_enabled''', '''experimental''', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
5
0
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __a = logging.get_logger(__name__) class A__ ( UpperCamelCase ): """simple docstring""" UpperCamelCase_ : Any = ['''input_features''', '''attention_mask'''] def __init__( self : Union[str, Any] , lowerCAmelCase__ : Dict=8_0 , lowerCAmelCase__ : List[str]=1_6_0_0_0 , lowerCAmelCase__ : Optional[int]=8_0 , lowerCAmelCase__ : Optional[int]=0.0 , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : Union[str, Any]=True , **lowerCAmelCase__ : int , ) -> Dict: """simple docstring""" super().__init__(feature_size=lowerCAmelCase__ , sampling_rate=lowerCAmelCase__ , padding_value=lowerCAmelCase__ , **lowerCAmelCase__ ) _UpperCAmelCase : Any = num_mel_bins _UpperCAmelCase : Any = do_ceptral_normalize _UpperCAmelCase : Union[str, Any] = normalize_means _UpperCAmelCase : int = normalize_vars _UpperCAmelCase : Any = True def _lowerCAmelCase ( self : List[str] , lowerCAmelCase__ : str , ) -> np.ndarray: """simple docstring""" _UpperCAmelCase : List[Any] = waveform * (2**1_5) # Kaldi compliance: 16-bit signed integers _UpperCAmelCase : List[str] = torch.from_numpy(lowerCAmelCase__ ).unsqueeze(0 ) _UpperCAmelCase : Any = ta_kaldi.fbank(lowerCAmelCase__ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def _lowerCAmelCase ( lowerCAmelCase__ : int , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Any = True , lowerCAmelCase__ : Any = True , lowerCAmelCase__ : Any = 0.0 , ) -> np.ndarray: """simple docstring""" if normalize_means: _UpperCAmelCase : Union[str, Any] = x[:input_length].mean(axis=0 ) _UpperCAmelCase : Optional[int] = np.subtract(lowerCAmelCase__ , lowerCAmelCase__ ) if normalize_vars: _UpperCAmelCase : Dict = x[:input_length].std(axis=0 ) _UpperCAmelCase : int = np.divide(lowerCAmelCase__ , lowerCAmelCase__ ) if input_length < x.shape[0]: _UpperCAmelCase : Optional[int] = padding_value # make sure array is in float32 _UpperCAmelCase : Any = x.astype(np.floataa ) return x def _lowerCAmelCase ( self : Optional[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] = None ) -> List[np.ndarray]: """simple docstring""" _UpperCAmelCase : int = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(lowerCAmelCase__ , lowerCAmelCase__ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] def __call__( self : Union[str, Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any] = False , lowerCAmelCase__ : Dict = None , lowerCAmelCase__ : Union[str, Any] = False , lowerCAmelCase__ : Tuple = None , lowerCAmelCase__ : Union[str, Any] = None , lowerCAmelCase__ : int = None , lowerCAmelCase__ : Tuple = None , **lowerCAmelCase__ : int , ) -> BatchFeature: """simple docstring""" 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 `raw_speech` 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." ) _UpperCAmelCase : str = isinstance(lowerCAmelCase__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"""Only mono-channel audio is supported for input to {self}""" ) _UpperCAmelCase : str = is_batched_numpy or ( isinstance(lowerCAmelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: _UpperCAmelCase : int = [np.asarray(lowerCAmelCase__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(lowerCAmelCase__ , np.ndarray ): _UpperCAmelCase : Union[str, Any] = np.asarray(lowerCAmelCase__ , dtype=np.floataa ) elif isinstance(lowerCAmelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): _UpperCAmelCase : str = raw_speech.astype(np.floataa ) # always return batch if not is_batched: _UpperCAmelCase : Optional[int] = [raw_speech] # extract fbank features _UpperCAmelCase : str = [self._extract_fbank_features(lowerCAmelCase__ ) for waveform in raw_speech] # convert into correct format for padding _UpperCAmelCase : Dict = BatchFeature({"input_features": features} ) _UpperCAmelCase : List[Any] = self.pad( lowerCAmelCase__ , padding=lowerCAmelCase__ , max_length=lowerCAmelCase__ , truncation=lowerCAmelCase__ , pad_to_multiple_of=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , **lowerCAmelCase__ , ) # make sure list is in array format _UpperCAmelCase : str = padded_inputs.get("input_features" ) if isinstance(input_features[0] , lowerCAmelCase__ ): _UpperCAmelCase : List[str] = [np.asarray(lowerCAmelCase__ , dtype=np.floataa ) for feature in input_features] _UpperCAmelCase : Tuple = padded_inputs.get("attention_mask" ) if attention_mask is not None: _UpperCAmelCase : Optional[int] = [np.asarray(lowerCAmelCase__ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: _UpperCAmelCase : List[Any] = ( np.array(lowerCAmelCase__ , dtype=np.intaa ) if self._get_padding_strategies(lowerCAmelCase__ , max_length=lowerCAmelCase__ ) is not PaddingStrategy.DO_NOT_PAD else None ) _UpperCAmelCase : Tuple = self.normalize( padded_inputs["input_features"] , attention_mask=lowerCAmelCase__ ) if return_tensors is not None: _UpperCAmelCase : Optional[Any] = padded_inputs.convert_to_tensors(lowerCAmelCase__ ) return padded_inputs
145
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''wavlm''' def __init__(self , UpperCAmelCase=3_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase="group" , UpperCAmelCase="gelu" , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase=False , UpperCAmelCase=1_2_8 , UpperCAmelCase=1_6 , UpperCAmelCase=3_2_0 , UpperCAmelCase=8_0_0 , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.05 , UpperCAmelCase=1_0 , UpperCAmelCase=2 , UpperCAmelCase=0.0 , UpperCAmelCase=1_0 , UpperCAmelCase=3_2_0 , UpperCAmelCase=2 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_0 , UpperCAmelCase=2_5_6 , UpperCAmelCase=2_5_6 , UpperCAmelCase=0.1 , UpperCAmelCase="mean" , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=2_5_6 , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase=(5, 3, 3, 1, 1) , UpperCAmelCase=(1, 2, 3, 1, 1) , UpperCAmelCase=5_1_2 , UpperCAmelCase=8_0 , UpperCAmelCase=0 , UpperCAmelCase=1 , UpperCAmelCase=2 , UpperCAmelCase=False , UpperCAmelCase=3 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase , pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase ) _lowercase =hidden_size _lowercase =feat_extract_norm _lowercase =feat_extract_activation _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =conv_bias _lowercase =num_buckets _lowercase =max_bucket_distance _lowercase =num_conv_pos_embeddings _lowercase =num_conv_pos_embedding_groups _lowercase =len(self.conv_dim ) _lowercase =num_hidden_layers _lowercase =intermediate_size _lowercase =hidden_act _lowercase =num_attention_heads _lowercase =hidden_dropout _lowercase =attention_dropout _lowercase =activation_dropout _lowercase =feat_proj_dropout _lowercase =final_dropout _lowercase =layerdrop _lowercase =layer_norm_eps _lowercase =initializer_range _lowercase =num_ctc_classes _lowercase =vocab_size _lowercase =do_stable_layer_norm _lowercase =use_weighted_layer_sum _lowercase =classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _lowercase =apply_spec_augment _lowercase =mask_time_prob _lowercase =mask_time_length _lowercase =mask_time_min_masks _lowercase =mask_feature_prob _lowercase =mask_feature_length # parameters for pretraining with codevector quantized representations _lowercase =num_codevectors_per_group _lowercase =num_codevector_groups _lowercase =contrastive_logits_temperature _lowercase =num_negatives _lowercase =codevector_dim _lowercase =proj_codevector_dim _lowercase =diversity_loss_weight # ctc loss _lowercase =ctc_loss_reduction _lowercase =ctc_zero_infinity # adapter _lowercase =add_adapter _lowercase =adapter_kernel_size _lowercase =adapter_stride _lowercase =num_adapter_layers _lowercase =output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _lowercase =classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =xvector_output_dim @property def __A (self ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
5
0
'''simple docstring''' from typing import TYPE_CHECKING from ..utils import _LazyModule __A : Any = { "config": [ "EXTERNAL_DATA_FORMAT_SIZE_LIMIT", "OnnxConfig", "OnnxConfigWithPast", "OnnxSeq2SeqConfigWithPast", "PatchingSpec", ], "convert": ["export", "validate_model_outputs"], "features": ["FeaturesManager"], "utils": ["ParameterFormat", "compute_serialized_parameters_size"], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys __A : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
120
import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class lowerCamelCase__ ( unittest.TestCase): def __A (self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() def __A (self ) -> Optional[Any]: _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _lowercase ='''xvjiarui/stable-diffusion-2-inpainting''' _lowercase , _lowercase =FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase , safety_checker=UpperCAmelCase ) _lowercase ='''Face of a yellow cat, high resolution, sitting on a park bench''' _lowercase =jax.random.PRNGKey(0 ) _lowercase =5_0 _lowercase =jax.device_count() _lowercase =num_samples * [prompt] _lowercase =num_samples * [init_image] _lowercase =num_samples * [mask_image] _lowercase , _lowercase , _lowercase =pipeline.prepare_inputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # shard inputs and rng _lowercase =replicate(UpperCAmelCase ) _lowercase =jax.random.split(UpperCAmelCase , jax.device_count() ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =pipeline( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , jit=UpperCAmelCase ) _lowercase =output.images.reshape(UpperCAmelCase , 5_1_2 , 5_1_2 , 3 ) _lowercase =images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] _lowercase =jnp.asarray(jax.device_get(image_slice.flatten() ) ) _lowercase =jnp.array( [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084] ) print(f"output_slice: {output_slice}" ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2
5
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __UpperCAmelCase : Dict = { "configuration_xlm": ["XLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMConfig", "XLMOnnxConfig"], "tokenization_xlm": ["XLMTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Optional[int] = [ "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: __UpperCAmelCase : Union[str, Any] = [ "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 __UpperCAmelCase : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
111
import comet # From: unbabel-comet import torch import datasets UpperCAmelCase__ = datasets.logging.get_logger(__name__) UpperCAmelCase__ = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' UpperCAmelCase__ = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' UpperCAmelCase__ = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): def __A (self ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A (self , UpperCAmelCase ) -> Dict: if self.config_name == "default": _lowercase =comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: _lowercase =comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=False ) -> int: if gpus is None: _lowercase =1 if torch.cuda.is_available() else 0 _lowercase ={'''src''': sources, '''mt''': predictions, '''ref''': references} _lowercase =[dict(zip(UpperCAmelCase , UpperCAmelCase ) ) for t in zip(*data.values() )] _lowercase , _lowercase =self.scorer.predict(UpperCAmelCase , gpus=UpperCAmelCase , progress_bar=UpperCAmelCase ) return {"mean_score": mean_score, "scores": scores}
5
0
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) UpperCAmelCase : List[str] = pytest.mark.integration @pytest.mark.parametrize("path" , ["paws", "csv"] ) def _A ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : List[str] ): """simple docstring""" inspect_dataset(__snake_case , __snake_case ) a__ : Optional[int] =path + ".py" assert script_name in os.listdir(__snake_case ) assert "__pycache__" not in os.listdir(__snake_case ) @pytest.mark.filterwarnings("ignore:inspect_metric is deprecated:FutureWarning" ) @pytest.mark.filterwarnings("ignore:metric_module_factory is deprecated:FutureWarning" ) @pytest.mark.parametrize("path" , ["accuracy"] ) def _A ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : List[Any] ): """simple docstring""" inspect_metric(__snake_case , __snake_case ) a__ : Dict =path + ".py" assert script_name in os.listdir(__snake_case ) assert "__pycache__" not in os.listdir(__snake_case ) @pytest.mark.parametrize( "path, config_name, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def _A ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Dict ): """simple docstring""" a__ : int =get_dataset_config_info(__snake_case , config_name=__snake_case ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def _A ( SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Optional[int] ): """simple docstring""" with pytest.raises(__snake_case ): get_dataset_config_info(__snake_case , config_name=__snake_case ) @pytest.mark.parametrize( "path, expected" , [ ("squad", "plain_text"), ("acronym_identification", "default"), ("lhoestq/squad", "plain_text"), ("lhoestq/test", "default"), ("lhoestq/demo1", "lhoestq--demo1"), ("dalle-mini/wit", "dalle-mini--wit"), ] , ) def _A ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Any ): """simple docstring""" a__ : Optional[Any] =get_dataset_config_names(__snake_case ) assert expected in config_names @pytest.mark.parametrize( "path, expected_configs, expected_splits_in_first_config" , [ ("squad", ["plain_text"], ["train", "validation"]), ("dalle-mini/wit", ["dalle-mini--wit"], ["train"]), ("paws", ["labeled_final", "labeled_swap", "unlabeled_final"], ["train", "test", "validation"]), ] , ) def _A ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Dict ): """simple docstring""" a__ : Optional[Any] =get_dataset_infos(__snake_case ) assert list(infos.keys() ) == expected_configs a__ : Union[str, Any] =expected_configs[0] assert expected_config in infos a__ : str =infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( "path, expected_config, expected_splits" , [ ("squad", "plain_text", ["train", "validation"]), ("dalle-mini/wit", "dalle-mini--wit", ["train"]), ("paws", "labeled_final", ["train", "test", "validation"]), ] , ) def _A ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Dict ): """simple docstring""" a__ : Optional[int] =get_dataset_infos(__snake_case ) assert expected_config in infos a__ : Union[str, Any] =infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( "path, config_name, expected_exception" , [ ("paws", None, ValueError), ] , ) def _A ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Tuple ): """simple docstring""" with pytest.raises(__snake_case ): get_dataset_split_names(__snake_case , config_name=__snake_case )
95
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 1 @register_to_config def __init__(self , UpperCAmelCase=2_0_0_0 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=1e-3 ) -> List[str]: _lowercase =None _lowercase =None _lowercase =None def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> str: _lowercase =torch.linspace(1 , self.config.sampling_eps , UpperCAmelCase , device=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> Optional[int]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _lowercase =( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _lowercase =torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _lowercase =std.flatten() while len(std.shape ) < len(score.shape ): _lowercase =std.unsqueeze(-1 ) _lowercase =-score / std # compute _lowercase =-1.0 / len(self.timesteps ) _lowercase =self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _lowercase =beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _lowercase =beta_t.unsqueeze(-1 ) _lowercase =-0.5 * beta_t * x _lowercase =torch.sqrt(UpperCAmelCase ) _lowercase =drift - diffusion**2 * score _lowercase =x + drift * dt # add noise _lowercase =randn_tensor(x.shape , layout=x.layout , generator=UpperCAmelCase , device=x.device , dtype=x.dtype ) _lowercase =x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__(self ) -> str: return self.config.num_train_timesteps
5
0
from math import log from scipy.constants import Boltzmann, physical_constants A : str = 3_0_0 # TEMPERATURE (unit = K) def __lowerCAmelCase ( a__ , a__ , a__ , ) -> float: if donor_conc <= 0: raise ValueError('''Donor concentration should be positive''' ) elif acceptor_conc <= 0: raise ValueError('''Acceptor concentration should be positive''' ) elif intrinsic_conc <= 0: raise ValueError('''Intrinsic concentration should be positive''' ) elif donor_conc <= intrinsic_conc: raise ValueError( '''Donor concentration should be greater than intrinsic concentration''' ) elif acceptor_conc <= intrinsic_conc: raise ValueError( '''Acceptor concentration should be greater than intrinsic concentration''' ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
6
def __lowerCAmelCase ( a__ ) -> str: __a = [] __a = set({'''(''', '''[''', '''{'''} ) __a = set({''')''', ''']''', '''}'''} ) __a = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(a__ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(a__ ) == 0 or (len(a__ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(a__ ) == 0 def __lowerCAmelCase ( ) -> Dict: __a = input('''Enter sequence of brackets: ''' ) if is_balanced(a__ ): print(a__ , '''is balanced''' ) else: print(a__ , '''is not balanced''' ) if __name__ == "__main__": main()
6
1
from math import sqrt def __lowerCAmelCase ( a__ ) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(sqrt(a__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def __lowerCAmelCase ( a__ = 1_0001 ) -> int: __a = 0 __a = 1 while count != nth and number < 3: number += 1 if is_prime(a__ ): count += 1 while count != nth: number += 2 if is_prime(a__ ): count += 1 return number if __name__ == "__main__": print(F"{solution() = }")
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : str = { 'configuration_blenderbot': [ 'BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BlenderbotConfig', 'BlenderbotOnnxConfig', ], 'tokenization_blenderbot': ['BlenderbotTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ['BlenderbotTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ 'BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlenderbotForCausalLM', 'BlenderbotForConditionalGeneration', 'BlenderbotModel', 'BlenderbotPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'TFBlenderbotForConditionalGeneration', 'TFBlenderbotModel', 'TFBlenderbotPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'FlaxBlenderbotForConditionalGeneration', 'FlaxBlenderbotModel', 'FlaxBlenderbotPreTrainedModel', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys A : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation A : Dict = logging.get_logger(__name__) A : List[Any] = {'tokenizer_file': 'tokenizer.json'} A : Tuple = { 'tokenizer_file': { 'bigscience/tokenizer': 'https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json', 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json', }, } class __A( a ): snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = ['''input_ids''', '''attention_mask'''] snake_case_ = None def __init__( self , _snake_case=None , _snake_case=None , _snake_case=None , _snake_case="<unk>" , _snake_case="<s>" , _snake_case="</s>" , _snake_case="<pad>" , _snake_case=False , _snake_case=False , **_snake_case , ) -> Tuple: '''simple docstring''' super().__init__( _snake_case , _snake_case , tokenizer_file=_snake_case , unk_token=_snake_case , bos_token=_snake_case , eos_token=_snake_case , pad_token=_snake_case , add_prefix_space=_snake_case , clean_up_tokenization_spaces=_snake_case , **_snake_case , ) __a = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _snake_case ) != add_prefix_space: __a = getattr(_snake_case , pre_tok_state.pop('''type''' ) ) __a = add_prefix_space __a = pre_tok_class(**_snake_case ) __a = add_prefix_space def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> BatchEncoding: '''simple docstring''' __a = kwargs.get('''is_split_into_words''' , _snake_case ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with""" ''' pretokenized inputs.''' ) return super()._batch_encode_plus(*_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> BatchEncoding: '''simple docstring''' __a = kwargs.get('''is_split_into_words''' , _snake_case ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with""" ''' pretokenized inputs.''' ) return super()._encode_plus(*_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None ) -> Tuple[str]: '''simple docstring''' __a = self._tokenizer.model.save(_snake_case , name=_snake_case ) return tuple(_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> List[int]: '''simple docstring''' __a = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(_snake_case , add_special_tokens=_snake_case ) + [self.eos_token_id] ) if len(_snake_case ) > self.model_max_length: __a = input_ids[-self.model_max_length :] return input_ids
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : Dict = { 'configuration_xlm_roberta': [ 'XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMRobertaConfig', 'XLMRobertaOnnxConfig', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Union[str, Any] = ['XLMRobertaTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = ['XLMRobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = [ 'XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMRobertaForCausalLM', 'XLMRobertaForMaskedLM', 'XLMRobertaForMultipleChoice', 'XLMRobertaForQuestionAnswering', 'XLMRobertaForSequenceClassification', 'XLMRobertaForTokenClassification', 'XLMRobertaModel', 'XLMRobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = [ 'TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFXLMRobertaForCausalLM', 'TFXLMRobertaForMaskedLM', 'TFXLMRobertaForMultipleChoice', 'TFXLMRobertaForQuestionAnswering', 'TFXLMRobertaForSequenceClassification', 'TFXLMRobertaForTokenClassification', 'TFXLMRobertaModel', 'TFXLMRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ 'FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'FlaxXLMRobertaForMaskedLM', 'FlaxXLMRobertaForCausalLM', 'FlaxXLMRobertaForMultipleChoice', 'FlaxXLMRobertaForQuestionAnswering', 'FlaxXLMRobertaForSequenceClassification', 'FlaxXLMRobertaForTokenClassification', 'FlaxXLMRobertaModel', 'FlaxXLMRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A : Tuple = logging.get_logger(__name__) A : Dict = { 'RWKV/rwkv-4-169m-pile': 'https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json', 'RWKV/rwkv-4-430m-pile': 'https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json', 'RWKV/rwkv-4-1b5-pile': 'https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json', 'RWKV/rwkv-4-3b-pile': 'https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json', 'RWKV/rwkv-4-7b-pile': 'https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json', 'RWKV/rwkv-4-14b-pile': 'https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json', 'RWKV/rwkv-raven-1b5': 'https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json', 'RWKV/rwkv-raven-3b': 'https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json', 'RWKV/rwkv-raven-7b': 'https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json', 'RWKV/rwkv-raven-14b': 'https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json', } class __A( a ): snake_case_ = '''rwkv''' snake_case_ = {'''max_position_embeddings''': '''context_length'''} def __init__( self , _snake_case=50_277 , _snake_case=1_024 , _snake_case=4_096 , _snake_case=32 , _snake_case=None , _snake_case=None , _snake_case=1E-5 , _snake_case=0 , _snake_case=0 , _snake_case=6 , _snake_case=False , _snake_case=True , **_snake_case , ) -> List[str]: '''simple docstring''' __a = vocab_size __a = context_length __a = hidden_size __a = num_hidden_layers __a = attention_hidden_size if attention_hidden_size is not None else hidden_size __a = intermediate_size if intermediate_size is not None else 4 * hidden_size __a = layer_norm_epsilon __a = rescale_every __a = use_cache __a = bos_token_id __a = eos_token_id super().__init__( tie_word_embeddings=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case )
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : Optional[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 : int = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = [ '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 : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionAttendAndExcitePipeline, UNetaDConditionModel, ) from diffusers.utils import load_numpy, skip_mps, slow from diffusers.utils.testing_utils import require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin A : List[Any] = False @skip_mps class __A( a , a , a , unittest.TestCase ): snake_case_ = StableDiffusionAttendAndExcitePipeline snake_case_ = False snake_case_ = TEXT_TO_IMAGE_PARAMS snake_case_ = TEXT_TO_IMAGE_BATCH_PARAMS.union({'''token_indices'''} ) snake_case_ = TEXT_TO_IMAGE_IMAGE_PARAMS snake_case_ = TEXT_TO_IMAGE_IMAGE_PARAMS @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> List[str]: '''simple docstring''' super().setUpClass() torch.use_deterministic_algorithms(_snake_case ) @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> int: '''simple docstring''' super().tearDownClass() torch.use_deterministic_algorithms(_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=_snake_case , ) __a = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=_snake_case , set_alpha_to_one=_snake_case , ) torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) __a = CLIPTextModel(_snake_case ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case=0 ) -> Any: '''simple docstring''' if str(_snake_case ).startswith('''mps''' ): __a = torch.manual_seed(_snake_case ) else: __a = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) __a = __a = { '''prompt''': '''a cat and a frog''', '''token_indices''': [2, 5], '''generator''': generator, '''num_inference_steps''': 1, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', '''max_iter_to_alter''': 2, '''thresholds''': {0: 0.7}, } return inputs def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' __a = '''cpu''' __a = self.get_dummy_components() __a = self.pipeline_class(**_snake_case ) pipe.to(_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) __a = self.get_dummy_inputs(_snake_case ) __a = pipe(**_snake_case ).images __a = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 64, 64, 3) ) __a = np.array( [0.6390_5364, 0.6289_7307, 0.4859_9017, 0.513_3624, 0.555_0048, 0.4576_9516, 0.5032_6973, 0.502_3139, 0.4538_4496] ) __a = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_snake_case , 1E-3 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' super().test_cpu_offload_forward_pass(expected_max_diff=5E-4 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' self._test_inference_batch_single_identical(batch_size=2 , expected_max_diff=7E-4 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5E-4 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' super().test_save_load_local(expected_max_difference=5E-4 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' super().test_save_load_optional_components(expected_max_difference=4E-4 ) @require_torch_gpu @slow class __A( unittest.TestCase ): @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> Dict: '''simple docstring''' super().setUpClass() torch.use_deterministic_algorithms(_snake_case ) @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> Union[str, Any]: '''simple docstring''' super().tearDownClass() torch.use_deterministic_algorithms(_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' __a = torch.manual_seed(51 ) __a = StableDiffusionAttendAndExcitePipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , safety_checker=_snake_case , torch_dtype=torch.floataa ) pipe.to('''cuda''' ) __a = '''a painting of an elephant with glasses''' __a = [5, 7] __a = pipe( prompt=_snake_case , token_indices=_snake_case , guidance_scale=7.5 , generator=_snake_case , num_inference_steps=5 , max_iter_to_alter=5 , output_type='''numpy''' , ).images[0] __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy''' ) assert np.abs((expected_image - image).max() ) < 5E-1
6
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( 'stable diffusion controlnet', '0.22.0', 'Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.', standard_warn=False, stacklevel=3, )
6
1
import math import flax.linen as nn import jax.numpy as jnp def __lowerCAmelCase ( a__ , a__ , a__ = 1 , a__ = 1 , a__ = 1.0e4 , a__ = False , a__ = 1.0 , ) -> jnp.ndarray: assert timesteps.ndim == 1, "Timesteps should be a 1d-array" assert embedding_dim % 2 == 0, F"""Embedding dimension {embedding_dim} should be even""" __a = float(embedding_dim // 2 ) __a = math.log(max_timescale / min_timescale ) / (num_timescales - freq_shift) __a = min_timescale * jnp.exp(jnp.arange(a__ , dtype=jnp.floataa ) * -log_timescale_increment ) __a = jnp.expand_dims(a__ , 1 ) * jnp.expand_dims(a__ , 0 ) # scale embeddings __a = scale * emb if flip_sin_to_cos: __a = jnp.concatenate([jnp.cos(a__ ), jnp.sin(a__ )] , axis=1 ) else: __a = jnp.concatenate([jnp.sin(a__ ), jnp.cos(a__ )] , axis=1 ) __a = jnp.reshape(a__ , [jnp.shape(a__ )[0], embedding_dim] ) return signal class __A( nn.Module ): snake_case_ = 3_2 snake_case_ = jnp.floataa @nn.compact def __call__( self , _snake_case ) -> str: '''simple docstring''' __a = nn.Dense(self.time_embed_dim , dtype=self.dtype , name='''linear_1''' )(_snake_case ) __a = nn.silu(_snake_case ) __a = nn.Dense(self.time_embed_dim , dtype=self.dtype , name='''linear_2''' )(_snake_case ) return temb class __A( nn.Module ): snake_case_ = 3_2 snake_case_ = False snake_case_ = 1 @nn.compact def __call__( self , _snake_case ) -> Dict: '''simple docstring''' return get_sinusoidal_embeddings( _snake_case , embedding_dim=self.dim , flip_sin_to_cos=self.flip_sin_to_cos , freq_shift=self.freq_shift )
6
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=a ) class __A( a ): snake_case_ = field(default='''language-modeling''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) snake_case_ = Features({'''text''': Value('''string''' )} ) snake_case_ = Features({} ) snake_case_ = "text" @property def SCREAMING_SNAKE_CASE_ ( self ) -> Dict[str, str]: '''simple docstring''' return {self.text_column: "text"}
6
1
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING A : Optional[int] = logging.get_logger(__name__) A : List[Any] = { 'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json', } class __A( a ): snake_case_ = '''instructblip_vision_model''' def __init__( self , _snake_case=1_408 , _snake_case=6_144 , _snake_case=39 , _snake_case=16 , _snake_case=224 , _snake_case=14 , _snake_case="gelu" , _snake_case=1E-6 , _snake_case=0.0 , _snake_case=1E-10 , _snake_case=True , **_snake_case , ) -> List[str]: '''simple docstring''' super().__init__(**_snake_case ) __a = hidden_size __a = intermediate_size __a = num_hidden_layers __a = num_attention_heads __a = patch_size __a = image_size __a = initializer_range __a = attention_dropout __a = layer_norm_eps __a = hidden_act __a = qkv_bias @classmethod def SCREAMING_SNAKE_CASE_ ( cls , _snake_case , **_snake_case ) -> "PretrainedConfig": '''simple docstring''' cls._set_token_in_kwargs(_snake_case ) __a , __a = cls.get_config_dict(_snake_case , **_snake_case ) # get the vision config dict if we are loading from InstructBlipConfig if config_dict.get('''model_type''' ) == "instructblip": __a = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """ F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_snake_case , **_snake_case ) class __A( a ): snake_case_ = '''instructblip_qformer''' def __init__( self , _snake_case=30_522 , _snake_case=768 , _snake_case=12 , _snake_case=12 , _snake_case=3_072 , _snake_case="gelu" , _snake_case=0.1 , _snake_case=0.1 , _snake_case=512 , _snake_case=0.02 , _snake_case=1E-12 , _snake_case=0 , _snake_case="absolute" , _snake_case=2 , _snake_case=1_408 , **_snake_case , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=_snake_case , **_snake_case ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = initializer_range __a = layer_norm_eps __a = position_embedding_type __a = cross_attention_frequency __a = encoder_hidden_size @classmethod def SCREAMING_SNAKE_CASE_ ( cls , _snake_case , **_snake_case ) -> "PretrainedConfig": '''simple docstring''' cls._set_token_in_kwargs(_snake_case ) __a , __a = cls.get_config_dict(_snake_case , **_snake_case ) # get the qformer config dict if we are loading from InstructBlipConfig if config_dict.get('''model_type''' ) == "instructblip": __a = config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """ F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_snake_case , **_snake_case ) class __A( a ): snake_case_ = '''instructblip''' snake_case_ = True def __init__( self , _snake_case=None , _snake_case=None , _snake_case=None , _snake_case=32 , **_snake_case ) -> Optional[Any]: '''simple docstring''' super().__init__(**_snake_case ) if vision_config is None: __a = {} logger.info('''vision_config is None. initializing the InstructBlipVisionConfig with default values.''' ) if qformer_config is None: __a = {} logger.info('''qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.''' ) if text_config is None: __a = {} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) __a = InstructBlipVisionConfig(**_snake_case ) __a = InstructBlipQFormerConfig(**_snake_case ) __a = text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' __a = CONFIG_MAPPING[text_model_type](**_snake_case ) __a = self.text_config.tie_word_embeddings __a = self.text_config.is_encoder_decoder __a = num_query_tokens __a = self.vision_config.hidden_size __a = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES __a = 1.0 __a = 0.02 @classmethod def SCREAMING_SNAKE_CASE_ ( cls , _snake_case , _snake_case , _snake_case , **_snake_case , ) -> Optional[int]: '''simple docstring''' return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **_snake_case , ) def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' __a = copy.deepcopy(self.__dict__ ) __a = self.vision_config.to_dict() __a = self.qformer_config.to_dict() __a = self.text_config.to_dict() __a = self.__class__.model_type return output
6
import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def __lowerCAmelCase ( a__ , a__ , a__=1024 , a__=1024 , a__=False , **a__ ) -> Optional[Any]: __a = AutoTokenizer.from_pretrained(a__ ) __a = SeqaSeqDataset(a__ , a__ , a__ , a__ , type_path='''train''' , **a__ ) __a = tok.pad_token_id def get_lens(a__ ): __a = tqdm( DataLoader(a__ , batch_size=512 , num_workers=8 , shuffle=a__ , collate_fn=ds.collate_fn ) , desc=str(ds.len_file ) , ) __a = [] for batch in dl: __a = batch['''input_ids'''].ne(a__ ).sum(1 ).tolist() __a = batch['''labels'''].ne(a__ ).sum(1 ).tolist() if consider_target: for src, tgt in zip(a__ , a__ ): max_lens.append(max(a__ , a__ ) ) else: max_lens.extend(a__ ) return max_lens __a = get_lens(a__ ) __a = SeqaSeqDataset(a__ , a__ , a__ , a__ , type_path='''val''' , **a__ ) __a = get_lens(a__ ) pickle_save(a__ , train_ds.len_file ) pickle_save(a__ , val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
6
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) A : Dict = { 'configuration_trocr': ['TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TrOCRConfig'], 'processing_trocr': ['TrOCRProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ 'TROCR_PRETRAINED_MODEL_ARCHIVE_LIST', 'TrOCRForCausalLM', 'TrOCRPreTrainedModel', ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys A : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
from math import cos, sin, sqrt, tau from audio_filters.iir_filter import IIRFilter def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = (1 - _cos) / 2 __a = 1 - _cos __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = (1 + _cos) / 2 __a = -1 - _cos __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = _sin / 2 __a = 0 __a = -ba __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 1 - alpha __a = -2 * _cos __a = 1 + alpha __a = IIRFilter(2 ) filt.set_coefficients([ba, ba, ba] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = 1 + alpha * big_a __a = -2 * _cos __a = 1 - alpha * big_a __a = 1 + alpha / big_a __a = -2 * _cos __a = 1 - alpha / big_a __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = (big_a + 1) - (big_a - 1) * _cos __a = (big_a + 1) + (big_a - 1) * _cos __a = (big_a - 1) - (big_a + 1) * _cos __a = (big_a - 1) + (big_a + 1) * _cos __a = 2 * sqrt(a__ ) * alpha __a = big_a * (pmc + aaa) __a = 2 * big_a * mpc __a = big_a * (pmc - aaa) __a = ppmc + aaa __a = -2 * pmpc __a = ppmc - aaa __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = (big_a + 1) - (big_a - 1) * _cos __a = (big_a + 1) + (big_a - 1) * _cos __a = (big_a - 1) - (big_a + 1) * _cos __a = (big_a - 1) + (big_a + 1) * _cos __a = 2 * sqrt(a__ ) * alpha __a = big_a * (ppmc + aaa) __a = -2 * big_a * pmpc __a = big_a * (ppmc - aaa) __a = pmc + aaa __a = 2 * mpc __a = pmc - aaa __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt
6
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : Union[str, Any] = { 'xlm-roberta-base': 'https://huggingface.co/xlm-roberta-base/resolve/main/config.json', 'xlm-roberta-large': 'https://huggingface.co/xlm-roberta-large/resolve/main/config.json', 'xlm-roberta-large-finetuned-conll02-dutch': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll02-spanish': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll03-english': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll03-german': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json' ), } class __A( a ): snake_case_ = '''xlm-roberta''' def __init__( self , _snake_case=30_522 , _snake_case=768 , _snake_case=12 , _snake_case=12 , _snake_case=3_072 , _snake_case="gelu" , _snake_case=0.1 , _snake_case=0.1 , _snake_case=512 , _snake_case=2 , _snake_case=0.02 , _snake_case=1E-12 , _snake_case=1 , _snake_case=0 , _snake_case=2 , _snake_case="absolute" , _snake_case=True , _snake_case=None , **_snake_case , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = layer_norm_eps __a = position_embedding_type __a = use_cache __a = classifier_dropout class __A( a ): @property def SCREAMING_SNAKE_CASE_ ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": __a = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __a = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
6
def __lowerCAmelCase ( a__ , a__ , a__ ) -> list: __a = len(a__ ) __a = [[0] * n for i in range(a__ )] for i in range(a__ ): __a = y_points[i] for i in range(2 , a__ ): for j in range(a__ , a__ ): __a = ( (xa - x_points[j - i + 1]) * q[j][i - 1] - (xa - x_points[j]) * q[j - 1][i - 1] ) / (x_points[j] - x_points[j - i + 1]) return [q[n - 1][n - 1], q] if __name__ == "__main__": import doctest doctest.testmod()
6
1
def __lowerCAmelCase ( a__ , a__ ) -> float: def get_matched_characters(a__ , a__ ) -> str: __a = [] __a = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): __a = int(max(0 , i - limit ) ) __a = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(a__ ) __a = F"""{_stra[0:_stra.index(a__ )]} {_stra[_stra.index(a__ ) + 1:]}""" return "".join(a__ ) # matching characters __a = get_matched_characters(a__ , a__ ) __a = get_matched_characters(a__ , a__ ) __a = len(a__ ) # transposition __a = ( len([(ca, ca) for ca, ca in zip(a__ , a__ ) if ca != ca] ) // 2 ) if not match_count: __a = 0.0 else: __a = ( 1 / 3 * ( match_count / len(a__ ) + match_count / len(a__ ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters __a = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
6
from __future__ import annotations import time from collections.abc import Sequence from random import randint from matplotlib import pyplot as plt def __lowerCAmelCase ( a__ , a__ , a__ ) -> tuple[int | None, int | None, float]: if not arr: return None, None, 0 if low == high: return low, high, arr[low] __a = (low + high) // 2 __a , __a , __a = max_subarray(a__ , a__ , a__ ) __a , __a , __a = max_subarray(a__ , mid + 1 , a__ ) __a , __a , __a = max_cross_sum(a__ , a__ , a__ , a__ ) if left_sum >= right_sum and left_sum >= cross_sum: return left_low, left_high, left_sum elif right_sum >= left_sum and right_sum >= cross_sum: return right_low, right_high, right_sum return cross_left, cross_right, cross_sum def __lowerCAmelCase ( a__ , a__ , a__ , a__ ) -> tuple[int, int, float]: __a , __a = float('''-inf''' ), -1 __a , __a = float('''-inf''' ), -1 __a = 0 for i in range(a__ , low - 1 , -1 ): summ += arr[i] if summ > left_sum: __a = summ __a = i __a = 0 for i in range(mid + 1 , high + 1 ): summ += arr[i] if summ > right_sum: __a = summ __a = i return max_left, max_right, (left_sum + right_sum) def __lowerCAmelCase ( a__ ) -> float: __a = [randint(1 , a__ ) for _ in range(a__ )] __a = time.time() max_subarray(a__ , 0 , input_size - 1 ) __a = time.time() return end - start def __lowerCAmelCase ( ) -> None: __a = [10, 100, 1000, 1_0000, 5_0000, 10_0000, 20_0000, 30_0000, 40_0000, 50_0000] __a = [time_max_subarray(a__ ) for input_size in input_sizes] print('''No of Inputs\t\tTime Taken''' ) for input_size, runtime in zip(a__ , a__ ): print(a__ , '''\t\t''' , a__ ) plt.plot(a__ , a__ ) plt.xlabel('''Number of Inputs''' ) plt.ylabel('''Time taken in seconds''' ) plt.show() if __name__ == "__main__": from doctest import testmod testmod()
6
1
from __future__ import annotations from functools import lru_cache from math import ceil A : str = 1_0_0 A : Union[str, Any] = set(range(3, NUM_PRIMES, 2)) primes.add(2) A : int for prime in range(3, ceil(NUM_PRIMES**0.5), 2): if prime not in primes: continue primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) @lru_cache(maxsize=100 ) def __lowerCAmelCase ( a__ ) -> set[int]: if number_to_partition < 0: return set() elif number_to_partition == 0: return {1} __a = set() __a = 42 __a = 42 for prime in primes: if prime > number_to_partition: continue for sub in partition(number_to_partition - prime ): ret.add(sub * prime ) return ret def __lowerCAmelCase ( a__ = 5000 ) -> int | None: for number_to_partition in range(1 , a__ ): if len(partition(a__ ) ) > number_unique_partitions: return number_to_partition return None if __name__ == "__main__": print(F"{solution() = }")
6
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __A( a , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __A( unittest.TestCase ): @property def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = ort.SessionOptions() __a = False return options def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) __a = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) __a = '''A red cat sitting on a park bench''' __a = np.random.RandomState(0 ) __a = pipe( prompt=_snake_case , image=_snake_case , mask_image=_snake_case , guidance_scale=7.5 , num_inference_steps=10 , generator=_snake_case , output_type='''np''' , ) __a = output.images __a = images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) __a = np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) __a = LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) __a = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=_snake_case , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) __a = '''A red cat sitting on a park bench''' __a = np.random.RandomState(0 ) __a = pipe( prompt=_snake_case , image=_snake_case , mask_image=_snake_case , guidance_scale=7.5 , num_inference_steps=20 , generator=_snake_case , output_type='''np''' , ) __a = output.images __a = images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) __a = np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3
6
1
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
6
from math import ceil def __lowerCAmelCase ( a__ = 1001 ) -> int: __a = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): __a = 2 * i + 1 __a = 2 * i __a = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[Any] = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number')
6
1
from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class __A: snake_case_ = 42 snake_case_ = None # Automatically constructed snake_case_ = "dict" snake_case_ = None snake_case_ = field(default='''Translation''' , init=a , repr=a ) def __call__( self ) -> Tuple: '''simple docstring''' return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE_ ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: '''simple docstring''' from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __A: snake_case_ = None snake_case_ = None snake_case_ = None # Automatically constructed snake_case_ = "dict" snake_case_ = None snake_case_ = field(default='''TranslationVariableLanguages''' , init=a , repr=a ) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a = sorted(set(self.languages ) ) if self.languages else None __a = len(self.languages ) if self.languages else None def __call__( self ) -> Any: '''simple docstring''' return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> str: '''simple docstring''' __a = set(self.languages ) if self.languages and set(_snake_case ) - lang_set: raise ValueError( F"""Some languages in example ({', '.join(sorted(set(_snake_case ) - lang_set ) )}) are not in valid set ({', '.join(_snake_case )}).""" ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. __a = [] for lang, text in translation_dict.items(): if isinstance(_snake_case , _snake_case ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. __a , __a = zip(*sorted(_snake_case ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE_ ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: '''simple docstring''' from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
6
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __A( a ): snake_case_ = ['''image_processor''', '''tokenizer'''] snake_case_ = '''ChineseCLIPImageProcessor''' snake_case_ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self , _snake_case=None , _snake_case=None , **_snake_case ) -> Tuple: '''simple docstring''' __a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _snake_case , ) __a = kwargs.pop('''feature_extractor''' ) __a = 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__(_snake_case , _snake_case ) __a = self.image_processor def __call__( self , _snake_case=None , _snake_case=None , _snake_case=None , **_snake_case ) -> Optional[Any]: '''simple docstring''' 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: __a = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) if images is not None: __a = self.image_processor(_snake_case , return_tensors=_snake_case , **_snake_case ) if text is not None and images is not None: __a = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**_snake_case ) , tensor_type=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> Dict: '''simple docstring''' return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' __a = self.tokenizer.model_input_names __a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _snake_case , ) return self.image_processor_class
6
1
from __future__ import annotations def __lowerCAmelCase ( a__ , a__ ) -> float: __a = sorted(numsa + numsa ) __a , __a = divmod(len(a__ ) , 2 ) if mod == 1: return all_numbers[div] else: return (all_numbers[div] + all_numbers[div - 1]) / 2 if __name__ == "__main__": import doctest doctest.testmod() A : Optional[int] = [float(x) for x in input('Enter the elements of first array: ').split()] A : Any = [float(x) for x in input('Enter the elements of second array: ').split()] print(F"The median of two arrays is: {median_of_two_arrays(array_a, array_a)}")
6
from __future__ import annotations import typing from collections import Counter def __lowerCAmelCase ( a__ ) -> typing.Counter[int]: __a = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(a__ , max_perimeter + 1 ): __a = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(a__ ): __a = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def __lowerCAmelCase ( a__ = 1000 ) -> int: __a = pythagorean_triple(a__ ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(F"Perimeter {solution()} has maximum solutions")
6
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL A : Optional[int] = logging.get_logger(__name__) class __A( a ): snake_case_ = ['''pixel_values'''] def __init__( self , _snake_case = True , _snake_case = None , _snake_case = PILImageResampling.BICUBIC , _snake_case = True , _snake_case = 1 / 255 , _snake_case = True , _snake_case = None , _snake_case = None , _snake_case = True , **_snake_case , ) -> None: '''simple docstring''' super().__init__(**_snake_case ) __a = size if size is not None else {'''height''': 384, '''width''': 384} __a = get_size_dict(_snake_case , default_to_square=_snake_case ) __a = do_resize __a = size __a = resample __a = do_rescale __a = rescale_factor __a = do_normalize __a = image_mean if image_mean is not None else OPENAI_CLIP_MEAN __a = image_std if image_std is not None else OPENAI_CLIP_STD __a = do_convert_rgb def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = PILImageResampling.BICUBIC , _snake_case = None , **_snake_case , ) -> np.ndarray: '''simple docstring''' __a = get_size_dict(_snake_case , default_to_square=_snake_case ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) __a = (size['''height'''], size['''width''']) return resize(_snake_case , size=_snake_case , resample=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = None , **_snake_case , ) -> Tuple: '''simple docstring''' return rescale(_snake_case , scale=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case = None , **_snake_case , ) -> np.ndarray: '''simple docstring''' return normalize(_snake_case , mean=_snake_case , std=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = ChannelDimension.FIRST , **_snake_case , ) -> PIL.Image.Image: '''simple docstring''' __a = do_resize if do_resize is not None else self.do_resize __a = resample if resample is not None else self.resample __a = do_rescale if do_rescale is not None else self.do_rescale __a = rescale_factor if rescale_factor is not None else self.rescale_factor __a = do_normalize if do_normalize is not None else self.do_normalize __a = image_mean if image_mean is not None else self.image_mean __a = image_std if image_std is not None else self.image_std __a = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb __a = size if size is not None else self.size __a = get_size_dict(_snake_case , default_to_square=_snake_case ) __a = make_list_of_images(_snake_case ) if not valid_images(_snake_case ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: __a = [convert_to_rgb(_snake_case ) for image in images] # All transformations expect numpy arrays. __a = [to_numpy_array(_snake_case ) for image in images] if do_resize: __a = [self.resize(image=_snake_case , size=_snake_case , resample=_snake_case ) for image in images] if do_rescale: __a = [self.rescale(image=_snake_case , scale=_snake_case ) for image in images] if do_normalize: __a = [self.normalize(image=_snake_case , mean=_snake_case , std=_snake_case ) for image in images] __a = [to_channel_dimension_format(_snake_case , _snake_case ) for image in images] __a = BatchFeature(data={'''pixel_values''': images} , tensor_type=_snake_case ) return encoded_outputs
6
# flake8: noqa # Lint as: python3 A : Optional[Any] = [ 'VerificationMode', 'Version', 'disable_progress_bar', 'enable_progress_bar', 'is_progress_bar_enabled', 'experimental', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
6
1
from sklearn.metrics import matthews_corrcoef import datasets A : int = '\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n' A : Dict = '\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric("matthews_correlation")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results[\'matthews_correlation\'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric("matthews_correlation")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results[\'matthews_correlation\'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric("matthews_correlation")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results[\'matthews_correlation\'], 2))\n -0.25\n' A : Optional[int] = '\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A( datasets.Metric ): def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ) , reference_urls=[ '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html''' ] , ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case=None ) -> Any: '''simple docstring''' return { "matthews_correlation": float(matthews_corrcoef(_snake_case , _snake_case , sample_weight=_snake_case ) ), }
6
from typing import Dict from .base import GenericTensor, Pipeline class __A( a ): def SCREAMING_SNAKE_CASE_ ( self , _snake_case=None , _snake_case=None , _snake_case=None , **_snake_case ) -> Optional[Any]: '''simple docstring''' if tokenize_kwargs is None: __a = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( '''truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)''' ) __a = truncation __a = tokenize_kwargs __a = {} if return_tensors is not None: __a = return_tensors return preprocess_params, {}, postprocess_params def SCREAMING_SNAKE_CASE_ ( self , _snake_case , **_snake_case ) -> Dict[str, GenericTensor]: '''simple docstring''' __a = self.framework __a = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Optional[Any]: '''simple docstring''' __a = self.model(**_snake_case ) return model_outputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case=False ) -> Optional[int]: '''simple docstring''' if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self , *_snake_case , **_snake_case ) -> Any: '''simple docstring''' return super().__call__(*_snake_case , **_snake_case )
6
1
import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('''dataset_size''' , [None, 400 * 2**20, 600 * 2**20] ) @pytest.mark.parametrize('''input_in_memory_max_size''' , ['''default''', 0, 100 * 2**20, 900 * 2**20] ) def __lowerCAmelCase ( a__ , a__ , a__ ) -> Tuple: if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , '''IN_MEMORY_MAX_SIZE''' , a__ ) __a = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: __a = dataset_size < in_memory_max_size else: __a = False __a = is_small_dataset(a__ ) assert result == expected
6
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 : List[str] = logging.get_logger(__name__) A : Optional[int] = { 'facebook/levit-128S': 'https://huggingface.co/facebook/levit-128S/resolve/main/config.json', # See all LeViT models at https://huggingface.co/models?filter=levit } class __A( a ): snake_case_ = '''levit''' def __init__( self , _snake_case=224 , _snake_case=3 , _snake_case=3 , _snake_case=2 , _snake_case=1 , _snake_case=16 , _snake_case=[128, 256, 384] , _snake_case=[4, 8, 12] , _snake_case=[4, 4, 4] , _snake_case=[16, 16, 16] , _snake_case=0 , _snake_case=[2, 2, 2] , _snake_case=[2, 2, 2] , _snake_case=0.02 , **_snake_case , ) -> Optional[Any]: '''simple docstring''' super().__init__(**_snake_case ) __a = image_size __a = num_channels __a = kernel_size __a = stride __a = padding __a = hidden_sizes __a = num_attention_heads __a = depths __a = key_dim __a = drop_path_rate __a = patch_size __a = attention_ratio __a = mlp_ratio __a = initializer_range __a = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __A( a ): snake_case_ = version.parse('''1.11''' ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> float: '''simple docstring''' return 1E-4
6
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : str = { 'configuration_blenderbot': [ 'BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BlenderbotConfig', 'BlenderbotOnnxConfig', ], 'tokenization_blenderbot': ['BlenderbotTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ['BlenderbotTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ 'BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlenderbotForCausalLM', 'BlenderbotForConditionalGeneration', 'BlenderbotModel', 'BlenderbotPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'TFBlenderbotForConditionalGeneration', 'TFBlenderbotModel', 'TFBlenderbotPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'FlaxBlenderbotForConditionalGeneration', 'FlaxBlenderbotModel', 'FlaxBlenderbotPreTrainedModel', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys A : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel A : int = '0.12' # assumed parallelism: 8 @require_flax @is_staging_test class __A( unittest.TestCase ): @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> Union[str, Any]: '''simple docstring''' __a = TOKEN HfFolder.save_token(_snake_case ) @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> Union[str, Any]: '''simple docstring''' try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_snake_case ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""" ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_snake_case , repo_id='''test-model-flax''' , push_to_hub=_snake_case , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""" ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_snake_case ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _snake_case , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_snake_case , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) def __lowerCAmelCase ( a__ , a__ ) -> str: __a = True __a = flatten_dict(modela.params ) __a = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1e-4: __a = False return models_are_equal @require_flax class __A( unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_snake_case ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_snake_case , _snake_case ) ) with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertTrue(check_models_equal(_snake_case , _snake_case ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_snake_case ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_snake_case , _snake_case ) , max_shard_size='''10KB''' ) with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertTrue(check_models_equal(_snake_case , _snake_case ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertIsNotNone(_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertIsNotNone(_snake_case )
6
1
import argparse import torch from ...utils import logging from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert logging.set_verbosity_info() def __lowerCAmelCase ( a__ , a__ , a__ ) -> Optional[int]: # Initialise PyTorch model __a = AlbertConfig.from_json_file(a__ ) print(F"""Building PyTorch model from configuration: {config}""" ) __a = AlbertForPreTraining(a__ ) # Load weights from tf checkpoint load_tf_weights_in_albert(a__ , a__ , a__ ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() , a__ ) if __name__ == "__main__": A : Union[str, 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( '--albert_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained ALBERT 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[str] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)
6
# XXX: we want transformers master here - in the absense of conftest manipulating sys.path: # hack it in for now: import sys from pathlib import Path A : Optional[Any] = Path(__file__).resolve().parents[3] / 'src' sys.path.insert(1, str(git_repo_path)) import dataclasses # noqa import io # noqa import itertools # noqa import json # noqa import os # noqa import unittest # noqa from copy import deepcopy # noqa from parameterized import parameterized # noqa from transformers import TrainingArguments, is_torch_available # noqa from transformers.deepspeed import is_deepspeed_available # noqa from transformers.file_utils import WEIGHTS_NAME # noqa from transformers.testing_utils import ( # noqa CaptureLogger, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, mockenv_context, require_deepspeed, require_torch_gpu, require_torch_multi_gpu, slow, ) from transformers.trainer_utils import set_seed # noqa set_seed(4_2) A : List[str] = {'base': 'patrickvonplaten/wav2vec2_tiny_random', 'robust': 'patrickvonplaten/wav2vec2_tiny_random_robust'} A : Optional[int] = 'zero2' A : str = 'zero3' A : Tuple = [ZEROa, ZEROa] def __lowerCAmelCase ( a__ , a__ , a__ ) -> Tuple: # customize the test name generator function as we want both params to appear in the sub-test # name, as by default it shows only the first param __a = parameterized.to_safe_name('''_'''.join(str(a__ ) for x in param.args ) ) return F"""{func.__name__}_{param_based_name}""" # Cartesian-product of zero stages with models to test A : Union[str, Any] = list(itertools.product(stages, models.keys())) @slow @require_deepspeed @require_torch_gpu class __A( a ): @parameterized.expand(_snake_case , name_func=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Any: '''simple docstring''' self.run_and_check( stage=_snake_case , model=_snake_case , distributed=_snake_case , fpaa=_snake_case , ) @require_torch_multi_gpu @parameterized.expand(_snake_case , name_func=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> int: '''simple docstring''' self.run_and_check( stage=_snake_case , model=_snake_case , distributed=_snake_case , fpaa=_snake_case , ) @parameterized.expand(_snake_case , name_func=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> str: '''simple docstring''' self.run_and_check( stage=_snake_case , model=_snake_case , distributed=_snake_case , fpaa=_snake_case , ) @require_torch_multi_gpu @parameterized.expand(_snake_case , name_func=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[Any]: '''simple docstring''' self.run_and_check( stage=_snake_case , model=_snake_case , distributed=_snake_case , fpaa=_snake_case , ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Tuple: '''simple docstring''' pass def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = 10 , _snake_case = True , _snake_case = True , _snake_case = True , ) -> Any: '''simple docstring''' __a = models[model] __a = self.run_trainer( stage=_snake_case , model_name=_snake_case , eval_steps=_snake_case , num_train_epochs=1 , distributed=_snake_case , fpaa=_snake_case , ) self.do_checks(_snake_case ) return output_dir def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = 10 , _snake_case = 1 , _snake_case = True , _snake_case = True , ) -> Union[str, Any]: '''simple docstring''' __a = self.get_auto_remove_tmp_dir('''./xxx''' , after=_snake_case ) __a = F""" --model_name_or_path {model_name} --dataset_name hf-internal-testing/librispeech_asr_dummy --dataset_config_name clean --train_split_name validation --validation_split_name validation --output_dir {output_dir} --num_train_epochs {str(_snake_case )} --per_device_train_batch_size 2 --per_device_eval_batch_size 2 --evaluation_strategy steps --learning_rate 5e-4 --warmup_steps 8 --orthography timit --preprocessing_num_workers 1 --group_by_length --freeze_feature_extractor --report_to none --save_steps 0 --eval_steps {eval_steps} --report_to none """.split() if fpaa: args.extend(['''--fp16'''] ) # currently ds_config_wav2vec2_zero.json requires "zero_optimization.find_unused_parameters": true, # hence the separate config files __a = F"""--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json""".split() __a = [F"""{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py"""] __a = self.get_launcher(_snake_case ) __a = launcher + script + args + ds_args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(_snake_case , env=self.get_env() ) return output_dir def SCREAMING_SNAKE_CASE_ ( self , _snake_case=False ) -> List[str]: '''simple docstring''' __a = min(2 , get_gpu_count() ) if distributed else 1 return F"""deepspeed --num_nodes 1 --num_gpus {num_gpus}""".split()
6
1
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version A : List[Any] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') require_version('datasets>=1.8.0', 'To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt') @dataclass class __A: snake_case_ = field( default='''cifar10''' , metadata={'''help''': '''Name of a dataset from the datasets package'''} ) snake_case_ = field( default=a , metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} ) snake_case_ = field( default=a , metadata={'''help''': '''The column name of the images in the files.'''} ) snake_case_ = field(default=a , metadata={'''help''': '''A folder containing the training data.'''} ) snake_case_ = field(default=a , metadata={'''help''': '''A folder containing the validation data.'''} ) snake_case_ = field( default=0.15 , metadata={'''help''': '''Percent to split off of train for validation.'''} ) snake_case_ = field( default=a , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of training examples to this ''' '''value if set.''' ) } , ) snake_case_ = field( default=a , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of evaluation examples to this ''' '''value if set.''' ) } , ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' __a = {} if self.train_dir is not None: __a = self.train_dir if self.validation_dir is not None: __a = self.validation_dir __a = data_files if data_files else None @dataclass class __A: snake_case_ = field( default=a , metadata={ '''help''': ( '''The model checkpoint for weights initialization.Don\'t set if you want to train a model from scratch.''' ) } , ) snake_case_ = field( default=a , metadata={'''help''': '''Pretrained config name or path if not the same as model_name_or_path'''} ) snake_case_ = field( default=a , metadata={ '''help''': ( '''Override some existing default config settings when a model is trained from scratch. Example: ''' '''n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index''' ) } , ) snake_case_ = field( default=a , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from s3'''} ) snake_case_ = field( default='''main''' , metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''} , ) snake_case_ = field(default=a , metadata={'''help''': '''Name or path of preprocessor config.'''} ) snake_case_ = field( default=a , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) snake_case_ = field( default=0.75 , metadata={'''help''': '''The ratio of the number of masked tokens in the input sequence.'''} ) snake_case_ = field( default=a , metadata={'''help''': '''Whether or not to train with normalized pixel values as target.'''} ) @dataclass class __A( a ): snake_case_ = field( default=1E-3 , metadata={'''help''': '''Base learning rate: absolute_lr = base_lr * total_batch_size / 256.'''} ) def __lowerCAmelCase ( a__ ) -> Tuple: __a = torch.stack([example['''pixel_values'''] for example in examples] ) return {"pixel_values": pixel_values} def __lowerCAmelCase ( ) -> int: # 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. __a = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __a , __a , __a = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __a , __a , __a = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_mae''' , a__ , a__ ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() __a = training_args.get_process_log_level() logger.setLevel(a__ ) transformers.utils.logging.set_verbosity(a__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. __a = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __a = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Initialize our dataset. __a = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. __a = None if '''validation''' in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , a__ ) and data_args.train_val_split > 0.0: __a = ds['''train'''].train_test_split(data_args.train_val_split ) __a = split['''train'''] __a = split['''test'''] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __a = { '''cache_dir''': model_args.cache_dir, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.config_name: __a = ViTMAEConfig.from_pretrained(model_args.config_name , **a__ ) elif model_args.model_name_or_path: __a = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **a__ ) else: __a = ViTMAEConfig() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.config_overrides is not None: logger.info(F"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(F"""New config: {config}""" ) # adapt config config.update( { '''mask_ratio''': model_args.mask_ratio, '''norm_pix_loss''': model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: __a = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **a__ ) elif model_args.model_name_or_path: __a = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **a__ ) else: __a = ViTImageProcessor() # create model if model_args.model_name_or_path: __a = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''' ) __a = ViTMAEForPreTraining(a__ ) if training_args.do_train: __a = ds['''train'''].column_names else: __a = ds['''validation'''].column_names if data_args.image_column_name is not None: __a = data_args.image_column_name elif "image" in column_names: __a = '''image''' elif "img" in column_names: __a = '''img''' else: __a = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: __a = image_processor.size['''shortest_edge'''] else: __a = (image_processor.size['''height'''], image_processor.size['''width''']) __a = Compose( [ Lambda(lambda a__ : img.convert('''RGB''' ) if img.mode != "RGB" else img ), RandomResizedCrop(a__ , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(a__ ): __a = [transforms(a__ ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: __a = ds['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(a__ ) if training_args.do_eval: if "validation" not in ds: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: __a = ( ds['''validation'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(a__ ) # Compute absolute learning rate __a = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: __a = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer __a = Trainer( model=a__ , args=a__ , train_dataset=ds['''train'''] if training_args.do_train else None , eval_dataset=ds['''validation'''] if training_args.do_eval else None , tokenizer=a__ , data_collator=a__ , ) # Training if training_args.do_train: __a = None if training_args.resume_from_checkpoint is not None: __a = training_args.resume_from_checkpoint elif last_checkpoint is not None: __a = last_checkpoint __a = trainer.train(resume_from_checkpoint=a__ ) trainer.save_model() trainer.log_metrics('''train''' , train_result.metrics ) trainer.save_metrics('''train''' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: __a = trainer.evaluate() trainer.log_metrics('''eval''' , a__ ) trainer.save_metrics('''eval''' , a__ ) # Write model card and (optionally) push to hub __a = { '''tasks''': '''masked-auto-encoding''', '''dataset''': data_args.dataset_name, '''tags''': ['''masked-auto-encoding'''], } if training_args.push_to_hub: trainer.push_to_hub(**a__ ) else: trainer.create_model_card(**a__ ) def __lowerCAmelCase ( a__ ) -> Optional[int]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
6
import gc import unittest import torch from parameterized import parameterized from diffusers import AutoencoderKL from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class __A( a , a , unittest.TestCase ): snake_case_ = AutoencoderKL snake_case_ = '''sample''' snake_case_ = 1E-2 @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = 4 __a = 3 __a = (32, 32) __a = floats_tensor((batch_size, num_channels) + sizes ).to(_snake_case ) return {"sample": image} @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' return (3, 32, 32) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' return (3, 32, 32) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a = { '''block_out_channels''': [32, 64], '''in_channels''': 3, '''out_channels''': 3, '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], '''latent_channels''': 4, } __a = self.dummy_input return init_dict, inputs_dict def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' pass def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skipIf(torch_device == '''mps''' , '''Gradient checkpointing skipped on MPS''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' __a , __a = self.prepare_init_args_and_inputs_for_common() __a = self.model_class(**_snake_case ) model.to(_snake_case ) assert not model.is_gradient_checkpointing and model.training __a = model(**_snake_case ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model.zero_grad() __a = torch.randn_like(_snake_case ) __a = (out - labels).mean() loss.backward() # re-instantiate the model now enabling gradient checkpointing __a = self.model_class(**_snake_case ) # clone model model_a.load_state_dict(model.state_dict() ) model_a.to(_snake_case ) model_a.enable_gradient_checkpointing() assert model_a.is_gradient_checkpointing and model_a.training __a = model_a(**_snake_case ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model_a.zero_grad() __a = (out_a - labels).mean() loss_a.backward() # compare the output and parameters gradients self.assertTrue((loss - loss_a).abs() < 1E-5 ) __a = dict(model.named_parameters() ) __a = dict(model_a.named_parameters() ) for name, param in named_params.items(): self.assertTrue(torch_all_close(param.grad.data , named_params_a[name].grad.data , atol=5E-5 ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a , __a = AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy''' , output_loading_info=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(_snake_case ) __a = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' __a = AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy''' ) __a = model.to(_snake_case ) model.eval() if torch_device == "mps": __a = torch.manual_seed(0 ) else: __a = torch.Generator(device=_snake_case ).manual_seed(0 ) __a = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) __a = image.to(_snake_case ) with torch.no_grad(): __a = model(_snake_case , sample_posterior=_snake_case , generator=_snake_case ).sample __a = output[0, -1, -3:, -3:].flatten().cpu() # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. if torch_device == "mps": __a = torch.tensor( [ -4.0_078E-01, -3.8_323E-04, -1.2_681E-01, -1.1_462E-01, 2.0_095E-01, 1.0_893E-01, -8.8_247E-02, -3.0_361E-01, -9.8_644E-03, ] ) elif torch_device == "cpu": __a = torch.tensor( [-0.1352, 0.0878, 0.0419, -0.0818, -0.1069, 0.0688, -0.1458, -0.4446, -0.0026] ) else: __a = torch.tensor( [-0.2421, 0.4642, 0.2507, -0.0438, 0.0682, 0.3160, -0.2018, -0.0727, 0.2485] ) self.assertTrue(torch_all_close(_snake_case , _snake_case , rtol=1E-2 ) ) @slow class __A( unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[Any]: '''simple docstring''' return F"""gaussian_noise_s={seed}_shape={'_'.join([str(_snake_case ) for s in shape] )}.npy""" def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self , _snake_case=0 , _snake_case=(4, 3, 512, 512) , _snake_case=False ) -> Any: '''simple docstring''' __a = torch.floataa if fpaa else torch.floataa __a = torch.from_numpy(load_hf_numpy(self.get_file_format(_snake_case , _snake_case ) ) ).to(_snake_case ).to(_snake_case ) return image def SCREAMING_SNAKE_CASE_ ( self , _snake_case="CompVis/stable-diffusion-v1-4" , _snake_case=False ) -> Optional[Any]: '''simple docstring''' __a = '''fp16''' if fpaa else None __a = torch.floataa if fpaa else torch.floataa __a = AutoencoderKL.from_pretrained( _snake_case , subfolder='''vae''' , torch_dtype=_snake_case , revision=_snake_case , ) model.to(_snake_case ).eval() return model def SCREAMING_SNAKE_CASE_ ( self , _snake_case=0 ) -> Tuple: '''simple docstring''' if torch_device == "mps": return torch.manual_seed(_snake_case ) return torch.Generator(device=_snake_case ).manual_seed(_snake_case ) @parameterized.expand( [ # fmt: off [33, [-0.1603, 0.9878, -0.0495, -0.0790, -0.2709, 0.8375, -0.2060, -0.0824], [-0.2395, 0.0098, 0.0102, -0.0709, -0.2840, -0.0274, -0.0718, -0.1824]], [47, [-0.2376, 0.1168, 0.1332, -0.4840, -0.2508, -0.0791, -0.0493, -0.4089], [0.0350, 0.0847, 0.0467, 0.0344, -0.0842, -0.0547, -0.0633, -0.1131]], # fmt: on ] ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case ) -> List[Any]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case ) __a = self.get_generator(_snake_case ) with torch.no_grad(): __a = model(_snake_case , generator=_snake_case , sample_posterior=_snake_case ).sample assert sample.shape == image.shape __a = sample[-1, -2:, -2:, :2].flatten().float().cpu() __a = torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice ) assert torch_all_close(_snake_case , _snake_case , atol=3E-3 ) @parameterized.expand( [ # fmt: off [33, [-0.0513, 0.0289, 1.3799, 0.2166, -0.2573, -0.0871, 0.5103, -0.0999]], [47, [-0.4128, -0.1320, -0.3704, 0.1965, -0.4116, -0.2332, -0.3340, 0.2247]], # fmt: on ] ) @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Tuple: '''simple docstring''' __a = self.get_sd_vae_model(fpaa=_snake_case ) __a = self.get_sd_image(_snake_case , fpaa=_snake_case ) __a = self.get_generator(_snake_case ) with torch.no_grad(): __a = model(_snake_case , generator=_snake_case , sample_posterior=_snake_case ).sample assert sample.shape == image.shape __a = sample[-1, -2:, :2, -2:].flatten().float().cpu() __a = torch.tensor(_snake_case ) assert torch_all_close(_snake_case , _snake_case , atol=1E-2 ) @parameterized.expand( [ # fmt: off [33, [-0.1609, 0.9866, -0.0487, -0.0777, -0.2716, 0.8368, -0.2055, -0.0814], [-0.2395, 0.0098, 0.0102, -0.0709, -0.2840, -0.0274, -0.0718, -0.1824]], [47, [-0.2377, 0.1147, 0.1333, -0.4841, -0.2506, -0.0805, -0.0491, -0.4085], [0.0350, 0.0847, 0.0467, 0.0344, -0.0842, -0.0547, -0.0633, -0.1131]], # fmt: on ] ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case ) with torch.no_grad(): __a = model(_snake_case ).sample assert sample.shape == image.shape __a = sample[-1, -2:, -2:, :2].flatten().float().cpu() __a = torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice ) assert torch_all_close(_snake_case , _snake_case , atol=3E-3 ) @parameterized.expand( [ # fmt: off [13, [-0.2051, -0.1803, -0.2311, -0.2114, -0.3292, -0.3574, -0.2953, -0.3323]], [37, [-0.2632, -0.2625, -0.2199, -0.2741, -0.4539, -0.4990, -0.3720, -0.4925]], # fmt: on ] ) @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case , shape=(3, 4, 64, 64) ) with torch.no_grad(): __a = model.decode(_snake_case ).sample assert list(sample.shape ) == [3, 3, 512, 512] __a = sample[-1, -2:, :2, -2:].flatten().cpu() __a = torch.tensor(_snake_case ) assert torch_all_close(_snake_case , _snake_case , atol=1E-3 ) @parameterized.expand( [ # fmt: off [27, [-0.0369, 0.0207, -0.0776, -0.0682, -0.1747, -0.1930, -0.1465, -0.2039]], [16, [-0.1628, -0.2134, -0.2747, -0.2642, -0.3774, -0.4404, -0.3687, -0.4277]], # fmt: on ] ) @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[Any]: '''simple docstring''' __a = self.get_sd_vae_model(fpaa=_snake_case ) __a = self.get_sd_image(_snake_case , shape=(3, 4, 64, 64) , fpaa=_snake_case ) with torch.no_grad(): __a = model.decode(_snake_case ).sample assert list(sample.shape ) == [3, 3, 512, 512] __a = sample[-1, -2:, :2, -2:].flatten().float().cpu() __a = torch.tensor(_snake_case ) assert torch_all_close(_snake_case , _snake_case , atol=5E-3 ) @parameterized.expand([(13,), (16,), (27,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason='''xformers is not required when using PyTorch 2.0.''' ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Union[str, Any]: '''simple docstring''' __a = self.get_sd_vae_model(fpaa=_snake_case ) __a = self.get_sd_image(_snake_case , shape=(3, 4, 64, 64) , fpaa=_snake_case ) with torch.no_grad(): __a = model.decode(_snake_case ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): __a = model.decode(_snake_case ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(_snake_case , _snake_case , atol=1E-1 ) @parameterized.expand([(13,), (16,), (37,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason='''xformers is not required when using PyTorch 2.0.''' ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> List[str]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case , shape=(3, 4, 64, 64) ) with torch.no_grad(): __a = model.decode(_snake_case ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): __a = model.decode(_snake_case ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(_snake_case , _snake_case , atol=1E-2 ) @parameterized.expand( [ # fmt: off [33, [-0.3001, 0.0918, -2.6984, -3.9720, -3.2099, -5.0353, 1.7338, -0.2065, 3.4267]], [47, [-1.5030, -4.3871, -6.0355, -9.1157, -1.6661, -2.7853, 2.1607, -5.0823, 2.5633]], # fmt: on ] ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case ) __a = self.get_generator(_snake_case ) with torch.no_grad(): __a = model.encode(_snake_case ).latent_dist __a = dist.sample(generator=_snake_case ) assert list(sample.shape ) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]] __a = sample[0, -1, -3:, -3:].flatten().cpu() __a = torch.tensor(_snake_case ) __a = 3E-3 if torch_device != '''mps''' else 1E-2 assert torch_all_close(_snake_case , _snake_case , atol=_snake_case )
6
1
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 __A: def __init__( self , _snake_case , _snake_case=12 , _snake_case=7 , _snake_case=True , _snake_case=True , _snake_case=True , _snake_case=99 , _snake_case=32 , _snake_case=32 , _snake_case=2 , _snake_case=4 , _snake_case=37 , _snake_case=0.1 , _snake_case=0.1 , _snake_case=512 , _snake_case=0.02 , _snake_case=0 , _snake_case=None , ) -> Dict: '''simple docstring''' __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_input_mask __a = use_labels __a = vocab_size __a = hidden_size __a = projection_dim __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = dropout __a = attention_dropout __a = max_position_embeddings __a = initializer_range __a = scope __a = bos_token_id def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: __a = input_mask.numpy() __a , __a = input_mask.shape __a = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_snake_case ): __a = 1 __a = 0 __a = self.get_config() return config, input_ids, tf.convert_to_tensor(_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' 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 SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case ) -> Optional[Any]: '''simple docstring''' __a = TFBlipTextModel(config=_snake_case ) __a = model(_snake_case , attention_mask=_snake_case , training=_snake_case ) __a = model(_snake_case , training=_snake_case ) 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 SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a = self.prepare_config_and_inputs() __a , __a , __a = config_and_inputs __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __A( a , unittest.TestCase ): snake_case_ = (TFBlipTextModel,) if is_tf_available() else () snake_case_ = False snake_case_ = False snake_case_ = False def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = BlipTextModelTester(self ) __a = ConfigTester(self , config_class=_snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' pass def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' pass @slow def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = TFBlipTextModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case=True ) -> Optional[Any]: '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=_snake_case )
6
import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup A : str = logging.get_logger(__name__) class __A( a ): def __init__( self , **_snake_case ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''bs4'''] ) super().__init__(**_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> int: '''simple docstring''' __a = [] __a = [] __a = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag __a = parent.find_all(child.name , recursive=_snake_case ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(_snake_case ) else next(i for i, s in enumerate(_snake_case , 1 ) if s is child ) ) __a = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Optional[int]: '''simple docstring''' __a = BeautifulSoup(_snake_case , '''html.parser''' ) __a = [] __a = [] __a = [] for element in html_code.descendants: if type(_snake_case ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue __a = html.unescape(_snake_case ).strip() if not text_in_this_tag: continue all_doc_strings.append(_snake_case ) __a , __a = self.xpath_soup(_snake_case ) stringaxtag_seq.append(_snake_case ) stringaxsubs_seq.append(_snake_case ) if len(_snake_case ) != len(_snake_case ): raise ValueError('''Number of doc strings and xtags does not correspond''' ) if len(_snake_case ) != len(_snake_case ): raise ValueError('''Number of doc strings and xsubs does not correspond''' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = '''''' for tagname, subs in zip(_snake_case , _snake_case ): xpath += F"""/{tagname}""" if subs != 0: xpath += F"""[{subs}]""" return xpath def __call__( self , _snake_case ) -> BatchFeature: '''simple docstring''' __a = False # Check that strings has a valid type if isinstance(_snake_case , _snake_case ): __a = True elif isinstance(_snake_case , (list, tuple) ): if len(_snake_case ) == 0 or isinstance(html_strings[0] , _snake_case ): __a = True if not valid_strings: raise ValueError( '''HTML strings must of type `str`, `List[str]` (batch of examples), ''' F"""but is of type {type(_snake_case )}.""" ) __a = bool(isinstance(_snake_case , (list, tuple) ) and (isinstance(html_strings[0] , _snake_case )) ) if not is_batched: __a = [html_strings] # Get nodes + xpaths __a = [] __a = [] for html_string in html_strings: __a , __a , __a = self.get_three_from_single(_snake_case ) nodes.append(_snake_case ) __a = [] for node, tag_list, sub_list in zip(_snake_case , _snake_case , _snake_case ): __a = self.construct_xpath(_snake_case , _snake_case ) xpath_strings.append(_snake_case ) xpaths.append(_snake_case ) # return as Dict __a = {'''nodes''': nodes, '''xpaths''': xpaths} __a = BatchFeature(data=_snake_case , tensor_type=_snake_case ) return encoded_inputs
6
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) A : int = { 'configuration_vision_encoder_decoder': ['VisionEncoderDecoderConfig', 'VisionEncoderDecoderOnnxConfig'] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = ['VisionEncoderDecoderModel'] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[str] = ['TFVisionEncoderDecoderModel'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[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 : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
def __lowerCAmelCase ( a__ , a__ ) -> float: def get_matched_characters(a__ , a__ ) -> str: __a = [] __a = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): __a = int(max(0 , i - limit ) ) __a = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(a__ ) __a = F"""{_stra[0:_stra.index(a__ )]} {_stra[_stra.index(a__ ) + 1:]}""" return "".join(a__ ) # matching characters __a = get_matched_characters(a__ , a__ ) __a = get_matched_characters(a__ , a__ ) __a = len(a__ ) # transposition __a = ( len([(ca, ca) for ca, ca in zip(a__ , a__ ) if ca != ca] ) // 2 ) if not match_count: __a = 0.0 else: __a = ( 1 / 3 * ( match_count / len(a__ ) + match_count / len(a__ ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters __a = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
6
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A : Tuple = { 'configuration_graphormer': ['GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GraphormerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ 'GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'GraphormerForGraphClassification', 'GraphormerModel', 'GraphormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_graphormer import GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, GraphormerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_graphormer import ( GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST, GraphormerForGraphClassification, GraphormerModel, GraphormerPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
def __lowerCAmelCase ( a__ ) -> str: __a = [] __a = set({'''(''', '''[''', '''{'''} ) __a = set({''')''', ''']''', '''}'''} ) __a = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(a__ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(a__ ) == 0 or (len(a__ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(a__ ) == 0 def __lowerCAmelCase ( ) -> Dict: __a = input('''Enter sequence of brackets: ''' ) if is_balanced(a__ ): print(a__ , '''is balanced''' ) else: print(a__ , '''is not balanced''' ) if __name__ == "__main__": main()
6
1
import unittest from knapsack import greedy_knapsack as kp class __A( unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' __a = [10, 20, 30, 40, 50, 60] __a = [2, 4, 6, 8, 10, 12] __a = 100 self.assertEqual(kp.calc_profit(_snake_case , _snake_case , _snake_case ) , 210 ) def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' self.assertRaisesRegex(_snake_case , '''max_weight must greater than zero.''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' self.assertRaisesRegex(_snake_case , '''Weight can not be negative.''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' self.assertRaisesRegex(_snake_case , '''Profit can not be negative.''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' self.assertRaisesRegex(_snake_case , '''max_weight must greater than zero.''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' self.assertRaisesRegex( _snake_case , '''The length of profit and weight must be same.''' ) if __name__ == "__main__": unittest.main()
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : str = { 'configuration_blenderbot': [ 'BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BlenderbotConfig', 'BlenderbotOnnxConfig', ], 'tokenization_blenderbot': ['BlenderbotTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ['BlenderbotTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ 'BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlenderbotForCausalLM', 'BlenderbotForConditionalGeneration', 'BlenderbotModel', 'BlenderbotPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'TFBlenderbotForConditionalGeneration', 'TFBlenderbotModel', 'TFBlenderbotPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'FlaxBlenderbotForConditionalGeneration', 'FlaxBlenderbotModel', 'FlaxBlenderbotPreTrainedModel', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys A : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
import math from typing import Callable, List, Optional, Union import numpy as np import PIL import torch from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline from diffusers.schedulers import DDIMScheduler, DDPMScheduler, LMSDiscreteScheduler, PNDMScheduler def __lowerCAmelCase ( a__ , a__ , a__=[] ) -> Any: __a = size[0] - overlap_pixels * 2 __a = size[1] - overlap_pixels * 2 for letter in ["l", "r"]: if letter in remove_borders: size_x += overlap_pixels for letter in ["t", "b"]: if letter in remove_borders: size_y += overlap_pixels __a = np.ones((size_y, size_x) , dtype=np.uinta ) * 255 __a = np.pad(a__ , mode='''linear_ramp''' , pad_width=a__ , end_values=0 ) if "l" in remove_borders: __a = mask[:, overlap_pixels : mask.shape[1]] if "r" in remove_borders: __a = mask[:, 0 : mask.shape[1] - overlap_pixels] if "t" in remove_borders: __a = mask[overlap_pixels : mask.shape[0], :] if "b" in remove_borders: __a = mask[0 : mask.shape[0] - overlap_pixels, :] return mask def __lowerCAmelCase ( a__ , a__ , a__ ) -> str: return max(a__ , min(a__ , a__ ) ) def __lowerCAmelCase ( a__ , a__ , a__ ) -> int: return ( clamp(rect[0] , min[0] , max[0] ), clamp(rect[1] , min[1] , max[1] ), clamp(rect[2] , min[0] , max[0] ), clamp(rect[3] , min[1] , max[1] ), ) def __lowerCAmelCase ( a__ , a__ , a__ ) -> str: __a = list(a__ ) rect[0] -= overlap rect[1] -= overlap rect[2] += overlap rect[3] += overlap __a = clamp_rect(a__ , [0, 0] , [image_size[0], image_size[1]] ) return rect def __lowerCAmelCase ( a__ , a__ , a__ , a__ ) -> List[str]: __a = Image.new('''RGB''' , (tile.size[0] + original_slice, tile.size[1]) ) result.paste( original_image.resize((tile.size[0], tile.size[1]) , Image.BICUBIC ).crop( (slice_x, 0, slice_x + original_slice, tile.size[1]) ) , (0, 0) , ) result.paste(a__ , (original_slice, 0) ) return result def __lowerCAmelCase ( a__ , a__ ) -> List[str]: __a = (original_image_slice * 4, 0, tile.size[0], tile.size[1]) __a = tile.crop(a__ ) return tile def __lowerCAmelCase ( a__ , a__ ) -> Any: __a = n % d return n - divisor class __A( a ): def __init__( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case = 350 , ) -> int: '''simple docstring''' super().__init__( vae=_snake_case , text_encoder=_snake_case , tokenizer=_snake_case , unet=_snake_case , low_res_scheduler=_snake_case , scheduler=_snake_case , max_noise_level=_snake_case , ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case ) -> Tuple: '''simple docstring''' torch.manual_seed(0 ) __a = ( min(image.size[0] - (tile_size + original_image_slice) , x * tile_size ), min(image.size[1] - (tile_size + original_image_slice) , y * tile_size ), min(image.size[0] , (x + 1) * tile_size ), min(image.size[1] , (y + 1) * tile_size ), ) __a = add_overlap_rect(_snake_case , _snake_case , image.size ) __a = image.crop(_snake_case ) __a = ((crop_rect[0] + ((crop_rect[2] - crop_rect[0]) / 2)) / image.size[0]) * tile.size[0] __a = translated_slice_x - (original_image_slice / 2) __a = max(0 , _snake_case ) __a = squeeze_tile(_snake_case , _snake_case , _snake_case , _snake_case ) __a = to_input.size __a = to_input.resize((tile_size, tile_size) , Image.BICUBIC ) __a = super(_snake_case , self ).__call__(image=_snake_case , **_snake_case ).images[0] __a = upscaled_tile.resize((orig_input_size[0] * 4, orig_input_size[1] * 4) , Image.BICUBIC ) __a = unsqueeze_tile(_snake_case , _snake_case ) __a = upscaled_tile.resize((tile.size[0] * 4, tile.size[1] * 4) , Image.BICUBIC ) __a = [] if x == 0: remove_borders.append('''l''' ) elif crop_rect[2] == image.size[0]: remove_borders.append('''r''' ) if y == 0: remove_borders.append('''t''' ) elif crop_rect[3] == image.size[1]: remove_borders.append('''b''' ) __a = Image.fromarray( make_transparency_mask( (upscaled_tile.size[0], upscaled_tile.size[1]) , tile_border * 4 , remove_borders=_snake_case ) , mode='''L''' , ) final_image.paste( _snake_case , (crop_rect_with_overlap[0] * 4, crop_rect_with_overlap[1] * 4) , _snake_case ) @torch.no_grad() def __call__( self , _snake_case , _snake_case , _snake_case = 75 , _snake_case = 9.0 , _snake_case = 50 , _snake_case = None , _snake_case = 1 , _snake_case = 0.0 , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = 1 , _snake_case = 128 , _snake_case = 32 , _snake_case = 32 , ) -> Optional[int]: '''simple docstring''' __a = Image.new('''RGB''' , (image.size[0] * 4, image.size[1] * 4) ) __a = math.ceil(image.size[0] / tile_size ) __a = math.ceil(image.size[1] / tile_size ) __a = tcx * tcy __a = 0 for y in range(_snake_case ): for x in range(_snake_case ): self._process_tile( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , prompt=_snake_case , num_inference_steps=_snake_case , guidance_scale=_snake_case , noise_level=_snake_case , negative_prompt=_snake_case , num_images_per_prompt=_snake_case , eta=_snake_case , generator=_snake_case , latents=_snake_case , ) current_count += 1 if callback is not None: callback({'''progress''': current_count / total_tile_count, '''image''': final_image} ) return final_image def __lowerCAmelCase ( ) -> Optional[Any]: # Run a demo __a = '''stabilityai/stable-diffusion-x4-upscaler''' __a = StableDiffusionTiledUpscalePipeline.from_pretrained(a__ , revision='''fp16''' , torch_dtype=torch.floataa ) __a = pipe.to('''cuda''' ) __a = Image.open('''../../docs/source/imgs/diffusers_library.jpg''' ) def callback(a__ ): print(F"""progress: {obj['progress']:.4f}""" ) obj["image"].save('''diffusers_library_progress.jpg''' ) __a = pipe(image=a__ , prompt='''Black font, white background, vector''' , noise_level=40 , callback=a__ ) final_image.save('''diffusers_library.jpg''' ) if __name__ == "__main__": main()
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : Dict = { 'configuration_xlm_roberta': [ 'XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMRobertaConfig', 'XLMRobertaOnnxConfig', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Union[str, Any] = ['XLMRobertaTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = ['XLMRobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = [ 'XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMRobertaForCausalLM', 'XLMRobertaForMaskedLM', 'XLMRobertaForMultipleChoice', 'XLMRobertaForQuestionAnswering', 'XLMRobertaForSequenceClassification', 'XLMRobertaForTokenClassification', 'XLMRobertaModel', 'XLMRobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = [ 'TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFXLMRobertaForCausalLM', 'TFXLMRobertaForMaskedLM', 'TFXLMRobertaForMultipleChoice', 'TFXLMRobertaForQuestionAnswering', 'TFXLMRobertaForSequenceClassification', 'TFXLMRobertaForTokenClassification', 'TFXLMRobertaModel', 'TFXLMRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ 'FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'FlaxXLMRobertaForMaskedLM', 'FlaxXLMRobertaForCausalLM', 'FlaxXLMRobertaForMultipleChoice', 'FlaxXLMRobertaForQuestionAnswering', 'FlaxXLMRobertaForSequenceClassification', 'FlaxXLMRobertaForTokenClassification', 'FlaxXLMRobertaModel', 'FlaxXLMRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
import argparse import os.path as osp import re import torch from safetensors.torch import load_file, save_file # =================# # UNet Conversion # # =================# A : Dict = [ # (stable-diffusion, HF Diffusers) ('time_embed.0.weight', 'time_embedding.linear_1.weight'), ('time_embed.0.bias', 'time_embedding.linear_1.bias'), ('time_embed.2.weight', 'time_embedding.linear_2.weight'), ('time_embed.2.bias', 'time_embedding.linear_2.bias'), ('input_blocks.0.0.weight', 'conv_in.weight'), ('input_blocks.0.0.bias', 'conv_in.bias'), ('out.0.weight', 'conv_norm_out.weight'), ('out.0.bias', 'conv_norm_out.bias'), ('out.2.weight', 'conv_out.weight'), ('out.2.bias', 'conv_out.bias'), ] A : List[Any] = [ # (stable-diffusion, HF Diffusers) ('in_layers.0', 'norm1'), ('in_layers.2', 'conv1'), ('out_layers.0', 'norm2'), ('out_layers.3', 'conv2'), ('emb_layers.1', 'time_emb_proj'), ('skip_connection', 'conv_shortcut'), ] A : List[Any] = [] # hardcoded number of downblocks and resnets/attentions... # would need smarter logic for other networks. for i in range(4): # loop over downblocks/upblocks for j in range(2): # loop over resnets/attentions for downblocks A : Dict = F"down_blocks.{i}.resnets.{j}." A : int = F"input_blocks.{3*i + j + 1}.0." unet_conversion_map_layer.append((sd_down_res_prefix, hf_down_res_prefix)) if i < 3: # no attention layers in down_blocks.3 A : List[Any] = F"down_blocks.{i}.attentions.{j}." A : Optional[int] = F"input_blocks.{3*i + j + 1}.1." unet_conversion_map_layer.append((sd_down_atn_prefix, hf_down_atn_prefix)) for j in range(3): # loop over resnets/attentions for upblocks A : Optional[int] = F"up_blocks.{i}.resnets.{j}." A : List[Any] = F"output_blocks.{3*i + j}.0." unet_conversion_map_layer.append((sd_up_res_prefix, hf_up_res_prefix)) if i > 0: # no attention layers in up_blocks.0 A : Optional[int] = F"up_blocks.{i}.attentions.{j}." A : Dict = F"output_blocks.{3*i + j}.1." unet_conversion_map_layer.append((sd_up_atn_prefix, hf_up_atn_prefix)) if i < 3: # no downsample in down_blocks.3 A : Dict = F"down_blocks.{i}.downsamplers.0.conv." A : Dict = F"input_blocks.{3*(i+1)}.0.op." unet_conversion_map_layer.append((sd_downsample_prefix, hf_downsample_prefix)) # no upsample in up_blocks.3 A : str = F"up_blocks.{i}.upsamplers.0." A : int = F"output_blocks.{3*i + 2}.{1 if i == 0 else 2}." unet_conversion_map_layer.append((sd_upsample_prefix, hf_upsample_prefix)) A : Tuple = 'mid_block.attentions.0.' A : List[str] = 'middle_block.1.' unet_conversion_map_layer.append((sd_mid_atn_prefix, hf_mid_atn_prefix)) for j in range(2): A : List[Any] = F"mid_block.resnets.{j}." A : List[Any] = F"middle_block.{2*j}." unet_conversion_map_layer.append((sd_mid_res_prefix, hf_mid_res_prefix)) def __lowerCAmelCase ( a__ ) -> Tuple: # buyer beware: this is a *brittle* function, # and correct output requires that all of these pieces interact in # the exact order in which I have arranged them. __a = {k: k for k in unet_state_dict.keys()} for sd_name, hf_name in unet_conversion_map: __a = sd_name for k, v in mapping.items(): if "resnets" in k: for sd_part, hf_part in unet_conversion_map_resnet: __a = v.replace(a__ , a__ ) __a = v for k, v in mapping.items(): for sd_part, hf_part in unet_conversion_map_layer: __a = v.replace(a__ , a__ ) __a = v __a = {v: unet_state_dict[k] for k, v in mapping.items()} return new_state_dict # ================# # VAE Conversion # # ================# A : str = [ # (stable-diffusion, HF Diffusers) ('nin_shortcut', 'conv_shortcut'), ('norm_out', 'conv_norm_out'), ('mid.attn_1.', 'mid_block.attentions.0.'), ] for i in range(4): # down_blocks have two resnets for j in range(2): A : Union[str, Any] = F"encoder.down_blocks.{i}.resnets.{j}." A : int = F"encoder.down.{i}.block.{j}." vae_conversion_map.append((sd_down_prefix, hf_down_prefix)) if i < 3: A : List[str] = F"down_blocks.{i}.downsamplers.0." A : List[Any] = F"down.{i}.downsample." vae_conversion_map.append((sd_downsample_prefix, hf_downsample_prefix)) A : Any = F"up_blocks.{i}.upsamplers.0." A : List[Any] = F"up.{3-i}.upsample." vae_conversion_map.append((sd_upsample_prefix, hf_upsample_prefix)) # up_blocks have three resnets # also, up blocks in hf are numbered in reverse from sd for j in range(3): A : str = F"decoder.up_blocks.{i}.resnets.{j}." A : Union[str, Any] = F"decoder.up.{3-i}.block.{j}." vae_conversion_map.append((sd_up_prefix, hf_up_prefix)) # this part accounts for mid blocks in both the encoder and the decoder for i in range(2): A : str = F"mid_block.resnets.{i}." A : Tuple = F"mid.block_{i+1}." vae_conversion_map.append((sd_mid_res_prefix, hf_mid_res_prefix)) A : Optional[Any] = [ # (stable-diffusion, HF Diffusers) ('norm.', 'group_norm.'), ('q.', 'query.'), ('k.', 'key.'), ('v.', 'value.'), ('proj_out.', 'proj_attn.'), ] def __lowerCAmelCase ( a__ ) -> Dict: # convert HF linear weights to SD conv2d weights return w.reshape(*w.shape , 1 , 1 ) def __lowerCAmelCase ( a__ ) -> List[Any]: __a = {k: k for k in vae_state_dict.keys()} for k, v in mapping.items(): for sd_part, hf_part in vae_conversion_map: __a = v.replace(a__ , a__ ) __a = v for k, v in mapping.items(): if "attentions" in k: for sd_part, hf_part in vae_conversion_map_attn: __a = v.replace(a__ , a__ ) __a = v __a = {v: vae_state_dict[k] for k, v in mapping.items()} __a = ['''q''', '''k''', '''v''', '''proj_out'''] for k, v in new_state_dict.items(): for weight_name in weights_to_convert: if F"""mid.attn_1.{weight_name}.weight""" in k: print(F"""Reshaping {k} for SD format""" ) __a = reshape_weight_for_sd(a__ ) return new_state_dict # =========================# # Text Encoder Conversion # # =========================# A : Any = [ # (stable-diffusion, HF Diffusers) ('resblocks.', 'text_model.encoder.layers.'), ('ln_1', 'layer_norm1'), ('ln_2', 'layer_norm2'), ('.c_fc.', '.fc1.'), ('.c_proj.', '.fc2.'), ('.attn', '.self_attn'), ('ln_final.', 'transformer.text_model.final_layer_norm.'), ('token_embedding.weight', 'transformer.text_model.embeddings.token_embedding.weight'), ('positional_embedding', 'transformer.text_model.embeddings.position_embedding.weight'), ] A : Optional[int] = {re.escape(x[1]): x[0] for x in textenc_conversion_lst} A : Dict = re.compile('|'.join(protected.keys())) # Ordering is from https://github.com/pytorch/pytorch/blob/master/test/cpp/api/modules.cpp A : int = {'q': 0, 'k': 1, 'v': 2} def __lowerCAmelCase ( a__ ) -> Tuple: __a = {} __a = {} __a = {} for k, v in text_enc_dict.items(): if ( k.endswith('''.self_attn.q_proj.weight''' ) or k.endswith('''.self_attn.k_proj.weight''' ) or k.endswith('''.self_attn.v_proj.weight''' ) ): __a = k[: -len('''.q_proj.weight''' )] __a = k[-len('''q_proj.weight''' )] if k_pre not in capture_qkv_weight: __a = [None, None, None] __a = v continue if ( k.endswith('''.self_attn.q_proj.bias''' ) or k.endswith('''.self_attn.k_proj.bias''' ) or k.endswith('''.self_attn.v_proj.bias''' ) ): __a = k[: -len('''.q_proj.bias''' )] __a = k[-len('''q_proj.bias''' )] if k_pre not in capture_qkv_bias: __a = [None, None, None] __a = v continue __a = textenc_pattern.sub(lambda a__ : protected[re.escape(m.group(0 ) )] , a__ ) __a = v for k_pre, tensors in capture_qkv_weight.items(): if None in tensors: raise Exception('''CORRUPTED MODEL: one of the q-k-v values for the text encoder was missing''' ) __a = textenc_pattern.sub(lambda a__ : protected[re.escape(m.group(0 ) )] , a__ ) __a = torch.cat(a__ ) for k_pre, tensors in capture_qkv_bias.items(): if None in tensors: raise Exception('''CORRUPTED MODEL: one of the q-k-v values for the text encoder was missing''' ) __a = textenc_pattern.sub(lambda a__ : protected[re.escape(m.group(0 ) )] , a__ ) __a = torch.cat(a__ ) return new_state_dict def __lowerCAmelCase ( a__ ) -> Any: return text_enc_dict if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('--model_path', default=None, type=str, required=True, help='Path to the model to convert.') parser.add_argument('--checkpoint_path', default=None, type=str, required=True, help='Path to the output model.') parser.add_argument('--half', action='store_true', help='Save weights in half precision.') parser.add_argument( '--use_safetensors', action='store_true', help='Save weights use safetensors, default is ckpt.' ) A : List[str] = parser.parse_args() assert args.model_path is not None, "Must provide a model path!" assert args.checkpoint_path is not None, "Must provide a checkpoint path!" # Path for safetensors A : int = osp.join(args.model_path, 'unet', 'diffusion_pytorch_model.safetensors') A : Union[str, Any] = osp.join(args.model_path, 'vae', 'diffusion_pytorch_model.safetensors') A : Union[str, Any] = osp.join(args.model_path, 'text_encoder', 'model.safetensors') # Load models from safetensors if it exists, if it doesn't pytorch if osp.exists(unet_path): A : Tuple = load_file(unet_path, device='cpu') else: A : List[Any] = osp.join(args.model_path, 'unet', 'diffusion_pytorch_model.bin') A : List[str] = torch.load(unet_path, map_location='cpu') if osp.exists(vae_path): A : Optional[int] = load_file(vae_path, device='cpu') else: A : Dict = osp.join(args.model_path, 'vae', 'diffusion_pytorch_model.bin') A : List[Any] = torch.load(vae_path, map_location='cpu') if osp.exists(text_enc_path): A : int = load_file(text_enc_path, device='cpu') else: A : Optional[Any] = osp.join(args.model_path, 'text_encoder', 'pytorch_model.bin') A : Optional[Any] = torch.load(text_enc_path, map_location='cpu') # Convert the UNet model A : Union[str, Any] = convert_unet_state_dict(unet_state_dict) A : Optional[Any] = {'model.diffusion_model.' + k: v for k, v in unet_state_dict.items()} # Convert the VAE model A : Any = convert_vae_state_dict(vae_state_dict) A : List[Any] = {'first_stage_model.' + k: v for k, v in vae_state_dict.items()} # Easiest way to identify v2.0 model seems to be that the text encoder (OpenCLIP) is deeper A : List[str] = 'text_model.encoder.layers.22.layer_norm2.bias' in text_enc_dict if is_vaa_model: # Need to add the tag 'transformer' in advance so we can knock it out from the final layer-norm A : Dict = {'transformer.' + k: v for k, v in text_enc_dict.items()} A : str = convert_text_enc_state_dict_vaa(text_enc_dict) A : Union[str, Any] = {'cond_stage_model.model.' + k: v for k, v in text_enc_dict.items()} else: A : int = convert_text_enc_state_dict(text_enc_dict) A : Optional[Any] = {'cond_stage_model.transformer.' + k: v for k, v in text_enc_dict.items()} # Put together new checkpoint A : List[Any] = {**unet_state_dict, **vae_state_dict, **text_enc_dict} if args.half: A : Dict = {k: v.half() for k, v in state_dict.items()} if args.use_safetensors: save_file(state_dict, args.checkpoint_path) else: A : str = {'state_dict': state_dict} torch.save(state_dict, args.checkpoint_path)
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : Optional[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 : int = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = [ '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 : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A : str = logging.get_logger(__name__) A : Dict = { 'vinvino02/glpn-kitti': 'https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json', # See all GLPN models at https://huggingface.co/models?filter=glpn } class __A( a ): snake_case_ = '''glpn''' def __init__( self , _snake_case=3 , _snake_case=4 , _snake_case=[2, 2, 2, 2] , _snake_case=[8, 4, 2, 1] , _snake_case=[32, 64, 160, 256] , _snake_case=[7, 3, 3, 3] , _snake_case=[4, 2, 2, 2] , _snake_case=[1, 2, 5, 8] , _snake_case=[4, 4, 4, 4] , _snake_case="gelu" , _snake_case=0.0 , _snake_case=0.0 , _snake_case=0.02 , _snake_case=0.1 , _snake_case=1E-6 , _snake_case=64 , _snake_case=10 , _snake_case=-1 , **_snake_case , ) -> List[Any]: '''simple docstring''' super().__init__(**_snake_case ) __a = num_channels __a = num_encoder_blocks __a = depths __a = sr_ratios __a = hidden_sizes __a = patch_sizes __a = strides __a = mlp_ratios __a = num_attention_heads __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = initializer_range __a = drop_path_rate __a = layer_norm_eps __a = decoder_hidden_size __a = max_depth __a = head_in_index
6
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( 'stable diffusion controlnet', '0.22.0', 'Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.', standard_warn=False, stacklevel=3, )
6
1
import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class __A( a ): snake_case_ = (PNDMScheduler,) snake_case_ = (('''num_inference_steps''', 5_0),) def SCREAMING_SNAKE_CASE_ ( self , **_snake_case ) -> str: '''simple docstring''' __a = { '''num_train_timesteps''': 1_000, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**_snake_case ) return config def SCREAMING_SNAKE_CASE_ ( self , _snake_case=0 , **_snake_case ) -> Any: '''simple docstring''' __a = dict(self.forward_default_kwargs ) __a = kwargs.pop('''num_inference_steps''' , _snake_case ) __a = self.dummy_sample __a = 0.1 * sample __a = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __a = self.get_scheduler_config(**_snake_case ) __a = scheduler_class(**_snake_case ) scheduler.set_timesteps(_snake_case ) # copy over dummy past residuals __a = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_snake_case ) __a = scheduler_class.from_pretrained(_snake_case ) new_scheduler.set_timesteps(_snake_case ) # copy over dummy past residuals __a = dummy_past_residuals[:] __a = scheduler.step_prk(_snake_case , _snake_case , _snake_case , **_snake_case ).prev_sample __a = new_scheduler.step_prk(_snake_case , _snake_case , _snake_case , **_snake_case ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __a = scheduler.step_plms(_snake_case , _snake_case , _snake_case , **_snake_case ).prev_sample __a = new_scheduler.step_plms(_snake_case , _snake_case , _snake_case , **_snake_case ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' pass def SCREAMING_SNAKE_CASE_ ( self , _snake_case=0 , **_snake_case ) -> Union[str, Any]: '''simple docstring''' __a = dict(self.forward_default_kwargs ) __a = kwargs.pop('''num_inference_steps''' , _snake_case ) __a = self.dummy_sample __a = 0.1 * sample __a = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: __a = self.get_scheduler_config() __a = scheduler_class(**_snake_case ) scheduler.set_timesteps(_snake_case ) # copy over dummy past residuals (must be after setting timesteps) __a = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_snake_case ) __a = scheduler_class.from_pretrained(_snake_case ) # copy over dummy past residuals new_scheduler.set_timesteps(_snake_case ) # copy over dummy past residual (must be after setting timesteps) __a = dummy_past_residuals[:] __a = scheduler.step_prk(_snake_case , _snake_case , _snake_case , **_snake_case ).prev_sample __a = new_scheduler.step_prk(_snake_case , _snake_case , _snake_case , **_snake_case ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" __a = scheduler.step_plms(_snake_case , _snake_case , _snake_case , **_snake_case ).prev_sample __a = new_scheduler.step_plms(_snake_case , _snake_case , _snake_case , **_snake_case ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self , **_snake_case ) -> Tuple: '''simple docstring''' __a = self.scheduler_classes[0] __a = self.get_scheduler_config(**_snake_case ) __a = scheduler_class(**_snake_case ) __a = 10 __a = self.dummy_model() __a = self.dummy_sample_deter scheduler.set_timesteps(_snake_case ) for i, t in enumerate(scheduler.prk_timesteps ): __a = model(_snake_case , _snake_case ) __a = scheduler.step_prk(_snake_case , _snake_case , _snake_case ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): __a = model(_snake_case , _snake_case ) __a = scheduler.step_plms(_snake_case , _snake_case , _snake_case ).prev_sample return sample def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = dict(self.forward_default_kwargs ) __a = kwargs.pop('''num_inference_steps''' , _snake_case ) for scheduler_class in self.scheduler_classes: __a = self.get_scheduler_config() __a = scheduler_class(**_snake_case ) __a = self.dummy_sample __a = 0.1 * sample if num_inference_steps is not None and hasattr(_snake_case , '''set_timesteps''' ): scheduler.set_timesteps(_snake_case ) elif num_inference_steps is not None and not hasattr(_snake_case , '''set_timesteps''' ): __a = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) __a = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] __a = dummy_past_residuals[:] __a = scheduler.step_prk(_snake_case , 0 , _snake_case , **_snake_case ).prev_sample __a = scheduler.step_prk(_snake_case , 1 , _snake_case , **_snake_case ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) __a = scheduler.step_plms(_snake_case , 0 , _snake_case , **_snake_case ).prev_sample __a = scheduler.step_plms(_snake_case , 1 , _snake_case , **_snake_case ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' for timesteps in [100, 1_000]: self.check_over_configs(num_train_timesteps=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' for steps_offset in [0, 1]: self.check_over_configs(steps_offset=_snake_case ) __a = self.scheduler_classes[0] __a = self.get_scheduler_config(steps_offset=1 ) __a = scheduler_class(**_snake_case ) scheduler.set_timesteps(10 ) assert torch.equal( scheduler.timesteps , torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1] ) , ) def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0001, 0.001] , [0.002, 0.02] ): self.check_over_configs(beta_start=_snake_case , beta_end=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' for t in [1, 5, 10]: self.check_over_forward(time_step=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' __a = 27 for scheduler_class in self.scheduler_classes: __a = self.dummy_sample __a = 0.1 * sample __a = self.get_scheduler_config() __a = scheduler_class(**_snake_case ) scheduler.set_timesteps(_snake_case ) # 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] ): __a = scheduler.step_prk(_snake_case , _snake_case , _snake_case ).prev_sample def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' with self.assertRaises(_snake_case ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_snake_case ) scheduler.step_plms(self.dummy_sample , 1 , self.dummy_sample ).prev_sample def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = self.full_loop() __a = torch.sum(torch.abs(_snake_case ) ) __a = torch.mean(torch.abs(_snake_case ) ) assert abs(result_sum.item() - 198.1318 ) < 1E-2 assert abs(result_mean.item() - 0.2580 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a = self.full_loop(prediction_type='''v_prediction''' ) __a = torch.sum(torch.abs(_snake_case ) ) __a = torch.mean(torch.abs(_snake_case ) ) assert abs(result_sum.item() - 67.3986 ) < 1E-2 assert abs(result_mean.item() - 0.0878 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = self.full_loop(set_alpha_to_one=_snake_case , beta_start=0.01 ) __a = torch.sum(torch.abs(_snake_case ) ) __a = torch.mean(torch.abs(_snake_case ) ) assert abs(result_sum.item() - 230.0399 ) < 1E-2 assert abs(result_mean.item() - 0.2995 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a = self.full_loop(set_alpha_to_one=_snake_case , beta_start=0.01 ) __a = torch.sum(torch.abs(_snake_case ) ) __a = torch.mean(torch.abs(_snake_case ) ) assert abs(result_sum.item() - 186.9482 ) < 1E-2 assert abs(result_mean.item() - 0.2434 ) < 1E-3
6
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=a ) class __A( a ): snake_case_ = field(default='''language-modeling''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) snake_case_ = Features({'''text''': Value('''string''' )} ) snake_case_ = Features({} ) snake_case_ = "text" @property def SCREAMING_SNAKE_CASE_ ( self ) -> Dict[str, str]: '''simple docstring''' return {self.text_column: "text"}
6
1
from argparse import ArgumentParser from . import BaseTransformersCLICommand def __lowerCAmelCase ( a__ ) -> Union[str, Any]: return DownloadCommand(args.model , args.cache_dir , args.force , args.trust_remote_code ) class __A( a ): @staticmethod def SCREAMING_SNAKE_CASE_ ( _snake_case ) -> int: '''simple docstring''' __a = parser.add_parser('''download''' ) download_parser.add_argument( '''--cache-dir''' , type=_snake_case , default=_snake_case , help='''Path to location to store the models''' ) download_parser.add_argument( '''--force''' , action='''store_true''' , help='''Force the model to be download even if already in cache-dir''' ) download_parser.add_argument( '''--trust-remote-code''' , action='''store_true''' , help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine''' , ) download_parser.add_argument('''model''' , type=_snake_case , help='''Name of the model to download''' ) download_parser.set_defaults(func=_snake_case ) def __init__( self , _snake_case , _snake_case , _snake_case , _snake_case ) -> Dict: '''simple docstring''' __a = model __a = cache __a = force __a = trust_remote_code def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code )
6
import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def __lowerCAmelCase ( a__ , a__ , a__=1024 , a__=1024 , a__=False , **a__ ) -> Optional[Any]: __a = AutoTokenizer.from_pretrained(a__ ) __a = SeqaSeqDataset(a__ , a__ , a__ , a__ , type_path='''train''' , **a__ ) __a = tok.pad_token_id def get_lens(a__ ): __a = tqdm( DataLoader(a__ , batch_size=512 , num_workers=8 , shuffle=a__ , collate_fn=ds.collate_fn ) , desc=str(ds.len_file ) , ) __a = [] for batch in dl: __a = batch['''input_ids'''].ne(a__ ).sum(1 ).tolist() __a = batch['''labels'''].ne(a__ ).sum(1 ).tolist() if consider_target: for src, tgt in zip(a__ , a__ ): max_lens.append(max(a__ , a__ ) ) else: max_lens.extend(a__ ) return max_lens __a = get_lens(a__ ) __a = SeqaSeqDataset(a__ , a__ , a__ , a__ , type_path='''val''' , **a__ ) __a = get_lens(a__ ) pickle_save(a__ , train_ds.len_file ) pickle_save(a__ , val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
6
1
import random class __A: @staticmethod def SCREAMING_SNAKE_CASE_ ( _snake_case ) -> tuple[list[int], list[int]]: '''simple docstring''' __a = [ord(_snake_case ) for i in text] __a = [] __a = [] for i in plain: __a = random.randint(1 , 300 ) __a = (i + k) * k cipher.append(_snake_case ) key.append(_snake_case ) return cipher, key @staticmethod def SCREAMING_SNAKE_CASE_ ( _snake_case , _snake_case ) -> str: '''simple docstring''' __a = [] for i in range(len(_snake_case ) ): __a = int((cipher[i] - (key[i]) ** 2) / key[i] ) plain.append(chr(_snake_case ) ) return "".join(_snake_case ) if __name__ == "__main__": A , A : Any = Onepad().encrypt('Hello') print(c, k) print(Onepad().decrypt(c, k))
6
from math import cos, sin, sqrt, tau from audio_filters.iir_filter import IIRFilter def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = (1 - _cos) / 2 __a = 1 - _cos __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = (1 + _cos) / 2 __a = -1 - _cos __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = _sin / 2 __a = 0 __a = -ba __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 1 - alpha __a = -2 * _cos __a = 1 + alpha __a = IIRFilter(2 ) filt.set_coefficients([ba, ba, ba] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = 1 + alpha * big_a __a = -2 * _cos __a = 1 - alpha * big_a __a = 1 + alpha / big_a __a = -2 * _cos __a = 1 - alpha / big_a __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = (big_a + 1) - (big_a - 1) * _cos __a = (big_a + 1) + (big_a - 1) * _cos __a = (big_a - 1) - (big_a + 1) * _cos __a = (big_a - 1) + (big_a + 1) * _cos __a = 2 * sqrt(a__ ) * alpha __a = big_a * (pmc + aaa) __a = 2 * big_a * mpc __a = big_a * (pmc - aaa) __a = ppmc + aaa __a = -2 * pmpc __a = ppmc - aaa __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = (big_a + 1) - (big_a - 1) * _cos __a = (big_a + 1) + (big_a - 1) * _cos __a = (big_a - 1) - (big_a + 1) * _cos __a = (big_a - 1) + (big_a + 1) * _cos __a = 2 * sqrt(a__ ) * alpha __a = big_a * (ppmc + aaa) __a = -2 * big_a * pmpc __a = big_a * (ppmc - aaa) __a = pmc + aaa __a = 2 * mpc __a = pmc - aaa __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt
6
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType from ...utils.imports import is_botoa_available from .config_args import SageMakerConfig from .config_utils import ( DYNAMO_BACKENDS, _ask_field, _ask_options, _convert_dynamo_backend, _convert_mixed_precision, _convert_sagemaker_distributed_mode, _convert_yes_no_to_bool, ) if is_botoa_available(): import botoa # noqa: F401 def __lowerCAmelCase ( a__ ) -> Dict: __a = botoa.client('''iam''' ) __a = { '''Version''': '''2012-10-17''', '''Statement''': [ {'''Effect''': '''Allow''', '''Principal''': {'''Service''': '''sagemaker.amazonaws.com'''}, '''Action''': '''sts:AssumeRole'''} ], } try: # create the role, associated with the chosen trust policy iam_client.create_role( RoleName=a__ , AssumeRolePolicyDocument=json.dumps(a__ , indent=2 ) ) __a = { '''Version''': '''2012-10-17''', '''Statement''': [ { '''Effect''': '''Allow''', '''Action''': [ '''sagemaker:*''', '''ecr:GetDownloadUrlForLayer''', '''ecr:BatchGetImage''', '''ecr:BatchCheckLayerAvailability''', '''ecr:GetAuthorizationToken''', '''cloudwatch:PutMetricData''', '''cloudwatch:GetMetricData''', '''cloudwatch:GetMetricStatistics''', '''cloudwatch:ListMetrics''', '''logs:CreateLogGroup''', '''logs:CreateLogStream''', '''logs:DescribeLogStreams''', '''logs:PutLogEvents''', '''logs:GetLogEvents''', '''s3:CreateBucket''', '''s3:ListBucket''', '''s3:GetBucketLocation''', '''s3:GetObject''', '''s3:PutObject''', ], '''Resource''': '''*''', } ], } # attach policy to role iam_client.put_role_policy( RoleName=a__ , PolicyName=F"""{role_name}_policy_permission""" , PolicyDocument=json.dumps(a__ , indent=2 ) , ) except iam_client.exceptions.EntityAlreadyExistsException: print(F"""role {role_name} already exists. Using existing one""" ) def __lowerCAmelCase ( a__ ) -> Optional[int]: __a = botoa.client('''iam''' ) return iam_client.get_role(RoleName=a__ )["Role"]["Arn"] def __lowerCAmelCase ( ) -> Tuple: __a = _ask_options( '''How do you want to authorize?''' , ['''AWS Profile''', '''Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) '''] , a__ , ) __a = None if credentials_configuration == 0: __a = _ask_field('''Enter your AWS Profile name: [default] ''' , default='''default''' ) __a = aws_profile else: print( '''Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,''' '''`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`''' ) __a = _ask_field('''AWS Access Key ID: ''' ) __a = aws_access_key_id __a = _ask_field('''AWS Secret Access Key: ''' ) __a = aws_secret_access_key __a = _ask_field('''Enter your AWS Region: [us-east-1]''' , default='''us-east-1''' ) __a = aws_region __a = _ask_options( '''Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?''' , ['''Provide IAM Role name''', '''Create new IAM role using credentials'''] , a__ , ) if role_management == 0: __a = _ask_field('''Enter your IAM role name: ''' ) else: __a = '''accelerate_sagemaker_execution_role''' print(F"""Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials""" ) _create_iam_role_for_sagemaker(a__ ) __a = _ask_field( '''Do you want to use custom Docker image? [yes/NO]: ''' , _convert_yes_no_to_bool , default=a__ , error_message='''Please enter yes or no.''' , ) __a = None if is_custom_docker_image: __a = _ask_field('''Enter your Docker image: ''' , lambda a__ : str(a__ ).lower() ) __a = _ask_field( '''Do you want to provide SageMaker input channels with data locations? [yes/NO]: ''' , _convert_yes_no_to_bool , default=a__ , error_message='''Please enter yes or no.''' , ) __a = None if is_sagemaker_inputs_enabled: __a = _ask_field( '''Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): ''' , lambda a__ : str(a__ ).lower() , ) __a = _ask_field( '''Do you want to enable SageMaker metrics? [yes/NO]: ''' , _convert_yes_no_to_bool , default=a__ , error_message='''Please enter yes or no.''' , ) __a = None if is_sagemaker_metrics_enabled: __a = _ask_field( '''Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): ''' , lambda a__ : str(a__ ).lower() , ) __a = _ask_options( '''What is the distributed mode?''' , ['''No distributed training''', '''Data parallelism'''] , _convert_sagemaker_distributed_mode , ) __a = {} __a = _ask_field( '''Do you wish to optimize your script with torch dynamo?[yes/NO]:''' , _convert_yes_no_to_bool , default=a__ , error_message='''Please enter yes or no.''' , ) if use_dynamo: __a = '''dynamo_''' __a = _ask_options( '''Which dynamo backend would you like to use?''' , [x.lower() for x in DYNAMO_BACKENDS] , _convert_dynamo_backend , default=2 , ) __a = _ask_field( '''Do you want to customize the defaults sent to torch.compile? [yes/NO]: ''' , _convert_yes_no_to_bool , default=a__ , error_message='''Please enter yes or no.''' , ) if use_custom_options: __a = _ask_options( '''Which mode do you want to use?''' , a__ , lambda a__ : TORCH_DYNAMO_MODES[int(a__ )] , default='''default''' , ) __a = _ask_field( '''Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: ''' , _convert_yes_no_to_bool , default=a__ , error_message='''Please enter yes or no.''' , ) __a = _ask_field( '''Do you want to enable dynamic shape tracing? [yes/NO]: ''' , _convert_yes_no_to_bool , default=a__ , error_message='''Please enter yes or no.''' , ) __a = '''Which EC2 instance type you want to use for your training?''' if distributed_type != SageMakerDistributedType.NO: __a = _ask_options( a__ , a__ , lambda a__ : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(a__ )] ) else: eca_instance_query += "? [ml.p3.2xlarge]:" __a = _ask_field(a__ , lambda a__ : str(a__ ).lower() , default='''ml.p3.2xlarge''' ) __a = 1 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL): __a = _ask_field( '''How many machines do you want use? [1]: ''' , a__ , default=1 , ) __a = _ask_options( '''Do you wish to use FP16 or BF16 (mixed precision)?''' , ['''no''', '''fp16''', '''bf16''', '''fp8'''] , _convert_mixed_precision , ) if use_dynamo and mixed_precision == "no": print( '''Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.''' ) return SageMakerConfig( image_uri=a__ , compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER , distributed_type=a__ , use_cpu=a__ , dynamo_config=a__ , eca_instance_type=a__ , profile=a__ , region=a__ , iam_role_name=a__ , mixed_precision=a__ , num_machines=a__ , sagemaker_inputs_file=a__ , sagemaker_metrics_file=a__ , )
6
def __lowerCAmelCase ( a__ , a__ , a__ ) -> list: __a = len(a__ ) __a = [[0] * n for i in range(a__ )] for i in range(a__ ): __a = y_points[i] for i in range(2 , a__ ): for j in range(a__ , a__ ): __a = ( (xa - x_points[j - i + 1]) * q[j][i - 1] - (xa - x_points[j]) * q[j - 1][i - 1] ) / (x_points[j] - x_points[j - i + 1]) return [q[n - 1][n - 1], q] if __name__ == "__main__": import doctest doctest.testmod()
6
1
# Note: if you intend to run this script make sure you look under scripts/fsmt/ # to locate the appropriate script to do the work correctly. There is a set of scripts to: # - download and prepare data and run the conversion script # - perform eval to get the best hparam into the config # - generate model_cards - useful if you have multiple models from the same paper import argparse import json import os import re from collections import OrderedDict from os.path import basename, dirname import fairseq import torch from fairseq import hub_utils from fairseq.data.dictionary import Dictionary from transformers import FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() A : Dict = 2 # based on the results of a search on a range of `num_beams`, `length_penalty` and `early_stopping` # values against wmt19 test data to obtain the best BLEU scores, we will use the following defaults: # # * `num_beams`: 5 (higher scores better, but requires more memory/is slower, can be adjusted by users) # * `early_stopping`: `False` consistently scored better # * `length_penalty` varied, so will assign the best one depending on the model A : str = { # fairseq: 'wmt19-ru-en': {'length_penalty': 1.1}, 'wmt19-en-ru': {'length_penalty': 1.15}, 'wmt19-en-de': {'length_penalty': 1.0}, 'wmt19-de-en': {'length_penalty': 1.1}, # allenai: 'wmt16-en-de-dist-12-1': {'length_penalty': 0.6}, 'wmt16-en-de-dist-6-1': {'length_penalty': 0.6}, 'wmt16-en-de-12-1': {'length_penalty': 0.8}, 'wmt19-de-en-6-6-base': {'length_penalty': 0.6}, 'wmt19-de-en-6-6-big': {'length_penalty': 0.6}, } # this remaps the different models to their organization names A : Union[str, Any] = {} for m in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: A : Any = 'facebook' for m in [ "wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1", "wmt19-de-en-6-6-base", "wmt19-de-en-6-6-big", ]: A : Dict = 'allenai' def __lowerCAmelCase ( a__ ) -> int: # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} __a = dict((re.sub(R'''@@$''' , '''''' , a__ ), v) if k.endswith('''@@''' ) else (re.sub(R'''$''' , '''</w>''' , a__ ), v) for k, v in d.items() ) __a = '''<s> <pad> </s> <unk>'''.split() # restore the special tokens for k in keep_keys: del da[F"""{k}</w>"""] __a = d[k] # restore return da def __lowerCAmelCase ( a__ , a__ ) -> Optional[int]: # prep assert os.path.exists(a__ ) os.makedirs(a__ , exist_ok=a__ ) print(F"""Writing results to {pytorch_dump_folder_path}""" ) # handle various types of models __a = basename(a__ ) __a = dirname(a__ ) __a = fairseq.model_parallel.models.transformer.ModelParallelTransformerModel __a = cls.hub_models() __a = {'''bpe''': '''fastbpe''', '''tokenizer''': '''moses'''} __a = '''.''' # note: since the model dump is old, fairseq has upgraded its model some # time later, and it does a whole lot of rewrites and splits on the saved # weights, therefore we can't use torch.load() directly on the model file. # see: upgrade_state_dict(state_dict) in fairseq_model.py print(F"""using checkpoint {checkpoint_file}""" ) __a = hub_utils.from_pretrained( a__ , a__ , a__ , archive_map=a__ , **a__ ) __a = vars(chkpt['''args''']['''model'''] ) __a = args['''source_lang'''] __a = args['''target_lang'''] __a = dirname(a__ ) __a = basename(a__ ) # dicts __a = os.path.join(a__ , F"""dict.{src_lang}.txt""" ) __a = os.path.join(a__ , F"""dict.{tgt_lang}.txt""" ) __a = Dictionary.load(a__ ) __a = rewrite_dict_keys(src_dict.indices ) __a = len(a__ ) __a = os.path.join(a__ , '''vocab-src.json''' ) print(F"""Generating {src_vocab_file} of {src_vocab_size} of {src_lang} records""" ) with open(a__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(a__ , ensure_ascii=a__ , indent=a__ ) ) # detect whether this is a do_lower_case situation, which can be derived by checking whether we # have at least one uppercase letter in the source vocab __a = True for k in src_vocab.keys(): if not k.islower(): __a = False break __a = Dictionary.load(a__ ) __a = rewrite_dict_keys(tgt_dict.indices ) __a = len(a__ ) __a = os.path.join(a__ , '''vocab-tgt.json''' ) print(F"""Generating {tgt_vocab_file} of {tgt_vocab_size} of {tgt_lang} records""" ) with open(a__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(a__ , ensure_ascii=a__ , indent=a__ ) ) # merges_file (bpecodes) __a = os.path.join(a__ , VOCAB_FILES_NAMES['''merges_file'''] ) for fn in ["bpecodes", "code"]: # older fairseq called the merges file "code" __a = os.path.join(a__ , a__ ) if os.path.exists(a__ ): break with open(a__ , encoding='''utf-8''' ) as fin: __a = fin.read() __a = re.sub(R''' \d+$''' , '''''' , a__ , 0 , re.M ) # remove frequency number print(F"""Generating {merges_file}""" ) with open(a__ , '''w''' , encoding='''utf-8''' ) as fout: fout.write(a__ ) # model config __a = os.path.join(a__ , '''config.json''' ) # validate bpe/tokenizer config, as currently it's hardcoded to moses+fastbpe - # may have to modify the tokenizer if a different type is used by a future model assert args["bpe"] == "fastbpe", F"""need to extend tokenizer to support bpe={args['bpe']}""" assert args["tokenizer"] == "moses", F"""need to extend tokenizer to support bpe={args['tokenizer']}""" __a = { '''architectures''': ['''FSMTForConditionalGeneration'''], '''model_type''': '''fsmt''', '''activation_dropout''': args['''activation_dropout'''], '''activation_function''': '''relu''', '''attention_dropout''': args['''attention_dropout'''], '''d_model''': args['''decoder_embed_dim'''], '''dropout''': args['''dropout'''], '''init_std''': 0.02, '''max_position_embeddings''': args['''max_source_positions'''], '''num_hidden_layers''': args['''encoder_layers'''], '''src_vocab_size''': src_vocab_size, '''tgt_vocab_size''': tgt_vocab_size, '''langs''': [src_lang, tgt_lang], '''encoder_attention_heads''': args['''encoder_attention_heads'''], '''encoder_ffn_dim''': args['''encoder_ffn_embed_dim'''], '''encoder_layerdrop''': args['''encoder_layerdrop'''], '''encoder_layers''': args['''encoder_layers'''], '''decoder_attention_heads''': args['''decoder_attention_heads'''], '''decoder_ffn_dim''': args['''decoder_ffn_embed_dim'''], '''decoder_layerdrop''': args['''decoder_layerdrop'''], '''decoder_layers''': args['''decoder_layers'''], '''bos_token_id''': 0, '''pad_token_id''': 1, '''eos_token_id''': 2, '''is_encoder_decoder''': True, '''scale_embedding''': not args['''no_scale_embedding'''], '''tie_word_embeddings''': args['''share_all_embeddings'''], } # good hparam defaults to start with __a = 5 __a = False if model_dir in best_score_hparams and "length_penalty" in best_score_hparams[model_dir]: __a = best_score_hparams[model_dir]['''length_penalty'''] else: __a = 1.0 print(F"""Generating {fsmt_model_config_file}""" ) with open(a__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(a__ , ensure_ascii=a__ , indent=a__ ) ) # tokenizer config __a = os.path.join(a__ , a__ ) __a = { '''langs''': [src_lang, tgt_lang], '''model_max_length''': 1024, '''do_lower_case''': do_lower_case, } print(F"""Generating {fsmt_tokenizer_config_file}""" ) with open(a__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(a__ , ensure_ascii=a__ , indent=a__ ) ) # model __a = chkpt['''models'''][0] __a = model.state_dict() # rename keys to start with 'model.' __a = OrderedDict(('''model.''' + k, v) for k, v in model_state_dict.items() ) # remove unneeded keys __a = [ '''model.model''', '''model.encoder.version''', '''model.decoder.version''', '''model.encoder_embed_tokens.weight''', '''model.decoder_embed_tokens.weight''', '''model.encoder.embed_positions._float_tensor''', '''model.decoder.embed_positions._float_tensor''', ] for k in ignore_keys: model_state_dict.pop(a__ , a__ ) __a = FSMTConfig.from_pretrained(a__ ) __a = FSMTForConditionalGeneration(a__ ) # check that it loads ok model_new.load_state_dict(a__ , strict=a__ ) # save __a = os.path.join(a__ , a__ ) print(F"""Generating {pytorch_weights_dump_path}""" ) torch.save(a__ , a__ ) print('''Conversion is done!''' ) print('''\nLast step is to upload the files to s3''' ) print(F"""cd {data_root}""" ) print(F"""transformers-cli upload {model_dir}""" ) if __name__ == "__main__": A : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--fsmt_checkpoint_path', default=None, type=str, required=True, help=( 'Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts,' ' bpecodes, etc.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) A : List[str] = parser.parse_args() convert_fsmt_checkpoint_to_pytorch(args.fsmt_checkpoint_path, args.pytorch_dump_folder_path)
6
from __future__ import annotations import time from collections.abc import Sequence from random import randint from matplotlib import pyplot as plt def __lowerCAmelCase ( a__ , a__ , a__ ) -> tuple[int | None, int | None, float]: if not arr: return None, None, 0 if low == high: return low, high, arr[low] __a = (low + high) // 2 __a , __a , __a = max_subarray(a__ , a__ , a__ ) __a , __a , __a = max_subarray(a__ , mid + 1 , a__ ) __a , __a , __a = max_cross_sum(a__ , a__ , a__ , a__ ) if left_sum >= right_sum and left_sum >= cross_sum: return left_low, left_high, left_sum elif right_sum >= left_sum and right_sum >= cross_sum: return right_low, right_high, right_sum return cross_left, cross_right, cross_sum def __lowerCAmelCase ( a__ , a__ , a__ , a__ ) -> tuple[int, int, float]: __a , __a = float('''-inf''' ), -1 __a , __a = float('''-inf''' ), -1 __a = 0 for i in range(a__ , low - 1 , -1 ): summ += arr[i] if summ > left_sum: __a = summ __a = i __a = 0 for i in range(mid + 1 , high + 1 ): summ += arr[i] if summ > right_sum: __a = summ __a = i return max_left, max_right, (left_sum + right_sum) def __lowerCAmelCase ( a__ ) -> float: __a = [randint(1 , a__ ) for _ in range(a__ )] __a = time.time() max_subarray(a__ , 0 , input_size - 1 ) __a = time.time() return end - start def __lowerCAmelCase ( ) -> None: __a = [10, 100, 1000, 1_0000, 5_0000, 10_0000, 20_0000, 30_0000, 40_0000, 50_0000] __a = [time_max_subarray(a__ ) for input_size in input_sizes] print('''No of Inputs\t\tTime Taken''' ) for input_size, runtime in zip(a__ , a__ ): print(a__ , '''\t\t''' , a__ ) plt.plot(a__ , a__ ) plt.xlabel('''Number of Inputs''' ) plt.ylabel('''Time taken in seconds''' ) plt.show() if __name__ == "__main__": from doctest import testmod testmod()
6
1
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split A : Union[str, Any] = datasets.load_iris() A : int = np.array(data['data']) A : Optional[Any] = np.array(data['target']) A : int = data['target_names'] A , A , A , A : Tuple = train_test_split(X, y) def __lowerCAmelCase ( a__ , a__ ) -> Dict: return np.linalg.norm(np.array(a__ ) - np.array(a__ ) ) def __lowerCAmelCase ( a__ , a__ , a__ , a__ , a__=5 ) -> Tuple: __a = zip(a__ , a__ ) # List of distances of all points from the point to be classified __a = [] for data_point in data: __a = euclidean_distance(data_point[0] , a__ ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. __a = [i[1] for i in sorted(a__ )[:k]] # Most commonly occurring class among them # is the class into which the point is classified __a = Counter(a__ ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
6
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __A( a , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __A( unittest.TestCase ): @property def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = ort.SessionOptions() __a = False return options def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) __a = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) __a = '''A red cat sitting on a park bench''' __a = np.random.RandomState(0 ) __a = pipe( prompt=_snake_case , image=_snake_case , mask_image=_snake_case , guidance_scale=7.5 , num_inference_steps=10 , generator=_snake_case , output_type='''np''' , ) __a = output.images __a = images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) __a = np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) __a = LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) __a = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=_snake_case , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) __a = '''A red cat sitting on a park bench''' __a = np.random.RandomState(0 ) __a = pipe( prompt=_snake_case , image=_snake_case , mask_image=_snake_case , guidance_scale=7.5 , num_inference_steps=20 , generator=_snake_case , output_type='''np''' , ) __a = output.images __a = images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) __a = np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3
6
1
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch A : Dict = logging.get_logger(__name__) class __A( a ): snake_case_ = ['''pixel_values'''] def __init__( self , _snake_case = True , _snake_case = None , _snake_case = PILImageResampling.BILINEAR , _snake_case = True , _snake_case = None , _snake_case = True , _snake_case = 1 / 255 , _snake_case = True , _snake_case = None , _snake_case = None , **_snake_case , ) -> None: '''simple docstring''' super().__init__(**_snake_case ) __a = size if size is not None else {'''shortest_edge''': 256} __a = get_size_dict(_snake_case , default_to_square=_snake_case ) __a = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} __a = get_size_dict(_snake_case , param_name='''crop_size''' ) __a = do_resize __a = size __a = resample __a = do_center_crop __a = crop_size __a = do_rescale __a = rescale_factor __a = do_normalize __a = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN __a = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = PILImageResampling.BICUBIC , _snake_case = None , **_snake_case , ) -> np.ndarray: '''simple docstring''' __a = get_size_dict(_snake_case , default_to_square=_snake_case ) if "shortest_edge" not in size: raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""" ) __a = get_resize_output_image_size(_snake_case , size=size['''shortest_edge'''] , default_to_square=_snake_case ) return resize(_snake_case , size=_snake_case , resample=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = None , **_snake_case , ) -> np.ndarray: '''simple docstring''' __a = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}""" ) return center_crop(_snake_case , size=(size['''height'''], size['''width''']) , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = None , **_snake_case ) -> np.ndarray: '''simple docstring''' return rescale(_snake_case , scale=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case = None , **_snake_case , ) -> np.ndarray: '''simple docstring''' return normalize(_snake_case , mean=_snake_case , std=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = ChannelDimension.FIRST , **_snake_case , ) -> Optional[int]: '''simple docstring''' __a = do_resize if do_resize is not None else self.do_resize __a = size if size is not None else self.size __a = get_size_dict(_snake_case , default_to_square=_snake_case ) __a = resample if resample is not None else self.resample __a = do_center_crop if do_center_crop is not None else self.do_center_crop __a = crop_size if crop_size is not None else self.crop_size __a = get_size_dict(_snake_case , param_name='''crop_size''' ) __a = do_rescale if do_rescale is not None else self.do_rescale __a = rescale_factor if rescale_factor is not None else self.rescale_factor __a = do_normalize if do_normalize is not None else self.do_normalize __a = image_mean if image_mean is not None else self.image_mean __a = image_std if image_std is not None else self.image_std __a = make_list_of_images(_snake_case ) if not valid_images(_snake_case ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. __a = [to_numpy_array(_snake_case ) for image in images] if do_resize: __a = [self.resize(image=_snake_case , size=_snake_case , resample=_snake_case ) for image in images] if do_center_crop: __a = [self.center_crop(image=_snake_case , size=_snake_case ) for image in images] if do_rescale: __a = [self.rescale(image=_snake_case , scale=_snake_case ) for image in images] if do_normalize: __a = [self.normalize(image=_snake_case , mean=_snake_case , std=_snake_case ) for image in images] __a = [to_channel_dimension_format(_snake_case , _snake_case ) for image in images] __a = {'''pixel_values''': images} return BatchFeature(data=_snake_case , tensor_type=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None ) -> Optional[int]: '''simple docstring''' __a = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_snake_case ) != len(_snake_case ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(_snake_case ): __a = target_sizes.numpy() __a = [] for idx in range(len(_snake_case ) ): __a = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=_snake_case ) __a = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_snake_case ) else: __a = logits.argmax(dim=1 ) __a = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
6
from math import ceil def __lowerCAmelCase ( a__ = 1001 ) -> int: __a = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): __a = 2 * i + 1 __a = 2 * i __a = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[Any] = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number')
6
1
# flake8: noqa # Lint as: python3 A : Optional[Any] = [ 'VerificationMode', 'Version', 'disable_progress_bar', 'enable_progress_bar', 'is_progress_bar_enabled', 'experimental', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
6
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __A( a ): snake_case_ = ['''image_processor''', '''tokenizer'''] snake_case_ = '''ChineseCLIPImageProcessor''' snake_case_ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self , _snake_case=None , _snake_case=None , **_snake_case ) -> Tuple: '''simple docstring''' __a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _snake_case , ) __a = kwargs.pop('''feature_extractor''' ) __a = 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__(_snake_case , _snake_case ) __a = self.image_processor def __call__( self , _snake_case=None , _snake_case=None , _snake_case=None , **_snake_case ) -> Optional[Any]: '''simple docstring''' 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: __a = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) if images is not None: __a = self.image_processor(_snake_case , return_tensors=_snake_case , **_snake_case ) if text is not None and images is not None: __a = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**_snake_case ) , tensor_type=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> Dict: '''simple docstring''' return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' __a = self.tokenizer.model_input_names __a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _snake_case , ) return self.image_processor_class
6
1
import hashlib import unittest from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available from transformers.pipelines import DepthEstimationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_torch_available(): import torch if is_vision_available(): from PIL import Image else: class __A: @staticmethod def SCREAMING_SNAKE_CASE_ ( *_snake_case , **_snake_case ) -> Optional[int]: '''simple docstring''' pass def __lowerCAmelCase ( a__ ) -> str: __a = hashlib.mda(image.tobytes() ) return m.hexdigest() @is_pipeline_test @require_vision @require_timm @require_torch class __A( unittest.TestCase ): snake_case_ = MODEL_FOR_DEPTH_ESTIMATION_MAPPING def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case ) -> Tuple: '''simple docstring''' __a = DepthEstimationPipeline(model=_snake_case , image_processor=_snake_case ) return depth_estimator, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = depth_estimator('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) self.assertEqual({'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )} , _snake_case ) import datasets __a = datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' , '''image''' , split='''test''' ) __a = depth_estimator( [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ), '''http://images.cocodataset.org/val2017/000000039769.jpg''', # RGBA dataset[0]['''file'''], # LA dataset[1]['''file'''], # L dataset[2]['''file'''], ] ) self.assertEqual( [ {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, ] , _snake_case , ) @require_tf @unittest.skip('''Depth estimation is not implemented in TF''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' pass @slow @require_torch def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' __a = '''Intel/dpt-large''' __a = pipeline('''depth-estimation''' , model=_snake_case ) __a = depth_estimator('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) __a = hashimage(outputs['''depth'''] ) # This seems flaky. # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977") self.assertEqual(nested_simplify(outputs['''predicted_depth'''].max().item() ) , 29.304 ) self.assertEqual(nested_simplify(outputs['''predicted_depth'''].min().item() ) , 2.662 ) @require_torch def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' self.skipTest('''There is not hf-internal-testing tiny model for either GLPN nor DPT''' )
6
from __future__ import annotations import typing from collections import Counter def __lowerCAmelCase ( a__ ) -> typing.Counter[int]: __a = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(a__ , max_perimeter + 1 ): __a = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(a__ ): __a = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def __lowerCAmelCase ( a__ = 1000 ) -> int: __a = pythagorean_triple(a__ ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(F"Perimeter {solution()} has maximum solutions")
6
1
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class __A: def __init__( self , _snake_case , _snake_case=100 , _snake_case=13 , _snake_case=30 , _snake_case=2 , _snake_case=3 , _snake_case=True , _snake_case=True , _snake_case=32 , _snake_case=4 , _snake_case=4 , _snake_case=37 , _snake_case="gelu" , _snake_case=0.1 , _snake_case=0.1 , _snake_case=10 , _snake_case=0.02 , _snake_case=3 , _snake_case=None , _snake_case=[0, 1, 2, 3] , ) -> List[str]: '''simple docstring''' __a = parent __a = 100 __a = batch_size __a = image_size __a = patch_size __a = num_channels __a = is_training __a = use_labels __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = type_sequence_label_size __a = initializer_range __a = scope __a = out_indices __a = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) __a = (image_size // patch_size) ** 2 __a = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' __a = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) __a = self.get_config() return config, pixel_values, labels, pixel_labels def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' return BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case ) -> List[Any]: '''simple docstring''' __a = BeitModel(config=_snake_case ) model.to(_snake_case ) model.eval() __a = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case ) -> Optional[Any]: '''simple docstring''' __a = BeitForMaskedImageModeling(config=_snake_case ) model.to(_snake_case ) model.eval() __a = model(_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case ) -> Union[str, Any]: '''simple docstring''' __a = self.type_sequence_label_size __a = BeitForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() __a = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images __a = 1 __a = BeitForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() __a = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __a = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case ) -> List[str]: '''simple docstring''' __a = self.num_labels __a = BeitForSemanticSegmentation(_snake_case ) model.to(_snake_case ) model.eval() __a = model(_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) __a = model(_snake_case , labels=_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a = self.prepare_config_and_inputs() __a , __a , __a , __a = config_and_inputs __a = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __A( a , a , unittest.TestCase ): snake_case_ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) snake_case_ = ( { '''feature-extraction''': BeitModel, '''image-classification''': BeitForImageClassification, '''image-segmentation''': BeitForSemanticSegmentation, } if is_torch_available() else {} ) snake_case_ = False snake_case_ = False snake_case_ = False def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = BeitModelTester(self ) __a = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='''BEiT does not use inputs_embeds''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip(reason='''BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' pass def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __a = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , nn.Linear ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_snake_case ) __a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __a = [*signature.parameters.keys()] __a = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' if not self.model_tester.is_training: return __a , __a = self.model_tester.prepare_config_and_inputs_for_common() __a = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(_snake_case ), BeitForMaskedImageModeling]: continue __a = model_class(_snake_case ) model.to(_snake_case ) model.train() __a = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) __a = model(**_snake_case ).loss loss.backward() def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return __a = False __a = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(_snake_case ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue __a = model_class(_snake_case ) model.gradient_checkpointing_enable() model.to(_snake_case ) model.train() __a = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) __a = model(**_snake_case ).loss loss.backward() def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() __a = _config_zero_init(_snake_case ) for model_class in self.all_model_classes: __a = model_class(config=_snake_case ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue 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""" , ) @slow def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = BeitModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def __lowerCAmelCase ( ) -> Optional[int]: __a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __A( unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224''' ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k''' ).to(_snake_case ) __a = self.default_image_processor __a = prepare_img() __a = image_processor(images=_snake_case , return_tensors='''pt''' ).pixel_values.to(_snake_case ) # prepare bool_masked_pos __a = torch.ones((1, 196) , dtype=torch.bool ).to(_snake_case ) # forward pass with torch.no_grad(): __a = model(pixel_values=_snake_case , bool_masked_pos=_snake_case ) __a = outputs.logits # verify the logits __a = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , _snake_case ) __a = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(_snake_case ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , _snake_case , atol=1E-2 ) ) @slow def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' __a = BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224''' ).to(_snake_case ) __a = self.default_image_processor __a = prepare_img() __a = image_processor(images=_snake_case , return_tensors='''pt''' ).to(_snake_case ) # forward pass with torch.no_grad(): __a = model(**_snake_case ) __a = outputs.logits # verify the logits __a = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , _snake_case ) __a = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(_snake_case ) self.assertTrue(torch.allclose(logits[0, :3] , _snake_case , atol=1E-4 ) ) __a = 281 self.assertEqual(logits.argmax(-1 ).item() , _snake_case ) @slow def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' __a = BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k''' ).to( _snake_case ) __a = self.default_image_processor __a = prepare_img() __a = image_processor(images=_snake_case , return_tensors='''pt''' ).to(_snake_case ) # forward pass with torch.no_grad(): __a = model(**_snake_case ) __a = outputs.logits # verify the logits __a = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , _snake_case ) __a = torch.tensor([1.6881, -0.2787, 0.5901] ).to(_snake_case ) self.assertTrue(torch.allclose(logits[0, :3] , _snake_case , atol=1E-4 ) ) __a = 2_396 self.assertEqual(logits.argmax(-1 ).item() , _snake_case ) @slow def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' __a = BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640''' ) __a = model.to(_snake_case ) __a = BeitImageProcessor(do_resize=_snake_case , size=640 , do_center_crop=_snake_case ) __a = load_dataset('''hf-internal-testing/fixtures_ade20k''' , split='''test''' ) __a = Image.open(ds[0]['''file'''] ) __a = image_processor(images=_snake_case , return_tensors='''pt''' ).to(_snake_case ) # forward pass with torch.no_grad(): __a = model(**_snake_case ) __a = outputs.logits # verify the logits __a = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , _snake_case ) __a = version.parse(PIL.__version__ ) < version.parse('''9.0.0''' ) if is_pillow_less_than_a: __a = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=_snake_case , ) else: __a = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=_snake_case , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' __a = BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640''' ) __a = model.to(_snake_case ) __a = BeitImageProcessor(do_resize=_snake_case , size=640 , do_center_crop=_snake_case ) __a = load_dataset('''hf-internal-testing/fixtures_ade20k''' , split='''test''' ) __a = Image.open(ds[0]['''file'''] ) __a = image_processor(images=_snake_case , return_tensors='''pt''' ).to(_snake_case ) # forward pass with torch.no_grad(): __a = model(**_snake_case ) __a = outputs.logits.detach().cpu() __a = image_processor.post_process_semantic_segmentation(outputs=_snake_case , target_sizes=[(500, 300)] ) __a = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , _snake_case ) __a = image_processor.post_process_semantic_segmentation(outputs=_snake_case ) __a = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , _snake_case )
6
# flake8: noqa # Lint as: python3 A : Optional[Any] = [ 'VerificationMode', 'Version', 'disable_progress_bar', 'enable_progress_bar', 'is_progress_bar_enabled', 'experimental', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
6
1
import os from typing import Dict, List, Tuple, TypeVar, Union A : str = TypeVar('T') A : Dict = Union[List[T], Tuple[T, ...]] A : Union[str, Any] = Union[T, List[T], Dict[str, T]] A : int = Union[str, bytes, os.PathLike]
6
from typing import Dict from .base import GenericTensor, Pipeline class __A( a ): def SCREAMING_SNAKE_CASE_ ( self , _snake_case=None , _snake_case=None , _snake_case=None , **_snake_case ) -> Optional[Any]: '''simple docstring''' if tokenize_kwargs is None: __a = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( '''truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)''' ) __a = truncation __a = tokenize_kwargs __a = {} if return_tensors is not None: __a = return_tensors return preprocess_params, {}, postprocess_params def SCREAMING_SNAKE_CASE_ ( self , _snake_case , **_snake_case ) -> Dict[str, GenericTensor]: '''simple docstring''' __a = self.framework __a = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Optional[Any]: '''simple docstring''' __a = self.model(**_snake_case ) return model_outputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case=False ) -> Optional[int]: '''simple docstring''' if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self , *_snake_case , **_snake_case ) -> Any: '''simple docstring''' return super().__call__(*_snake_case , **_snake_case )
6
1
import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A( a , unittest.TestCase ): snake_case_ = DDIMPipeline snake_case_ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case_ = PipelineTesterMixin.required_optional_params - { '''num_images_per_prompt''', '''latents''', '''callback''', '''callback_steps''', } snake_case_ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case_ = False def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' torch.manual_seed(0 ) __a = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) __a = DDIMScheduler() __a = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case=0 ) -> List[str]: '''simple docstring''' if str(_snake_case ).startswith('''mps''' ): __a = torch.manual_seed(_snake_case ) else: __a = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) __a = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a = '''cpu''' __a = self.get_dummy_components() __a = self.pipeline_class(**_snake_case ) pipe.to(_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) __a = self.get_dummy_inputs(_snake_case ) __a = pipe(**_snake_case ).images __a = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) __a = np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) __a = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_snake_case , 1E-3 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' super().test_save_load_local(expected_max_difference=3E-3 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' super().test_save_load_optional_components(expected_max_difference=3E-3 ) def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __A( unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = '''google/ddpm-cifar10-32''' __a = UNetaDModel.from_pretrained(_snake_case ) __a = DDIMScheduler() __a = DDIMPipeline(unet=_snake_case , scheduler=_snake_case ) ddim.to(_snake_case ) ddim.set_progress_bar_config(disable=_snake_case ) __a = torch.manual_seed(0 ) __a = ddim(generator=_snake_case , eta=0.0 , output_type='''numpy''' ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __a = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' __a = '''google/ddpm-ema-bedroom-256''' __a = UNetaDModel.from_pretrained(_snake_case ) __a = DDIMScheduler.from_pretrained(_snake_case ) __a = DDIMPipeline(unet=_snake_case , scheduler=_snake_case ) ddpm.to(_snake_case ) ddpm.set_progress_bar_config(disable=_snake_case ) __a = torch.manual_seed(0 ) __a = ddpm(generator=_snake_case , output_type='''numpy''' ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) __a = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
6
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 : List[str] = logging.get_logger(__name__) A : Optional[int] = { 'facebook/levit-128S': 'https://huggingface.co/facebook/levit-128S/resolve/main/config.json', # See all LeViT models at https://huggingface.co/models?filter=levit } class __A( a ): snake_case_ = '''levit''' def __init__( self , _snake_case=224 , _snake_case=3 , _snake_case=3 , _snake_case=2 , _snake_case=1 , _snake_case=16 , _snake_case=[128, 256, 384] , _snake_case=[4, 8, 12] , _snake_case=[4, 4, 4] , _snake_case=[16, 16, 16] , _snake_case=0 , _snake_case=[2, 2, 2] , _snake_case=[2, 2, 2] , _snake_case=0.02 , **_snake_case , ) -> Optional[Any]: '''simple docstring''' super().__init__(**_snake_case ) __a = image_size __a = num_channels __a = kernel_size __a = stride __a = padding __a = hidden_sizes __a = num_attention_heads __a = depths __a = key_dim __a = drop_path_rate __a = patch_size __a = attention_ratio __a = mlp_ratio __a = initializer_range __a = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __A( a ): snake_case_ = version.parse('''1.11''' ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> float: '''simple docstring''' return 1E-4
6
1
def __lowerCAmelCase ( a__ = 1000 ) -> int: __a = 3 __a = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(F"{solution() = }")
6
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel A : int = '0.12' # assumed parallelism: 8 @require_flax @is_staging_test class __A( unittest.TestCase ): @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> Union[str, Any]: '''simple docstring''' __a = TOKEN HfFolder.save_token(_snake_case ) @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> Union[str, Any]: '''simple docstring''' try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_snake_case ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""" ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_snake_case , repo_id='''test-model-flax''' , push_to_hub=_snake_case , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""" ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_snake_case ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _snake_case , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_snake_case , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) def __lowerCAmelCase ( a__ , a__ ) -> str: __a = True __a = flatten_dict(modela.params ) __a = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1e-4: __a = False return models_are_equal @require_flax class __A( unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_snake_case ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_snake_case , _snake_case ) ) with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertTrue(check_models_equal(_snake_case , _snake_case ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_snake_case ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_snake_case , _snake_case ) , max_shard_size='''10KB''' ) with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertTrue(check_models_equal(_snake_case , _snake_case ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertIsNotNone(_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertIsNotNone(_snake_case )
6
1
from collections.abc import Generator def __lowerCAmelCase ( ) -> Generator[int, None, None]: __a , __a = 0, 1 while True: __a , __a = b, a + b yield b def __lowerCAmelCase ( a__ = 1000 ) -> int: __a = 1 __a = fibonacci_generator() while len(str(next(a__ ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
6
# XXX: we want transformers master here - in the absense of conftest manipulating sys.path: # hack it in for now: import sys from pathlib import Path A : Optional[Any] = Path(__file__).resolve().parents[3] / 'src' sys.path.insert(1, str(git_repo_path)) import dataclasses # noqa import io # noqa import itertools # noqa import json # noqa import os # noqa import unittest # noqa from copy import deepcopy # noqa from parameterized import parameterized # noqa from transformers import TrainingArguments, is_torch_available # noqa from transformers.deepspeed import is_deepspeed_available # noqa from transformers.file_utils import WEIGHTS_NAME # noqa from transformers.testing_utils import ( # noqa CaptureLogger, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, mockenv_context, require_deepspeed, require_torch_gpu, require_torch_multi_gpu, slow, ) from transformers.trainer_utils import set_seed # noqa set_seed(4_2) A : List[str] = {'base': 'patrickvonplaten/wav2vec2_tiny_random', 'robust': 'patrickvonplaten/wav2vec2_tiny_random_robust'} A : Optional[int] = 'zero2' A : str = 'zero3' A : Tuple = [ZEROa, ZEROa] def __lowerCAmelCase ( a__ , a__ , a__ ) -> Tuple: # customize the test name generator function as we want both params to appear in the sub-test # name, as by default it shows only the first param __a = parameterized.to_safe_name('''_'''.join(str(a__ ) for x in param.args ) ) return F"""{func.__name__}_{param_based_name}""" # Cartesian-product of zero stages with models to test A : Union[str, Any] = list(itertools.product(stages, models.keys())) @slow @require_deepspeed @require_torch_gpu class __A( a ): @parameterized.expand(_snake_case , name_func=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Any: '''simple docstring''' self.run_and_check( stage=_snake_case , model=_snake_case , distributed=_snake_case , fpaa=_snake_case , ) @require_torch_multi_gpu @parameterized.expand(_snake_case , name_func=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> int: '''simple docstring''' self.run_and_check( stage=_snake_case , model=_snake_case , distributed=_snake_case , fpaa=_snake_case , ) @parameterized.expand(_snake_case , name_func=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> str: '''simple docstring''' self.run_and_check( stage=_snake_case , model=_snake_case , distributed=_snake_case , fpaa=_snake_case , ) @require_torch_multi_gpu @parameterized.expand(_snake_case , name_func=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[Any]: '''simple docstring''' self.run_and_check( stage=_snake_case , model=_snake_case , distributed=_snake_case , fpaa=_snake_case , ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Tuple: '''simple docstring''' pass def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = 10 , _snake_case = True , _snake_case = True , _snake_case = True , ) -> Any: '''simple docstring''' __a = models[model] __a = self.run_trainer( stage=_snake_case , model_name=_snake_case , eval_steps=_snake_case , num_train_epochs=1 , distributed=_snake_case , fpaa=_snake_case , ) self.do_checks(_snake_case ) return output_dir def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = 10 , _snake_case = 1 , _snake_case = True , _snake_case = True , ) -> Union[str, Any]: '''simple docstring''' __a = self.get_auto_remove_tmp_dir('''./xxx''' , after=_snake_case ) __a = F""" --model_name_or_path {model_name} --dataset_name hf-internal-testing/librispeech_asr_dummy --dataset_config_name clean --train_split_name validation --validation_split_name validation --output_dir {output_dir} --num_train_epochs {str(_snake_case )} --per_device_train_batch_size 2 --per_device_eval_batch_size 2 --evaluation_strategy steps --learning_rate 5e-4 --warmup_steps 8 --orthography timit --preprocessing_num_workers 1 --group_by_length --freeze_feature_extractor --report_to none --save_steps 0 --eval_steps {eval_steps} --report_to none """.split() if fpaa: args.extend(['''--fp16'''] ) # currently ds_config_wav2vec2_zero.json requires "zero_optimization.find_unused_parameters": true, # hence the separate config files __a = F"""--deepspeed {self.test_file_dir_str}/ds_config_wav2vec2_{stage}.json""".split() __a = [F"""{self.examples_dir_str}/research_projects/wav2vec2/run_asr.py"""] __a = self.get_launcher(_snake_case ) __a = launcher + script + args + ds_args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(_snake_case , env=self.get_env() ) return output_dir def SCREAMING_SNAKE_CASE_ ( self , _snake_case=False ) -> List[str]: '''simple docstring''' __a = min(2 , get_gpu_count() ) if distributed else 1 return F"""deepspeed --num_nodes 1 --num_gpus {num_gpus}""".split()
6
1
import datasets from .nmt_bleu import compute_bleu # From: https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py A : Dict = '\\n@INPROCEEDINGS{Papineni02bleu:a,\n author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu},\n title = {BLEU: a Method for Automatic Evaluation of Machine Translation},\n booktitle = {},\n year = {2002},\n pages = {311--318}\n}\n@inproceedings{lin-och-2004-orange,\n title = "{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation",\n author = "Lin, Chin-Yew and\n Och, Franz Josef",\n booktitle = "{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics",\n month = "aug 23{--}aug 27",\n year = "2004",\n address = "Geneva, Switzerland",\n publisher = "COLING",\n url = "https://www.aclweb.org/anthology/C04-1072",\n pages = "501--507",\n}\n' A : Union[str, Any] = '\\nBLEU (bilingual evaluation understudy) is an algorithm for evaluating the quality of text which has been machine-translated from one natural language to another.\nQuality is considered to be the correspondence between a machine\'s output and that of a human: "the closer a machine translation is to a professional human translation,\nthe better it is" – this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and\nremains one of the most popular automated and inexpensive metrics.\n\nScores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations.\nThose scores are then averaged over the whole corpus to reach an estimate of the translation\'s overall quality. Intelligibility or grammatical correctness\nare not taken into account[citation needed].\n\nBLEU\'s output is always a number between 0 and 1. This value indicates how similar the candidate text is to the reference texts, with values closer to 1\nrepresenting more similar texts. Few human translations will attain a score of 1, since this would indicate that the candidate is identical to one of the\nreference translations. For this reason, it is not necessary to attain a score of 1. Because there are more opportunities to match, adding additional\nreference translations will increase the BLEU score.\n' A : Dict = '\nComputes BLEU score of translated segments against one or more references.\nArgs:\n predictions: list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references: list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n max_order: Maximum n-gram order to use when computing BLEU score.\n smooth: Whether or not to apply Lin et al. 2004 smoothing.\nReturns:\n \'bleu\': bleu score,\n \'precisions\': geometric mean of n-gram precisions,\n \'brevity_penalty\': brevity penalty,\n \'length_ratio\': ratio of lengths,\n \'translation_length\': translation_length,\n \'reference_length\': reference_length\nExamples:\n\n >>> predictions = [\n ... ["hello", "there", "general", "kenobi"], # tokenized prediction of the first sample\n ... ["foo", "bar", "foobar"] # tokenized prediction of the second sample\n ... ]\n >>> references = [\n ... [["hello", "there", "general", "kenobi"], ["hello", "there", "!"]], # tokenized references for the first sample (2 references)\n ... [["foo", "bar", "foobar"]] # tokenized references for the second sample (1 reference)\n ... ]\n >>> bleu = datasets.load_metric("bleu")\n >>> results = bleu.compute(predictions=predictions, references=references)\n >>> print(results["bleu"])\n 1.0\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A( datasets.Metric ): def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''string''' , id='''token''' ) , id='''sequence''' ), '''references''': datasets.Sequence( datasets.Sequence(datasets.Value('''string''' , id='''token''' ) , id='''sequence''' ) , id='''references''' ), } ) , codebase_urls=['''https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/BLEU''', '''https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213''', ] , ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case=4 , _snake_case=False ) -> Any: '''simple docstring''' __a = compute_bleu( reference_corpus=_snake_case , translation_corpus=_snake_case , max_order=_snake_case , smooth=_snake_case ) ((__a) , (__a) , (__a) , (__a) , (__a) , (__a)) = score return { "bleu": bleu, "precisions": precisions, "brevity_penalty": bp, "length_ratio": ratio, "translation_length": translation_length, "reference_length": reference_length, }
6
import gc import unittest import torch from parameterized import parameterized from diffusers import AutoencoderKL from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class __A( a , a , unittest.TestCase ): snake_case_ = AutoencoderKL snake_case_ = '''sample''' snake_case_ = 1E-2 @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = 4 __a = 3 __a = (32, 32) __a = floats_tensor((batch_size, num_channels) + sizes ).to(_snake_case ) return {"sample": image} @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' return (3, 32, 32) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' return (3, 32, 32) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a = { '''block_out_channels''': [32, 64], '''in_channels''': 3, '''out_channels''': 3, '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], '''latent_channels''': 4, } __a = self.dummy_input return init_dict, inputs_dict def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' pass def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' pass @unittest.skipIf(torch_device == '''mps''' , '''Gradient checkpointing skipped on MPS''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' __a , __a = self.prepare_init_args_and_inputs_for_common() __a = self.model_class(**_snake_case ) model.to(_snake_case ) assert not model.is_gradient_checkpointing and model.training __a = model(**_snake_case ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model.zero_grad() __a = torch.randn_like(_snake_case ) __a = (out - labels).mean() loss.backward() # re-instantiate the model now enabling gradient checkpointing __a = self.model_class(**_snake_case ) # clone model model_a.load_state_dict(model.state_dict() ) model_a.to(_snake_case ) model_a.enable_gradient_checkpointing() assert model_a.is_gradient_checkpointing and model_a.training __a = model_a(**_snake_case ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model_a.zero_grad() __a = (out_a - labels).mean() loss_a.backward() # compare the output and parameters gradients self.assertTrue((loss - loss_a).abs() < 1E-5 ) __a = dict(model.named_parameters() ) __a = dict(model_a.named_parameters() ) for name, param in named_params.items(): self.assertTrue(torch_all_close(param.grad.data , named_params_a[name].grad.data , atol=5E-5 ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' __a , __a = AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy''' , output_loading_info=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(_snake_case ) __a = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' __a = AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy''' ) __a = model.to(_snake_case ) model.eval() if torch_device == "mps": __a = torch.manual_seed(0 ) else: __a = torch.Generator(device=_snake_case ).manual_seed(0 ) __a = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) __a = image.to(_snake_case ) with torch.no_grad(): __a = model(_snake_case , sample_posterior=_snake_case , generator=_snake_case ).sample __a = output[0, -1, -3:, -3:].flatten().cpu() # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. if torch_device == "mps": __a = torch.tensor( [ -4.0_078E-01, -3.8_323E-04, -1.2_681E-01, -1.1_462E-01, 2.0_095E-01, 1.0_893E-01, -8.8_247E-02, -3.0_361E-01, -9.8_644E-03, ] ) elif torch_device == "cpu": __a = torch.tensor( [-0.1352, 0.0878, 0.0419, -0.0818, -0.1069, 0.0688, -0.1458, -0.4446, -0.0026] ) else: __a = torch.tensor( [-0.2421, 0.4642, 0.2507, -0.0438, 0.0682, 0.3160, -0.2018, -0.0727, 0.2485] ) self.assertTrue(torch_all_close(_snake_case , _snake_case , rtol=1E-2 ) ) @slow class __A( unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[Any]: '''simple docstring''' return F"""gaussian_noise_s={seed}_shape={'_'.join([str(_snake_case ) for s in shape] )}.npy""" def SCREAMING_SNAKE_CASE_ ( self ) -> Dict: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self , _snake_case=0 , _snake_case=(4, 3, 512, 512) , _snake_case=False ) -> Any: '''simple docstring''' __a = torch.floataa if fpaa else torch.floataa __a = torch.from_numpy(load_hf_numpy(self.get_file_format(_snake_case , _snake_case ) ) ).to(_snake_case ).to(_snake_case ) return image def SCREAMING_SNAKE_CASE_ ( self , _snake_case="CompVis/stable-diffusion-v1-4" , _snake_case=False ) -> Optional[Any]: '''simple docstring''' __a = '''fp16''' if fpaa else None __a = torch.floataa if fpaa else torch.floataa __a = AutoencoderKL.from_pretrained( _snake_case , subfolder='''vae''' , torch_dtype=_snake_case , revision=_snake_case , ) model.to(_snake_case ).eval() return model def SCREAMING_SNAKE_CASE_ ( self , _snake_case=0 ) -> Tuple: '''simple docstring''' if torch_device == "mps": return torch.manual_seed(_snake_case ) return torch.Generator(device=_snake_case ).manual_seed(_snake_case ) @parameterized.expand( [ # fmt: off [33, [-0.1603, 0.9878, -0.0495, -0.0790, -0.2709, 0.8375, -0.2060, -0.0824], [-0.2395, 0.0098, 0.0102, -0.0709, -0.2840, -0.0274, -0.0718, -0.1824]], [47, [-0.2376, 0.1168, 0.1332, -0.4840, -0.2508, -0.0791, -0.0493, -0.4089], [0.0350, 0.0847, 0.0467, 0.0344, -0.0842, -0.0547, -0.0633, -0.1131]], # fmt: on ] ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case ) -> List[Any]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case ) __a = self.get_generator(_snake_case ) with torch.no_grad(): __a = model(_snake_case , generator=_snake_case , sample_posterior=_snake_case ).sample assert sample.shape == image.shape __a = sample[-1, -2:, -2:, :2].flatten().float().cpu() __a = torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice ) assert torch_all_close(_snake_case , _snake_case , atol=3E-3 ) @parameterized.expand( [ # fmt: off [33, [-0.0513, 0.0289, 1.3799, 0.2166, -0.2573, -0.0871, 0.5103, -0.0999]], [47, [-0.4128, -0.1320, -0.3704, 0.1965, -0.4116, -0.2332, -0.3340, 0.2247]], # fmt: on ] ) @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Tuple: '''simple docstring''' __a = self.get_sd_vae_model(fpaa=_snake_case ) __a = self.get_sd_image(_snake_case , fpaa=_snake_case ) __a = self.get_generator(_snake_case ) with torch.no_grad(): __a = model(_snake_case , generator=_snake_case , sample_posterior=_snake_case ).sample assert sample.shape == image.shape __a = sample[-1, -2:, :2, -2:].flatten().float().cpu() __a = torch.tensor(_snake_case ) assert torch_all_close(_snake_case , _snake_case , atol=1E-2 ) @parameterized.expand( [ # fmt: off [33, [-0.1609, 0.9866, -0.0487, -0.0777, -0.2716, 0.8368, -0.2055, -0.0814], [-0.2395, 0.0098, 0.0102, -0.0709, -0.2840, -0.0274, -0.0718, -0.1824]], [47, [-0.2377, 0.1147, 0.1333, -0.4841, -0.2506, -0.0805, -0.0491, -0.4085], [0.0350, 0.0847, 0.0467, 0.0344, -0.0842, -0.0547, -0.0633, -0.1131]], # fmt: on ] ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case ) with torch.no_grad(): __a = model(_snake_case ).sample assert sample.shape == image.shape __a = sample[-1, -2:, -2:, :2].flatten().float().cpu() __a = torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice ) assert torch_all_close(_snake_case , _snake_case , atol=3E-3 ) @parameterized.expand( [ # fmt: off [13, [-0.2051, -0.1803, -0.2311, -0.2114, -0.3292, -0.3574, -0.2953, -0.3323]], [37, [-0.2632, -0.2625, -0.2199, -0.2741, -0.4539, -0.4990, -0.3720, -0.4925]], # fmt: on ] ) @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case , shape=(3, 4, 64, 64) ) with torch.no_grad(): __a = model.decode(_snake_case ).sample assert list(sample.shape ) == [3, 3, 512, 512] __a = sample[-1, -2:, :2, -2:].flatten().cpu() __a = torch.tensor(_snake_case ) assert torch_all_close(_snake_case , _snake_case , atol=1E-3 ) @parameterized.expand( [ # fmt: off [27, [-0.0369, 0.0207, -0.0776, -0.0682, -0.1747, -0.1930, -0.1465, -0.2039]], [16, [-0.1628, -0.2134, -0.2747, -0.2642, -0.3774, -0.4404, -0.3687, -0.4277]], # fmt: on ] ) @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[Any]: '''simple docstring''' __a = self.get_sd_vae_model(fpaa=_snake_case ) __a = self.get_sd_image(_snake_case , shape=(3, 4, 64, 64) , fpaa=_snake_case ) with torch.no_grad(): __a = model.decode(_snake_case ).sample assert list(sample.shape ) == [3, 3, 512, 512] __a = sample[-1, -2:, :2, -2:].flatten().float().cpu() __a = torch.tensor(_snake_case ) assert torch_all_close(_snake_case , _snake_case , atol=5E-3 ) @parameterized.expand([(13,), (16,), (27,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason='''xformers is not required when using PyTorch 2.0.''' ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Union[str, Any]: '''simple docstring''' __a = self.get_sd_vae_model(fpaa=_snake_case ) __a = self.get_sd_image(_snake_case , shape=(3, 4, 64, 64) , fpaa=_snake_case ) with torch.no_grad(): __a = model.decode(_snake_case ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): __a = model.decode(_snake_case ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(_snake_case , _snake_case , atol=1E-1 ) @parameterized.expand([(13,), (16,), (37,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason='''xformers is not required when using PyTorch 2.0.''' ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> List[str]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case , shape=(3, 4, 64, 64) ) with torch.no_grad(): __a = model.decode(_snake_case ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): __a = model.decode(_snake_case ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(_snake_case , _snake_case , atol=1E-2 ) @parameterized.expand( [ # fmt: off [33, [-0.3001, 0.0918, -2.6984, -3.9720, -3.2099, -5.0353, 1.7338, -0.2065, 3.4267]], [47, [-1.5030, -4.3871, -6.0355, -9.1157, -1.6661, -2.7853, 2.1607, -5.0823, 2.5633]], # fmt: on ] ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = self.get_sd_vae_model() __a = self.get_sd_image(_snake_case ) __a = self.get_generator(_snake_case ) with torch.no_grad(): __a = model.encode(_snake_case ).latent_dist __a = dist.sample(generator=_snake_case ) assert list(sample.shape ) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]] __a = sample[0, -1, -3:, -3:].flatten().cpu() __a = torch.tensor(_snake_case ) __a = 3E-3 if torch_device != '''mps''' else 1E-2 assert torch_all_close(_snake_case , _snake_case , atol=_snake_case )
6
1
from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class __A: snake_case_ = 42 snake_case_ = None snake_case_ = None def __lowerCAmelCase ( ) -> Node | None: __a = Node(1 ) __a = Node(2 ) __a = Node(3 ) __a = Node(4 ) __a = Node(5 ) return tree def __lowerCAmelCase ( a__ ) -> list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def __lowerCAmelCase ( a__ ) -> list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def __lowerCAmelCase ( a__ ) -> list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def __lowerCAmelCase ( a__ ) -> int: return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def __lowerCAmelCase ( a__ ) -> Sequence[Node | None]: __a = [] if root is None: return output __a = deque([root] ) while process_queue: __a = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def __lowerCAmelCase ( a__ , a__ ) -> Sequence[Node | None]: __a = [] def populate_output(a__ , a__ ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left , level - 1 ) populate_output(root.right , level - 1 ) populate_output(a__ , a__ ) return output def __lowerCAmelCase ( a__ , a__ ) -> Sequence[Node | None]: __a = [] def populate_output(a__ , a__ ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right , level - 1 ) populate_output(root.left , level - 1 ) populate_output(a__ , a__ ) return output def __lowerCAmelCase ( a__ ) -> Sequence[Node | None] | list[Any]: if root is None: return [] __a = [] __a = 0 __a = height(a__ ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(a__ , a__ ) ) __a = 1 else: output.append(get_nodes_from_right_to_left(a__ , a__ ) ) __a = 0 return output def __lowerCAmelCase ( ) -> None: # Main function for testing. __a = make_tree() print(F"""In-order Traversal: {inorder(a__ )}""" ) print(F"""Pre-order Traversal: {preorder(a__ )}""" ) print(F"""Post-order Traversal: {postorder(a__ )}""" , '''\n''' ) print(F"""Height of Tree: {height(a__ )}""" , '''\n''' ) print('''Complete Level Order Traversal: ''' ) print(level_order(a__ ) , '''\n''' ) print('''Level-wise order Traversal: ''' ) for level in range(1 , height(a__ ) + 1 ): print(F"""Level {level}:""" , get_nodes_from_left_to_right(a__ , level=a__ ) ) print('''\nZigZag order Traversal: ''' ) print(zigzag(a__ ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
6
import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup A : str = logging.get_logger(__name__) class __A( a ): def __init__( self , **_snake_case ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''bs4'''] ) super().__init__(**_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> int: '''simple docstring''' __a = [] __a = [] __a = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag __a = parent.find_all(child.name , recursive=_snake_case ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(_snake_case ) else next(i for i, s in enumerate(_snake_case , 1 ) if s is child ) ) __a = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Optional[int]: '''simple docstring''' __a = BeautifulSoup(_snake_case , '''html.parser''' ) __a = [] __a = [] __a = [] for element in html_code.descendants: if type(_snake_case ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue __a = html.unescape(_snake_case ).strip() if not text_in_this_tag: continue all_doc_strings.append(_snake_case ) __a , __a = self.xpath_soup(_snake_case ) stringaxtag_seq.append(_snake_case ) stringaxsubs_seq.append(_snake_case ) if len(_snake_case ) != len(_snake_case ): raise ValueError('''Number of doc strings and xtags does not correspond''' ) if len(_snake_case ) != len(_snake_case ): raise ValueError('''Number of doc strings and xsubs does not correspond''' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = '''''' for tagname, subs in zip(_snake_case , _snake_case ): xpath += F"""/{tagname}""" if subs != 0: xpath += F"""[{subs}]""" return xpath def __call__( self , _snake_case ) -> BatchFeature: '''simple docstring''' __a = False # Check that strings has a valid type if isinstance(_snake_case , _snake_case ): __a = True elif isinstance(_snake_case , (list, tuple) ): if len(_snake_case ) == 0 or isinstance(html_strings[0] , _snake_case ): __a = True if not valid_strings: raise ValueError( '''HTML strings must of type `str`, `List[str]` (batch of examples), ''' F"""but is of type {type(_snake_case )}.""" ) __a = bool(isinstance(_snake_case , (list, tuple) ) and (isinstance(html_strings[0] , _snake_case )) ) if not is_batched: __a = [html_strings] # Get nodes + xpaths __a = [] __a = [] for html_string in html_strings: __a , __a , __a = self.get_three_from_single(_snake_case ) nodes.append(_snake_case ) __a = [] for node, tag_list, sub_list in zip(_snake_case , _snake_case , _snake_case ): __a = self.construct_xpath(_snake_case , _snake_case ) xpath_strings.append(_snake_case ) xpaths.append(_snake_case ) # return as Dict __a = {'''nodes''': nodes, '''xpaths''': xpaths} __a = BatchFeature(data=_snake_case , tensor_type=_snake_case ) return encoded_inputs
6
1
import argparse from pathlib import Path import fairseq import torch from fairseq.models.xmod import XMODModel as FairseqXmodModel from packaging import version from transformers import XmodConfig, XmodForMaskedLM, XmodForSequenceClassification from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse('0.12.2'): raise Exception('requires fairseq >= 0.12.2') if version.parse(fairseq.__version__) > version.parse('2'): raise Exception('requires fairseq < v2') logging.set_verbosity_info() A : str = logging.get_logger(__name__) A : Union[str, Any] = 'Hello, World!' A : Any = 'en_XX' def __lowerCAmelCase ( a__ , a__ , a__ ) -> Any: __a = Path('''data_bin''' ) __a = FairseqXmodModel.from_pretrained( model_name_or_path=str(Path(a__ ).parent ) , checkpoint_file=Path(a__ ).name , _name='''xmod_base''' , arch='''xmod_base''' , task='''multilingual_masked_lm''' , data_name_or_path=str(a__ ) , bpe='''sentencepiece''' , sentencepiece_model=str(Path(a__ ).parent / '''sentencepiece.bpe.model''' ) , src_dict=str(data_dir / '''dict.txt''' ) , ) xmod.eval() # disable dropout print(a__ ) __a = xmod.model.encoder.sentence_encoder __a = XmodConfig( vocab_size=xmod_sent_encoder.embed_tokens.num_embeddings , hidden_size=xmod.cfg.model.encoder_embed_dim , num_hidden_layers=xmod.cfg.model.encoder_layers , num_attention_heads=xmod.cfg.model.encoder_attention_heads , intermediate_size=xmod.cfg.model.encoder_ffn_embed_dim , max_position_embeddings=514 , type_vocab_size=1 , layer_norm_eps=1e-5 , pre_norm=xmod.cfg.model.encoder_normalize_before , adapter_reduction_factor=getattr(xmod.cfg.model , '''bottleneck''' , 2 ) , adapter_layer_norm=xmod.cfg.model.adapter_layer_norm , adapter_reuse_layer_norm=xmod.cfg.model.adapter_reuse_layer_norm , ln_before_adapter=xmod.cfg.model.ln_before_adapter , languages=xmod.cfg.model.languages , ) if classification_head: __a = xmod.model.classification_heads['''mnli'''].out_proj.weight.shape[0] print('''Our X-MOD config:''' , a__ ) __a = XmodForSequenceClassification(a__ ) if classification_head else XmodForMaskedLM(a__ ) model.eval() # Now let's copy all the weights. # Embeddings __a = xmod_sent_encoder.embed_tokens.weight __a = xmod_sent_encoder.embed_positions.weight __a = torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight ) # just zero them out b/c xmod doesn't use them. __a = xmod_sent_encoder.layernorm_embedding.weight __a = xmod_sent_encoder.layernorm_embedding.bias for i in range(config.num_hidden_layers ): # Encoder: start of layer __a = model.roberta.encoder.layer[i] __a = xmod_sent_encoder.layers[i] # self attention __a = layer.attention.self if not ( xmod_layer.self_attn.k_proj.weight.data.shape == xmod_layer.self_attn.q_proj.weight.data.shape == xmod_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size) ) ): raise AssertionError('''Dimensions of self-attention weights do not match.''' ) __a = xmod_layer.self_attn.q_proj.weight __a = xmod_layer.self_attn.q_proj.bias __a = xmod_layer.self_attn.k_proj.weight __a = xmod_layer.self_attn.k_proj.bias __a = xmod_layer.self_attn.v_proj.weight __a = xmod_layer.self_attn.v_proj.bias # self-attention output __a = layer.attention.output if self_output.dense.weight.shape != xmod_layer.self_attn.out_proj.weight.shape: raise AssertionError('''Dimensions of self-attention output weights do not match.''' ) __a = xmod_layer.self_attn.out_proj.weight __a = xmod_layer.self_attn.out_proj.bias __a = xmod_layer.self_attn_layer_norm.weight __a = xmod_layer.self_attn_layer_norm.bias # intermediate __a = layer.intermediate if intermediate.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError('''Dimensions of intermediate weights do not match.''' ) __a = xmod_layer.fca.weight __a = xmod_layer.fca.bias # output __a = layer.output if bert_output.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError('''Dimensions of feed-forward weights do not match.''' ) __a = xmod_layer.fca.weight __a = xmod_layer.fca.bias __a = xmod_layer.final_layer_norm.weight __a = xmod_layer.final_layer_norm.bias if bert_output.adapter_layer_norm is not None: __a = xmod_layer.adapter_layer_norm.weight __a = xmod_layer.adapter_layer_norm.bias if sorted(bert_output.adapter_modules.keys() ) != sorted(xmod_layer.adapter_modules.keys() ): raise AssertionError('''Lists of language adapters do not match.''' ) for lang_code, adapter in xmod_layer.adapter_modules.items(): __a = bert_output.adapter_modules[lang_code] __a = xmod_layer.adapter_modules[lang_code] __a = from_adapter.fca.weight __a = from_adapter.fca.bias __a = from_adapter.fca.weight __a = from_adapter.fca.bias # end of layer if xmod_sent_encoder.layer_norm is not None: __a = xmod_sent_encoder.layer_norm.weight __a = xmod_sent_encoder.layer_norm.bias if classification_head: __a = xmod.model.classification_heads['''mnli'''].dense.weight __a = xmod.model.classification_heads['''mnli'''].dense.bias __a = xmod.model.classification_heads['''mnli'''].out_proj.weight __a = xmod.model.classification_heads['''mnli'''].out_proj.bias else: # LM Head __a = xmod.model.encoder.lm_head.dense.weight __a = xmod.model.encoder.lm_head.dense.bias __a = xmod.model.encoder.lm_head.layer_norm.weight __a = xmod.model.encoder.lm_head.layer_norm.bias __a = xmod.model.encoder.lm_head.weight __a = xmod.model.encoder.lm_head.bias # Let's check that we get the same results. __a = xmod.encode(a__ ).unsqueeze(0 ) # batch of size 1 model.roberta.set_default_language(a__ ) __a = model(a__ )[0] if classification_head: __a = xmod.model.classification_heads['''mnli'''](xmod.extract_features(a__ ) ) else: __a = xmod.model(a__ , lang_id=[SAMPLE_LANGUAGE] )[0] print(our_output.shape , their_output.shape ) __a = torch.max(torch.abs(our_output - their_output ) ).item() print(F"""max_absolute_diff = {max_absolute_diff}""" ) # ~ 1e-7 __a = torch.allclose(a__ , a__ , atol=1e-3 ) print('''Do both models output the same tensors?''' , '''🔥''' if success else '''💩''' ) if not success: raise Exception('''Something went wRoNg''' ) Path(a__ ).mkdir(parents=a__ , exist_ok=a__ ) print(F"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(a__ ) if __name__ == "__main__": A : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--xmod_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--classification_head', action='store_true', help='Whether to convert a final classification head.' ) A : Optional[int] = parser.parse_args() convert_xmod_checkpoint_to_pytorch( args.xmod_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
6
def __lowerCAmelCase ( a__ , a__ ) -> float: def get_matched_characters(a__ , a__ ) -> str: __a = [] __a = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): __a = int(max(0 , i - limit ) ) __a = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(a__ ) __a = F"""{_stra[0:_stra.index(a__ )]} {_stra[_stra.index(a__ ) + 1:]}""" return "".join(a__ ) # matching characters __a = get_matched_characters(a__ , a__ ) __a = get_matched_characters(a__ , a__ ) __a = len(a__ ) # transposition __a = ( len([(ca, ca) for ca, ca in zip(a__ , a__ ) if ca != ca] ) // 2 ) if not match_count: __a = 0.0 else: __a = ( 1 / 3 * ( match_count / len(a__ ) + match_count / len(a__ ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters __a = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
6
1
import argparse import torch from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def __lowerCAmelCase ( a__ , a__ , a__ ) -> Optional[Any]: # Construct model if gpta_config_file == "": __a = GPTaConfig() else: __a = GPTaConfig.from_json_file(a__ ) __a = GPTaModel(a__ ) # Load weights from numpy load_tf_weights_in_gpta(a__ , a__ , a__ ) # Save pytorch-model __a = pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME __a = pytorch_dump_folder_path + '''/''' + CONFIG_NAME print(F"""Save PyTorch model to {pytorch_weights_dump_path}""" ) torch.save(model.state_dict() , a__ ) print(F"""Save configuration file to {pytorch_config_dump_path}""" ) with open(a__ , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '--gpt2_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--gpt2_config_file', default='', type=str, help=( 'An optional config json file corresponding to the pre-trained OpenAI model. \n' 'This specifies the model architecture.' ), ) A : Tuple = parser.parse_args() convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)
6
def __lowerCAmelCase ( a__ ) -> str: __a = [] __a = set({'''(''', '''[''', '''{'''} ) __a = set({''')''', ''']''', '''}'''} ) __a = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(a__ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(a__ ) == 0 or (len(a__ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(a__ ) == 0 def __lowerCAmelCase ( ) -> Dict: __a = input('''Enter sequence of brackets: ''' ) if is_balanced(a__ ): print(a__ , '''is balanced''' ) else: print(a__ , '''is not balanced''' ) if __name__ == "__main__": main()
6
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) A : Dict = {'processing_layoutxlm': ['LayoutXLMProcessor']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[int] = ['LayoutXLMTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ['LayoutXLMTokenizerFast'] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys A : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : str = { 'configuration_blenderbot': [ 'BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BlenderbotConfig', 'BlenderbotOnnxConfig', ], 'tokenization_blenderbot': ['BlenderbotTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ['BlenderbotTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ 'BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlenderbotForCausalLM', 'BlenderbotForConditionalGeneration', 'BlenderbotModel', 'BlenderbotPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'TFBlenderbotForConditionalGeneration', 'TFBlenderbotModel', 'TFBlenderbotPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'FlaxBlenderbotForConditionalGeneration', 'FlaxBlenderbotModel', 'FlaxBlenderbotPreTrainedModel', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys A : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { 'tiiuae/falcon-40b': 'https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json', 'tiiuae/falcon-7b': 'https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json', } class __A( a ): snake_case_ = '''falcon''' snake_case_ = ['''past_key_values'''] def __init__( self , _snake_case=65_024 , _snake_case=4_544 , _snake_case=32 , _snake_case=71 , _snake_case=1E-5 , _snake_case=0.02 , _snake_case=True , _snake_case=0.0 , _snake_case=0.0 , _snake_case=None , _snake_case=False , _snake_case=False , _snake_case=True , _snake_case=True , _snake_case=False , _snake_case=11 , _snake_case=11 , **_snake_case , ) -> List[Any]: '''simple docstring''' __a = vocab_size # Backward compatibility with n_embed kwarg __a = kwargs.pop('''n_embed''' , _snake_case ) __a = hidden_size if n_embed is None else n_embed __a = num_hidden_layers __a = num_attention_heads __a = layer_norm_epsilon __a = initializer_range __a = use_cache __a = hidden_dropout __a = attention_dropout __a = bos_token_id __a = eos_token_id __a = num_attention_heads if num_kv_heads is None else num_kv_heads __a = alibi __a = new_decoder_architecture __a = multi_query # Ignored when new_decoder_architecture is True __a = parallel_attn __a = bias super().__init__(bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' return self.hidden_size // self.num_attention_heads @property def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' return not self.alibi
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : Dict = { 'configuration_xlm_roberta': [ 'XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMRobertaConfig', 'XLMRobertaOnnxConfig', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Union[str, Any] = ['XLMRobertaTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = ['XLMRobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = [ 'XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMRobertaForCausalLM', 'XLMRobertaForMaskedLM', 'XLMRobertaForMultipleChoice', 'XLMRobertaForQuestionAnswering', 'XLMRobertaForSequenceClassification', 'XLMRobertaForTokenClassification', 'XLMRobertaModel', 'XLMRobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = [ 'TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFXLMRobertaForCausalLM', 'TFXLMRobertaForMaskedLM', 'TFXLMRobertaForMultipleChoice', 'TFXLMRobertaForQuestionAnswering', 'TFXLMRobertaForSequenceClassification', 'TFXLMRobertaForTokenClassification', 'TFXLMRobertaModel', 'TFXLMRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ 'FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'FlaxXLMRobertaForMaskedLM', 'FlaxXLMRobertaForCausalLM', 'FlaxXLMRobertaForMultipleChoice', 'FlaxXLMRobertaForQuestionAnswering', 'FlaxXLMRobertaForSequenceClassification', 'FlaxXLMRobertaForTokenClassification', 'FlaxXLMRobertaModel', 'FlaxXLMRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings A : Optional[Any] = R'\n [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n Args:\n title_sep (`str`, *optional*, defaults to `" / "`):\n Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n doc_sep (`str`, *optional*, defaults to `" // "`):\n Separator inserted between the text of the retrieved document and the original input when calling\n [`RagRetriever`].\n n_docs (`int`, *optional*, defaults to 5):\n Number of documents to retrieve.\n max_combined_length (`int`, *optional*, defaults to 300):\n Max length of contextualized input returned by [`~RagRetriever.__call__`].\n retrieval_vector_size (`int`, *optional*, defaults to 768):\n Dimensionality of the document embeddings indexed by [`RagRetriever`].\n retrieval_batch_size (`int`, *optional*, defaults to 8):\n Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n [`RagRetriever`].\n dataset (`str`, *optional*, defaults to `"wiki_dpr"`):\n A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n using `datasets.list_datasets()`).\n dataset_split (`str`, *optional*, defaults to `"train"`)\n Which split of the `dataset` to load.\n index_name (`str`, *optional*, defaults to `"compressed"`)\n The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and\n `"compressed"`.\n index_path (`str`, *optional*)\n The path to the serialized faiss index on disk.\n passages_path (`str`, *optional*):\n A path to text passages compatible with the faiss index. Required if using\n [`~models.rag.retrieval_rag.LegacyIndex`]\n use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n Whether to load a "dummy" variant of the dataset specified by `dataset`.\n label_smoothing (`float`, *optional*, defaults to 0.0):\n Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n in the loss calculation. If set to 0, no label smoothing is performed.\n do_marginalize (`bool`, *optional*, defaults to `False`):\n If `True`, the logits are marginalized over all documents by making use of\n `torch.nn.functional.log_softmax`.\n reduce_loss (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n do_deduplication (`bool`, *optional*, defaults to `True`):\n Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n set to `False` if used while training with distributed backend.\n exclude_bos_score (`bool`, *optional*, defaults to `False`):\n Whether or not to disregard the BOS token when computing the loss.\n output_retrieved(`bool`, *optional*, defaults to `False`):\n If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n `context_attention_mask` are returned. See returned tensors for more detail.\n use_cache (`bool`, *optional*, defaults to `True`):\n Whether or not the model should return the last key/values attentions (not used by all models).\n forced_eos_token_id (`int`, *optional*):\n The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n `eos_token_id`.\n' @add_start_docstrings(a ) class __A( a ): snake_case_ = '''rag''' snake_case_ = True def __init__( self , _snake_case=None , _snake_case=True , _snake_case=None , _snake_case=None , _snake_case=None , _snake_case=None , _snake_case=None , _snake_case=" / " , _snake_case=" // " , _snake_case=5 , _snake_case=300 , _snake_case=768 , _snake_case=8 , _snake_case="wiki_dpr" , _snake_case="train" , _snake_case="compressed" , _snake_case=None , _snake_case=None , _snake_case=False , _snake_case=False , _snake_case=0.0 , _snake_case=True , _snake_case=False , _snake_case=False , _snake_case=False , _snake_case=True , _snake_case=None , **_snake_case , ) -> Optional[Any]: '''simple docstring''' super().__init__( bos_token_id=_snake_case , pad_token_id=_snake_case , eos_token_id=_snake_case , decoder_start_token_id=_snake_case , forced_eos_token_id=_snake_case , is_encoder_decoder=_snake_case , prefix=_snake_case , vocab_size=_snake_case , **_snake_case , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" __a = kwargs.pop('''question_encoder''' ) __a = question_encoder_config.pop('''model_type''' ) __a = kwargs.pop('''generator''' ) __a = decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig __a = AutoConfig.for_model(_snake_case , **_snake_case ) __a = AutoConfig.for_model(_snake_case , **_snake_case ) __a = reduce_loss __a = label_smoothing __a = exclude_bos_score __a = do_marginalize __a = title_sep __a = doc_sep __a = n_docs __a = max_combined_length __a = dataset __a = dataset_split __a = index_name __a = retrieval_vector_size __a = retrieval_batch_size __a = passages_path __a = index_path __a = use_dummy_dataset __a = output_retrieved __a = do_deduplication __a = use_cache if self.forced_eos_token_id is None: __a = getattr(self.generator , '''forced_eos_token_id''' , _snake_case ) @classmethod def SCREAMING_SNAKE_CASE_ ( cls , _snake_case , _snake_case , **_snake_case ) -> PretrainedConfig: '''simple docstring''' return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = copy.deepcopy(self.__dict__ ) __a = self.question_encoder.to_dict() __a = self.generator.to_dict() __a = self.__class__.model_type return output
6
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : Optional[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 : int = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = [ '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 : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
1
from __future__ import annotations import collections import pprint from pathlib import Path def __lowerCAmelCase ( a__ ) -> str: return "".join(sorted(a__ ) ) def __lowerCAmelCase ( a__ ) -> list[str]: return word_by_signature[signature(a__ )] A : str = Path(__file__).parent.joinpath('words.txt').read_text(encoding='utf-8') A : List[str] = sorted({word.strip().lower() for word in data.splitlines()}) A : Tuple = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": A : Dict = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open('anagrams.txt', 'w') as file: file.write('all_anagrams = \n ') file.write(pprint.pformat(all_anagrams))
6
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( 'stable diffusion controlnet', '0.22.0', 'Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.', standard_warn=False, stacklevel=3, )
6
1
from argparse import ArgumentParser from datasets.commands.convert import ConvertCommand from datasets.commands.dummy_data import DummyDataCommand from datasets.commands.env import EnvironmentCommand from datasets.commands.run_beam import RunBeamCommand from datasets.commands.test import TestCommand from datasets.utils.logging import set_verbosity_info def __lowerCAmelCase ( a__ ) -> int: return {key.lstrip('''-''' ): value for key, value in zip(unknown_args[::2] , unknown_args[1::2] )} def __lowerCAmelCase ( ) -> Union[str, Any]: __a = ArgumentParser( '''HuggingFace Datasets CLI tool''' , usage='''datasets-cli <command> [<args>]''' , allow_abbrev=a__ ) __a = parser.add_subparsers(help='''datasets-cli command helpers''' ) set_verbosity_info() # Register commands ConvertCommand.register_subcommand(a__ ) EnvironmentCommand.register_subcommand(a__ ) TestCommand.register_subcommand(a__ ) RunBeamCommand.register_subcommand(a__ ) DummyDataCommand.register_subcommand(a__ ) # Parse args __a , __a = parser.parse_known_args() if not hasattr(a__ , '''func''' ): parser.print_help() exit(1 ) __a = parse_unknown_args(a__ ) # Run __a = args.func(a__ , **a__ ) service.run() if __name__ == "__main__": main()
6
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=a ) class __A( a ): snake_case_ = field(default='''language-modeling''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) snake_case_ = Features({'''text''': Value('''string''' )} ) snake_case_ = Features({} ) snake_case_ = "text" @property def SCREAMING_SNAKE_CASE_ ( self ) -> Dict[str, str]: '''simple docstring''' return {self.text_column: "text"}
6
1
import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## A : List[str] = 1_6 A : str = 3_2 def __lowerCAmelCase ( a__ , a__ = 16 ) -> Optional[Any]: __a = AutoTokenizer.from_pretrained('''bert-base-cased''' ) __a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(a__ ): # max_length=None => use the model max length (it's actually the default) __a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=a__ , max_length=a__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): __a = datasets.map( a__ , batched=a__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(a__ ): # On TPU it's best to pad everything to the same length or training will be very slow. __a = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": __a = 16 elif accelerator.mixed_precision != "no": __a = 8 else: __a = None return tokenizer.pad( a__ , padding='''longest''' , max_length=a__ , pad_to_multiple_of=a__ , return_tensors='''pt''' , ) # Instantiate dataloaders. __a = DataLoader( tokenized_datasets['''train'''] , shuffle=a__ , collate_fn=a__ , batch_size=a__ ) __a = DataLoader( tokenized_datasets['''validation'''] , shuffle=a__ , collate_fn=a__ , batch_size=a__ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders A : Union[str, Any] = mocked_dataloaders # noqa: F811 def __lowerCAmelCase ( a__ , a__ ) -> List[str]: # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , a__ ) == "1": __a = 2 # Initialize accelerator __a = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __a = config['''lr'''] __a = int(config['''num_epochs'''] ) __a = int(config['''seed'''] ) __a = int(config['''batch_size'''] ) __a = evaluate.load('''glue''' , '''mrpc''' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=a__ ) def inner_training_loop(a__ ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(a__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __a = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=a__ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). __a = model.to(accelerator.device ) # Instantiate optimizer __a = AdamW(params=model.parameters() , lr=a__ ) __a , __a = get_dataloaders(a__ , a__ ) # Instantiate scheduler __a = get_linear_schedule_with_warmup( optimizer=a__ , num_warmup_steps=100 , num_training_steps=(len(a__ ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __a , __a , __a , __a , __a = accelerator.prepare( a__ , a__ , a__ , a__ , a__ ) # Now we train the model for epoch in range(a__ ): model.train() for step, batch in enumerate(a__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) __a = model(**a__ ) __a = outputs.loss accelerator.backward(a__ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(a__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __a = model(**a__ ) __a = outputs.logits.argmax(dim=-1 ) __a , __a = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=a__ , references=a__ , ) __a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , a__ ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def __lowerCAmelCase ( ) -> Dict: __a = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=a__ , default=a__ , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) __a = parser.parse_args() __a = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(a__ , a__ ) if __name__ == "__main__": main()
6
import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def __lowerCAmelCase ( a__ , a__ , a__=1024 , a__=1024 , a__=False , **a__ ) -> Optional[Any]: __a = AutoTokenizer.from_pretrained(a__ ) __a = SeqaSeqDataset(a__ , a__ , a__ , a__ , type_path='''train''' , **a__ ) __a = tok.pad_token_id def get_lens(a__ ): __a = tqdm( DataLoader(a__ , batch_size=512 , num_workers=8 , shuffle=a__ , collate_fn=ds.collate_fn ) , desc=str(ds.len_file ) , ) __a = [] for batch in dl: __a = batch['''input_ids'''].ne(a__ ).sum(1 ).tolist() __a = batch['''labels'''].ne(a__ ).sum(1 ).tolist() if consider_target: for src, tgt in zip(a__ , a__ ): max_lens.append(max(a__ , a__ ) ) else: max_lens.extend(a__ ) return max_lens __a = get_lens(a__ ) __a = SeqaSeqDataset(a__ , a__ , a__ , a__ , type_path='''val''' , **a__ ) __a = get_lens(a__ ) pickle_save(a__ , train_ds.len_file ) pickle_save(a__ , val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
6
1
from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 A : Any = { # 1536-bit 5: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 2048-bit 1_4: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AACAA68FFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 3072-bit 1_5: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 4096-bit 1_6: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199' + 'FFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 6144-bit 1_7: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08' + '8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B' + '302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9' + 'A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6' + '49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8' + 'FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C' + '180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' + '3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D' + '04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D' + 'B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226' + '1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC' + 'E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26' + '99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB' + '04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2' + '233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127' + 'D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406' + 'AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918' + 'DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151' + '2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03' + 'F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F' + 'BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B' + 'B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632' + '387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E' + '6DCC4024FFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, # 8192-bit 1_8: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD' + 'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831' + '179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B' + 'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF' + '5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6' + 'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3' + '23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328' + '06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C' + 'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE' + '12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4' + '38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300' + '741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568' + '3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9' + '22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B' + '4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A' + '062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36' + '4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1' + 'B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92' + '4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47' + '9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71' + '60C980DD98EDD3DFFFFFFFFFFFFFFFFF', base=1_6, ), 'generator': 2, }, } class __A: def __init__( self , _snake_case = 14 ) -> None: '''simple docstring''' if group not in primes: raise ValueError('''Unsupported Group''' ) __a = primes[group]['''prime'''] __a = primes[group]['''generator'''] __a = int(hexlify(urandom(32 ) ) , base=16 ) def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' return hex(self.__private_key )[2:] def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' __a = pow(self.generator , self.__private_key , self.prime ) return hex(_snake_case )[2:] def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> bool: '''simple docstring''' return ( 2 <= key <= self.prime - 2 and pow(_snake_case , (self.prime - 1) // 2 , self.prime ) == 1 ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> str: '''simple docstring''' __a = int(_snake_case , base=16 ) if not self.is_valid_public_key(_snake_case ): raise ValueError('''Invalid public key''' ) __a = pow(_snake_case , self.__private_key , self.prime ) return shaaaa(str(_snake_case ).encode() ).hexdigest() @staticmethod def SCREAMING_SNAKE_CASE_ ( _snake_case , _snake_case ) -> bool: '''simple docstring''' return ( 2 <= remote_public_key_str <= prime - 2 and pow(_snake_case , (prime - 1) // 2 , _snake_case ) == 1 ) @staticmethod def SCREAMING_SNAKE_CASE_ ( _snake_case , _snake_case , _snake_case = 14 ) -> str: '''simple docstring''' __a = int(_snake_case , base=16 ) __a = int(_snake_case , base=16 ) __a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(_snake_case , _snake_case ): raise ValueError('''Invalid public key''' ) __a = pow(_snake_case , _snake_case , _snake_case ) return shaaaa(str(_snake_case ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
6
from math import cos, sin, sqrt, tau from audio_filters.iir_filter import IIRFilter def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = (1 - _cos) / 2 __a = 1 - _cos __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = (1 + _cos) / 2 __a = -1 - _cos __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = _sin / 2 __a = 0 __a = -ba __a = 1 + alpha __a = -2 * _cos __a = 1 - alpha __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ = 1 / sqrt(2 ) ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 1 - alpha __a = -2 * _cos __a = 1 + alpha __a = IIRFilter(2 ) filt.set_coefficients([ba, ba, ba] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = 1 + alpha * big_a __a = -2 * _cos __a = 1 - alpha * big_a __a = 1 + alpha / big_a __a = -2 * _cos __a = 1 - alpha / big_a __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = (big_a + 1) - (big_a - 1) * _cos __a = (big_a + 1) + (big_a - 1) * _cos __a = (big_a - 1) - (big_a + 1) * _cos __a = (big_a - 1) + (big_a + 1) * _cos __a = 2 * sqrt(a__ ) * alpha __a = big_a * (pmc + aaa) __a = 2 * big_a * mpc __a = big_a * (pmc - aaa) __a = ppmc + aaa __a = -2 * pmpc __a = ppmc - aaa __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def __lowerCAmelCase ( a__ , a__ , a__ , a__ = 1 / sqrt(2 ) , ) -> IIRFilter: __a = tau * frequency / samplerate __a = sin(a__ ) __a = cos(a__ ) __a = _sin / (2 * q_factor) __a = 10 ** (gain_db / 40) __a = (big_a + 1) - (big_a - 1) * _cos __a = (big_a + 1) + (big_a - 1) * _cos __a = (big_a - 1) - (big_a + 1) * _cos __a = (big_a - 1) + (big_a + 1) * _cos __a = 2 * sqrt(a__ ) * alpha __a = big_a * (ppmc + aaa) __a = -2 * big_a * pmpc __a = big_a * (ppmc - aaa) __a = pmc + aaa __a = 2 * mpc __a = pmc - aaa __a = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt
6
1
import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __A( a ): snake_case_ = ['''image_processor''', '''tokenizer'''] snake_case_ = '''AutoImageProcessor''' snake_case_ = '''AutoTokenizer''' def __init__( self , _snake_case=None , _snake_case=None , **_snake_case ) -> str: '''simple docstring''' __a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _snake_case , ) __a = kwargs.pop('''feature_extractor''' ) __a = 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__(_snake_case , _snake_case ) __a = self.image_processor __a = False def __call__( self , *_snake_case , **_snake_case ) -> Optional[int]: '''simple docstring''' if self._in_target_context_manager: return self.current_processor(*_snake_case , **_snake_case ) __a = kwargs.pop('''images''' , _snake_case ) __a = kwargs.pop('''text''' , _snake_case ) if len(_snake_case ) > 0: __a = args[0] __a = args[1:] 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: __a = self.image_processor(_snake_case , *_snake_case , **_snake_case ) if text is not None: __a = self.tokenizer(_snake_case , **_snake_case ) if text is None: return inputs elif images is None: return encodings else: __a = encodings['''input_ids'''] return inputs def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> List[str]: '''simple docstring''' return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> int: '''simple docstring''' return self.tokenizer.decode(*_snake_case , **_snake_case ) @contextmanager def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''' ) __a = True __a = self.tokenizer yield __a = self.image_processor __a = False def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case=False , _snake_case=None ) -> int: '''simple docstring''' if added_vocab is None: __a = self.tokenizer.get_added_vocab() __a = {} while tokens: __a = re.search(r'''<s_(.*?)>''' , _snake_case , re.IGNORECASE ) if start_token is None: break __a = start_token.group(1 ) __a = re.search(rF"""</s_{key}>""" , _snake_case , re.IGNORECASE ) __a = start_token.group() if end_token is None: __a = tokens.replace(_snake_case , '''''' ) else: __a = end_token.group() __a = re.escape(_snake_case ) __a = re.escape(_snake_case ) __a = re.search(F"""{start_token_escaped}(.*?){end_token_escaped}""" , _snake_case , re.IGNORECASE ) if content is not None: __a = content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node __a = self.tokenajson(_snake_case , is_inner_value=_snake_case , added_vocab=_snake_case ) if value: if len(_snake_case ) == 1: __a = value[0] __a = value else: # leaf nodes __a = [] for leaf in content.split(r'''<sep/>''' ): __a = leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": __a = leaf[1:-2] # for categorical special tokens output[key].append(_snake_case ) if len(output[key] ) == 1: __a = output[key][0] __a = tokens[tokens.find(_snake_case ) + len(_snake_case ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:] , is_inner_value=_snake_case , added_vocab=_snake_case ) if len(_snake_case ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _snake_case , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , _snake_case , ) return self.image_processor
6
def __lowerCAmelCase ( a__ , a__ , a__ ) -> list: __a = len(a__ ) __a = [[0] * n for i in range(a__ )] for i in range(a__ ): __a = y_points[i] for i in range(2 , a__ ): for j in range(a__ , a__ ): __a = ( (xa - x_points[j - i + 1]) * q[j][i - 1] - (xa - x_points[j]) * q[j - 1][i - 1] ) / (x_points[j] - x_points[j - i + 1]) return [q[n - 1][n - 1], q] if __name__ == "__main__": import doctest doctest.testmod()
6
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging A : Tuple = logging.get_logger(__name__) if is_vision_available(): import PIL class __A( a ): snake_case_ = ['''pixel_values'''] def __init__( self , _snake_case = True , _snake_case = None , _snake_case = PILImageResampling.BICUBIC , _snake_case = True , _snake_case = None , _snake_case = True , _snake_case = 1 / 255 , _snake_case = True , _snake_case = None , _snake_case = None , _snake_case = True , **_snake_case , ) -> None: '''simple docstring''' super().__init__(**_snake_case ) __a = size if size is not None else {'''shortest_edge''': 224} __a = get_size_dict(_snake_case , default_to_square=_snake_case ) __a = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} __a = get_size_dict(_snake_case , default_to_square=_snake_case , param_name='''crop_size''' ) __a = do_resize __a = size __a = resample __a = do_center_crop __a = crop_size __a = do_rescale __a = rescale_factor __a = do_normalize __a = image_mean if image_mean is not None else OPENAI_CLIP_MEAN __a = image_std if image_std is not None else OPENAI_CLIP_STD __a = do_convert_rgb def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = PILImageResampling.BICUBIC , _snake_case = None , **_snake_case , ) -> np.ndarray: '''simple docstring''' __a = get_size_dict(_snake_case , default_to_square=_snake_case ) if "shortest_edge" not in size: raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""" ) __a = get_resize_output_image_size(_snake_case , size=size['''shortest_edge'''] , default_to_square=_snake_case ) return resize(_snake_case , size=_snake_case , resample=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = None , **_snake_case , ) -> np.ndarray: '''simple docstring''' __a = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` parameter must contain the keys (height, width). Got {size.keys()}""" ) return center_crop(_snake_case , size=(size['''height'''], size['''width''']) , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case = None , **_snake_case , ) -> Tuple: '''simple docstring''' return rescale(_snake_case , scale=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case , _snake_case = None , **_snake_case , ) -> np.ndarray: '''simple docstring''' return normalize(_snake_case , mean=_snake_case , std=_snake_case , data_format=_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = ChannelDimension.FIRST , **_snake_case , ) -> PIL.Image.Image: '''simple docstring''' __a = do_resize if do_resize is not None else self.do_resize __a = size if size is not None else self.size __a = get_size_dict(_snake_case , param_name='''size''' , default_to_square=_snake_case ) __a = resample if resample is not None else self.resample __a = do_center_crop if do_center_crop is not None else self.do_center_crop __a = crop_size if crop_size is not None else self.crop_size __a = get_size_dict(_snake_case , param_name='''crop_size''' , default_to_square=_snake_case ) __a = do_rescale if do_rescale is not None else self.do_rescale __a = rescale_factor if rescale_factor is not None else self.rescale_factor __a = do_normalize if do_normalize is not None else self.do_normalize __a = image_mean if image_mean is not None else self.image_mean __a = image_std if image_std is not None else self.image_std __a = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb __a = make_list_of_images(_snake_case ) if not valid_images(_snake_case ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: __a = [convert_to_rgb(_snake_case ) for image in images] # All transformations expect numpy arrays. __a = [to_numpy_array(_snake_case ) for image in images] if do_resize: __a = [self.resize(image=_snake_case , size=_snake_case , resample=_snake_case ) for image in images] if do_center_crop: __a = [self.center_crop(image=_snake_case , size=_snake_case ) for image in images] if do_rescale: __a = [self.rescale(image=_snake_case , scale=_snake_case ) for image in images] if do_normalize: __a = [self.normalize(image=_snake_case , mean=_snake_case , std=_snake_case ) for image in images] __a = [to_channel_dimension_format(_snake_case , _snake_case ) for image in images] __a = {'''pixel_values''': images} return BatchFeature(data=_snake_case , tensor_type=_snake_case )
6
from __future__ import annotations import time from collections.abc import Sequence from random import randint from matplotlib import pyplot as plt def __lowerCAmelCase ( a__ , a__ , a__ ) -> tuple[int | None, int | None, float]: if not arr: return None, None, 0 if low == high: return low, high, arr[low] __a = (low + high) // 2 __a , __a , __a = max_subarray(a__ , a__ , a__ ) __a , __a , __a = max_subarray(a__ , mid + 1 , a__ ) __a , __a , __a = max_cross_sum(a__ , a__ , a__ , a__ ) if left_sum >= right_sum and left_sum >= cross_sum: return left_low, left_high, left_sum elif right_sum >= left_sum and right_sum >= cross_sum: return right_low, right_high, right_sum return cross_left, cross_right, cross_sum def __lowerCAmelCase ( a__ , a__ , a__ , a__ ) -> tuple[int, int, float]: __a , __a = float('''-inf''' ), -1 __a , __a = float('''-inf''' ), -1 __a = 0 for i in range(a__ , low - 1 , -1 ): summ += arr[i] if summ > left_sum: __a = summ __a = i __a = 0 for i in range(mid + 1 , high + 1 ): summ += arr[i] if summ > right_sum: __a = summ __a = i return max_left, max_right, (left_sum + right_sum) def __lowerCAmelCase ( a__ ) -> float: __a = [randint(1 , a__ ) for _ in range(a__ )] __a = time.time() max_subarray(a__ , 0 , input_size - 1 ) __a = time.time() return end - start def __lowerCAmelCase ( ) -> None: __a = [10, 100, 1000, 1_0000, 5_0000, 10_0000, 20_0000, 30_0000, 40_0000, 50_0000] __a = [time_max_subarray(a__ ) for input_size in input_sizes] print('''No of Inputs\t\tTime Taken''' ) for input_size, runtime in zip(a__ , a__ ): print(a__ , '''\t\t''' , a__ ) plt.plot(a__ , a__ ) plt.xlabel('''Number of Inputs''' ) plt.ylabel('''Time taken in seconds''' ) plt.show() if __name__ == "__main__": from doctest import testmod testmod()
6
1
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class __A( a ): snake_case_ = (DPMSolverSDEScheduler,) snake_case_ = 1_0 def SCREAMING_SNAKE_CASE_ ( self , **_snake_case ) -> Union[str, Any]: '''simple docstring''' __a = { '''num_train_timesteps''': 1_100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''noise_sampler_seed''': 0, } config.update(**_snake_case ) return config def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_snake_case , beta_end=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps ) __a = self.dummy_model() __a = self.dummy_sample_deter * scheduler.init_noise_sigma __a = sample.to(_snake_case ) for i, t in enumerate(scheduler.timesteps ): __a = scheduler.scale_model_input(_snake_case , _snake_case ) __a = model(_snake_case , _snake_case ) __a = scheduler.step(_snake_case , _snake_case , _snake_case ) __a = output.prev_sample __a = torch.sum(torch.abs(_snake_case ) ) __a = torch.mean(torch.abs(_snake_case ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47_8210_4492_1875 ) < 1E-2 assert abs(result_mean.item() - 0.2178_7059_6456_5277 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59_3521_1181_6406 ) < 1E-2 assert abs(result_mean.item() - 0.2_2342_9068_9229_9652 ) < 1E-3 else: assert abs(result_sum.item() - 162.52_3834_2285_1562 ) < 1E-2 assert abs(result_mean.item() - 0.211_6195_7085_1326 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' __a = self.scheduler_classes[0] __a = self.get_scheduler_config(prediction_type='''v_prediction''' ) __a = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps ) __a = self.dummy_model() __a = self.dummy_sample_deter * scheduler.init_noise_sigma __a = sample.to(_snake_case ) for i, t in enumerate(scheduler.timesteps ): __a = scheduler.scale_model_input(_snake_case , _snake_case ) __a = model(_snake_case , _snake_case ) __a = scheduler.step(_snake_case , _snake_case , _snake_case ) __a = output.prev_sample __a = torch.sum(torch.abs(_snake_case ) ) __a = torch.mean(torch.abs(_snake_case ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77_1492_0043_9453 ) < 1E-2 assert abs(result_mean.item() - 0.1_6226_2890_1481_6284 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1_6633_6059_5703 ) < 1E-2 assert abs(result_mean.item() - 0.1_6688_3260_0116_7297 ) < 1E-3 else: assert abs(result_sum.item() - 119.8_4875_4882_8125 ) < 1E-2 assert abs(result_mean.item() - 0.1560_5306_6253_6621 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps , device=_snake_case ) __a = self.dummy_model() __a = self.dummy_sample_deter.to(_snake_case ) * scheduler.init_noise_sigma for t in scheduler.timesteps: __a = scheduler.scale_model_input(_snake_case , _snake_case ) __a = model(_snake_case , _snake_case ) __a = scheduler.step(_snake_case , _snake_case , _snake_case ) __a = output.prev_sample __a = torch.sum(torch.abs(_snake_case ) ) __a = torch.mean(torch.abs(_snake_case ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46_9573_9746_0938 ) < 1E-2 assert abs(result_mean.item() - 0.2_1805_9346_0798_2635 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59_3536_3769_5312 ) < 1E-2 assert abs(result_mean.item() - 0.2_2342_9083_8241_5771 ) < 1E-3 else: assert abs(result_sum.item() - 162.52_3834_2285_1562 ) < 1E-2 assert abs(result_mean.item() - 0.211_6195_7085_1326 ) < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_snake_case , use_karras_sigmas=_snake_case ) scheduler.set_timesteps(self.num_inference_steps , device=_snake_case ) __a = self.dummy_model() __a = self.dummy_sample_deter.to(_snake_case ) * scheduler.init_noise_sigma __a = sample.to(_snake_case ) for t in scheduler.timesteps: __a = scheduler.scale_model_input(_snake_case , _snake_case ) __a = model(_snake_case , _snake_case ) __a = scheduler.step(_snake_case , _snake_case , _snake_case ) __a = output.prev_sample __a = torch.sum(torch.abs(_snake_case ) ) __a = torch.mean(torch.abs(_snake_case ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66_9741_3574_2188 ) < 1E-2 assert abs(result_mean.item() - 0.2_3003_8727_3098_1811 ) < 1E-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63_6535_6445_3125 ) < 1E-2 assert abs(result_mean.item() - 0.2_3003_8727_3098_1811 ) < 1E-2 else: assert abs(result_sum.item() - 170.3_1352_2338_8672 ) < 1E-2 assert abs(result_mean.item() - 0.2_3003_8727_3098_1811 ) < 1E-2
6
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __A( a , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class __A( unittest.TestCase ): @property def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = ort.SessionOptions() __a = False return options def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) __a = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) __a = '''A red cat sitting on a park bench''' __a = np.random.RandomState(0 ) __a = pipe( prompt=_snake_case , image=_snake_case , mask_image=_snake_case , guidance_scale=7.5 , num_inference_steps=10 , generator=_snake_case , output_type='''np''' , ) __a = output.images __a = images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) __a = np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) __a = LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) __a = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=_snake_case , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) __a = '''A red cat sitting on a park bench''' __a = np.random.RandomState(0 ) __a = pipe( prompt=_snake_case , image=_snake_case , mask_image=_snake_case , guidance_scale=7.5 , num_inference_steps=20 , generator=_snake_case , output_type='''np''' , ) __a = output.images __a = images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) __a = np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3
6
1
from argparse import ArgumentParser, Namespace from typing import Any, List, Optional from ..pipelines import Pipeline, get_supported_tasks, pipeline from ..utils import logging from . import BaseTransformersCLICommand try: from fastapi import Body, FastAPI, HTTPException from fastapi.routing import APIRoute from pydantic import BaseModel from starlette.responses import JSONResponse from uvicorn import run A : Optional[int] = True except (ImportError, AttributeError): A : Any = object def __lowerCAmelCase ( *a__ , **a__ ) -> Any: pass A : Optional[Any] = False A : Optional[int] = logging.get_logger('transformers-cli/serving') def __lowerCAmelCase ( a__ ) -> Any: __a = pipeline( task=args.task , model=args.model if args.model else None , config=args.config , tokenizer=args.tokenizer , device=args.device , ) return ServeCommand(a__ , args.host , args.port , args.workers ) class __A( a ): snake_case_ = 42 class __A( a ): snake_case_ = 42 snake_case_ = 42 class __A( a ): snake_case_ = 42 class __A( a ): snake_case_ = 42 class __A( a ): @staticmethod def SCREAMING_SNAKE_CASE_ ( _snake_case ) -> Optional[int]: '''simple docstring''' __a = parser.add_parser( '''serve''' , help='''CLI tool to run inference requests through REST and GraphQL endpoints.''' ) serve_parser.add_argument( '''--task''' , type=_snake_case , choices=get_supported_tasks() , help='''The task to run the pipeline on''' , ) serve_parser.add_argument('''--host''' , type=_snake_case , default='''localhost''' , help='''Interface the server will listen on.''' ) serve_parser.add_argument('''--port''' , type=_snake_case , default=8_888 , help='''Port the serving will listen to.''' ) serve_parser.add_argument('''--workers''' , type=_snake_case , default=1 , help='''Number of http workers''' ) serve_parser.add_argument('''--model''' , type=_snake_case , help='''Model\'s name or path to stored model.''' ) serve_parser.add_argument('''--config''' , type=_snake_case , help='''Model\'s config name or path to stored model.''' ) serve_parser.add_argument('''--tokenizer''' , type=_snake_case , help='''Tokenizer name to use.''' ) serve_parser.add_argument( '''--device''' , type=_snake_case , default=-1 , help='''Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)''' , ) serve_parser.set_defaults(func=_snake_case ) def __init__( self , _snake_case , _snake_case , _snake_case , _snake_case ) -> Any: '''simple docstring''' __a = pipeline __a = host __a = port __a = workers if not _serve_dependencies_installed: raise RuntimeError( '''Using serve command requires FastAPI and uvicorn. ''' '''Please install transformers with [serving]: pip install "transformers[serving]".''' '''Or install FastAPI and uvicorn separately.''' ) else: logger.info(F"""Serving model over {host}:{port}""" ) __a = FastAPI( routes=[ APIRoute( '''/''' , self.model_info , response_model=_snake_case , response_class=_snake_case , methods=['''GET'''] , ), APIRoute( '''/tokenize''' , self.tokenize , response_model=_snake_case , response_class=_snake_case , methods=['''POST'''] , ), APIRoute( '''/detokenize''' , self.detokenize , response_model=_snake_case , response_class=_snake_case , methods=['''POST'''] , ), APIRoute( '''/forward''' , self.forward , response_model=_snake_case , response_class=_snake_case , methods=['''POST'''] , ), ] , timeout=600 , ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' run(self._app , host=self.host , port=self.port , workers=self.workers ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' return ServeModelInfoResult(infos=vars(self._pipeline.model.config ) ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case = Body(_snake_case , embed=_snake_case ) , _snake_case = Body(_snake_case , embed=_snake_case ) ) -> str: '''simple docstring''' try: __a = self._pipeline.tokenizer.tokenize(_snake_case ) if return_ids: __a = self._pipeline.tokenizer.convert_tokens_to_ids(_snake_case ) return ServeTokenizeResult(tokens=_snake_case , tokens_ids=_snake_case ) else: return ServeTokenizeResult(tokens=_snake_case ) except Exception as e: raise HTTPException(status_code=500 , detail={'''model''': '''''', '''error''': str(_snake_case )} ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case = Body(_snake_case , embed=_snake_case ) , _snake_case = Body(_snake_case , embed=_snake_case ) , _snake_case = Body(_snake_case , embed=_snake_case ) , ) -> str: '''simple docstring''' try: __a = self._pipeline.tokenizer.decode(_snake_case , _snake_case , _snake_case ) return ServeDeTokenizeResult(model='''''' , text=_snake_case ) except Exception as e: raise HTTPException(status_code=500 , detail={'''model''': '''''', '''error''': str(_snake_case )} ) async def SCREAMING_SNAKE_CASE_ ( self , _snake_case=Body(_snake_case , embed=_snake_case ) ) -> List[str]: '''simple docstring''' if len(_snake_case ) == 0: return ServeForwardResult(output=[] , attention=[] ) try: # Forward through the model __a = self._pipeline(_snake_case ) return ServeForwardResult(output=_snake_case ) except Exception as e: raise HTTPException(500 , {'''error''': str(_snake_case )} )
6
from math import ceil def __lowerCAmelCase ( a__ = 1001 ) -> int: __a = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): __a = 2 * i + 1 __a = 2 * i __a = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[Any] = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number')
6
1
from scipy.stats import pearsonr import datasets A : List[Any] = '\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n' A : int = '\nArgs:\n predictions (`list` of `int`): Predicted class labels, as returned by a model.\n references (`list` of `int`): Ground truth labels.\n return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n Example 1-A simple example using only predictions and references.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n\n Example 2-The same as Example 1, but that also returns the `p-value`.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n >>> print(sorted(list(results.keys())))\n [\'p-value\', \'pearsonr\']\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n >>> print(round(results[\'p-value\'], 2))\n 0.15\n' A : int = '\n@article{2020SciPy-NMeth,\nauthor = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Antonio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\njournal = {Nature Methods},\nyear = {2020},\nvolume = {17},\npages = {261--272},\nadsurl = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A( datasets.Metric ): def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) , reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'''] , ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case , _snake_case=False ) -> Dict: '''simple docstring''' if return_pvalue: __a = pearsonr(_snake_case , _snake_case ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(_snake_case , _snake_case )[0] )}
6
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __A( a ): snake_case_ = ['''image_processor''', '''tokenizer'''] snake_case_ = '''ChineseCLIPImageProcessor''' snake_case_ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self , _snake_case=None , _snake_case=None , **_snake_case ) -> Tuple: '''simple docstring''' __a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _snake_case , ) __a = kwargs.pop('''feature_extractor''' ) __a = 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__(_snake_case , _snake_case ) __a = self.image_processor def __call__( self , _snake_case=None , _snake_case=None , _snake_case=None , **_snake_case ) -> Optional[Any]: '''simple docstring''' 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: __a = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) if images is not None: __a = self.image_processor(_snake_case , return_tensors=_snake_case , **_snake_case ) if text is not None and images is not None: __a = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**_snake_case ) , tensor_type=_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , *_snake_case , **_snake_case ) -> Dict: '''simple docstring''' return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' __a = self.tokenizer.model_input_names __a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _snake_case , ) return self.image_processor_class
6
1
class __A: def __init__( self ) -> List[str]: '''simple docstring''' __a = '''''' __a = '''''' __a = [] def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> int: '''simple docstring''' if m == -1: return n + 1 elif n == -1: return m + 1 elif self.dp[m][n] > -1: return self.dp[m][n] else: if self.worda[m] == self.worda[n]: __a = self.__min_dist_top_down_dp(m - 1 , n - 1 ) else: __a = self.__min_dist_top_down_dp(_snake_case , n - 1 ) __a = self.__min_dist_top_down_dp(m - 1 , _snake_case ) __a = self.__min_dist_top_down_dp(m - 1 , n - 1 ) __a = 1 + min(_snake_case , _snake_case , _snake_case ) return self.dp[m][n] def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> int: '''simple docstring''' __a = worda __a = worda __a = [[-1 for _ in range(len(_snake_case ) )] for _ in range(len(_snake_case ) )] return self.__min_dist_top_down_dp(len(_snake_case ) - 1 , len(_snake_case ) - 1 ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> int: '''simple docstring''' __a = worda __a = worda __a = len(_snake_case ) __a = len(_snake_case ) __a = [[0 for _ in range(n + 1 )] for _ in range(m + 1 )] for i in range(m + 1 ): for j in range(n + 1 ): if i == 0: # first string is empty __a = j elif j == 0: # second string is empty __a = i elif worda[i - 1] == worda[j - 1]: # last characters are equal __a = self.dp[i - 1][j - 1] else: __a = self.dp[i][j - 1] __a = self.dp[i - 1][j] __a = self.dp[i - 1][j - 1] __a = 1 + min(_snake_case , _snake_case , _snake_case ) return self.dp[m][n] if __name__ == "__main__": A : Dict = EditDistance() print('****************** Testing Edit Distance DP Algorithm ******************') print() A : List[Any] = input('Enter the first string: ').strip() A : Union[str, Any] = input('Enter the second string: ').strip() print() print(F"The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}") print(F"The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}") print() print('*************** End of Testing Edit Distance DP Algorithm ***************')
6
from __future__ import annotations import typing from collections import Counter def __lowerCAmelCase ( a__ ) -> typing.Counter[int]: __a = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(a__ , max_perimeter + 1 ): __a = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(a__ ): __a = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def __lowerCAmelCase ( a__ = 1000 ) -> int: __a = pythagorean_triple(a__ ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(F"Perimeter {solution()} has maximum solutions")
6
1
def __lowerCAmelCase ( a__ ) -> str: __a = [] __a = set({'''(''', '''[''', '''{'''} ) __a = set({''')''', ''']''', '''}'''} ) __a = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(a__ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(a__ ) == 0 or (len(a__ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(a__ ) == 0 def __lowerCAmelCase ( ) -> Dict: __a = input('''Enter sequence of brackets: ''' ) if is_balanced(a__ ): print(a__ , '''is balanced''' ) else: print(a__ , '''is not balanced''' ) if __name__ == "__main__": main()
6
# flake8: noqa # Lint as: python3 A : Optional[Any] = [ 'VerificationMode', 'Version', 'disable_progress_bar', 'enable_progress_bar', 'is_progress_bar_enabled', 'experimental', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
6
1
from __future__ import annotations import numpy as np def __lowerCAmelCase ( a__ ) -> tuple[np.ndarray, np.ndarray]: __a , __a = np.shape(a__ ) if rows != columns: __a = ( '''\'table\' has to be of square shaped array but got a ''' F"""{rows}x{columns} array:\n{table}""" ) raise ValueError(a__ ) __a = np.zeros((rows, columns) ) __a = np.zeros((rows, columns) ) for i in range(a__ ): for j in range(a__ ): __a = sum(lower[i][k] * upper[k][j] for k in range(a__ ) ) if upper[j][j] == 0: raise ArithmeticError('''No LU decomposition exists''' ) __a = (table[i][j] - total) / upper[j][j] __a = 1 for j in range(a__ , a__ ): __a = sum(lower[i][k] * upper[k][j] for k in range(a__ ) ) __a = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
6
from typing import Dict from .base import GenericTensor, Pipeline class __A( a ): def SCREAMING_SNAKE_CASE_ ( self , _snake_case=None , _snake_case=None , _snake_case=None , **_snake_case ) -> Optional[Any]: '''simple docstring''' if tokenize_kwargs is None: __a = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( '''truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)''' ) __a = truncation __a = tokenize_kwargs __a = {} if return_tensors is not None: __a = return_tensors return preprocess_params, {}, postprocess_params def SCREAMING_SNAKE_CASE_ ( self , _snake_case , **_snake_case ) -> Dict[str, GenericTensor]: '''simple docstring''' __a = self.framework __a = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Optional[Any]: '''simple docstring''' __a = self.model(**_snake_case ) return model_outputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case=False ) -> Optional[int]: '''simple docstring''' if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self , *_snake_case , **_snake_case ) -> Any: '''simple docstring''' return super().__call__(*_snake_case , **_snake_case )
6
1