code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" import os def lowercase ( ) -> Union[str, Any]: __a = os.path.dirname(os.path.realpath(lowerCAmelCase__ ) ) __a = os.path.join(lowerCAmelCase__ , '''triangle.txt''' ) with open(lowerCAmelCase__ ) as f: __a = f.readlines() __a = [] for line in triangle: __a = [] for number in line.strip().split(''' ''' ): numbers_from_line.append(int(lowerCAmelCase__ ) ) a.append(lowerCAmelCase__ ) for i in range(1 , len(lowerCAmelCase__ ) ): for j in range(len(a[i] ) ): __a = a[i - 1][j] if j != len(a[i - 1] ) else 0 __a = a[i - 1][j - 1] if j > 0 else 0 a[i][j] += max(lowerCAmelCase__ , lowerCAmelCase__ ) return max(a[-1] ) if __name__ == "__main__": print(solution())
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str ) -> list: if n_term == "": return [] __a = [] for temp in range(int(lowerCAmelCase__ ) ): series.append(f'''1/{temp + 1}''' if series else '''1''' ) return series if __name__ == "__main__": lowercase_ = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
45
1
"""simple docstring""" from __future__ import annotations import math __UpperCamelCase = '''2020.9.26''' __UpperCamelCase = '''xcodz-dot, cclaus, dhruvmanila''' def UpperCAmelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> tuple[float, float]: if not all(isinstance(UpperCAmelCase , (float, int) ) for val in locals().values() ): snake_case_ = f'Input values must either be float or int: {list(locals().values() )}' raise TypeError(UpperCAmelCase ) snake_case_ = ((x * distance) / (z + distance)) * scale snake_case_ = ((y * distance) / (z + distance)) * scale return projected_x, projected_y def UpperCAmelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> tuple[float, float, float]: if not isinstance(UpperCAmelCase , UpperCAmelCase ): raise TypeError('Axis must be a str' ) snake_case_ = locals() del input_variables["axis"] if not all(isinstance(UpperCAmelCase , (float, int) ) for val in input_variables.values() ): snake_case_ = ( 'Input values except axis must either be float or int: ' f'{list(input_variables.values() )}' ) raise TypeError(UpperCAmelCase ) snake_case_ = (angle % 360) / 450 * 180 / math.pi if axis == "z": snake_case_ = x * math.cos(UpperCAmelCase ) - y * math.sin(UpperCAmelCase ) snake_case_ = y * math.cos(UpperCAmelCase ) + x * math.sin(UpperCAmelCase ) snake_case_ = z elif axis == "x": snake_case_ = y * math.cos(UpperCAmelCase ) - z * math.sin(UpperCAmelCase ) snake_case_ = z * math.cos(UpperCAmelCase ) + y * math.sin(UpperCAmelCase ) snake_case_ = x elif axis == "y": snake_case_ = x * math.cos(UpperCAmelCase ) - z * math.sin(UpperCAmelCase ) snake_case_ = z * math.cos(UpperCAmelCase ) + x * math.sin(UpperCAmelCase ) snake_case_ = y else: raise ValueError('not a valid axis, choose one of \'x\', \'y\', \'z\'' ) return new_x, new_y, new_z if __name__ == "__main__": import doctest doctest.testmod() print(F"""{convert_to_ad(1.0, 2.0, 3.0, 10.0, 10.0) = }""") print(F"""{rotate(1.0, 2.0, 3.0, "y", 90.0) = }""")
312
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mvp import MvpTokenizer __UpperCamelCase = logging.get_logger(__name__) __UpperCamelCase = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} # See all MVP models at https://huggingface.co/models?filter=mvp __UpperCamelCase = { '''vocab_file''': { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/vocab.json''', }, '''added_tokens.json''': { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/added_tokens.json''', }, '''merges_file''': { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/tokenizer.json''', }, } __UpperCamelCase = { '''RUCAIBox/mvp''': 1024, } class UpperCamelCase ( lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE_ = ["input_ids", "attention_mask"] SCREAMING_SNAKE_CASE_ = MvpTokenizer def __init__( self, lowerCAmelCase__=None, lowerCAmelCase__=None, lowerCAmelCase__=None, lowerCAmelCase__="replace", lowerCAmelCase__="<s>", lowerCAmelCase__="</s>", lowerCAmelCase__="</s>", lowerCAmelCase__="<s>", lowerCAmelCase__="<unk>", lowerCAmelCase__="<pad>", lowerCAmelCase__="<mask>", lowerCAmelCase__=False, lowerCAmelCase__=True, **lowerCAmelCase__, ) -> Union[str, Any]: super().__init__( lowerCAmelCase__, lowerCAmelCase__, tokenizer_file=lowerCAmelCase__, errors=lowerCAmelCase__, bos_token=lowerCAmelCase__, eos_token=lowerCAmelCase__, sep_token=lowerCAmelCase__, cls_token=lowerCAmelCase__, unk_token=lowerCAmelCase__, pad_token=lowerCAmelCase__, mask_token=lowerCAmelCase__, add_prefix_space=lowerCAmelCase__, trim_offsets=lowerCAmelCase__, **lowerCAmelCase__, ) snake_case_ = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()) if pre_tok_state.get('add_prefix_space', lowerCAmelCase__) != add_prefix_space: snake_case_ = getattr(lowerCAmelCase__, pre_tok_state.pop('type')) snake_case_ = add_prefix_space snake_case_ = pre_tok_class(**lowerCAmelCase__) snake_case_ = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` snake_case_ = 'post_processor' snake_case_ = getattr(self.backend_tokenizer, lowerCAmelCase__, lowerCAmelCase__) if tokenizer_component_instance: snake_case_ = json.loads(tokenizer_component_instance.__getstate__()) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: snake_case_ = tuple(state['sep']) if "cls" in state: snake_case_ = tuple(state['cls']) snake_case_ = False if state.get('add_prefix_space', lowerCAmelCase__) != add_prefix_space: snake_case_ = add_prefix_space snake_case_ = True if state.get('trim_offsets', lowerCAmelCase__) != trim_offsets: snake_case_ = trim_offsets snake_case_ = True if changes_to_apply: snake_case_ = getattr(lowerCAmelCase__, state.pop('type')) snake_case_ = component_class(**lowerCAmelCase__) setattr(self.backend_tokenizer, lowerCAmelCase__, lowerCAmelCase__) @property def a_ ( self) -> str: if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.') return None return str(self._mask_token) @mask_token.setter def a_ ( self, lowerCAmelCase__) -> Any: snake_case_ = AddedToken(lowerCAmelCase__, lstrip=lowerCAmelCase__, rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__, lowerCAmelCase__) else value snake_case_ = value def a_ ( self, *lowerCAmelCase__, **lowerCAmelCase__) -> BatchEncoding: snake_case_ = kwargs.get('is_split_into_words', lowerCAmelCase__) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' 'to use it with pretokenized inputs.') return super()._batch_encode_plus(*lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, *lowerCAmelCase__, **lowerCAmelCase__) -> BatchEncoding: snake_case_ = kwargs.get('is_split_into_words', lowerCAmelCase__) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' 'to use it with pretokenized inputs.') return super()._encode_plus(*lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__ = None) -> Tuple[str]: snake_case_ = self._tokenizer.model.save(lowerCAmelCase__, name=lowerCAmelCase__) return tuple(lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__=None) -> str: snake_case_ = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def a_ ( self, lowerCAmelCase__, lowerCAmelCase__ = None) -> List[int]: snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0]
312
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowercase__ : int = { 'configuration_conditional_detr': [ 'CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConditionalDetrConfig', 'ConditionalDetrOnnxConfig', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : str = ['ConditionalDetrFeatureExtractor'] lowercase__ : Any = ['ConditionalDetrImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Any = [ 'CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST', 'ConditionalDetrForObjectDetection', 'ConditionalDetrForSegmentation', 'ConditionalDetrModel', 'ConditionalDetrPreTrainedModel', ] if TYPE_CHECKING: from .configuration_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, ConditionalDetrConfig, ConditionalDetrOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_conditional_detr import ConditionalDetrFeatureExtractor from .image_processing_conditional_detr import ConditionalDetrImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrModel, ConditionalDetrPreTrainedModel, ) else: import sys lowercase__ : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
324
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: lowercase__ : str = None lowercase__ : Optional[int] = logging.get_logger(__name__) lowercase__ : Optional[Any] = {'vocab_file': 'sentencepiece.model', 'tokenizer_file': 'tokenizer.json'} lowercase__ : int = { 'vocab_file': { 'google/rembert': 'https://huggingface.co/google/rembert/resolve/main/sentencepiece.model', }, 'tokenizer_file': { 'google/rembert': 'https://huggingface.co/google/rembert/resolve/main/tokenizer.json', }, } lowercase__ : Optional[int] = { 'google/rembert': 2_56, } lowercase__ : str = '▁' class __lowerCAmelCase ( __magic_name__ ): """simple docstring""" _snake_case : str = VOCAB_FILES_NAMES _snake_case : str = PRETRAINED_VOCAB_FILES_MAP _snake_case : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _snake_case : Dict = RemBertTokenizer def __init__( self : List[Any] , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : str=None , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : str=True , lowerCAmelCase__ : Union[str, Any]=False , lowerCAmelCase__ : List[Any]="[CLS]" , lowerCAmelCase__ : str="[SEP]" , lowerCAmelCase__ : Optional[Any]="<unk>" , lowerCAmelCase__ : Optional[int]="[SEP]" , lowerCAmelCase__ : List[str]="<pad>" , lowerCAmelCase__ : str="[CLS]" , lowerCAmelCase__ : List[Any]="[MASK]" , **lowerCAmelCase__ : List[Any] , ) -> Any: '''simple docstring''' _UpperCamelCase = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else mask_token super().__init__( lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , do_lower_case=lowerCAmelCase__ , remove_space=lowerCAmelCase__ , keep_accents=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , **lowerCAmelCase__ , ) _UpperCamelCase = do_lower_case _UpperCamelCase = remove_space _UpperCamelCase = keep_accents _UpperCamelCase = vocab_file _UpperCamelCase = False if not self.vocab_file else True def snake_case__ ( self : Union[str, Any] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' _UpperCamelCase = [self.sep_token_id] _UpperCamelCase = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def snake_case__ ( self : int , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None , lowerCAmelCase__ : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(lowerCAmelCase__ )) + [1] + ([0] * len(lowerCAmelCase__ )) + [1] return [1] + ([0] * len(lowerCAmelCase__ )) + [1] def snake_case__ ( self : List[str] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' _UpperCamelCase = [self.sep_token_id] _UpperCamelCase = [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 snake_case__ ( self : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(lowerCAmelCase__ ): logger.error('''Vocabulary path ({}) should be a directory'''.format(lowerCAmelCase__ ) ) return _UpperCamelCase = os.path.join( lowerCAmelCase__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCAmelCase__ ): copyfile(self.vocab_file , lowerCAmelCase__ ) return (out_vocab_file,)
324
1
import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) def __lowerCamelCase ( lowerCAmelCase__ ): lowerCAmelCase__ = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): lowerCAmelCase__ = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): lowerCAmelCase__ = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCAmelCase__ = key[key.find('patch_embed' ) + len('patch_embed' )] lowerCAmelCase__ = key.replace(F"""patch_embed{idx}""" , F"""patch_embeddings.{int(lowerCAmelCase__ )-1}""" ) if "norm" in key: lowerCAmelCase__ = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCAmelCase__ = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] lowerCAmelCase__ = key.replace(F"""layer_norm{idx}""" , F"""layer_norm.{int(lowerCAmelCase__ )-1}""" ) if "layer_norm1" in key: lowerCAmelCase__ = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: lowerCAmelCase__ = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 lowerCAmelCase__ = key[key.find('block' ) + len('block' )] lowerCAmelCase__ = key.replace(F"""block{idx}""" , F"""block.{int(lowerCAmelCase__ )-1}""" ) if "attn.q" in key: lowerCAmelCase__ = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: lowerCAmelCase__ = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: lowerCAmelCase__ = key.replace('attn' , 'attention.self' ) if "fc1" in key: lowerCAmelCase__ = key.replace('fc1' , 'dense1' ) if "fc2" in key: lowerCAmelCase__ = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: lowerCAmelCase__ = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: lowerCAmelCase__ = key.replace('linear_fuse.conv' , 'linear_fuse' ) lowerCAmelCase__ = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCAmelCase__ = key[key.find('linear_c' ) + len('linear_c' )] lowerCAmelCase__ = key.replace(F"""linear_c{idx}""" , F"""linear_c.{int(lowerCAmelCase__ )-1}""" ) if "bot_conv" in key: lowerCAmelCase__ = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: lowerCAmelCase__ = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: lowerCAmelCase__ = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: lowerCAmelCase__ = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: lowerCAmelCase__ = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: lowerCAmelCase__ = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: lowerCAmelCase__ = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): lowerCAmelCase__ = key.replace('module.last_layer_depth' , 'head.head' ) lowerCAmelCase__ = value return new_state_dict def __lowerCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCAmelCase__ = state_dict.pop(F"""glpn.encoder.block.{i}.{j}.attention.self.kv.weight""" ) lowerCAmelCase__ = state_dict.pop(F"""glpn.encoder.block.{i}.{j}.attention.self.kv.bias""" ) # next, add keys and values (in that order) to the state dict lowerCAmelCase__ = kv_weight[ : config.hidden_sizes[i], : ] lowerCAmelCase__ = kv_bias[: config.hidden_sizes[i]] lowerCAmelCase__ = kv_weight[ config.hidden_sizes[i] :, : ] lowerCAmelCase__ = kv_bias[config.hidden_sizes[i] :] def __lowerCamelCase ( ): lowerCAmelCase__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase__ = Image.open(requests.get(lowerCAmelCase__ , stream=lowerCAmelCase__ ).raw ) return image @torch.no_grad() def __lowerCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__=False , lowerCAmelCase__=None ): lowerCAmelCase__ = GLPNConfig(hidden_sizes=[6_4, 1_2_8, 3_2_0, 5_1_2] , decoder_hidden_size=6_4 , depths=[3, 8, 2_7, 3] ) # load image processor (only resize + rescale) lowerCAmelCase__ = GLPNImageProcessor() # prepare image lowerCAmelCase__ = prepare_img() lowerCAmelCase__ = image_processor(images=lowerCAmelCase__ , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict lowerCAmelCase__ = torch.load(lowerCAmelCase__ , map_location=torch.device('cpu' ) ) # rename keys lowerCAmelCase__ = rename_keys(lowerCAmelCase__ ) # key and value matrices need special treatment read_in_k_v(lowerCAmelCase__ , lowerCAmelCase__ ) # create HuggingFace model and load state dict lowerCAmelCase__ = GLPNForDepthEstimation(lowerCAmelCase__ ) model.load_state_dict(lowerCAmelCase__ ) model.eval() # forward pass lowerCAmelCase__ = model(lowerCAmelCase__ ) lowerCAmelCase__ = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCAmelCase__ = torch.tensor( [[4.4_1_4_7, 4.0_8_7_3, 4.0_6_7_3], [3.7_8_9_0, 3.2_8_8_1, 3.1_5_2_5], [3.7_6_7_4, 3.5_4_2_3, 3.4_9_1_3]] ) elif "kitti" in model_name: lowerCAmelCase__ = torch.tensor( [[3.4_2_9_1, 2.7_8_6_5, 2.5_1_5_1], [3.2_8_4_1, 2.7_0_2_1, 2.3_5_0_2], [3.1_1_4_7, 2.4_6_2_5, 2.2_4_8_1]] ) else: raise ValueError(F"""Unknown model name: {model_name}""" ) lowerCAmelCase__ = torch.Size([1, 4_8_0, 6_4_0] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCAmelCase__ , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCAmelCase__ , lowerCAmelCase__ ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCAmelCase__ , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCAmelCase__ , lowerCAmelCase__ ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCAmelCase__ , ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument( '--checkpoint_path', default=None, type=str, help='Path to the original PyTorch checkpoint (.pth file).', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to upload the model to the HuggingFace hub.' ) parser.add_argument( '--model_name', default='glpn-kitti', type=str, help='Name of the model in case you\'re pushing to the hub.', ) lowerCAmelCase__ = parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
119
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCAmelCase__ = { 'configuration_transfo_xl': ['TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TransfoXLConfig'], 'tokenization_transfo_xl': ['TransfoXLCorpus', 'TransfoXLTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ 'TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST', 'AdaptiveEmbedding', 'TransfoXLForSequenceClassification', 'TransfoXLLMHeadModel', 'TransfoXLModel', 'TransfoXLPreTrainedModel', 'load_tf_weights_in_transfo_xl', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ 'TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFAdaptiveEmbedding', 'TFTransfoXLForSequenceClassification', 'TFTransfoXLLMHeadModel', 'TFTransfoXLMainLayer', 'TFTransfoXLModel', 'TFTransfoXLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_transfo_xl import ( TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, AdaptiveEmbedding, TransfoXLForSequenceClassification, TransfoXLLMHeadModel, TransfoXLModel, TransfoXLPreTrainedModel, load_tf_weights_in_transfo_xl, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_transfo_xl import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFAdaptiveEmbedding, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLMainLayer, TFTransfoXLModel, TFTransfoXLPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
119
1
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" snake_case__ : Optional[int] = "philschmid/bart-large-cnn-samsum" snake_case__ : str = ( "This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, " "and returns a summary of the text." ) snake_case__ : Union[str, Any] = "summarizer" snake_case__ : Optional[Any] = AutoTokenizer snake_case__ : Optional[int] = AutoModelForSeqaSeqLM snake_case__ : Any = ["text"] snake_case__ : Tuple = ["text"] def UpperCAmelCase_ ( self : str , UpperCAmelCase__ : Optional[Any] ) -> int: return self.pre_processor(UpperCAmelCase__ , return_tensors="pt" , truncation=UpperCAmelCase__ ) def UpperCAmelCase_ ( self : str , UpperCAmelCase__ : int ) -> Any: return self.model.generate(**UpperCAmelCase__ )[0] def UpperCAmelCase_ ( self : Any , UpperCAmelCase__ : List[Any] ) -> List[Any]: return self.pre_processor.decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ , clean_up_tokenization_spaces=UpperCAmelCase__ )
54
"""simple docstring""" import warnings from typing import Any, Dict, List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function 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 lowercase_ ( __lowerCAmelCase ): '''simple docstring''' UpperCAmelCase : Any = ['''input_values''', '''attention_mask'''] def __init__( self : Dict , _UpperCAmelCase : int = 1 , _UpperCAmelCase : int = 16_000 , _UpperCAmelCase : float = 0.0 , _UpperCAmelCase : bool = False , _UpperCAmelCase : int = 80 , _UpperCAmelCase : int = 16 , _UpperCAmelCase : int = 64 , _UpperCAmelCase : str = "hann_window" , _UpperCAmelCase : float = 1.0 , _UpperCAmelCase : float = 80 , _UpperCAmelCase : float = 7_600 , _UpperCAmelCase : float = 1E-1_0 , _UpperCAmelCase : int = 2 , _UpperCAmelCase : bool = True , **_UpperCAmelCase : List[Any] , ): super().__init__(feature_size=_UpperCAmelCase , sampling_rate=_UpperCAmelCase , padding_value=_UpperCAmelCase , **_UpperCAmelCase ) _A = do_normalize _A = return_attention_mask _A = num_mel_bins _A = hop_length _A = win_length _A = win_function _A = frame_signal_scale _A = fmin _A = fmax _A = mel_floor _A = reduction_factor _A = win_length * sampling_rate // 1_000 _A = hop_length * sampling_rate // 1_000 _A = optimal_fft_length(self.sample_size ) _A = (self.n_fft // 2) + 1 _A = window_function(window_length=self.sample_size , name=self.win_function , periodic=_UpperCAmelCase ) _A = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.num_mel_bins , min_frequency=self.fmin , max_frequency=self.fmax , sampling_rate=self.sampling_rate , norm='slaney' , mel_scale='slaney' , ) if frame_signal_scale != 1.0: warnings.warn( 'The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers' , _UpperCAmelCase , ) if reduction_factor != 2.0: warnings.warn( 'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers' , _UpperCAmelCase , ) @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def lowerCAmelCase_ ( _UpperCAmelCase : List[np.ndarray] , _UpperCAmelCase : List[np.ndarray] , _UpperCAmelCase : float = 0.0 ): if attention_mask is not None: _A = np.array(_UpperCAmelCase , np.intaa ) _A = [] for vector, length in zip(_UpperCAmelCase , attention_mask.sum(-1 ) ): _A = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7 ) if length < normed_slice.shape[0]: _A = padding_value normed_input_values.append(_UpperCAmelCase ) else: _A = [(x - x.mean()) / np.sqrt(x.var() + 1E-7 ) for x in input_values] return normed_input_values def lowerCAmelCase_ ( self : Dict , _UpperCAmelCase : np.ndarray , ): _A = spectrogram( _UpperCAmelCase , window=self.window , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , mel_filters=self.mel_filters , mel_floor=self.mel_floor , log_mel='log10' , ) return log_mel_spec.T def __call__( self : int , _UpperCAmelCase : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _UpperCAmelCase : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _UpperCAmelCase : Union[bool, str, PaddingStrategy] = False , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : bool = False , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : Optional[bool] = None , _UpperCAmelCase : Optional[Union[str, TensorType]] = None , _UpperCAmelCase : Optional[int] = None , **_UpperCAmelCase : Optional[int] , ): if audio is None and audio_target is None: raise ValueError('You must provide either `audio` or `audio_target` values.' ) if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) if audio is not None: _A = self._process_audio( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase , ) else: _A = None if audio_target is not None: _A = self._process_audio( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase , ) if inputs is None: return inputs_target else: _A = inputs_target['input_values'] _A = inputs_target.get('attention_mask' ) if decoder_attention_mask is not None: _A = decoder_attention_mask return inputs def lowerCAmelCase_ ( self : Optional[int] , _UpperCAmelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCAmelCase : bool = False , _UpperCAmelCase : Union[bool, str, PaddingStrategy] = False , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : bool = False , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : Optional[bool] = None , _UpperCAmelCase : Optional[Union[str, TensorType]] = None , **_UpperCAmelCase : List[Any] , ): _A = isinstance(_UpperCAmelCase , np.ndarray ) and len(speech.shape ) > 1 if is_batched_numpy and len(speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) _A = is_batched_numpy or ( isinstance(_UpperCAmelCase , (list, tuple) ) and (isinstance(speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: _A = [np.asarray(_UpperCAmelCase , dtype=np.floataa ) for speech in speech] elif not is_batched and not isinstance(_UpperCAmelCase , np.ndarray ): _A = np.asarray(_UpperCAmelCase , dtype=np.floataa ) elif isinstance(_UpperCAmelCase , np.ndarray ) and speech.dtype is np.dtype(np.floataa ): _A = speech.astype(np.floataa ) # always return batch if not is_batched: _A = [speech] # needed to make pad() work on spectrogram inputs _A = self.feature_size # convert into correct format for padding if is_target: _A = [self._extract_mel_features(_UpperCAmelCase ) for waveform in speech] _A = BatchFeature({'input_values': features} ) _A = self.num_mel_bins else: _A = BatchFeature({'input_values': speech} ) _A = self.pad( _UpperCAmelCase , padding=_UpperCAmelCase , max_length=_UpperCAmelCase , truncation=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_attention_mask=_UpperCAmelCase , **_UpperCAmelCase , ) _A = feature_size_hack # convert input values to correct format _A = padded_inputs['input_values'] if not isinstance(input_values[0] , np.ndarray ): _A = [np.asarray(_UpperCAmelCase , dtype=np.floataa ) for array in input_values] elif ( not isinstance(_UpperCAmelCase , np.ndarray ) and isinstance(input_values[0] , np.ndarray ) and input_values[0].dtype is np.dtype(np.floataa ) ): _A = [array.astype(np.floataa ) for array in input_values] elif isinstance(_UpperCAmelCase , np.ndarray ) and input_values.dtype is np.dtype(np.floataa ): _A = input_values.astype(np.floataa ) # convert attention_mask to correct format _A = padded_inputs.get('attention_mask' ) if attention_mask is not None: _A = [np.asarray(_UpperCAmelCase , dtype=np.intaa ) for array in attention_mask] # zero-mean and unit-variance normalization if not is_target and self.do_normalize: _A = ( attention_mask if self._get_padding_strategies(_UpperCAmelCase , max_length=_UpperCAmelCase ) is not PaddingStrategy.DO_NOT_PAD else None ) _A = self.zero_mean_unit_var_norm( padded_inputs['input_values'] , attention_mask=_UpperCAmelCase , padding_value=self.padding_value ) if return_tensors is not None: _A = padded_inputs.convert_to_tensors(_UpperCAmelCase ) return padded_inputs def lowerCAmelCase_ ( self : Any ): _A = super().to_dict() # Don't serialize these as they are derived from the other properties. _A = ['window', 'mel_filters', 'sample_size', 'sample_stride', 'n_fft', 'n_freqs'] for name in names: if name in output: del output[name] return output
315
0
'''simple docstring''' import unittest from diffusers.pipelines.pipeline_utils import is_safetensors_compatible class UpperCamelCase__( unittest.TestCase ): def a__( self : Optional[int] )-> List[Any]: """simple docstring""" UpperCAmelCase = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowerCAmelCase ) ) def a__( self : int )-> Union[str, Any]: """simple docstring""" UpperCAmelCase = [ '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowerCAmelCase ) ) def a__( self : Optional[int] )-> List[Any]: """simple docstring""" UpperCAmelCase = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', '''unet/diffusion_pytorch_model.bin''', # Removed: 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(lowerCAmelCase ) ) def a__( self : Dict )-> Optional[int]: """simple docstring""" UpperCAmelCase = [ '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowerCAmelCase ) ) def a__( self : Optional[Any] )-> Tuple: """simple docstring""" UpperCAmelCase = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', # Removed: 'text_encoder/model.safetensors', '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertFalse(is_safetensors_compatible(lowerCAmelCase ) ) def a__( self : List[Any] )-> Optional[Any]: """simple docstring""" UpperCAmelCase = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] UpperCAmelCase = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCAmelCase , variant=lowerCAmelCase ) ) def a__( self : Dict )-> Tuple: """simple docstring""" UpperCAmelCase = [ '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] UpperCAmelCase = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCAmelCase , variant=lowerCAmelCase ) ) def a__( self : List[Any] )-> List[str]: """simple docstring""" UpperCAmelCase = [ '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] UpperCAmelCase = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCAmelCase , variant=lowerCAmelCase ) ) def a__( self : List[str] )-> int: """simple docstring""" UpperCAmelCase = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', '''unet/diffusion_pytorch_model.fp16.bin''', # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors', ] UpperCAmelCase = '''fp16''' self.assertFalse(is_safetensors_compatible(lowerCAmelCase , variant=lowerCAmelCase ) ) def a__( self : int )-> int: """simple docstring""" UpperCAmelCase = [ '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', ] UpperCAmelCase = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCAmelCase , variant=lowerCAmelCase ) ) def a__( self : Optional[int] )-> List[str]: """simple docstring""" UpperCAmelCase = [ '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', ] UpperCAmelCase = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCAmelCase , variant=lowerCAmelCase ) ) def a__( self : str )-> Tuple: """simple docstring""" UpperCAmelCase = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', # 'text_encoder/model.fp16.safetensors', '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] UpperCAmelCase = '''fp16''' self.assertFalse(is_safetensors_compatible(lowerCAmelCase , variant=lowerCAmelCase ) )
91
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, 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 PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class UpperCamelCase__( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): __magic_name__ : List[str] = StableDiffusionSAGPipeline __magic_name__ : str = TEXT_TO_IMAGE_PARAMS __magic_name__ : Any = TEXT_TO_IMAGE_BATCH_PARAMS __magic_name__ : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS __magic_name__ : List[Any] = TEXT_TO_IMAGE_IMAGE_PARAMS __magic_name__ : str = False def a__( self : Union[str, Any] )-> Tuple: """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) UpperCAmelCase = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=lowerCAmelCase , set_alpha_to_one=lowerCAmelCase , ) torch.manual_seed(0 ) UpperCAmelCase = 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 , ) torch.manual_seed(0 ) UpperCAmelCase = 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=1000 , ) UpperCAmelCase = CLIPTextModel(lowerCAmelCase ) UpperCAmelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) UpperCAmelCase = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def a__( self : Optional[Any] , lowerCAmelCase : str , lowerCAmelCase : Tuple=0 )-> str: """simple docstring""" if str(lowerCAmelCase ).startswith('''mps''' ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase ).manual_seed(lowerCAmelCase ) UpperCAmelCase = { '''prompt''': '''.''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 1.0, '''sag_scale''': 1.0, '''output_type''': '''numpy''', } return inputs def a__( self : Any )-> List[str]: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class UpperCamelCase__( unittest.TestCase ): def a__( self : Union[str, Any] )-> Any: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def a__( self : Union[str, Any] )-> Tuple: """simple docstring""" UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) UpperCAmelCase = sag_pipe.to(lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , generator=lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) UpperCAmelCase = output.images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) UpperCAmelCase = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def a__( self : int )-> Union[str, Any]: """simple docstring""" UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) UpperCAmelCase = sag_pipe.to(lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , generator=lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) UpperCAmelCase = output.images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) UpperCAmelCase = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def a__( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) UpperCAmelCase = sag_pipe.to(lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , width=768 , height=512 , generator=lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' , ) UpperCAmelCase = output.images assert image.shape == (1, 512, 768, 3)
91
1
import inspect import os import re from transformers.configuration_utils import PretrainedConfig 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_config_docstrings.py a_ = """src/transformers""" # This is to make sure the transformers module imported is the one in the repo. a_ = direct_transformers_import(PATH_TO_TRANSFORMERS) a_ = transformers.models.auto.configuration_auto.CONFIG_MAPPING a_ = { # used to compute the property `self.chunk_length` """EncodecConfig""": ["""overlap"""], # used as `self.bert_model = BertModel(config, ...)` """DPRConfig""": True, # not used in modeling files, but it's an important information """FSMTConfig""": ["""langs"""], # used internally in the configuration class file """GPTNeoConfig""": ["""attention_types"""], # used internally in the configuration class file """EsmConfig""": ["""is_folding_model"""], # used during training (despite we don't have training script for these models yet) """Mask2FormerConfig""": ["""ignore_value"""], # `ignore_value` used during training (despite we don't have training script for these models yet) # `norm` used in conversion script (despite not using in the modeling file) """OneFormerConfig""": ["""ignore_value""", """norm"""], # used during preprocessing and collation, see `collating_graphormer.py` """GraphormerConfig""": ["""spatial_pos_max"""], # used internally in the configuration class file """T5Config""": ["""feed_forward_proj"""], # used internally in the configuration class file # `tokenizer_class` get default value `T5Tokenizer` intentionally """MT5Config""": ["""feed_forward_proj""", """tokenizer_class"""], """UMT5Config""": ["""feed_forward_proj""", """tokenizer_class"""], # used internally in the configuration class file """LongT5Config""": ["""feed_forward_proj"""], # used internally in the configuration class file """SwitchTransformersConfig""": ["""feed_forward_proj"""], # having default values other than `1e-5` - we can't fix them without breaking """BioGptConfig""": ["""layer_norm_eps"""], # having default values other than `1e-5` - we can't fix them without breaking """GLPNConfig""": ["""layer_norm_eps"""], # having default values other than `1e-5` - we can't fix them without breaking """SegformerConfig""": ["""layer_norm_eps"""], # having default values other than `1e-5` - we can't fix them without breaking """CvtConfig""": ["""layer_norm_eps"""], # having default values other than `1e-5` - we can't fix them without breaking """PerceiverConfig""": ["""layer_norm_eps"""], # used internally to calculate the feature size """InformerConfig""": ["""num_static_real_features""", """num_time_features"""], # used internally to calculate the feature size """TimeSeriesTransformerConfig""": ["""num_static_real_features""", """num_time_features"""], # used internally to calculate the feature size """AutoformerConfig""": ["""num_static_real_features""", """num_time_features"""], # used internally to calculate `mlp_dim` """SamVisionConfig""": ["""mlp_ratio"""], # For (head) training, but so far not implemented """ClapAudioConfig""": ["""num_classes"""], # Not used, but providing useful information to users """SpeechT5HifiGanConfig""": ["""sampling_rate"""], } # TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure SPECIAL_CASES_TO_ALLOW.update( { """CLIPSegConfig""": True, """DeformableDetrConfig""": True, """DetaConfig""": True, """DinatConfig""": True, """DonutSwinConfig""": True, """EfficientFormerConfig""": True, """FSMTConfig""": True, """JukeboxConfig""": True, """LayoutLMv2Config""": True, """MaskFormerSwinConfig""": True, """MT5Config""": True, """NatConfig""": True, """OneFormerConfig""": True, """PerceiverConfig""": True, """RagConfig""": True, """SpeechT5Config""": True, """SwinConfig""": True, """Swin2SRConfig""": True, """Swinv2Config""": True, """SwitchTransformersConfig""": True, """TableTransformerConfig""": True, """TapasConfig""": True, """TransfoXLConfig""": True, """UniSpeechConfig""": True, """UniSpeechSatConfig""": True, """WavLMConfig""": True, """WhisperConfig""": True, # TODO: @Arthur (for `alignment_head` and `alignment_layer`) """JukeboxPriorConfig""": True, # TODO: @Younes (for `is_decoder`) """Pix2StructTextConfig""": True, } ) def a__ ( _UpperCamelCase : str ,_UpperCamelCase : int ,_UpperCamelCase : List[Any] ,_UpperCamelCase : Dict ): __lowerCamelCase = False for attribute in attributes: for modeling_source in source_strings: # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)` if ( F"""config.{attribute}""" in modeling_source or F"""getattr(config, \"{attribute}\"""" in modeling_source or F"""getattr(self.config, \"{attribute}\"""" in modeling_source ): __lowerCamelCase = True # Deal with multi-line cases elif ( re.search( RF"""getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*\"{attribute}\"""" ,_UpperCamelCase ,) is not None ): __lowerCamelCase = True # `SequenceSummary` is called with `SequenceSummary(config)` elif attribute in [ "summary_type", "summary_use_proj", "summary_activation", "summary_last_dropout", "summary_proj_to_labels", "summary_first_dropout", ]: if "SequenceSummary" in modeling_source: __lowerCamelCase = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files __lowerCamelCase = [ '''bos_index''', '''eos_index''', '''pad_index''', '''unk_index''', '''mask_index''', '''image_size''', '''use_cache''', '''out_features''', '''out_indices''', ] __lowerCamelCase = ['''encoder_no_repeat_ngram_size'''] # Special cases to be allowed __lowerCamelCase = True if not attribute_used: __lowerCamelCase = False for attribute in attributes: # Allow if the default value in the configuration class is different from the one in `PretrainedConfig` if attribute in ["is_encoder_decoder"] and default_value is True: __lowerCamelCase = True elif attribute in ["tie_word_embeddings"] and default_value is False: __lowerCamelCase = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: __lowerCamelCase = True elif attribute.endswith('''_token_id''' ): __lowerCamelCase = True # configuration class specific cases if not case_allowed: __lowerCamelCase = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ ,[] ) __lowerCamelCase = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def a__ ( _UpperCamelCase : str ): __lowerCamelCase = dict(inspect.signature(config_class.__init__ ).parameters ) __lowerCamelCase = [x for x in list(signature.keys() ) if x not in ['''self''', '''kwargs''']] __lowerCamelCase = [signature[param].default for param in parameter_names] # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long # as one variant is used, the test should pass __lowerCamelCase = {} if len(config_class.attribute_map ) > 0: __lowerCamelCase = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files __lowerCamelCase = inspect.getsourcefile(_UpperCamelCase ) __lowerCamelCase = os.path.dirname(_UpperCamelCase ) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. __lowerCamelCase = [os.path.join(_UpperCamelCase ,_UpperCamelCase ) for fn in os.listdir(_UpperCamelCase ) if fn.startswith('''modeling_''' )] # Get the source code strings __lowerCamelCase = [] for path in modeling_paths: if os.path.isfile(_UpperCamelCase ): with open(_UpperCamelCase ) as fp: modeling_sources.append(fp.read() ) __lowerCamelCase = [] for config_param, default_value in zip(_UpperCamelCase ,_UpperCamelCase ): # `attributes` here is all the variant names for `config_param` __lowerCamelCase = [config_param] # some configuration classes have non-empty `attribute_map`, and both names could be used in the # corresponding modeling files. As long as one of them appears, it is fine. if config_param in reversed_attribute_map: attributes.append(reversed_attribute_map[config_param] ) if not check_attribute_being_used(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ): unused_attributes.append(attributes[0] ) return sorted(_UpperCamelCase ) def a__ ( ): __lowerCamelCase = {} for _config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in _config_class.__module__: continue # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.) __lowerCamelCase = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class ) ,lambda _UpperCamelCase : inspect.isclass(_UpperCamelCase ) and issubclass(_UpperCamelCase ,_UpperCamelCase ) and inspect.getmodule(_UpperCamelCase ) == inspect.getmodule(_config_class ) ,) ] for config_class in config_classes_in_module: __lowerCamelCase = check_config_attributes_being_used(_UpperCamelCase ) if len(_UpperCamelCase ) > 0: __lowerCamelCase = unused_attributes if len(_UpperCamelCase ) > 0: __lowerCamelCase = '''The following configuration classes contain unused attributes in the corresponding modeling files:\n''' for name, attributes in configs_with_unused_attributes.items(): error += F"""{name}: {attributes}\n""" raise ValueError(_UpperCamelCase ) if __name__ == "__main__": check_config_attributes()
330
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params a_ = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ["""memory_attention""", """encoder_attn"""], ["""attention""", """attn"""], ["""/""", """."""], [""".LayerNorm.gamma""", """_layer_norm.weight"""], [""".LayerNorm.beta""", """_layer_norm.bias"""], ["""r.layer_""", """r.layers."""], ["""output_proj""", """out_proj"""], ["""ffn.dense_1.""", """fc2."""], ["""ffn.dense.""", """fc1."""], ["""ffn_layer_norm""", """final_layer_norm"""], ["""kernel""", """weight"""], ["""encoder_layer_norm.""", """encoder.layer_norm."""], ["""decoder_layer_norm.""", """decoder.layer_norm."""], ["""embeddings.weights""", """shared.weight"""], ] def a__ ( _UpperCamelCase : int ): for pegasus_name, hf_name in PATTERNS: __lowerCamelCase = k.replace(_UpperCamelCase ,_UpperCamelCase ) return k def a__ ( _UpperCamelCase : dict ,_UpperCamelCase : dict ): __lowerCamelCase = DEFAULTS.copy() cfg_kwargs.update(_UpperCamelCase ) __lowerCamelCase = PegasusConfig(**_UpperCamelCase ) __lowerCamelCase = PegasusForConditionalGeneration(_UpperCamelCase ) __lowerCamelCase = torch_model.model.state_dict() __lowerCamelCase = {} for k, v in tf_weights.items(): __lowerCamelCase = rename_state_dict_key(_UpperCamelCase ) if new_k not in sd: raise ValueError(F"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: __lowerCamelCase = v.T __lowerCamelCase = torch.tensor(_UpperCamelCase ,dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, F"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected __lowerCamelCase = torch.zeros_like(mapping['''shared.weight'''][cfg.pad_token_id + 1] ) __lowerCamelCase = mapping['''shared.weight'''] __lowerCamelCase = mapping['''shared.weight'''] __lowerCamelCase = {k: torch.zeros_like(_UpperCamelCase ) for k, v in sd.items() if k.endswith('''bias''' ) and k not in mapping} mapping.update(**_UpperCamelCase ) __lowerCamelCase ,__lowerCamelCase = torch_model.model.load_state_dict(_UpperCamelCase ,strict=_UpperCamelCase ) __lowerCamelCase = [ k for k in missing if k not in ['''encoder.embed_positions.weight''', '''decoder.embed_positions.weight'''] ] assert unexpected_missing == [], F"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], F"""no matches found for the following tf keys {extra}""" return torch_model def a__ ( _UpperCamelCase : str="./ckpt/aeslc/model.ckpt-32000" ): __lowerCamelCase = tf.train.list_variables(_UpperCamelCase ) __lowerCamelCase = {} __lowerCamelCase = ['''Adafactor''', '''global_step'''] for name, shape in tqdm(_UpperCamelCase ,desc='''converting tf checkpoint to dict''' ): __lowerCamelCase = any(pat in name for pat in ignore_name ) if skip_key: continue __lowerCamelCase = tf.train.load_variable(_UpperCamelCase ,_UpperCamelCase ) __lowerCamelCase = array return tf_weights def a__ ( _UpperCamelCase : str ,_UpperCamelCase : str ): # save tokenizer first __lowerCamelCase = Path(_UpperCamelCase ).parent.name __lowerCamelCase = task_specific_params[F"""summarization_{dataset}"""]['''max_position_embeddings'''] __lowerCamelCase = PegasusTokenizer.from_pretrained('''sshleifer/pegasus''' ,model_max_length=_UpperCamelCase ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(_UpperCamelCase ) # convert model __lowerCamelCase = get_tf_weights_as_numpy(_UpperCamelCase ) __lowerCamelCase = task_specific_params[F"""summarization_{dataset}"""] if dataset == "large": __lowerCamelCase = task_specific_params __lowerCamelCase = convert_pegasus(_UpperCamelCase ,_UpperCamelCase ) torch_model.save_pretrained(_UpperCamelCase ) __lowerCamelCase = torch_model.state_dict() sd.pop('''model.decoder.embed_positions.weight''' ) sd.pop('''model.encoder.embed_positions.weight''' ) torch.save(_UpperCamelCase ,Path(_UpperCamelCase ) / '''pytorch_model.bin''' ) if __name__ == "__main__": a_ = argparse.ArgumentParser() # Required parameters parser.add_argument("""tf_ckpt_path""", type=str, help="""passed to tf.train.list_variables""") parser.add_argument("""save_dir""", default=None, type=str, help="""Path to the output PyTorch model.""") a_ = parser.parse_args() if args.save_dir is None: a_ = Path(args.tf_ckpt_path).parent.name a_ = os.path.join("""pegasus""", dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
330
1
import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger lowerCamelCase : Optional[Any] =get_logger(__name__) class __a : def __init__( self : List[str] , SCREAMING_SNAKE_CASE : Optional[str] = None ): '''simple docstring''' UpperCamelCase__ : Any = ( os.path.join(SCREAMING_SNAKE_CASE , config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) UpperCamelCase__ : Optional[Any] = Extractor def __lowercase ( self : Dict , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" UpperCamelCase__ : Dict = os.path.abspath(SCREAMING_SNAKE_CASE ) return os.path.join(self.extract_dir , hash_url_to_filename(SCREAMING_SNAKE_CASE ) ) def __lowercase ( self : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : bool ): '''simple docstring''' return force_extract or ( not os.path.isfile(SCREAMING_SNAKE_CASE ) and not (os.path.isdir(SCREAMING_SNAKE_CASE ) and os.listdir(SCREAMING_SNAKE_CASE )) ) def __lowercase ( self : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : bool = False ): '''simple docstring''' UpperCamelCase__ : Optional[int] = self.extractor.infer_extractor_format(SCREAMING_SNAKE_CASE ) if not extractor_format: return input_path UpperCamelCase__ : Tuple = self._get_output_path(SCREAMING_SNAKE_CASE ) if self._do_extract(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): self.extractor.extract(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return output_path class __a ( A__ ): @classmethod @abstractmethod def __lowercase ( cls : Optional[int] , SCREAMING_SNAKE_CASE : Union[Path, str] , **SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' ... @staticmethod @abstractmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' ... class __a ( A__ , A__ ): _lowerCAmelCase : List[bytes] = [] @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , "rb" ) as f: return f.read(SCREAMING_SNAKE_CASE ) @classmethod def __lowercase ( cls : List[str] , SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : bytes = b"" ): '''simple docstring''' if not magic_number: UpperCamelCase__ : List[str] = max(len(SCREAMING_SNAKE_CASE ) for cls_magic_number in cls.magic_numbers ) try: UpperCamelCase__ : Tuple = cls.read_magic_number(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) except OSError: return False return any(magic_number.startswith(SCREAMING_SNAKE_CASE ) for cls_magic_number in cls.magic_numbers ) class __a ( A__ ): @classmethod def __lowercase ( cls : Optional[Any] , SCREAMING_SNAKE_CASE : Union[Path, str] , **SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' return tarfile.is_tarfile(SCREAMING_SNAKE_CASE ) @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' def resolved(SCREAMING_SNAKE_CASE : str ) -> str: return os.path.realpath(os.path.abspath(SCREAMING_SNAKE_CASE ) ) def badpath(SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ).startswith(SCREAMING_SNAKE_CASE ) def badlink(SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : str ) -> bool: # Links are interpreted relative to the directory containing the link UpperCamelCase__ : Optional[Any] = resolved(os.path.join(SCREAMING_SNAKE_CASE , os.path.dirname(info.name ) ) ) return badpath(info.linkname , base=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[str] = resolved(SCREAMING_SNAKE_CASE ) for finfo in members: if badpath(finfo.name , SCREAMING_SNAKE_CASE ): logger.error(F'Extraction of {finfo.name} is blocked (illegal path)' ) elif finfo.issym() and badlink(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): logger.error(F'Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}' ) elif finfo.islnk() and badlink(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): logger.error(F'Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}' ) else: yield finfo @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : int = tarfile.open(SCREAMING_SNAKE_CASE ) tar_file.extractall(SCREAMING_SNAKE_CASE , members=TarExtractor.safemembers(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) tar_file.close() class __a ( A__ ): _lowerCAmelCase : Any = [b'''\x1F\x8B'''] @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' with gzip.open(SCREAMING_SNAKE_CASE , "rb" ) as gzip_file: with open(SCREAMING_SNAKE_CASE , "wb" ) as extracted_file: shutil.copyfileobj(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) class __a ( A__ ): _lowerCAmelCase : Optional[int] = [ b'''PK\x03\x04''', b'''PK\x05\x06''', # empty archive b'''PK\x07\x08''', # spanned archive ] @classmethod def __lowercase ( cls : Optional[Any] , SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : bytes = b"" ): '''simple docstring''' if super().is_extractable(SCREAMING_SNAKE_CASE , magic_number=SCREAMING_SNAKE_CASE ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(SCREAMING_SNAKE_CASE , "rb" ) as fp: UpperCamelCase__ : Dict = _EndRecData(SCREAMING_SNAKE_CASE ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: UpperCamelCase__ : Union[str, Any] = fp.read(SCREAMING_SNAKE_CASE ) # CD is where we expect it to be if len(SCREAMING_SNAKE_CASE ) == sizeCentralDir: UpperCamelCase__ : List[Any] = struct.unpack(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) with zipfile.ZipFile(SCREAMING_SNAKE_CASE , "r" ) as zip_file: zip_file.extractall(SCREAMING_SNAKE_CASE ) zip_file.close() class __a ( A__ ): _lowerCAmelCase : Union[str, Any] = [b'''\xFD\x37\x7A\x58\x5A\x00'''] @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' with lzma.open(SCREAMING_SNAKE_CASE ) as compressed_file: with open(SCREAMING_SNAKE_CASE , "wb" ) as extracted_file: shutil.copyfileobj(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) class __a ( A__ ): _lowerCAmelCase : int = [b'''Rar!\x1a\x07\x00''', b'''Rar!\x1a\x07\x01\x00'''] # RAR_ID # RAR5_ID @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' if not config.RARFILE_AVAILABLE: raise ImportError("Please pip install rarfile" ) import rarfile os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Optional[Any] = rarfile.RarFile(SCREAMING_SNAKE_CASE ) rf.extractall(SCREAMING_SNAKE_CASE ) rf.close() class __a ( A__ ): _lowerCAmelCase : List[Any] = [b'''\x28\xb5\x2F\xFD'''] @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' if not config.ZSTANDARD_AVAILABLE: raise ImportError("Please pip install zstandard" ) import zstandard as zstd UpperCamelCase__ : int = zstd.ZstdDecompressor() with open(SCREAMING_SNAKE_CASE , "rb" ) as ifh, open(SCREAMING_SNAKE_CASE , "wb" ) as ofh: dctx.copy_stream(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) class __a ( A__ ): _lowerCAmelCase : List[Any] = [b'''\x42\x5A\x68'''] @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' with bza.open(SCREAMING_SNAKE_CASE , "rb" ) as compressed_file: with open(SCREAMING_SNAKE_CASE , "wb" ) as extracted_file: shutil.copyfileobj(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) class __a ( A__ ): _lowerCAmelCase : int = [b'''\x37\x7A\xBC\xAF\x27\x1C'''] @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' if not config.PY7ZR_AVAILABLE: raise ImportError("Please pip install py7zr" ) import pyazr os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) with pyazr.SevenZipFile(SCREAMING_SNAKE_CASE , "r" ) as archive: archive.extractall(SCREAMING_SNAKE_CASE ) class __a ( A__ ): _lowerCAmelCase : Tuple = [b'''\x04\x22\x4D\x18'''] @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] ): '''simple docstring''' if not config.LZ4_AVAILABLE: raise ImportError("Please pip install lz4" ) import lza.frame with lza.frame.open(SCREAMING_SNAKE_CASE , "rb" ) as compressed_file: with open(SCREAMING_SNAKE_CASE , "wb" ) as extracted_file: shutil.copyfileobj(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) class __a : # Put zip file to the last, b/c it is possible wrongly detected as zip (I guess it means: as tar or gzip) _lowerCAmelCase : Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def __lowercase ( cls : Optional[int] ): '''simple docstring''' return max( len(SCREAMING_SNAKE_CASE ) for extractor in cls.extractors.values() if issubclass(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' try: return MagicNumberBaseExtractor.read_magic_number(SCREAMING_SNAKE_CASE , magic_number_length=SCREAMING_SNAKE_CASE ) except OSError: return b"" @classmethod def __lowercase ( cls : Tuple , SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : bool = False ): '''simple docstring''' warnings.warn( "Method 'is_extractable' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'infer_extractor_format' instead." , category=SCREAMING_SNAKE_CASE , ) UpperCamelCase__ : str = cls.infer_extractor_format(SCREAMING_SNAKE_CASE ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def __lowercase ( cls : Dict , SCREAMING_SNAKE_CASE : Union[Path, str] ): # <Added version="2.4.0"/> '''simple docstring''' UpperCamelCase__ : int = cls._get_magic_number_max_length() UpperCamelCase__ : Dict = cls._read_magic_number(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(SCREAMING_SNAKE_CASE , magic_number=SCREAMING_SNAKE_CASE ): return extractor_format @classmethod def __lowercase ( cls : Tuple , SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Union[Path, str] , SCREAMING_SNAKE_CASE : Optional[str] = None , SCREAMING_SNAKE_CASE : Optional[BaseExtractor] = "deprecated" , ): '''simple docstring''' os.makedirs(os.path.dirname(SCREAMING_SNAKE_CASE ) , exist_ok=SCREAMING_SNAKE_CASE ) # Prevent parallel extractions UpperCamelCase__ : Union[str, Any] = str(Path(SCREAMING_SNAKE_CASE ).with_suffix(".lock" ) ) with FileLock(SCREAMING_SNAKE_CASE ): shutil.rmtree(SCREAMING_SNAKE_CASE , ignore_errors=SCREAMING_SNAKE_CASE ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): # passed as positional arg warnings.warn( "Parameter 'extractor' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'extractor_format' instead." , category=SCREAMING_SNAKE_CASE , ) UpperCamelCase__ : str = extractor if extractor != "deprecated" else extractor_format else: UpperCamelCase__ : List[Any] = cls.extractors[extractor_format] return extractor.extract(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) else: warnings.warn( "Parameter 'extractor_format' was made required in version 2.4.0 and not passing it will raise an " "exception in 3.0.0." , category=SCREAMING_SNAKE_CASE , ) for extractor in cls.extractors.values(): if extractor.is_extractable(SCREAMING_SNAKE_CASE ): return extractor.extract(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )
196
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase : List[Any] =logging.get_logger(__name__) lowerCamelCase : Optional[Any] ={ '''google/bit-50''': '''https://huggingface.co/google/bit-50/resolve/main/config.json''', } class __a ( A__ , A__ ): _lowerCAmelCase : Union[str, Any] = '''bit''' _lowerCAmelCase : List[str] = ['''preactivation''', '''bottleneck'''] _lowerCAmelCase : Any = ['''SAME''', '''VALID'''] def __init__( self : str , SCREAMING_SNAKE_CASE : Tuple=3 , SCREAMING_SNAKE_CASE : str=64 , SCREAMING_SNAKE_CASE : List[Any]=[2_56, 5_12, 10_24, 20_48] , SCREAMING_SNAKE_CASE : Union[str, Any]=[3, 4, 6, 3] , SCREAMING_SNAKE_CASE : str="preactivation" , SCREAMING_SNAKE_CASE : Any="relu" , SCREAMING_SNAKE_CASE : Dict=None , SCREAMING_SNAKE_CASE : List[Any]=32 , SCREAMING_SNAKE_CASE : Tuple=0.0 , SCREAMING_SNAKE_CASE : Dict=False , SCREAMING_SNAKE_CASE : int=32 , SCREAMING_SNAKE_CASE : str=1 , SCREAMING_SNAKE_CASE : List[str]=None , SCREAMING_SNAKE_CASE : Optional[int]=None , **SCREAMING_SNAKE_CASE : int , ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE ) if layer_type not in self.layer_types: raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types )}' ) if global_padding is not None: if global_padding.upper() in self.supported_padding: UpperCamelCase__ : Any = global_padding.upper() else: raise ValueError(F'Padding strategy {global_padding} not supported' ) UpperCamelCase__ : Dict = num_channels UpperCamelCase__ : Dict = embedding_size UpperCamelCase__ : Tuple = hidden_sizes UpperCamelCase__ : Any = depths UpperCamelCase__ : Optional[int] = layer_type UpperCamelCase__ : int = hidden_act UpperCamelCase__ : str = global_padding UpperCamelCase__ : Any = num_groups UpperCamelCase__ : str = drop_path_rate UpperCamelCase__ : Optional[Any] = embedding_dynamic_padding UpperCamelCase__ : Tuple = output_stride UpperCamelCase__ : List[str] = width_factor UpperCamelCase__ : Any = ["stem"] + [F'stage{idx}' for idx in range(1 , len(SCREAMING_SNAKE_CASE ) + 1 )] UpperCamelCase__ , UpperCamelCase__ : Union[str, Any] = get_aligned_output_features_output_indices( out_features=SCREAMING_SNAKE_CASE , out_indices=SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
196
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) a_ : Union[str, Any] = { """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""], """processing_trocr""": ["""TrOCRProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Union[str, 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_ : Tuple = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
75
from __future__ import annotations def snake_case_ ( snake_case , snake_case ) -> bool: if len(snake_case ) == 0: return False lowercase__: Any = len(snake_case ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , snake_case ) else: return binary_search(a_list[midpoint + 1 :] , snake_case ) if __name__ == "__main__": __lowerCAmelCase = input('''Enter numbers separated by comma:\n''').strip() __lowerCAmelCase = [int(item.strip()) for item in user_input.split(''',''')] __lowerCAmelCase = int(input('''Enter the number to be found in the list:\n''').strip()) __lowerCAmelCase = '''''' if binary_search(sequence, target) else '''not ''' print(F'''{target} was {not_str}found in {sequence}''')
196
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase__ = { "configuration_albert": ["ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "AlbertConfig", "AlbertOnnxConfig"], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = ["AlbertTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = ["AlbertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = [ "ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "AlbertForMaskedLM", "AlbertForMultipleChoice", "AlbertForPreTraining", "AlbertForQuestionAnswering", "AlbertForSequenceClassification", "AlbertForTokenClassification", "AlbertModel", "AlbertPreTrainedModel", "load_tf_weights_in_albert", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = [ "TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFAlbertForMaskedLM", "TFAlbertForMultipleChoice", "TFAlbertForPreTraining", "TFAlbertForQuestionAnswering", "TFAlbertForSequenceClassification", "TFAlbertForTokenClassification", "TFAlbertMainLayer", "TFAlbertModel", "TFAlbertPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = [ "FlaxAlbertForMaskedLM", "FlaxAlbertForMultipleChoice", "FlaxAlbertForPreTraining", "FlaxAlbertForQuestionAnswering", "FlaxAlbertForSequenceClassification", "FlaxAlbertForTokenClassification", "FlaxAlbertModel", "FlaxAlbertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, AlbertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert import AlbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert_fast import AlbertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_albert import ( ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, AlbertPreTrainedModel, load_tf_weights_in_albert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_albert import ( TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFAlbertForMaskedLM, TFAlbertForMultipleChoice, TFAlbertForPreTraining, TFAlbertForQuestionAnswering, TFAlbertForSequenceClassification, TFAlbertForTokenClassification, TFAlbertMainLayer, TFAlbertModel, TFAlbertPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, FlaxAlbertPreTrainedModel, ) else: import sys lowercase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
354
"""simple docstring""" from __future__ import annotations import math class __lowerCamelCase : '''simple docstring''' def __init__( self : Dict , a_ : int ): lowerCAmelCase_ : Union[str, Any] = size # approximate the overall size of segment tree with given value lowerCAmelCase_ : Union[str, Any] = [0 for i in range(0 , 4 * size )] # create array to store lazy update lowerCAmelCase_ : int = [0 for i in range(0 , 4 * size )] lowerCAmelCase_ : Any = [0 for i in range(0 , 4 * size )] # flag for lazy update def lowerCamelCase ( self : List[Any] , a_ : int ): return idx * 2 def lowerCamelCase ( self : Tuple , a_ : int ): return idx * 2 + 1 def lowerCamelCase ( self : Tuple , a_ : int , a_ : int , a_ : int , a_ : list[int] ): if left_element == right_element: lowerCAmelCase_ : Tuple = a[left_element - 1] else: lowerCAmelCase_ : Tuple = (left_element + right_element) // 2 self.build(self.left(a_ ) , a_ , a_ , a_ ) self.build(self.right(a_ ) , mid + 1 , a_ , a_ ) lowerCAmelCase_ : int = max( self.segment_tree[self.left(a_ )] , self.segment_tree[self.right(a_ )] ) def lowerCamelCase ( self : Union[str, Any] , a_ : int , a_ : int , a_ : int , a_ : int , a_ : int , a_ : int ): if self.flag[idx] is True: lowerCAmelCase_ : Dict = self.lazy[idx] lowerCAmelCase_ : Optional[Any] = False if left_element != right_element: lowerCAmelCase_ : str = self.lazy[idx] lowerCAmelCase_ : Dict = self.lazy[idx] lowerCAmelCase_ : List[Any] = True lowerCAmelCase_ : Union[str, Any] = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: lowerCAmelCase_ : Dict = val if left_element != right_element: lowerCAmelCase_ : Union[str, Any] = val lowerCAmelCase_ : Dict = val lowerCAmelCase_ : List[Any] = True lowerCAmelCase_ : List[str] = True return True lowerCAmelCase_ : Optional[Any] = (left_element + right_element) // 2 self.update(self.left(a_ ) , a_ , a_ , a_ , a_ , a_ ) self.update(self.right(a_ ) , mid + 1 , a_ , a_ , a_ , a_ ) lowerCAmelCase_ : int = max( self.segment_tree[self.left(a_ )] , self.segment_tree[self.right(a_ )] ) return True def lowerCamelCase ( self : int , a_ : int , a_ : int , a_ : int , a_ : int , a_ : int ): if self.flag[idx] is True: lowerCAmelCase_ : Union[str, Any] = self.lazy[idx] lowerCAmelCase_ : Optional[int] = False if left_element != right_element: lowerCAmelCase_ : int = self.lazy[idx] lowerCAmelCase_ : int = self.lazy[idx] lowerCAmelCase_ : Optional[Any] = True lowerCAmelCase_ : Dict = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] lowerCAmelCase_ : Any = (left_element + right_element) // 2 lowerCAmelCase_ : Union[str, Any] = self.query(self.left(a_ ) , a_ , a_ , a_ , a_ ) lowerCAmelCase_ : List[str] = self.query(self.right(a_ ) , mid + 1 , a_ , a_ , a_ ) return max(a_ , a_ ) def __str__( self : str ): return str([self.query(1 , 1 , self.size , a_ , a_ ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": lowercase__ = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] lowercase__ = 15 lowercase__ = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 11)) print(segt.query(1, 1, size, 7, 12)) segt.update(1, 1, size, 1, 3, 111) print(segt.query(1, 1, size, 1, 15)) segt.update(1, 1, size, 7, 8, 235) print(segt)
161
0
'''simple docstring''' from collections import deque class _UpperCAmelCase : """simple docstring""" def __init__( self : List[Any] , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : int ): '''simple docstring''' _A = process_name # process name _A = arrival_time # arrival time of the process # completion time of finished process or last interrupted time _A = arrival_time _A = burst_time # remaining burst time _A = 0 # total time of the process wait in ready queue _A = 0 # time from arrival time to completion time class _UpperCAmelCase : """simple docstring""" def __init__( self : str , __UpperCAmelCase : int , __UpperCAmelCase : list[int] , __UpperCAmelCase : deque[Process] , __UpperCAmelCase : int , ): '''simple docstring''' _A = number_of_queues # time slice of queues that round robin algorithm applied _A = time_slices # unfinished process is in this ready_queue _A = queue # current time _A = current_time # finished process is in this sequence queue _A = deque() def lowerCAmelCase ( self : Any ): '''simple docstring''' _A = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def lowerCAmelCase ( self : Any , __UpperCAmelCase : list[Process] ): '''simple docstring''' _A = [] for i in range(len(__UpperCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def lowerCAmelCase ( self : List[str] , __UpperCAmelCase : list[Process] ): '''simple docstring''' _A = [] for i in range(len(__UpperCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def lowerCAmelCase ( self : Dict , __UpperCAmelCase : list[Process] ): '''simple docstring''' _A = [] for i in range(len(__UpperCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def lowerCAmelCase ( self : Any , __UpperCAmelCase : deque[Process] ): '''simple docstring''' return [q.burst_time for q in queue] def lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : Process ): '''simple docstring''' process.waiting_time += self.current_time - process.stop_time return process.waiting_time def lowerCAmelCase ( self : Tuple , __UpperCAmelCase : deque[Process] ): '''simple docstring''' _A = deque() # sequence deque of finished process while len(__UpperCAmelCase ) != 0: _A = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__UpperCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 _A = 0 # set the process's turnaround time because it is finished _A = self.current_time - cp.arrival_time # set the completion time _A = self.current_time # add the process to queue that has finished queue finished.append(__UpperCAmelCase ) self.finish_queue.extend(__UpperCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def lowerCAmelCase ( self : Tuple , __UpperCAmelCase : deque[Process] , __UpperCAmelCase : int ): '''simple docstring''' _A = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__UpperCAmelCase ) ): _A = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__UpperCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time _A = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__UpperCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished _A = 0 # set the finish time _A = self.current_time # update the process' turnaround time because it is finished _A = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__UpperCAmelCase ) self.finish_queue.extend(__UpperCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def lowerCAmelCase ( self : str ): '''simple docstring''' for i in range(self.number_of_queues - 1 ): _A , _A = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest lowerCamelCase_ = Process('''P1''', 0, 53) lowerCamelCase_ = Process('''P2''', 0, 17) lowerCamelCase_ = Process('''P3''', 0, 68) lowerCamelCase_ = Process('''P4''', 0, 24) lowerCamelCase_ = 3 lowerCamelCase_ = [17, 25] lowerCamelCase_ = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) lowerCamelCase_ = Process('''P1''', 0, 53) lowerCamelCase_ = Process('''P2''', 0, 17) lowerCamelCase_ = Process('''P3''', 0, 68) lowerCamelCase_ = Process('''P4''', 0, 24) lowerCamelCase_ = 3 lowerCamelCase_ = [17, 25] lowerCamelCase_ = deque([Pa, Pa, Pa, Pa]) lowerCamelCase_ = MLFQ(number_of_queues, time_slices, queue, 0) lowerCamelCase_ = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F"""waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}""" ) # print completion times of processes(P1, P2, P3, P4) print( F"""completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}""" ) # print total turnaround times of processes(P1, P2, P3, P4) print( F"""turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}""" ) # print sequence of finished processes print( F"""sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}""" )
79
import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py a : List[str] = "src/diffusers" a : str = "." # This is to make sure the diffusers module imported is the one in the repo. a : Tuple = importlib.util.spec_from_file_location( "diffusers", os.path.join(DIFFUSERS_PATH, "__init__.py"), submodule_search_locations=[DIFFUSERS_PATH], ) a : List[str] = spec.loader.load_module() def lowerCamelCase__ ( __lowerCamelCase : Dict , __lowerCamelCase : Tuple ): return line.startswith(__lowerCamelCase ) or len(__lowerCamelCase ) <= 1 or re.search(R"""^\s*\)(\s*->.*:|:)\s*$""" , __lowerCamelCase ) is not None def lowerCamelCase__ ( __lowerCamelCase : Any ): __UpperCAmelCase : Optional[int] = object_name.split(""".""" ) __UpperCAmelCase : List[Any] = 0 # First let's find the module where our object lives. __UpperCAmelCase : Optional[Any] = parts[i] while i < len(__lowerCamelCase ) and not os.path.isfile(os.path.join(__lowerCamelCase , f"""{module}.py""" ) ): i += 1 if i < len(__lowerCamelCase ): __UpperCAmelCase : List[str] = os.path.join(__lowerCamelCase , parts[i] ) if i >= len(__lowerCamelCase ): raise ValueError(f"""`object_name` should begin with the name of a module of diffusers but got {object_name}.""" ) with open(os.path.join(__lowerCamelCase , f"""{module}.py""" ) , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __UpperCAmelCase : Optional[Any] = f.readlines() # Now let's find the class / func in the code! __UpperCAmelCase : List[str] = """""" __UpperCAmelCase : int = 0 for name in parts[i + 1 :]: while ( line_index < len(__lowerCamelCase ) and re.search(Rf"""^{indent}(class|def)\s+{name}(\(|\:)""" , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(__lowerCamelCase ): raise ValueError(f""" {object_name} does not match any function or class in {module}.""" ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). __UpperCAmelCase : List[str] = line_index while line_index < len(__lowerCamelCase ) and _should_continue(lines[line_index] , __lowerCamelCase ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 __UpperCAmelCase : Dict = lines[start_index:line_index] return "".join(__lowerCamelCase ) a : Any = re.compile(r"^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)") a : Optional[int] = re.compile(r"^\s*(\S+)->(\S+)(\s+.*|$)") a : Dict = re.compile(r"<FILL\s+[^>]*>") def lowerCamelCase__ ( __lowerCamelCase : List[Any] ): __UpperCAmelCase : Optional[Any] = code.split("""\n""" ) __UpperCAmelCase : str = 0 while idx < len(__lowerCamelCase ) and len(lines[idx] ) == 0: idx += 1 if idx < len(__lowerCamelCase ): return re.search(R"""^(\s*)\S""" , lines[idx] ).groups()[0] return "" def lowerCamelCase__ ( __lowerCamelCase : List[str] ): __UpperCAmelCase : Tuple = len(get_indent(__lowerCamelCase ) ) > 0 if has_indent: __UpperCAmelCase : Optional[Any] = f"""class Bla:\n{code}""" __UpperCAmelCase : Dict = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 , preview=__lowerCamelCase ) __UpperCAmelCase : Dict = black.format_str(__lowerCamelCase , mode=__lowerCamelCase ) __UpperCAmelCase , __UpperCAmelCase : Any = style_docstrings_in_code(__lowerCamelCase ) return result[len("""class Bla:\n""" ) :] if has_indent else result def lowerCamelCase__ ( __lowerCamelCase : Dict , __lowerCamelCase : Optional[Any]=False ): with open(__lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __UpperCAmelCase : Optional[Any] = f.readlines() __UpperCAmelCase : Optional[int] = [] __UpperCAmelCase : str = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(__lowerCamelCase ): __UpperCAmelCase : Dict = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : Union[str, Any] = search.groups() __UpperCAmelCase : Any = find_code_in_diffusers(__lowerCamelCase ) __UpperCAmelCase : Optional[int] = get_indent(__lowerCamelCase ) __UpperCAmelCase : Tuple = line_index + 1 if indent == theoretical_indent else line_index + 2 __UpperCAmelCase : Any = theoretical_indent __UpperCAmelCase : Any = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. __UpperCAmelCase : int = True while line_index < len(__lowerCamelCase ) and should_continue: line_index += 1 if line_index >= len(__lowerCamelCase ): break __UpperCAmelCase : List[Any] = lines[line_index] __UpperCAmelCase : str = _should_continue(__lowerCamelCase , __lowerCamelCase ) and re.search(f"""^{indent}# End copy""" , __lowerCamelCase ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 __UpperCAmelCase : Optional[int] = lines[start_index:line_index] __UpperCAmelCase : int = """""".join(__lowerCamelCase ) # Remove any nested `Copied from` comments to avoid circular copies __UpperCAmelCase : Tuple = [line for line in theoretical_code.split("""\n""" ) if _re_copy_warning.search(__lowerCamelCase ) is None] __UpperCAmelCase : List[Any] = """\n""".join(__lowerCamelCase ) # Before comparing, use the `replace_pattern` on the original code. if len(__lowerCamelCase ) > 0: __UpperCAmelCase : List[str] = replace_pattern.replace("""with""" , """""" ).split(""",""" ) __UpperCAmelCase : Any = [_re_replace_pattern.search(__lowerCamelCase ) for p in patterns] for pattern in patterns: if pattern is None: continue __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : Union[str, Any] = pattern.groups() __UpperCAmelCase : List[str] = re.sub(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) if option.strip() == "all-casing": __UpperCAmelCase : List[Any] = re.sub(obja.lower() , obja.lower() , __lowerCamelCase ) __UpperCAmelCase : int = re.sub(obja.upper() , obja.upper() , __lowerCamelCase ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line __UpperCAmelCase : Union[str, Any] = blackify(lines[start_index - 1] + theoretical_code ) __UpperCAmelCase : Optional[Any] = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: __UpperCAmelCase : int = lines[:start_index] + [theoretical_code] + lines[line_index:] __UpperCAmelCase : Union[str, Any] = start_index + 1 if overwrite and len(__lowerCamelCase ) > 0: # Warn the user a file has been modified. print(f"""Detected changes, rewriting {filename}.""" ) with open(__lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(__lowerCamelCase ) return diffs def lowerCamelCase__ ( __lowerCamelCase : bool = False ): __UpperCAmelCase : Tuple = glob.glob(os.path.join(__lowerCamelCase , """**/*.py""" ) , recursive=__lowerCamelCase ) __UpperCAmelCase : Optional[int] = [] for filename in all_files: __UpperCAmelCase : str = is_copy_consistent(__lowerCamelCase , __lowerCamelCase ) diffs += [f"""- {filename}: copy does not match {d[0]} at line {d[1]}""" for d in new_diffs] if not overwrite and len(__lowerCamelCase ) > 0: __UpperCAmelCase : Union[str, Any] = """\n""".join(__lowerCamelCase ) raise Exception( """Found the following copy inconsistencies:\n""" + diff + """\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.""" ) if __name__ == "__main__": a : Dict = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") a : Optional[int] = parser.parse_args() check_copies(args.fix_and_overwrite)
114
0
'''simple docstring''' import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors _a : Optional[int] = logging.getLogger(__name__) class _UpperCAmelCase ( lowerCAmelCase_ ): a : str ="""sequence-classification""" def __init__( self,__SCREAMING_SNAKE_CASE ): '''simple docstring''' if type(__SCREAMING_SNAKE_CASE ) == dict: __lowerCAmelCase = Namespace(**__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = glue_output_modes[hparams.task] __lowerCAmelCase = glue_tasks_num_labels[hparams.task] super().__init__(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,self.mode ) def lowerCamelCase__ ( self,**__SCREAMING_SNAKE_CASE ): '''simple docstring''' return self.model(**__SCREAMING_SNAKE_CASE ) def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCAmelCase = {"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: __lowerCAmelCase = batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None __lowerCAmelCase = self(**__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = outputs[0] __lowerCAmelCase = self.trainer.lr_schedulers[0]["""scheduler"""] __lowerCAmelCase = {"""loss""": loss, """rate""": lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def lowerCamelCase__ ( self ): '''simple docstring''' __lowerCAmelCase = self.hparams __lowerCAmelCase = processors[args.task]() __lowerCAmelCase = processor.get_labels() for mode in ["train", "dev"]: __lowerCAmelCase = self._feature_file(__SCREAMING_SNAKE_CASE ) if os.path.exists(__SCREAMING_SNAKE_CASE ) and not args.overwrite_cache: logger.info("""Loading features from cached file %s""",__SCREAMING_SNAKE_CASE ) else: logger.info("""Creating features from dataset file at %s""",args.data_dir ) __lowerCAmelCase = ( processor.get_dev_examples(args.data_dir ) if mode == """dev""" else processor.get_train_examples(args.data_dir ) ) __lowerCAmelCase = convert_examples_to_features( __SCREAMING_SNAKE_CASE,self.tokenizer,max_length=args.max_seq_length,label_list=self.labels,output_mode=args.glue_output_mode,) logger.info("""Saving features into cached file %s""",__SCREAMING_SNAKE_CASE ) torch.save(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ) def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE = False ): '''simple docstring''' __lowerCAmelCase = """dev""" if mode == """test""" else mode __lowerCAmelCase = self._feature_file(__SCREAMING_SNAKE_CASE ) logger.info("""Loading features from cached file %s""",__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = torch.load(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = torch.tensor([f.input_ids for f in features],dtype=torch.long ) __lowerCAmelCase = torch.tensor([f.attention_mask for f in features],dtype=torch.long ) __lowerCAmelCase = torch.tensor([f.token_type_ids for f in features],dtype=torch.long ) if self.hparams.glue_output_mode == "classification": __lowerCAmelCase = torch.tensor([f.label for f in features],dtype=torch.long ) elif self.hparams.glue_output_mode == "regression": __lowerCAmelCase = torch.tensor([f.label for f in features],dtype=torch.float ) return DataLoader( TensorDataset(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ),batch_size=__SCREAMING_SNAKE_CASE,shuffle=__SCREAMING_SNAKE_CASE,) def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCAmelCase = {"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: __lowerCAmelCase = batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None __lowerCAmelCase = self(**__SCREAMING_SNAKE_CASE ) __lowerCAmelCase , __lowerCAmelCase = outputs[:2] __lowerCAmelCase = logits.detach().cpu().numpy() __lowerCAmelCase = inputs["""labels"""].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCAmelCase = torch.stack([x["""val_loss"""] for x in outputs] ).mean().detach().cpu().item() __lowerCAmelCase = np.concatenate([x["""pred"""] for x in outputs],axis=0 ) if self.hparams.glue_output_mode == "classification": __lowerCAmelCase = np.argmax(__SCREAMING_SNAKE_CASE,axis=1 ) elif self.hparams.glue_output_mode == "regression": __lowerCAmelCase = np.squeeze(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = np.concatenate([x["""target"""] for x in outputs],axis=0 ) __lowerCAmelCase = [[] for _ in range(out_label_ids.shape[0] )] __lowerCAmelCase = [[] for _ in range(out_label_ids.shape[0] )] __lowerCAmelCase = {**{"""val_loss""": val_loss_mean}, **compute_metrics(self.hparams.task,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE )} __lowerCAmelCase = dict(results.items() ) __lowerCAmelCase = results return ret, preds_list, out_label_list def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = self._eval_end(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = ret["""log"""] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = self._eval_end(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = ret["""log"""] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def lowerCamelCase__ ( __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ): '''simple docstring''' BaseTransformer.add_model_specific_args(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ) parser.add_argument( """--max_seq_length""",default=1_28,type=__SCREAMING_SNAKE_CASE,help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ),) parser.add_argument( """--task""",default="""""",type=__SCREAMING_SNAKE_CASE,required=__SCREAMING_SNAKE_CASE,help="""The GLUE task to run""",) parser.add_argument( """--gpus""",default=0,type=__SCREAMING_SNAKE_CASE,help="""The number of GPUs allocated for this, it is by default 0 meaning none""",) parser.add_argument( """--overwrite_cache""",action="""store_true""",help="""Overwrite the cached training and evaluation sets""" ) return parser def _lowerCAmelCase ( ) -> str: __lowerCAmelCase = argparse.ArgumentParser() add_generic_args(lowercase , os.getcwd() ) __lowerCAmelCase = GLUETransformer.add_model_specific_args(lowercase , os.getcwd() ) __lowerCAmelCase = parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: __lowerCAmelCase = os.path.join( """./results""" , f'{args.task}_{time.strftime("%Y%m%d_%H%M%S" )}' , ) os.makedirs(args.output_dir ) __lowerCAmelCase = GLUETransformer(lowercase ) __lowerCAmelCase = generic_train(lowercase , lowercase ) # Optionally, predict on dev set and write to output_dir if args.do_predict: __lowerCAmelCase = sorted(glob.glob(os.path.join(args.output_dir , """checkpoint-epoch=*.ckpt""" ) , recursive=lowercase ) ) __lowerCAmelCase = model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(lowercase ) if __name__ == "__main__": main()
46
'''simple docstring''' import os import shutil from pathlib import Path from typing import Optional, Union import numpy as np from huggingface_hub import hf_hub_download from ..utils import ONNX_EXTERNAL_WEIGHTS_NAME, ONNX_WEIGHTS_NAME, is_onnx_available, logging if is_onnx_available(): import onnxruntime as ort _a : List[Any] = logging.get_logger(__name__) _a : Union[str, Any] = { """tensor(bool)""": np.bool_, """tensor(int8)""": np.inta, """tensor(uint8)""": np.uinta, """tensor(int16)""": np.intaa, """tensor(uint16)""": np.uintaa, """tensor(int32)""": np.intaa, """tensor(uint32)""": np.uintaa, """tensor(int64)""": np.intaa, """tensor(uint64)""": np.uintaa, """tensor(float16)""": np.floataa, """tensor(float)""": np.floataa, """tensor(double)""": np.floataa, } class _UpperCAmelCase : def __init__( self,__SCREAMING_SNAKE_CASE=None,**__SCREAMING_SNAKE_CASE ): '''simple docstring''' logger.info("""`diffusers.OnnxRuntimeModel` is experimental and might change in the future.""" ) __lowerCAmelCase = model __lowerCAmelCase = kwargs.get("""model_save_dir""",__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = kwargs.get("""latest_model_name""",__SCREAMING_SNAKE_CASE ) def __call__( self,**__SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCAmelCase = {k: np.array(__SCREAMING_SNAKE_CASE ) for k, v in kwargs.items()} return self.model.run(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ) @staticmethod def lowerCamelCase__ ( __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE=None,__SCREAMING_SNAKE_CASE=None ): '''simple docstring''' if provider is None: logger.info("""No onnxruntime provider specified, using CPUExecutionProvider""" ) __lowerCAmelCase = """CPUExecutionProvider""" return ort.InferenceSession(__SCREAMING_SNAKE_CASE,providers=[provider],sess_options=__SCREAMING_SNAKE_CASE ) def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE = None,**__SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCAmelCase = file_name if file_name is not None else ONNX_WEIGHTS_NAME __lowerCAmelCase = self.model_save_dir.joinpath(self.latest_model_name ) __lowerCAmelCase = Path(__SCREAMING_SNAKE_CASE ).joinpath(__SCREAMING_SNAKE_CASE ) try: shutil.copyfile(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ) except shutil.SameFileError: pass # copy external weights (for models >2GB) __lowerCAmelCase = self.model_save_dir.joinpath(__SCREAMING_SNAKE_CASE ) if src_path.exists(): __lowerCAmelCase = Path(__SCREAMING_SNAKE_CASE ).joinpath(__SCREAMING_SNAKE_CASE ) try: shutil.copyfile(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ) except shutil.SameFileError: pass def lowerCamelCase__ ( self,__SCREAMING_SNAKE_CASE,**__SCREAMING_SNAKE_CASE,): '''simple docstring''' if os.path.isfile(__SCREAMING_SNAKE_CASE ): logger.error(f'Provided path ({save_directory}) should be a directory, not a file' ) return os.makedirs(__SCREAMING_SNAKE_CASE,exist_ok=__SCREAMING_SNAKE_CASE ) # saving model weights/files self._save_pretrained(__SCREAMING_SNAKE_CASE,**__SCREAMING_SNAKE_CASE ) @classmethod def lowerCamelCase__ ( cls,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE = None,__SCREAMING_SNAKE_CASE = None,__SCREAMING_SNAKE_CASE = False,__SCREAMING_SNAKE_CASE = None,__SCREAMING_SNAKE_CASE = None,__SCREAMING_SNAKE_CASE = None,__SCREAMING_SNAKE_CASE = None,**__SCREAMING_SNAKE_CASE,): '''simple docstring''' __lowerCAmelCase = file_name if file_name is not None else ONNX_WEIGHTS_NAME # load model from local directory if os.path.isdir(__SCREAMING_SNAKE_CASE ): __lowerCAmelCase = OnnxRuntimeModel.load_model( os.path.join(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE ),provider=__SCREAMING_SNAKE_CASE,sess_options=__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = Path(__SCREAMING_SNAKE_CASE ) # load model from hub else: # download model __lowerCAmelCase = hf_hub_download( repo_id=__SCREAMING_SNAKE_CASE,filename=__SCREAMING_SNAKE_CASE,use_auth_token=__SCREAMING_SNAKE_CASE,revision=__SCREAMING_SNAKE_CASE,cache_dir=__SCREAMING_SNAKE_CASE,force_download=__SCREAMING_SNAKE_CASE,) __lowerCAmelCase = Path(__SCREAMING_SNAKE_CASE ).parent __lowerCAmelCase = Path(__SCREAMING_SNAKE_CASE ).name __lowerCAmelCase = OnnxRuntimeModel.load_model(__SCREAMING_SNAKE_CASE,provider=__SCREAMING_SNAKE_CASE,sess_options=__SCREAMING_SNAKE_CASE ) return cls(model=__SCREAMING_SNAKE_CASE,**__SCREAMING_SNAKE_CASE ) @classmethod def lowerCamelCase__ ( cls,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE = True,__SCREAMING_SNAKE_CASE = None,__SCREAMING_SNAKE_CASE = None,**__SCREAMING_SNAKE_CASE,): '''simple docstring''' __lowerCAmelCase = None if len(str(__SCREAMING_SNAKE_CASE ).split("""@""" ) ) == 2: __lowerCAmelCase , __lowerCAmelCase = model_id.split("""@""" ) return cls._from_pretrained( model_id=__SCREAMING_SNAKE_CASE,revision=__SCREAMING_SNAKE_CASE,cache_dir=__SCREAMING_SNAKE_CASE,force_download=__SCREAMING_SNAKE_CASE,use_auth_token=__SCREAMING_SNAKE_CASE,**__SCREAMING_SNAKE_CASE,)
46
1
"""simple docstring""" import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class SCREAMING_SNAKE_CASE__ ( lowerCAmelCase__ ): def SCREAMING_SNAKE_CASE ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[Any] = tempfile.mkdtemp() UpperCAmelCase : Tuple = 8 # DPR tok UpperCAmelCase : Tuple = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] UpperCAmelCase : Tuple = os.path.join(self.tmpdirname , """dpr_tokenizer""" ) os.makedirs(lowercase_ , exist_ok=lowercase_ ) UpperCAmelCase : List[str] = os.path.join(lowercase_ , DPR_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] ) ) # BART tok UpperCAmelCase : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] UpperCAmelCase : Union[str, Any] = dict(zip(lowercase_ , range(len(lowercase_ ) ) ) ) UpperCAmelCase : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCAmelCase : List[str] = {'''unk_token''': '''<unk>'''} UpperCAmelCase : Any = os.path.join(self.tmpdirname , """bart_tokenizer""" ) os.makedirs(lowercase_ , exist_ok=lowercase_ ) UpperCAmelCase : Optional[int] = os.path.join(lowercase_ , BART_VOCAB_FILES_NAMES["""vocab_file"""] ) UpperCAmelCase : int = os.path.join(lowercase_ , BART_VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(lowercase_ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(lowercase_ ) ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: '''simple docstring''' return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def SCREAMING_SNAKE_CASE ( self ) -> Tuple: '''simple docstring''' return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , """bart_tokenizer""" ) ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' shutil.rmtree(self.tmpdirname ) @require_tokenizers def SCREAMING_SNAKE_CASE ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : Optional[int] = os.path.join(self.tmpdirname , """rag_tokenizer""" ) UpperCAmelCase : List[str] = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) UpperCAmelCase : Tuple = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(lowercase_ ) rag_tokenizer.save_pretrained(lowercase_ ) UpperCAmelCase : Optional[Any] = RagTokenizer.from_pretrained(lowercase_ , config=lowercase_ ) self.assertIsInstance(new_rag_tokenizer.question_encoder , lowercase_ ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , lowercase_ ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def SCREAMING_SNAKE_CASE ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple = RagTokenizer.from_pretrained("""facebook/rag-token-nq""" ) UpperCAmelCase : List[str] = [ '''who got the first nobel prize in physics''', '''when is the next deadpool movie being released''', '''which mode is used for short wave broadcast service''', '''who is the owner of reading football club''', '''when is the next scandal episode coming out''', '''when is the last time the philadelphia won the superbowl''', '''what is the most current adobe flash player version''', '''how many episodes are there in dragon ball z''', '''what is the first step in the evolution of the eye''', '''where is gall bladder situated in human body''', '''what is the main mineral in lithium batteries''', '''who is the president of usa right now''', '''where do the greasers live in the outsiders''', '''panda is a national animal of which country''', '''what is the name of manchester united stadium''', ] UpperCAmelCase : Optional[Any] = tokenizer(lowercase_ ) self.assertIsNotNone(lowercase_ ) @slow def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' UpperCAmelCase : int = RagTokenizer.from_pretrained("""facebook/rag-sequence-nq""" ) UpperCAmelCase : Any = [ '''who got the first nobel prize in physics''', '''when is the next deadpool movie being released''', '''which mode is used for short wave broadcast service''', '''who is the owner of reading football club''', '''when is the next scandal episode coming out''', '''when is the last time the philadelphia won the superbowl''', '''what is the most current adobe flash player version''', '''how many episodes are there in dragon ball z''', '''what is the first step in the evolution of the eye''', '''where is gall bladder situated in human body''', '''what is the main mineral in lithium batteries''', '''who is the president of usa right now''', '''where do the greasers live in the outsiders''', '''panda is a national animal of which country''', '''what is the name of manchester united stadium''', ] UpperCAmelCase : Optional[int] = tokenizer(lowercase_ ) self.assertIsNotNone(lowercase_ )
109
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_pegasus import PegasusTokenizer else: lowercase__ : int = None lowercase__ : Any = logging.get_logger(__name__) lowercase__ : List[str] = '''▁''' lowercase__ : Optional[int] = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} lowercase__ : str = { '''vocab_file''': {'''google/pegasus-xsum''': '''https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'''}, '''tokenizer_file''': { '''google/pegasus-xsum''': '''https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json''' }, } lowercase__ : List[Any] = { '''google/pegasus-xsum''': 5_12, } class _UpperCAmelCase ( lowerCAmelCase__): _lowerCAmelCase : List[str] = VOCAB_FILES_NAMES _lowerCAmelCase : List[str] = PRETRAINED_VOCAB_FILES_MAP _lowerCAmelCase : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCAmelCase : Tuple = PegasusTokenizer _lowerCAmelCase : str = ["""input_ids""", """attention_mask"""] def __init__( self : Any , lowercase_ : Optional[Any]=None , lowercase_ : int=None , lowercase_ : Tuple="<pad>" , lowercase_ : int="</s>" , lowercase_ : Tuple="<unk>" , lowercase_ : str="<mask_2>" , lowercase_ : Optional[Any]="<mask_1>" , lowercase_ : str=None , lowercase_ : List[str]=103 , **lowercase_ : List[Any] , ): snake_case_ : Dict = offset if additional_special_tokens is not None: if not isinstance(lowercase_ , lowercase_ ): raise TypeError( f"additional_special_tokens should be of type {type(lowercase_ )}, but is" f" {type(lowercase_ )}" ) snake_case_ : str = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ f"<unk_{i}>" for i in range(len(lowercase_ ) , self.offset - 1 ) ] if len(set(lowercase_ ) ) != len(lowercase_ ): raise ValueError( '''Please make sure that the provided additional_special_tokens do not contain an incorrectly''' f" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}." ) snake_case_ : Union[str, Any] = additional_special_tokens_extended else: snake_case_ : Dict = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [f"<unk_{i}>" for i in range(2 , self.offset )] super().__init__( lowercase_ , tokenizer_file=lowercase_ , pad_token=lowercase_ , eos_token=lowercase_ , unk_token=lowercase_ , mask_token=lowercase_ , mask_token_sent=lowercase_ , offset=lowercase_ , additional_special_tokens=lowercase_ , **lowercase_ , ) snake_case_ : List[Any] = vocab_file snake_case_ : List[Any] = False if not self.vocab_file else True def _snake_case ( self : str , lowercase_ : Union[str, Any] ): snake_case_ : Any = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( '''There should be 3 special tokens: mask_token, pad_token, and eos_token +''' f" {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}" ) return [1 if x in all_special_ids else 0 for x in seq] def _snake_case ( self : int , lowercase_ : List , lowercase_ : Optional[List] = None , lowercase_ : bool = False ): if already_has_special_tokens: return self._special_token_mask(lowercase_ ) elif token_ids_a is None: return self._special_token_mask(lowercase_ ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def _snake_case ( self : List[Any] , lowercase_ : Optional[int] , lowercase_ : str=None ): if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def _snake_case ( self : Optional[Any] , lowercase_ : str , lowercase_ : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(lowercase_ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return snake_case_ : Dict = os.path.join( lowercase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowercase_ ): copyfile(self.vocab_file , lowercase_ ) return (out_vocab_file,)
264
0
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () __lowerCamelCase : Dict = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). __lowerCamelCase : Union[str, Any] = [0, 25, 50] __lowerCamelCase : Optional[Any] = [25, 50, 75] __lowerCamelCase : Optional[int] = fuzz.membership.trimf(X, abca) __lowerCamelCase : List[str] = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. __lowerCamelCase : Tuple = np.ones(75) __lowerCamelCase : Dict = np.zeros((75,)) # 1. Union = max(µA(x), µB(x)) __lowerCamelCase : Optional[Any] = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) __lowerCamelCase : str = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) __lowerCamelCase : str = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) __lowerCamelCase : Dict = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] __lowerCamelCase : Dict = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) __lowerCamelCase : Union[str, Any] = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] __lowerCamelCase : Tuple = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] __lowerCamelCase : Optional[int] = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title("""Young""") plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title("""Middle aged""") plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title("""union""") plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title("""intersection""") plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title("""complement_a""") plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title("""difference a/b""") plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title("""alg_sum""") plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title("""alg_product""") plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title("""bdd_sum""") plt.grid(True) plt.subplot(4, 3, 10) plt.plot(X, bdd_difference) plt.title("""bdd_difference""") plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
358
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCamelCase : Any = { """configuration_instructblip""": [ """INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """InstructBlipConfig""", """InstructBlipQFormerConfig""", """InstructBlipVisionConfig""", ], """processing_instructblip""": ["""InstructBlipProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase : List[Any] = [ """INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """InstructBlipQFormerModel""", """InstructBlipPreTrainedModel""", """InstructBlipForConditionalGeneration""", """InstructBlipVisionModel""", ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys __lowerCamelCase : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
286
0
'''simple docstring''' # Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING, Dict, Optional import numpy as np import pyarrow as pa from .. import config from ..utils.logging import get_logger from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import jax import jaxlib __snake_case : List[str] = get_logger() __snake_case : Optional[dict] = None class lowerCamelCase ( TensorFormatter[Mapping, 'jax.Array', Mapping] ): '''simple docstring''' def __init__( self : List[Any] , lowerCAmelCase_ : int=None , lowerCAmelCase_ : Optional[Any]=None , **lowerCAmelCase_ : Any ) -> Optional[Any]: '''simple docstring''' super().__init__(features=lowerCAmelCase_ ) import jax from jaxlib.xla_client import Device if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): raise ValueError( f"Expected {device} to be a `str` not {type(lowerCAmelCase_ )}, as `jaxlib.xla_extension.Device` " """is not serializable neither with `pickle` nor with `dill`. Instead you can surround """ """the device with `str()` to get its string identifier that will be internally mapped """ """to the actual `jaxlib.xla_extension.Device`.""" ) A__ : Dict =device if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else str(jax.devices()[0] ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: A__ : str =self._map_devices_to_str() if self.device not in list(DEVICE_MAPPING.keys() ): logger.warning( f"Device with string identifier {self.device} not listed among the available " f"devices: {list(DEVICE_MAPPING.keys() )}, so falling back to the default " f"device: {str(jax.devices()[0] )}." ) A__ : int =str(jax.devices()[0] ) A__ : List[Any] =jnp_array_kwargs @staticmethod def lowercase__ ( ) -> Dict[str, "jaxlib.xla_extension.Device"]: '''simple docstring''' import jax return {str(lowerCAmelCase_ ): device for device in jax.devices()} def lowercase__ ( self : Dict , lowerCAmelCase_ : Union[str, Any] ) -> Dict: '''simple docstring''' import jax import jax.numpy as jnp if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) and column: if all( isinstance(lowerCAmelCase_ , jax.Array ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return jnp.stack(lowerCAmelCase_ , axis=0 ) return column def lowercase__ ( self : Tuple , lowerCAmelCase_ : str ) -> Union[str, Any]: '''simple docstring''' import jax import jax.numpy as jnp if isinstance(lowerCAmelCase_ , (str, bytes, type(lowerCAmelCase_ )) ): return value elif isinstance(lowerCAmelCase_ , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() A__ : Optional[int] ={} if isinstance(lowerCAmelCase_ , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.integer ): # the default int precision depends on the jax config # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision if jax.config.jax_enable_xaa: A__ : Optional[int] ={"""dtype""": jnp.intaa} else: A__ : int ={"""dtype""": jnp.intaa} elif isinstance(lowerCAmelCase_ , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.floating ): A__ : Optional[int] ={"""dtype""": jnp.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(lowerCAmelCase_ , PIL.Image.Image ): A__ : int =np.asarray(lowerCAmelCase_ ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: A__ : int =self._map_devices_to_str() with jax.default_device(DEVICE_MAPPING[self.device] ): # calling jnp.array on a np.ndarray does copy the data # see https://github.com/google/jax/issues/4486 return jnp.array(lowerCAmelCase_ , **{**default_dtype, **self.jnp_array_kwargs} ) def lowercase__ ( self : Tuple , lowerCAmelCase_ : Tuple ) -> int: '''simple docstring''' import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(lowerCAmelCase_ , torch.Tensor ): return self._tensorize(data_struct.detach().cpu().numpy()[()] ) if hasattr(lowerCAmelCase_ , """__array__""" ) and not isinstance(lowerCAmelCase_ , jax.Array ): A__ : int =data_struct.__array__() # support for nested types like struct of list of struct if isinstance(lowerCAmelCase_ , np.ndarray ): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(lowerCAmelCase_ ) for substruct in data_struct] ) elif isinstance(lowerCAmelCase_ , (list, tuple) ): return self._consolidate([self.recursive_tensorize(lowerCAmelCase_ ) for substruct in data_struct] ) return self._tensorize(lowerCAmelCase_ ) def lowercase__ ( self : Optional[Any] , lowerCAmelCase_ : dict ) -> Optional[Any]: '''simple docstring''' return map_nested(self._recursive_tensorize , lowerCAmelCase_ , map_list=lowerCAmelCase_ ) def lowercase__ ( self : Dict , lowerCAmelCase_ : pa.Table ) -> Mapping: '''simple docstring''' A__ : List[str] =self.numpy_arrow_extractor().extract_row(lowerCAmelCase_ ) A__ : List[str] =self.python_features_decoder.decode_row(lowerCAmelCase_ ) return self.recursive_tensorize(lowerCAmelCase_ ) def lowercase__ ( self : str , lowerCAmelCase_ : pa.Table ) -> "jax.Array": '''simple docstring''' A__ : Any =self.numpy_arrow_extractor().extract_column(lowerCAmelCase_ ) A__ : Any =self.python_features_decoder.decode_column(lowerCAmelCase_ , pa_table.column_names[0] ) A__ : List[Any] =self.recursive_tensorize(lowerCAmelCase_ ) A__ : List[str] =self._consolidate(lowerCAmelCase_ ) return column def lowercase__ ( self : Dict , lowerCAmelCase_ : pa.Table ) -> Mapping: '''simple docstring''' A__ : List[Any] =self.numpy_arrow_extractor().extract_batch(lowerCAmelCase_ ) A__ : int =self.python_features_decoder.decode_batch(lowerCAmelCase_ ) A__ : List[Any] =self.recursive_tensorize(lowerCAmelCase_ ) for column_name in batch: A__ : str =self._consolidate(batch[column_name] ) return batch
134
'''simple docstring''' 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, ) __snake_case : Any = pytest.mark.integration @pytest.mark.parametrize("""path""", ["""paws""", """csv"""] ) def __lowerCamelCase ( __snake_case : Any, __snake_case : List[Any] ) -> Optional[int]: """simple docstring""" inspect_dataset(__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.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""", ["""accuracy"""] ) def __lowerCamelCase ( __snake_case : Tuple, __snake_case : Any ) -> int: """simple docstring""" inspect_metric(__snake_case, __snake_case ) A__ : int =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 __lowerCamelCase ( __snake_case : List[str], __snake_case : Union[str, Any], __snake_case : Optional[int] ) -> Optional[int]: """simple docstring""" A__ : Optional[Any] =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 __lowerCamelCase ( __snake_case : Optional[int], __snake_case : Optional[Any], __snake_case : List[Any] ) -> List[str]: """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 __lowerCamelCase ( __snake_case : Tuple, __snake_case : str ) -> Optional[Any]: """simple docstring""" A__ : Optional[int] =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 __lowerCamelCase ( __snake_case : List[Any], __snake_case : Dict, __snake_case : Optional[Any] ) -> int: """simple docstring""" A__ : List[Any] =get_dataset_infos(__snake_case ) assert list(infos.keys() ) == expected_configs A__ : Optional[int] =expected_configs[0] assert expected_config in infos A__ : List[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 __lowerCamelCase ( __snake_case : Optional[int], __snake_case : List[Any], __snake_case : Optional[Any] ) -> Any: """simple docstring""" A__ : str =get_dataset_infos(__snake_case ) assert expected_config in infos A__ : int =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 __lowerCamelCase ( __snake_case : Dict, __snake_case : Optional[int], __snake_case : Dict ) -> Tuple: """simple docstring""" with pytest.raises(__snake_case ): get_dataset_split_names(__snake_case, config_name=__snake_case )
134
1
import operator as op _lowerCamelCase : int = "scaler.pt" _lowerCamelCase : Any = "pytorch_model" _lowerCamelCase : int = "random_states" _lowerCamelCase : str = "optimizer" _lowerCamelCase : Dict = "scheduler" _lowerCamelCase : Optional[int] = "pytorch_model.bin" _lowerCamelCase : List[str] = "pytorch_model.bin.index.json" _lowerCamelCase : Optional[Any] = "model.safetensors" _lowerCamelCase : Optional[Any] = "model.safetensors.index.json" _lowerCamelCase : List[Any] = "1.10.2" _lowerCamelCase : List[Any] = "py38" _lowerCamelCase : str = "4.17.0" _lowerCamelCase : str = ["ml.p3.16xlarge", "ml.p3dn.24xlarge", "ml.p4dn.24xlarge"] _lowerCamelCase : Optional[Any] = ["FULL_SHARD", "SHARD_GRAD_OP", "NO_SHARD", "HYBRID_SHARD", "HYBRID_SHARD_ZERO2"] _lowerCamelCase : Optional[int] = ["TRANSFORMER_BASED_WRAP", "SIZE_BASED_WRAP", "NO_WRAP"] _lowerCamelCase : str = ["BACKWARD_PRE", "BACKWARD_POST", "NO_PREFETCH"] _lowerCamelCase : Optional[Any] = ["FULL_STATE_DICT", "LOCAL_STATE_DICT", "SHARDED_STATE_DICT"] _lowerCamelCase : str = "2.0.1" _lowerCamelCase : Union[str, Any] = ["pdsh", "standard", "openmpi", "mvapich"] _lowerCamelCase : Optional[Any] = ["default", "reduce-overhead", "max-autotune"] _lowerCamelCase : Tuple = {">": op.gt, ">=": op.ge, "==": op.eq, "!=": op.ne, "<=": op.le, "<": op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 _lowerCamelCase : Optional[Any] = [ "nnodes", "nproc_per_node", "rdzv_backend", "rdzv_endpoint", "rdzv_id", "rdzv_conf", "standalone", "max_restarts", "monitor_interval", "start_method", "role", "module", "m", "no_python", "run_path", "log_dir", "r", "redirects", "t", "tee", "node_rank", "master_addr", "master_port", ] _lowerCamelCase : List[str] = ["DEEPSPEED", "MULTI_GPU", "FSDP", "MEGATRON_LM"] _lowerCamelCase : List[str] = ["DEEPSPEED", "MULTI_XPU", "FSDP"]
369
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : str = {"configuration_xlnet": ["XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLNetConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ["XLNetTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[Any] = ["XLNetTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Dict = [ "XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "XLNetForMultipleChoice", "XLNetForQuestionAnswering", "XLNetForQuestionAnsweringSimple", "XLNetForSequenceClassification", "XLNetForTokenClassification", "XLNetLMHeadModel", "XLNetModel", "XLNetPreTrainedModel", "load_tf_weights_in_xlnet", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Union[str, Any] = [ "TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLNetForMultipleChoice", "TFXLNetForQuestionAnsweringSimple", "TFXLNetForSequenceClassification", "TFXLNetForTokenClassification", "TFXLNetLMHeadModel", "TFXLNetMainLayer", "TFXLNetModel", "TFXLNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet import XLNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet_fast import XLNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlnet import ( XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, XLNetForMultipleChoice, XLNetForQuestionAnswering, XLNetForQuestionAnsweringSimple, XLNetForSequenceClassification, XLNetForTokenClassification, XLNetLMHeadModel, XLNetModel, XLNetPreTrainedModel, load_tf_weights_in_xlnet, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlnet import ( TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLNetForMultipleChoice, TFXLNetForQuestionAnsweringSimple, TFXLNetForSequenceClassification, TFXLNetForTokenClassification, TFXLNetLMHeadModel, TFXLNetMainLayer, TFXLNetModel, TFXLNetPreTrainedModel, ) else: import sys _lowerCamelCase : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
99
0
from __future__ import annotations import math __a :Tuple = '2020.9.26' __a :Any = 'xcodz-dot, cclaus, dhruvmanila' def __snake_case ( __UpperCamelCase : float ,__UpperCamelCase : float ,__UpperCamelCase : float ,__UpperCamelCase : float ,__UpperCamelCase : float ): """simple docstring""" if not all(isinstance(__UpperCamelCase ,(float, int) ) for val in locals().values() ): A_ = f'''Input values must either be float or int: {list(locals().values() )}''' raise TypeError(__UpperCamelCase ) A_ = ((x * distance) / (z + distance)) * scale A_ = ((y * distance) / (z + distance)) * scale return projected_x, projected_y def __snake_case ( __UpperCamelCase : float ,__UpperCamelCase : float ,__UpperCamelCase : float ,__UpperCamelCase : str ,__UpperCamelCase : float ): """simple docstring""" if not isinstance(__UpperCamelCase ,__UpperCamelCase ): raise TypeError("Axis must be a str" ) A_ = locals() del input_variables["axis"] if not all(isinstance(__UpperCamelCase ,(float, int) ) for val in input_variables.values() ): A_ = ( "Input values except axis must either be float or int: " f'''{list(input_variables.values() )}''' ) raise TypeError(__UpperCamelCase ) A_ = (angle % 360) / 450 * 180 / math.pi if axis == "z": A_ = x * math.cos(__UpperCamelCase ) - y * math.sin(__UpperCamelCase ) A_ = y * math.cos(__UpperCamelCase ) + x * math.sin(__UpperCamelCase ) A_ = z elif axis == "x": A_ = y * math.cos(__UpperCamelCase ) - z * math.sin(__UpperCamelCase ) A_ = z * math.cos(__UpperCamelCase ) + y * math.sin(__UpperCamelCase ) A_ = x elif axis == "y": A_ = x * math.cos(__UpperCamelCase ) - z * math.sin(__UpperCamelCase ) A_ = z * math.cos(__UpperCamelCase ) + x * math.sin(__UpperCamelCase ) A_ = y else: raise ValueError("not a valid axis, choose one of 'x', 'y', 'z'" ) return new_x, new_y, new_z if __name__ == "__main__": import doctest doctest.testmod() print(F"{convert_to_ad(1.0, 2.0, 3.0, 10.0, 10.0) = }") print(F"{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }")
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(e for e in range(3 ,__UpperCamelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
312
1
"""simple docstring""" from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class lowerCAmelCase__ : def __init__( self : Any , _lowerCamelCase : Optional[Any] , ): _snake_case = parent _snake_case = 13 _snake_case = 7 _snake_case = 30 _snake_case = self.seq_length + self.mem_len _snake_case = 15 _snake_case = True _snake_case = True _snake_case = 99 _snake_case = [10, 50, 80] _snake_case = 32 _snake_case = 32 _snake_case = 4 _snake_case = 8 _snake_case = 128 _snake_case = 2 _snake_case = 2 _snake_case = None _snake_case = 1 _snake_case = 0 _snake_case = 3 _snake_case = self.vocab_size - 1 _snake_case = 0.0_1 def lowercase ( self : Optional[int] ): _snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _snake_case = None if self.use_labels: _snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _snake_case = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def lowercase ( self : Any ): random.seed(self.seed ) tf.random.set_seed(self.seed ) def lowercase ( self : Dict , _lowerCamelCase : int , _lowerCamelCase : List[Any] , _lowerCamelCase : Dict , _lowerCamelCase : List[str] ): _snake_case = TFTransfoXLModel(_lowerCamelCase ) _snake_case , _snake_case = model(_lowerCamelCase ).to_tuple() _snake_case = {'''input_ids''': input_ids_a, '''mems''': mems_a} _snake_case , _snake_case = model(_lowerCamelCase ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def lowercase ( self : List[Any] , _lowerCamelCase : Dict , _lowerCamelCase : Optional[Any] , _lowerCamelCase : Optional[int] , _lowerCamelCase : Tuple ): _snake_case = TFTransfoXLLMHeadModel(_lowerCamelCase ) _snake_case , _snake_case = model(_lowerCamelCase ).to_tuple() _snake_case = {'''input_ids''': input_ids_a, '''labels''': lm_labels} _snake_case , _snake_case = model(_lowerCamelCase ).to_tuple() _snake_case , _snake_case = model([input_ids_a, mems_a] ).to_tuple() _snake_case = {'''input_ids''': input_ids_a, '''mems''': mems_a, '''labels''': lm_labels} _snake_case , _snake_case = model(_lowerCamelCase ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def lowercase ( self : Any , _lowerCamelCase : Optional[Any] , _lowerCamelCase : Optional[int] , _lowerCamelCase : Optional[Any] , _lowerCamelCase : List[str] ): _snake_case = TFTransfoXLForSequenceClassification(_lowerCamelCase ) _snake_case = model(_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase ( self : str ): _snake_case = self.prepare_config_and_inputs() ((_snake_case) , (_snake_case) , (_snake_case) , (_snake_case)) = config_and_inputs _snake_case = {'''input_ids''': input_ids_a} return config, inputs_dict @require_tf class lowerCAmelCase__ ( A_ , A_ , unittest.TestCase ): __a = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) __a = () if is_tf_available() else () __a = ( { """feature-extraction""": TFTransfoXLModel, """text-classification""": TFTransfoXLForSequenceClassification, """text-generation""": TFTransfoXLLMHeadModel, """zero-shot""": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented __a = False __a = False __a = False __a = False def lowercase ( self : List[Any] , _lowerCamelCase : List[Any] , _lowerCamelCase : Tuple , _lowerCamelCase : List[Any] , _lowerCamelCase : Optional[int] , _lowerCamelCase : Union[str, Any] ): if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def lowercase ( self : List[Any] ): _snake_case = TFTransfoXLModelTester(self ) _snake_case = ConfigTester(self , config_class=_lowerCamelCase , d_embed=37 ) def lowercase ( self : List[str] ): self.config_tester.run_common_tests() def lowercase ( self : Union[str, Any] ): self.model_tester.set_seed() _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*_lowerCamelCase ) def lowercase ( self : str ): self.model_tester.set_seed() _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*_lowerCamelCase ) def lowercase ( self : str ): _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*_lowerCamelCase ) def lowercase ( self : str ): _snake_case , _snake_case = self.model_tester.prepare_config_and_inputs_for_common() _snake_case = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: _snake_case = model_class(_lowerCamelCase ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: _snake_case = model.get_output_embeddings() assert isinstance(_lowerCamelCase , tf.keras.layers.Layer ) _snake_case = model.get_bias() assert name is None else: _snake_case = model.get_output_embeddings() assert x is None _snake_case = model.get_bias() assert name is None def lowercase ( self : Optional[Any] ): # TODO JP: Make TransfoXL XLA compliant pass @slow def lowercase ( self : int ): for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _snake_case = TFTransfoXLModel.from_pretrained(_lowerCamelCase ) self.assertIsNotNone(_lowerCamelCase ) @unittest.skip(reason='''This model doesn\'t play well with fit() due to not returning a single loss.''' ) def lowercase ( self : int ): pass @require_tf class lowerCAmelCase__ ( unittest.TestCase ): @unittest.skip('''Skip test until #12651 is resolved.''' ) @slow def lowercase ( self : List[Any] ): _snake_case = TFTransfoXLLMHeadModel.from_pretrained('''transfo-xl-wt103''' ) # fmt: off _snake_case = tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off _snake_case = [33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0,33,1,1857,2,1,1009,4,1109,11739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> _snake_case = model.generate(_lowerCamelCase , max_length=200 , do_sample=_lowerCamelCase ) self.assertListEqual(output_ids[0].numpy().tolist() , _lowerCamelCase )
370
"""simple docstring""" from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class lowerCAmelCase__ : __a = 42 __a = 42 class lowerCAmelCase__ : def __init__( self : int , _lowerCamelCase : int ): _snake_case = [[] for _ in range(_lowerCamelCase )] _snake_case = size def __getitem__( self : Optional[int] , _lowerCamelCase : int ): return iter(self._graph[vertex] ) @property def lowercase ( self : Optional[int] ): return self._size def lowercase ( self : List[Any] , _lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : int ): if weight not in (0, 1): raise ValueError('''Edge weight must be either 0 or 1.''' ) if to_vertex < 0 or to_vertex >= self.size: raise ValueError('''Vertex indexes must be in [0; size).''' ) self._graph[from_vertex].append(Edge(_lowerCamelCase , _lowerCamelCase ) ) def lowercase ( self : List[Any] , _lowerCamelCase : int , _lowerCamelCase : int ): _snake_case = deque([start_vertex] ) _snake_case = [None] * self.size _snake_case = 0 while queue: _snake_case = queue.popleft() _snake_case = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: _snake_case = current_distance + edge.weight _snake_case = distances[edge.destination_vertex] if ( isinstance(_lowerCamelCase , _lowerCamelCase ) and new_distance >= dest_vertex_distance ): continue _snake_case = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex ) else: queue.append(edge.destination_vertex ) if distances[finish_vertex] is None: raise ValueError('''No path from start_vertex to finish_vertex.''' ) return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
40
0
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, 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, is_valid_image, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __UpperCAmelCase = logging.get_logger(__name__) def UpperCamelCase ( snake_case__ : str ) -> List[List[ImageInput]]: if isinstance(snake_case__ , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(snake_case__ , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(snake_case__ ): return [[videos]] raise ValueError(F"""Could not make batched video from {videos}""" ) class lowerCAmelCase_ ( a__ ): UpperCAmelCase__ : List[Any] = ["pixel_values"] def __init__( self, SCREAMING_SNAKE_CASE_ = True, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR, SCREAMING_SNAKE_CASE_ = True, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = True, SCREAMING_SNAKE_CASE_ = 1 / 255, SCREAMING_SNAKE_CASE_ = True, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, **SCREAMING_SNAKE_CASE_, ) -> None: super().__init__(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = size if size is not None else {'shortest_edge': 224} UpperCamelCase : Tuple = get_size_dict(SCREAMING_SNAKE_CASE_, default_to_square=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = crop_size if crop_size is not None else {'height': 224, 'width': 224} UpperCamelCase : Optional[int] = get_size_dict(SCREAMING_SNAKE_CASE_, param_name='crop_size' ) UpperCamelCase : Dict = do_resize UpperCamelCase : Any = size UpperCamelCase : Any = do_center_crop UpperCamelCase : Optional[Any] = crop_size UpperCamelCase : str = resample UpperCamelCase : str = do_rescale UpperCamelCase : str = rescale_factor UpperCamelCase : Tuple = do_normalize UpperCamelCase : Optional[Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCamelCase : List[str] = image_std if image_std is not None else IMAGENET_STANDARD_STD def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR, SCREAMING_SNAKE_CASE_ = None, **SCREAMING_SNAKE_CASE_, ) -> np.ndarray: UpperCamelCase : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_, default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" in size: UpperCamelCase : int = get_resize_output_image_size(SCREAMING_SNAKE_CASE_, size['shortest_edge'], default_to_square=SCREAMING_SNAKE_CASE_ ) elif "height" in size and "width" in size: UpperCamelCase : Any = (size['height'], size['width']) else: raise ValueError(F"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}""" ) return resize(SCREAMING_SNAKE_CASE_, size=SCREAMING_SNAKE_CASE_, resample=SCREAMING_SNAKE_CASE_, data_format=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = None, **SCREAMING_SNAKE_CASE_, ) -> np.ndarray: UpperCamelCase : List[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F"""Size must have 'height' and 'width' as keys. Got {size.keys()}""" ) return center_crop(SCREAMING_SNAKE_CASE_, size=(size['height'], size['width']), data_format=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = None, **SCREAMING_SNAKE_CASE_, ) -> List[str]: return rescale(SCREAMING_SNAKE_CASE_, scale=SCREAMING_SNAKE_CASE_, data_format=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = None, **SCREAMING_SNAKE_CASE_, ) -> np.ndarray: return normalize(SCREAMING_SNAKE_CASE_, mean=SCREAMING_SNAKE_CASE_, std=SCREAMING_SNAKE_CASE_, data_format=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST, ) -> np.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_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. UpperCamelCase : str = to_numpy_array(SCREAMING_SNAKE_CASE_ ) if do_resize: UpperCamelCase : str = self.resize(image=SCREAMING_SNAKE_CASE_, size=SCREAMING_SNAKE_CASE_, resample=SCREAMING_SNAKE_CASE_ ) if do_center_crop: UpperCamelCase : int = self.center_crop(SCREAMING_SNAKE_CASE_, size=SCREAMING_SNAKE_CASE_ ) if do_rescale: UpperCamelCase : Dict = self.rescale(image=SCREAMING_SNAKE_CASE_, scale=SCREAMING_SNAKE_CASE_ ) if do_normalize: UpperCamelCase : List[str] = self.normalize(image=SCREAMING_SNAKE_CASE_, mean=SCREAMING_SNAKE_CASE_, std=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = to_channel_dimension_format(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) return image def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST, **SCREAMING_SNAKE_CASE_, ) -> PIL.Image.Image: UpperCamelCase : Optional[int] = do_resize if do_resize is not None else self.do_resize UpperCamelCase : Tuple = resample if resample is not None else self.resample UpperCamelCase : Optional[int] = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCamelCase : List[Any] = do_rescale if do_rescale is not None else self.do_rescale UpperCamelCase : List[str] = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCamelCase : Tuple = do_normalize if do_normalize is not None else self.do_normalize UpperCamelCase : Tuple = image_mean if image_mean is not None else self.image_mean UpperCamelCase : str = image_std if image_std is not None else self.image_std UpperCamelCase : Dict = size if size is not None else self.size UpperCamelCase : Any = get_size_dict(SCREAMING_SNAKE_CASE_, default_to_square=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[Any] = crop_size if crop_size is not None else self.crop_size UpperCamelCase : int = get_size_dict(SCREAMING_SNAKE_CASE_, param_name='crop_size' ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) UpperCamelCase : Optional[Any] = make_batched(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = [ [ self._preprocess_image( image=SCREAMING_SNAKE_CASE_, do_resize=SCREAMING_SNAKE_CASE_, size=SCREAMING_SNAKE_CASE_, resample=SCREAMING_SNAKE_CASE_, do_center_crop=SCREAMING_SNAKE_CASE_, crop_size=SCREAMING_SNAKE_CASE_, do_rescale=SCREAMING_SNAKE_CASE_, rescale_factor=SCREAMING_SNAKE_CASE_, do_normalize=SCREAMING_SNAKE_CASE_, image_mean=SCREAMING_SNAKE_CASE_, image_std=SCREAMING_SNAKE_CASE_, data_format=SCREAMING_SNAKE_CASE_, ) for img in video ] for video in videos ] UpperCamelCase : List[Any] = {'pixel_values': videos} return BatchFeature(data=SCREAMING_SNAKE_CASE_, tensor_type=SCREAMING_SNAKE_CASE_ )
119
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __UpperCAmelCase = { '''configuration_transfo_xl''': ['''TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TransfoXLConfig'''], '''tokenization_transfo_xl''': ['''TransfoXLCorpus''', '''TransfoXLTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ '''TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST''', '''AdaptiveEmbedding''', '''TransfoXLForSequenceClassification''', '''TransfoXLLMHeadModel''', '''TransfoXLModel''', '''TransfoXLPreTrainedModel''', '''load_tf_weights_in_transfo_xl''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ '''TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFAdaptiveEmbedding''', '''TFTransfoXLForSequenceClassification''', '''TFTransfoXLLMHeadModel''', '''TFTransfoXLMainLayer''', '''TFTransfoXLModel''', '''TFTransfoXLPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_transfo_xl import ( TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, AdaptiveEmbedding, TransfoXLForSequenceClassification, TransfoXLLMHeadModel, TransfoXLModel, TransfoXLPreTrainedModel, load_tf_weights_in_transfo_xl, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_transfo_xl import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFAdaptiveEmbedding, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLMainLayer, TFTransfoXLModel, TFTransfoXLPreTrainedModel, ) else: import sys __UpperCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
119
1
"""simple docstring""" import copy import re class _UpperCAmelCase : """simple docstring""" a_ = '''hp''' a_ = {} a_ = None @classmethod def lowercase ( cls : str , lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : Dict ) -> Optional[Any]: __lowerCAmelCase = prefix __lowerCAmelCase = defaults cls.build_naming_info() @staticmethod def lowercase ( lowerCAmelCase_ : List[str] , lowerCAmelCase_ : int ) -> Optional[Any]: if len(_a ) == 0: return "" __lowerCAmelCase = None if any(char.isdigit() for char in word ): raise Exception(f"""Parameters should not contain numbers: '{word}' contains a number""" ) if word in info["short_word"]: return info["short_word"][word] for prefix_len in range(1 , len(_a ) + 1 ): __lowerCAmelCase = word[:prefix_len] if prefix in info["reverse_short_word"]: continue else: __lowerCAmelCase = prefix break if short_word is None: # Paranoid fallback def int_to_alphabetic(lowerCAmelCase_ : Optional[Any] ): __lowerCAmelCase = '' while integer != 0: __lowerCAmelCase = chr(ord('A' ) + integer % 1_0 ) + s integer //= 1_0 return s __lowerCAmelCase = 0 while True: __lowerCAmelCase = word + '#' + int_to_alphabetic(_a ) if sword in info["reverse_short_word"]: continue else: __lowerCAmelCase = sword break __lowerCAmelCase = short_word __lowerCAmelCase = word return short_word @staticmethod def lowercase ( lowerCAmelCase_ : int , lowerCAmelCase_ : Dict ) -> Union[str, Any]: __lowerCAmelCase = param_name.split('_' ) __lowerCAmelCase = [TrialShortNamer.shortname_for_word(_a , _a ) for word in words] # We try to create a separatorless short name, but if there is a collision we have to fallback # to a separated short name __lowerCAmelCase = ['', '_'] for separator in separators: __lowerCAmelCase = separator.join(_a ) if shortname not in info["reverse_short_param"]: __lowerCAmelCase = shortname __lowerCAmelCase = param_name return shortname return param_name @staticmethod def lowercase ( lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Any ) -> Optional[int]: __lowerCAmelCase = TrialShortNamer.shortname_for_key(_a , _a ) __lowerCAmelCase = short_name __lowerCAmelCase = param_name @classmethod def lowercase ( cls : Any ) -> Dict: if cls.NAMING_INFO is not None: return __lowerCAmelCase = { 'short_word': {}, 'reverse_short_word': {}, 'short_param': {}, 'reverse_short_param': {}, } __lowerCAmelCase = list(cls.DEFAULTS.keys() ) for k in field_keys: cls.add_new_param_name(_a , _a ) __lowerCAmelCase = info @classmethod def lowercase ( cls : List[str] , lowerCAmelCase_ : Tuple ) -> int: cls.build_naming_info() assert cls.PREFIX is not None __lowerCAmelCase = [copy.copy(cls.PREFIX )] for k, v in params.items(): if k not in cls.DEFAULTS: raise Exception(f"""You should provide a default value for the param name {k} with value {v}""" ) if v == cls.DEFAULTS[k]: # The default value is not added to the name continue __lowerCAmelCase = cls.NAMING_INFO['short_param'][k] if isinstance(_a , _a ): __lowerCAmelCase = 1 if v else 0 __lowerCAmelCase = '' if isinstance(_a , (int, float) ) else '-' __lowerCAmelCase = f"""{key}{sep}{v}""" name.append(_a ) return "_".join(_a ) @classmethod def lowercase ( cls : List[str] , lowerCAmelCase_ : str ) -> List[str]: __lowerCAmelCase = repr[len(cls.PREFIX ) + 1 :] if repr == "": __lowerCAmelCase = [] else: __lowerCAmelCase = repr.split('_' ) __lowerCAmelCase = {} for value in values: if "-" in value: __lowerCAmelCase = value.split('-' ) else: __lowerCAmelCase = re.sub('[0-9.]' , '' , _a ) __lowerCAmelCase = float(re.sub('[^0-9.]' , '' , _a ) ) __lowerCAmelCase = cls.NAMING_INFO['reverse_short_param'][p_k] __lowerCAmelCase = p_v for k in cls.DEFAULTS: if k not in parameters: __lowerCAmelCase = cls.DEFAULTS[k] return parameters
355
import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" def lowercase ( self : List[str] ) -> int: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowercase ( self : Union[str, Any] ) -> Any: torch.manual_seed(0 ) __lowerCAmelCase = UNetaDModel( sample_size=(3_2, 6_4) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(1_2_8, 1_2_8) , down_block_types=('AttnDownBlock2D', 'DownBlock2D') , up_block_types=('UpBlock2D', 'AttnUpBlock2D') , ) return model @property def lowercase ( self : Optional[Any] ) -> List[Any]: torch.manual_seed(0 ) __lowerCAmelCase = UNetaDConditionModel( sample_size=(6_4, 3_2) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(1_2_8, 1_2_8) , down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D') , up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D') , cross_attention_dim=1_0 , ) return model @property def lowercase ( self : Dict ) -> Optional[Any]: torch.manual_seed(0 ) __lowerCAmelCase = AutoencoderKL( sample_size=(1_2_8, 6_4) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(1_2_8, 1_2_8) , down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D') , up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D') , ) __lowerCAmelCase = UNetaDModel( sample_size=(6_4, 3_2) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(1_2_8, 1_2_8) , down_block_types=('AttnDownBlock2D', 'DownBlock2D') , up_block_types=('UpBlock2D', 'AttnUpBlock2D') , ) return vqvae, unet @slow def lowercase ( self : Dict ) -> Optional[Any]: __lowerCAmelCase = 'cpu' # ensure determinism for the device-dependent torch.Generator __lowerCAmelCase = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) __lowerCAmelCase = DDPMScheduler() __lowerCAmelCase = AudioDiffusionPipeline(vqvae=lowerCAmelCase_ , unet=self.dummy_unet , mel=lowerCAmelCase_ , scheduler=lowerCAmelCase_ ) __lowerCAmelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) __lowerCAmelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(4_2 ) __lowerCAmelCase = pipe(generator=lowerCAmelCase_ , steps=4 ) __lowerCAmelCase = output.audios[0] __lowerCAmelCase = output.images[0] __lowerCAmelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(4_2 ) __lowerCAmelCase = pipe(generator=lowerCAmelCase_ , steps=4 , return_dict=lowerCAmelCase_ ) __lowerCAmelCase = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) __lowerCAmelCase = np.frombuffer(image.tobytes() , dtype='uint8' )[:1_0] __lowerCAmelCase = np.frombuffer(image_from_tuple.tobytes() , dtype='uint8' )[:1_0] __lowerCAmelCase = np.array([6_9, 2_5_5, 2_5_5, 2_5_5, 0, 0, 7_7, 1_8_1, 1_2, 1_2_7] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 __lowerCAmelCase = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) __lowerCAmelCase = DDIMScheduler() __lowerCAmelCase = self.dummy_vqvae_and_unet __lowerCAmelCase = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=lowerCAmelCase_ , scheduler=lowerCAmelCase_ ) __lowerCAmelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) np.random.seed(0 ) __lowerCAmelCase = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) __lowerCAmelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(4_2 ) __lowerCAmelCase = pipe(raw_audio=lowerCAmelCase_ , generator=lowerCAmelCase_ , start_step=5 , steps=1_0 ) __lowerCAmelCase = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) __lowerCAmelCase = np.frombuffer(image.tobytes() , dtype='uint8' )[:1_0] __lowerCAmelCase = np.array([1_2_0, 1_1_7, 1_1_0, 1_0_9, 1_3_8, 1_6_7, 1_3_8, 1_4_8, 1_3_2, 1_2_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 __lowerCAmelCase = self.dummy_unet_condition __lowerCAmelCase = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=lowerCAmelCase_ , mel=lowerCAmelCase_ , scheduler=lowerCAmelCase_ ) __lowerCAmelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) np.random.seed(0 ) __lowerCAmelCase = torch.rand((1, 1, 1_0) ) __lowerCAmelCase = pipe(generator=lowerCAmelCase_ , encoding=lowerCAmelCase_ ) __lowerCAmelCase = output.images[0] __lowerCAmelCase = np.frombuffer(image.tobytes() , dtype='uint8' )[:1_0] __lowerCAmelCase = np.array([1_0_7, 1_0_3, 1_2_0, 1_2_7, 1_4_2, 1_2_2, 1_1_3, 1_2_2, 9_7, 1_1_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" def lowercase ( self : List[Any] ) -> Optional[int]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase ( self : Union[str, Any] ) -> Optional[int]: __lowerCAmelCase = torch_device __lowerCAmelCase = DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256' ) __lowerCAmelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) __lowerCAmelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(4_2 ) __lowerCAmelCase = pipe(generator=lowerCAmelCase_ ) __lowerCAmelCase = output.audios[0] __lowerCAmelCase = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] __lowerCAmelCase = np.frombuffer(image.tobytes() , dtype='uint8' )[:1_0] __lowerCAmelCase = np.array([1_5_1, 1_6_7, 1_5_4, 1_4_4, 1_2_2, 1_3_4, 1_2_1, 1_0_5, 7_0, 2_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
207
0
"""simple docstring""" import unittest from transformers import CamembertTokenizer, CamembertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import is_torch_available from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase_ : Tuple = get_tests_dir("""fixtures/test_sentencepiece.model""") UpperCAmelCase_ : List[str] = get_tests_dir("""fixtures/test_sentencepiece_bpe.model""") UpperCAmelCase_ : Optional[int] = """pt""" if is_torch_available() else """tf""" @require_sentencepiece @require_tokenizers class lowerCAmelCase__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = CamembertTokenizer __UpperCamelCase = CamembertTokenizerFast __UpperCamelCase = True __UpperCamelCase = True def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_ : Optional[int] = CamembertTokenizer(lowercase_) tokenizer.save_pretrained(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Any = '''<pad>''' SCREAMING_SNAKE_CASE_ : Optional[Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowercase_) , lowercase_) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowercase_) , lowercase_) def _SCREAMING_SNAKE_CASE ( self : Dict): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] , '''<s>NOTUSED''') self.assertEqual(vocab_keys[1] , '''<pad>''') self.assertEqual(vocab_keys[-1] , '''<mask>''') self.assertEqual(len(lowercase_) , 1004) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1005) def _SCREAMING_SNAKE_CASE ( self : Dict): '''simple docstring''' SCREAMING_SNAKE_CASE_ : int = CamembertTokenizer(lowercase_) tokenizer.save_pretrained(self.tmpdirname) SCREAMING_SNAKE_CASE_ : Tuple = CamembertTokenizerFast.from_pretrained(self.tmpdirname) SCREAMING_SNAKE_CASE_ : Tuple = '''I was born in 92000, and this is falsé.''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer.encode(lowercase_) SCREAMING_SNAKE_CASE_ : Union[str, Any] = rust_tokenizer.encode(lowercase_) self.assertListEqual(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : Any = tokenizer.encode(lowercase_ , add_special_tokens=lowercase_) SCREAMING_SNAKE_CASE_ : Dict = rust_tokenizer.encode(lowercase_ , add_special_tokens=lowercase_) self.assertListEqual(lowercase_ , lowercase_) # <unk> tokens are not the same for `rust` than for `slow`. # Because spm gives back raw token instead of `unk` in EncodeAsPieces # tokens = tokenizer.tokenize(sequence) SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer.convert_ids_to_tokens(lowercase_) SCREAMING_SNAKE_CASE_ : Any = rust_tokenizer.tokenize(lowercase_) self.assertListEqual(lowercase_ , lowercase_) def _SCREAMING_SNAKE_CASE ( self : int): '''simple docstring''' if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE_ : str = self.get_tokenizer() SCREAMING_SNAKE_CASE_ : Optional[int] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_ : Optional[int] = '''I was born in 92000, and this is falsé.''' SCREAMING_SNAKE_CASE_ : Any = tokenizer.tokenize(lowercase_) SCREAMING_SNAKE_CASE_ : Tuple = rust_tokenizer.tokenize(lowercase_) self.assertListEqual(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : Any = tokenizer.encode(lowercase_ , add_special_tokens=lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = rust_tokenizer.encode(lowercase_ , add_special_tokens=lowercase_) self.assertListEqual(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : Tuple = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer.encode(lowercase_) SCREAMING_SNAKE_CASE_ : Tuple = rust_tokenizer.encode(lowercase_) self.assertListEqual(lowercase_ , lowercase_) @slow def _SCREAMING_SNAKE_CASE ( self : Any): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Any = {'''input_ids''': [[5, 54, 7196, 297, 30, 23, 776, 18, 11, 3215, 3705, 8252, 22, 3164, 1181, 2116, 29, 16, 813, 25, 791, 3314, 20, 3446, 38, 27575, 120, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 468, 17, 11, 9088, 20, 1517, 8, 22804, 18818, 10, 38, 629, 607, 607, 142, 19, 7196, 867, 56, 10326, 24, 2267, 20, 416, 5072, 15612, 233, 734, 7, 2399, 27, 16, 3015, 1649, 7, 24, 20, 4338, 2399, 27, 13, 3400, 14, 13, 6189, 8, 930, 9, 6]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # camembert is a french model. So we also use french texts. SCREAMING_SNAKE_CASE_ : Tuple = [ '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ''' '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''', '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ''' '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ''' '''telles que la traduction et la synthèse de texte.''', ] self.tokenizer_integration_test_util( expected_encoding=lowercase_ , model_name='''camembert-base''' , revision='''3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf''' , sequences=lowercase_ , )
91
"""simple docstring""" from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
91
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A: List[Any] = { "configuration_groupvit": [ "GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "GroupViTConfig", "GroupViTOnnxConfig", "GroupViTTextConfig", "GroupViTVisionConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A: Any = [ "GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "GroupViTModel", "GroupViTPreTrainedModel", "GroupViTTextModel", "GroupViTVisionModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A: int = [ "TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFGroupViTModel", "TFGroupViTPreTrainedModel", "TFGroupViTTextModel", "TFGroupViTVisionModel", ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys A: Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
76
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_convbert import ConvBertTokenizer A: int = logging.get_logger(__name__) A: Any = {"vocab_file": "vocab.txt"} A: Optional[int] = { "vocab_file": { "YituTech/conv-bert-base": "https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt", "YituTech/conv-bert-medium-small": ( "https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt" ), "YituTech/conv-bert-small": "https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt", } } A: Optional[int] = { "YituTech/conv-bert-base": 5_1_2, "YituTech/conv-bert-medium-small": 5_1_2, "YituTech/conv-bert-small": 5_1_2, } A: int = { "YituTech/conv-bert-base": {"do_lower_case": True}, "YituTech/conv-bert-medium-small": {"do_lower_case": True}, "YituTech/conv-bert-small": {"do_lower_case": True}, } class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase__ ): __lowerCAmelCase : List[Any] = VOCAB_FILES_NAMES __lowerCAmelCase : Any = PRETRAINED_VOCAB_FILES_MAP __lowerCAmelCase : List[str] = PRETRAINED_INIT_CONFIGURATION __lowerCAmelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCAmelCase : int = ConvBertTokenizer def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE="[UNK]" , _SCREAMING_SNAKE_CASE="[SEP]" , _SCREAMING_SNAKE_CASE="[PAD]" , _SCREAMING_SNAKE_CASE="[CLS]" , _SCREAMING_SNAKE_CASE="[MASK]" , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=None , **_SCREAMING_SNAKE_CASE , ) -> Union[str, Any]: '''simple docstring''' super().__init__( _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , do_lower_case=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , tokenize_chinese_chars=_SCREAMING_SNAKE_CASE , strip_accents=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) UpperCAmelCase : int = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" , _SCREAMING_SNAKE_CASE ) != do_lower_case or normalizer_state.get("""strip_accents""" , _SCREAMING_SNAKE_CASE ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" , _SCREAMING_SNAKE_CASE ) != tokenize_chinese_chars ): UpperCAmelCase : Dict = getattr(_SCREAMING_SNAKE_CASE , normalizer_state.pop("""type""" ) ) UpperCAmelCase : str = do_lower_case UpperCAmelCase : Optional[int] = strip_accents UpperCAmelCase : List[str] = tokenize_chinese_chars UpperCAmelCase : Dict = normalizer_class(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase : int = do_lower_case def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ) -> Tuple: '''simple docstring''' UpperCAmelCase : Any = [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 SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : str = [self.sep_token_id] UpperCAmelCase : 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 SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: '''simple docstring''' UpperCAmelCase : Dict = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
76
1
import unittest from diffusers.pipelines.pipeline_utils import is_safetensors_compatible class __a ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self ) -> str: '''simple docstring''' lowercase__: List[str] = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertTrue(is_safetensors_compatible(lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> Tuple: '''simple docstring''' lowercase__: str = [ 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertTrue(is_safetensors_compatible(lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> int: '''simple docstring''' lowercase__: Dict = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', # Removed: 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> str: '''simple docstring''' lowercase__: str = [ 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', ] self.assertTrue(is_safetensors_compatible(lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> Dict: '''simple docstring''' lowercase__: List[Any] = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', # Removed: 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> int: '''simple docstring''' lowercase__: int = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] lowercase__: Optional[Any] = 'fp16' self.assertTrue(is_safetensors_compatible(lowerCAmelCase__ , variant=lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> int: '''simple docstring''' lowercase__: Optional[int] = [ 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] lowercase__: Tuple = 'fp16' self.assertTrue(is_safetensors_compatible(lowerCAmelCase__ , variant=lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> Optional[int]: '''simple docstring''' # pass variant but use the non-variant filenames lowercase__: Tuple = [ 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] lowercase__: Tuple = 'fp16' self.assertTrue(is_safetensors_compatible(lowerCAmelCase__ , variant=lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> Union[str, Any]: '''simple docstring''' lowercase__: Optional[Any] = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors', ] lowercase__: Optional[Any] = 'fp16' self.assertFalse(is_safetensors_compatible(lowerCAmelCase__ , variant=lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> Optional[Any]: '''simple docstring''' lowercase__: Dict = [ 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', ] lowercase__: Optional[int] = 'fp16' self.assertTrue(is_safetensors_compatible(lowerCAmelCase__ , variant=lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> str: '''simple docstring''' # pass variant but use the non-variant filenames lowercase__: Any = [ 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', ] lowercase__: int = 'fp16' self.assertTrue(is_safetensors_compatible(lowerCAmelCase__ , variant=lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> Any: '''simple docstring''' lowercase__: Dict = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', # 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] lowercase__: Optional[int] = 'fp16' self.assertFalse(is_safetensors_compatible(lowerCAmelCase__ , variant=lowerCAmelCase__ ) )
196
from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_outputs import ( BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import logging from .configuration_regnet import RegNetConfig __lowerCAmelCase = logging.get_logger(__name__) # General docstring __lowerCAmelCase = '''RegNetConfig''' # Base docstring __lowerCAmelCase = '''facebook/regnet-y-040''' __lowerCAmelCase = [1, 10_88, 7, 7] # Image classification docstring __lowerCAmelCase = '''facebook/regnet-y-040''' __lowerCAmelCase = '''tabby, tabby cat''' __lowerCAmelCase = [ '''facebook/regnet-y-040''', # See all regnet models at https://huggingface.co/models?filter=regnet ] class __a ( nn.Module ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = 3 , lowerCAmelCase__ = 1 , lowerCAmelCase__ = 1 , lowerCAmelCase__ = "relu" , ) -> Optional[Any]: '''simple docstring''' super().__init__() lowercase__: Any = nn.Convad( lowerCAmelCase__ , lowerCAmelCase__ , kernel_size=lowerCAmelCase__ , stride=lowerCAmelCase__ , padding=kernel_size // 2 , groups=lowerCAmelCase__ , bias=lowerCAmelCase__ , ) lowercase__: str = nn.BatchNormad(lowerCAmelCase__ ) lowercase__: Union[str, Any] = ACTaFN[activation] if activation is not None else nn.Identity() def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> Dict: '''simple docstring''' lowercase__: List[str] = self.convolution(lowerCAmelCase__ ) lowercase__: Optional[Any] = self.normalization(lowerCAmelCase__ ) lowercase__: Union[str, Any] = self.activation(lowerCAmelCase__ ) return hidden_state class __a ( nn.Module ): def __init__( self , lowerCAmelCase__ ) -> Union[str, Any]: '''simple docstring''' super().__init__() lowercase__: Dict = RegNetConvLayer( config.num_channels , config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act ) lowercase__: Dict = config.num_channels def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> int: '''simple docstring''' lowercase__: Tuple = pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) lowercase__: Optional[int] = self.embedder(lowerCAmelCase__ ) return hidden_state class __a ( nn.Module ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = 2 ) -> Optional[Any]: '''simple docstring''' super().__init__() lowercase__: Optional[Any] = nn.Convad(lowerCAmelCase__ , lowerCAmelCase__ , kernel_size=1 , stride=lowerCAmelCase__ , bias=lowerCAmelCase__ ) lowercase__: Union[str, Any] = nn.BatchNormad(lowerCAmelCase__ ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> Tensor: '''simple docstring''' lowercase__: Any = self.convolution(lowerCAmelCase__ ) lowercase__: str = self.normalization(lowerCAmelCase__ ) return hidden_state class __a ( nn.Module ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> List[str]: '''simple docstring''' super().__init__() lowercase__: Any = nn.AdaptiveAvgPoolad((1, 1) ) lowercase__: str = nn.Sequential( nn.Convad(lowerCAmelCase__ , lowerCAmelCase__ , kernel_size=1 ) , nn.ReLU() , nn.Convad(lowerCAmelCase__ , lowerCAmelCase__ , kernel_size=1 ) , nn.Sigmoid() , ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> Optional[Any]: '''simple docstring''' # b c h w -> b c 1 1 lowercase__: str = self.pooler(lowerCAmelCase__ ) lowercase__: List[str] = self.attention(lowerCAmelCase__ ) lowercase__: List[Any] = hidden_state * attention return hidden_state class __a ( nn.Module ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = 1 ) -> Dict: '''simple docstring''' super().__init__() lowercase__: str = in_channels != out_channels or stride != 1 lowercase__: Optional[int] = max(1 , out_channels // config.groups_width ) lowercase__: Union[str, Any] = ( RegNetShortCut(lowerCAmelCase__ , lowerCAmelCase__ , stride=lowerCAmelCase__ ) if should_apply_shortcut else nn.Identity() ) lowercase__: Dict = nn.Sequential( RegNetConvLayer(lowerCAmelCase__ , lowerCAmelCase__ , kernel_size=1 , activation=config.hidden_act ) , RegNetConvLayer(lowerCAmelCase__ , lowerCAmelCase__ , stride=lowerCAmelCase__ , groups=lowerCAmelCase__ , activation=config.hidden_act ) , RegNetConvLayer(lowerCAmelCase__ , lowerCAmelCase__ , kernel_size=1 , activation=lowerCAmelCase__ ) , ) lowercase__: Tuple = ACTaFN[config.hidden_act] def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> int: '''simple docstring''' lowercase__: Dict = hidden_state lowercase__: Union[str, Any] = self.layer(lowerCAmelCase__ ) lowercase__: int = self.shortcut(lowerCAmelCase__ ) hidden_state += residual lowercase__: Optional[int] = self.activation(lowerCAmelCase__ ) return hidden_state class __a ( nn.Module ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = 1 ) -> Dict: '''simple docstring''' super().__init__() lowercase__: Optional[int] = in_channels != out_channels or stride != 1 lowercase__: List[str] = max(1 , out_channels // config.groups_width ) lowercase__: Any = ( RegNetShortCut(lowerCAmelCase__ , lowerCAmelCase__ , stride=lowerCAmelCase__ ) if should_apply_shortcut else nn.Identity() ) lowercase__: str = nn.Sequential( RegNetConvLayer(lowerCAmelCase__ , lowerCAmelCase__ , kernel_size=1 , activation=config.hidden_act ) , RegNetConvLayer(lowerCAmelCase__ , lowerCAmelCase__ , stride=lowerCAmelCase__ , groups=lowerCAmelCase__ , activation=config.hidden_act ) , RegNetSELayer(lowerCAmelCase__ , reduced_channels=int(round(in_channels / 4 ) ) ) , RegNetConvLayer(lowerCAmelCase__ , lowerCAmelCase__ , kernel_size=1 , activation=lowerCAmelCase__ ) , ) lowercase__: Union[str, Any] = ACTaFN[config.hidden_act] def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> List[str]: '''simple docstring''' lowercase__: Optional[Any] = hidden_state lowercase__: Optional[int] = self.layer(lowerCAmelCase__ ) lowercase__: str = self.shortcut(lowerCAmelCase__ ) hidden_state += residual lowercase__: Optional[int] = self.activation(lowerCAmelCase__ ) return hidden_state class __a ( nn.Module ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = 2 , lowerCAmelCase__ = 2 , ) -> Tuple: '''simple docstring''' super().__init__() lowercase__: Optional[int] = RegNetXLayer if config.layer_type == 'x' else RegNetYLayer lowercase__: str = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , stride=lowerCAmelCase__ , ) , *[layer(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) for _ in range(depth - 1 )] , ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> Optional[Any]: '''simple docstring''' lowercase__: str = self.layers(lowerCAmelCase__ ) return hidden_state class __a ( nn.Module ): def __init__( self , lowerCAmelCase__ ) -> Dict: '''simple docstring''' super().__init__() lowercase__: int = nn.ModuleList([] ) # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( RegNetStage( lowerCAmelCase__ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) lowercase__: int = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(lowerCAmelCase__ , config.depths[1:] ): self.stages.append(RegNetStage(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , depth=lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = False , lowerCAmelCase__ = True ) -> BaseModelOutputWithNoAttention: '''simple docstring''' lowercase__: List[str] = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: lowercase__: Optional[Any] = hidden_states + (hidden_state,) lowercase__: List[Any] = stage_module(lowerCAmelCase__ ) if output_hidden_states: lowercase__: Optional[Any] = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=lowerCAmelCase__ , hidden_states=lowerCAmelCase__ ) class __a ( __UpperCamelCase ): __lowercase : Dict = RegNetConfig __lowercase : Dict = 'regnet' __lowercase : str = 'pixel_values' __lowercase : List[str] = True def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> List[str]: '''simple docstring''' if isinstance(lowerCAmelCase__ , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode='fan_out' , nonlinearity='relu' ) elif isinstance(lowerCAmelCase__ , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__=False ) -> Optional[Any]: '''simple docstring''' if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): lowercase__: Any = value __lowerCAmelCase = r''' This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`RegNetConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. ''' __lowerCAmelCase = r''' Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConvNextImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple. ''' @add_start_docstrings( 'The bare RegNet model outputting raw features without any specific head on top.' , __UpperCamelCase , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet class __a ( __UpperCamelCase ): def __init__( self , lowerCAmelCase__ ) -> List[str]: '''simple docstring''' super().__init__(lowerCAmelCase__ ) lowercase__: Tuple = config lowercase__: List[str] = RegNetEmbeddings(lowerCAmelCase__ ) lowercase__: Optional[int] = RegNetEncoder(lowerCAmelCase__ ) lowercase__: Optional[Any] = nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = None ) -> BaseModelOutputWithPoolingAndNoAttention: '''simple docstring''' lowercase__: List[Any] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase__: Union[str, Any] = return_dict if return_dict is not None else self.config.use_return_dict lowercase__: Any = self.embedder(lowerCAmelCase__ ) lowercase__: List[Any] = self.encoder( lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) lowercase__: Optional[Any] = encoder_outputs[0] lowercase__: Optional[int] = self.pooler(lowerCAmelCase__ ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase__ , pooler_output=lowerCAmelCase__ , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( '\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , __UpperCamelCase , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet class __a ( __UpperCamelCase ): def __init__( self , lowerCAmelCase__ ) -> str: '''simple docstring''' super().__init__(lowerCAmelCase__ ) lowercase__: Dict = config.num_labels lowercase__: Dict = RegNetModel(lowerCAmelCase__ ) # classification head lowercase__: str = nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , ) -> ImageClassifierOutputWithNoAttention: '''simple docstring''' lowercase__: str = return_dict if return_dict is not None else self.config.use_return_dict lowercase__: Optional[int] = self.regnet(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) lowercase__: Dict = outputs.pooler_output if return_dict else outputs[1] lowercase__: List[str] = self.classifier(lowerCAmelCase__ ) lowercase__: Optional[Any] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: lowercase__: Dict = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): lowercase__: Optional[int] = 'single_label_classification' else: lowercase__: Tuple = 'multi_label_classification' if self.config.problem_type == "regression": lowercase__: List[Any] = MSELoss() if self.num_labels == 1: lowercase__: Optional[int] = loss_fct(logits.squeeze() , labels.squeeze() ) else: lowercase__: int = loss_fct(lowerCAmelCase__ , lowerCAmelCase__ ) elif self.config.problem_type == "single_label_classification": lowercase__: Dict = CrossEntropyLoss() lowercase__: Optional[int] = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": lowercase__: List[Any] = BCEWithLogitsLoss() lowercase__: Any = loss_fct(lowerCAmelCase__ , lowerCAmelCase__ ) if not return_dict: lowercase__: int = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=lowerCAmelCase__ , logits=lowerCAmelCase__ , hidden_states=outputs.hidden_states )
196
1
"""simple docstring""" from __future__ import annotations from typing import Any class a__ : def __init__( self , A = 6 ) -> None: '''simple docstring''' a = None a = None self.create_linked_list(A ) def lowerCAmelCase_ ( self , A ) -> None: '''simple docstring''' a = Node() a = current_node a = current_node a = current_node for _ in range(1 , A ): a = Node() a = current_node a = previous_node a = current_node a = self.front a = previous_node def lowerCAmelCase_ ( self ) -> bool: '''simple docstring''' return ( self.front == self.rear and self.front is not None and self.front.data is None ) def lowerCAmelCase_ ( self ) -> Any | None: '''simple docstring''' self.check_can_perform_operation() return self.front.data if self.front else None def lowerCAmelCase_ ( self , A ) -> None: '''simple docstring''' if self.rear is None: return self.check_is_full() if not self.is_empty(): a = self.rear.next if self.rear: a = data def lowerCAmelCase_ ( self ) -> Any: '''simple docstring''' self.check_can_perform_operation() if self.rear is None or self.front is None: return None if self.front == self.rear: a = self.front.data a = None return data a = self.front a = old_front.next a = old_front.data a = None return data def lowerCAmelCase_ ( self ) -> None: '''simple docstring''' if self.is_empty(): raise Exception("Empty Queue" ) def lowerCAmelCase_ ( self ) -> None: '''simple docstring''' if self.rear and self.rear.next == self.front: raise Exception("Full Queue" ) class a__ : def __init__( self ) -> None: '''simple docstring''' a = None a = None a = None if __name__ == "__main__": import doctest doctest.testmod()
368
from __future__ import annotations def SCREAMING_SNAKE_CASE ( __UpperCamelCase) -> list[int]: a = 2 a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__UpperCamelCase) if n > 1: factors.append(__UpperCamelCase) return factors if __name__ == "__main__": import doctest doctest.testmod()
180
0
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast from ...utils import logging if TYPE_CHECKING: from ...feature_extraction_utils import FeatureExtractionMixin from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { '''openai/whisper-base''': '''https://huggingface.co/openai/whisper-base/resolve/main/config.json''', } # fmt: off __lowerCAmelCase = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 357, 366, 438, 532, 685, 705, 796, 930, 1_058, 1_220, 1_267, 1_279, 1_303, 1_343, 1_377, 1_391, 1_635, 1_782, 1_875, 2_162, 2_361, 2_488, 3_467, 4_008, 4_211, 4_600, 4_808, 5_299, 5_855, 6_329, 7_203, 9_609, 9_959, 10_563, 10_786, 11_420, 11_709, 11_907, 13_163, 13_697, 13_700, 14_808, 15_306, 16_410, 16_791, 17_992, 19_203, 19_510, 20_724, 22_305, 22_935, 27_007, 30_109, 30_420, 33_409, 34_949, 40_283, 40_493, 40_549, 47_282, 49_146, 50_257, 50_359, 50_360, 50_361 ] __lowerCAmelCase = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 359, 503, 522, 542, 873, 893, 902, 918, 922, 931, 1_350, 1_853, 1_982, 2_460, 2_627, 3_246, 3_253, 3_268, 3_536, 3_846, 3_961, 4_183, 4_667, 6_585, 6_647, 7_273, 9_061, 9_383, 10_428, 10_929, 11_938, 12_033, 12_331, 12_562, 13_793, 14_157, 14_635, 15_265, 15_618, 16_553, 16_604, 18_362, 18_956, 20_075, 21_675, 22_520, 26_130, 26_161, 26_435, 28_279, 29_464, 31_650, 32_302, 32_470, 36_865, 42_863, 47_425, 49_870, 50_254, 50_258, 50_360, 50_361, 50_362 ] class __magic_name__ ( _UpperCamelCase ): lowerCAmelCase : Union[str, Any] = 'whisper' lowerCAmelCase : Optional[Any] = ['past_key_values'] lowerCAmelCase : str = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'} def __init__( self : Optional[Any] ,_UpperCAmelCase : Dict=51865 ,_UpperCAmelCase : List[str]=80 ,_UpperCAmelCase : List[str]=6 ,_UpperCAmelCase : str=4 ,_UpperCAmelCase : Optional[int]=6 ,_UpperCAmelCase : Optional[int]=4 ,_UpperCAmelCase : str=1536 ,_UpperCAmelCase : Optional[int]=1536 ,_UpperCAmelCase : str=0.0 ,_UpperCAmelCase : str=0.0 ,_UpperCAmelCase : Optional[Any]=50257 ,_UpperCAmelCase : Union[str, Any]=True ,_UpperCAmelCase : Dict=True ,_UpperCAmelCase : Union[str, Any]="gelu" ,_UpperCAmelCase : Dict=256 ,_UpperCAmelCase : List[str]=0.0 ,_UpperCAmelCase : Union[str, Any]=0.0 ,_UpperCAmelCase : List[Any]=0.0 ,_UpperCAmelCase : Union[str, Any]=0.02 ,_UpperCAmelCase : Any=False ,_UpperCAmelCase : Any=1500 ,_UpperCAmelCase : Dict=448 ,_UpperCAmelCase : List[Any]=50256 ,_UpperCAmelCase : List[str]=50256 ,_UpperCAmelCase : Optional[int]=50256 ,_UpperCAmelCase : Optional[Any]=None ,_UpperCAmelCase : int=[220, 50256] ,_UpperCAmelCase : List[Any]=False ,_UpperCAmelCase : int=256 ,_UpperCAmelCase : Optional[Any]=False ,_UpperCAmelCase : List[str]=0.05 ,_UpperCAmelCase : Any=10 ,_UpperCAmelCase : Union[str, Any]=2 ,_UpperCAmelCase : Optional[int]=0.0 ,_UpperCAmelCase : Optional[int]=10 ,_UpperCAmelCase : Optional[Any]=0 ,_UpperCAmelCase : Any=7 ,**_UpperCAmelCase : Optional[int] ,): _a : Tuple = vocab_size _a : List[str] = num_mel_bins _a : Any = d_model _a : Dict = encoder_layers _a : Tuple = encoder_attention_heads _a : Optional[Any] = decoder_layers _a : int = decoder_attention_heads _a : Optional[Any] = decoder_ffn_dim _a : Tuple = encoder_ffn_dim _a : Optional[int] = dropout _a : Tuple = attention_dropout _a : Union[str, Any] = activation_dropout _a : List[str] = activation_function _a : Optional[Any] = init_std _a : str = encoder_layerdrop _a : int = decoder_layerdrop _a : Dict = use_cache _a : List[str] = encoder_layers _a : Optional[Any] = scale_embedding # scale factor will be sqrt(d_model) if True _a : int = max_source_positions _a : Union[str, Any] = max_target_positions # Audio Classification-specific parameters. Feel free to ignore for other classes. _a : str = classifier_proj_size _a : str = use_weighted_layer_sum # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _a : Optional[int] = apply_spec_augment _a : int = mask_time_prob _a : str = mask_time_length _a : Optional[int] = mask_time_min_masks _a : str = mask_feature_prob _a : int = mask_feature_length _a : Dict = mask_feature_min_masks _a : Tuple = median_filter_width super().__init__( pad_token_id=_UpperCAmelCase ,bos_token_id=_UpperCAmelCase ,eos_token_id=_UpperCAmelCase ,is_encoder_decoder=_UpperCAmelCase ,decoder_start_token_id=_UpperCAmelCase ,suppress_tokens=_UpperCAmelCase ,begin_suppress_tokens=_UpperCAmelCase ,**_UpperCAmelCase ,) class __magic_name__ ( _UpperCamelCase ): @property def __lowercase ( self : Optional[int] ): _a : Tuple = OrderedDict( [ ('input_features', {0: 'batch', 1: 'feature_size', 2: 'encoder_sequence'}), ] ) if self.use_past: _a : Dict = {0: 'batch'} else: _a : int = {0: 'batch', 1: 'decoder_sequence'} if self.use_past: self.fill_with_past_key_values_(_UpperCAmelCase ,direction='inputs' ) return common_inputs def __lowercase ( self : List[str] ,_UpperCAmelCase : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] ,_UpperCAmelCase : int = -1 ,_UpperCAmelCase : int = -1 ,_UpperCAmelCase : bool = False ,_UpperCAmelCase : Optional["TensorType"] = None ,_UpperCAmelCase : int = 22050 ,_UpperCAmelCase : float = 5.0 ,_UpperCAmelCase : int = 220 ,): _a : List[Any] = OrderedDict() _a : int = OnnxConfig.generate_dummy_inputs( self ,preprocessor=preprocessor.feature_extractor ,batch_size=_UpperCAmelCase ,framework=_UpperCAmelCase ,sampling_rate=_UpperCAmelCase ,time_duration=_UpperCAmelCase ,frequency=_UpperCAmelCase ,) _a : List[Any] = encoder_inputs['input_features'].shape[2] _a : Optional[Any] = encoder_sequence_length // 2 if self.use_past else seq_length _a : Optional[int] = super().generate_dummy_inputs( preprocessor.tokenizer ,_UpperCAmelCase ,_UpperCAmelCase ,_UpperCAmelCase ,_UpperCAmelCase ) _a : Any = encoder_inputs.pop('input_features' ) _a : List[Any] = decoder_inputs.pop('decoder_input_ids' ) if "past_key_values" in decoder_inputs: _a : Optional[int] = decoder_inputs.pop('past_key_values' ) return dummy_inputs @property def __lowercase ( self : Dict ): return 1E-3
89
'''simple docstring''' from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_outputs import ( BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import logging from .configuration_regnet import RegNetConfig a__ : Tuple = logging.get_logger(__name__) # General docstring a__ : List[Any] = "RegNetConfig" # Base docstring a__ : Dict = "facebook/regnet-y-040" a__ : Optional[int] = [1, 1_0_8_8, 7, 7] # Image classification docstring a__ : Union[str, Any] = "facebook/regnet-y-040" a__ : Union[str, Any] = "tabby, tabby cat" a__ : int = [ "facebook/regnet-y-040", # See all regnet models at https://huggingface.co/models?filter=regnet ] class UpperCamelCase__ ( nn.Module): def __init__( self :Union[str, Any] , _A :int , _A :int , _A :int = 3 , _A :int = 1 , _A :int = 1 , _A :Optional[str] = "relu" , ) -> int: '''simple docstring''' super().__init__() __A = nn.Convad( _A , _A , kernel_size=_A , stride=_A , padding=kernel_size // 2 , groups=_A , bias=_A , ) __A = nn.BatchNormad(_A ) __A = ACTaFN[activation] if activation is not None else nn.Identity() def lowercase_ ( self :Tuple , _A :Union[str, Any] ) -> int: '''simple docstring''' __A = self.convolution(_A ) __A = self.normalization(_A ) __A = self.activation(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Optional[int] , _A :RegNetConfig ) -> List[str]: '''simple docstring''' super().__init__() __A = RegNetConvLayer( config.num_channels , config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act ) __A = config.num_channels def lowercase_ ( self :Any , _A :Optional[int] ) -> Optional[int]: '''simple docstring''' __A = pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) __A = self.embedder(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Optional[int] , _A :int , _A :int , _A :int = 2 ) -> Any: '''simple docstring''' super().__init__() __A = nn.Convad(_A , _A , kernel_size=1 , stride=_A , bias=_A ) __A = nn.BatchNormad(_A ) def lowercase_ ( self :Optional[int] , _A :Tensor ) -> Tensor: '''simple docstring''' __A = self.convolution(_A ) __A = self.normalization(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Optional[Any] , _A :int , _A :int ) -> List[str]: '''simple docstring''' super().__init__() __A = nn.AdaptiveAvgPoolad((1, 1) ) __A = nn.Sequential( nn.Convad(_A , _A , kernel_size=1 ) , nn.ReLU() , nn.Convad(_A , _A , kernel_size=1 ) , nn.Sigmoid() , ) def lowercase_ ( self :Any , _A :str ) -> int: '''simple docstring''' __A = self.pooler(_A ) __A = self.attention(_A ) __A = hidden_state * attention return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :int , _A :RegNetConfig , _A :int , _A :int , _A :int = 1 ) -> List[Any]: '''simple docstring''' super().__init__() __A = in_channels != out_channels or stride != 1 __A = max(1 , out_channels // config.groups_width ) __A = ( RegNetShortCut(_A , _A , stride=_A ) if should_apply_shortcut else nn.Identity() ) __A = nn.Sequential( RegNetConvLayer(_A , _A , kernel_size=1 , activation=config.hidden_act ) , RegNetConvLayer(_A , _A , stride=_A , groups=_A , activation=config.hidden_act ) , RegNetConvLayer(_A , _A , kernel_size=1 , activation=_A ) , ) __A = ACTaFN[config.hidden_act] def lowercase_ ( self :Optional[Any] , _A :int ) -> int: '''simple docstring''' __A = hidden_state __A = self.layer(_A ) __A = self.shortcut(_A ) hidden_state += residual __A = self.activation(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Optional[int] , _A :RegNetConfig , _A :int , _A :int , _A :int = 1 ) -> Any: '''simple docstring''' super().__init__() __A = in_channels != out_channels or stride != 1 __A = max(1 , out_channels // config.groups_width ) __A = ( RegNetShortCut(_A , _A , stride=_A ) if should_apply_shortcut else nn.Identity() ) __A = nn.Sequential( RegNetConvLayer(_A , _A , kernel_size=1 , activation=config.hidden_act ) , RegNetConvLayer(_A , _A , stride=_A , groups=_A , activation=config.hidden_act ) , RegNetSELayer(_A , reduced_channels=int(round(in_channels / 4 ) ) ) , RegNetConvLayer(_A , _A , kernel_size=1 , activation=_A ) , ) __A = ACTaFN[config.hidden_act] def lowercase_ ( self :int , _A :int ) -> int: '''simple docstring''' __A = hidden_state __A = self.layer(_A ) __A = self.shortcut(_A ) hidden_state += residual __A = self.activation(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Tuple , _A :RegNetConfig , _A :int , _A :int , _A :int = 2 , _A :int = 2 , ) -> Any: '''simple docstring''' super().__init__() __A = RegNetXLayer if config.layer_type == 'x' else RegNetYLayer __A = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer( _A , _A , _A , stride=_A , ) , *[layer(_A , _A , _A ) for _ in range(depth - 1 )] , ) def lowercase_ ( self :List[str] , _A :Optional[int] ) -> Tuple: '''simple docstring''' __A = self.layers(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Union[str, Any] , _A :RegNetConfig ) -> List[str]: '''simple docstring''' super().__init__() __A = nn.ModuleList([] ) # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( RegNetStage( _A , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) __A = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(_A , config.depths[1:] ): self.stages.append(RegNetStage(_A , _A , _A , depth=_A ) ) def lowercase_ ( self :str , _A :Tensor , _A :bool = False , _A :bool = True ) -> BaseModelOutputWithNoAttention: '''simple docstring''' __A = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: __A = hidden_states + (hidden_state,) __A = stage_module(_A ) if output_hidden_states: __A = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=_A , hidden_states=_A ) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE): UpperCAmelCase__ : int = RegNetConfig UpperCAmelCase__ : Dict = 'regnet' UpperCAmelCase__ : int = 'pixel_values' UpperCAmelCase__ : Optional[int] = True def lowercase_ ( self :str , _A :Optional[int] ) -> Tuple: '''simple docstring''' if isinstance(_A , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode='fan_out' , nonlinearity='relu' ) elif isinstance(_A , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def lowercase_ ( self :int , _A :str , _A :Dict=False ) -> Dict: '''simple docstring''' if isinstance(_A , _A ): __A = value a__ : Optional[int] = R"\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n" a__ : int = R"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConvNextImageProcessor.__call__`] for details.\n\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( 'The bare RegNet model outputting raw features without any specific head on top.' , SCREAMING_SNAKE_CASE , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet class UpperCamelCase__ ( SCREAMING_SNAKE_CASE): def __init__( self :List[str] , _A :List[Any] ) -> List[str]: '''simple docstring''' super().__init__(_A ) __A = config __A = RegNetEmbeddings(_A ) __A = RegNetEncoder(_A ) __A = nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=_A , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def lowercase_ ( self :List[Any] , _A :Tensor , _A :Optional[bool] = None , _A :Optional[bool] = None ) -> BaseModelOutputWithPoolingAndNoAttention: '''simple docstring''' __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.embedder(_A ) __A = self.encoder( _A , output_hidden_states=_A , return_dict=_A ) __A = encoder_outputs[0] __A = self.pooler(_A ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=_A , pooler_output=_A , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( '\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , SCREAMING_SNAKE_CASE , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet class UpperCamelCase__ ( SCREAMING_SNAKE_CASE): def __init__( self :Optional[int] , _A :Optional[Any] ) -> Optional[Any]: '''simple docstring''' super().__init__(_A ) __A = config.num_labels __A = RegNetModel(_A ) # classification head __A = nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=_A , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def lowercase_ ( self :Optional[int] , _A :Optional[torch.FloatTensor] = None , _A :Optional[torch.LongTensor] = None , _A :Optional[bool] = None , _A :Optional[bool] = None , ) -> ImageClassifierOutputWithNoAttention: '''simple docstring''' __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet(_A , output_hidden_states=_A , return_dict=_A ) __A = outputs.pooler_output if return_dict else outputs[1] __A = self.classifier(_A ) __A = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: __A = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): __A = 'single_label_classification' else: __A = 'multi_label_classification' if self.config.problem_type == "regression": __A = MSELoss() if self.num_labels == 1: __A = loss_fct(logits.squeeze() , labels.squeeze() ) else: __A = loss_fct(_A , _A ) elif self.config.problem_type == "single_label_classification": __A = CrossEntropyLoss() __A = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": __A = BCEWithLogitsLoss() __A = loss_fct(_A , _A ) if not return_dict: __A = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=_A , logits=_A , hidden_states=outputs.hidden_states )
161
0
import inspect import unittest import warnings from math import ceil, floor from transformers import LevitConfig from transformers.file_utils import cached_property, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, MODEL_MAPPING, LevitForImageClassification, LevitForImageClassificationWithTeacher, LevitModel, ) from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class _UpperCamelCase ( __UpperCamelCase ): def lowercase ( self: List[str] ) -> Any: """simple docstring""" UpperCamelCase_ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "hidden_sizes" ) ) self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "num_attention_heads" ) ) class _UpperCamelCase : def __init__( self: int , _SCREAMING_SNAKE_CASE: Optional[Any] , _SCREAMING_SNAKE_CASE: int=13 , _SCREAMING_SNAKE_CASE: str=64 , _SCREAMING_SNAKE_CASE: Any=3 , _SCREAMING_SNAKE_CASE: List[Any]=3 , _SCREAMING_SNAKE_CASE: Tuple=2 , _SCREAMING_SNAKE_CASE: Union[str, Any]=1 , _SCREAMING_SNAKE_CASE: Union[str, Any]=16 , _SCREAMING_SNAKE_CASE: Dict=[128, 256, 384] , _SCREAMING_SNAKE_CASE: Optional[int]=[4, 6, 8] , _SCREAMING_SNAKE_CASE: Dict=[2, 3, 4] , _SCREAMING_SNAKE_CASE: Optional[Any]=[16, 16, 16] , _SCREAMING_SNAKE_CASE: Tuple=0 , _SCREAMING_SNAKE_CASE: str=[2, 2, 2] , _SCREAMING_SNAKE_CASE: Optional[Any]=[2, 2, 2] , _SCREAMING_SNAKE_CASE: Tuple=0.02 , _SCREAMING_SNAKE_CASE: Any=True , _SCREAMING_SNAKE_CASE: Union[str, Any]=True , _SCREAMING_SNAKE_CASE: Any=2 , ) -> Dict: """simple docstring""" UpperCamelCase_ = parent UpperCamelCase_ = batch_size UpperCamelCase_ = image_size UpperCamelCase_ = num_channels UpperCamelCase_ = kernel_size UpperCamelCase_ = stride UpperCamelCase_ = padding UpperCamelCase_ = hidden_sizes UpperCamelCase_ = num_attention_heads UpperCamelCase_ = depths UpperCamelCase_ = key_dim UpperCamelCase_ = drop_path_rate UpperCamelCase_ = patch_size UpperCamelCase_ = attention_ratio UpperCamelCase_ = mlp_ratio UpperCamelCase_ = initializer_range UpperCamelCase_ = [ ["""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], ] UpperCamelCase_ = is_training UpperCamelCase_ = use_labels UpperCamelCase_ = num_labels UpperCamelCase_ = initializer_range def lowercase ( self: Union[str, Any] ) -> List[Any]: """simple docstring""" UpperCamelCase_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase_ = None if self.use_labels: UpperCamelCase_ = ids_tensor([self.batch_size] , self.num_labels ) UpperCamelCase_ = self.get_config() return config, pixel_values, labels def lowercase ( self: Any ) -> int: """simple docstring""" return LevitConfig( image_size=self.image_size , num_channels=self.num_channels , kernel_size=self.kernel_size , stride=self.stride , padding=self.padding , patch_size=self.patch_size , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , depths=self.depths , key_dim=self.key_dim , drop_path_rate=self.drop_path_rate , mlp_ratio=self.mlp_ratio , attention_ratio=self.attention_ratio , initializer_range=self.initializer_range , down_ops=self.down_ops , ) def lowercase ( self: Union[str, Any] , _SCREAMING_SNAKE_CASE: Optional[int] , _SCREAMING_SNAKE_CASE: Tuple , _SCREAMING_SNAKE_CASE: str ) -> Optional[int]: """simple docstring""" UpperCamelCase_ = LevitModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase_ = model(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = (self.image_size, self.image_size) UpperCamelCase_ = image_size[0], image_size[1] for _ in range(4 ): UpperCamelCase_ = floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) UpperCamelCase_ = floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, ceil(height / 4 ) * ceil(width / 4 ), self.hidden_sizes[-1]) , ) def lowercase ( self: Optional[int] , _SCREAMING_SNAKE_CASE: Optional[int] , _SCREAMING_SNAKE_CASE: List[str] , _SCREAMING_SNAKE_CASE: Any ) -> str: """simple docstring""" UpperCamelCase_ = self.num_labels UpperCamelCase_ = LevitForImageClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase_ = model(_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase ( self: int ) -> List[str]: """simple docstring""" UpperCamelCase_ = self.prepare_config_and_inputs() UpperCamelCase_ = config_and_inputs UpperCamelCase_ = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class _UpperCamelCase ( __UpperCamelCase , __UpperCamelCase , unittest.TestCase ): _UpperCamelCase : List[str] = ( (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher) if is_torch_available() else () ) _UpperCamelCase : List[str] = ( { """feature-extraction""": LevitModel, """image-classification""": (LevitForImageClassification, LevitForImageClassificationWithTeacher), } if is_torch_available() else {} ) _UpperCamelCase : Dict = False _UpperCamelCase : List[Any] = False _UpperCamelCase : Optional[Any] = False _UpperCamelCase : Optional[Any] = False _UpperCamelCase : Union[str, Any] = False def lowercase ( self: Dict ) -> str: """simple docstring""" UpperCamelCase_ = LevitModelTester(self ) UpperCamelCase_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def lowercase ( self: Dict ) -> Optional[Any]: """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowercase ( self: Tuple ) -> List[Any]: """simple docstring""" return @unittest.skip(reason="Levit does not use inputs_embeds" ) def lowercase ( self: List[str] ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip(reason="Levit does not support input and output embeddings" ) def lowercase ( self: Optional[Any] ) -> int: """simple docstring""" pass @unittest.skip(reason="Levit does not output attentions" ) def lowercase ( self: Dict ) -> Optional[Any]: """simple docstring""" pass def lowercase ( self: Union[str, Any] ) -> str: """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase_ = model_class(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase_ = [*signature.parameters.keys()] UpperCamelCase_ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _SCREAMING_SNAKE_CASE ) def lowercase ( self: List[str] ) -> int: """simple docstring""" def check_hidden_states_output(_SCREAMING_SNAKE_CASE: Optional[Any] , _SCREAMING_SNAKE_CASE: Union[str, Any] , _SCREAMING_SNAKE_CASE: str ): UpperCamelCase_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): UpperCamelCase_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCamelCase_ = outputs.hidden_states UpperCamelCase_ = len(self.model_tester.depths ) + 1 self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = (self.model_tester.image_size, self.model_tester.image_size) UpperCamelCase_ = image_size[0], image_size[1] for _ in range(4 ): UpperCamelCase_ = floor( ( (height + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) UpperCamelCase_ = floor( ( (width + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [ height * width, self.model_tester.hidden_sizes[0], ] , ) UpperCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase_ = True check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCamelCase_ = True check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def lowercase ( self: List[Any] ) -> List[str]: """simple docstring""" pass def lowercase ( self: Union[str, Any] , _SCREAMING_SNAKE_CASE: Union[str, Any] , _SCREAMING_SNAKE_CASE: List[str] , _SCREAMING_SNAKE_CASE: Optional[int]=False ) -> List[str]: """simple docstring""" UpperCamelCase_ = super()._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , return_labels=_SCREAMING_SNAKE_CASE ) if return_labels: if model_class.__name__ == "LevitForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def lowercase ( self: Dict ) -> Tuple: """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def lowercase ( self: Any ) -> List[Any]: """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE ) def lowercase ( self: str ) -> List[Any]: """simple docstring""" if not self.model_tester.is_training: return UpperCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase_ = True for model_class in self.all_model_classes: # LevitForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(_SCREAMING_SNAKE_CASE ) or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue UpperCamelCase_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.train() UpperCamelCase_ = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , return_labels=_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = model(**_SCREAMING_SNAKE_CASE ).loss loss.backward() def lowercase ( self: List[str] ) -> int: """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return UpperCamelCase_ = False UpperCamelCase_ = True for model_class in self.all_model_classes: if model_class in get_values(_SCREAMING_SNAKE_CASE ) or not model_class.supports_gradient_checkpointing: continue # LevitForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "LevitForImageClassificationWithTeacher": continue UpperCamelCase_ = model_class(_SCREAMING_SNAKE_CASE ) model.gradient_checkpointing_enable() model.to(_SCREAMING_SNAKE_CASE ) model.train() UpperCamelCase_ = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , return_labels=_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = model(**_SCREAMING_SNAKE_CASE ).loss loss.backward() def lowercase ( self: Any ) -> str: """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase_ = [ {"""title""": """multi_label_classification""", """num_labels""": 2, """dtype""": torch.float}, {"""title""": """single_label_classification""", """num_labels""": 1, """dtype""": torch.long}, {"""title""": """regression""", """num_labels""": 1, """dtype""": torch.float}, ] for model_class in self.all_model_classes: if ( model_class not in [ *get_values(_SCREAMING_SNAKE_CASE ), ] or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=f'''Testing {model_class} with {problem_type["title"]}''' ): UpperCamelCase_ = problem_type["""title"""] UpperCamelCase_ = problem_type["""num_labels"""] UpperCamelCase_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.train() UpperCamelCase_ = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , return_labels=_SCREAMING_SNAKE_CASE ) if problem_type["num_labels"] > 1: UpperCamelCase_ = inputs["""labels"""].unsqueeze(1 ).repeat(1 , problem_type["num_labels"] ) UpperCamelCase_ = inputs["""labels"""].to(problem_type["dtype"] ) # This tests that we do not trigger the warning form PyTorch "Using a target size that is different # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure # they have the same size." which is a symptom something in wrong for the regression problem. # See https://github.com/huggingface/transformers/issues/11780 with warnings.catch_warnings(record=_SCREAMING_SNAKE_CASE ) as warning_list: UpperCamelCase_ = model(**_SCREAMING_SNAKE_CASE ).loss for w in warning_list: if "Using a target size that is different to the input size" in str(w.message ): raise ValueError( f'''Something is going wrong in the regression problem: intercepted {w.message}''' ) loss.backward() @slow def lowercase ( self: Union[str, Any] ) -> List[Any]: """simple docstring""" for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase_ = LevitModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def lowerCAmelCase_ ( ) -> Union[str, Any]: UpperCamelCase_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class _UpperCamelCase ( unittest.TestCase ): @cached_property def lowercase ( self: Any ) -> Tuple: """simple docstring""" return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def lowercase ( self: Optional[int] ) -> List[Any]: """simple docstring""" UpperCamelCase_ = LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = self.default_image_processor UpperCamelCase_ = prepare_img() UpperCamelCase_ = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors="pt" ).to(_SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): UpperCamelCase_ = model(**_SCREAMING_SNAKE_CASE ) # verify the logits UpperCamelCase_ = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = torch.tensor([1.04_48, -0.37_45, -1.83_17] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _SCREAMING_SNAKE_CASE , atol=1e-4 ) )
356
import io import math from typing import Dict, Optional, Union import numpy as np from huggingface_hub import hf_hub_download from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import convert_to_rgb, normalize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, get_image_size, infer_channel_dimension_format, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_vision_available, logging from ...utils.import_utils import requires_backends if is_vision_available(): import textwrap from PIL import Image, ImageDraw, ImageFont if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: _UpperCAmelCase = False _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = 'ybelkada/fonts' def lowerCAmelCase_ ( ) -> Dict: if is_torch_available() and not is_torch_greater_or_equal_than_1_11: raise ImportError( F'''You are using torch=={torch.__version__}, but torch>=1.11.0 is required to use ''' "Pix2StructImageProcessor. Please upgrade torch." ) def lowerCAmelCase_ ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) -> List[Any]: requires_backends(UpperCamelCase_ , ["torch"] ) _check_torch_version() UpperCamelCase_ = image_tensor.unsqueeze(0 ) UpperCamelCase_ = torch.nn.functional.unfold(UpperCamelCase_ , (patch_height, patch_width) , stride=(patch_height, patch_width) ) UpperCamelCase_ = patches.reshape(image_tensor.size(0 ) , image_tensor.size(1 ) , UpperCamelCase_ , UpperCamelCase_ , -1 ) UpperCamelCase_ = patches.permute(0 , 4 , 2 , 3 , 1 ).reshape( image_tensor.size(2 ) // patch_height , image_tensor.size(3 ) // patch_width , image_tensor.size(1 ) * patch_height * patch_width , ) return patches.unsqueeze(0 ) def lowerCAmelCase_ ( UpperCamelCase_ , UpperCamelCase_ = 36 , UpperCamelCase_ = "black" , UpperCamelCase_ = "white" , UpperCamelCase_ = 5 , UpperCamelCase_ = 5 , UpperCamelCase_ = 5 , UpperCamelCase_ = 5 , UpperCamelCase_ = None , UpperCamelCase_ = None , ) -> Image.Image: requires_backends(UpperCamelCase_ , "vision" ) # Add new lines so that each line is no more than 80 characters. UpperCamelCase_ = textwrap.TextWrapper(width=80 ) UpperCamelCase_ = wrapper.wrap(text=UpperCamelCase_ ) UpperCamelCase_ = "\n".join(UpperCamelCase_ ) if font_bytes is not None and font_path is None: UpperCamelCase_ = io.BytesIO(UpperCamelCase_ ) elif font_path is not None: UpperCamelCase_ = font_path else: UpperCamelCase_ = hf_hub_download(UpperCamelCase_ , "Arial.TTF" ) UpperCamelCase_ = ImageFont.truetype(UpperCamelCase_ , encoding="UTF-8" , size=UpperCamelCase_ ) # Use a temporary canvas to determine the width and height in pixels when # rendering the text. UpperCamelCase_ = ImageDraw.Draw(Image.new("RGB" , (1, 1) , UpperCamelCase_ ) ) UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = temp_draw.textbbox((0, 0) , UpperCamelCase_ , UpperCamelCase_ ) # Create the actual image with a bit of padding around the text. UpperCamelCase_ = text_width + left_padding + right_padding UpperCamelCase_ = text_height + top_padding + bottom_padding UpperCamelCase_ = Image.new("RGB" , (image_width, image_height) , UpperCamelCase_ ) UpperCamelCase_ = ImageDraw.Draw(UpperCamelCase_ ) draw.text(xy=(left_padding, top_padding) , text=UpperCamelCase_ , fill=UpperCamelCase_ , font=UpperCamelCase_ ) return image def lowerCAmelCase_ ( UpperCamelCase_ , UpperCamelCase_ , **UpperCamelCase_ ) -> Union[str, Any]: requires_backends(UpperCamelCase_ , "vision" ) # Convert to PIL image if necessary UpperCamelCase_ = to_pil_image(UpperCamelCase_ ) UpperCamelCase_ = render_text(UpperCamelCase_ , **UpperCamelCase_ ) UpperCamelCase_ = max(header_image.width , image.width ) UpperCamelCase_ = int(image.height * (new_width / image.width) ) UpperCamelCase_ = int(header_image.height * (new_width / header_image.width) ) UpperCamelCase_ = Image.new("RGB" , (new_width, new_height + new_header_height) , "white" ) new_image.paste(header_image.resize((new_width, new_header_height) ) , (0, 0) ) new_image.paste(image.resize((new_width, new_height) ) , (0, new_header_height) ) # Convert back to the original framework if necessary UpperCamelCase_ = to_numpy_array(UpperCamelCase_ ) if infer_channel_dimension_format(UpperCamelCase_ ) == ChannelDimension.LAST: UpperCamelCase_ = to_channel_dimension_format(UpperCamelCase_ , ChannelDimension.LAST ) return new_image class _UpperCamelCase ( lowerCAmelCase_ ): _UpperCamelCase : str = ['''flattened_patches'''] def __init__( self: List[Any] , _SCREAMING_SNAKE_CASE: bool = True , _SCREAMING_SNAKE_CASE: bool = True , _SCREAMING_SNAKE_CASE: Dict[str, int] = None , _SCREAMING_SNAKE_CASE: int = 2048 , _SCREAMING_SNAKE_CASE: bool = False , **_SCREAMING_SNAKE_CASE: Optional[Any] , ) -> None: """simple docstring""" super().__init__(**_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = patch_size if patch_size is not None else {"height": 16, "width": 16} UpperCamelCase_ = do_normalize UpperCamelCase_ = do_convert_rgb UpperCamelCase_ = max_patches UpperCamelCase_ = is_vqa def lowercase ( self: Dict , _SCREAMING_SNAKE_CASE: np.ndarray , _SCREAMING_SNAKE_CASE: int , _SCREAMING_SNAKE_CASE: dict , **_SCREAMING_SNAKE_CASE: Union[str, Any] ) -> np.ndarray: """simple docstring""" requires_backends(self.extract_flattened_patches , "torch" ) _check_torch_version() # convert to torch UpperCamelCase_ = to_channel_dimension_format(_SCREAMING_SNAKE_CASE , ChannelDimension.FIRST ) UpperCamelCase_ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ , UpperCamelCase_ = patch_size["height"], patch_size["width"] UpperCamelCase_ , UpperCamelCase_ = get_image_size(_SCREAMING_SNAKE_CASE ) # maximize scale s.t. UpperCamelCase_ = math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width) ) UpperCamelCase_ = max(min(math.floor(scale * image_height / patch_height ) , _SCREAMING_SNAKE_CASE ) , 1 ) UpperCamelCase_ = max(min(math.floor(scale * image_width / patch_width ) , _SCREAMING_SNAKE_CASE ) , 1 ) UpperCamelCase_ = max(num_feasible_rows * patch_height , 1 ) UpperCamelCase_ = max(num_feasible_cols * patch_width , 1 ) UpperCamelCase_ = torch.nn.functional.interpolate( image.unsqueeze(0 ) , size=(resized_height, resized_width) , mode="bilinear" , align_corners=_SCREAMING_SNAKE_CASE , antialias=_SCREAMING_SNAKE_CASE , ).squeeze(0 ) # [1, rows, columns, patch_height * patch_width * image_channels] UpperCamelCase_ = torch_extract_patches(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = patches.shape UpperCamelCase_ = patches_shape[1] UpperCamelCase_ = patches_shape[2] UpperCamelCase_ = patches_shape[3] # [rows * columns, patch_height * patch_width * image_channels] UpperCamelCase_ = patches.reshape([rows * columns, depth] ) # [rows * columns, 1] UpperCamelCase_ = torch.arange(_SCREAMING_SNAKE_CASE ).reshape([rows, 1] ).repeat(1 , _SCREAMING_SNAKE_CASE ).reshape([rows * columns, 1] ) UpperCamelCase_ = torch.arange(_SCREAMING_SNAKE_CASE ).reshape([1, columns] ).repeat(_SCREAMING_SNAKE_CASE , 1 ).reshape([rows * columns, 1] ) # Offset by 1 so the ids do not contain zeros, which represent padding. row_ids += 1 col_ids += 1 # Prepare additional patch features. # [rows * columns, 1] UpperCamelCase_ = row_ids.to(torch.floataa ) UpperCamelCase_ = col_ids.to(torch.floataa ) # [rows * columns, 2 + patch_height * patch_width * image_channels] UpperCamelCase_ = torch.cat([row_ids, col_ids, patches] , -1 ) # [max_patches, 2 + patch_height * patch_width * image_channels] UpperCamelCase_ = torch.nn.functional.pad(_SCREAMING_SNAKE_CASE , [0, 0, 0, max_patches - (rows * columns)] ).float() UpperCamelCase_ = to_numpy_array(_SCREAMING_SNAKE_CASE ) return result def lowercase ( self: Optional[Any] , _SCREAMING_SNAKE_CASE: np.ndarray , _SCREAMING_SNAKE_CASE: Optional[Union[str, ChannelDimension]] = None , **_SCREAMING_SNAKE_CASE: List[str] ) -> np.ndarray: """simple docstring""" if image.dtype == np.uinta: UpperCamelCase_ = image.astype(np.floataa ) # take mean across the whole `image` UpperCamelCase_ = np.mean(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = np.std(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = max(_SCREAMING_SNAKE_CASE , 1.0 / math.sqrt(np.prod(image.shape ) ) ) return normalize(_SCREAMING_SNAKE_CASE , mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def lowercase ( self: Optional[int] , _SCREAMING_SNAKE_CASE: ImageInput , _SCREAMING_SNAKE_CASE: Optional[str] = None , _SCREAMING_SNAKE_CASE: bool = None , _SCREAMING_SNAKE_CASE: Optional[bool] = None , _SCREAMING_SNAKE_CASE: Optional[int] = None , _SCREAMING_SNAKE_CASE: Optional[Dict[str, int]] = None , _SCREAMING_SNAKE_CASE: Optional[Union[str, TensorType]] = None , _SCREAMING_SNAKE_CASE: ChannelDimension = ChannelDimension.FIRST , **_SCREAMING_SNAKE_CASE: List[Any] , ) -> ImageInput: """simple docstring""" UpperCamelCase_ = do_normalize if do_normalize is not None else self.do_normalize UpperCamelCase_ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCamelCase_ = patch_size if patch_size is not None else self.patch_size UpperCamelCase_ = max_patches if max_patches is not None else self.max_patches UpperCamelCase_ = self.is_vqa if kwargs.get("data_format" , _SCREAMING_SNAKE_CASE ) is not None: raise ValueError("data_format is not an accepted input as the outputs are " ) UpperCamelCase_ = make_list_of_images(_SCREAMING_SNAKE_CASE ) if not valid_images(_SCREAMING_SNAKE_CASE ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) # PIL RGBA images are converted to RGB if do_convert_rgb: UpperCamelCase_ = [convert_to_rgb(_SCREAMING_SNAKE_CASE ) for image in images] # All transformations expect numpy arrays. UpperCamelCase_ = [to_numpy_array(_SCREAMING_SNAKE_CASE ) for image in images] if is_vqa: if header_text is None: raise ValueError("A header text must be provided for VQA models." ) UpperCamelCase_ = kwargs.pop("font_bytes" , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = kwargs.pop("font_path" , _SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCamelCase_ = [header_text] * len(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = [ render_header(_SCREAMING_SNAKE_CASE , header_text[i] , font_bytes=_SCREAMING_SNAKE_CASE , font_path=_SCREAMING_SNAKE_CASE ) for i, image in enumerate(_SCREAMING_SNAKE_CASE ) ] if do_normalize: UpperCamelCase_ = [self.normalize(image=_SCREAMING_SNAKE_CASE ) for image in images] # convert to torch tensor and permute UpperCamelCase_ = [ self.extract_flattened_patches(image=_SCREAMING_SNAKE_CASE , max_patches=_SCREAMING_SNAKE_CASE , patch_size=_SCREAMING_SNAKE_CASE ) for image in images ] # create attention mask in numpy UpperCamelCase_ = [(image.sum(axis=-1 ) != 0).astype(np.floataa ) for image in images] UpperCamelCase_ = BatchFeature( data={"flattened_patches": images, "attention_mask": attention_masks} , tensor_type=_SCREAMING_SNAKE_CASE ) return encoded_outputs
328
0
"""simple docstring""" import torch def UpperCAmelCase__ ( ): '''simple docstring''' if torch.cuda.is_available(): lowerCAmelCase = torch.cuda.device_count() else: lowerCAmelCase = 0 print(F'Successfully ran on {num_gpus} GPUs' ) if __name__ == "__main__": main()
46
"""simple docstring""" import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {"vocab_file": "spiece.model"} SCREAMING_SNAKE_CASE__ = { "vocab_file": { "AI-Sweden/gpt-sw3-126m": "https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model", "AI-Sweden/gpt-sw3-350m": "https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model", "AI-Sweden/gpt-sw3-1.6b": "https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model", "AI-Sweden/gpt-sw3-6.7b": "https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model", "AI-Sweden/gpt-sw3-20b": "https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model", } } SCREAMING_SNAKE_CASE__ = { "AI-Sweden/gpt-sw3-126m": 2_048, "AI-Sweden/gpt-sw3-350m": 2_048, "AI-Sweden/gpt-sw3-1.6b": 2_048, "AI-Sweden/gpt-sw3-6.7b": 2_048, "AI-Sweden/gpt-sw3-20b": 2_048, } class lowercase ( _UpperCAmelCase ): _SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES _SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP _SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] def __init__( self , lowercase , lowercase=False , lowercase=False , lowercase=False , lowercase=None , lowercase=None , lowercase=None , lowercase=None , lowercase = None , **lowercase , ) -> None: lowerCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs lowerCAmelCase = kwargs.get("""name_or_path""" ) if name_or_path is None: logger.warning( """name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b,""" """ you are testing the model, this can safely be ignored""" ) lowerCAmelCase = """None""" # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing lowerCAmelCase = """<|endoftext|>""" if eos_token is None else eos_token lowerCAmelCase = """<unk>""" if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: lowerCAmelCase = unk_token if pad_token is None else pad_token lowerCAmelCase = eos_token if bos_token is None else bos_token else: lowerCAmelCase = """<pad>""" if pad_token is None else pad_token lowerCAmelCase = """<s>""" if bos_token is None else bos_token super().__init__( do_lower_case=lowercase , remove_space=lowercase , keep_accents=lowercase , bos_token=lowercase , eos_token=lowercase , unk_token=lowercase , pad_token=lowercase , sp_model_kwargs=self.sp_model_kwargs , **lowercase , ) lowerCAmelCase = do_lower_case lowerCAmelCase = remove_space lowerCAmelCase = keep_accents lowerCAmelCase = vocab_file lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowercase ) # Used for whitespace normalization in input texts # fmt : off lowerCAmelCase = {""" """, """ """, """ """, """ """, """ """, """ """, """ """, """ """, """ """, """ """, """""", """„"""} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing lowerCAmelCase = re.compile( f'[{"".join(map(lowercase , list(range(0 , 9 ) ) + list(range(11 , 32 ) ) + list(range(127 , 160 ) ) + [160, 173, 8_203] ) )}]' ) def __getstate__( self ) -> Optional[int]: lowerCAmelCase = self.__dict__.copy() lowerCAmelCase = None return state def __setstate__( self , lowercase ) -> str: lowerCAmelCase = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): lowerCAmelCase = {} lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def _snake_case ( self ) -> int: return len(self.sp_model ) def _snake_case ( self , lowercase ) -> str: lowerCAmelCase = self.non_printing_characters_re.sub("""""" , lowercase ) # Normalize whitespaces lowerCAmelCase = """""".join([char if char not in self.whitespaces else """ """ for char in text] ) # NFC Unicode normalization lowerCAmelCase = unicodedata.normalize("""NFC""" , lowercase ) return text def _snake_case ( self , lowercase , **lowercase ) -> List[str]: lowerCAmelCase = self.preprocess_text(lowercase ) return self.sp_model.encode(lowercase , out_type=lowercase ) def _snake_case ( self , lowercase ) -> int: return self.sp_model.PieceToId(lowercase ) def _snake_case ( self , lowercase ) -> str: return self.sp_model.IdToPiece(lowercase ) @staticmethod def _snake_case ( lowercase ) -> str: return out_string def _snake_case ( self , lowercase ) -> str: lowerCAmelCase = [] lowerCAmelCase = """""" lowerCAmelCase = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(lowercase ) + token lowerCAmelCase = True lowerCAmelCase = [] else: current_sub_tokens.append(lowercase ) lowerCAmelCase = False out_string += self.sp_model.decode(lowercase ) return out_string def _snake_case ( self ) -> Dict[str, int]: lowerCAmelCase = {self.convert_ids_to_tokens(lowercase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def _snake_case ( self , lowercase , lowercase = None ) -> Tuple[str]: if not os.path.isdir(lowercase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return lowerCAmelCase = os.path.join( lowercase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowercase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowercase ) elif not os.path.isfile(self.vocab_file ): with open(lowercase , """wb""" ) as fi: lowerCAmelCase = self.sp_model.serialized_model_proto() fi.write(lowercase ) return (out_vocab_file,) def _snake_case ( self , lowercase , lowercase = False ) -> Union[List[int], List[List[int]], "torch.Tensor"]: if isinstance(lowercase , lowercase ): lowerCAmelCase = self.preprocess_text(lowercase ) lowerCAmelCase = self.sp_model.encode(lowercase ) else: lowerCAmelCase = [self.preprocess_text(lowercase ) for t in text] lowerCAmelCase = self.sp_model.encode(lowercase ) if return_tensors is True or return_tensors == "pt": lowerCAmelCase = torch.tensor(lowercase ) return token_ids def _snake_case ( self , lowercase ) -> str: return self.sp_model.decode(lowercase ) def _snake_case ( self , lowercase ) -> List[int]: lowerCAmelCase = [f'User: {text}' if is_user else f'Bot: {text}' for is_user, text in conversation.iter_texts()] lowerCAmelCase = ( f'{self.eos_token}{self.bos_token}' + f'{self.bos_token}'.join(lowercase ) + f'{self.bos_token}Bot:' ) return self.encode(text=lowercase )
46
1
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" def update_area_of_max_square(_SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 UpperCAmelCase_ : int = update_area_of_max_square(_SCREAMING_SNAKE_CASE , col + 1 ) UpperCAmelCase_ : List[Any] = update_area_of_max_square(row + 1 , col + 1 ) UpperCAmelCase_ : Union[str, Any] = update_area_of_max_square(row + 1 , _SCREAMING_SNAKE_CASE ) if mat[row][col]: UpperCAmelCase_ : List[str] = 1 + min([right, diagonal, down] ) UpperCAmelCase_ : Dict = max(largest_square_area[0] , _SCREAMING_SNAKE_CASE ) return sub_problem_sol else: return 0 UpperCAmelCase_ : List[Any] = [0] update_area_of_max_square(0 , 0 ) return largest_square_area[0] def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" def update_area_of_max_square_using_dp_array( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] UpperCAmelCase_ : List[str] = update_area_of_max_square_using_dp_array(_SCREAMING_SNAKE_CASE , col + 1 , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = update_area_of_max_square_using_dp_array(row + 1 , col + 1 , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = update_area_of_max_square_using_dp_array(row + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if mat[row][col]: UpperCAmelCase_ : Any = 1 + min([right, diagonal, down] ) UpperCAmelCase_ : int = max(largest_square_area[0] , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = sub_problem_sol return sub_problem_sol else: return 0 UpperCAmelCase_ : Any = [0] UpperCAmelCase_ : Optional[Any] = [[-1] * cols for _ in range(_SCREAMING_SNAKE_CASE )] update_area_of_max_square_using_dp_array(0 , 0 , _SCREAMING_SNAKE_CASE ) return largest_square_area[0] def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" UpperCAmelCase_ : int = [[0] * (cols + 1) for _ in range(rows + 1 )] UpperCAmelCase_ : Union[str, Any] = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): UpperCAmelCase_ : Union[str, Any] = dp_array[row][col + 1] UpperCAmelCase_ : str = dp_array[row + 1][col + 1] UpperCAmelCase_ : Dict = dp_array[row + 1][col] if mat[row][col] == 1: UpperCAmelCase_ : Union[str, Any] = 1 + min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = max(dp_array[row][col] , _SCREAMING_SNAKE_CASE ) else: UpperCAmelCase_ : Union[str, Any] = 0 return largest_square_area def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" UpperCAmelCase_ : str = [0] * (cols + 1) UpperCAmelCase_ : List[str] = [0] * (cols + 1) UpperCAmelCase_ : int = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): UpperCAmelCase_ : Optional[Any] = current_row[col + 1] UpperCAmelCase_ : Tuple = next_row[col + 1] UpperCAmelCase_ : List[Any] = next_row[col] if mat[row][col] == 1: UpperCAmelCase_ : List[str] = 1 + min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = max(current_row[col] , _SCREAMING_SNAKE_CASE ) else: UpperCAmelCase_ : List[str] = 0 UpperCAmelCase_ : Optional[int] = current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
67
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list ) -> list: """simple docstring""" UpperCAmelCase_ : str = len(_SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: UpperCAmelCase_ , UpperCAmelCase_ : List[str] = arr[i + 1], arr[i] return arr if __name__ == "__main__": _lowerCamelCase = list(range(10, 0, -1)) print(f"""Original: {arr}. Sorted: {odd_even_transposition(arr)}""")
67
1
import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) __lowercase = logging.getLogger() def lowerCamelCase ( ): '''simple docstring''' __UpperCamelCase :List[Any] = argparse.ArgumentParser() parser.add_argument('''-f''' ) __UpperCamelCase :Any = parser.parse_args() return args.f def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :Union[str, Any] = {} __UpperCamelCase :str = os.path.join(SCREAMING_SNAKE_CASE , '''all_results.json''' ) if os.path.exists(SCREAMING_SNAKE_CASE ): with open(SCREAMING_SNAKE_CASE , '''r''' ) as f: __UpperCamelCase :Optional[Any] = json.load(SCREAMING_SNAKE_CASE ) else: raise ValueError(f"""can't find {path}""" ) return results def lowerCamelCase ( ): '''simple docstring''' __UpperCamelCase :Optional[Any] = torch.cuda.is_available() and torch_device == '''cuda''' return is_using_cuda and is_apex_available() __lowercase = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowerCamelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @classmethod def UpperCamelCase__ ( cls) -> int: # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU __UpperCamelCase :List[Any] = tempfile.mkdtemp() __UpperCamelCase :List[Any] = os.path.join(cls.tmpdir , '''default_config.yml''') write_basic_config(save_location=cls.configPath) __UpperCamelCase :int = ['''accelerate''', '''launch''', '''--config_file''', cls.configPath] @classmethod def UpperCamelCase__ ( cls) -> Union[str, Any]: shutil.rmtree(cls.tmpdir) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> Union[str, Any]: __UpperCamelCase :List[str] = self.get_auto_remove_tmp_dir() __UpperCamelCase :Any = f""" {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --seed=42 --checkpointing_steps epoch --with_tracking """.split() if is_cuda_and_apex_available(): testargs.append('''--fp16''') run_command(self._launch_args + testargs) __UpperCamelCase :Optional[Any] = get_results(__lowercase) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''epoch_0'''))) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''glue_no_trainer'''))) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> int: __UpperCamelCase :Any = self.get_auto_remove_tmp_dir() __UpperCamelCase :List[str] = f""" {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking """.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs) __UpperCamelCase :Dict = get_results(__lowercase) self.assertLess(result['''perplexity'''] , 100) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''epoch_0'''))) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''clm_no_trainer'''))) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> Any: __UpperCamelCase :List[Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase :List[Any] = f""" {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py --model_name_or_path distilroberta-base --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --output_dir {tmp_dir} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs) __UpperCamelCase :Union[str, Any] = get_results(__lowercase) self.assertLess(result['''perplexity'''] , 42) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''epoch_0'''))) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''mlm_no_trainer'''))) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> Tuple: # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu __UpperCamelCase :Union[str, Any] = 7 if get_gpu_count() > 1 else 2 __UpperCamelCase :List[Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase :List[Any] = f""" {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/conll/sample.json --validation_file tests/fixtures/tests_samples/conll/sample.json --output_dir {tmp_dir} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs) __UpperCamelCase :Optional[Any] = get_results(__lowercase) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75) self.assertLess(result['''train_loss'''] , 0.5) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''epoch_0'''))) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''ner_no_trainer'''))) @unittest.skip(reason='''Fix me @muellerzr''') @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> Union[str, Any]: __UpperCamelCase :Union[str, Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase :int = f""" {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py --model_name_or_path bert-base-uncased --version_2_with_negative --train_file tests/fixtures/tests_samples/SQUAD/sample.json --validation_file tests/fixtures/tests_samples/SQUAD/sample.json --output_dir {tmp_dir} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs) __UpperCamelCase :Any = get_results(__lowercase) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result['''eval_f1'''] , 28) self.assertGreaterEqual(result['''eval_exact'''] , 28) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''epoch_0'''))) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''qa_no_trainer'''))) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> Union[str, Any]: __UpperCamelCase :int = self.get_auto_remove_tmp_dir() __UpperCamelCase :int = f""" {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking """.split() run_command(self._launch_args + testargs) __UpperCamelCase :Dict = get_results(__lowercase) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.8) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''swag_no_trainer'''))) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> str: __UpperCamelCase :str = self.get_auto_remove_tmp_dir() __UpperCamelCase :Dict = f""" {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py --model_name_or_path t5-small --train_file tests/fixtures/tests_samples/xsum/sample.json --validation_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs) __UpperCamelCase :Tuple = get_results(__lowercase) self.assertGreaterEqual(result['''eval_rouge1'''] , 10) self.assertGreaterEqual(result['''eval_rouge2'''] , 2) self.assertGreaterEqual(result['''eval_rougeL'''] , 7) self.assertGreaterEqual(result['''eval_rougeLsum'''] , 7) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''epoch_0'''))) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''summarization_no_trainer'''))) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> str: __UpperCamelCase :Optional[int] = self.get_auto_remove_tmp_dir() __UpperCamelCase :Dict = f""" {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs) __UpperCamelCase :Optional[int] = get_results(__lowercase) self.assertGreaterEqual(result['''eval_bleu'''] , 30) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''epoch_0'''))) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''translation_no_trainer'''))) @slow def UpperCamelCase__ ( self) -> Tuple: __UpperCamelCase :Tuple = logging.StreamHandler(sys.stdout) logger.addHandler(__lowercase) __UpperCamelCase :int = self.get_auto_remove_tmp_dir() __UpperCamelCase :Any = f""" {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch """.split() run_command(self._launch_args + testargs) __UpperCamelCase :List[str] = get_results(__lowercase) self.assertGreaterEqual(result['''eval_overall_accuracy'''] , 0.10) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}) def UpperCamelCase__ ( self) -> Optional[int]: __UpperCamelCase :Union[str, Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase :Optional[Any] = f""" {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 """.split() if is_cuda_and_apex_available(): testargs.append('''--fp16''') run_command(self._launch_args + testargs) __UpperCamelCase :Optional[Any] = get_results(__lowercase) # The base model scores a 25% self.assertGreaterEqual(result['''eval_accuracy'''] , 0.6) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''step_1'''))) self.assertTrue(os.path.exists(os.path.join(__lowercase , '''image_classification_no_trainer''')))
43
"""simple docstring""" import os # Precomputes a list of the 100 first triangular numbers lowerCamelCase_ : List[str] = [int(0.5 * n * (n + 1)) for n in range(1, 1_01)] def UpperCAmelCase__ ( ): """simple docstring""" A_ : Union[str, Any] = os.path.dirname(os.path.realpath(_UpperCAmelCase ) ) A_ : Tuple = os.path.join(_UpperCAmelCase , 'words.txt' ) A_ : List[Any] = '' with open(_UpperCAmelCase ) as f: A_ : int = f.readline() A_ : Optional[Any] = [word.strip('"' ) for word in words.strip('\r\n' ).split(',' )] A_ : Dict = [ word for word in [sum(ord(_UpperCAmelCase ) - 64 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(_UpperCAmelCase ) if __name__ == "__main__": print(solution())
286
0
from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class __magic_name__ : def __init__( self , _lowercase , _lowercase=13 , _lowercase=7 , _lowercase=True , _lowercase=True , _lowercase=True , _lowercase=True , _lowercase=99 , _lowercase=32 , _lowercase=2 , _lowercase=4 , _lowercase=37 , _lowercase="gelu" , _lowercase=0.1 , _lowercase=0.1 , _lowercase=512 , _lowercase=16 , _lowercase=2 , _lowercase=0.02 , _lowercase=3 , _lowercase=4 , _lowercase=None , _lowercase=1_000 , )-> Union[str, Any]: UpperCamelCase_ = parent UpperCamelCase_ = batch_size UpperCamelCase_ = seq_length UpperCamelCase_ = is_training UpperCamelCase_ = use_input_mask UpperCamelCase_ = use_token_type_ids UpperCamelCase_ = use_labels UpperCamelCase_ = vocab_size UpperCamelCase_ = hidden_size UpperCamelCase_ = num_hidden_layers UpperCamelCase_ = num_attention_heads UpperCamelCase_ = intermediate_size UpperCamelCase_ = hidden_act UpperCamelCase_ = hidden_dropout_prob UpperCamelCase_ = attention_probs_dropout_prob UpperCamelCase_ = max_position_embeddings UpperCamelCase_ = type_vocab_size UpperCamelCase_ = type_sequence_label_size UpperCamelCase_ = initializer_range UpperCamelCase_ = num_labels UpperCamelCase_ = num_choices UpperCamelCase_ = scope UpperCamelCase_ = range_bbox def UpperCAmelCase_ ( self )-> Dict: UpperCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # convert bbox to numpy since TF does not support item assignment UpperCamelCase_ = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase_ = bbox[i, j, 3] UpperCamelCase_ = bbox[i, j, 1] UpperCamelCase_ = t if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase_ = bbox[i, j, 2] UpperCamelCase_ = bbox[i, j, 0] UpperCamelCase_ = t UpperCamelCase_ = tf.convert_to_tensor(_lowercase ) UpperCamelCase_ = None if self.use_input_mask: UpperCamelCase_ = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase_ = None if self.use_token_type_ids: UpperCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCamelCase_ = None UpperCamelCase_ = None UpperCamelCase_ = None if self.use_labels: UpperCamelCase_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase_ = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase_ = LayoutLMConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase_ ( self , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase )-> str: UpperCamelCase_ = TFLayoutLMModel(config=_lowercase ) UpperCamelCase_ = model(_lowercase , _lowercase , attention_mask=_lowercase , token_type_ids=_lowercase ) UpperCamelCase_ = model(_lowercase , _lowercase , token_type_ids=_lowercase ) UpperCamelCase_ = model(_lowercase , _lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase_ ( self , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase )-> str: UpperCamelCase_ = TFLayoutLMForMaskedLM(config=_lowercase ) UpperCamelCase_ = model(_lowercase , _lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , labels=_lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase_ ( self , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase )-> Optional[Any]: UpperCamelCase_ = self.num_labels UpperCamelCase_ = TFLayoutLMForSequenceClassification(config=_lowercase ) UpperCamelCase_ = model(_lowercase , _lowercase , attention_mask=_lowercase , token_type_ids=_lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase_ ( self , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase )-> Any: UpperCamelCase_ = self.num_labels UpperCamelCase_ = TFLayoutLMForTokenClassification(config=_lowercase ) UpperCamelCase_ = model(_lowercase , _lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , labels=_lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase_ ( self , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase )-> int: UpperCamelCase_ = TFLayoutLMForQuestionAnswering(config=_lowercase ) UpperCamelCase_ = model(_lowercase , _lowercase , attention_mask=_lowercase , token_type_ids=_lowercase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCAmelCase_ ( self )-> Dict: UpperCamelCase_ = self.prepare_config_and_inputs() ( ( UpperCamelCase_ ) , ( UpperCamelCase_ ) , ( UpperCamelCase_ ) , ( UpperCamelCase_ ) , ( UpperCamelCase_ ) , ( UpperCamelCase_ ) , ( UpperCamelCase_ ) , ( UpperCamelCase_ ) , ) = config_and_inputs UpperCamelCase_ = { "input_ids": input_ids, "bbox": bbox, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_tf class __magic_name__ ( snake_case , snake_case , unittest.TestCase ): UpperCamelCase_ :List[str] = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) UpperCamelCase_ :List[str] = ( { """feature-extraction""": TFLayoutLMModel, """fill-mask""": TFLayoutLMForMaskedLM, """text-classification""": TFLayoutLMForSequenceClassification, """token-classification""": TFLayoutLMForTokenClassification, """zero-shot""": TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase_ :str = False UpperCamelCase_ :Union[str, Any] = True UpperCamelCase_ :Tuple = 1_0 def UpperCAmelCase_ ( self )-> int: UpperCamelCase_ = TFLayoutLMModelTester(self ) UpperCamelCase_ = ConfigTester(self , config_class=_lowercase , hidden_size=37 ) def UpperCAmelCase_ ( self )-> Union[str, Any]: self.config_tester.run_common_tests() def UpperCAmelCase_ ( self )-> Any: UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowercase ) def UpperCAmelCase_ ( self )-> List[Any]: UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_lowercase ) def UpperCAmelCase_ ( self )-> Union[str, Any]: UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_lowercase ) def UpperCAmelCase_ ( self )-> List[Any]: UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_lowercase ) def UpperCAmelCase_ ( self )-> Dict: UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_lowercase ) @slow def UpperCAmelCase_ ( self )-> Optional[Any]: for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase_ = TFLayoutLMModel.from_pretrained(_lowercase ) self.assertIsNotNone(_lowercase ) @unittest.skip("Onnx compliancy broke with TF 2.10" ) def UpperCAmelCase_ ( self )-> Tuple: pass def lowerCAmelCase( )-> Optional[int]: """simple docstring""" UpperCamelCase_ = tf.convert_to_tensor([[1_0_1,1_0_1_9,1_0_1_4,1_0_1_6,1_0_3_7,1_2_8_4_9,4_7_4_7,1_0_0_4,1_4_2_4_6,2_2_7_8,5_4_3_9,4_5_2_4,5_0_0_2,2_9_3_0,2_1_9_3,2_9_3_0,4_3_4_1,3_2_0_8,1_0_0_5,1_0_5_5,2_1_7_1,2_8_4_8,1_1_3_0_0,3_5_3_1,1_0_2],[1_0_1,4_0_7_0,4_0_3_4,7_0_2_0,1_0_2_4,3_0_5_8,1_0_1_5,1_0_1_3,2_8_6_1,1_0_1_3,6_0_7_0,1_9_2_7_4,2_7_7_2,6_2_0_5,2_7_8_1_4,1_6_1_4_7,1_6_1_4_7,4_3_4_3,2_0_4_7,1_0_2_8_3,1_0_9_6_9,1_4_3_8_9,1_0_1_2,2_3_3_8,1_0_2]] ) # noqa: E231 UpperCamelCase_ = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 UpperCamelCase_ = tf.convert_to_tensor([[[0,0,0,0],[4_2_3,2_3_7,4_4_0,2_5_1],[4_2_7,2_7_2,4_4_1,2_8_7],[4_1_9,1_1_5,4_3_7,1_2_9],[9_6_1,8_8_5,9_9_2,9_1_2],[2_5_6,3_8,3_3_0,5_8],[2_5_6,3_8,3_3_0,5_8],[3_3_6,4_2,3_5_3,5_7],[3_6_0,3_9,4_0_1,5_6],[3_6_0,3_9,4_0_1,5_6],[4_1_1,3_9,4_7_1,5_9],[4_7_9,4_1,5_2_8,5_9],[5_3_3,3_9,6_3_0,6_0],[6_7,1_1_3,1_3_4,1_3_1],[1_4_1,1_1_5,2_0_9,1_3_2],[6_8,1_4_9,1_3_3,1_6_6],[1_4_1,1_4_9,1_8_7,1_6_4],[1_9_5,1_4_8,2_8_7,1_6_5],[1_9_5,1_4_8,2_8_7,1_6_5],[1_9_5,1_4_8,2_8_7,1_6_5],[2_9_5,1_4_8,3_4_9,1_6_5],[4_4_1,1_4_9,4_9_2,1_6_6],[4_9_7,1_4_9,5_4_6,1_6_4],[6_4,2_0_1,1_2_5,2_1_8],[1_0_0_0,1_0_0_0,1_0_0_0,1_0_0_0]],[[0,0,0,0],[6_6_2,1_5_0,7_5_4,1_6_6],[6_6_5,1_9_9,7_4_2,2_1_1],[5_1_9,2_1_3,5_5_4,2_2_8],[5_1_9,2_1_3,5_5_4,2_2_8],[1_3_4,4_3_3,1_8_7,4_5_4],[1_3_0,4_6_7,2_0_4,4_8_0],[1_3_0,4_6_7,2_0_4,4_8_0],[1_3_0,4_6_7,2_0_4,4_8_0],[1_3_0,4_6_7,2_0_4,4_8_0],[1_3_0,4_6_7,2_0_4,4_8_0],[3_1_4,4_6_9,3_7_6,4_8_2],[5_0_4,6_8_4,5_8_2,7_0_6],[9_4_1,8_2_5,9_7_3,9_0_0],[9_4_1,8_2_5,9_7_3,9_0_0],[9_4_1,8_2_5,9_7_3,9_0_0],[9_4_1,8_2_5,9_7_3,9_0_0],[6_1_0,7_4_9,6_5_2,7_6_5],[1_3_0,6_5_9,1_6_8,6_7_2],[1_7_6,6_5_7,2_3_7,6_7_2],[2_3_8,6_5_7,3_1_2,6_7_2],[4_4_3,6_5_3,6_2_8,6_7_2],[4_4_3,6_5_3,6_2_8,6_7_2],[7_1_6,3_0_1,8_2_5,3_1_7],[1_0_0_0,1_0_0_0,1_0_0_0,1_0_0_0]]] ) # noqa: E231 UpperCamelCase_ = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ) # noqa: E231 # these are sequence labels (i.e. at the token level) UpperCamelCase_ = tf.convert_to_tensor([[-1_0_0,1_0,1_0,1_0,9,1,-1_0_0,7,7,-1_0_0,7,7,4,2,5,2,8,8,-1_0_0,-1_0_0,5,0,3,2,-1_0_0],[-1_0_0,1_2,1_2,1_2,-1_0_0,1_2,1_0,-1_0_0,-1_0_0,-1_0_0,-1_0_0,1_0,1_2,9,-1_0_0,-1_0_0,-1_0_0,1_0,1_0,1_0,9,1_2,-1_0_0,1_0,-1_0_0]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class __magic_name__ ( unittest.TestCase ): @slow def UpperCAmelCase_ ( self )-> List[Any]: UpperCamelCase_ = TFLayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased" ) UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = prepare_layoutlm_batch_inputs() # forward pass UpperCamelCase_ = model(input_ids=_lowercase , bbox=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase ) # test the sequence output on [0, :3, :3] UpperCamelCase_ = tf.convert_to_tensor( [[0.1_785, -0.1_947, -0.0_425], [-0.3_254, -0.2_807, 0.2_553], [-0.5_391, -0.3_322, 0.3_364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , _lowercase , atol=1e-3 ) ) # test the pooled output on [1, :3] UpperCamelCase_ = tf.convert_to_tensor([-0.6_580, -0.0_214, 0.8_552] ) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , _lowercase , atol=1e-3 ) ) @slow def UpperCAmelCase_ ( self )-> Union[str, Any]: # initialize model with randomly initialized sequence classification head UpperCamelCase_ = TFLayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=2 ) UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = prepare_layoutlm_batch_inputs() # forward pass UpperCamelCase_ = model( input_ids=_lowercase , bbox=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , labels=tf.convert_to_tensor([1, 1] ) , ) # test whether we get a loss as a scalar UpperCamelCase_ = outputs.loss UpperCamelCase_ = (2,) self.assertEqual(loss.shape , _lowercase ) # test the shape of the logits UpperCamelCase_ = outputs.logits UpperCamelCase_ = (2, 2) self.assertEqual(logits.shape , _lowercase ) @slow def UpperCAmelCase_ ( self )-> List[Any]: # initialize model with randomly initialized token classification head UpperCamelCase_ = TFLayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=13 ) UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = prepare_layoutlm_batch_inputs() # forward pass UpperCamelCase_ = model( input_ids=_lowercase , bbox=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , labels=_lowercase ) # test the shape of the logits UpperCamelCase_ = outputs.logits UpperCamelCase_ = tf.convert_to_tensor((2, 25, 13) ) self.assertEqual(logits.shape , _lowercase ) @slow def UpperCAmelCase_ ( self )-> List[str]: # initialize model with randomly initialized token classification head UpperCamelCase_ = TFLayoutLMForQuestionAnswering.from_pretrained("microsoft/layoutlm-base-uncased" ) UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = prepare_layoutlm_batch_inputs() # forward pass UpperCamelCase_ = model(input_ids=_lowercase , bbox=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase ) # test the shape of the logits UpperCamelCase_ = tf.convert_to_tensor((2, 25) ) self.assertEqual(outputs.start_logits.shape , _lowercase ) self.assertEqual(outputs.end_logits.shape , _lowercase )
60
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __magic_name__ ( unittest.TestCase ): def __init__( self , _lowercase , _lowercase=7 , _lowercase=3 , _lowercase=18 , _lowercase=30 , _lowercase=400 , _lowercase=True , _lowercase=None , _lowercase=True , )-> Optional[int]: UpperCamelCase_ = size if size is not None else {"height": 18, "width": 18} UpperCamelCase_ = parent UpperCamelCase_ = batch_size UpperCamelCase_ = num_channels UpperCamelCase_ = image_size UpperCamelCase_ = min_resolution UpperCamelCase_ = max_resolution UpperCamelCase_ = do_resize UpperCamelCase_ = size UpperCamelCase_ = apply_ocr def UpperCAmelCase_ ( self )-> str: return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class __magic_name__ ( snake_case , unittest.TestCase ): UpperCamelCase_ :Union[str, Any] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def UpperCAmelCase_ ( self )-> Any: UpperCamelCase_ = LayoutLMvaImageProcessingTester(self ) @property def UpperCAmelCase_ ( self )-> Optional[Any]: return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self )-> Dict: UpperCamelCase_ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_lowercase , "do_resize" ) ) self.assertTrue(hasattr(_lowercase , "size" ) ) self.assertTrue(hasattr(_lowercase , "apply_ocr" ) ) def UpperCAmelCase_ ( self )-> List[Any]: UpperCamelCase_ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) UpperCamelCase_ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def UpperCAmelCase_ ( self )-> Any: pass def UpperCAmelCase_ ( self )-> List[str]: # Initialize image_processing UpperCamelCase_ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowercase ) for image in image_inputs: self.assertIsInstance(_lowercase , Image.Image ) # Test not batched input UpperCamelCase_ = image_processing(image_inputs[0] , return_tensors="pt" ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) self.assertIsInstance(encoding.words , _lowercase ) self.assertIsInstance(encoding.boxes , _lowercase ) # Test batched UpperCamelCase_ = image_processing(_lowercase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def UpperCAmelCase_ ( self )-> str: # Initialize image_processing UpperCamelCase_ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowercase , numpify=_lowercase ) for image in image_inputs: self.assertIsInstance(_lowercase , np.ndarray ) # Test not batched input UpperCamelCase_ = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched UpperCamelCase_ = image_processing(_lowercase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def UpperCAmelCase_ ( self )-> List[str]: # Initialize image_processing UpperCamelCase_ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowercase , torchify=_lowercase ) for image in image_inputs: self.assertIsInstance(_lowercase , torch.Tensor ) # Test not batched input UpperCamelCase_ = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched UpperCamelCase_ = image_processing(_lowercase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def UpperCAmelCase_ ( self )-> Any: # with apply_OCR = True UpperCamelCase_ = LayoutLMvaImageProcessor() from datasets import load_dataset UpperCamelCase_ = load_dataset("hf-internal-testing/fixtures_docvqa" , split="test" ) UpperCamelCase_ = Image.open(ds[0]["file"] ).convert("RGB" ) UpperCamelCase_ = image_processing(_lowercase , return_tensors="pt" ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 UpperCamelCase_ = [["11:14", "to", "11:39", "a.m", "11:39", "to", "11:44", "a.m.", "11:44", "a.m.", "to", "12:25", "p.m.", "12:25", "to", "12:58", "p.m.", "12:58", "to", "4:00", "p.m.", "2:00", "to", "5:00", "p.m.", "Coffee", "Break", "Coffee", "will", "be", "served", "for", "men", "and", "women", "in", "the", "lobby", "adjacent", "to", "exhibit", "area.", "Please", "move", "into", "exhibit", "area.", "(Exhibits", "Open)", "TRRF", "GENERAL", "SESSION", "(PART", "|)", "Presiding:", "Lee", "A.", "Waller", "TRRF", "Vice", "President", "“Introductory", "Remarks”", "Lee", "A.", "Waller,", "TRRF", "Vice", "Presi-", "dent", "Individual", "Interviews", "with", "TRRF", "Public", "Board", "Members", "and", "Sci-", "entific", "Advisory", "Council", "Mem-", "bers", "Conducted", "by", "TRRF", "Treasurer", "Philip", "G.", "Kuehn", "to", "get", "answers", "which", "the", "public", "refrigerated", "warehousing", "industry", "is", "looking", "for.", "Plus", "questions", "from", "the", "floor.", "Dr.", "Emil", "M.", "Mrak,", "University", "of", "Cal-", "ifornia,", "Chairman,", "TRRF", "Board;", "Sam", "R.", "Cecil,", "University", "of", "Georgia", "College", "of", "Agriculture;", "Dr.", "Stanley", "Charm,", "Tufts", "University", "School", "of", "Medicine;", "Dr.", "Robert", "H.", "Cotton,", "ITT", "Continental", "Baking", "Company;", "Dr.", "Owen", "Fennema,", "University", "of", "Wis-", "consin;", "Dr.", "Robert", "E.", "Hardenburg,", "USDA.", "Questions", "and", "Answers", "Exhibits", "Open", "Capt.", "Jack", "Stoney", "Room", "TRRF", "Scientific", "Advisory", "Council", "Meeting", "Ballroom", "Foyer"]] # noqa: E231 UpperCamelCase_ = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , _lowercase ) self.assertListEqual(encoding.boxes , _lowercase ) # with apply_OCR = False UpperCamelCase_ = LayoutLMvaImageProcessor(apply_ocr=_lowercase ) UpperCamelCase_ = image_processing(_lowercase , return_tensors="pt" ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
60
1
'''simple docstring''' snake_case_ : str = 9.8_06_65 def A__ ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = g ): if fluid_density <= 0: raise ValueError('Impossible fluid density' ) if volume < 0: raise ValueError('Impossible Object volume' ) if gravity <= 0: raise ValueError('Impossible Gravity' ) return fluid_density * gravity * volume if __name__ == "__main__": import doctest # run doctest doctest.testmod()
83
from ...utils import is_torch_available, is_transformers_available if is_transformers_available() and is_torch_available(): from .pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings, VQDiffusionPipeline
99
0
'''simple docstring''' import argparse _SCREAMING_SNAKE_CASE = "docs/source/_static/js/custom.js" def __lowerCamelCase ( __lowerCAmelCase : Dict ) -> int: with open(__lowerCAmelCase , encoding="""utf-8""" , newline="""\n""" ) as f: snake_case = f.readlines() snake_case = 0 # First let's put the right version while not lines[index].startswith("""const stableVersion =""" ): index += 1 snake_case = F'''const stableVersion = "v{version}"\n''' # Then update the dictionary while not lines[index].startswith("""const versionMapping = {""" ): index += 1 # We go until the end while not lines[index].startswith("""}""" ): index += 1 # We add the new version at the end lines[index - 1] += F''' "v{version}": "v{version}",\n''' with open(__lowerCAmelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(__lowerCAmelCase ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("--version", help="Release version.") _SCREAMING_SNAKE_CASE = parser.parse_args() update_custom_js(args.version)
3
'''simple docstring''' def __lowerCamelCase ( __lowerCAmelCase : int ) -> int: if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError("""multiplicative_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""multiplicative_persistence() does not accept negative values""" ) snake_case = 0 snake_case = str(__lowerCAmelCase ) while len(__lowerCAmelCase ) != 1: snake_case = [int(__lowerCAmelCase ) for i in num_string] snake_case = 1 for i in range(0 , len(__lowerCAmelCase ) ): total *= numbers[i] snake_case = str(__lowerCAmelCase ) steps += 1 return steps def __lowerCamelCase ( __lowerCAmelCase : int ) -> int: if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError("""additive_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""additive_persistence() does not accept negative values""" ) snake_case = 0 snake_case = str(__lowerCAmelCase ) while len(__lowerCAmelCase ) != 1: snake_case = [int(__lowerCAmelCase ) for i in num_string] snake_case = 0 for i in range(0 , len(__lowerCAmelCase ) ): total += numbers[i] snake_case = str(__lowerCAmelCase ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
3
1
def lowerCamelCase ( ): '''simple docstring''' return 1 def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' return 0 if x < 0 else two_pence(x - 2 ) + one_pence() def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' return 0 if x < 0 else five_pence(x - 5 ) + two_pence(A_ ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' return 0 if x < 0 else ten_pence(x - 10 ) + five_pence(A_ ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' return 0 if x < 0 else twenty_pence(x - 20 ) + ten_pence(A_ ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' return 0 if x < 0 else fifty_pence(x - 50 ) + twenty_pence(A_ ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' return 0 if x < 0 else one_pound(x - 100 ) + fifty_pence(A_ ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' return 0 if x < 0 else two_pound(x - 200 ) + one_pound(A_ ) def lowerCamelCase ( SCREAMING_SNAKE_CASE = 200 ): '''simple docstring''' return two_pound(A_ ) if __name__ == "__main__": print(solution(int(input().strip())))
43
"""simple docstring""" from __future__ import annotations from PIL import Image # Define glider example __lowercase = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] # Define blinker example __lowercase = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def lowercase ( A_ )-> list[list[int]]: '''simple docstring''' a : str = [] for i in range(len(A_ ) ): a : str = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours a : Union[str, Any] = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(A_ ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(A_ ) - 1: neighbour_count += cells[i + 1][j] if i < len(A_ ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. a : Tuple = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(A_ ) return next_generation def lowercase ( A_ , A_ )-> list[Image.Image]: '''simple docstring''' a : List[str] = [] for _ in range(A_ ): # Create output image a : str = Image.new("RGB" , (len(cells[0] ), len(A_ )) ) a : Union[str, Any] = img.load() # Save cells to image for x in range(len(A_ ) ): for y in range(len(cells[0] ) ): a : Optional[Any] = 255 - cells[y][x] * 255 a : str = (colour, colour, colour) # Save image images.append(A_ ) a : Tuple = new_generation(A_ ) return images if __name__ == "__main__": __lowercase = generate_images(GLIDER, 16) images[0].save("""out.gif""", save_all=True, append_images=images[1:])
40
0
"""simple docstring""" def a_ ( _lowercase ): def merge(_lowercase , _lowercase ) -> list: def _merge(): while left and right: yield (left if left[0] <= right[0] else right).pop(0 ) yield from left yield from right return list(_merge() ) if len(_lowercase ) <= 1: return collection _UpperCamelCase : List[str] = len(_lowercase ) // 2 return merge(merge_sort(collection[:mid] ) , merge_sort(collection[mid:] ) ) if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase_ =input("""Enter numbers separated by a comma:\n""").strip() UpperCamelCase_ =[int(item) for item in user_input.split(""",""")] print(*merge_sort(unsorted), sep=""",""")
361
"""simple docstring""" import argparse import torch from transformers import MobileBertConfig, MobileBertForPreTraining, load_tf_weights_in_mobilebert from transformers.utils import logging logging.set_verbosity_info() def a_ ( _lowercase , _lowercase , _lowercase ): # Initialise PyTorch model _UpperCamelCase : List[Any] = MobileBertConfig.from_json_file(_lowercase ) print(F"""Building PyTorch model from configuration: {config}""" ) _UpperCamelCase : List[str] = MobileBertForPreTraining(_lowercase ) # Load weights from tf checkpoint _UpperCamelCase : Union[str, Any] = load_tf_weights_in_mobilebert(_lowercase , _lowercase , _lowercase ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() , _lowercase ) if __name__ == "__main__": UpperCamelCase_ =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( """--mobilebert_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained MobileBERT 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.""" ) UpperCamelCase_ =parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.mobilebert_config_file, args.pytorch_dump_path)
128
0
import argparse import os from pathlib import Path import torch from bark.generation import _load_model as _bark_load_model from huggingface_hub import hf_hub_download from transformers import EncodecConfig, EncodecModel, set_seed from transformers.models.bark.configuration_bark import ( BarkCoarseConfig, BarkConfig, BarkFineConfig, BarkSemanticConfig, ) from transformers.models.bark.generation_configuration_bark import ( BarkCoarseGenerationConfig, BarkFineGenerationConfig, BarkGenerationConfig, BarkSemanticGenerationConfig, ) from transformers.models.bark.modeling_bark import BarkCoarseModel, BarkFineModel, BarkModel, BarkSemanticModel from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) set_seed(770) UpperCAmelCase__ = { "c_attn": "att_proj", "c_proj": "out_proj", "c_fc": "in_proj", "transformer.": "", "h.": "layers.", "ln_1": "layernorm_1", "ln_2": "layernorm_2", "ln_f": "layernorm_final", "wpe": "position_embeds_layer", "wte": "input_embeds_layer", } UpperCAmelCase__ = { "text_small": { "repo_id": "suno/bark", "file_name": "text.pt", }, "coarse_small": { "repo_id": "suno/bark", "file_name": "coarse.pt", }, "fine_small": { "repo_id": "suno/bark", "file_name": "fine.pt", }, "text": { "repo_id": "suno/bark", "file_name": "text_2.pt", }, "coarse": { "repo_id": "suno/bark", "file_name": "coarse_2.pt", }, "fine": { "repo_id": "suno/bark", "file_name": "fine_2.pt", }, } UpperCAmelCase__ = os.path.dirname(os.path.abspath(__file__)) UpperCAmelCase__ = os.path.join(os.path.expanduser("~"), ".cache") UpperCAmelCase__ = os.path.join(os.getenv("XDG_CACHE_HOME", default_cache_dir), "suno", "bark_v0") def _a ( a :List[str] , a :Union[str, Any]=False ) -> List[Any]: a = model_type if use_small: key += "_small" return os.path.join(a , REMOTE_MODEL_PATHS[key]['''file_name'''] ) def _a ( a :Any , a :Optional[Any] ) -> Tuple: os.makedirs(a , exist_ok=a ) hf_hub_download(repo_id=a , filename=a , local_dir=a ) def _a ( a :Union[str, Any] , a :Optional[Any] , a :Optional[Any]=False , a :Optional[Any]="text" ) -> List[str]: if model_type == "text": a = BarkSemanticModel a = BarkSemanticConfig a = BarkSemanticGenerationConfig elif model_type == "coarse": a = BarkCoarseModel a = BarkCoarseConfig a = BarkCoarseGenerationConfig elif model_type == "fine": a = BarkFineModel a = BarkFineConfig a = BarkFineGenerationConfig else: raise NotImplementedError() a = F"""{model_type}_small""" if use_small else model_type a = REMOTE_MODEL_PATHS[model_key] if not os.path.exists(a ): logger.info(F"""{model_type} model not found, downloading into `{CACHE_DIR}`.""" ) _download(model_info['''repo_id'''] , model_info['''file_name'''] ) a = torch.load(a , map_location=a ) # this is a hack a = checkpoint['''model_args'''] if "input_vocab_size" not in model_args: a = model_args['''vocab_size'''] a = model_args['''vocab_size'''] del model_args["vocab_size"] # convert Bark model arguments to HF Bark model arguments a = model_args.pop('''n_head''' ) a = model_args.pop('''n_embd''' ) a = model_args.pop('''n_layer''' ) a = ConfigClass(**checkpoint['''model_args'''] ) a = ModelClass(config=a ) a = GenerationConfigClass() a = model_generation_config a = checkpoint['''model'''] # fixup checkpoint a = '''_orig_mod.''' for k, v in list(state_dict.items() ): if k.startswith(a ): # replace part of the key with corresponding layer name in HF implementation a = k[len(a ) :] for old_layer_name in new_layer_name_dict: a = new_k.replace(a , new_layer_name_dict[old_layer_name] ) a = state_dict.pop(a ) a = set(state_dict.keys() ) - set(model.state_dict().keys() ) a = {k for k in extra_keys if not k.endswith('''.attn.bias''' )} a = set(model.state_dict().keys() ) - set(state_dict.keys() ) a = {k for k in missing_keys if not k.endswith('''.attn.bias''' )} if len(a ) != 0: raise ValueError(F"""extra keys found: {extra_keys}""" ) if len(a ) != 0: raise ValueError(F"""missing keys: {missing_keys}""" ) model.load_state_dict(a , strict=a ) a = model.num_parameters(exclude_embeddings=a ) a = checkpoint['''best_val_loss'''].item() logger.info(F"""model loaded: {round(n_params/1e6 , 1 )}M params, {round(a , 3 )} loss""" ) model.eval() model.to(a ) del checkpoint, state_dict return model def _a ( a :Any , a :List[str]=False , a :Dict="text" ) -> Any: if model_type not in ("text", "coarse", "fine"): raise NotImplementedError() a = '''cpu''' # do conversion on cpu a = _get_ckpt_path(a , use_small=a ) a = _load_model(a , a , model_type=a , use_small=a ) # load bark initial model a = _bark_load_model(a , '''cpu''' , model_type=a , use_small=a ) if model_type == "text": a = bark_model['''model'''] if model.num_parameters(exclude_embeddings=a ) != bark_model.get_num_params(): raise ValueError('''initial and new models don\'t have the same number of parameters''' ) # check if same output as the bark model a = 5 a = 10 if model_type in ["text", "coarse"]: a = torch.randint(256 , (batch_size, sequence_length) , dtype=torch.int ) a = bark_model(a )[0] a = model(a ) # take last logits a = output_new_model_total.logits[:, [-1], :] else: a = 3 a = 8 a = torch.randint(256 , (batch_size, sequence_length, n_codes_total) , dtype=torch.int ) a = model(a , a ) a = bark_model(a , a ) a = output_new_model_total.logits # output difference should come from the difference of self-attention implementation design if output_new_model.shape != output_old_model.shape: raise ValueError('''initial and new outputs don\'t have the same shape''' ) if (output_new_model - output_old_model).abs().max().item() > 1e-3: raise ValueError('''initial and new outputs are not equal''' ) Path(a ).mkdir(exist_ok=a ) model.save_pretrained(a ) def _a ( a :int , a :Optional[Any] , a :Dict , a :List[str] , a :Tuple , a :Optional[int] , ) -> Union[str, Any]: a = os.path.join(a , a ) a = BarkSemanticConfig.from_pretrained(os.path.join(a , '''config.json''' ) ) a = BarkCoarseConfig.from_pretrained(os.path.join(a , '''config.json''' ) ) a = BarkFineConfig.from_pretrained(os.path.join(a , '''config.json''' ) ) a = EncodecConfig.from_pretrained('''facebook/encodec_24khz''' ) a = BarkSemanticModel.from_pretrained(a ) a = BarkCoarseModel.from_pretrained(a ) a = BarkFineModel.from_pretrained(a ) a = EncodecModel.from_pretrained('''facebook/encodec_24khz''' ) a = BarkConfig.from_sub_model_configs( a , a , a , a ) a = BarkGenerationConfig.from_sub_model_configs( semantic.generation_config , coarseAcoustic.generation_config , fineAcoustic.generation_config ) a = BarkModel(a ) a = semantic a = coarseAcoustic a = fineAcoustic a = codec a = bark_generation_config Path(a ).mkdir(exist_ok=a ) bark.save_pretrained(a , repo_id=a , push_to_hub=a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument("model_type", type=str, help="text, coarse or fine.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--is_small", action="store_true", help="convert the small version instead of the large.") UpperCAmelCase__ = parser.parse_args() load_model(args.pytorch_dump_folder_path, model_type=args.model_type, use_small=args.is_small)
0
from typing import Tuple, Union from ...modeling_outputs import BackboneOutput from ...modeling_utils import PreTrainedModel from ...utils import is_timm_available, is_torch_available, requires_backends from ...utils.backbone_utils import BackboneMixin from .configuration_timm_backbone import TimmBackboneConfig if is_timm_available(): import timm if is_torch_available(): from torch import Tensor class _UpperCAmelCase ( A__ ,A__ ): """simple docstring""" lowercase__ = """pixel_values""" lowercase__ = False lowercase__ = TimmBackboneConfig def __init__( self : Tuple, lowerCamelCase : List[str], **lowerCamelCase : List[str] ): '''simple docstring''' requires_backends(self, '''timm''' ) super().__init__(lowerCamelCase ) lowercase__ = config if config.backbone is None: raise ValueError('''backbone is not set in the config. Please set it to a timm model name.''' ) if config.backbone not in timm.list_models(): raise ValueError(F"""backbone {config.backbone} is not supported by timm.""" ) if hasattr(lowerCamelCase, '''out_features''' ) and config.out_features is not None: raise ValueError('''out_features is not supported by TimmBackbone. Please use out_indices instead.''' ) lowercase__ = getattr(lowerCamelCase, '''use_pretrained_backbone''', lowerCamelCase ) if pretrained is None: raise ValueError('''use_pretrained_backbone is not set in the config. Please set it to True or False.''' ) # We just take the final layer by default. This matches the default for the transformers models. lowercase__ = config.out_indices if getattr(lowerCamelCase, '''out_indices''', lowerCamelCase ) is not None else (-1,) lowercase__ = timm.create_model( config.backbone, pretrained=lowerCamelCase, features_only=config.features_only, in_chans=config.num_channels, out_indices=lowerCamelCase, **lowerCamelCase, ) # These are used to control the output of the model when called. If output_hidden_states is True, then # return_layers is modified to include all layers. lowercase__ = self._backbone.return_layers lowercase__ = {layer['''module''']: str(lowerCamelCase ) for i, layer in enumerate(self._backbone.feature_info.info )} super()._init_backbone(lowerCamelCase ) @classmethod def lowercase__ ( cls : List[str], lowerCamelCase : List[str], *lowerCamelCase : Optional[int], **lowerCamelCase : List[Any] ): '''simple docstring''' requires_backends(cls, ['''vision''', '''timm'''] ) from ...models.timm_backbone import TimmBackboneConfig lowercase__ = kwargs.pop('''config''', TimmBackboneConfig() ) lowercase__ = kwargs.pop('''use_timm_backbone''', lowerCamelCase ) if not use_timm: raise ValueError('''use_timm_backbone must be True for timm backbones''' ) lowercase__ = kwargs.pop('''num_channels''', config.num_channels ) lowercase__ = kwargs.pop('''features_only''', config.features_only ) lowercase__ = kwargs.pop('''use_pretrained_backbone''', config.use_pretrained_backbone ) lowercase__ = kwargs.pop('''out_indices''', config.out_indices ) lowercase__ = TimmBackboneConfig( backbone=lowerCamelCase, num_channels=lowerCamelCase, features_only=lowerCamelCase, use_pretrained_backbone=lowerCamelCase, out_indices=lowerCamelCase, ) return super()._from_config(lowerCamelCase, **lowerCamelCase ) def lowercase__ ( self : List[Any], lowerCamelCase : Optional[Any] ): '''simple docstring''' pass def lowercase__ ( self : int, lowerCamelCase : int, lowerCamelCase : Optional[int]=None, lowerCamelCase : List[Any]=None, lowerCamelCase : int=None, **lowerCamelCase : Optional[int] ): '''simple docstring''' lowercase__ = return_dict if return_dict is not None else self.config.use_return_dict lowercase__ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase__ = output_attentions if output_attentions is not None else self.config.output_attentions if output_attentions: raise ValueError('''Cannot output attentions for timm backbones at the moment''' ) if output_hidden_states: # We modify the return layers to include all the stages of the backbone lowercase__ = self._all_layers lowercase__ = self._backbone(lowerCamelCase, **lowerCamelCase ) lowercase__ = self._return_layers lowercase__ = tuple(hidden_states[i] for i in self.out_indices ) else: lowercase__ = self._backbone(lowerCamelCase, **lowerCamelCase ) lowercase__ = None lowercase__ = tuple(lowerCamelCase ) lowercase__ = tuple(lowerCamelCase ) if hidden_states is not None else None if not return_dict: lowercase__ = (feature_maps,) if output_hidden_states: lowercase__ = output + (hidden_states,) return output return BackboneOutput(feature_maps=lowerCamelCase, hidden_states=lowerCamelCase, attentions=lowerCamelCase )
207
0
"""simple docstring""" from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split _a : List[Any] = datasets.load_iris() _a : Dict = np.array(data['data']) _a : int = np.array(data['target']) _a : str = data["""target_names"""] _a : Optional[Any] = train_test_split(X, y) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : List[Any] ) -> Union[str, Any]: return np.linalg.norm(np.array(_lowerCamelCase ) - np.array(_lowerCamelCase ) ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ,_lowerCamelCase : Optional[Any] ,_lowerCamelCase : Tuple ,_lowerCamelCase : Tuple ,_lowerCamelCase : List[Any]=5 ) -> Optional[Any]: _lowerCAmelCase : Optional[int] = zip(_lowerCamelCase ,_lowerCamelCase ) # List of distances of all points from the point to be classified _lowerCAmelCase : List[Any] = [] for data_point in data: _lowerCAmelCase : Optional[Any] = euclidean_distance(data_point[0] ,_lowerCamelCase ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. _lowerCAmelCase : List[Any] = [i[1] for i in sorted(_lowerCamelCase )[:k]] # Most commonly occurring class among them # is the class into which the point is classified _lowerCAmelCase : List[str] = Counter(_lowerCamelCase ).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]))
359
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import PoolFormerImageProcessor class __A ( unittest.TestCase ): def __init__( self , a__ , a__=7 , a__=3 , a__=30 , a__=400 , a__=True , a__=None , a__=0.9 , a__=None , a__=True , a__=[0.5, 0.5, 0.5] , a__=[0.5, 0.5, 0.5] , ): _lowerCAmelCase : int = size if size is not None else {"""shortest_edge""": 30} _lowerCAmelCase : Dict = crop_size if crop_size is not None else {"""height""": 30, """width""": 30} _lowerCAmelCase : List[Any] = parent _lowerCAmelCase : Optional[int] = batch_size _lowerCAmelCase : Any = num_channels _lowerCAmelCase : str = min_resolution _lowerCAmelCase : Dict = max_resolution _lowerCAmelCase : str = do_resize_and_center_crop _lowerCAmelCase : List[str] = size _lowerCAmelCase : int = crop_pct _lowerCAmelCase : int = crop_size _lowerCAmelCase : Union[str, Any] = do_normalize _lowerCAmelCase : Tuple = image_mean _lowerCAmelCase : Optional[Any] = image_std def __A ( self ): return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : List[str] = PoolFormerImageProcessor if is_vision_available() else None def __A ( self ): _lowerCAmelCase : str = PoolFormerImageProcessingTester(self ) @property def __A ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __A ( self ): _lowerCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a__ , """do_resize_and_center_crop""" ) ) self.assertTrue(hasattr(a__ , """size""" ) ) self.assertTrue(hasattr(a__ , """crop_pct""" ) ) self.assertTrue(hasattr(a__ , """do_normalize""" ) ) self.assertTrue(hasattr(a__ , """image_mean""" ) ) self.assertTrue(hasattr(a__ , """image_std""" ) ) def __A ( self ): _lowerCAmelCase : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 30} ) self.assertEqual(image_processor.crop_size , {"""height""": 30, """width""": 30} ) _lowerCAmelCase : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size , {"""height""": 84, """width""": 84} ) def __A ( self ): pass def __A ( self ): # Initialize image_processing _lowerCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _lowerCAmelCase : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=a__ ) for image in image_inputs: self.assertIsInstance(a__ , Image.Image ) # Test not batched input _lowerCAmelCase : Tuple = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _lowerCAmelCase : List[str] = image_processing(a__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def __A ( self ): # Initialize image_processing _lowerCAmelCase : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _lowerCAmelCase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=a__ , numpify=a__ ) for image in image_inputs: self.assertIsInstance(a__ , np.ndarray ) # Test not batched input _lowerCAmelCase : Dict = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _lowerCAmelCase : int = image_processing(a__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def __A ( self ): # Initialize image_processing _lowerCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _lowerCAmelCase : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=a__ , torchify=a__ ) for image in image_inputs: self.assertIsInstance(a__ , torch.Tensor ) # Test not batched input _lowerCAmelCase : str = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _lowerCAmelCase : List[str] = image_processing(a__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , )
126
0
import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def lowerCamelCase__ ( _a , _a): # Load checkpoint SCREAMING_SNAKE_CASE : int = torch.load(_a , map_location="cpu") SCREAMING_SNAKE_CASE : Dict = chkpt["model"] # We have the base model one level deeper than the original XLM repository SCREAMING_SNAKE_CASE : Optional[int] = {} for k, v in state_dict.items(): if "pred_layer" in k: SCREAMING_SNAKE_CASE : List[str] = v else: SCREAMING_SNAKE_CASE : int = v SCREAMING_SNAKE_CASE : int = chkpt["params"] SCREAMING_SNAKE_CASE : Union[str, Any] = {n: v for n, v in config.items() if not isinstance(_a , (torch.FloatTensor, numpy.ndarray))} SCREAMING_SNAKE_CASE : List[Any] = chkpt["dico_word2id"] SCREAMING_SNAKE_CASE : List[Any] = {s + "</w>" if s.find("@@") == -1 and i > 13 else s.replace("@@" , ""): i for s, i in vocab.items()} # Save pytorch-model SCREAMING_SNAKE_CASE : Tuple = pytorch_dump_folder_path + "/" + WEIGHTS_NAME SCREAMING_SNAKE_CASE : Any = pytorch_dump_folder_path + "/" + CONFIG_NAME SCREAMING_SNAKE_CASE : Optional[int] = pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["vocab_file"] print(f"Save PyTorch model to {pytorch_weights_dump_path}") torch.save(_a , _a) print(f"Save configuration file to {pytorch_config_dump_path}") with open(_a , "w" , encoding="utf-8") as f: f.write(json.dumps(_a , indent=2) + "\n") print(f"Save vocab file to {pytorch_config_dump_path}") with open(_a , "w" , encoding="utf-8") as f: f.write(json.dumps(_a , indent=2) + "\n") if __name__ == "__main__": a_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--xlm_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) a_ = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
76
from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { 'sayakpaul/vit-msn-base': 'https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class _UpperCamelCase ( __A ): '''simple docstring''' lowerCamelCase__ ='vit_msn' def __init__( self : str , a : Tuple=768 , a : Tuple=12 , a : Any=12 , a : int=3072 , a : List[Any]="gelu" , a : Dict=0.0 , a : int=0.0 , a : str=0.02 , a : List[str]=1e-06 , a : List[Any]=224 , a : Union[str, Any]=16 , a : Union[str, Any]=3 , a : Tuple=True , **a : Dict , ) -> List[Any]: """simple docstring""" super().__init__(**a ) SCREAMING_SNAKE_CASE : Dict = hidden_size SCREAMING_SNAKE_CASE : Optional[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[Any] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE : int = hidden_act SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : int = layer_norm_eps SCREAMING_SNAKE_CASE : Dict = image_size SCREAMING_SNAKE_CASE : Tuple = patch_size SCREAMING_SNAKE_CASE : Optional[int] = num_channels SCREAMING_SNAKE_CASE : List[str] = qkv_bias
76
1
'''simple docstring''' import os def __magic_name__ ( ) -> Any: '''simple docstring''' snake_case_ = os.path.dirname(os.path.realpath(__UpperCAmelCase ) ) snake_case_ = os.path.join(__UpperCAmelCase, '''triangle.txt''' ) with open(__UpperCAmelCase ) as f: snake_case_ = f.readlines() snake_case_ = [] for line in triangle: snake_case_ = [] for number in line.strip().split(''' ''' ): numbers_from_line.append(int(__UpperCAmelCase ) ) a.append(__UpperCAmelCase ) for i in range(1, len(__UpperCAmelCase ) ): for j in range(len(a[i] ) ): snake_case_ = a[i - 1][j] if j != len(a[i - 1] ) else 0 snake_case_ = a[i - 1][j - 1] if j > 0 else 0 a[i][j] += max(__UpperCAmelCase, __UpperCAmelCase ) return max(a[-1] ) if __name__ == "__main__": print(solution())
72
'''simple docstring''' import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger a : Any = get_logger(__name__) a : Union[str, Any] = Path(__file__).parent / 'model_card_template.md' a : List[Any] = uuida().hex a : List[str] = os.getenv('HF_HUB_OFFLINE', '').upper() in ENV_VARS_TRUE_VALUES a : str = os.getenv('DISABLE_TELEMETRY', '').upper() in ENV_VARS_TRUE_VALUES a : Optional[Any] = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '/api/telemetry/' def __magic_name__ ( __UpperCAmelCase = None ) -> str: '''simple docstring''' snake_case_ = F"diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F"; torch/{_torch_version}" if is_flax_available(): ua += F"; jax/{_jax_version}" ua += F"; flax/{_flax_version}" if is_onnx_available(): ua += F"; onnxruntime/{_onnxruntime_version}" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''', '''''' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(__UpperCAmelCase, __UpperCAmelCase ): ua += "; " + "; ".join(F"{k}/{v}" for k, v in user_agent.items() ) elif isinstance(__UpperCAmelCase, __UpperCAmelCase ): ua += "; " + user_agent return ua def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase = None, __UpperCAmelCase = None ) -> Optional[Any]: '''simple docstring''' if token is None: snake_case_ = HfFolder.get_token() if organization is None: snake_case_ = whoami(__UpperCAmelCase )['''name'''] return F"{username}/{model_id}" else: return F"{organization}/{model_id}" def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''' ) if hasattr(__UpperCAmelCase, '''local_rank''' ) and args.local_rank not in [-1, 0]: return snake_case_ = args.hub_token if hasattr(__UpperCAmelCase, '''hub_token''' ) else None snake_case_ = get_full_repo_name(__UpperCAmelCase, token=__UpperCAmelCase ) snake_case_ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''', license='''apache-2.0''', library_name='''diffusers''', tags=[], datasets=args.dataset_name, metrics=[], ), template_path=__UpperCAmelCase, model_name=__UpperCAmelCase, repo_name=__UpperCAmelCase, dataset_name=args.dataset_name if hasattr(__UpperCAmelCase, '''dataset_name''' ) else None, learning_rate=args.learning_rate, train_batch_size=args.train_batch_size, eval_batch_size=args.eval_batch_size, gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(__UpperCAmelCase, '''gradient_accumulation_steps''' ) else None ), adam_betaa=args.adam_betaa if hasattr(__UpperCAmelCase, '''adam_beta1''' ) else None, adam_betaa=args.adam_betaa if hasattr(__UpperCAmelCase, '''adam_beta2''' ) else None, adam_weight_decay=args.adam_weight_decay if hasattr(__UpperCAmelCase, '''adam_weight_decay''' ) else None, adam_epsilon=args.adam_epsilon if hasattr(__UpperCAmelCase, '''adam_epsilon''' ) else None, lr_scheduler=args.lr_scheduler if hasattr(__UpperCAmelCase, '''lr_scheduler''' ) else None, lr_warmup_steps=args.lr_warmup_steps if hasattr(__UpperCAmelCase, '''lr_warmup_steps''' ) else None, ema_inv_gamma=args.ema_inv_gamma if hasattr(__UpperCAmelCase, '''ema_inv_gamma''' ) else None, ema_power=args.ema_power if hasattr(__UpperCAmelCase, '''ema_power''' ) else None, ema_max_decay=args.ema_max_decay if hasattr(__UpperCAmelCase, '''ema_max_decay''' ) else None, mixed_precision=args.mixed_precision, ) snake_case_ = os.path.join(args.output_dir, '''README.md''' ) model_card.save(__UpperCAmelCase ) def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase = None ) -> Optional[Any]: '''simple docstring''' if resolved_file is None or commit_hash is not None: return commit_hash snake_case_ = str(Path(__UpperCAmelCase ).as_posix() ) snake_case_ = re.search(r'''snapshots/([^/]+)/''', __UpperCAmelCase ) if search is None: return None snake_case_ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(__UpperCAmelCase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. a : str = os.path.expanduser( os.getenv('HF_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'huggingface')) ) a : Optional[Any] = os.path.join(hf_cache_home, 'diffusers') def __magic_name__ ( __UpperCAmelCase = None, __UpperCAmelCase = None ) -> None: '''simple docstring''' if new_cache_dir is None: snake_case_ = DIFFUSERS_CACHE if old_cache_dir is None: snake_case_ = old_diffusers_cache snake_case_ = Path(__UpperCAmelCase ).expanduser() snake_case_ = Path(__UpperCAmelCase ).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*''' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): snake_case_ = new_cache_dir / old_blob_path.relative_to(__UpperCAmelCase ) new_blob_path.parent.mkdir(parents=__UpperCAmelCase, exist_ok=__UpperCAmelCase ) os.replace(__UpperCAmelCase, __UpperCAmelCase ) try: os.symlink(__UpperCAmelCase, __UpperCAmelCase ) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). a : Tuple = os.path.join(DIFFUSERS_CACHE, 'version_diffusers_cache.txt') if not os.path.isfile(cache_version_file): a : Tuple = 0 else: with open(cache_version_file) as f: try: a : Optional[Any] = int(f.read()) except ValueError: a : List[str] = 0 if cache_version < 1: a : Tuple = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( 'The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ' 'existing cached models. This is a one-time operation, you can interrupt it or run it ' 'later by calling `diffusers.utils.hub_utils.move_cache()`.' ) try: move_cache() except Exception as e: a : str = '\n'.join(traceback.format_tb(e.__traceback__)) logger.error( f'''There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ''' 'file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ' 'message and we will do our best to help.' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, 'w') as f: f.write('1') except Exception: logger.warning( f'''There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ''' 'the directory exists and can be written to.' ) def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase = None ) -> str: '''simple docstring''' if variant is not None: snake_case_ = weights_name.split('''.''' ) snake_case_ = splits[:-1] + [variant] + splits[-1:] snake_case_ = '''.'''.join(__UpperCAmelCase ) return weights_name def __magic_name__ ( __UpperCAmelCase, *, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase=None, ) -> int: '''simple docstring''' snake_case_ = str(__UpperCAmelCase ) if os.path.isfile(__UpperCAmelCase ): return pretrained_model_name_or_path elif os.path.isdir(__UpperCAmelCase ): if os.path.isfile(os.path.join(__UpperCAmelCase, __UpperCAmelCase ) ): # Load from a PyTorch checkpoint snake_case_ = os.path.join(__UpperCAmelCase, __UpperCAmelCase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(__UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase ) ): snake_case_ = os.path.join(__UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase ) return model_file else: raise EnvironmentError( F"Error no file named {weights_name} found in directory {pretrained_model_name_or_path}." ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(__UpperCAmelCase ).base_version ) >= version.parse('''0.20.0''' ) ): try: snake_case_ = hf_hub_download( __UpperCAmelCase, filename=_add_variant(__UpperCAmelCase, __UpperCAmelCase ), cache_dir=__UpperCAmelCase, force_download=__UpperCAmelCase, proxies=__UpperCAmelCase, resume_download=__UpperCAmelCase, local_files_only=__UpperCAmelCase, use_auth_token=__UpperCAmelCase, user_agent=__UpperCAmelCase, subfolder=__UpperCAmelCase, revision=revision or commit_hash, ) warnings.warn( F"Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.", __UpperCAmelCase, ) return model_file except: # noqa: E722 warnings.warn( F"You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(__UpperCAmelCase, __UpperCAmelCase )} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(__UpperCAmelCase, __UpperCAmelCase )}' so that the correct variant file can be added.", __UpperCAmelCase, ) try: # 2. Load model file as usual snake_case_ = hf_hub_download( __UpperCAmelCase, filename=__UpperCAmelCase, cache_dir=__UpperCAmelCase, force_download=__UpperCAmelCase, proxies=__UpperCAmelCase, resume_download=__UpperCAmelCase, local_files_only=__UpperCAmelCase, use_auth_token=__UpperCAmelCase, user_agent=__UpperCAmelCase, subfolder=__UpperCAmelCase, revision=revision or commit_hash, ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier " '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''' ) except RevisionNotFoundError: raise EnvironmentError( F"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for " '''this model name. Check the model page at ''' F"'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions." ) except EntryNotFoundError: raise EnvironmentError( F"{pretrained_model_name_or_path} does not appear to have a file named {weights_name}." ) except HTTPError as err: raise EnvironmentError( F"There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}" ) except ValueError: raise EnvironmentError( F"We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it" F" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a" F" directory containing a file named {weights_name} or" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''' ) except EnvironmentError: raise EnvironmentError( F"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from " '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F"Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory " F"containing a file named {weights_name}" )
72
1
'''simple docstring''' import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version a__ : Optional[int] = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize a__ : List[str] = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" a__ : Optional[Any] = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" a__ : Optional[Any] = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n \'meteor\': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric(\'meteor\')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class UpperCamelCase__ ( datasets.Metric): def lowercase_ ( self :List[str] ) -> Optional[int]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Value('string' , id='sequence' ), } ) , codebase_urls=['https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py'] , reference_urls=[ 'https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score', 'https://en.wikipedia.org/wiki/METEOR', ] , ) def lowercase_ ( self :List[str] , _A :Tuple ) -> Tuple: '''simple docstring''' import nltk nltk.download('wordnet' ) if NLTK_VERSION >= version.Version('3.6.5' ): nltk.download('punkt' ) if NLTK_VERSION >= version.Version('3.6.6' ): nltk.download('omw-1.4' ) def lowercase_ ( self :Any , _A :Union[str, Any] , _A :Dict , _A :str=0.9 , _A :List[str]=3 , _A :Optional[int]=0.5 ) -> Any: '''simple docstring''' if NLTK_VERSION >= version.Version('3.6.5' ): __A = [ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase_ ) , word_tokenize(lowerCAmelCase_ ) , alpha=lowerCAmelCase_ , beta=lowerCAmelCase_ , gamma=lowerCAmelCase_ ) for ref, pred in zip(lowerCAmelCase_ , lowerCAmelCase_ ) ] else: __A = [ meteor_score.single_meteor_score(lowerCAmelCase_ , lowerCAmelCase_ , alpha=lowerCAmelCase_ , beta=lowerCAmelCase_ , gamma=lowerCAmelCase_ ) for ref, pred in zip(lowerCAmelCase_ , lowerCAmelCase_ ) ] return {"meteor": np.mean(lowerCAmelCase_ )}
161
def snake_case ( snake_case__ :str = "The quick brown fox jumps over the lazy dog" , ) -> bool: _A = set() # Replace all the whitespace in our sentence _A = input_str.replace(""" """ , """""") for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower()) return len(snake_case__) == 26 def snake_case ( snake_case__ :str = "The quick brown fox jumps over the lazy dog" , ) -> bool: _A = [False] * 26 for char in input_str: if char.islower(): _A = True elif char.isupper(): _A = True return all(snake_case__) def snake_case ( snake_case__ :str = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()}) == 26 def snake_case ( ) -> None: from timeit import timeit _A = """from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest""" print(timeit("""is_pangram()""" , setup=snake_case__)) print(timeit("""is_pangram_faster()""" , setup=snake_case__)) print(timeit("""is_pangram_fastest()""" , setup=snake_case__)) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
180
0
"""simple docstring""" # HF Trainer benchmarking tool # # This tool can be used to run and compare multiple dimensions of the HF Trainers args. # # It then prints a report once in github format with all the information that needs to be shared # with others and second time in a console-friendly format, so it's easier to use for tuning things up. # # The main idea is: # # ./trainer-benchmark.py --base-cmd '<cmd args that don't change>' \ # --variations '--tf32 0|--tf32 1' '--fp16 0|--fp16 1|--bf16 1' \ # --target-metric-key train_samples_per_second # # The variations can be any command line argument that you want to compare and not just dtype as in # the example. # # --variations allows you to compare variations in multiple dimensions. # # as the first dimention has 2 options and the second 3 in our example, this will run the trainer 6 # times adding one of: # # 1. --tf32 0 --fp16 0 # 2. --tf32 0 --fp16 1 # 3. --tf32 0 --bf16 1 # 4. --tf32 1 --fp16 0 # 5. --tf32 1 --fp16 1 # 6. --tf32 1 --bf16 1 # # and print the results. This is just a cartesian product - and more than 2 dimensions can be used. # # If you want to rely on defaults, this: # --variations '--tf32 0|--tf32 1' '--fp16 0|--fp16 1|--bf16 1' # is identical to this: # --variations '--tf32 0|--tf32 1' '|--fp16|--bf16' # # the leading empty variation in the 2nd dimension is a valid variation. # # So here we get the following 6 variations: # # 1. --tf32 0 # 2. --tf32 0 --fp16 # 3. --tf32 0 --bf16 # 4. --tf32 1 # 5. --tf32 1 --fp16 # 6. --tf32 1 --bf16 # # In this particular case we don't know what the default tf32 setting is as it's normally # pytorch-version dependent). That's why it's best to do an explicit setting of each variation: # `--tf32 0|--tf32 1` # # Here is a full example of a train: # # CUDA_VISIBLE_DEVICES=0 python ./scripts/benchmark/trainer-benchmark.py \ # --base-cmd \ # ' examples/pytorch/translation/run_translation.py --model_name_or_path t5-small \ # --output_dir output_dir --do_train --label_smoothing 0.1 --logging_strategy no \ # --save_strategy no --per_device_train_batch_size 32 --max_source_length 512 \ # --max_target_length 512 --num_train_epochs 1 --overwrite_output_dir \ # --source_lang en --target_lang ro --dataset_name wmt16 --dataset_config "ro-en" \ # --source_prefix "translate English to Romanian: " --warmup_steps 50 \ # --max_train_samples 20000 --dataloader_num_workers 2 ' \ # --target-metric-key train_samples_per_second --repeat-times 1 --variations \ # '|--fp16|--bf16' '--tf32 0|--tf32 1' --report-metric-keys train_loss \ # --repeat-times 1 --base-variation '--tf32 0' # # and here is a possible output: # # # | Variation | Train | Diff | Train | # | | samples | % | loss | # | | per | | | # | | second | | | # |:----------------|----------:|-------:|--------:| # | --tf32 0 | 285.11 | 0 | 2.51 | # | --tf32 1 | 342.09 | 20 | 2.51 | # | --fp16 --tf32 0 | 423.49 | 49 | 2.51 | # | --fp16 --tf32 1 | 423.13 | 48 | 2.51 | # | --bf16 --tf32 0 | 416.80 | 46 | 2.52 | # | --bf16 --tf32 1 | 415.87 | 46 | 2.52 | # # # So you can quickly compare the different outcomes. # # Typically running each experiment once is enough, but if the environment is unstable you can # re-run each multiple times, e.g., 3 using --repeat-times 3 and it will report the averaged results. # # By default it'll use the lowest result as the base line to use as 100% and then compare the rest to # it as can be seen from the table above, but you can also specify which combination is the one to use as # the baseline, e.g., to change to another entry use: --base-variation '--tf32 1 --fp16 0' # # --target-metric-key is there to tell the program which metrics to compare - the different metric keys are # inside output_dir/all_results.json. e.g., to measure eval performance instead of train use: # --target-metric-key eval_samples_per_second # but of course you will need to adjust the --base-cmd value in the example to perform evaluation as # well (as currently it doesn't) # import argparse import datetime import io import itertools import json import math import os import platform import re import shlex import subprocess import sys from pathlib import Path from statistics import fmean import pandas as pd import torch from tqdm import tqdm import transformers lowercase_ = float("nan") class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a ): __a = sys.stdout __a = open(_a , '''a''' ) def __getattr__( self , _a ): return getattr(self.stdout , _a ) def __UpperCAmelCase ( self , _a ): self.stdout.write(_a ) # strip tqdm codes self.file.write(re.sub(R'''^.*\r''' , '''''' , _a , 0 , re.M ) ) def lowercase ( lowerCAmelCase__ : List[str]=80 , lowerCAmelCase__ : Optional[int]=False ) -> Union[str, Any]: __a = [] # deal with critical env vars __a = ['''CUDA_VISIBLE_DEVICES'''] for key in env_keys: __a = os.environ.get(lowerCAmelCase__ , lowerCAmelCase__ ) if val is not None: cmd.append(f'''{key}={val}''' ) # python executable (not always needed if the script is executable) __a = sys.executable if full_python_path else sys.executable.split('''/''' )[-1] cmd.append(lowerCAmelCase__ ) # now the normal args cmd += list(map(shlex.quote , sys.argv ) ) # split up into up to MAX_WIDTH lines with shell multi-line escapes __a = [] __a = '''''' while len(lowerCAmelCase__ ) > 0: current_line += f'''{cmd.pop(0 )} ''' if len(lowerCAmelCase__ ) == 0 or len(lowerCAmelCase__ ) + len(cmd[0] ) + 1 > max_width - 1: lines.append(lowerCAmelCase__ ) __a = '''''' return "\\\n".join(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple ) -> Any: # unwrap multi-line input __a = re.sub(r'''[\\\n]+''' , ''' ''' , args.base_cmd ) # remove --output_dir if any and set our own __a = re.sub('''--output_dir\s+[^\s]+''' , '''''' , args.base_cmd ) args.base_cmd += f''' --output_dir {output_dir}''' # ensure we have --overwrite_output_dir __a = re.sub('''--overwrite_output_dir\s+''' , '''''' , args.base_cmd ) args.base_cmd += " --overwrite_output_dir" return [sys.executable] + shlex.split(args.base_cmd ) def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int ) -> int: # Enable to debug everything but the run itself, to do it fast and see the progress. # This is useful for debugging the output formatting quickly - we can remove it later once # everybody is happy with the output if 0: import random from time import sleep sleep(0 ) return dict( {k: random.uniform(0 , 100 ) for k in metric_keys} , **{target_metric_key: random.choice([nan, 10.31, 1_00.2, 55.66_66, 2_22.22_22_22_22] )} , ) __a = subprocess.run(lowerCAmelCase__ , capture_output=lowerCAmelCase__ , text=lowerCAmelCase__ ) if verbose: print('''STDOUT''' , result.stdout ) print('''STDERR''' , result.stderr ) # save the streams __a = variation.replace(''' ''' , '''-''' ) with open(Path(lowerCAmelCase__ ) / f'''log.{prefix}.stdout.txt''' , '''w''' ) as f: f.write(result.stdout ) with open(Path(lowerCAmelCase__ ) / f'''log.{prefix}.stderr.txt''' , '''w''' ) as f: f.write(result.stderr ) if result.returncode != 0: if verbose: print('''failed''' ) return {target_metric_key: nan} with io.open(f'''{output_dir}/all_results.json''' , '''r''' , encoding='''utf-8''' ) as f: __a = json.load(lowerCAmelCase__ ) # filter out just the keys we want return {k: v for k, v in metrics.items() if k in metric_keys} def lowercase ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Dict , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] , ) -> int: __a = [] __a = [] __a = f'''{id}: {variation:<{longest_variation_len}}''' __a = f'''{preamble}: ''' __a = set(report_metric_keys + [target_metric_key] ) for i in tqdm(range(lowerCAmelCase__ ) , desc=lowerCAmelCase__ , leave=lowerCAmelCase__ ): __a = process_run_single( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = single_run_metrics[target_metric_key] if not math.isnan(lowerCAmelCase__ ): metrics.append(lowerCAmelCase__ ) results.append(lowerCAmelCase__ ) outcome += "✓" else: outcome += "✘" __a = f'''\33[2K\r{outcome}''' if len(lowerCAmelCase__ ) > 0: __a = {k: fmean([x[k] for x in metrics] ) for k in metrics[0].keys()} __a = round(mean_metrics[target_metric_key] , 2 ) __a = f'''{outcome} {mean_target}''' if len(lowerCAmelCase__ ) > 1: results_str += f''' {tuple(round(lowerCAmelCase__ , 2 ) for x in results )}''' print(lowerCAmelCase__ ) __a = variation return mean_metrics else: print(lowerCAmelCase__ ) return {variation_key: variation, target_metric_key: nan} def lowercase ( ) -> Dict: __a = torch.cuda.get_device_properties(torch.device('''cuda''' ) ) return f''' Datetime : {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S' )} Software: transformers: {transformers.__version__} torch : {torch.__version__} cuda : {torch.version.cuda} python : {platform.python_version()} Hardware: {torch.cuda.device_count()} GPUs : {properties.name}, {properties.total_memory/2**30:0.2f}GB ''' def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[Any] ) -> str: __a = pd.DataFrame(lowerCAmelCase__ ) __a = '''variation''' __a = '''diff_%''' __a = nan if base_variation is not None and len(df[df[variation_key] == base_variation] ): # this may still return nan __a = df.loc[df[variation_key] == base_variation][target_metric_key].item() if math.isnan(lowerCAmelCase__ ): # as a fallback, use the minimal value as the sentinel __a = df.loc[df[target_metric_key] != nan][target_metric_key].min() # create diff column if possible if not math.isnan(lowerCAmelCase__ ): __a = df.apply( lambda lowerCAmelCase__ : round(100 * (r[target_metric_key] - sentinel_value) / sentinel_value ) if not math.isnan(r[target_metric_key] ) else 0 , axis='''columns''' , ) # re-order columns __a = [variation_key, target_metric_key, diff_key, *report_metric_keys] __a = df.reindex(lowerCAmelCase__ , axis='''columns''' ) # reorder cols # capitalize __a = df.rename(str.capitalize , axis='''columns''' ) # make the cols as narrow as possible __a = df.rename(lambda lowerCAmelCase__ : c.replace('''_''' , '''<br>''' ) , axis='''columns''' ) __a = df.rename(lambda lowerCAmelCase__ : c.replace('''_''' , '''\n''' ) , axis='''columns''' ) __a = ['''''', '''Copy between the cut-here-lines and paste as is to github or a forum'''] report += ["----------8<-----------------8<--------"] report += ["*** Results:", df_github.to_markdown(index=lowerCAmelCase__ , floatfmt='''.2f''' )] report += ["```"] report += ["*** Setup:", get_versions()] report += ["*** The benchmark command line was:", get_original_command()] report += ["```"] report += ["----------8<-----------------8<--------"] report += ["*** Results (console):", df_console.to_markdown(index=lowerCAmelCase__ , floatfmt='''.2f''' )] print('''\n\n'''.join(lowerCAmelCase__ ) ) def lowercase ( ) -> Union[str, Any]: __a = argparse.ArgumentParser() parser.add_argument( '''--base-cmd''' , default=lowerCAmelCase__ , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help='''Base cmd''' , ) parser.add_argument( '''--variations''' , default=lowerCAmelCase__ , type=lowerCAmelCase__ , nargs='''+''' , required=lowerCAmelCase__ , help='''Multi-dimensional variations, example: \'|--fp16|--bf16\' \'|--tf32\'''' , ) parser.add_argument( '''--base-variation''' , default=lowerCAmelCase__ , type=lowerCAmelCase__ , help='''Baseline variation to compare to. if None the minimal target value will be used to compare against''' , ) parser.add_argument( '''--target-metric-key''' , default=lowerCAmelCase__ , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help='''Target metric key in output_dir/all_results.json, e.g., train_samples_per_second''' , ) parser.add_argument( '''--report-metric-keys''' , default='''''' , type=lowerCAmelCase__ , help='''Report metric keys - other metric keys from output_dir/all_results.json to report, e.g., train_loss. Use a single argument e.g., \'train_loss train_samples''' , ) parser.add_argument( '''--repeat-times''' , default=1 , type=lowerCAmelCase__ , help='''How many times to re-run each variation - an average will be reported''' , ) parser.add_argument( '''--output_dir''' , default='''output_benchmark''' , type=lowerCAmelCase__ , help='''The output directory where all the benchmark reports will go to and additionally this directory will be used to override --output_dir in the script that is being benchmarked''' , ) parser.add_argument( '''--verbose''' , default=lowerCAmelCase__ , action='''store_true''' , help='''Whether to show the outputs of each run or just the benchmark progress''' , ) __a = parser.parse_args() __a = args.output_dir Path(lowerCAmelCase__ ).mkdir(exist_ok=lowerCAmelCase__ ) __a = get_base_command(lowerCAmelCase__ , lowerCAmelCase__ ) # split each dimension into its --foo variations __a = [list(map(str.strip , re.split(r'''\|''' , lowerCAmelCase__ ) ) ) for x in args.variations] # build a cartesian product of dimensions and convert those back into cmd-line arg strings, # while stripping white space for inputs that were empty __a = list(map(str.strip , map(''' '''.join , itertools.product(*lowerCAmelCase__ ) ) ) ) __a = max(len(lowerCAmelCase__ ) for x in variations ) # split wanted keys __a = args.report_metric_keys.split() # capture prints into a log file for convenience __a = f'''benchmark-report-{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S' )}.txt''' print(f'''\nNote: each run\'s output is also logged under {output_dir}/log.*.std*.txt''' ) print(f'''and this script\'s output is also piped into {report_fn}''' ) __a = Tee(lowerCAmelCase__ ) print(f'''\n*** Running {len(lowerCAmelCase__ )} benchmarks:''' ) print(f'''Base command: {' '.join(lowerCAmelCase__ )}''' ) __a = '''variation''' __a = [] for id, variation in enumerate(tqdm(lowerCAmelCase__ , desc='''Total completion: ''' , leave=lowerCAmelCase__ ) ): __a = base_cmd + variation.split() results.append( process_run( id + 1 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , args.target_metric_key , lowerCAmelCase__ , args.repeat_times , lowerCAmelCase__ , args.verbose , ) ) process_results(lowerCAmelCase__ , args.target_metric_key , lowerCAmelCase__ , args.base_variation , lowerCAmelCase__ ) if __name__ == "__main__": main()
11
"""simple docstring""" from math import factorial, radians def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : int = 18 , lowerCAmelCase__ : int = 10 ) -> float: __a = angle_in_degrees - ((angle_in_degrees // 3_60.0) * 3_60.0) # Converting from degrees to radians __a = radians(lowerCAmelCase__ ) __a = angle_in_radians __a = 3 __a = -1 for _ in range(lowerCAmelCase__ ): result += (b * (angle_in_radians**a)) / factorial(lowerCAmelCase__ ) __a = -b # One positive term and the next will be negative and so on... a += 2 # Increased by 2 for every term. return round(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": __import__("doctest").testmod()
11
1
'''simple docstring''' import unittest from .lib import ( Matrix, Vector, axpy, square_zero_matrix, unit_basis_vector, zero_vector, ) class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" def A ( self : Tuple ): """simple docstring""" UpperCamelCase = Vector([1, 2, 3] ) self.assertEqual(x.component(0 ) , 1 ) self.assertEqual(x.component(2 ) , 3 ) UpperCamelCase = Vector() def A ( self : Any ): """simple docstring""" UpperCamelCase = Vector([0, 0, 0, 0, 0, 1] ) self.assertEqual(str(UpperCamelCase__ ) , '(0,0,0,0,0,1)' ) def A ( self : Dict ): """simple docstring""" UpperCamelCase = Vector([1, 2, 3, 4] ) self.assertEqual(len(UpperCamelCase__ ) , 4 ) def A ( self : Dict ): """simple docstring""" UpperCamelCase = Vector([1, 2] ) UpperCamelCase = Vector([1, 2, 3, 4, 5] ) UpperCamelCase = Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ) UpperCamelCase = Vector([1, -1, 1, -1, 2, -3, 4, -5] ) self.assertAlmostEqual(x.euclidean_length() , 2.2_3_6 , 3 ) self.assertAlmostEqual(y.euclidean_length() , 7.4_1_6 , 3 ) self.assertEqual(z.euclidean_length() , 0 ) self.assertAlmostEqual(w.euclidean_length() , 7.6_1_6 , 3 ) def A ( self : List[Any] ): """simple docstring""" UpperCamelCase = Vector([1, 2, 3] ) UpperCamelCase = Vector([1, 1, 1] ) self.assertEqual((x + y).component(0 ) , 2 ) self.assertEqual((x + y).component(1 ) , 3 ) self.assertEqual((x + y).component(2 ) , 4 ) def A ( self : Any ): """simple docstring""" UpperCamelCase = Vector([1, 2, 3] ) UpperCamelCase = Vector([1, 1, 1] ) self.assertEqual((x - y).component(0 ) , 0 ) self.assertEqual((x - y).component(1 ) , 1 ) self.assertEqual((x - y).component(2 ) , 2 ) def A ( self : Tuple ): """simple docstring""" UpperCamelCase = Vector([1, 2, 3] ) UpperCamelCase = Vector([2, -1, 4] ) # for test of dot product UpperCamelCase = Vector([1, -2, -1] ) self.assertEqual(str(x * 3.0 ) , '(3.0,6.0,9.0)' ) self.assertEqual((a * b) , 0 ) def A ( self : Dict ): """simple docstring""" self.assertEqual(str(zero_vector(1_0 ) ).count('0' ) , 1_0 ) def A ( self : Union[str, Any] ): """simple docstring""" self.assertEqual(str(unit_basis_vector(3 , 1 ) ) , '(0,1,0)' ) def A ( self : str ): """simple docstring""" UpperCamelCase = Vector([1, 2, 3] ) UpperCamelCase = Vector([1, 0, 1] ) self.assertEqual(str(axpy(2 , UpperCamelCase__ , UpperCamelCase__ ) ) , '(3,4,7)' ) def A ( self : Optional[Any] ): """simple docstring""" UpperCamelCase = Vector([1, 0, 0, 0, 0, 0] ) UpperCamelCase = x.copy() self.assertEqual(str(UpperCamelCase__ ) , str(UpperCamelCase__ ) ) def A ( self : List[str] ): """simple docstring""" UpperCamelCase = Vector([1, 0, 0] ) x.change_component(0 , 0 ) x.change_component(1 , 1 ) self.assertEqual(str(UpperCamelCase__ ) , '(0,1,0)' ) def A ( self : Optional[Any] ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual('|1,2,3|\n|2,4,5|\n|6,7,8|\n' , str(UpperCamelCase__ ) ) def A ( self : Any ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) UpperCamelCase = [[-3, -1_4, -1_0], [-5, -1_0, -5], [-2, -1, 0]] for x in range(a.height() ): for y in range(a.width() ): self.assertEqual(minors[x][y] , a.minor(UpperCamelCase__ , UpperCamelCase__ ) ) def A ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) UpperCamelCase = [[-3, 1_4, -1_0], [5, -1_0, 5], [-2, 1, 0]] for x in range(a.height() ): for y in range(a.width() ): self.assertEqual(cofactors[x][y] , a.cofactor(UpperCamelCase__ , UpperCamelCase__ ) ) def A ( self : List[Any] ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual(-5 , a.determinant() ) def A ( self : List[str] ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]] , 3 , 3 ) UpperCamelCase = Vector([1, 2, 3] ) self.assertEqual('(14,32,50)' , str(a * x ) ) self.assertEqual('|2,4,6|\n|8,10,12|\n|14,16,18|\n' , str(a * 2 ) ) def A ( self : Optional[Any] ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) a.change_component(0 , 2 , 5 ) self.assertEqual('|1,2,5|\n|2,4,5|\n|6,7,8|\n' , str(UpperCamelCase__ ) ) def A ( self : List[str] ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual(7 , a.component(2 , 1 ) , 0.0_1 ) def A ( self : List[str] ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) UpperCamelCase = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 1_0]] , 3 , 3 ) self.assertEqual('|2,4,10|\n|4,8,10|\n|12,14,18|\n' , str(a + b ) ) def A ( self : List[str] ): """simple docstring""" UpperCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) UpperCamelCase = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 1_0]] , 3 , 3 ) self.assertEqual('|0,0,-4|\n|0,0,0|\n|0,0,-2|\n' , str(a - b ) ) def A ( self : List[str] ): """simple docstring""" self.assertEqual( '|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n' , str(square_zero_matrix(5 ) ) , ) if __name__ == "__main__": unittest.main()
28
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase__ : Any = logging.get_logger(__name__) lowercase__ : Tuple = { "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json", "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json", } class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" _snake_case = 'xlnet' _snake_case = ['mems'] _snake_case = { 'n_token': 'vocab_size', # Backward compatibility 'hidden_size': 'd_model', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , SCREAMING_SNAKE_CASE_=32000 , SCREAMING_SNAKE_CASE_=1024 , SCREAMING_SNAKE_CASE_=24 , SCREAMING_SNAKE_CASE_=16 , SCREAMING_SNAKE_CASE_=4096 , SCREAMING_SNAKE_CASE_="gelu" , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_="bi" , SCREAMING_SNAKE_CASE_=0.0_2 , SCREAMING_SNAKE_CASE_=1E-12 , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=512 , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=-1 , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_="last" , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_="tanh" , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=5 , SCREAMING_SNAKE_CASE_=5 , SCREAMING_SNAKE_CASE_=5 , SCREAMING_SNAKE_CASE_=1 , SCREAMING_SNAKE_CASE_=2 , **SCREAMING_SNAKE_CASE_ , )-> List[str]: '''simple docstring''' __UpperCamelCase = vocab_size __UpperCamelCase = d_model __UpperCamelCase = n_layer __UpperCamelCase = n_head if d_model % n_head != 0: raise ValueError(F"'d_model % n_head' ({d_model % n_head}) should be equal to 0" ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( F"`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})" ) __UpperCamelCase = d_model // n_head __UpperCamelCase = ff_activation __UpperCamelCase = d_inner __UpperCamelCase = untie_r __UpperCamelCase = attn_type __UpperCamelCase = initializer_range __UpperCamelCase = layer_norm_eps __UpperCamelCase = dropout __UpperCamelCase = mem_len __UpperCamelCase = reuse_len __UpperCamelCase = bi_data __UpperCamelCase = clamp_len __UpperCamelCase = same_length __UpperCamelCase = summary_type __UpperCamelCase = summary_use_proj __UpperCamelCase = summary_activation __UpperCamelCase = summary_last_dropout __UpperCamelCase = start_n_top __UpperCamelCase = end_n_top __UpperCamelCase = bos_token_id __UpperCamelCase = pad_token_id __UpperCamelCase = eos_token_id if "use_cache" in kwargs: warnings.warn( '''The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`''' ''' instead.''' , SCREAMING_SNAKE_CASE_ , ) __UpperCamelCase = kwargs['''use_cache'''] __UpperCamelCase = use_mems_eval __UpperCamelCase = use_mems_train super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) @property def A__ ( self )-> Optional[Any]: '''simple docstring''' logger.info(F"The model {self.model_type} is one of the few models that has no sequence length limit." ) return -1 @max_position_embeddings.setter def A__ ( self , SCREAMING_SNAKE_CASE_ )-> List[str]: '''simple docstring''' raise NotImplementedError( F"The model {self.model_type} is one of the few models that has no sequence length limit." )
328
0
from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class A_ (a__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict = """new-model""" if is_tf_available(): class A_ (a__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict = NewModelConfig @require_tf class A_ (unittest.TestCase ): '''simple docstring''' @slow def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Any = "bert-base-cased" UpperCAmelCase_ : Any = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : Any = TFAutoModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = "bert-base-cased" UpperCAmelCase_ : Dict = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : List[Any] = TFAutoModelForPreTraining.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : List[Any] = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : List[Any] = TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase_ : Dict = TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase__ , output_loading_info=lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Tuple = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : Any = TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : List[Any] = TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase_ : List[Any] = TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase__ , output_loading_info=lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : Any = TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase_ : Optional[int] = TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase__ , output_loading_info=lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: UpperCAmelCase_ : List[Any] = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : List[str] = TFAutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: UpperCAmelCase_ : Optional[Any] = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : int = TFAutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow @require_tensorflow_probability def UpperCamelCase__ ( self ): """simple docstring""" for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : Optional[int] = TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase_ : Dict = TFAutoModelForTableQuestionAnswering.from_pretrained( lowerCAmelCase__ , output_loading_info=lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(model.num_parameters() , 1_4410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase__ ) , 1_4410 ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : str = TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(model.num_parameters() , 1_4410 ) self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase__ ) , 1_4410 ) def UpperCamelCase__ ( self ): """simple docstring""" # For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel UpperCAmelCase_ : List[str] = TFAutoModel.from_pretrained("sgugger/funnel-random-tiny" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ : str = copy.deepcopy(model.config ) UpperCAmelCase_ : Optional[Any] = ["FunnelBaseModel"] UpperCAmelCase_ : List[Any] = TFAutoModel.from_config(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase_ : List[str] = TFAutoModel.from_pretrained(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) def UpperCamelCase__ ( self ): """simple docstring""" try: AutoConfig.register("new-model" , lowerCAmelCase__ ) UpperCAmelCase_ : str = [ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowerCAmelCase__ ): auto_class.register(lowerCAmelCase__ , lowerCAmelCase__ ) auto_class.register(lowerCAmelCase__ , lowerCAmelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCAmelCase__ ): auto_class.register(lowerCAmelCase__ , lowerCAmelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API UpperCAmelCase_ : List[str] = BertModelTester(self ).get_config() UpperCAmelCase_ : Any = NewModelConfig(**tiny_config.to_dict() ) UpperCAmelCase_ : str = auto_class.from_config(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase_ : Any = auto_class.from_pretrained(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def UpperCamelCase__ ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCAmelCase__ , "bert-base is not a local folder and is not a valid model identifier" ): UpperCAmelCase_ : Optional[Any] = TFAutoModel.from_pretrained("bert-base" ) def UpperCamelCase__ ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCAmelCase__ , R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): UpperCAmelCase_ : Any = TFAutoModel.from_pretrained(lowerCAmelCase__ , revision="aaaaaa" ) def UpperCamelCase__ ( self ): """simple docstring""" with self.assertRaisesRegex( lowerCAmelCase__ , "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin" , ): UpperCAmelCase_ : Union[str, Any] = TFAutoModel.from_pretrained("hf-internal-testing/config-no-model" ) def UpperCamelCase__ ( self ): """simple docstring""" with self.assertRaisesRegex(lowerCAmelCase__ , "Use `from_pt=True` to load this model" ): UpperCAmelCase_ : Optional[int] = TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only" ) def UpperCamelCase__ ( self ): """simple docstring""" # Make sure we have cached the model. UpperCAmelCase_ : Optional[int] = TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert" ) with RequestCounter() as counter: UpperCAmelCase_ : Tuple = TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert" ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint UpperCAmelCase_ : Tuple = TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded" ) with RequestCounter() as counter: UpperCAmelCase_ : Tuple = TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded" ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
362
"""simple docstring""" import random import unittest import torch from diffusers import IFImgaImgSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class A_ (lowercase__ ,lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = IFImgaImgSuperResolutionPipeline SCREAMING_SNAKE_CASE__ : Optional[int] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""} SCREAMING_SNAKE_CASE__ : List[str] = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""original_image"""} ) SCREAMING_SNAKE_CASE__ : List[Any] = PipelineTesterMixin.required_optional_params - {"""latents"""} def UpperCamelCase__ ( self ): """simple docstring""" return self._get_superresolution_dummy_components() def UpperCamelCase__ ( self , lowercase_ , lowercase_=0 ): """simple docstring""" if str(lowercase_ ).startswith("mps" ): UpperCAmelCase_ : Optional[Any] = torch.manual_seed(lowercase_ ) else: UpperCAmelCase_ : Union[str, Any] = torch.Generator(device=lowercase_ ).manual_seed(lowercase_ ) UpperCAmelCase_ : Any = floats_tensor((1, 3, 32, 32) , rng=random.Random(lowercase_ ) ).to(lowercase_ ) UpperCAmelCase_ : Optional[int] = floats_tensor((1, 3, 16, 16) , rng=random.Random(lowercase_ ) ).to(lowercase_ ) UpperCAmelCase_ : int = { "prompt": "A painting of a squirrel eating a burger", "image": image, "original_image": original_image, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def UpperCamelCase__ ( self ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def UpperCamelCase__ ( self ): """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != "cuda" , reason="float16 requires CUDA" ) def UpperCamelCase__ ( self ): """simple docstring""" # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1E-1 ) def UpperCamelCase__ ( self ): """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def UpperCamelCase__ ( self ): """simple docstring""" self._test_save_load_local() def UpperCamelCase__ ( self ): """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
23
0
'''simple docstring''' import math from collections.abc import Callable def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> float: __lowerCamelCase = xa __lowerCamelCase = xa while True: if x_n == x_na or function(UpperCamelCase__ ) == function(UpperCamelCase__ ): raise ZeroDivisionError('''float division by zero, could not find root''' ) __lowerCamelCase = x_na - ( function(UpperCamelCase__ ) / ((function(UpperCamelCase__ ) - function(UpperCamelCase__ )) / (x_na - x_n)) ) if abs(x_na - x_na ) < 10**-5: return x_na __lowerCamelCase = x_na __lowerCamelCase = x_na def __lowerCAmelCase ( UpperCamelCase__ ) -> float: return math.pow(UpperCamelCase__ , 3 ) - (2 * x) - 5 if __name__ == "__main__": print(intersection(f, 3, 3.5))
67
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: __UpperCAmelCase =None __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase ={ "vocab_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json" ), }, } __UpperCAmelCase ={ "moussaKam/mbarthez": 1_0_2_4, "moussaKam/barthez": 1_0_2_4, "moussaKam/barthez-orangesum-title": 1_0_2_4, } __UpperCAmelCase ="▁" class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =VOCAB_FILES_NAMES lowerCamelCase : Optional[Any] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] =["input_ids", "attention_mask"] lowerCamelCase : Union[str, Any] =BarthezTokenizer def __init__( self : Optional[Any] , a : Dict=None , a : str=None , a : List[Any]="<s>" , a : Optional[int]="</s>" , a : List[str]="</s>" , a : Tuple="<s>" , a : str="<unk>" , a : Any="<pad>" , a : Union[str, Any]="<mask>" , **a : Union[str, Any] , ): """simple docstring""" __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) __lowerCamelCase = vocab_file __lowerCamelCase = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] __lowerCamelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
67
1
"""simple docstring""" def __lowerCAmelCase ( lowercase : str ) -> bool: """simple docstring""" if not all(x.isalpha() for x in string ): raise ValueError("String must only contain alphabetic characters." ) snake_case : Any = sorted(string.lower() ) return len(lowercase ) == len(set(lowercase ) ) if __name__ == "__main__": __snake_case = input("""Enter a string """).strip() __snake_case = is_isogram(input_str) print(F'''{input_str} is {"an" if isogram else "not an"} isogram.''')
368
"""simple docstring""" import unittest from transformers import is_tf_available from transformers.testing_utils import require_tf if is_tf_available(): import tensorflow as tf from tensorflow.python.eager import context from tensorflow.python.framework import ops from transformers import GradientAccumulator, create_optimizer @require_tf class _lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' self.assertEqual(len(UpperCamelCase__ ) , len(UpperCamelCase__ ) ) for a, b in zip(UpperCamelCase__ , UpperCamelCase__ ): self.assertAlmostEqual(UpperCamelCase__ , UpperCamelCase__ , delta=UpperCamelCase__ ) def lowerCamelCase ( self ) -> Any: '''simple docstring''' snake_case : List[Any] = GradientAccumulator() accumulator([tf.constant([1.0, 2.0] )] ) accumulator([tf.constant([-2.0, 1.0] )] ) accumulator([tf.constant([-1.0, 2.0] )] ) with self.assertRaises(UpperCamelCase__ ): accumulator([tf.constant([1.0, 1.0] ), tf.constant([2.0, 2.0] )] ) self.assertEqual(accumulator.step , 3 ) self.assertEqual(len(accumulator.gradients ) , 1 ) self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist() , [-2.0, 5.0] , tol=1e-2 ) accumulator.reset() self.assertEqual(accumulator.step , 0 ) self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist() , [0.0, 0.0] , tol=1e-2 ) def lowerCamelCase ( self ) -> int: '''simple docstring''' snake_case : Optional[Any] = None ops.enable_eager_execution_internal() snake_case : str = tf.config.list_physical_devices("CPU" ) if len(UpperCamelCase__ ) == 1: tf.config.set_logical_device_configuration( physical_devices[0] , [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()] ) snake_case : Optional[int] = tf.config.list_logical_devices(device_type="CPU" ) snake_case : List[str] = tf.distribute.MirroredStrategy(devices=devices[:2] ) with strategy.scope(): snake_case : int = GradientAccumulator() snake_case : int = tf.Variable([4.0, 3.0] ) snake_case ,snake_case : Any = create_optimizer(5e-5 , 10 , 5 ) snake_case : Tuple = tf.Variable([0.0, 0.0] , trainable=UpperCamelCase__ ) def accumulate_on_replica(UpperCamelCase__ ): accumulator([gradient] ) def apply_on_replica(): optimizer.apply_gradients(list(zip(accumulator.gradients , [variable] ) ) ) @tf.function def accumulate(UpperCamelCase__ , UpperCamelCase__ ): with strategy.scope(): snake_case : Union[str, Any] = strategy.experimental_local_results(UpperCamelCase__ ) local_variables[0].assign(UpperCamelCase__ ) local_variables[1].assign(UpperCamelCase__ ) strategy.run(UpperCamelCase__ , args=(gradient_placeholder,) ) @tf.function def apply_grad(): with strategy.scope(): strategy.run(UpperCamelCase__ ) def _check_local_values(UpperCamelCase__ , UpperCamelCase__ ): snake_case : List[Any] = strategy.experimental_local_results(accumulator._gradients[0] ) self.assertListAlmostEqual(values[0].value() , UpperCamelCase__ , tol=1e-2 ) self.assertListAlmostEqual(values[1].value() , UpperCamelCase__ , tol=1e-2 ) accumulate([1.0, 2.0] , [-1.0, 1.0] ) accumulate([3.0, -1.0] , [-1.0, -1.0] ) accumulate([-2.0, 2.0] , [3.0, -2.0] ) self.assertEqual(accumulator.step , 3 ) _check_local_values([2.0, 3.0] , [1.0, -2.0] ) apply_grad() self.assertListAlmostEqual(variable.value() , [4.0, 3.0] , tol=1e-2 ) accumulator.reset() self.assertEqual(accumulator.step , 0 ) _check_local_values([0.0, 0.0] , [0.0, 0.0] )
112
0
"""simple docstring""" # DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from dataclasses import dataclass from typing import Optional, Tuple, Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin, SchedulerOutput @dataclass class snake_case_( a__ ): __UpperCamelCase = 42 __UpperCamelCase = 42 class snake_case_( a__ , a__ ): __UpperCamelCase = 1 @register_to_config def __init__( self : str , UpperCamelCase_ : int = 2_0_0_0 , UpperCamelCase_ : float = 0.15 , UpperCamelCase_ : float = 0.01 , UpperCamelCase_ : float = 1_348.0 , UpperCamelCase_ : float = 1E-5 , UpperCamelCase_ : int = 1 , ): # standard deviation of the initial noise distribution lowerCAmelCase : Union[str, Any] = sigma_max # setable values lowerCAmelCase : Dict = None self.set_sigmas(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) def lowerCamelCase__ ( self : Tuple , UpperCamelCase_ : torch.FloatTensor , UpperCamelCase_ : Optional[int] = None ): return sample def lowerCamelCase__ ( self : Optional[int] , UpperCamelCase_ : int , UpperCamelCase_ : float = None , UpperCamelCase_ : Union[str, torch.device] = None ): lowerCAmelCase : Optional[Any] = sampling_eps if sampling_eps is not None else self.config.sampling_eps lowerCAmelCase : Dict = torch.linspace(1 , UpperCamelCase_ , UpperCamelCase_ , device=UpperCamelCase_ ) def lowerCamelCase__ ( self : str , UpperCamelCase_ : int , UpperCamelCase_ : float = None , UpperCamelCase_ : float = None , UpperCamelCase_ : float = None ): lowerCAmelCase : Optional[Any] = sigma_min if sigma_min is not None else self.config.sigma_min lowerCAmelCase : Union[str, Any] = sigma_max if sigma_max is not None else self.config.sigma_max lowerCAmelCase : int = sampling_eps if sampling_eps is not None else self.config.sampling_eps if self.timesteps is None: self.set_timesteps(UpperCamelCase_ , UpperCamelCase_ ) lowerCAmelCase : Optional[int] = sigma_min * (sigma_max / sigma_min) ** (self.timesteps / sampling_eps) lowerCAmelCase : Any = torch.exp(torch.linspace(math.log(UpperCamelCase_ ) , math.log(UpperCamelCase_ ) , UpperCamelCase_ ) ) lowerCAmelCase : Dict = torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps] ) def lowerCamelCase__ ( self : Union[str, Any] , UpperCamelCase_ : Union[str, Any] , UpperCamelCase_ : List[Any] ): return torch.where( timesteps == 0 , torch.zeros_like(t.to(timesteps.device ) ) , self.discrete_sigmas[timesteps - 1].to(timesteps.device ) , ) def lowerCamelCase__ ( self : List[Any] , UpperCamelCase_ : torch.FloatTensor , UpperCamelCase_ : int , UpperCamelCase_ : torch.FloatTensor , UpperCamelCase_ : Optional[torch.Generator] = None , UpperCamelCase_ : bool = True , ): if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) lowerCAmelCase : Optional[int] = timestep * torch.ones( sample.shape[0] , device=sample.device ) # torch.repeat_interleave(timestep, sample.shape[0]) lowerCAmelCase : Any = (timestep * (len(self.timesteps ) - 1)).long() # mps requires indices to be in the same device, so we use cpu as is the default with cuda lowerCAmelCase : int = timesteps.to(self.discrete_sigmas.device ) lowerCAmelCase : Union[str, Any] = self.discrete_sigmas[timesteps].to(sample.device ) lowerCAmelCase : Any = self.get_adjacent_sigma(UpperCamelCase_ , UpperCamelCase_ ).to(sample.device ) lowerCAmelCase : Union[str, Any] = torch.zeros_like(UpperCamelCase_ ) lowerCAmelCase : str = (sigma**2 - adjacent_sigma**2) ** 0.5 # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x) # also equation 47 shows the analog from SDE models to ancestral sampling methods lowerCAmelCase : List[str] = diffusion.flatten() while len(diffusion.shape ) < len(sample.shape ): lowerCAmelCase : List[Any] = diffusion.unsqueeze(-1 ) lowerCAmelCase : int = drift - diffusion**2 * model_output # equation 6: sample noise for the diffusion term of lowerCAmelCase : str = randn_tensor( sample.shape , layout=sample.layout , generator=UpperCamelCase_ , device=sample.device , dtype=sample.dtype ) lowerCAmelCase : Optional[Any] = sample - drift # subtract because `dt` is a small negative timestep # TODO is the variable diffusion the correct scaling term for the noise? lowerCAmelCase : Union[str, Any] = prev_sample_mean + diffusion * noise # add impact of diffusion field g if not return_dict: return (prev_sample, prev_sample_mean) return SdeVeOutput(prev_sample=UpperCamelCase_ , prev_sample_mean=UpperCamelCase_ ) def lowerCamelCase__ ( self : Any , UpperCamelCase_ : torch.FloatTensor , UpperCamelCase_ : torch.FloatTensor , UpperCamelCase_ : Optional[torch.Generator] = None , UpperCamelCase_ : bool = True , ): if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z" # sample noise for correction lowerCAmelCase : Union[str, Any] = randn_tensor(sample.shape , layout=sample.layout , generator=UpperCamelCase_ ).to(sample.device ) # compute step size from the model_output, the noise, and the snr lowerCAmelCase : Tuple = torch.norm(model_output.reshape(model_output.shape[0] , -1 ) , dim=-1 ).mean() lowerCAmelCase : str = torch.norm(noise.reshape(noise.shape[0] , -1 ) , dim=-1 ).mean() lowerCAmelCase : Optional[int] = (self.config.snr * noise_norm / grad_norm) ** 2 * 2 lowerCAmelCase : Tuple = step_size * torch.ones(sample.shape[0] ).to(sample.device ) # self.repeat_scalar(step_size, sample.shape[0]) # compute corrected sample: model_output term and noise term lowerCAmelCase : Any = step_size.flatten() while len(step_size.shape ) < len(sample.shape ): lowerCAmelCase : str = step_size.unsqueeze(-1 ) lowerCAmelCase : Optional[Any] = sample + step_size * model_output lowerCAmelCase : List[str] = prev_sample_mean + ((step_size * 2) ** 0.5) * noise if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCamelCase_ ) def lowerCamelCase__ ( self : str , UpperCamelCase_ : torch.FloatTensor , UpperCamelCase_ : torch.FloatTensor , UpperCamelCase_ : torch.FloatTensor , ): # Make sure sigmas and timesteps have the same device and dtype as original_samples lowerCAmelCase : List[Any] = timesteps.to(original_samples.device ) lowerCAmelCase : Optional[int] = self.discrete_sigmas.to(original_samples.device )[timesteps] lowerCAmelCase : str = ( noise * sigmas[:, None, None, None] if noise is not None else torch.randn_like(UpperCamelCase_ ) * sigmas[:, None, None, None] ) lowerCAmelCase : int = noise + original_samples return noisy_samples def __len__( self : Tuple ): return self.config.num_train_timesteps
60
"""simple docstring""" import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process snake_case__ : Dict = logging.getLogger(__name__) def _snake_case ( _snake_case : Any , _snake_case : Any ): return (preds == labels).mean() @dataclass class snake_case_: __UpperCamelCase = field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) __UpperCamelCase = field( default=a__ , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) __UpperCamelCase = field( default=a__ , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) __UpperCamelCase = field( default=a__ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) @dataclass class snake_case_: __UpperCamelCase = field(metadata={'''help''': '''The name of the task to train on: ''' + ''', '''.join(processors.keys() )} ) __UpperCamelCase = field(metadata={'''help''': '''Should contain the data files for the task.'''} ) __UpperCamelCase = field( default=128 , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) } , ) __UpperCamelCase = field( default=a__ , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) def _snake_case ( ): # 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. lowerCAmelCase : str = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) lowerCAmelCase, lowerCAmelCase, lowerCAmelCase : Optional[int] = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ''' --overwrite_output_dir to overcome.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , _snake_case ) # Set seed set_seed(training_args.seed ) try: lowerCAmelCase : Tuple = processors[data_args.task_name]() lowerCAmelCase : Any = processor.get_labels() lowerCAmelCase : Union[str, Any] = len(_snake_case ) except KeyError: raise ValueError('''Task not found: %s''' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. lowerCAmelCase : List[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=_snake_case , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) lowerCAmelCase : Optional[Any] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) lowerCAmelCase : List[str] = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=_snake_case , cache_dir=model_args.cache_dir , ) # Get datasets lowerCAmelCase : Dict = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=_snake_case , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) lowerCAmelCase : Any = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=_snake_case , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(_snake_case : EvalPrediction ) -> Dict: lowerCAmelCase : int = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(_snake_case , p.label_ids )} # Data collator lowerCAmelCase : List[Any] = DataCollatorWithPadding(_snake_case , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer lowerCAmelCase : Union[str, Any] = Trainer( model=_snake_case , args=_snake_case , train_dataset=_snake_case , eval_dataset=_snake_case , compute_metrics=_snake_case , data_collator=_snake_case , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation lowerCAmelCase : int = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) lowerCAmelCase : Any = trainer.evaluate() lowerCAmelCase : int = os.path.join(training_args.output_dir , '''eval_results.txt''' ) if trainer.is_world_master(): with open(_snake_case , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in result.items(): logger.info(''' %s = %s''' , _snake_case , _snake_case ) writer.write('''%s = %s\n''' % (key, value) ) results.update(_snake_case ) return results def _snake_case ( _snake_case : List[str] ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
60
1
def lowercase_ ( _lowerCamelCase : int , _lowerCamelCase : int): if a < 0 or b < 0: raise ValueError("the value of both inputs must be positive") lowercase__ : Tuple = str(bin(_lowerCamelCase))[2:] # remove the leading "0b" lowercase__ : List[Any] = str(bin(_lowerCamelCase))[2:] lowercase__ : Any = max(len(_lowerCamelCase) , len(_lowerCamelCase)) return "0b" + "".join( str(int("1" in (char_a, char_b))) for char_a, char_b in zip(a_binary.zfill(_lowerCamelCase) , b_binary.zfill(_lowerCamelCase))) if __name__ == "__main__": import doctest doctest.testmod()
333
import argparse import datetime def lowercase_ ( _lowerCamelCase : str): lowercase__ : Optional[Any] = { "0": "Sunday", "1": "Monday", "2": "Tuesday", "3": "Wednesday", "4": "Thursday", "5": "Friday", "6": "Saturday", } lowercase__ : Any = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(_lowerCamelCase) < 11: raise ValueError("Must be 10 characters long") # Get month lowercase__ : int = int(date_input[0] + date_input[1]) # Validate if not 0 < m < 13: raise ValueError("Month must be between 1 - 12") lowercase__ : str = date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'") # Get day lowercase__ : int = int(date_input[3] + date_input[4]) # Validate if not 0 < d < 32: raise ValueError("Date must be between 1 - 31") # Get second separator lowercase__ : str = date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'") # Get year lowercase__ : int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9]) # Arbitrary year range if not 45 < y < 8500: raise ValueError( "Year out of range. There has to be some sort of limit...right?") # Get datetime obj for validation lowercase__ : Union[str, Any] = datetime.date(int(_lowerCamelCase) , int(_lowerCamelCase) , int(_lowerCamelCase)) # Start math if m <= 2: lowercase__ : Optional[Any] = y - 1 lowercase__ : int = m + 12 # maths var lowercase__ : int = int(str(_lowerCamelCase)[:2]) lowercase__ : int = int(str(_lowerCamelCase)[2:]) lowercase__ : int = int(2.6 * m - 5.39) lowercase__ : int = int(c / 4) lowercase__ : int = int(k / 4) lowercase__ : int = int(d + k) lowercase__ : int = int(t + u + v + x) lowercase__ : int = int(z - (2 * c)) lowercase__ : int = round(w % 7) # End math # Validate math if f != convert_datetime_days[dt_ck.weekday()]: raise AssertionError("The date was evaluated incorrectly. Contact developer.") # Response lowercase__ : str = f'''Your date {date_input}, is a {days[str(_lowerCamelCase)]}!''' return response if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase = argparse.ArgumentParser( description=( '''Find out what day of the week nearly any date is or was. Enter ''' '''date as a string in the mm-dd-yyyy or mm/dd/yyyy format''' ) ) parser.add_argument( '''date_input''', type=str, help='''Date as a string (mm-dd-yyyy or mm/dd/yyyy)''' ) UpperCamelCase = parser.parse_args() zeller(args.date_input)
333
1
'''simple docstring''' import argparse lowercase : Dict = 'docs/source/_static/js/custom.js' def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' with open(snake_case__ , encoding='''utf-8''' , newline='''\n''' ) as f: A : Dict = f.readlines() A : Dict = 0 # First let's put the right version while not lines[index].startswith('''const stableVersion =''' ): index += 1 A : Union[str, Any] = F'const stableVersion = "v{version}"\n' # Then update the dictionary while not lines[index].startswith('''const versionMapping = {''' ): index += 1 # We go until the end while not lines[index].startswith('''}''' ): index += 1 # We add the new version at the end lines[index - 1] += F' "v{version}": "v{version}",\n' with open(snake_case__ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(snake_case__ ) if __name__ == "__main__": lowercase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument('--version', help='Release version.') lowercase : List[str] = parser.parse_args() update_custom_js(args.version)
3
'''simple docstring''' import os def lowerCAmelCase_ ( ): '''simple docstring''' A : List[Any] = os.path.join(os.path.dirname(snake_case__ ) , '''num.txt''' ) with open(snake_case__ ) as file_hand: return str(sum(int(snake_case__ ) for line in file_hand ) )[:10] if __name__ == "__main__": print(solution())
3
1
'''simple docstring''' from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
361
'''simple docstring''' import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE = { "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json", } class _lowerCAmelCase ( A__ ): """simple docstring""" snake_case_ = "mvp" snake_case_ = ["past_key_values"] snake_case_ = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : int , __snake_case : Optional[int]=5_02_67 , __snake_case : List[Any]=10_24 , __snake_case : str=12 , __snake_case : Union[str, Any]=40_96 , __snake_case : List[Any]=16 , __snake_case : Tuple=12 , __snake_case : Tuple=40_96 , __snake_case : Union[str, Any]=16 , __snake_case : Any=0.0 , __snake_case : Dict=0.0 , __snake_case : List[Any]="gelu" , __snake_case : Tuple=10_24 , __snake_case : int=0.1 , __snake_case : Any=0.0 , __snake_case : List[str]=0.0 , __snake_case : Dict=0.02 , __snake_case : Any=0.0 , __snake_case : Optional[int]=False , __snake_case : List[str]=True , __snake_case : Tuple=1 , __snake_case : Tuple=0 , __snake_case : List[str]=2 , __snake_case : Optional[Any]=True , __snake_case : Dict=2 , __snake_case : Any=2 , __snake_case : Any=False , __snake_case : Any=1_00 , __snake_case : Optional[Any]=8_00 , **__snake_case : List[Any] , )-> Optional[int]: snake_case = vocab_size snake_case = max_position_embeddings snake_case = d_model snake_case = encoder_ffn_dim snake_case = encoder_layers snake_case = encoder_attention_heads snake_case = decoder_ffn_dim snake_case = decoder_layers snake_case = decoder_attention_heads snake_case = dropout snake_case = attention_dropout snake_case = activation_dropout snake_case = activation_function snake_case = init_std snake_case = encoder_layerdrop snake_case = decoder_layerdrop snake_case = classifier_dropout snake_case = use_cache snake_case = encoder_layers snake_case = scale_embedding # scale factor will be sqrt(d_model) if True snake_case = use_prompt snake_case = prompt_length snake_case = prompt_mid_dim super().__init__( pad_token_id=__snake_case , bos_token_id=__snake_case , eos_token_id=__snake_case , is_encoder_decoder=__snake_case , decoder_start_token_id=__snake_case , forced_eos_token_id=__snake_case , **__snake_case , ) if self.forced_bos_token_id is None and kwargs.get("""force_bos_token_to_be_generated""" , __snake_case ): snake_case = self.bos_token_id warnings.warn( f'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. ''' """The config can simply be saved and uploaded again to be fixed.""" )
3
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) A_ = {"""configuration_deit""": ["""DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DeiTConfig""", """DeiTOnnxConfig"""]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ = ["""DeiTFeatureExtractor"""] A_ = ["""DeiTImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ = [ """DEIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """DeiTForImageClassification""", """DeiTForImageClassificationWithTeacher""", """DeiTForMaskedImageModeling""", """DeiTModel""", """DeiTPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ = [ """TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFDeiTForImageClassification""", """TFDeiTForImageClassificationWithTeacher""", """TFDeiTForMaskedImageModeling""", """TFDeiTModel""", """TFDeiTPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys A_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
64
import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils""")) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class _lowercase (unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = 0 @slow def _lowerCamelCase ( self ): '''simple docstring''' for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , (BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(snake_case__ ) , 0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , (GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(snake_case__ ) , 0 ) def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) self.assertIsInstance(snake_case__ , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) self.assertIsInstance(snake_case__ , (RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 20 ) def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoConfig.from_pretrained(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) # Check that tokenizer_type ≠ model_type UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , config=snake_case__ ) self.assertIsInstance(snake_case__ , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def _lowerCamelCase ( self ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy("./tests/fixtures/vocab.txt" , os.path.join(snake_case__ , "vocab.txt" ) ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , tokenizer_type="bert" , use_fast=snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy("./tests/fixtures/vocab.json" , os.path.join(snake_case__ , "vocab.json" ) ) shutil.copy("./tests/fixtures/merges.txt" , os.path.join(snake_case__ , "merges.txt" ) ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , tokenizer_type="gpt2" , use_fast=snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) @require_tokenizers def _lowerCamelCase ( self ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy("./tests/fixtures/vocab.txt" , os.path.join(snake_case__ , "vocab.txt" ) ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , tokenizer_type="bert" ) self.assertIsInstance(snake_case__ , snake_case__ ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy("./tests/fixtures/vocab.json" , os.path.join(snake_case__ , "vocab.json" ) ) shutil.copy("./tests/fixtures/merges.txt" , os.path.join(snake_case__ , "merges.txt" ) ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , tokenizer_type="gpt2" ) self.assertIsInstance(snake_case__ , snake_case__ ) def _lowerCamelCase ( self ): '''simple docstring''' with pytest.raises(snake_case__ ): AutoTokenizer.from_pretrained("./" , tokenizer_type="xxx" ) @require_tokenizers def _lowerCamelCase ( self ): '''simple docstring''' for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: UpperCamelCase_ = tokenizer_class.from_pretrained("wietsedv/bert-base-dutch-cased" ) self.assertIsInstance(snake_case__ , (BertTokenizer, BertTokenizerFast) ) if isinstance(snake_case__ , snake_case__ ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case , snake_case__ ) else: self.assertEqual(tokenizer.do_lower_case , snake_case__ ) self.assertEqual(tokenizer.model_max_length , 512 ) @require_tokenizers def _lowerCamelCase ( self ): '''simple docstring''' for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( snake_case__ , "julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier" , ): UpperCamelCase_ = tokenizer_class.from_pretrained("julien-c/herlolip-not-exists" ) def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = TOKENIZER_MAPPING.values() UpperCamelCase_ = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(snake_case__ ) @require_tokenizers def _lowerCamelCase ( self ): '''simple docstring''' self.assertIsInstance(AutoTokenizer.from_pretrained("bert-base-cased" , use_fast=snake_case__ ) , snake_case__ ) self.assertIsInstance(AutoTokenizer.from_pretrained("bert-base-cased" ) , snake_case__ ) @require_tokenizers def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoTokenizer.from_pretrained("distilbert-base-uncased" , do_lower_case=snake_case__ ) UpperCamelCase_ = "Hello, world. How are you?" UpperCamelCase_ = tokenizer.tokenize(snake_case__ ) self.assertEqual("[UNK]" , tokens[0] ) UpperCamelCase_ = AutoTokenizer.from_pretrained("microsoft/mpnet-base" , do_lower_case=snake_case__ ) UpperCamelCase_ = tokenizer.tokenize(snake_case__ ) self.assertEqual("[UNK]" , tokens[0] ) @require_tokenizers def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoTokenizer.from_pretrained("robot-test/dummy-tokenizer-fast-with-model-config" ) self.assertEqual(type(snake_case__ ) , snake_case__ ) self.assertEqual(tokenizer.model_max_length , 512 ) self.assertEqual(tokenizer.vocab_size , 3_0000 ) self.assertEqual(tokenizer.unk_token , "[UNK]" ) self.assertEqual(tokenizer.padding_side , "right" ) self.assertEqual(tokenizer.truncation_side , "right" ) def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) self.assertIsInstance(snake_case__ , (BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(snake_case__ ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) self.assertIsInstance(snake_case__ , tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size , 12 ) def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoTokenizer.from_pretrained("ctrl" ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(snake_case__ , snake_case__ ) def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = get_tokenizer_config("bert-base-cased" ) UpperCamelCase_ = config.pop("_commit_hash" , snake_case__ ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(snake_case__ , {"do_lower_case": False} ) # This model does not have a tokenizer_config so we get back an empty dict. UpperCamelCase_ = get_tokenizer_config(snake_case__ ) self.assertDictEqual(snake_case__ , {} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(snake_case__ ) UpperCamelCase_ = get_tokenizer_config(snake_case__ ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config["tokenizer_class"] , "BertTokenizer" ) def _lowerCamelCase ( self ): '''simple docstring''' try: AutoConfig.register("custom" , snake_case__ ) AutoTokenizer.register(snake_case__ , slow_tokenizer_class=snake_case__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(snake_case__ ): AutoTokenizer.register(snake_case__ , slow_tokenizer_class=snake_case__ ) UpperCamelCase_ = CustomTokenizer.from_pretrained(snake_case__ ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(snake_case__ ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def _lowerCamelCase ( self ): '''simple docstring''' try: AutoConfig.register("custom" , snake_case__ ) # Can register in two steps AutoTokenizer.register(snake_case__ , slow_tokenizer_class=snake_case__ ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, None) ) AutoTokenizer.register(snake_case__ , fast_tokenizer_class=snake_case__ ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( snake_case__ , slow_tokenizer_class=snake_case__ , fast_tokenizer_class=snake_case__ ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(snake_case__ ): AutoTokenizer.register(snake_case__ , fast_tokenizer_class=snake_case__ ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase_ = BertTokenizerFast.from_pretrained(snake_case__ ) bert_tokenizer.save_pretrained(snake_case__ ) UpperCamelCase_ = CustomTokenizerFast.from_pretrained(snake_case__ ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(snake_case__ ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , use_fast=snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def _lowerCamelCase ( self ): '''simple docstring''' with self.assertRaises(snake_case__ ): UpperCamelCase_ = AutoTokenizer.from_pretrained("hf-internal-testing/test_dynamic_tokenizer" ) # If remote code is disabled, we can't load this config. with self.assertRaises(snake_case__ ): UpperCamelCase_ = AutoTokenizer.from_pretrained( "hf-internal-testing/test_dynamic_tokenizer" , trust_remote_code=snake_case__ ) UpperCamelCase_ = AutoTokenizer.from_pretrained("hf-internal-testing/test_dynamic_tokenizer" , trust_remote_code=snake_case__ ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(snake_case__ ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , trust_remote_code=snake_case__ ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizerFast" ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , "NewTokenizerFast" ) # Test we can also load the slow version UpperCamelCase_ = AutoTokenizer.from_pretrained( "hf-internal-testing/test_dynamic_tokenizer" , trust_remote_code=snake_case__ , use_fast=snake_case__ ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizer" ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(snake_case__ ) UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , trust_remote_code=snake_case__ , use_fast=snake_case__ ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , "NewTokenizer" ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizer" ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , "NewTokenizer" ) @require_tokenizers def _lowerCamelCase ( self ): '''simple docstring''' class _lowercase (a_ ): '''simple docstring''' lowercase__ = False class _lowercase (a_ ): '''simple docstring''' lowercase__ = NewTokenizer lowercase__ = False try: AutoConfig.register("custom" , snake_case__ ) AutoTokenizer.register(snake_case__ , slow_tokenizer_class=snake_case__ ) AutoTokenizer.register(snake_case__ , fast_tokenizer_class=snake_case__ ) # If remote code is not set, the default is to use local UpperCamelCase_ = AutoTokenizer.from_pretrained("hf-internal-testing/test_dynamic_tokenizer" ) self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizerFast" ) self.assertFalse(tokenizer.special_attribute_present ) UpperCamelCase_ = AutoTokenizer.from_pretrained("hf-internal-testing/test_dynamic_tokenizer" , use_fast=snake_case__ ) self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizer" ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. UpperCamelCase_ = AutoTokenizer.from_pretrained( "hf-internal-testing/test_dynamic_tokenizer" , trust_remote_code=snake_case__ ) self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizerFast" ) self.assertFalse(tokenizer.special_attribute_present ) UpperCamelCase_ = AutoTokenizer.from_pretrained( "hf-internal-testing/test_dynamic_tokenizer" , trust_remote_code=snake_case__ , use_fast=snake_case__ ) self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizer" ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub UpperCamelCase_ = AutoTokenizer.from_pretrained( "hf-internal-testing/test_dynamic_tokenizer" , trust_remote_code=snake_case__ ) self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizerFast" ) self.assertTrue(tokenizer.special_attribute_present ) UpperCamelCase_ = AutoTokenizer.from_pretrained( "hf-internal-testing/test_dynamic_tokenizer" , trust_remote_code=snake_case__ , use_fast=snake_case__ ) self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizer" ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoTokenizer.from_pretrained( "hf-internal-testing/test_dynamic_tokenizer_legacy" , trust_remote_code=snake_case__ ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizerFast" ) # Test we can also load the slow version UpperCamelCase_ = AutoTokenizer.from_pretrained( "hf-internal-testing/test_dynamic_tokenizer_legacy" , trust_remote_code=snake_case__ , use_fast=snake_case__ ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizer" ) else: self.assertEqual(tokenizer.__class__.__name__ , "NewTokenizer" ) def _lowerCamelCase ( self ): '''simple docstring''' with self.assertRaisesRegex( snake_case__ , "bert-base is not a local folder and is not a valid model identifier" ): UpperCamelCase_ = AutoTokenizer.from_pretrained("bert-base" ) def _lowerCamelCase ( self ): '''simple docstring''' with self.assertRaisesRegex( snake_case__ , R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): UpperCamelCase_ = AutoTokenizer.from_pretrained(snake_case__ , revision="aaaaaa" ) def _lowerCamelCase ( self ): '''simple docstring''' UpperCamelCase_ = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert" ) with RequestCounter() as counter: UpperCamelCase_ = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert" ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
128
0
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from argparse import ArgumentParser from accelerate.commands.config import get_config_parser from accelerate.commands.env import env_command_parser from accelerate.commands.launch import launch_command_parser from accelerate.commands.test import test_command_parser from accelerate.commands.tpu import tpu_command_parser def a_ ( ): lowerCAmelCase = ArgumentParser('Accelerate CLI tool' , usage='accelerate <command> [<args>]' , allow_abbrev=lowerCamelCase ) lowerCAmelCase = parser.add_subparsers(help='accelerate command helpers' ) # Register commands get_config_parser(subparsers=lowerCamelCase ) env_command_parser(subparsers=lowerCamelCase ) launch_command_parser(subparsers=lowerCamelCase ) tpu_command_parser(subparsers=lowerCamelCase ) test_command_parser(subparsers=lowerCamelCase ) # Let's go lowerCAmelCase = parser.parse_args() if not hasattr(lowerCamelCase , 'func' ): parser.print_help() exit(1 ) # Run args.func(lowerCamelCase ) if __name__ == "__main__": main()
55
'''simple docstring''' import itertools import string from collections.abc import Generator, Iterable def a_ ( lowerCamelCase : Iterable[str] , lowerCamelCase : int ): lowerCAmelCase = iter(lowerCamelCase ) while True: lowerCAmelCase = tuple(itertools.islice(lowerCamelCase , lowerCamelCase ) ) if not chunk: return yield chunk def a_ ( lowerCamelCase : str ): lowerCAmelCase = ''.join([c.upper() for c in dirty if c in string.ascii_letters] ) lowerCAmelCase = '' if len(lowerCamelCase ) < 2: return dirty for i in range(len(lowerCamelCase ) - 1 ): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(lowerCamelCase ) & 1: clean += "X" return clean def a_ ( lowerCamelCase : str ): # I and J are used interchangeably to allow # us to use a 5x5 table (25 letters) lowerCAmelCase = 'ABCDEFGHIKLMNOPQRSTUVWXYZ' # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler lowerCAmelCase = [] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(lowerCamelCase ) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(lowerCamelCase ) return table def a_ ( lowerCamelCase : str , lowerCamelCase : str ): lowerCAmelCase = generate_table(lowerCamelCase ) lowerCAmelCase = prepare_input(lowerCamelCase ) lowerCAmelCase = '' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(lowerCamelCase , 2 ): lowerCAmelCase , lowerCAmelCase = divmod(table.index(lowerCamelCase ) , 5 ) lowerCAmelCase , lowerCAmelCase = divmod(table.index(lowerCamelCase ) , 5 ) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def a_ ( lowerCamelCase : str , lowerCamelCase : str ): lowerCAmelCase = generate_table(lowerCamelCase ) lowerCAmelCase = '' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(lowerCamelCase , 2 ): lowerCAmelCase , lowerCAmelCase = divmod(table.index(lowerCamelCase ) , 5 ) lowerCAmelCase , lowerCAmelCase = divmod(table.index(lowerCamelCase ) , 5 ) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
55
1
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
"""simple docstring""" import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed lowerCAmelCase = """true""" def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : int=8_2 , snake_case_ : Optional[Any]=1_6 ) ->Dict: set_seed(4_2 ) lowerCamelCase__ : List[Any] =RegressionModel() lowerCamelCase__ : List[Any] =deepcopy(snake_case_ ) lowerCamelCase__ : List[str] =RegressionDataset(length=snake_case_ ) lowerCamelCase__ : Any =DataLoader(snake_case_ , batch_size=snake_case_ ) model.to(accelerator.device ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =accelerator.prepare(snake_case_ , snake_case_ ) return model, ddp_model, dataloader def lowerCAmelCase_ ( snake_case_ : Accelerator , snake_case_ : str=False ) ->List[str]: lowerCamelCase__ : int =AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) lowerCamelCase__ : List[Any] =load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(snake_case_ : Optional[Any] ): lowerCamelCase__ : Optional[int] =tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=snake_case_ , max_length=snake_case_ ) return outputs with accelerator.main_process_first(): lowerCamelCase__ : Tuple =dataset.map( snake_case_ , batched=snake_case_ , remove_columns=['idx', 'sentence1', 'sentence2'] , ) lowerCamelCase__ : List[Any] =tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(snake_case_ : Union[str, Any] ): if use_longest: return tokenizer.pad(snake_case_ , padding='longest' , return_tensors='pt' ) return tokenizer.pad(snake_case_ , padding='max_length' , max_length=1_2_8 , return_tensors='pt' ) return DataLoader(snake_case_ , shuffle=snake_case_ , collate_fn=snake_case_ , batch_size=1_6 ) def lowerCAmelCase_ ( snake_case_ : List[str] , snake_case_ : Tuple ) ->Any: lowerCamelCase__ : Optional[int] =Accelerator(dispatch_batches=snake_case_ , split_batches=snake_case_ ) lowerCamelCase__ : List[Any] =get_dataloader(snake_case_ , not dispatch_batches ) lowerCamelCase__ : Union[str, Any] =AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Dict =accelerator.prepare(snake_case_ , snake_case_ ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def lowerCAmelCase_ ( snake_case_ : Tuple , snake_case_ : List[Any] , snake_case_ : List[str] ) ->Dict: lowerCamelCase__ : Optional[Any] =[] for batch in dataloader: lowerCamelCase__ , lowerCamelCase__ : int =batch.values() with torch.no_grad(): lowerCamelCase__ : Optional[Any] =model(snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =[], [] for logit, targ in logits_and_targets: logits.append(snake_case_ ) targs.append(snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =torch.cat(snake_case_ ), torch.cat(snake_case_ ) return logits, targs def lowerCAmelCase_ ( snake_case_ : Accelerator , snake_case_ : Optional[int]=8_2 , snake_case_ : Any=False , snake_case_ : List[Any]=False , snake_case_ : Optional[int]=1_6 ) ->List[str]: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict =get_basic_setup(snake_case_ , snake_case_ , snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Any =generate_predictions(snake_case_ , snake_case_ , snake_case_ ) assert ( len(snake_case_ ) == num_samples ), f"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(snake_case_ )}""" def lowerCAmelCase_ ( snake_case_ : bool = False , snake_case_ : bool = False ) ->str: lowerCamelCase__ : Dict =evaluate.load('glue' , 'mrpc' ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =get_mrpc_setup(snake_case_ , snake_case_ ) # First do baseline lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict =setup['no'] model.to(snake_case_ ) model.eval() for batch in dataloader: batch.to(snake_case_ ) with torch.inference_mode(): lowerCamelCase__ : Any =model(**snake_case_ ) lowerCamelCase__ : List[str] =outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=snake_case_ , references=batch['labels'] ) lowerCamelCase__ : Optional[Any] =metric.compute() # Then do distributed lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Optional[int] =setup['ddp'] model.eval() for batch in dataloader: with torch.inference_mode(): lowerCamelCase__ : List[Any] =model(**snake_case_ ) lowerCamelCase__ : str =outputs.logits.argmax(dim=-1 ) lowerCamelCase__ : int =batch['labels'] lowerCamelCase__ , lowerCamelCase__ : List[Any] =accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=snake_case_ , references=snake_case_ ) lowerCamelCase__ : List[str] =metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), f"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def lowerCAmelCase_ ( ) ->str: lowerCamelCase__ : List[str] =Accelerator(split_batches=snake_case_ , dispatch_batches=snake_case_ ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('**Testing gather_for_metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""" ) test_mrpc(snake_case_ , snake_case_ ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test torch metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: lowerCamelCase__ : Dict =Accelerator(split_batches=snake_case_ , dispatch_batches=snake_case_ ) if accelerator.is_local_main_process: print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""" ) test_torch_metrics(snake_case_ , 9_9 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) lowerCamelCase__ : List[Any] =Accelerator() test_torch_metrics(snake_case_ , 5_1_2 ) accelerator.state._reset_state() def lowerCAmelCase_ ( snake_case_ : List[Any] ) ->Dict: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
126
0
"""simple docstring""" import importlib import os import fsspec import pytest from fsspec import register_implementation from fsspec.registry import _registry as _fsspec_registry from datasets.filesystems import COMPRESSION_FILESYSTEMS, HfFileSystem, extract_path_from_uri, is_remote_filesystem from .utils import require_lza, require_zstandard def __lowercase ( lowerCamelCase : Optional[Any] ): assert "mock" in _fsspec_registry assert "bz2" in _fsspec_registry def __lowercase ( ): assert "mock" not in _fsspec_registry assert "bz2" in _fsspec_registry def __lowercase ( ): UpperCamelCase_ : Dict = 'mock-s3-bucket' UpperCamelCase_ : Dict = F"s3://{mock_bucket}" UpperCamelCase_ : str = extract_path_from_uri(lowerCamelCase ) assert dataset_path.startswith('s3://' ) is False UpperCamelCase_ : Tuple = './local/path' UpperCamelCase_ : Any = extract_path_from_uri(lowerCamelCase ) assert dataset_path == new_dataset_path def __lowercase ( lowerCamelCase : Optional[Any] ): UpperCamelCase_ : Tuple = is_remote_filesystem(lowerCamelCase ) assert is_remote is True UpperCamelCase_ : Tuple = fsspec.filesystem('file' ) UpperCamelCase_ : Tuple = is_remote_filesystem(lowerCamelCase ) assert is_remote is False @pytest.mark.parametrize('compression_fs_class' , lowerCamelCase ) def __lowercase ( lowerCamelCase : Union[str, Any] , lowerCamelCase : Optional[int] , lowerCamelCase : Tuple , lowerCamelCase : Any , lowerCamelCase : Optional[Any] , lowerCamelCase : Any , lowerCamelCase : List[Any] ): UpperCamelCase_ : str = {'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_file, 'bz2': bza_file, 'lz4': lza_file} UpperCamelCase_ : Optional[int] = input_paths[compression_fs_class.protocol] if input_path is None: UpperCamelCase_ : int = F"for '{compression_fs_class.protocol}' compression protocol, " if compression_fs_class.protocol == "lz4": reason += require_lza.kwargs["reason"] elif compression_fs_class.protocol == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(lowerCamelCase ) UpperCamelCase_ : str = fsspec.filesystem(compression_fs_class.protocol , fo=lowerCamelCase ) assert isinstance(lowerCamelCase , lowerCamelCase ) UpperCamelCase_ : List[str] = os.path.basename(lowerCamelCase ) UpperCamelCase_ : Union[str, Any] = expected_filename[: expected_filename.rindex('.' )] assert fs.glob('*' ) == [expected_filename] with fs.open(lowerCamelCase , 'r' , encoding='utf-8' ) as f, open(lowerCamelCase , encoding='utf-8' ) as expected_file: assert f.read() == expected_file.read() @pytest.mark.parametrize('protocol' , ['zip', 'gzip'] ) def __lowercase ( lowerCamelCase : Any , lowerCamelCase : Dict , lowerCamelCase : int ): UpperCamelCase_ : int = {'zip': zip_jsonl_path, 'gzip': jsonl_gz_path} UpperCamelCase_ : str = compressed_file_paths[protocol] UpperCamelCase_ : Union[str, Any] = 'dataset.jsonl' UpperCamelCase_ : List[Any] = F"{protocol}://{member_file_path}::{compressed_file_path}" UpperCamelCase_ : int = fsspec.get_fs_token_paths(lowerCamelCase ) assert fs.isfile(lowerCamelCase ) assert not fs.isfile('non_existing_' + member_file_path ) @pytest.mark.integration def __lowercase ( lowerCamelCase : int , lowerCamelCase : str , lowerCamelCase : Any , lowerCamelCase : Optional[Any] ): UpperCamelCase_ : Optional[Any] = hf_api.dataset_info(lowerCamelCase , token=lowerCamelCase ) UpperCamelCase_ : Union[str, Any] = HfFileSystem(repo_info=lowerCamelCase , token=lowerCamelCase ) assert sorted(hffs.glob('*' ) ) == [".gitattributes", "data"] assert hffs.isdir('data' ) assert hffs.isfile('.gitattributes' ) and hffs.isfile('data/text_data.txt' ) with open(lowerCamelCase ) as f: assert hffs.open('data/text_data.txt' , 'r' ).read() == f.read() def __lowercase ( ): UpperCamelCase_ : Tuple = 'bz2' # Import module import datasets.filesystems # Overwrite protocol and reload register_implementation(lowerCamelCase , lowerCamelCase , clobber=lowerCamelCase ) with pytest.warns(lowerCamelCase ) as warning_info: importlib.reload(datasets.filesystems ) assert len(lowerCamelCase ) == 1 assert ( str(warning_info[0].message ) == F"A filesystem protocol was already set for {protocol} and will be overwritten." )
350
import numpy # List of input, output pairs a_ = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) a_ = (((515, 22, 13), 555), ((61, 35, 49), 150)) a_ = [2, 4, 1, 5] a_ = len(train_data) a_ = 0.009 def __lowercase ( lowerCamelCase : Optional[int] , lowerCamelCase : Any="train" ): return calculate_hypothesis_value(lowerCamelCase , lowerCamelCase ) - output( lowerCamelCase , lowerCamelCase ) def __lowercase ( lowerCamelCase : str ): UpperCamelCase_ : List[str] = 0 for i in range(len(lowerCamelCase ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def __lowercase ( lowerCamelCase : int , lowerCamelCase : Any ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def __lowercase ( lowerCamelCase : Union[str, Any] , lowerCamelCase : List[Any] ): if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def __lowercase ( lowerCamelCase : Union[str, Any] , lowerCamelCase : Dict=m ): UpperCamelCase_ : str = 0 for i in range(lowerCamelCase ): if index == -1: summation_value += _error(lowerCamelCase ) else: summation_value += _error(lowerCamelCase ) * train_data[i][0][index] return summation_value def __lowercase ( lowerCamelCase : int ): UpperCamelCase_ : List[str] = summation_of_cost_derivative(lowerCamelCase , lowerCamelCase ) / m return cost_derivative_value def __lowercase ( ): global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCamelCase_ : Optional[int] = 0.0_0_0_0_0_2 UpperCamelCase_ : Optional[int] = 0 UpperCamelCase_ : Union[str, Any] = 0 while True: j += 1 UpperCamelCase_ : Dict = [0, 0, 0, 0] for i in range(0 , len(lowerCamelCase ) ): UpperCamelCase_ : Any = get_cost_derivative(i - 1 ) UpperCamelCase_ : List[str] = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( lowerCamelCase , lowerCamelCase , atol=lowerCamelCase , rtol=lowerCamelCase , ): break UpperCamelCase_ : Optional[Any] = temp_parameter_vector print(('Number of iterations:', j) ) def __lowercase ( ): for i in range(len(lowerCamelCase ) ): print(('Actual output value:', output(lowerCamelCase , 'test' )) ) print(('Hypothesis output:', calculate_hypothesis_value(lowerCamelCase , 'test' )) ) if __name__ == "__main__": run_gradient_descent() print('\nTesting gradient descent for a linear hypothesis function.\n') test_gradient_descent()
50
0
"""simple docstring""" from math import ceil def snake_case_ ( A_ : Dict, A_ : Tuple ): '''simple docstring''' _lowerCamelCase : List[Any] = list(range(0, A_ ) ) _lowerCamelCase : List[Any] = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check _lowerCamelCase : Optional[int] = [] for i in device_map_blocks: if device_map_blocks.count(A_ ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(A_ ) # Missing blocks _lowerCamelCase : Dict = [i for i in blocks if i not in device_map_blocks] _lowerCamelCase : Union[str, Any] = [i for i in device_map_blocks if i not in blocks] if len(A_ ) != 0: raise ValueError( '''Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.''' ''' These attention blocks were specified more than once: ''' + str(A_ ) ) if len(A_ ) != 0: raise ValueError( '''There are attention blocks for this model that are not specified in the device_map. Add these attention ''' '''blocks to a device on the device_map: ''' + str(A_ ) ) if len(A_ ) != 0: raise ValueError( '''The device_map contains more attention blocks than this model has. Remove these from the device_map:''' + str(A_ ) ) def snake_case_ ( A_ : List[str], A_ : int ): '''simple docstring''' _lowerCamelCase : Any = list(range(A_ ) ) _lowerCamelCase : str = int(ceil(n_layers / len(A_ ) ) ) _lowerCamelCase : Union[str, Any] = [layers[i : i + n_blocks] for i in range(0, A_, A_ )] return dict(zip(A_, A_ ) )
72
"""simple docstring""" def snake_case_ ( A_ : list[int], A_ : str ): '''simple docstring''' _lowerCamelCase : Tuple = int(A_ ) # Initialize Result _lowerCamelCase : Dict = [] # Traverse through all denomination for denomination in reversed(A_ ): # Find denominations while int(A_ ) >= int(A_ ): total_value -= int(A_ ) answer.append(A_ ) # Append the "answers" array return answer # Driver Code if __name__ == "__main__": lowerCAmelCase__ = [] lowerCAmelCase__ = '''0''' if ( input('''Do you want to enter your denominations ? (yY/n): ''').strip().lower() == "y" ): lowerCAmelCase__ = int(input('''Enter the number of denominations you want to add: ''').strip()) for i in range(0, n): denominations.append(int(input(F"""Denomination {i}: """).strip())) lowerCAmelCase__ = input('''Enter the change you want to make in Indian Currency: ''').strip() else: # All denominations of Indian Currency if user does not enter lowerCAmelCase__ = [1, 2, 5, 10, 20, 50, 100, 500, 2000] lowerCAmelCase__ = input('''Enter the change you want to make: ''').strip() if int(value) == 0 or int(value) < 0: print('''The total value cannot be zero or negative.''') else: print(F"""Following is minimal change for {value}: """) lowerCAmelCase__ = find_minimum_change(denominations, value) # Print result for i in range(len(answer)): print(answer[i], end=''' ''')
72
1
import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class __SCREAMING_SNAKE_CASE ( __lowercase): def __init__( self ): """simple docstring""" lowerCAmelCase__ = [] def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_init_end' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_train_begin' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_train_end' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_epoch_begin' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_epoch_end' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_step_begin' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_step_end' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_evaluate' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_predict' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_save' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_log' ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ): """simple docstring""" self.events.append('on_prediction_step' ) @require_torch class __SCREAMING_SNAKE_CASE ( unittest.TestCase): def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = tempfile.mkdtemp() def UpperCamelCase__ ( self ): """simple docstring""" shutil.rmtree(self.output_dir ) def UpperCamelCase__ ( self , _UpperCamelCase=0 , _UpperCamelCase=0 , _UpperCamelCase=64 , _UpperCamelCase=64 , _UpperCamelCase=None , _UpperCamelCase=False , **_UpperCamelCase ): """simple docstring""" # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. lowerCAmelCase__ = RegressionDataset(length=_UpperCamelCase ) lowerCAmelCase__ = RegressionDataset(length=_UpperCamelCase ) lowerCAmelCase__ = RegressionModelConfig(a=_UpperCamelCase , b=_UpperCamelCase ) lowerCAmelCase__ = RegressionPreTrainedModel(_UpperCamelCase ) lowerCAmelCase__ = TrainingArguments(self.output_dir , disable_tqdm=_UpperCamelCase , report_to=[] , **_UpperCamelCase ) return Trainer( _UpperCamelCase , _UpperCamelCase , train_dataset=_UpperCamelCase , eval_dataset=_UpperCamelCase , callbacks=_UpperCamelCase , ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" self.assertEqual(len(_UpperCamelCase ) , len(_UpperCamelCase ) ) # Order doesn't matter lowerCAmelCase__ = sorted(_UpperCamelCase , key=lambda _UpperCamelCase : cb.__name__ if isinstance(_UpperCamelCase , _UpperCamelCase ) else cb.__class__.__name__ ) lowerCAmelCase__ = sorted(_UpperCamelCase , key=lambda _UpperCamelCase : cb.__name__ if isinstance(_UpperCamelCase , _UpperCamelCase ) else cb.__class__.__name__ ) for cba, cba in zip(_UpperCamelCase , _UpperCamelCase ): if isinstance(_UpperCamelCase , _UpperCamelCase ) and isinstance(_UpperCamelCase , _UpperCamelCase ): self.assertEqual(_UpperCamelCase , _UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ) and not isinstance(_UpperCamelCase , _UpperCamelCase ): self.assertEqual(_UpperCamelCase , cba.__class__ ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ) and isinstance(_UpperCamelCase , _UpperCamelCase ): self.assertEqual(cba.__class__ , _UpperCamelCase ) else: self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def UpperCamelCase__ ( self , _UpperCamelCase ): """simple docstring""" lowerCAmelCase__ = ['on_init_end', 'on_train_begin'] lowerCAmelCase__ = 0 lowerCAmelCase__ = len(trainer.get_eval_dataloader() ) lowerCAmelCase__ = ['on_prediction_step'] * len(trainer.get_eval_dataloader() ) + ['on_log', 'on_evaluate'] for _ in range(trainer.state.num_train_epochs ): expected_events.append('on_epoch_begin' ) for _ in range(_UpperCamelCase ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append('on_log' ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append('on_save' ) expected_events.append('on_epoch_end' ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = self.get_trainer() lowerCAmelCase__ = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) # Callbacks passed at init are added to the default callbacks lowerCAmelCase__ = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(_UpperCamelCase ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback lowerCAmelCase__ = self.get_trainer(disable_tqdm=_UpperCamelCase ) lowerCAmelCase__ = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = DEFAULT_CALLBACKS.copy() + [ProgressCallback] lowerCAmelCase__ = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(_UpperCamelCase ) expected_callbacks.remove(_UpperCamelCase ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) lowerCAmelCase__ = self.get_trainer() lowerCAmelCase__ = trainer.pop_callback(_UpperCamelCase ) self.assertEqual(cb.__class__ , _UpperCamelCase ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) trainer.add_callback(_UpperCamelCase ) expected_callbacks.insert(0 , _UpperCamelCase ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) # We can also add, pop, or remove by instance lowerCAmelCase__ = self.get_trainer() lowerCAmelCase__ = trainer.callback_handler.callbacks[0] trainer.remove_callback(_UpperCamelCase ) expected_callbacks.remove(_UpperCamelCase ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) lowerCAmelCase__ = self.get_trainer() lowerCAmelCase__ = trainer.callback_handler.callbacks[0] lowerCAmelCase__ = trainer.pop_callback(_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) trainer.add_callback(_UpperCamelCase ) expected_callbacks.insert(0 , _UpperCamelCase ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _UpperCamelCase ) def UpperCamelCase__ ( self ): """simple docstring""" import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action='ignore' , category=_UpperCamelCase ) lowerCAmelCase__ = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() lowerCAmelCase__ = trainer.callback_handler.callbacks[-2].events self.assertEqual(_UpperCamelCase , self.get_expected_events(_UpperCamelCase ) ) # Independent log/save/eval lowerCAmelCase__ = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() lowerCAmelCase__ = trainer.callback_handler.callbacks[-2].events self.assertEqual(_UpperCamelCase , self.get_expected_events(_UpperCamelCase ) ) lowerCAmelCase__ = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() lowerCAmelCase__ = trainer.callback_handler.callbacks[-2].events self.assertEqual(_UpperCamelCase , self.get_expected_events(_UpperCamelCase ) ) lowerCAmelCase__ = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy='steps' ) trainer.train() lowerCAmelCase__ = trainer.callback_handler.callbacks[-2].events self.assertEqual(_UpperCamelCase , self.get_expected_events(_UpperCamelCase ) ) lowerCAmelCase__ = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy='epoch' ) trainer.train() lowerCAmelCase__ = trainer.callback_handler.callbacks[-2].events self.assertEqual(_UpperCamelCase , self.get_expected_events(_UpperCamelCase ) ) # A bit of everything lowerCAmelCase__ = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=10 , eval_steps=5 , evaluation_strategy='steps' , ) trainer.train() lowerCAmelCase__ = trainer.callback_handler.callbacks[-2].events self.assertEqual(_UpperCamelCase , self.get_expected_events(_UpperCamelCase ) ) # warning should be emitted for duplicated callbacks with patch('transformers.trainer_callback.logger.warning' ) as warn_mock: lowerCAmelCase__ = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(_UpperCamelCase ) in warn_mock.call_args[0][0]
122
from __future__ import annotations from collections.abc import Generator import requests from bsa import BeautifulSoup __snake_case : Any = """https://www.indeed.co.in/jobs?q=mobile+app+development&l=""" def _UpperCamelCase ( UpperCamelCase_ : str = "mumbai" ) -> Generator[tuple[str, str], None, None]: """simple docstring""" lowerCAmelCase__ = BeautifulSoup(requests.get(url + location ).content , 'html.parser' ) # This attribute finds out all the specifics listed in a job for job in soup.find_all('div' , attrs={'data-tn-component': 'organicJob'} ): lowerCAmelCase__ = job.find('a' , attrs={'data-tn-element': 'jobTitle'} ).text.strip() lowerCAmelCase__ = job.find('span' , {'class': 'company'} ).text.strip() yield job_title, company_name if __name__ == "__main__": for i, job in enumerate(fetch_jobs("""Bangalore"""), 1): print(f'Job {i:>2} is {job[0]} at {job[1]}')
122
1
# HF Trainer benchmarking tool # # This tool can be used to run and compare multiple dimensions of the HF Trainers args. # # It then prints a report once in github format with all the information that needs to be shared # with others and second time in a console-friendly format, so it's easier to use for tuning things up. # # The main idea is: # # ./trainer-benchmark.py --base-cmd '<cmd args that don't change>' \ # --variations '--tf32 0|--tf32 1' '--fp16 0|--fp16 1|--bf16 1' \ # --target-metric-key train_samples_per_second # # The variations can be any command line argument that you want to compare and not just dtype as in # the example. # # --variations allows you to compare variations in multiple dimensions. # # as the first dimention has 2 options and the second 3 in our example, this will run the trainer 6 # times adding one of: # # 1. --tf32 0 --fp16 0 # 2. --tf32 0 --fp16 1 # 3. --tf32 0 --bf16 1 # 4. --tf32 1 --fp16 0 # 5. --tf32 1 --fp16 1 # 6. --tf32 1 --bf16 1 # # and print the results. This is just a cartesian product - and more than 2 dimensions can be used. # # If you want to rely on defaults, this: # --variations '--tf32 0|--tf32 1' '--fp16 0|--fp16 1|--bf16 1' # is identical to this: # --variations '--tf32 0|--tf32 1' '|--fp16|--bf16' # # the leading empty variation in the 2nd dimension is a valid variation. # # So here we get the following 6 variations: # # 1. --tf32 0 # 2. --tf32 0 --fp16 # 3. --tf32 0 --bf16 # 4. --tf32 1 # 5. --tf32 1 --fp16 # 6. --tf32 1 --bf16 # # In this particular case we don't know what the default tf32 setting is as it's normally # pytorch-version dependent). That's why it's best to do an explicit setting of each variation: # `--tf32 0|--tf32 1` # # Here is a full example of a train: # # CUDA_VISIBLE_DEVICES=0 python ./scripts/benchmark/trainer-benchmark.py \ # --base-cmd \ # ' examples/pytorch/translation/run_translation.py --model_name_or_path t5-small \ # --output_dir output_dir --do_train --label_smoothing 0.1 --logging_strategy no \ # --save_strategy no --per_device_train_batch_size 32 --max_source_length 512 \ # --max_target_length 512 --num_train_epochs 1 --overwrite_output_dir \ # --source_lang en --target_lang ro --dataset_name wmt16 --dataset_config "ro-en" \ # --source_prefix "translate English to Romanian: " --warmup_steps 50 \ # --max_train_samples 20000 --dataloader_num_workers 2 ' \ # --target-metric-key train_samples_per_second --repeat-times 1 --variations \ # '|--fp16|--bf16' '--tf32 0|--tf32 1' --report-metric-keys train_loss \ # --repeat-times 1 --base-variation '--tf32 0' # # and here is a possible output: # # # | Variation | Train | Diff | Train | # | | samples | % | loss | # | | per | | | # | | second | | | # |:----------------|----------:|-------:|--------:| # | --tf32 0 | 285.11 | 0 | 2.51 | # | --tf32 1 | 342.09 | 20 | 2.51 | # | --fp16 --tf32 0 | 423.49 | 49 | 2.51 | # | --fp16 --tf32 1 | 423.13 | 48 | 2.51 | # | --bf16 --tf32 0 | 416.80 | 46 | 2.52 | # | --bf16 --tf32 1 | 415.87 | 46 | 2.52 | # # # So you can quickly compare the different outcomes. # # Typically running each experiment once is enough, but if the environment is unstable you can # re-run each multiple times, e.g., 3 using --repeat-times 3 and it will report the averaged results. # # By default it'll use the lowest result as the base line to use as 100% and then compare the rest to # it as can be seen from the table above, but you can also specify which combination is the one to use as # the baseline, e.g., to change to another entry use: --base-variation '--tf32 1 --fp16 0' # # --target-metric-key is there to tell the program which metrics to compare - the different metric keys are # inside output_dir/all_results.json. e.g., to measure eval performance instead of train use: # --target-metric-key eval_samples_per_second # but of course you will need to adjust the --base-cmd value in the example to perform evaluation as # well (as currently it doesn't) # import argparse import datetime import io import itertools import json import math import os import platform import re import shlex import subprocess import sys from pathlib import Path from statistics import fmean import pandas as pd import torch from tqdm import tqdm import transformers lowerCAmelCase__ = float('nan') class lowerCAmelCase__ : '''simple docstring''' def __init__( self , __lowerCamelCase) -> Optional[Any]: _A : List[Any] = sys.stdout _A : str = open(__lowerCamelCase , "a") def __getattr__( self , __lowerCamelCase) -> List[str]: return getattr(self.stdout , __lowerCamelCase) def _lowerCamelCase ( self , __lowerCamelCase) -> str: self.stdout.write(__lowerCamelCase) # strip tqdm codes self.file.write(re.sub(r"^.*\r" , "" , __lowerCamelCase , 0 , re.M)) def _UpperCAmelCase (UpperCamelCase__ : str=80 , UpperCamelCase__ : Tuple=False ): _A : Tuple = [] # deal with critical env vars _A : Dict = ["CUDA_VISIBLE_DEVICES"] for key in env_keys: _A : Optional[int] = os.environ.get(UpperCamelCase__ , UpperCamelCase__ ) if val is not None: cmd.append(f"{key}={val}" ) # python executable (not always needed if the script is executable) _A : Optional[int] = sys.executable if full_python_path else sys.executable.split("/" )[-1] cmd.append(UpperCamelCase__ ) # now the normal args cmd += list(map(shlex.quote , sys.argv ) ) # split up into up to MAX_WIDTH lines with shell multi-line escapes _A : Tuple = [] _A : Dict = "" while len(UpperCamelCase__ ) > 0: current_line += f"{cmd.pop(0 )} " if len(UpperCamelCase__ ) == 0 or len(UpperCamelCase__ ) + len(cmd[0] ) + 1 > max_width - 1: lines.append(UpperCamelCase__ ) _A : Union[str, Any] = "" return "\\\n".join(UpperCamelCase__ ) def _UpperCAmelCase (UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple ): # unwrap multi-line input _A : Union[str, Any] = re.sub(r"[\\\n]+" , " " , args.base_cmd ) # remove --output_dir if any and set our own _A : int = re.sub("--output_dir\s+[^\s]+" , "" , args.base_cmd ) args.base_cmd += f" --output_dir {output_dir}" # ensure we have --overwrite_output_dir _A : int = re.sub("--overwrite_output_dir\s+" , "" , args.base_cmd ) args.base_cmd += " --overwrite_output_dir" return [sys.executable] + shlex.split(args.base_cmd ) def _UpperCAmelCase (UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple , UpperCamelCase__ : List[str] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Optional[int] ): # Enable to debug everything but the run itself, to do it fast and see the progress. # This is useful for debugging the output formatting quickly - we can remove it later once # everybody is happy with the output if 0: import random from time import sleep sleep(0 ) return dict( {k: random.uniform(0 , 100 ) for k in metric_keys} , **{target_metric_key: random.choice([nan, 10.31, 1_00.2, 55.66_66, 2_22.22_22_22_22] )} , ) _A : Dict = subprocess.run(UpperCamelCase__ , capture_output=UpperCamelCase__ , text=UpperCamelCase__ ) if verbose: print("STDOUT" , result.stdout ) print("STDERR" , result.stderr ) # save the streams _A : Tuple = variation.replace(" " , "-" ) with open(Path(UpperCamelCase__ ) / f"log.{prefix}.stdout.txt" , "w" ) as f: f.write(result.stdout ) with open(Path(UpperCamelCase__ ) / f"log.{prefix}.stderr.txt" , "w" ) as f: f.write(result.stderr ) if result.returncode != 0: if verbose: print("failed" ) return {target_metric_key: nan} with io.open(f"{output_dir}/all_results.json" , "r" , encoding="utf-8" ) as f: _A : List[str] = json.load(UpperCamelCase__ ) # filter out just the keys we want return {k: v for k, v in metrics.items() if k in metric_keys} def _UpperCAmelCase (UpperCamelCase__ : int , UpperCamelCase__ : Dict , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : int , UpperCamelCase__ : Tuple , UpperCamelCase__ : List[Any] , UpperCamelCase__ : str , UpperCamelCase__ : Any , ): _A : Union[str, Any] = [] _A : Optional[int] = [] _A : Any = f"{id}: {variation:<{longest_variation_len}}" _A : Dict = f"{preamble}: " _A : Union[str, Any] = set(report_metric_keys + [target_metric_key] ) for i in tqdm(range(UpperCamelCase__ ) , desc=UpperCamelCase__ , leave=UpperCamelCase__ ): _A : Optional[Any] = process_run_single( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) _A : Optional[Any] = single_run_metrics[target_metric_key] if not math.isnan(UpperCamelCase__ ): metrics.append(UpperCamelCase__ ) results.append(UpperCamelCase__ ) outcome += "✓" else: outcome += "✘" _A : str = f"\33[2K\r{outcome}" if len(UpperCamelCase__ ) > 0: _A : List[str] = {k: fmean([x[k] for x in metrics] ) for k in metrics[0].keys()} _A : Any = round(mean_metrics[target_metric_key] , 2 ) _A : Tuple = f"{outcome} {mean_target}" if len(UpperCamelCase__ ) > 1: results_str += f" {tuple(round(UpperCamelCase__ , 2 ) for x in results )}" print(UpperCamelCase__ ) _A : Optional[int] = variation return mean_metrics else: print(UpperCamelCase__ ) return {variation_key: variation, target_metric_key: nan} def _UpperCAmelCase (): _A : int = torch.cuda.get_device_properties(torch.device("cuda" ) ) return f"\nDatetime : {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S' )}\n\nSoftware:\ntransformers: {transformers.__version__}\ntorch : {torch.__version__}\ncuda : {torch.version.cuda}\npython : {platform.python_version()}\n\nHardware:\n{torch.cuda.device_count()} GPUs : {properties.name}, {properties.total_memory/2**30:0.2f}GB\n" def _UpperCAmelCase (UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Dict ): _A : Any = pd.DataFrame(UpperCamelCase__ ) _A : List[str] = "variation" _A : List[Any] = "diff_%" _A : int = nan if base_variation is not None and len(df[df[variation_key] == base_variation] ): # this may still return nan _A : int = df.loc[df[variation_key] == base_variation][target_metric_key].item() if math.isnan(UpperCamelCase__ ): # as a fallback, use the minimal value as the sentinel _A : List[str] = df.loc[df[target_metric_key] != nan][target_metric_key].min() # create diff column if possible if not math.isnan(UpperCamelCase__ ): _A : Optional[Any] = df.apply( lambda UpperCamelCase__ : round(100 * (r[target_metric_key] - sentinel_value) / sentinel_value ) if not math.isnan(r[target_metric_key] ) else 0 , axis="columns" , ) # re-order columns _A : Union[str, Any] = [variation_key, target_metric_key, diff_key, *report_metric_keys] _A : Any = df.reindex(UpperCamelCase__ , axis="columns" ) # reorder cols # capitalize _A : Tuple = df.rename(str.capitalize , axis="columns" ) # make the cols as narrow as possible _A : List[str] = df.rename(lambda UpperCamelCase__ : c.replace("_" , "<br>" ) , axis="columns" ) _A : Union[str, Any] = df.rename(lambda UpperCamelCase__ : c.replace("_" , "\n" ) , axis="columns" ) _A : Optional[int] = ["", "Copy between the cut-here-lines and paste as is to github or a forum"] report += ["----------8<-----------------8<--------"] report += ["*** Results:", df_github.to_markdown(index=UpperCamelCase__ , floatfmt=".2f" )] report += ["```"] report += ["*** Setup:", get_versions()] report += ["*** The benchmark command line was:", get_original_command()] report += ["```"] report += ["----------8<-----------------8<--------"] report += ["*** Results (console):", df_console.to_markdown(index=UpperCamelCase__ , floatfmt=".2f" )] print("\n\n".join(UpperCamelCase__ ) ) def _UpperCAmelCase (): _A : int = argparse.ArgumentParser() parser.add_argument( "--base-cmd" , default=UpperCamelCase__ , type=UpperCamelCase__ , required=UpperCamelCase__ , help="Base cmd" , ) parser.add_argument( "--variations" , default=UpperCamelCase__ , type=UpperCamelCase__ , nargs="+" , required=UpperCamelCase__ , help="Multi-dimensional variations, example: '|--fp16|--bf16' '|--tf32'" , ) parser.add_argument( "--base-variation" , default=UpperCamelCase__ , type=UpperCamelCase__ , help="Baseline variation to compare to. if None the minimal target value will be used to compare against" , ) parser.add_argument( "--target-metric-key" , default=UpperCamelCase__ , type=UpperCamelCase__ , required=UpperCamelCase__ , help="Target metric key in output_dir/all_results.json, e.g., train_samples_per_second" , ) parser.add_argument( "--report-metric-keys" , default="" , type=UpperCamelCase__ , help="Report metric keys - other metric keys from output_dir/all_results.json to report, e.g., train_loss. Use a single argument e.g., 'train_loss train_samples" , ) parser.add_argument( "--repeat-times" , default=1 , type=UpperCamelCase__ , help="How many times to re-run each variation - an average will be reported" , ) parser.add_argument( "--output_dir" , default="output_benchmark" , type=UpperCamelCase__ , help="The output directory where all the benchmark reports will go to and additionally this directory will be used to override --output_dir in the script that is being benchmarked" , ) parser.add_argument( "--verbose" , default=UpperCamelCase__ , action="store_true" , help="Whether to show the outputs of each run or just the benchmark progress" , ) _A : int = parser.parse_args() _A : Union[str, Any] = args.output_dir Path(UpperCamelCase__ ).mkdir(exist_ok=UpperCamelCase__ ) _A : Tuple = get_base_command(UpperCamelCase__ , UpperCamelCase__ ) # split each dimension into its --foo variations _A : Dict = [list(map(str.strip , re.split(r"\|" , UpperCamelCase__ ) ) ) for x in args.variations] # build a cartesian product of dimensions and convert those back into cmd-line arg strings, # while stripping white space for inputs that were empty _A : Union[str, Any] = list(map(str.strip , map(" ".join , itertools.product(*UpperCamelCase__ ) ) ) ) _A : Union[str, Any] = max(len(UpperCamelCase__ ) for x in variations ) # split wanted keys _A : str = args.report_metric_keys.split() # capture prints into a log file for convenience _A : Optional[int] = f"benchmark-report-{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S' )}.txt" print(f"\nNote: each run's output is also logged under {output_dir}/log.*.std*.txt" ) print(f"and this script's output is also piped into {report_fn}" ) _A : Tuple = Tee(UpperCamelCase__ ) print(f"\n*** Running {len(UpperCamelCase__ )} benchmarks:" ) print(f"Base command: {' '.join(UpperCamelCase__ )}" ) _A : str = "variation" _A : Union[str, Any] = [] for id, variation in enumerate(tqdm(UpperCamelCase__ , desc="Total completion: " , leave=UpperCamelCase__ ) ): _A : Dict = base_cmd + variation.split() results.append( process_run( id + 1 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , args.target_metric_key , UpperCamelCase__ , args.repeat_times , UpperCamelCase__ , args.verbose , ) ) process_results(UpperCamelCase__ , args.target_metric_key , UpperCamelCase__ , args.base_variation , UpperCamelCase__ ) if __name__ == "__main__": main()
11
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { 'facebook/vit-mae-base': 'https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json', # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class lowerCAmelCase__ ( a): '''simple docstring''' __SCREAMING_SNAKE_CASE = "vit_mae" def __init__( self , __lowerCamelCase=7_6_8 , __lowerCamelCase=1_2 , __lowerCamelCase=1_2 , __lowerCamelCase=3_0_7_2 , __lowerCamelCase="gelu" , __lowerCamelCase=0.0 , __lowerCamelCase=0.0 , __lowerCamelCase=0.0_2 , __lowerCamelCase=1e-12 , __lowerCamelCase=2_2_4 , __lowerCamelCase=1_6 , __lowerCamelCase=3 , __lowerCamelCase=True , __lowerCamelCase=1_6 , __lowerCamelCase=5_1_2 , __lowerCamelCase=8 , __lowerCamelCase=2_0_4_8 , __lowerCamelCase=0.7_5 , __lowerCamelCase=False , **__lowerCamelCase , ) -> int: super().__init__(**__lowerCamelCase) _A : int = hidden_size _A : List[str] = num_hidden_layers _A : List[Any] = num_attention_heads _A : Optional[Any] = intermediate_size _A : Optional[int] = hidden_act _A : List[Any] = hidden_dropout_prob _A : List[Any] = attention_probs_dropout_prob _A : Union[str, Any] = initializer_range _A : str = layer_norm_eps _A : Any = image_size _A : int = patch_size _A : int = num_channels _A : Dict = qkv_bias _A : Tuple = decoder_num_attention_heads _A : Tuple = decoder_hidden_size _A : List[str] = decoder_num_hidden_layers _A : Optional[Any] = decoder_intermediate_size _A : List[str] = mask_ratio _A : Union[str, Any] = norm_pix_loss
11
1
def UpperCAmelCase_ (_lowerCAmelCase : int = 1_00 ): __UpperCamelCase : Tuple = n * (n + 1) * (2 * n + 1) / 6 __UpperCamelCase : List[str] = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(F"""{solution() = }""")
171
import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import List import timm import torch import torch.nn as nn from huggingface_hub import hf_hub_download from torch import Tensor from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification from transformers.utils import logging logging.set_verbosity_info() lowercase : Any = logging.get_logger() @dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" lowercase : nn.Module lowercase : List[nn.Module] = field(default_factory=lowerCamelCase__ ) lowercase : list = field(default_factory=lowerCamelCase__ ) def __lowerCamelCase ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: '''simple docstring''' __UpperCamelCase : Optional[int] = len(list(m.modules() ) ) == 1 or isinstance(__UpperCamelCase , nn.Convad ) or isinstance(__UpperCamelCase , nn.BatchNormad ) if has_not_submodules: self.traced.append(__UpperCamelCase ) def __call__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(__UpperCamelCase ) [x.remove() for x in self.handles] return self @property def __lowerCamelCase ( self ) -> Tuple: '''simple docstring''' return list(filter(lambda __UpperCamelCase : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" lowercase : nn.Module lowercase : nn.Module lowercase : int = 0 lowercase : List = field(default_factory=lowerCamelCase__ ) lowercase : List = field(default_factory=lowerCamelCase__ ) def __call__( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' __UpperCamelCase : Optional[Any] = Tracker(self.dest )(__UpperCamelCase ).parametrized __UpperCamelCase : Union[str, Any] = Tracker(self.src )(__UpperCamelCase ).parametrized __UpperCamelCase : Union[str, Any] = list(filter(lambda __UpperCamelCase : type(__UpperCamelCase ) not in self.src_skip , __UpperCamelCase ) ) __UpperCamelCase : Any = list(filter(lambda __UpperCamelCase : type(__UpperCamelCase ) not in self.dest_skip , __UpperCamelCase ) ) if len(__UpperCamelCase ) != len(__UpperCamelCase ): raise Exception( f'''Numbers of operations are different. Source module has {len(__UpperCamelCase )} operations while''' f''' destination module has {len(__UpperCamelCase )}.''' ) for dest_m, src_m in zip(__UpperCamelCase , __UpperCamelCase ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f'''Transfered from={src_m} to={dest_m}''' ) def UpperCAmelCase_ (_lowerCAmelCase : str , _lowerCAmelCase : ResNetConfig , _lowerCAmelCase : Path , _lowerCAmelCase : bool = True ): print(F'''Converting {name}...''' ) with torch.no_grad(): __UpperCamelCase : Optional[Any] = timm.create_model(_lowerCAmelCase , pretrained=_lowerCAmelCase ).eval() __UpperCamelCase : Union[str, Any] = ResNetForImageClassification(_lowerCAmelCase ).eval() __UpperCamelCase : Any = ModuleTransfer(src=_lowerCAmelCase , dest=_lowerCAmelCase ) __UpperCamelCase : Optional[int] = torch.randn((1, 3, 2_24, 2_24) ) module_transfer(_lowerCAmelCase ) assert torch.allclose(from_model(_lowerCAmelCase ) , our_model(_lowerCAmelCase ).logits ), "The model logits don't match the original one." __UpperCamelCase : Tuple = F'''resnet{"-".join(name.split("resnet" ) )}''' print(_lowerCAmelCase ) if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message="Add model" , use_temp_dir=_lowerCAmelCase , ) # we can use the convnext one __UpperCamelCase : List[str] = AutoImageProcessor.from_pretrained("facebook/convnext-base-224-22k-1k" ) image_processor.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message="Add image processor" , use_temp_dir=_lowerCAmelCase , ) print(F'''Pushed {checkpoint_name}''' ) def UpperCAmelCase_ (_lowerCAmelCase : Path , _lowerCAmelCase : str = None , _lowerCAmelCase : bool = True ): __UpperCamelCase : str = "imagenet-1k-id2label.json" __UpperCamelCase : Dict = 10_00 __UpperCamelCase : Any = (1, num_labels) __UpperCamelCase : Union[str, Any] = "huggingface/label-files" __UpperCamelCase : List[Any] = num_labels __UpperCamelCase : Tuple = json.load(open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type="dataset" ) , "r" ) ) __UpperCamelCase : Optional[Any] = {int(_lowerCAmelCase ): v for k, v in idalabel.items()} __UpperCamelCase : List[str] = idalabel __UpperCamelCase : str = {v: k for k, v in idalabel.items()} __UpperCamelCase : Dict = partial(_lowerCAmelCase , num_labels=_lowerCAmelCase , idalabel=_lowerCAmelCase , labelaid=_lowerCAmelCase ) __UpperCamelCase : List[str] = { "resnet18": ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[64, 1_28, 2_56, 5_12] , layer_type="basic" ), "resnet26": ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[2_56, 5_12, 10_24, 20_48] , layer_type="bottleneck" ), "resnet34": ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[64, 1_28, 2_56, 5_12] , layer_type="basic" ), "resnet50": ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[2_56, 5_12, 10_24, 20_48] , layer_type="bottleneck" ), "resnet101": ImageNetPreTrainedConfig( depths=[3, 4, 23, 3] , hidden_sizes=[2_56, 5_12, 10_24, 20_48] , layer_type="bottleneck" ), "resnet152": ImageNetPreTrainedConfig( depths=[3, 8, 36, 3] , hidden_sizes=[2_56, 5_12, 10_24, 20_48] , layer_type="bottleneck" ), } if model_name: convert_weight_and_push(_lowerCAmelCase , names_to_config[model_name] , _lowerCAmelCase , _lowerCAmelCase ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) return config, expected_shape if __name__ == "__main__": lowercase : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default=None, type=str, help=( "The name of the model you wish to convert, it must be one of the supported resnet* architecture," " currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=Path, required=True, help="Path to the output PyTorch model directory.", ) parser.add_argument( "--push_to_hub", default=True, type=bool, required=False, help="If True, push model and image processor to the hub.", ) lowercase : Union[str, Any] = parser.parse_args() lowercase : Path = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
171
1
"""simple docstring""" from .glue import GlueDataset, GlueDataTrainingArguments from .language_modeling import ( LineByLineTextDataset, LineByLineWithRefDataset, LineByLineWithSOPTextDataset, TextDataset, TextDatasetForNextSentencePrediction, ) from .squad import SquadDataset, SquadDataTrainingArguments
78
'''simple docstring''' from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake UpperCamelCase__: Tuple = numpy.array([0, 0]) UpperCamelCase__: Union[str, Any] = numpy.array([0.5, 0.8660254]) UpperCamelCase__: Dict = numpy.array([1, 0]) UpperCamelCase__: int = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def snake_case_ ( _lowerCAmelCase : list[numpy.ndarray] , _lowerCAmelCase : int ) -> list[numpy.ndarray]: UpperCAmelCase : Union[str, Any] = initial_vectors for _ in range(_lowerCAmelCase ): UpperCAmelCase : Union[str, Any] = iteration_step(_lowerCAmelCase ) return vectors def snake_case_ ( _lowerCAmelCase : list[numpy.ndarray] ) -> list[numpy.ndarray]: UpperCAmelCase : Tuple = [] for i, start_vector in enumerate(vectors[:-1] ): UpperCAmelCase : List[str] = vectors[i + 1] new_vectors.append(_lowerCAmelCase ) UpperCAmelCase : Optional[Any] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 , 60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def snake_case_ ( _lowerCAmelCase : numpy.ndarray , _lowerCAmelCase : float ) -> numpy.ndarray: UpperCAmelCase : List[str] = numpy.radians(_lowerCAmelCase ) UpperCAmelCase , UpperCAmelCase : Tuple = numpy.cos(_lowerCAmelCase ), numpy.sin(_lowerCAmelCase ) UpperCAmelCase : Union[str, Any] = numpy.array(((c, -s), (s, c)) ) return numpy.dot(_lowerCAmelCase , _lowerCAmelCase ) def snake_case_ ( _lowerCAmelCase : list[numpy.ndarray] ) -> None: UpperCAmelCase : List[Any] = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() UpperCAmelCase , UpperCAmelCase : str = zip(*_lowerCAmelCase ) plt.plot(_lowerCAmelCase , _lowerCAmelCase ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__: List[Any] = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
23
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 ): """simple docstring""" lowerCAmelCase = 1_0000 lowerCAmelCase = None lowerCAmelCase = None class __a( datasets.ArrowBasedBuilder ): """simple docstring""" lowerCAmelCase = ParquetConfig def a__ ( self ) -> Union[str, Any]: return datasets.DatasetInfo(features=self.config.features ) def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> Optional[int]: 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}''' ) UpperCAmelCase_ : List[Any] = dl_manager.download_and_extract(self.config.data_files ) if isinstance(_SCREAMING_SNAKE_CASE ,(str, list, tuple) ): UpperCAmelCase_ : List[str] = data_files if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Optional[Any] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive UpperCAmelCase_ : Dict = [dl_manager.iter_files(_SCREAMING_SNAKE_CASE ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN ,gen_kwargs={'''files''': files} )] UpperCAmelCase_ : Optional[Any] = [] for split_name, files in data_files.items(): if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Any = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive UpperCAmelCase_ : List[str] = [dl_manager.iter_files(_SCREAMING_SNAKE_CASE ) 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(_SCREAMING_SNAKE_CASE ): with open(_SCREAMING_SNAKE_CASE ,'''rb''' ) as f: UpperCAmelCase_ : Any = datasets.Features.from_arrow_schema(pq.read_schema(_SCREAMING_SNAKE_CASE ) ) break splits.append(datasets.SplitGenerator(name=_SCREAMING_SNAKE_CASE ,gen_kwargs={'''files''': files} ) ) return splits def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> 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 UpperCAmelCase_ : int = table_cast(_SCREAMING_SNAKE_CASE ,self.info.features.arrow_schema ) return pa_table def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> Optional[int]: UpperCAmelCase_ : List[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(_SCREAMING_SNAKE_CASE ) ): with open(_SCREAMING_SNAKE_CASE ,'''rb''' ) as f: UpperCAmelCase_ : int = pq.ParquetFile(_SCREAMING_SNAKE_CASE ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size ,columns=self.config.columns ) ): UpperCAmelCase_ : str = 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(_SCREAMING_SNAKE_CASE ) except ValueError as e: logger.error(f'''Failed to read file \'{file}\' with error {type(_SCREAMING_SNAKE_CASE )}: {e}''' ) raise
360
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast @require_vision class __a( unittest.TestCase ): """simple docstring""" def a__ ( self ) -> Optional[int]: UpperCAmelCase_ : Optional[int] = tempfile.mkdtemp() UpperCAmelCase_ : str = BlipImageProcessor() UpperCAmelCase_ : Dict = GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''' ) UpperCAmelCase_ : Optional[Any] = BlipaProcessor(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) processor.save_pretrained(self.tmpdirname ) def a__ ( self ,**_SCREAMING_SNAKE_CASE ) -> Optional[int]: return AutoProcessor.from_pretrained(self.tmpdirname ,**_SCREAMING_SNAKE_CASE ).tokenizer def a__ ( self ,**_SCREAMING_SNAKE_CASE ) -> Dict: return AutoProcessor.from_pretrained(self.tmpdirname ,**_SCREAMING_SNAKE_CASE ).image_processor def a__ ( self ) -> List[Any]: shutil.rmtree(self.tmpdirname ) def a__ ( self ) -> Tuple: UpperCAmelCase_ : Tuple = [np.random.randint(255 ,size=(3, 30, 400) ,dtype=np.uinta )] UpperCAmelCase_ : Optional[Any] = [Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE ,0 ,-1 ) ) for x in image_inputs] return image_inputs def a__ ( self ) -> List[str]: UpperCAmelCase_ : Dict = BlipaProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : str = self.get_tokenizer(bos_token='''(BOS)''' ,eos_token='''(EOS)''' ) UpperCAmelCase_ : int = self.get_image_processor(do_normalize=_SCREAMING_SNAKE_CASE ,padding_value=1.0 ) UpperCAmelCase_ : Union[str, Any] = BlipaProcessor.from_pretrained( self.tmpdirname ,bos_token='''(BOS)''' ,eos_token='''(EOS)''' ,do_normalize=_SCREAMING_SNAKE_CASE ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,_SCREAMING_SNAKE_CASE ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> Any: UpperCAmelCase_ : Dict = self.get_image_processor() UpperCAmelCase_ : Any = self.get_tokenizer() UpperCAmelCase_ : str = BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE ,image_processor=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = self.prepare_image_inputs() UpperCAmelCase_ : Optional[Any] = image_processor(_SCREAMING_SNAKE_CASE ,return_tensors='''np''' ) UpperCAmelCase_ : int = processor(images=_SCREAMING_SNAKE_CASE ,return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() ,input_processor[key].sum() ,delta=1e-2 ) def a__ ( self ) -> int: UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : List[Any] = self.get_tokenizer() UpperCAmelCase_ : Any = BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE ,image_processor=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = '''lower newer''' UpperCAmelCase_ : Optional[int] = processor(text=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = tokenizer(_SCREAMING_SNAKE_CASE ,return_token_type_ids=_SCREAMING_SNAKE_CASE ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def a__ ( self ) -> Optional[int]: UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : List[Any] = self.get_tokenizer() UpperCAmelCase_ : Tuple = BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE ,image_processor=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = '''lower newer''' UpperCAmelCase_ : int = self.prepare_image_inputs() UpperCAmelCase_ : List[str] = processor(text=_SCREAMING_SNAKE_CASE ,images=_SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) ,['''pixel_values''', '''input_ids''', '''attention_mask'''] ) # test if it raises when no input is passed with pytest.raises(_SCREAMING_SNAKE_CASE ): processor() def a__ ( self ) -> Optional[int]: UpperCAmelCase_ : Tuple = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : List[str] = BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE ,image_processor=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase_ : List[str] = processor.batch_decode(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = tokenizer.batch_decode(_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> str: UpperCAmelCase_ : Union[str, Any] = self.get_image_processor() UpperCAmelCase_ : int = self.get_tokenizer() UpperCAmelCase_ : Any = BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE ,image_processor=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = '''lower newer''' UpperCAmelCase_ : Union[str, Any] = self.prepare_image_inputs() UpperCAmelCase_ : Any = processor(text=_SCREAMING_SNAKE_CASE ,images=_SCREAMING_SNAKE_CASE ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) ,['''pixel_values''', '''input_ids''', '''attention_mask'''] )
235
0
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class lowercase_ ( lowerCamelCase__ ): '''simple docstring''' __snake_case = ['''image_processor''', '''tokenizer'''] __snake_case = '''Pix2StructImageProcessor''' __snake_case = ('''T5Tokenizer''', '''T5TokenizerFast''') def __init__( self : Optional[Any] , __UpperCAmelCase : str , __UpperCAmelCase : int ) ->str: """simple docstring""" a = False super().__init__(lowerCAmelCase__ , lowerCAmelCase__ ) def __call__( self : Dict , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __UpperCAmelCase : bool = True , __UpperCAmelCase : Union[bool, str, PaddingStrategy] = False , __UpperCAmelCase : Union[bool, str, TruncationStrategy] = None , __UpperCAmelCase : Optional[int] = None , __UpperCAmelCase : Optional[int] = 2_048 , __UpperCAmelCase : int = 0 , __UpperCAmelCase : Optional[int] = None , __UpperCAmelCase : Optional[bool] = None , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = False , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Union[str, TensorType]] = None , **__UpperCAmelCase : Optional[int] , ) ->List[str]: """simple docstring""" if images is None and text is None: raise ValueError('''You have to specify either images or text.''' ) # Get only text if images is None and not self.image_processor.is_vqa: a = self.tokenizer a = self.tokenizer( text=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ , stride=lowerCAmelCase__ , pad_to_multiple_of=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , return_overflowing_tokens=lowerCAmelCase__ , return_special_tokens_mask=lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ , return_length=lowerCAmelCase__ , verbose=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ , ) return text_encoding if not self.image_processor.is_vqa: # add pixel_values a = self.image_processor( lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , max_patches=lowerCAmelCase__ , **lowerCAmelCase__ ) else: # add pixel_values and bbox a = self.image_processor( lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , max_patches=lowerCAmelCase__ , header_text=lowerCAmelCase__ , **lowerCAmelCase__ ) if text is not None and not self.image_processor.is_vqa: a = self.tokenizer( text=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ , stride=lowerCAmelCase__ , pad_to_multiple_of=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , return_overflowing_tokens=lowerCAmelCase__ , return_special_tokens_mask=lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ , return_length=lowerCAmelCase__ , verbose=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ , ) if "attention_mask" in text_encoding: a = text_encoding.pop('''attention_mask''' ) if "input_ids" in text_encoding: a = text_encoding.pop('''input_ids''' ) else: a = None if text_encoding is not None: encoding_image_processor.update(lowerCAmelCase__ ) return encoding_image_processor def __lowerCAmelCase ( self : Optional[int] , *__UpperCAmelCase : int , **__UpperCAmelCase : Optional[int] ) ->Tuple: """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase__ , **lowerCAmelCase__ ) def __lowerCAmelCase ( self : int , *__UpperCAmelCase : Optional[int] , **__UpperCAmelCase : List[Any] ) ->List[Any]: """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase__ , **lowerCAmelCase__ ) @property def __lowerCAmelCase ( self : List[str] ) ->Union[str, 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 ) )
0
'''simple docstring''' from math import ceil, sqrt def lowerCAmelCase_ ( _lowerCamelCase: int = 1_00_00_00 ): __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: __SCREAMING_SNAKE_CASE : Union[str, Any] = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: __SCREAMING_SNAKE_CASE : Union[str, Any] = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f"{solution() = }")
112
0
from typing import Optional from urllib.parse import quote import huggingface_hub as hfh from packaging import version def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = None ): if version.parse(hfh.__version__ ).release < version.parse("0.11.0" ).release: # old versions of hfh don't url-encode the file path SCREAMING_SNAKE_CASE_: Union[str, Any] = quote(__UpperCAmelCase ) return hfh.hf_hub_url(__UpperCAmelCase , __UpperCAmelCase , repo_type="dataset" , revision=__UpperCAmelCase )
356
import re from typing import Callable, List, Optional, Union import tensorflow as tf try: from tensorflow.keras.optimizers.legacy import Adam except ImportError: from tensorflow.keras.optimizers import Adam class __lowercase ( tf.keras.optimizers.schedules.LearningRateSchedule ): """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : float , lowerCAmelCase__ : Callable , lowerCAmelCase__ : int , lowerCAmelCase__ : float = 1.0 , lowerCAmelCase__ : str = None , ): super().__init__() SCREAMING_SNAKE_CASE_: str = initial_learning_rate SCREAMING_SNAKE_CASE_: Dict = warmup_steps SCREAMING_SNAKE_CASE_: Any = power SCREAMING_SNAKE_CASE_: int = decay_schedule_fn SCREAMING_SNAKE_CASE_: Union[str, Any] = name def __call__( self : Optional[Any] , lowerCAmelCase__ : Any): with tf.name_scope(self.name or "WarmUp") as name: # Implements polynomial warmup. i.e., if global_step < warmup_steps, the # learning rate will be `global_step/num_warmup_steps * init_lr`. SCREAMING_SNAKE_CASE_: Any = tf.cast(lowerCAmelCase__ , tf.floataa) SCREAMING_SNAKE_CASE_: Optional[Any] = tf.cast(self.warmup_steps , tf.floataa) SCREAMING_SNAKE_CASE_: Optional[int] = global_step_float / warmup_steps_float SCREAMING_SNAKE_CASE_: Union[str, Any] = self.initial_learning_rate * tf.math.pow(lowerCAmelCase__ , self.power) return tf.cond( global_step_float < warmup_steps_float , lambda: warmup_learning_rate , lambda: self.decay_schedule_fn(step - self.warmup_steps) , name=lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : Tuple): return { "initial_learning_rate": self.initial_learning_rate, "decay_schedule_fn": self.decay_schedule_fn, "warmup_steps": self.warmup_steps, "power": self.power, "name": self.name, } def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 0.0 , _UpperCAmelCase = 0.9 , _UpperCAmelCase = 0.9_9_9 , _UpperCAmelCase = 1e-8 , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = 0.0 , _UpperCAmelCase = 1.0 , _UpperCAmelCase = None , ): SCREAMING_SNAKE_CASE_: Optional[int] = tf.keras.optimizers.schedules.PolynomialDecay( initial_learning_rate=_UpperCAmelCase , decay_steps=num_train_steps - num_warmup_steps , end_learning_rate=init_lr * min_lr_ratio , power=_UpperCAmelCase , ) if num_warmup_steps: SCREAMING_SNAKE_CASE_: Tuple = WarmUp( initial_learning_rate=_UpperCAmelCase , decay_schedule_fn=_UpperCAmelCase , warmup_steps=_UpperCAmelCase , ) if weight_decay_rate > 0.0: SCREAMING_SNAKE_CASE_: List[str] = AdamWeightDecay( learning_rate=_UpperCAmelCase , weight_decay_rate=_UpperCAmelCase , beta_a=_UpperCAmelCase , beta_a=_UpperCAmelCase , epsilon=_UpperCAmelCase , clipnorm=_UpperCAmelCase , global_clipnorm=_UpperCAmelCase , exclude_from_weight_decay=["LayerNorm", "layer_norm", "bias"] , include_in_weight_decay=_UpperCAmelCase , ) else: SCREAMING_SNAKE_CASE_: int = tf.keras.optimizers.Adam( learning_rate=_UpperCAmelCase , beta_a=_UpperCAmelCase , beta_a=_UpperCAmelCase , epsilon=_UpperCAmelCase , clipnorm=_UpperCAmelCase , global_clipnorm=_UpperCAmelCase , ) # We return the optimizer and the LR scheduler in order to better track the # evolution of the LR independently of the optimizer. return optimizer, lr_schedule class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : Union[float, tf.keras.optimizers.schedules.LearningRateSchedule] = 0.001 , lowerCAmelCase__ : float = 0.9 , lowerCAmelCase__ : float = 0.999 , lowerCAmelCase__ : float = 1E-7 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : float = 0.0 , lowerCAmelCase__ : Optional[List[str]] = None , lowerCAmelCase__ : Optional[List[str]] = None , lowerCAmelCase__ : str = "AdamWeightDecay" , **lowerCAmelCase__ : int , ): super().__init__(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , **lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = weight_decay_rate SCREAMING_SNAKE_CASE_: List[Any] = include_in_weight_decay SCREAMING_SNAKE_CASE_: List[Any] = exclude_from_weight_decay @classmethod def _SCREAMING_SNAKE_CASE ( cls : Dict , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: List[str] = {"WarmUp": WarmUp} return super(lowerCAmelCase__ , cls).from_config(lowerCAmelCase__ , custom_objects=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[int]): super(lowerCAmelCase__ , self)._prepare_local(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = tf.constant( self.weight_decay_rate , name="adam_weight_decay_rate") def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: str = self._do_use_weight_decay(var.name) if do_decay: return var.assign_sub( learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]["weight_decay_rate"] , use_locking=self._use_locking , ) return tf.no_op() def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[Any]=None , **lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = list(zip(*lowerCAmelCase__)) return super(lowerCAmelCase__ , self).apply_gradients(zip(lowerCAmelCase__ , lowerCAmelCase__) , name=lowerCAmelCase__ , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Tuple): if apply_state is None: return self._decayed_lr_t[var_dtype], {} SCREAMING_SNAKE_CASE_: Dict = apply_state or {} SCREAMING_SNAKE_CASE_: List[str] = apply_state.get((var_device, var_dtype)) if coefficients is None: SCREAMING_SNAKE_CASE_: Optional[int] = self._fallback_apply_state(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = coefficients return coefficients["lr_t"], {"apply_state": apply_state} def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple=None): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_lr(var.device , var.dtype.base_dtype , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = self._decay_weights_op(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) with tf.control_dependencies([decay]): return super(lowerCAmelCase__ , self)._resource_apply_dense(lowerCAmelCase__ , lowerCAmelCase__ , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Dict=None): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self._get_lr(var.device , var.dtype.base_dtype , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = self._decay_weights_op(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) with tf.control_dependencies([decay]): return super(lowerCAmelCase__ , self)._resource_apply_sparse(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: List[str] = super().get_config() config.update({"weight_decay_rate": self.weight_decay_rate}) return config def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): if self.weight_decay_rate == 0: return False if self._include_in_weight_decay: for r in self._include_in_weight_decay: if re.search(lowerCAmelCase__ , lowerCAmelCase__) is not None: return True if self._exclude_from_weight_decay: for r in self._exclude_from_weight_decay: if re.search(lowerCAmelCase__ , lowerCAmelCase__) is not None: return False return True class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Any = [] SCREAMING_SNAKE_CASE_: Any = None @property def _SCREAMING_SNAKE_CASE ( self : int): if self._accum_steps is None: SCREAMING_SNAKE_CASE_: Tuple = tf.Variable( tf.constant(0 , dtype=tf.intaa) , trainable=lowerCAmelCase__ , synchronization=tf.VariableSynchronization.ON_READ , aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA , ) return self._accum_steps.value() @property def _SCREAMING_SNAKE_CASE ( self : Tuple): if not self._gradients: raise ValueError("The accumulator should be called first to initialize the gradients") return [gradient.value() if gradient is not None else gradient for gradient in self._gradients] def __call__( self : str , lowerCAmelCase__ : Tuple): if not self._gradients: SCREAMING_SNAKE_CASE_: Optional[Any] = self.step # Create the step variable. self._gradients.extend( [ tf.Variable( tf.zeros_like(lowerCAmelCase__) , trainable=lowerCAmelCase__ , synchronization=tf.VariableSynchronization.ON_READ , aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA , ) if gradient is not None else gradient for gradient in gradients ]) if len(lowerCAmelCase__) != len(self._gradients): raise ValueError(F"Expected {len(self._gradients)} gradients, but got {len(lowerCAmelCase__)}") for accum_gradient, gradient in zip(self._gradients , lowerCAmelCase__): if accum_gradient is not None and gradient is not None: accum_gradient.assign_add(lowerCAmelCase__) self._accum_steps.assign_add(1) def _SCREAMING_SNAKE_CASE ( self : int): if not self._gradients: return self._accum_steps.assign(0) for gradient in self._gradients: if gradient is not None: gradient.assign(tf.zeros_like(lowerCAmelCase__))
127
0
def __a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) __UpperCAmelCase = str(bin(SCREAMING_SNAKE_CASE ) )[2:] # remove the leading "0b" __UpperCAmelCase = str(bin(SCREAMING_SNAKE_CASE ) )[2:] __UpperCAmelCase = max(len(SCREAMING_SNAKE_CASE ) , len(SCREAMING_SNAKE_CASE ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(SCREAMING_SNAKE_CASE ) , b_binary.zfill(SCREAMING_SNAKE_CASE ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
333
import math import sys def __a ( SCREAMING_SNAKE_CASE ) -> int: '''simple docstring''' if number != int(SCREAMING_SNAKE_CASE ): raise ValueError('''the value of input must be a natural number''' ) if number < 0: raise ValueError('''the value of input must not be a negative number''' ) if number == 0: return 1 __UpperCAmelCase = [-1] * (number + 1) __UpperCAmelCase = 0 for i in range(1 , number + 1 ): __UpperCAmelCase = sys.maxsize __UpperCAmelCase = int(math.sqrt(SCREAMING_SNAKE_CASE ) ) for j in range(1 , root + 1 ): __UpperCAmelCase = 1 + answers[i - (j**2)] __UpperCAmelCase = min(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __UpperCAmelCase = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
333
1
from typing import Tuple, Union from ...modeling_outputs import BackboneOutput from ...modeling_utils import PreTrainedModel from ...utils import is_timm_available, is_torch_available, requires_backends from ...utils.backbone_utils import BackboneMixin from .configuration_timm_backbone import TimmBackboneConfig if is_timm_available(): import timm if is_torch_available(): from torch import Tensor class _lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' a_ : List[Any] ="""pixel_values""" a_ : str =False a_ : str =TimmBackboneConfig def __init__( self : Any , UpperCamelCase : List[Any] , **UpperCamelCase : int ): '''simple docstring''' requires_backends(self , 'timm' ) super().__init__(__A ) _snake_case : Optional[Any] = config if config.backbone is None: raise ValueError('backbone is not set in the config. Please set it to a timm model name.' ) if config.backbone not in timm.list_models(): raise ValueError(f"""backbone {config.backbone} is not supported by timm.""" ) if hasattr(__A , 'out_features' ) and config.out_features is not None: raise ValueError('out_features is not supported by TimmBackbone. Please use out_indices instead.' ) _snake_case : Optional[Any] = getattr(__A , 'use_pretrained_backbone' , __A ) if pretrained is None: raise ValueError('use_pretrained_backbone is not set in the config. Please set it to True or False.' ) # We just take the final layer by default. This matches the default for the transformers models. _snake_case : Optional[int] = config.out_indices if getattr(__A , 'out_indices' , __A ) is not None else (-1,) _snake_case : Dict = timm.create_model( config.backbone , pretrained=__A , features_only=config.features_only , in_chans=config.num_channels , out_indices=__A , **__A , ) # These are used to control the output of the model when called. If output_hidden_states is True, then # return_layers is modified to include all layers. _snake_case : Union[str, Any] = self._backbone.return_layers _snake_case : int = {layer['module']: str(__A ) for i, layer in enumerate(self._backbone.feature_info.info )} super()._init_backbone(__A ) @classmethod def UpperCamelCase_ ( cls : Dict , UpperCamelCase : int , *UpperCamelCase : str , **UpperCamelCase : Union[str, Any] ): '''simple docstring''' requires_backends(cls , ['vision', 'timm'] ) from ...models.timm_backbone import TimmBackboneConfig _snake_case : int = kwargs.pop('config' , TimmBackboneConfig() ) _snake_case : List[Any] = kwargs.pop('use_timm_backbone' , __A ) if not use_timm: raise ValueError('use_timm_backbone must be True for timm backbones' ) _snake_case : List[str] = kwargs.pop('num_channels' , config.num_channels ) _snake_case : int = kwargs.pop('features_only' , config.features_only ) _snake_case : List[Any] = kwargs.pop('use_pretrained_backbone' , config.use_pretrained_backbone ) _snake_case : Optional[Any] = kwargs.pop('out_indices' , config.out_indices ) _snake_case : Any = TimmBackboneConfig( backbone=__A , num_channels=__A , features_only=__A , use_pretrained_backbone=__A , out_indices=__A , ) return super()._from_config(__A , **__A ) def UpperCamelCase_ ( self : Tuple , UpperCamelCase : Any ): '''simple docstring''' pass def UpperCamelCase_ ( self : List[Any] , UpperCamelCase : Union[str, Any] , UpperCamelCase : Optional[int]=None , UpperCamelCase : List[str]=None , UpperCamelCase : Tuple=None , **UpperCamelCase : Optional[Any] ): '''simple docstring''' _snake_case : List[str] = return_dict if return_dict is not None else self.config.use_return_dict _snake_case : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _snake_case : int = output_attentions if output_attentions is not None else self.config.output_attentions if output_attentions: raise ValueError('Cannot output attentions for timm backbones at the moment' ) if output_hidden_states: # We modify the return layers to include all the stages of the backbone _snake_case : str = self._all_layers _snake_case : Tuple = self._backbone(__A , **__A ) _snake_case : int = self._return_layers _snake_case : Optional[Any] = tuple(hidden_states[i] for i in self.out_indices ) else: _snake_case : Optional[int] = self._backbone(__A , **__A ) _snake_case : List[Any] = None _snake_case : int = tuple(__A ) _snake_case : List[Any] = tuple(__A ) if hidden_states is not None else None if not return_dict: _snake_case : Dict = (feature_maps,) if output_hidden_states: _snake_case : Optional[int] = output + (hidden_states,) return output return BackboneOutput(feature_maps=__A , hidden_states=__A , attentions=__A )
359
from math import ceil def lowerCamelCase_ ( lowerCAmelCase: Tuple , lowerCAmelCase: Union[str, Any] )-> str: _snake_case : Union[str, Any] = list(range(0 , lowerCAmelCase ) ) _snake_case : int = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check _snake_case : Any = [] for i in device_map_blocks: if device_map_blocks.count(lowerCAmelCase ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(lowerCAmelCase ) # Missing blocks _snake_case : Dict = [i for i in blocks if i not in device_map_blocks] _snake_case : Tuple = [i for i in device_map_blocks if i not in blocks] if len(lowerCAmelCase ) != 0: raise ValueError( 'Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.' ' These attention blocks were specified more than once: ' + str(lowerCAmelCase ) ) if len(lowerCAmelCase ) != 0: raise ValueError( 'There are attention blocks for this model that are not specified in the device_map. Add these attention ' 'blocks to a device on the device_map: ' + str(lowerCAmelCase ) ) if len(lowerCAmelCase ) != 0: raise ValueError( 'The device_map contains more attention blocks than this model has. Remove these from the device_map:' + str(lowerCAmelCase ) ) def lowerCamelCase_ ( lowerCAmelCase: Tuple , lowerCAmelCase: List[Any] )-> Optional[Any]: _snake_case : int = list(range(lowerCAmelCase ) ) _snake_case : Union[str, Any] = int(ceil(n_layers / len(lowerCAmelCase ) ) ) _snake_case : Optional[Any] = [layers[i : i + n_blocks] for i in range(0 , lowerCAmelCase , lowerCAmelCase )] return dict(zip(lowerCAmelCase , lowerCAmelCase ) )
260
0
from scipy.stats import pearsonr import datasets __UpperCamelCase : Optional[int] = '\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' __UpperCamelCase : Optional[Any] = '\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' __UpperCamelCase : str = '\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 lowercase__ ( datasets.Metric): def __A ( self : int ): '''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 __A ( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any]=False ): '''simple docstring''' if return_pvalue: SCREAMING_SNAKE_CASE : Union[str, Any] = pearsonr(UpperCamelCase__ , UpperCamelCase__ ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(UpperCamelCase__ , UpperCamelCase__ )[0] )}
182
'''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 lowercase : Optional[int] = logging.get_logger(__name__) class A ( __snake_case ): __magic_name__ = ['''input_features''', '''attention_mask'''] def __init__( self , SCREAMING_SNAKE_CASE=80 , SCREAMING_SNAKE_CASE=16000 , SCREAMING_SNAKE_CASE=80 , SCREAMING_SNAKE_CASE=0.0 , SCREAMING_SNAKE_CASE=True , SCREAMING_SNAKE_CASE=True , SCREAMING_SNAKE_CASE=True , **SCREAMING_SNAKE_CASE , ) -> int: """simple docstring""" super().__init__(feature_size=SCREAMING_SNAKE_CASE , sampling_rate=SCREAMING_SNAKE_CASE , padding_value=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) A : Optional[int] = num_mel_bins A : Tuple = do_ceptral_normalize A : Dict = normalize_means A : List[Any] = normalize_vars A : List[str] = True def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE , ) -> np.ndarray: """simple docstring""" A : List[Any] = waveform * (2**15) # Kaldi compliance: 16-bit signed integers A : Any = torch.from_numpy(SCREAMING_SNAKE_CASE ).unsqueeze(0 ) A : Any = ta_kaldi.fbank(SCREAMING_SNAKE_CASE , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def __lowerCAmelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = True , SCREAMING_SNAKE_CASE = True , SCREAMING_SNAKE_CASE = 0.0 , ) -> np.ndarray: """simple docstring""" if normalize_means: A : Dict = x[:input_length].mean(axis=0 ) A : Optional[Any] = np.subtract(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if normalize_vars: A : str = x[:input_length].std(axis=0 ) A : int = np.divide(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if input_length < x.shape[0]: A : List[str] = padding_value # make sure array is in float32 A : Tuple = x.astype(np.floataa ) return x def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = None ) -> List[np.ndarray]: """simple docstring""" A : List[Any] = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ] def __call__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = False , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = False , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = None , **SCREAMING_SNAKE_CASE , ) -> 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.''' ) A : List[Any] = isinstance(SCREAMING_SNAKE_CASE , 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}' ) A : Tuple = is_batched_numpy or ( isinstance(SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A : Union[str, Any] = [np.asarray(SCREAMING_SNAKE_CASE , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE , np.ndarray ): A : Union[str, Any] = np.asarray(SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(SCREAMING_SNAKE_CASE , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A : Optional[int] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A : Any = [raw_speech] # extract fbank features A : List[str] = [self._extract_fbank_features(SCREAMING_SNAKE_CASE ) for waveform in raw_speech] # convert into correct format for padding A : str = BatchFeature({'''input_features''': features} ) A : Union[str, Any] = self.pad( SCREAMING_SNAKE_CASE , padding=SCREAMING_SNAKE_CASE , max_length=SCREAMING_SNAKE_CASE , truncation=SCREAMING_SNAKE_CASE , pad_to_multiple_of=SCREAMING_SNAKE_CASE , return_attention_mask=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE , ) # make sure list is in array format A : List[str] = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , SCREAMING_SNAKE_CASE ): A : str = [np.asarray(SCREAMING_SNAKE_CASE , dtype=np.floataa ) for feature in input_features] A : Union[str, Any] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: A : Union[str, Any] = [np.asarray(SCREAMING_SNAKE_CASE , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: A : Dict = ( np.array(SCREAMING_SNAKE_CASE , dtype=np.intaa ) if self._get_padding_strategies(SCREAMING_SNAKE_CASE , max_length=SCREAMING_SNAKE_CASE ) is not PaddingStrategy.DO_NOT_PAD else None ) A : List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=SCREAMING_SNAKE_CASE ) if return_tensors is not None: A : int = padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE ) return padded_inputs
3
0
from collections.abc import Sequence def lowercase ( _SCREAMING_SNAKE_CASE : Sequence[int] | None = None ): '''simple docstring''' if nums is None or not nums: raise ValueError('''Input sequence should not be empty''' ) _UpperCAmelCase = nums[0] for i in range(1 , len(UpperCamelCase__ ) ): _UpperCAmelCase = nums[i] _UpperCAmelCase = max(UpperCamelCase__ , ans + num , UpperCamelCase__ ) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user __A : Optional[Any] = int(input("Enter number of elements : ").strip()) __A : Optional[int] = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n] print(max_subsequence_sum(array))
363
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : Optional[Any] = { "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"], "convert_funnel_original_tf_checkpoint_to_pytorch": [], "tokenization_funnel": ["FunnelTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[int] = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "FunnelBaseModel", "FunnelForMaskedLM", "FunnelForMultipleChoice", "FunnelForPreTraining", "FunnelForQuestionAnswering", "FunnelForSequenceClassification", "FunnelForTokenClassification", "FunnelModel", "FunnelPreTrainedModel", "load_tf_weights_in_funnel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Dict = [ "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFFunnelBaseModel", "TFFunnelForMaskedLM", "TFFunnelForMultipleChoice", "TFFunnelForPreTraining", "TFFunnelForQuestionAnswering", "TFFunnelForSequenceClassification", "TFFunnelForTokenClassification", "TFFunnelModel", "TFFunnelPreTrainedModel", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys __A : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
326
0
'''simple docstring''' from __future__ import annotations a_ : int = """#""" class snake_case : """simple docstring""" def __init__( self ): """simple docstring""" lowerCamelCase_ = {} def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = self._trie for char in text: if char not in trie: lowerCamelCase_ = {} lowerCamelCase_ = trie[char] lowerCamelCase_ = True def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = self._trie for char in prefix: if char in trie: lowerCamelCase_ = trie[char] else: return [] return self._elements(UpperCamelCase ) def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = [] for c, v in d.items(): lowerCamelCase_ = [" "] if c == END else [(c + s) for s in self._elements(UpperCamelCase )] result.extend(UpperCamelCase ) return tuple(UpperCamelCase ) a_ : Any = Trie() a_ : Dict = ("""depart""", """detergent""", """daring""", """dog""", """deer""", """deal""") for word in words: trie.insert_word(word) def __snake_case ( UpperCAmelCase_ : str ): lowerCamelCase_ = trie.find_word(UpperCAmelCase_ ) return tuple(string + word for word in suffixes ) def __snake_case ( ): print(autocomplete_using_trie("de" ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
55
'''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_ : int = logging.get_logger(__name__) class snake_case ( lowercase ): """simple docstring""" _lowerCamelCase = ["input_features", "attention_mask"] def __init__( self , UpperCamelCase=80 , UpperCamelCase=1_6000 , UpperCamelCase=80 , UpperCamelCase=0.0 , UpperCamelCase=True , UpperCamelCase=True , UpperCamelCase=True , **UpperCamelCase , ): """simple docstring""" super().__init__(feature_size=UpperCamelCase , sampling_rate=UpperCamelCase , padding_value=UpperCamelCase , **UpperCamelCase ) lowerCamelCase_ = num_mel_bins lowerCamelCase_ = do_ceptral_normalize lowerCamelCase_ = normalize_means lowerCamelCase_ = normalize_vars lowerCamelCase_ = True def snake_case ( self , UpperCamelCase , ): """simple docstring""" lowerCamelCase_ = waveform * (2**15) # Kaldi compliance: 16-bit signed integers lowerCamelCase_ = torch.from_numpy(UpperCamelCase ).unsqueeze(0 ) lowerCamelCase_ = ta_kaldi.fbank(UpperCamelCase , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def snake_case ( UpperCamelCase , UpperCamelCase , UpperCamelCase = True , UpperCamelCase = True , UpperCamelCase = 0.0 , ): """simple docstring""" # make sure we normalize float32 arrays if normalize_means: lowerCamelCase_ = x[:input_length].mean(axis=0 ) lowerCamelCase_ = np.subtract(UpperCamelCase , UpperCamelCase ) if normalize_vars: lowerCamelCase_ = x[:input_length].std(axis=0 ) lowerCamelCase_ = np.divide(UpperCamelCase , UpperCamelCase ) if input_length < x.shape[0]: lowerCamelCase_ = padding_value # make sure array is in float32 lowerCamelCase_ = x.astype(np.floataa ) return x def snake_case ( self , UpperCamelCase , UpperCamelCase = None ): """simple docstring""" lowerCamelCase_ = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(UpperCamelCase , UpperCamelCase , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(UpperCamelCase , UpperCamelCase ) ] def __call__( self , UpperCamelCase , UpperCamelCase = False , UpperCamelCase = None , UpperCamelCase = False , UpperCamelCase = None , UpperCamelCase = None , UpperCamelCase = None , UpperCamelCase = None , **UpperCamelCase , ): """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." ) lowerCamelCase_ = isinstance(UpperCamelCase , 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}''' ) lowerCamelCase_ = is_batched_numpy or ( isinstance(UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase_ = [np.asarray(UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(UpperCamelCase , np.ndarray ): lowerCamelCase_ = np.asarray(UpperCamelCase , dtype=np.floataa ) elif isinstance(UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase_ = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase_ = [raw_speech] # extract fbank features lowerCamelCase_ = [self._extract_fbank_features(UpperCamelCase ) for waveform in raw_speech] # convert into correct format for padding lowerCamelCase_ = BatchFeature({"input_features": features} ) lowerCamelCase_ = self.pad( UpperCamelCase , padding=UpperCamelCase , max_length=UpperCamelCase , truncation=UpperCamelCase , pad_to_multiple_of=UpperCamelCase , return_attention_mask=UpperCamelCase , **UpperCamelCase , ) # make sure list is in array format lowerCamelCase_ = padded_inputs.get("input_features" ) if isinstance(input_features[0] , UpperCamelCase ): lowerCamelCase_ = [np.asarray(UpperCamelCase , dtype=np.floataa ) for feature in input_features] lowerCamelCase_ = padded_inputs.get("attention_mask" ) if attention_mask is not None: lowerCamelCase_ = [np.asarray(UpperCamelCase , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: lowerCamelCase_ = ( np.array(UpperCamelCase , dtype=np.intaa ) if self._get_padding_strategies(UpperCamelCase , max_length=UpperCamelCase ) is not PaddingStrategy.DO_NOT_PAD else None ) lowerCamelCase_ = self.normalize( padded_inputs["input_features"] , attention_mask=UpperCamelCase ) if return_tensors is not None: lowerCamelCase_ = padded_inputs.convert_to_tensors(UpperCamelCase ) return padded_inputs
55
1
import unittest from transformers import BigBirdConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class __UpperCAmelCase ( unittest.TestCase ): def __init__( self : List[Any], __A : List[Any], __A : List[Any]=2, __A : Optional[Any]=5_6, __A : List[Any]=True, __A : List[Any]=True, __A : int=True, __A : str=True, __A : str=9_9, __A : Optional[int]=3_2, __A : List[Any]=2, __A : Tuple=2, __A : int=7, __A : List[Any]="gelu_new", __A : List[Any]=0.1, __A : Union[str, Any]=0.1, __A : str=5_1_2, __A : List[str]=1_6, __A : Union[str, Any]=2, __A : Dict=0.0_2, __A : Any=4, __A : List[str]="block_sparse", __A : int=True, __A : str=False, __A : Optional[Any]=2, __A : List[Any]=3, ): UpperCAmelCase : List[Any] = parent UpperCAmelCase : Union[str, Any] = batch_size UpperCAmelCase : Optional[int] = seq_length UpperCAmelCase : Optional[int] = is_training UpperCAmelCase : Optional[Any] = use_attention_mask UpperCAmelCase : Dict = use_token_type_ids UpperCAmelCase : Any = use_labels UpperCAmelCase : List[str] = vocab_size UpperCAmelCase : List[Any] = hidden_size UpperCAmelCase : Tuple = num_hidden_layers UpperCAmelCase : Any = num_attention_heads UpperCAmelCase : int = intermediate_size UpperCAmelCase : List[Any] = hidden_act UpperCAmelCase : Optional[Any] = hidden_dropout_prob UpperCAmelCase : int = attention_probs_dropout_prob UpperCAmelCase : Optional[Any] = max_position_embeddings UpperCAmelCase : Tuple = type_vocab_size UpperCAmelCase : Tuple = type_sequence_label_size UpperCAmelCase : Any = initializer_range UpperCAmelCase : List[Any] = num_choices UpperCAmelCase : str = rescale_embeddings UpperCAmelCase : Any = attention_type UpperCAmelCase : Optional[int] = use_bias UpperCAmelCase : Optional[Any] = block_size UpperCAmelCase : Any = num_random_blocks def __magic_name__ ( self : Any ): UpperCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) UpperCAmelCase : List[Any] = None if self.use_attention_mask: UpperCAmelCase : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Any = None if self.use_token_type_ids: UpperCAmelCase : str = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size ) UpperCAmelCase : str = BigBirdConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, is_decoder=__A, initializer_range=self.initializer_range, attention_type=self.attention_type, block_size=self.block_size, num_random_blocks=self.num_random_blocks, use_bias=self.use_bias, rescale_embeddings=self.rescale_embeddings, ) return config, input_ids, token_type_ids, attention_mask def __magic_name__ ( self : Tuple ): UpperCAmelCase : List[Any] = self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[str] = config_and_inputs UpperCAmelCase : Optional[int] = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask, } return config, inputs_dict @require_flax class __UpperCAmelCase ( lowerCamelCase__ , unittest.TestCase ): UpperCamelCase = ( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) UpperCamelCase = False UpperCamelCase = False def __magic_name__ ( self : int ): UpperCAmelCase : List[str] = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def __magic_name__ ( self : Any ): super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def __magic_name__ ( self : List[Any] ): super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def __magic_name__ ( self : Union[str, Any] ): super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def __magic_name__ ( self : Union[str, Any] ): super().test_hidden_states_output() @slow def __magic_name__ ( self : List[str] ): for model_class_name in self.all_model_classes: UpperCAmelCase : List[str] = model_class_name.from_pretrained('''google/bigbird-roberta-base''' ) self.assertIsNotNone(__A ) def __magic_name__ ( self : Any ): if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def __magic_name__ ( self : int ): UpperCAmelCase , UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): UpperCAmelCase : Any = self._prepare_for_class(__A, __A ) UpperCAmelCase : Optional[Any] = model_class(__A ) @jax.jit def model_jitted(__A : Optional[Any], __A : List[str]=None, **__A : List[Any] ): return model(input_ids=__A, attention_mask=__A, **__A ) with self.subTest('''JIT Enabled''' ): UpperCAmelCase : List[Any] = model_jitted(**__A ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): UpperCAmelCase : str = model_jitted(**__A ).to_tuple() self.assertEqual(len(__A ), len(__A ) ) for jitted_output, output in zip(__A, __A ): self.assertEqual(jitted_output.shape, output.shape ) def __magic_name__ ( self : Union[str, Any], __A : Union[str, Any], __A : Optional[int], __A : str, __A : Any=1E-5, __A : Tuple="outputs", __A : Optional[Any]=None ): # `bigbird_block_sparse_attention` in `FlaxBigBird` returns `attention_probs = None`, while in PyTorch version, # an effort was done to return `attention_probs` (yet to be verified). if name.startswith('''outputs.attentions''' ): return else: super().check_pt_flax_outputs(__A, __A, __A, __A, __A, __A )
99
from typing import Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING _lowerCamelCase : Dict = logging.get_logger(__name__) @add_end_docstrings(lowerCamelCase__ ) class __UpperCAmelCase ( lowerCamelCase__ ): def __init__( self : Optional[Any], *__A : Tuple, **__A : Tuple ): super().__init__(*__A, **__A ) self.check_model_type(__A ) def __magic_name__ ( self : Union[str, Any], __A : int=None, __A : Tuple=None, __A : Any=None, **__A : Optional[int] ): UpperCAmelCase , UpperCAmelCase : List[Any] = {}, {} if padding is not None: UpperCAmelCase : Optional[int] = padding if truncation is not None: UpperCAmelCase : Optional[int] = truncation if top_k is not None: UpperCAmelCase : Tuple = top_k return preprocess_params, {}, postprocess_params def __call__( self : Union[str, Any], __A : Union["Image.Image", str], __A : str = None, **__A : Optional[int] ): if isinstance(__A, (Image.Image, str) ) and isinstance(__A, __A ): UpperCAmelCase : int = {'''image''': image, '''question''': question} else: UpperCAmelCase : str = image UpperCAmelCase : Union[str, Any] = super().__call__(__A, **__A ) return results def __magic_name__ ( self : List[str], __A : Union[str, Any], __A : Tuple=False, __A : List[Any]=False ): UpperCAmelCase : int = load_image(inputs['''image'''] ) UpperCAmelCase : List[str] = self.tokenizer( inputs['''question'''], return_tensors=self.framework, padding=__A, truncation=__A ) UpperCAmelCase : Union[str, Any] = self.image_processor(images=__A, return_tensors=self.framework ) model_inputs.update(__A ) return model_inputs def __magic_name__ ( self : Optional[Any], __A : List[Any] ): UpperCAmelCase : Optional[int] = self.model(**__A ) return model_outputs def __magic_name__ ( self : Any, __A : List[str], __A : Union[str, Any]=5 ): if top_k > self.model.config.num_labels: UpperCAmelCase : Any = self.model.config.num_labels if self.framework == "pt": UpperCAmelCase : Any = model_outputs.logits.sigmoid()[0] UpperCAmelCase , UpperCAmelCase : Union[str, Any] = probs.topk(__A ) else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) UpperCAmelCase : str = scores.tolist() UpperCAmelCase : Tuple = ids.tolist() return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(__A, __A )]
99
1
"""simple docstring""" import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() A: Tuple = logging.get_logger(__name__) def _snake_case ( UpperCamelCase : List[Any] , UpperCamelCase : Optional[int] ): UpperCAmelCase : int = [] for i in range(encoder_config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F"encoder.deit.blocks.{i}.norm1.weight", F"encoder.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"encoder.deit.blocks.{i}.norm1.bias", F"encoder.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"encoder.deit.blocks.{i}.attn.proj.weight", F"encoder.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append( (F"encoder.deit.blocks.{i}.attn.proj.bias", F"encoder.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append( (F"encoder.deit.blocks.{i}.norm2.weight", F"encoder.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"encoder.deit.blocks.{i}.norm2.bias", F"encoder.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append( (F"encoder.deit.blocks.{i}.mlp.fc1.weight", F"encoder.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append( (F"encoder.deit.blocks.{i}.mlp.fc1.bias", F"encoder.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append( (F"encoder.deit.blocks.{i}.mlp.fc2.weight", F"encoder.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"encoder.deit.blocks.{i}.mlp.fc2.bias", F"encoder.encoder.layer.{i}.output.dense.bias") ) # cls token, position embeddings and patch embeddings of encoder rename_keys.extend( [ ("""encoder.deit.cls_token""", """encoder.embeddings.cls_token"""), ("""encoder.deit.pos_embed""", """encoder.embeddings.position_embeddings"""), ("""encoder.deit.patch_embed.proj.weight""", """encoder.embeddings.patch_embeddings.projection.weight"""), ("""encoder.deit.patch_embed.proj.bias""", """encoder.embeddings.patch_embeddings.projection.bias"""), ("""encoder.deit.norm.weight""", """encoder.layernorm.weight"""), ("""encoder.deit.norm.bias""", """encoder.layernorm.bias"""), ] ) return rename_keys def _snake_case ( UpperCamelCase : Dict , UpperCamelCase : Optional[Any] ): for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) UpperCAmelCase : Optional[int] = state_dict.pop(F"encoder.deit.blocks.{i}.attn.qkv.weight" ) UpperCAmelCase : str = in_proj_weight[ : encoder_config.hidden_size, : ] UpperCAmelCase : Dict = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] UpperCAmelCase : Optional[int] = in_proj_weight[ -encoder_config.hidden_size :, : ] def _snake_case ( UpperCamelCase : str , UpperCamelCase : Dict , UpperCamelCase : int ): UpperCAmelCase : Optional[int] = dct.pop(UpperCamelCase ) UpperCAmelCase : str = val def _snake_case ( UpperCamelCase : Any ): if "handwritten" in checkpoint_url: UpperCAmelCase : int = """https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg""" # industry # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-12.jpg" # have # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-10.jpg" # let # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg" # # url = "https://fki.tic.heia-fr.ch/static/img/a01-122.jpg" elif "printed" in checkpoint_url or "stage1" in checkpoint_url: UpperCAmelCase : Tuple = """https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg""" UpperCAmelCase : int = Image.open(requests.get(UpperCamelCase , stream=UpperCamelCase ).raw ).convert("""RGB""" ) return im @torch.no_grad() def _snake_case ( UpperCamelCase : Dict , UpperCamelCase : int ): UpperCAmelCase : Dict = ViTConfig(image_size=384 , qkv_bias=UpperCamelCase ) UpperCAmelCase : List[Any] = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: UpperCAmelCase : List[Any] = 768 elif "large" in checkpoint_url: # use ViT-large encoder UpperCAmelCase : Tuple = 1024 UpperCAmelCase : Any = 4096 UpperCAmelCase : List[str] = 24 UpperCAmelCase : str = 16 UpperCAmelCase : Optional[Any] = 1024 else: raise ValueError("""Should either find 'base' or 'large' in checkpoint URL""" ) # the large-printed + stage1 checkpoints uses sinusoidal position embeddings, no layernorm afterwards if "large-printed" in checkpoint_url or "stage1" in checkpoint_url: UpperCAmelCase : Optional[Any] = False UpperCAmelCase : int = """relu""" UpperCAmelCase : Dict = 1024 UpperCAmelCase : List[str] = True UpperCAmelCase : List[str] = False UpperCAmelCase : Any = False # load HuggingFace model UpperCAmelCase : Any = ViTModel(UpperCamelCase , add_pooling_layer=UpperCamelCase ) UpperCAmelCase : Any = TrOCRForCausalLM(UpperCamelCase ) UpperCAmelCase : Dict = VisionEncoderDecoderModel(encoder=UpperCamelCase , decoder=UpperCamelCase ) model.eval() # load state_dict of original model, rename some keys UpperCAmelCase : str = torch.hub.load_state_dict_from_url(UpperCamelCase , map_location="""cpu""" , check_hash=UpperCamelCase )["""model"""] UpperCAmelCase : str = create_rename_keys(UpperCamelCase , UpperCamelCase ) for src, dest in rename_keys: rename_key(UpperCamelCase , UpperCamelCase , UpperCamelCase ) read_in_q_k_v(UpperCamelCase , UpperCamelCase ) # remove parameters we don't need del state_dict["encoder.deit.head.weight"] del state_dict["encoder.deit.head.bias"] del state_dict["decoder.version"] # add prefix to decoder keys for key, val in state_dict.copy().items(): UpperCAmelCase : Any = state_dict.pop(UpperCamelCase ) if key.startswith("""decoder""" ) and "output_projection" not in key: UpperCAmelCase : Union[str, Any] = val else: UpperCAmelCase : List[str] = val # load state dict model.load_state_dict(UpperCamelCase ) # Check outputs on an image UpperCAmelCase : Optional[Any] = ViTImageProcessor(size=encoder_config.image_size ) UpperCAmelCase : List[str] = RobertaTokenizer.from_pretrained("""roberta-large""" ) UpperCAmelCase : Union[str, Any] = TrOCRProcessor(UpperCamelCase , UpperCamelCase ) UpperCAmelCase : Tuple = processor(images=prepare_img(UpperCamelCase ) , return_tensors="""pt""" ).pixel_values # verify logits UpperCAmelCase : Dict = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) UpperCAmelCase : Dict = model(pixel_values=UpperCamelCase , decoder_input_ids=UpperCamelCase ) UpperCAmelCase : List[Any] = outputs.logits UpperCAmelCase : Optional[int] = torch.Size([1, 1, 50265] ) if "trocr-base-handwritten" in checkpoint_url: UpperCAmelCase : Optional[Any] = torch.tensor( [-1.4502, -4.6683, -0.5347, -2.9291, 9.1435, -3.0571, 8.9764, 1.7560, 8.7358, -1.5311] ) elif "trocr-large-handwritten" in checkpoint_url: UpperCAmelCase : Optional[int] = torch.tensor( [-2.6437, -1.3129, -2.2596, -5.3455, 6.3539, 1.7604, 5.4991, 1.4702, 5.6113, 2.0170] ) elif "trocr-base-printed" in checkpoint_url: UpperCAmelCase : Tuple = torch.tensor( [-5.6816, -5.8388, 1.1398, -6.9034, 6.8505, -2.4393, 1.2284, -1.0232, -1.9661, -3.9210] ) elif "trocr-large-printed" in checkpoint_url: UpperCAmelCase : Tuple = torch.tensor( [-6.0162, -7.0959, 4.4155, -5.1063, 7.0468, -3.1631, 2.6466, -0.3081, -0.8106, -1.7535] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :10] , UpperCamelCase , atol=1e-3 ), "First elements of logits not as expected" Path(UpperCamelCase ).mkdir(exist_ok=UpperCamelCase ) print(F"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(UpperCamelCase ) print(F"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(UpperCamelCase ) if __name__ == "__main__": A: List[str] = argparse.ArgumentParser() parser.add_argument( "--checkpoint_url", default="https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt", type=str, help="URL to the original PyTorch checkpoint (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the folder to output PyTorch model." ) A: List[str] = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
109
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: _UpperCAmelCase : int = None _UpperCAmelCase : Dict = logging.get_logger(__name__) _UpperCAmelCase : Optional[int] = {"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""} _UpperCAmelCase : List[Any] = { """vocab_file""": { """facebook/nllb-200-distilled-600M""": ( """https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model""" ), }, """tokenizer_file""": { """facebook/nllb-200-distilled-600M""": ( """https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json""" ), }, } _UpperCAmelCase : List[str] = { """facebook/nllb-large-en-ro""": 10_24, """facebook/nllb-200-distilled-600M""": 10_24, } # fmt: off _UpperCAmelCase : Optional[int] = ["""ace_Arab""", """ace_Latn""", """acm_Arab""", """acq_Arab""", """aeb_Arab""", """afr_Latn""", """ajp_Arab""", """aka_Latn""", """amh_Ethi""", """apc_Arab""", """arb_Arab""", """ars_Arab""", """ary_Arab""", """arz_Arab""", """asm_Beng""", """ast_Latn""", """awa_Deva""", """ayr_Latn""", """azb_Arab""", """azj_Latn""", """bak_Cyrl""", """bam_Latn""", """ban_Latn""", """bel_Cyrl""", """bem_Latn""", """ben_Beng""", """bho_Deva""", """bjn_Arab""", """bjn_Latn""", """bod_Tibt""", """bos_Latn""", """bug_Latn""", """bul_Cyrl""", """cat_Latn""", """ceb_Latn""", """ces_Latn""", """cjk_Latn""", """ckb_Arab""", """crh_Latn""", """cym_Latn""", """dan_Latn""", """deu_Latn""", """dik_Latn""", """dyu_Latn""", """dzo_Tibt""", """ell_Grek""", """eng_Latn""", """epo_Latn""", """est_Latn""", """eus_Latn""", """ewe_Latn""", """fao_Latn""", """pes_Arab""", """fij_Latn""", """fin_Latn""", """fon_Latn""", """fra_Latn""", """fur_Latn""", """fuv_Latn""", """gla_Latn""", """gle_Latn""", """glg_Latn""", """grn_Latn""", """guj_Gujr""", """hat_Latn""", """hau_Latn""", """heb_Hebr""", """hin_Deva""", """hne_Deva""", """hrv_Latn""", """hun_Latn""", """hye_Armn""", """ibo_Latn""", """ilo_Latn""", """ind_Latn""", """isl_Latn""", """ita_Latn""", """jav_Latn""", """jpn_Jpan""", """kab_Latn""", """kac_Latn""", """kam_Latn""", """kan_Knda""", """kas_Arab""", """kas_Deva""", """kat_Geor""", """knc_Arab""", """knc_Latn""", """kaz_Cyrl""", """kbp_Latn""", """kea_Latn""", """khm_Khmr""", """kik_Latn""", """kin_Latn""", """kir_Cyrl""", """kmb_Latn""", """kon_Latn""", """kor_Hang""", """kmr_Latn""", """lao_Laoo""", """lvs_Latn""", """lij_Latn""", """lim_Latn""", """lin_Latn""", """lit_Latn""", """lmo_Latn""", """ltg_Latn""", """ltz_Latn""", """lua_Latn""", """lug_Latn""", """luo_Latn""", """lus_Latn""", """mag_Deva""", """mai_Deva""", """mal_Mlym""", """mar_Deva""", """min_Latn""", """mkd_Cyrl""", """plt_Latn""", """mlt_Latn""", """mni_Beng""", """khk_Cyrl""", """mos_Latn""", """mri_Latn""", """zsm_Latn""", """mya_Mymr""", """nld_Latn""", """nno_Latn""", """nob_Latn""", """npi_Deva""", """nso_Latn""", """nus_Latn""", """nya_Latn""", """oci_Latn""", """gaz_Latn""", """ory_Orya""", """pag_Latn""", """pan_Guru""", """pap_Latn""", """pol_Latn""", """por_Latn""", """prs_Arab""", """pbt_Arab""", """quy_Latn""", """ron_Latn""", """run_Latn""", """rus_Cyrl""", """sag_Latn""", """san_Deva""", """sat_Beng""", """scn_Latn""", """shn_Mymr""", """sin_Sinh""", """slk_Latn""", """slv_Latn""", """smo_Latn""", """sna_Latn""", """snd_Arab""", """som_Latn""", """sot_Latn""", """spa_Latn""", """als_Latn""", """srd_Latn""", """srp_Cyrl""", """ssw_Latn""", """sun_Latn""", """swe_Latn""", """swh_Latn""", """szl_Latn""", """tam_Taml""", """tat_Cyrl""", """tel_Telu""", """tgk_Cyrl""", """tgl_Latn""", """tha_Thai""", """tir_Ethi""", """taq_Latn""", """taq_Tfng""", """tpi_Latn""", """tsn_Latn""", """tso_Latn""", """tuk_Latn""", """tum_Latn""", """tur_Latn""", """twi_Latn""", """tzm_Tfng""", """uig_Arab""", """ukr_Cyrl""", """umb_Latn""", """urd_Arab""", """uzn_Latn""", """vec_Latn""", """vie_Latn""", """war_Latn""", """wol_Latn""", """xho_Latn""", """ydd_Hebr""", """yor_Latn""", """yue_Hant""", """zho_Hans""", """zho_Hant""", """zul_Latn"""] class lowerCAmelCase ( __UpperCamelCase ): UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = ["""input_ids""", """attention_mask"""] UpperCAmelCase__ = NllbTokenizer UpperCAmelCase__ = [] UpperCAmelCase__ = [] def __init__( self : Tuple , UpperCAmelCase : int=None , UpperCAmelCase : Any=None , UpperCAmelCase : str="<s>" , UpperCAmelCase : Optional[Any]="</s>" , UpperCAmelCase : str="</s>" , UpperCAmelCase : Tuple="<s>" , UpperCAmelCase : Optional[Any]="<unk>" , UpperCAmelCase : List[str]="<pad>" , UpperCAmelCase : Union[str, Any]="<mask>" , UpperCAmelCase : Tuple=None , UpperCAmelCase : int=None , UpperCAmelCase : Dict=None , UpperCAmelCase : Any=False , **UpperCAmelCase : Optional[int] , ) -> Tuple: # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase__ : List[Any] = AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token lowerCamelCase__ : Union[str, Any] = legacy_behaviour super().__init__( vocab_file=UpperCAmelCase , tokenizer_file=UpperCAmelCase , bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , unk_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , src_lang=UpperCAmelCase , tgt_lang=UpperCAmelCase , additional_special_tokens=UpperCAmelCase , legacy_behaviour=UpperCAmelCase , **UpperCAmelCase , ) lowerCamelCase__ : List[Any] = vocab_file lowerCamelCase__ : Dict = False if not self.vocab_file else True lowerCamelCase__ : Optional[Any] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) lowerCamelCase__ : str = { lang_code: self.convert_tokens_to_ids(UpperCAmelCase ) for lang_code in FAIRSEQ_LANGUAGE_CODES } lowerCamelCase__ : int = src_lang if src_lang is not None else 'eng_Latn' lowerCamelCase__ : List[Any] = self.convert_tokens_to_ids(self._src_lang ) lowerCamelCase__ : str = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def A_ ( self : int ) -> str: return self._src_lang @src_lang.setter def A_ ( self : List[Any] , UpperCAmelCase : str ) -> None: lowerCamelCase__ : Any = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def A_ ( self : Optional[Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def A_ ( self : Optional[Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ) -> List[int]: lowerCamelCase__ : Dict = [self.sep_token_id] lowerCamelCase__ : 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 + sep + token_ids_a + sep ) * [0] def A_ ( self : int , UpperCAmelCase : int , UpperCAmelCase : str , UpperCAmelCase : Optional[str] , UpperCAmelCase : Optional[str] , **UpperCAmelCase : List[str] ) -> Dict: if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCamelCase__ : Optional[int] = src_lang lowerCamelCase__ : Optional[int] = self(UpperCAmelCase , add_special_tokens=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = self.convert_tokens_to_ids(UpperCAmelCase ) lowerCamelCase__ : Union[str, Any] = tgt_lang_id return inputs def A_ ( self : Dict , UpperCAmelCase : List[str] , UpperCAmelCase : str = "eng_Latn" , UpperCAmelCase : Optional[List[str]] = None , UpperCAmelCase : str = "fra_Latn" , **UpperCAmelCase : Dict , ) -> BatchEncoding: lowerCamelCase__ : Any = src_lang lowerCamelCase__ : int = tgt_lang return super().prepare_seqaseq_batch(UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def A_ ( self : Union[str, Any] ) -> Optional[int]: return self.set_src_lang_special_tokens(self.src_lang ) def A_ ( self : Any ) -> Union[str, Any]: return self.set_tgt_lang_special_tokens(self.tgt_lang ) def A_ ( self : str , UpperCAmelCase : Optional[Any] ) -> None: lowerCamelCase__ : int = self.convert_tokens_to_ids(UpperCAmelCase ) if self.legacy_behaviour: lowerCamelCase__ : int = [] lowerCamelCase__ : str = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ : int = [self.cur_lang_code] lowerCamelCase__ : Tuple = [self.eos_token_id] lowerCamelCase__ : Any = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ : Optional[Any] = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ : str = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def A_ ( self : int , UpperCAmelCase : str ) -> None: lowerCamelCase__ : Union[str, Any] = self.convert_tokens_to_ids(UpperCAmelCase ) if self.legacy_behaviour: lowerCamelCase__ : Dict = [] lowerCamelCase__ : Union[str, Any] = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ : Any = [self.cur_lang_code] lowerCamelCase__ : Optional[Any] = [self.eos_token_id] lowerCamelCase__ : Union[str, Any] = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ : List[Any] = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ : Optional[int] = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def A_ ( self : Union[str, Any] , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory.""" ) return lowerCamelCase__ : int = os.path.join( UpperCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ): copyfile(self.vocab_file , UpperCAmelCase ) return (out_vocab_file,)
50
0
'''simple docstring''' import os import pytest from transformers.dynamic_module_utils import get_imports _SCREAMING_SNAKE_CASE = ''' import os ''' _SCREAMING_SNAKE_CASE = ''' def foo(): import os return False ''' _SCREAMING_SNAKE_CASE = ''' def foo(): def bar(): if True: import os return False return bar() ''' _SCREAMING_SNAKE_CASE = ''' import os try: import bar except ImportError: raise ValueError() ''' _SCREAMING_SNAKE_CASE = ''' import os def foo(): try: import bar except ImportError: raise ValueError() ''' _SCREAMING_SNAKE_CASE = ''' import os try: import bar except (ImportError, AttributeError): raise ValueError() ''' _SCREAMING_SNAKE_CASE = ''' import os try: import bar except ImportError as e: raise ValueError() ''' _SCREAMING_SNAKE_CASE = ''' import os try: import bar except: raise ValueError() ''' _SCREAMING_SNAKE_CASE = ''' import os try: import bar import baz except ImportError: raise ValueError() ''' _SCREAMING_SNAKE_CASE = ''' import os try: import bar import baz except ImportError: x = 1 raise ValueError() ''' _SCREAMING_SNAKE_CASE = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('''case''' , lowerCamelCase_ ) def _lowerCAmelCase ( lowerCamelCase_ : Optional[int] , lowerCamelCase_ : Tuple ): __lowercase = os.path.join(lowerCamelCase_ , '''test_file.py''' ) with open(lowerCamelCase_ , '''w''' ) as _tmp_file: _tmp_file.write(lowerCamelCase_ ) __lowercase = get_imports(lowerCamelCase_ ) assert parsed_imports == ["os"]
217
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..bit import BitConfig _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE = { '''Intel/dpt-large''': '''https://huggingface.co/Intel/dpt-large/resolve/main/config.json''', # See all DPT models at https://huggingface.co/models?filter=dpt } class __lowercase ( lowerCAmelCase__ ): '''simple docstring''' a : Union[str, Any] = "dpt" def __init__(self ,_lowerCamelCase=768 ,_lowerCamelCase=12 ,_lowerCamelCase=12 ,_lowerCamelCase=3072 ,_lowerCamelCase="gelu" ,_lowerCamelCase=0.0 ,_lowerCamelCase=0.0 ,_lowerCamelCase=0.0_2 ,_lowerCamelCase=1E-1_2 ,_lowerCamelCase=384 ,_lowerCamelCase=16 ,_lowerCamelCase=3 ,_lowerCamelCase=False ,_lowerCamelCase=True ,_lowerCamelCase=[2, 5, 8, 11] ,_lowerCamelCase="project" ,_lowerCamelCase=[4, 2, 1, 0.5] ,_lowerCamelCase=[96, 192, 384, 768] ,_lowerCamelCase=256 ,_lowerCamelCase=-1 ,_lowerCamelCase=False ,_lowerCamelCase=True ,_lowerCamelCase=0.4 ,_lowerCamelCase=255 ,_lowerCamelCase=0.1 ,_lowerCamelCase=[1, 1024, 24, 24] ,_lowerCamelCase=[0, 1] ,_lowerCamelCase=None ,**_lowerCamelCase ,) -> Tuple: '''simple docstring''' super().__init__(**_lowerCamelCase ) __lowercase = hidden_size __lowercase = is_hybrid if self.is_hybrid: if backbone_config is None: logger.info('''Initializing the config with a `BiT` backbone.''' ) __lowercase = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, } __lowercase = BitConfig(**_lowerCamelCase ) elif isinstance(_lowerCamelCase ,_lowerCamelCase ): logger.info('''Initializing the config with a `BiT` backbone.''' ) __lowercase = BitConfig(**_lowerCamelCase ) elif isinstance(_lowerCamelCase ,_lowerCamelCase ): __lowercase = backbone_config else: raise ValueError( f"backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}." ) __lowercase = backbone_featmap_shape __lowercase = neck_ignore_stages if readout_type != "project": raise ValueError('''Readout type must be \'project\' when using `DPT-hybrid` mode.''' ) else: __lowercase = None __lowercase = None __lowercase = [] __lowercase = num_hidden_layers __lowercase = num_attention_heads __lowercase = intermediate_size __lowercase = hidden_act __lowercase = hidden_dropout_prob __lowercase = attention_probs_dropout_prob __lowercase = initializer_range __lowercase = layer_norm_eps __lowercase = image_size __lowercase = patch_size __lowercase = num_channels __lowercase = qkv_bias __lowercase = backbone_out_indices if readout_type not in ["ignore", "add", "project"]: raise ValueError('''Readout_type must be one of [\'ignore\', \'add\', \'project\']''' ) __lowercase = readout_type __lowercase = reassemble_factors __lowercase = neck_hidden_sizes __lowercase = fusion_hidden_size __lowercase = head_in_index __lowercase = use_batch_norm_in_fusion_residual # auxiliary head attributes (semantic segmentation) __lowercase = use_auxiliary_head __lowercase = auxiliary_loss_weight __lowercase = semantic_loss_ignore_index __lowercase = semantic_classifier_dropout def _UpperCAmelCase (self ) -> Union[str, Any]: '''simple docstring''' __lowercase = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: __lowercase = self.backbone_config.to_dict() __lowercase = self.__class__.model_type return output
217
1
import unittest from pathlib import Path from shutil import copyfile from transformers import SPIECE_UNDERLINE, is_sentencepiece_available from transformers.models.speech_to_text import SpeechaTextTokenizer from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _A = get_tests_dir('''fixtures/test_sentencepiece.model''') if is_sentencepiece_available(): import sentencepiece as sp _A = 5 _A = 10 @require_sentencepiece @require_tokenizers class lowercase_ ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): A__ : List[Any] = SpeechaTextTokenizer A__ : str = False A__ : Union[str, Any] = True def lowerCamelCase_ ( self ): """simple docstring""" super().setUp() UpperCamelCase_ = sp.SentencePieceProcessor() spm_model.Load(__UpperCamelCase ) UpperCamelCase_ = ["""<s>""", """<pad>""", """</s>""", """<unk>"""] vocab += [spm_model.IdToPiece(id_ ) for id_ in range(len(__UpperCamelCase ) )] UpperCamelCase_ = dict(zip(__UpperCamelCase , range(len(__UpperCamelCase ) ) ) ) UpperCamelCase_ = Path(self.tmpdirname ) save_json(__UpperCamelCase , save_dir / VOCAB_FILES_NAMES["""vocab_file"""] ) if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists(): copyfile(__UpperCamelCase , save_dir / VOCAB_FILES_NAMES["""spm_file"""] ) UpperCamelCase_ = SpeechaTextTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = """<pad>""" UpperCamelCase_ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCamelCase ) , __UpperCamelCase ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<s>""" ) self.assertEqual(vocab_keys[1] , """<pad>""" ) self.assertEqual(vocab_keys[-1] , """j""" ) self.assertEqual(len(__UpperCamelCase ) , 1_0_0_1 ) def lowerCamelCase_ ( self ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_0_0_1 ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = SpeechaTextTokenizer.from_pretrained(self.tmpdirname ) UpperCamelCase_ = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(__UpperCamelCase , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCamelCase ) , [2_8_9, 5_0, 1_4, 1_7_4, 3_8_6] , ) UpperCamelCase_ = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( __UpperCamelCase , [SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """."""] , ) UpperCamelCase_ = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) self.assertListEqual(__UpperCamelCase , [1_2, 2_5, 8_8, 5_9, 2_8, 2_3, 1_1, 4, 6_0_6, 3_5_1, 3_5_1, 3_5_1, 7, 1_6, 7_0, 5_0, 7_6, 8_4, 1_0, 4, 8] ) UpperCamelCase_ = tokenizer.convert_ids_to_tokens(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """."""] , ) @slow def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = {"""input_ids""": [[3_7_9_1, 7_9_7, 3_1, 1_1, 6_4, 7_9_7, 3_1, 2_4_2_9, 4_3_3, 1_2, 1_1_7_6, 1_2, 2_0, 7_8_6, 9_1_5, 1_4_2, 2_4_1_3, 2_4_0, 3_7, 3_2_3_8, 7_9_7, 3_1, 1_1, 3_5, 9_3, 9_1_5, 1_4_2, 2_4_1_3, 2_4_0, 3_7, 5_5_4_0, 5_6_7, 1_2_7_6, 9_3, 3_7, 6_1_0, 4_0, 6_2, 4_5_5, 6_5_7, 1_0_4_2, 1_2_3, 7_8_0, 1_7_7, 3_7, 3_0_9, 2_4_1, 1_2_9_8, 5_1_4, 2_0, 2_9_2, 2_7_3_7, 1_1_4, 2_4_6_9, 2_4_1, 8_5, 6_4, 3_0_2, 5_4_8, 5_2_8, 4_2_3, 4, 5_0_9, 4_0_6, 4_2_3, 3_7, 6_0_1, 4, 7_7_7, 3_0_2, 5_4_8, 5_2_8, 4_2_3, 2_8_4, 4, 3_3_8_8, 5_1_1, 4_5_9, 4, 3_5_5_5, 4_0, 3_2_1, 3_0_2, 7_0_5, 4, 3_3_8_8, 5_1_1, 5_8_3, 3_2_6, 5, 5, 5, 6_2, 3_3_1_0, 5_6_0, 1_7_7, 2_6_8_0, 2_1_7, 1_5_0_8, 3_2, 3_1, 8_5_3, 4_1_8, 6_4, 5_8_3, 5_1_1, 1_6_0_5, 6_2, 3_5, 9_3, 5_6_0, 1_7_7, 2_6_8_0, 2_1_7, 1_5_0_8, 1_5_2_1, 6_4, 5_8_3, 5_1_1, 5_1_9, 6_2, 2_0, 1_5_1_5, 7_6_4, 2_0, 1_4_9, 2_6_1, 5_6_2_5, 7_9_7_2, 2_0, 5_5_4_0, 5_6_7, 1_2_7_6, 9_3, 3_9_2_5, 1_6_7_5, 1_1, 1_5, 8_0_2, 7_9_7_2, 5_7_6, 2_1_7, 1_5_0_8, 1_1, 3_5, 9_3, 1_2_5_3, 2_4_4_1, 1_5, 2_8_9, 6_5_2, 3_1, 4_1_6, 3_2_1, 3_8_4_2, 1_1_5, 4_0, 9_1_1, 8, 4_7_6, 6_1_9, 4, 3_8_0, 1_4_2, 4_2_3, 3_3_5, 2_4_0, 3_5, 9_3, 2_6_4, 8, 1_1, 3_3_5, 5_6_9, 4_2_0, 1_6_3, 5, 2], [2_6_0, 5_4_8, 5_2_8, 4_2_3, 2_0, 4_5_1, 2_0, 2_6_8_1, 1_1_5_3, 3_4_3_4, 2_0, 5_5_4_0, 3_7, 5_6_7, 1_2_6, 1_2_5_3, 2_4_4_1, 3_3_7_6, 4_4_9, 2_1_0, 4_3_1, 1_5_6_3, 1_7_7, 7_6_7, 5_5_4_0, 1_1, 1_2_0_3, 4_7_2, 1_1, 2_9_5_3, 6_8_5, 2_8_5, 3_6_4, 7_0_6, 1_1_5_3, 2_0, 6_7_9_9, 2_0, 2_8_6_9, 2_0, 4_4_6_4, 1_2_6, 4_0, 2_4_2_9, 2_0, 1_0_4_0, 8_6_6, 2_6_6_4, 4_1_8, 2_0, 3_1_8, 2_0, 1_7_2_6, 1_8_6, 2_0, 2_6_5, 5_2_2, 3_5, 9_3, 2_1_9_1, 4_6_3_4, 2_0, 1_0_4_0, 1_2, 6_7_9_9, 1_5, 2_2_8, 2_3_5_6, 1_4_2, 3_1, 1_1, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2_5_7_5, 2_6_6_6, 6_8_4, 1_5_8_2, 1_1_7_6, 1_2, 6_2_7, 1_4_9, 6_1_9, 2_0, 4_9_0_2, 5_6_3, 1_1, 2_0, 1_4_9, 2_6_1, 3_4_2_0, 2_3_5_6, 1_7_4, 1_4_2, 4_7_1_4, 1_3_1, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCamelCase , model_name="""facebook/s2t-small-mustc-en-de-st""" , revision="""a14f04cf0776c02f62a8cb800cf7909e15ea23ad""" , ) @require_sentencepiece class lowercase_ ( unittest.TestCase ): A__ : int = """valhalla/s2t_mustc_multilinguial_medium""" A__ : Tuple = """C'est trop cool""" A__ : Optional[Any] = """Esto es genial""" @classmethod def lowerCamelCase_ ( cls ): """simple docstring""" UpperCamelCase_ = SpeechaTextTokenizer.from_pretrained(cls.checkpoint_name ) return cls def lowerCamelCase_ ( self ): """simple docstring""" self.assertEqual(self.tokenizer.lang_code_to_id["""pt"""] , 4 ) self.assertEqual(self.tokenizer.lang_code_to_id["""ru"""] , 6 ) self.assertEqual(self.tokenizer.lang_code_to_id["""it"""] , 9 ) self.assertEqual(self.tokenizer.lang_code_to_id["""de"""] , 1_1 ) def lowerCamelCase_ ( self ): """simple docstring""" self.assertEqual(self.tokenizer.vocab_size , 1_0_0_0_0 ) def lowerCamelCase_ ( self ): """simple docstring""" self.assertIn(__UpperCamelCase , self.tokenizer.all_special_ids ) UpperCamelCase_ = [ES_CODE, 4, 1_6_0_1, 4_7, 7_6_4_7, 2] UpperCamelCase_ = self.tokenizer.decode(__UpperCamelCase , skip_special_tokens=__UpperCamelCase ) UpperCamelCase_ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=__UpperCamelCase ) self.assertEqual(__UpperCamelCase , __UpperCamelCase ) self.assertNotIn(self.tokenizer.eos_token , __UpperCamelCase ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = """fr""" UpperCamelCase_ = self.tokenizer(self.french_text ).input_ids self.assertEqual(encoded[0] , __UpperCamelCase ) self.assertEqual(encoded[-1] , self.tokenizer.eos_token_id ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = """fr""" self.assertListEqual(self.tokenizer.prefix_tokens , [FR_CODE] ) UpperCamelCase_ = """es""" self.assertListEqual(self.tokenizer.prefix_tokens , [ES_CODE] )
122
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _A = { '''configuration_megatron_bert''': ['''MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegatronBertConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ '''MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MegatronBertForCausalLM''', '''MegatronBertForMaskedLM''', '''MegatronBertForMultipleChoice''', '''MegatronBertForNextSentencePrediction''', '''MegatronBertForPreTraining''', '''MegatronBertForQuestionAnswering''', '''MegatronBertForSequenceClassification''', '''MegatronBertForTokenClassification''', '''MegatronBertModel''', '''MegatronBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys _A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
122
1
import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowerCAmelCase_ = logging.getLogger(__name__) def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase ) -> int: """simple docstring""" if os.path.exists(_UpperCamelCase ): if os.path.exists(os.path.join(_UpperCamelCase , '''config.json''' ) ) and os.path.isfile( os.path.join(_UpperCamelCase , '''config.json''' ) ): os.remove(os.path.join(_UpperCamelCase , '''config.json''' ) ) if os.path.exists(os.path.join(_UpperCamelCase , '''pytorch_model.bin''' ) ) and os.path.isfile( os.path.join(_UpperCamelCase , '''pytorch_model.bin''' ) ): os.remove(os.path.join(_UpperCamelCase , '''pytorch_model.bin''' ) ) else: os.makedirs(_UpperCamelCase ) model.save_pretrained(_UpperCamelCase ) def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase=False ) -> Optional[int]: """simple docstring""" snake_case_ : List[Any] = 2 if unlogit: snake_case_ : Any = torch.pow(_UpperCamelCase , _UpperCamelCase ) snake_case_ : Optional[Any] = p * torch.log(_UpperCamelCase ) snake_case_ : Dict = 0 return -plogp.sum(dim=-1 ) def lowerCamelCase_ ( _UpperCamelCase ) -> int: """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(f'''{x + 1}''' for x in range(len(_UpperCamelCase ) ) ) ) for row in range(len(_UpperCamelCase ) ): if tensor.dtype != torch.long: logger.info(f'''layer {row + 1}:\t''' + '''\t'''.join(f'''{x:.5f}''' for x in tensor[row].cpu().data ) ) else: logger.info(f'''layer {row + 1}:\t''' + '''\t'''.join(f'''{x:d}''' for x in tensor[row].cpu().data ) ) def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase=True , _UpperCamelCase=True , _UpperCamelCase=None , _UpperCamelCase=False ) -> Union[str, Any]: """simple docstring""" snake_case_ , snake_case_ : int = model.config.num_hidden_layers, model.config.num_attention_heads snake_case_ : int = torch.zeros(_UpperCamelCase , _UpperCamelCase ).to(args.device ) snake_case_ : Optional[int] = torch.zeros(_UpperCamelCase , _UpperCamelCase ).to(args.device ) if head_mask is None: snake_case_ : Tuple = torch.ones(_UpperCamelCase , _UpperCamelCase ).to(args.device ) head_mask.requires_grad_(requires_grad=_UpperCamelCase ) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: snake_case_ : Dict = None snake_case_ : Tuple = 0.0 snake_case_ : Dict = 0.0 for step, inputs in enumerate(tqdm(_UpperCamelCase , desc='''Iteration''' , disable=args.local_rank not in [-1, 0] ) ): snake_case_ : Any = tuple(t.to(args.device ) for t in inputs ) ((snake_case_) , ) : Union[str, Any] = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) snake_case_ : List[str] = model(_UpperCamelCase , labels=_UpperCamelCase , head_mask=_UpperCamelCase ) # (loss), lm_logits, presents, (all hidden_states), (attentions) snake_case_ , snake_case_ , snake_case_ : int = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(_UpperCamelCase ): snake_case_ : Dict = entropy(attn.detach() , _UpperCamelCase ) attn_entropy[layer] += masked_entropy.sum(-1 ).sum(0 ).sum(0 ).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(_UpperCamelCase ).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: snake_case_ : Union[str, Any] = 2 snake_case_ : Any = torch.pow(torch.pow(_UpperCamelCase , _UpperCamelCase ).sum(-1 ) , 1 / exponent ) head_importance /= norm_by_layer.unsqueeze(-1 ) + 1E-20 if not args.dont_normalize_global_importance: snake_case_ : Union[str, Any] = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''' ) print_ad_tensor(_UpperCamelCase ) if compute_importance: logger.info('''Head importance scores''' ) print_ad_tensor(_UpperCamelCase ) logger.info('''Head ranked by importance scores''' ) snake_case_ : Optional[int] = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device ) snake_case_ : Union[str, Any] = torch.arange( head_importance.numel() , device=args.device ) snake_case_ : Dict = head_ranks.view_as(_UpperCamelCase ) print_ad_tensor(_UpperCamelCase ) return attn_entropy, head_importance, total_loss def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) -> Optional[int]: """simple docstring""" snake_case_ , snake_case_ , snake_case_ : Optional[int] = compute_heads_importance(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , compute_entropy=_UpperCamelCase ) snake_case_ : Any = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , _UpperCamelCase , original_score * args.masking_threshold ) snake_case_ : Any = torch.ones_like(_UpperCamelCase ) snake_case_ : Union[str, Any] = max(1 , int(new_head_mask.numel() * args.masking_amount ) ) snake_case_ : List[Any] = original_score while current_score >= original_score * args.masking_threshold: snake_case_ : List[str] = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads snake_case_ : Optional[Any] = float('''Inf''' ) snake_case_ : List[Any] = head_importance.view(-1 ).sort()[1] if len(_UpperCamelCase ) <= num_to_mask: print('''BREAK BY num_to_mask''' ) break # mask heads snake_case_ : Optional[int] = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist() ) ) snake_case_ : Optional[Any] = new_head_mask.view(-1 ) snake_case_ : int = 0.0 snake_case_ : List[Any] = new_head_mask.view_as(_UpperCamelCase ) snake_case_ : List[str] = new_head_mask.clone().detach() print_ad_tensor(_UpperCamelCase ) # Compute metric and head importance again snake_case_ , snake_case_ , snake_case_ : str = compute_heads_importance( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , compute_entropy=_UpperCamelCase , head_mask=_UpperCamelCase ) snake_case_ : Tuple = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , _UpperCamelCase , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''' ) print_ad_tensor(_UpperCamelCase ) np.save(os.path.join(args.output_dir , '''head_mask.npy''' ) , head_mask.detach().cpu().numpy() ) return head_mask def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) -> str: """simple docstring""" snake_case_ : str = datetime.now() snake_case_ , snake_case_ , snake_case_ : List[Any] = compute_heads_importance( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , compute_entropy=_UpperCamelCase , compute_importance=_UpperCamelCase , head_mask=_UpperCamelCase ) snake_case_ : Union[str, Any] = 1 / loss snake_case_ : Union[str, Any] = datetime.now() - before_time snake_case_ : int = sum(p.numel() for p in model.parameters() ) snake_case_ : Tuple = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(_UpperCamelCase ) ) } for k, v in heads_to_prune.items(): if isinstance(_UpperCamelCase , _UpperCamelCase ): snake_case_ : Any = [ v, ] assert sum(len(_UpperCamelCase ) for h in heads_to_prune.values() ) == (1 - head_mask.long()).sum().item() model.prune_heads(_UpperCamelCase ) snake_case_ : Union[str, Any] = sum(p.numel() for p in model.parameters() ) snake_case_ : Dict = datetime.now() snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = compute_heads_importance( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , compute_entropy=_UpperCamelCase , compute_importance=_UpperCamelCase , head_mask=_UpperCamelCase , actually_pruned=_UpperCamelCase , ) snake_case_ : Union[str, Any] = 1 / loss snake_case_ : Optional[Any] = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , _UpperCamelCase , _UpperCamelCase , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , _UpperCamelCase , _UpperCamelCase ) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100 ) save_model(_UpperCamelCase , args.output_dir ) def lowerCamelCase_ ( ) -> Optional[int]: """simple docstring""" snake_case_ : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=_UpperCamelCase , type=_UpperCamelCase , required=_UpperCamelCase , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=_UpperCamelCase , type=_UpperCamelCase , required=_UpperCamelCase , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=_UpperCamelCase , type=_UpperCamelCase , required=_UpperCamelCase , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=_UpperCamelCase , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=_UpperCamelCase , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=_UpperCamelCase , type=_UpperCamelCase , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=_UpperCamelCase , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''' ) parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''' ) parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''' ) parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''' ) parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''' ) parser.add_argument( '''--masking_threshold''' , default=0.9 , type=_UpperCamelCase , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=_UpperCamelCase , help='''Amount to heads to masking at each masking step.''' ) parser.add_argument('''--metric_name''' , default='''acc''' , type=_UpperCamelCase , help='''Metric to use for head masking.''' ) parser.add_argument( '''--max_seq_length''' , default=128 , type=_UpperCamelCase , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=_UpperCamelCase , help='''Batch size.''' ) parser.add_argument('''--seed''' , type=_UpperCamelCase , default=42 ) parser.add_argument('''--local_rank''' , type=_UpperCamelCase , default=-1 , help='''local_rank for distributed training on gpus''' ) parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''' ) parser.add_argument('''--server_ip''' , type=_UpperCamelCase , default='''''' , help='''Can be used for distant debugging.''' ) parser.add_argument('''--server_port''' , type=_UpperCamelCase , default='''''' , help='''Can be used for distant debugging.''' ) snake_case_ : Any = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=_UpperCamelCase ) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: snake_case_ : Tuple = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''' ) snake_case_ : Tuple = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank ) snake_case_ : List[str] = torch.device('''cuda''' , args.local_rank ) snake_case_ : Union[str, Any] = 1 torch.distributed.init_process_group(backend='''nccl''' ) # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN ) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1 ) ) ) snake_case_ : int = GPTaLMHeadModel.from_pretrained(args.model_name_or_path ) # Distributed and parallel training model.to(args.device ) if args.local_rank != -1: snake_case_ : Any = nn.parallel.DistributedDataParallel( _UpperCamelCase , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=_UpperCamelCase ) elif args.n_gpu > 1: snake_case_ : Dict = nn.DataParallel(_UpperCamelCase ) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=_UpperCamelCase ) torch.save(_UpperCamelCase , os.path.join(args.output_dir , '''run_args.bin''' ) ) logger.info('''Training/evaluation parameters %s''' , _UpperCamelCase ) # Prepare dataset snake_case_ : str = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa ), ] ) snake_case_ : Any = (torch.from_numpy(_UpperCamelCase ),) snake_case_ : Any = TensorDataset(*_UpperCamelCase ) snake_case_ : List[str] = RandomSampler(_UpperCamelCase ) snake_case_ : int = DataLoader(_UpperCamelCase , sampler=_UpperCamelCase , batch_size=args.batch_size ) # Compute head entropy and importance score compute_heads_importance(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: snake_case_ : List[str] = mask_heads(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) prune_heads(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if __name__ == "__main__": main()
279
import unittest import numpy as np import torch from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __lowerCAmelCase ( unittest.TestCase ): @property def lowerCamelCase (self ) -> Union[str, Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case_ : int = 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''') , ) return model def lowerCamelCase (self ) -> Dict: '''simple docstring''' snake_case_ : Any = self.dummy_uncond_unet snake_case_ : Optional[Any] = PNDMScheduler() snake_case_ : Optional[Any] = PNDMPipeline(unet=__magic_name__ , scheduler=__magic_name__ ) pndm.to(__magic_name__ ) pndm.set_progress_bar_config(disable=__magic_name__ ) snake_case_ : str = torch.manual_seed(0 ) snake_case_ : Dict = pndm(generator=__magic_name__ , num_inference_steps=20 , output_type='''numpy''' ).images snake_case_ : str = torch.manual_seed(0 ) snake_case_ : str = pndm(generator=__magic_name__ , num_inference_steps=20 , output_type='''numpy''' , return_dict=__magic_name__ )[0] snake_case_ : Any = image[0, -3:, -3:, -1] snake_case_ : str = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case_ : str = np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase (self ) -> Any: '''simple docstring''' snake_case_ : Tuple = '''google/ddpm-cifar10-32''' snake_case_ : Tuple = UNetaDModel.from_pretrained(__magic_name__ ) snake_case_ : Optional[Any] = PNDMScheduler() snake_case_ : Any = PNDMPipeline(unet=__magic_name__ , scheduler=__magic_name__ ) pndm.to(__magic_name__ ) pndm.set_progress_bar_config(disable=__magic_name__ ) snake_case_ : int = torch.manual_seed(0 ) snake_case_ : Tuple = pndm(generator=__magic_name__ , output_type='''numpy''' ).images snake_case_ : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case_ : str = np.array([0.1_564, 0.14_645, 0.1_406, 0.14_715, 0.12_425, 0.14_045, 0.13_115, 0.12_175, 0.125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
279
1
"""simple docstring""" _A = """ # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git """ _A = [{"""type""": """code""", """content""": INSTALL_CONTENT}] _A = { """{processor_class}""": """FakeProcessorClass""", """{model_class}""": """FakeModelClass""", """{object_class}""": """FakeObjectClass""", }
171
"""simple docstring""" import os from datetime import datetime as dt from github import Github _A = [ """good first issue""", """feature request""", """wip""", ] def a__ ( ) -> str: UpperCAmelCase__ : Union[str, Any] = Github(os.environ["""GITHUB_TOKEN"""] ) UpperCAmelCase__ : Dict = g.get_repo("""huggingface/accelerate""" ) UpperCAmelCase__ : str = repo.get_issues(state="""open""" ) for issue in open_issues: UpperCAmelCase__ : Optional[Any] = sorted([comment for comment in issue.get_comments()] , key=lambda lowerCAmelCase : i.created_at , reverse=lowerCAmelCase ) UpperCAmelCase__ : List[Any] = comments[0] if len(lowerCAmelCase ) > 0 else None UpperCAmelCase__ : Optional[Any] = dt.utcnow() UpperCAmelCase__ : List[str] = (current_time - issue.updated_at).days UpperCAmelCase__ : Optional[int] = (current_time - issue.created_at).days if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and days_since_updated > 7 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Close issue since it has been 7 days of inactivity since bot mention. issue.edit(state="""closed""" ) elif ( days_since_updated > 23 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Add stale comment issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
171
1
"""simple docstring""" import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def lowerCAmelCase_( lowercase_ : str , lowercase_ : List[str] ) -> Any: # Load checkpoint _lowerCamelCase = torch.load(lowercase_ , map_location='''cpu''' ) _lowerCamelCase = chkpt['''model'''] # We have the base model one level deeper than the original XLM repository _lowerCamelCase = {} for k, v in state_dict.items(): if "pred_layer" in k: _lowerCamelCase = v else: _lowerCamelCase = v _lowerCamelCase = chkpt['''params'''] _lowerCamelCase = {n: v for n, v in config.items() if not isinstance(lowercase_ , (torch.FloatTensor, numpy.ndarray) )} _lowerCamelCase = chkpt['''dico_word2id'''] _lowerCamelCase = {s + '''</w>''' if s.find('''@@''' ) == -1 and i > 13 else s.replace('''@@''' , '''''' ): i for s, i in vocab.items()} # Save pytorch-model _lowerCamelCase = pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME _lowerCamelCase = pytorch_dump_folder_path + '''/''' + CONFIG_NAME _lowerCamelCase = pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''vocab_file'''] print(F"""Save PyTorch model to {pytorch_weights_dump_path}""" ) torch.save(lowercase_ , lowercase_ ) print(F"""Save configuration file to {pytorch_config_dump_path}""" ) with open(lowercase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(lowercase_ , indent=2 ) + '''\n''' ) print(F"""Save vocab file to {pytorch_config_dump_path}""" ) with open(lowercase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(lowercase_ , indent=2 ) + '''\n''' ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--xlm_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __SCREAMING_SNAKE_CASE : List[Any] = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
73
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) __SCREAMING_SNAKE_CASE : List[str] = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : List[str] = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : List[Any] = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Optional[int] = ['''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 __SCREAMING_SNAKE_CASE : str = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
73
1
"""simple docstring""" from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowercase_ ( nn.Module ): '''simple docstring''' def __init__( self : Dict , _UpperCAmelCase : Tuple = 16 , _UpperCAmelCase : Union[str, Any] = 88 , _UpperCAmelCase : str = None , _UpperCAmelCase : int = 1 , _UpperCAmelCase : int = 0.0 , _UpperCAmelCase : int = 32 , _UpperCAmelCase : str = None , _UpperCAmelCase : str = False , _UpperCAmelCase : int = None , _UpperCAmelCase : Optional[Any] = None , _UpperCAmelCase : Union[str, Any] = "geglu" , _UpperCAmelCase : Any = None , ): super().__init__() _A = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_a , attention_head_dim=_a , in_channels=_a , num_layers=_a , dropout=_a , norm_num_groups=_a , cross_attention_dim=_a , attention_bias=_a , sample_size=_a , num_vector_embeds=_a , activation_fn=_a , num_embeds_ada_norm=_a , ) 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 _A = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _A = [77, 257] # 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])` _A = [1, 0] def lowerCAmelCase_ ( self : List[str] , _UpperCAmelCase : Dict , _UpperCAmelCase : Tuple , _UpperCAmelCase : int=None , _UpperCAmelCase : Dict=None , _UpperCAmelCase : Optional[int]=None , _UpperCAmelCase : Any = True , ): _A = hidden_states _A = [] _A = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _A = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _A = self.transformer_index_for_condition[i] _A = self.transformers[transformer_index]( _a , encoder_hidden_states=_a , timestep=_a , cross_attention_kwargs=_a , return_dict=_a , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _A = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _A = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_a )
315
import requests a__ = '''YOUR API KEY''' def __UpperCAmelCase ( __a : str ,__a : str = giphy_api_key ) -> list: """simple docstring""" _a : Optional[Any] = '''+'''.join(query.split() ) _a : Union[str, Any] = F"""https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}""" _a : List[Any] = requests.get(__a ).json()['''data'''] return [gif["url"] for gif in gifs] if __name__ == "__main__": print('''\n'''.join(get_gifs('''space ship''')))
235
0
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, Pipeline, ZeroShotClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. A__ : Optional[int] ={'''LayoutLMv2Config''', '''LayoutLMv3Config'''} @is_pipeline_test class UpperCAmelCase ( unittest.TestCase ): _lowercase: Tuple = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING _lowercase: Union[str, Any] = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: _lowercase: Any = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: _lowercase: Optional[Any] = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } def lowercase__ ( self : Dict , __snake_case : str , __snake_case : Dict , __snake_case : Union[str, Any] ) -> List[Any]: _lowerCAmelCase = ZeroShotClassificationPipeline( model=_UpperCAmelCase , tokenizer=_UpperCAmelCase , candidate_labels=["""polics""", """health"""] ) return classifier, ["Who are you voting for in 2020?", "My stomach hurts."] def lowercase__ ( self : int , __snake_case : Union[str, Any] , __snake_case : Any ) -> List[str]: _lowerCAmelCase = classifier("""Who are you voting for in 2020?""" , candidate_labels="""politics""" ) self.assertEqual(_UpperCAmelCase , {"""sequence""": ANY(_UpperCAmelCase ), """labels""": [ANY(_UpperCAmelCase )], """scores""": [ANY(_UpperCAmelCase )]} ) # No kwarg _lowerCAmelCase = classifier("""Who are you voting for in 2020?""" , ["""politics"""] ) self.assertEqual(_UpperCAmelCase , {"""sequence""": ANY(_UpperCAmelCase ), """labels""": [ANY(_UpperCAmelCase )], """scores""": [ANY(_UpperCAmelCase )]} ) _lowerCAmelCase = classifier("""Who are you voting for in 2020?""" , candidate_labels=["""politics"""] ) self.assertEqual(_UpperCAmelCase , {"""sequence""": ANY(_UpperCAmelCase ), """labels""": [ANY(_UpperCAmelCase )], """scores""": [ANY(_UpperCAmelCase )]} ) _lowerCAmelCase = classifier("""Who are you voting for in 2020?""" , candidate_labels="""politics, public health""" ) self.assertEqual( _UpperCAmelCase , {"""sequence""": ANY(_UpperCAmelCase ), """labels""": [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )], """scores""": [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""] ) ) , 1.0 ) _lowerCAmelCase = classifier("""Who are you voting for in 2020?""" , candidate_labels=["""politics""", """public health"""] ) self.assertEqual( _UpperCAmelCase , {"""sequence""": ANY(_UpperCAmelCase ), """labels""": [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )], """scores""": [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""] ) ) , 1.0 ) _lowerCAmelCase = classifier( """Who are you voting for in 2020?""" , candidate_labels="""politics""" , hypothesis_template="""This text is about {}""" ) self.assertEqual(_UpperCAmelCase , {"""sequence""": ANY(_UpperCAmelCase ), """labels""": [ANY(_UpperCAmelCase )], """scores""": [ANY(_UpperCAmelCase )]} ) # https://github.com/huggingface/transformers/issues/13846 _lowerCAmelCase = classifier(["""I am happy"""] , ["""positive""", """negative"""] ) self.assertEqual( _UpperCAmelCase , [ {"""sequence""": ANY(_UpperCAmelCase ), """labels""": [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )], """scores""": [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )]} for i in range(1 ) ] , ) _lowerCAmelCase = classifier(["""I am happy""", """I am sad"""] , ["""positive""", """negative"""] ) self.assertEqual( _UpperCAmelCase , [ {"""sequence""": ANY(_UpperCAmelCase ), """labels""": [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )], """scores""": [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )]} for i in range(2 ) ] , ) with self.assertRaises(_UpperCAmelCase ): classifier("""""" , candidate_labels="""politics""" ) with self.assertRaises(_UpperCAmelCase ): classifier(_UpperCAmelCase , candidate_labels="""politics""" ) with self.assertRaises(_UpperCAmelCase ): classifier("""Who are you voting for in 2020?""" , candidate_labels="""""" ) with self.assertRaises(_UpperCAmelCase ): classifier("""Who are you voting for in 2020?""" , candidate_labels=_UpperCAmelCase ) with self.assertRaises(_UpperCAmelCase ): classifier( """Who are you voting for in 2020?""" , candidate_labels="""politics""" , hypothesis_template="""Not formatting template""" , ) with self.assertRaises(_UpperCAmelCase ): classifier( """Who are you voting for in 2020?""" , candidate_labels="""politics""" , hypothesis_template=_UpperCAmelCase , ) self.run_entailment_id(_UpperCAmelCase ) def lowercase__ ( self : Optional[int] , __snake_case : Dict ) -> Any: _lowerCAmelCase = zero_shot_classifier.model.config _lowerCAmelCase = config.labelaid _lowerCAmelCase = zero_shot_classifier.entailment_id _lowerCAmelCase = {'''LABEL_0''': 0, '''LABEL_1''': 1, '''LABEL_2''': 2} self.assertEqual(zero_shot_classifier.entailment_id , -1 ) _lowerCAmelCase = {'''entailment''': 0, '''neutral''': 1, '''contradiction''': 2} self.assertEqual(zero_shot_classifier.entailment_id , 0 ) _lowerCAmelCase = {'''ENTAIL''': 0, '''NON-ENTAIL''': 1} self.assertEqual(zero_shot_classifier.entailment_id , 0 ) _lowerCAmelCase = {'''ENTAIL''': 2, '''NEUTRAL''': 1, '''CONTR''': 0} self.assertEqual(zero_shot_classifier.entailment_id , 2 ) _lowerCAmelCase = original_labelaid self.assertEqual(_UpperCAmelCase , zero_shot_classifier.entailment_id ) @require_torch def lowercase__ ( self : Optional[Any] ) -> List[Any]: _lowerCAmelCase = pipeline( """zero-shot-classification""" , model="""sshleifer/tiny-distilbert-base-cased-distilled-squad""" , framework="""pt""" , ) # There was a regression in 4.10 for this # Adding a test so we don't make the mistake again. # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499 zero_shot_classifier( """Who are you voting for in 2020?""" * 1_00 , candidate_labels=["""politics""", """public health""", """science"""] ) @require_torch def lowercase__ ( self : Any ) -> Tuple: _lowerCAmelCase = pipeline( """zero-shot-classification""" , model="""sshleifer/tiny-distilbert-base-cased-distilled-squad""" , framework="""pt""" , ) _lowerCAmelCase = zero_shot_classifier( """Who are you voting for in 2020?""" , candidate_labels=["""politics""", """public health""", """science"""] ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { """sequence""": """Who are you voting for in 2020?""", """labels""": ["""science""", """public health""", """politics"""], """scores""": [0.3_33, 0.3_33, 0.3_33], } , ) @require_tf def lowercase__ ( self : List[Any] ) -> Union[str, Any]: _lowerCAmelCase = pipeline( """zero-shot-classification""" , model="""sshleifer/tiny-distilbert-base-cased-distilled-squad""" , framework="""tf""" , ) _lowerCAmelCase = zero_shot_classifier( """Who are you voting for in 2020?""" , candidate_labels=["""politics""", """public health""", """science"""] ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { """sequence""": """Who are you voting for in 2020?""", """labels""": ["""science""", """public health""", """politics"""], """scores""": [0.3_33, 0.3_33, 0.3_33], } , ) @slow @require_torch def lowercase__ ( self : int ) -> Union[str, Any]: _lowerCAmelCase = pipeline("""zero-shot-classification""" , model="""roberta-large-mnli""" , framework="""pt""" ) _lowerCAmelCase = zero_shot_classifier( """Who are you voting for in 2020?""" , candidate_labels=["""politics""", """public health""", """science"""] ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { """sequence""": """Who are you voting for in 2020?""", """labels""": ["""politics""", """public health""", """science"""], """scores""": [0.9_76, 0.0_15, 0.0_09], } , ) _lowerCAmelCase = zero_shot_classifier( """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks""" """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder""" """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based""" """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two""" """ machine translation tasks show these models to be superior in quality while being more parallelizable""" """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014""" """ English-to-German translation task, improving over the existing best results, including ensembles by""" """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new""" """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small""" """ fraction of the training costs of the best models from the literature. We show that the Transformer""" """ generalizes well to other tasks by applying it successfully to English constituency parsing both with""" """ large and limited training data.""" , candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""] , multi_label=_UpperCAmelCase , ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { """sequence""": ( """The dominant sequence transduction models are based on complex recurrent or convolutional neural""" """ networks in an encoder-decoder configuration. The best performing models also connect the""" """ encoder and decoder through an attention mechanism. We propose a new simple network""" """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence""" """ and convolutions entirely. Experiments on two machine translation tasks show these models to be""" """ superior in quality while being more parallelizable and requiring significantly less time to""" """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,""" """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014""" """ English-to-French translation task, our model establishes a new single-model state-of-the-art""" """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training""" """ costs of the best models from the literature. We show that the Transformer generalizes well to""" """ other tasks by applying it successfully to English constituency parsing both with large and""" """ limited training data.""" ), """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""], """scores""": [0.8_17, 0.7_13, 0.0_18, 0.0_18], } , ) @slow @require_tf def lowercase__ ( self : int ) -> Optional[Any]: _lowerCAmelCase = pipeline("""zero-shot-classification""" , model="""roberta-large-mnli""" , framework="""tf""" ) _lowerCAmelCase = zero_shot_classifier( """Who are you voting for in 2020?""" , candidate_labels=["""politics""", """public health""", """science"""] ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { """sequence""": """Who are you voting for in 2020?""", """labels""": ["""politics""", """public health""", """science"""], """scores""": [0.9_76, 0.0_15, 0.0_09], } , ) _lowerCAmelCase = zero_shot_classifier( """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks""" """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder""" """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based""" """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two""" """ machine translation tasks show these models to be superior in quality while being more parallelizable""" """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014""" """ English-to-German translation task, improving over the existing best results, including ensembles by""" """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new""" """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small""" """ fraction of the training costs of the best models from the literature. We show that the Transformer""" """ generalizes well to other tasks by applying it successfully to English constituency parsing both with""" """ large and limited training data.""" , candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""] , multi_label=_UpperCAmelCase , ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { """sequence""": ( """The dominant sequence transduction models are based on complex recurrent or convolutional neural""" """ networks in an encoder-decoder configuration. The best performing models also connect the""" """ encoder and decoder through an attention mechanism. We propose a new simple network""" """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence""" """ and convolutions entirely. Experiments on two machine translation tasks show these models to be""" """ superior in quality while being more parallelizable and requiring significantly less time to""" """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,""" """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014""" """ English-to-French translation task, our model establishes a new single-model state-of-the-art""" """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training""" """ costs of the best models from the literature. We show that the Transformer generalizes well to""" """ other tasks by applying it successfully to English constituency parsing both with large and""" """ limited training data.""" ), """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""], """scores""": [0.8_17, 0.7_13, 0.0_18, 0.0_18], } , )
352
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPSegProcessor, ViTImageProcessor @require_vision class UpperCAmelCase ( unittest.TestCase ): def lowercase__ ( self : List[str] ) -> Dict: _lowerCAmelCase = tempfile.mkdtemp() # fmt: off _lowerCAmelCase = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on _lowerCAmelCase = dict(zip(__snake_case , range(len(__snake_case ) ) ) ) _lowerCAmelCase = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] _lowerCAmelCase = {"""unk_token""": """<unk>"""} _lowerCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase = 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(__snake_case ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__snake_case ) ) _lowerCAmelCase = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 18, """do_normalize""": True, """image_mean""": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], """image_std""": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], } _lowerCAmelCase = os.path.join(self.tmpdirname , __snake_case ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__snake_case , __snake_case ) def lowercase__ ( self : Optional[int] , **__snake_case : Dict ) -> List[str]: return CLIPTokenizer.from_pretrained(self.tmpdirname , **__snake_case ) def lowercase__ ( self : List[str] , **__snake_case : Any ) -> List[Any]: return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__snake_case ) def lowercase__ ( self : Tuple , **__snake_case : List[str] ) -> int: return ViTImageProcessor.from_pretrained(self.tmpdirname , **__snake_case ) def lowercase__ ( self : str ) -> Optional[Any]: shutil.rmtree(self.tmpdirname ) def lowercase__ ( self : str ) -> List[Any]: _lowerCAmelCase = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] _lowerCAmelCase = [Image.fromarray(np.moveaxis(__snake_case , 0 , -1 ) ) for x in image_inputs] return image_inputs def lowercase__ ( self : Optional[Any] ) -> List[str]: _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = self.get_rust_tokenizer() _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = CLIPSegProcessor(tokenizer=__snake_case , image_processor=__snake_case ) processor_slow.save_pretrained(self.tmpdirname ) _lowerCAmelCase = CLIPSegProcessor.from_pretrained(self.tmpdirname , use_fast=__snake_case ) _lowerCAmelCase = CLIPSegProcessor(tokenizer=__snake_case , image_processor=__snake_case ) processor_fast.save_pretrained(self.tmpdirname ) _lowerCAmelCase = CLIPSegProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __snake_case ) self.assertIsInstance(processor_fast.tokenizer , __snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __snake_case ) self.assertIsInstance(processor_fast.image_processor , __snake_case ) def lowercase__ ( self : Optional[int] ) -> Dict: _lowerCAmelCase = CLIPSegProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _lowerCAmelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) _lowerCAmelCase = self.get_image_processor(do_normalize=__snake_case , padding_value=1.0 ) _lowerCAmelCase = CLIPSegProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__snake_case , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __snake_case ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __snake_case ) def lowercase__ ( self : Tuple ) -> List[Any]: _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = CLIPSegProcessor(tokenizer=__snake_case , image_processor=__snake_case ) _lowerCAmelCase = self.prepare_image_inputs() _lowerCAmelCase = image_processor(__snake_case , return_tensors="""np""" ) _lowerCAmelCase = processor(images=__snake_case , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def lowercase__ ( self : Any ) -> str: _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = CLIPSegProcessor(tokenizer=__snake_case , image_processor=__snake_case ) _lowerCAmelCase = """lower newer""" _lowerCAmelCase = processor(text=__snake_case ) _lowerCAmelCase = tokenizer(__snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowercase__ ( self : str ) -> Union[str, Any]: _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = CLIPSegProcessor(tokenizer=__snake_case , image_processor=__snake_case ) _lowerCAmelCase = """lower newer""" _lowerCAmelCase = self.prepare_image_inputs() _lowerCAmelCase = processor(text=__snake_case , images=__snake_case ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__snake_case ): processor() def lowercase__ ( self : Union[str, Any] ) -> Dict: _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = CLIPSegProcessor(tokenizer=__snake_case , image_processor=__snake_case ) _lowerCAmelCase = self.prepare_image_inputs() _lowerCAmelCase = self.prepare_image_inputs() _lowerCAmelCase = processor(images=__snake_case , visual_prompt=__snake_case ) self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """conditional_pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__snake_case ): processor() def lowercase__ ( self : int ) -> Tuple: _lowerCAmelCase = self.get_image_processor() _lowerCAmelCase = self.get_tokenizer() _lowerCAmelCase = CLIPSegProcessor(tokenizer=__snake_case , image_processor=__snake_case ) _lowerCAmelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] _lowerCAmelCase = processor.batch_decode(__snake_case ) _lowerCAmelCase = tokenizer.batch_decode(__snake_case ) self.assertListEqual(__snake_case , __snake_case )
220
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCamelCase = { '''configuration_upernet''': ['''UperNetConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = [ '''UperNetForSemanticSegmentation''', '''UperNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_upernet import UperNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel else: import sys __UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
69
import json import os import tempfile import unittest import unittest.mock as mock from pathlib import Path from requests.exceptions import HTTPError from transformers.utils import ( CONFIG_NAME, FLAX_WEIGHTS_NAME, TF2_WEIGHTS_NAME, TRANSFORMERS_CACHE, WEIGHTS_NAME, cached_file, get_file_from_repo, has_file, ) _SCREAMING_SNAKE_CASE : List[str] = "hf-internal-testing/tiny-random-bert" _SCREAMING_SNAKE_CASE : Optional[Any] = os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert") _SCREAMING_SNAKE_CASE : Optional[int] = "9b8c223d42b2188cb49d29af482996f9d0f3e5a6" class A__ ( unittest.TestCase ): """simple docstring""" def a_ ( self ): snake_case = cached_file(__snake_case , __snake_case ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(__snake_case ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(__snake_case , __snake_case ) ) ) with open(os.path.join(__snake_case , '''refs''' , '''main''' ) ) as f: snake_case = f.read() self.assertEqual(__snake_case , os.path.join(__snake_case , '''snapshots''' , __snake_case , __snake_case ) ) self.assertTrue(os.path.isfile(__snake_case ) ) # File is cached at the same place the second time. snake_case = cached_file(__snake_case , __snake_case ) self.assertEqual(__snake_case , __snake_case ) # Using a specific revision to test the full commit hash. snake_case = cached_file(__snake_case , __snake_case , revision='''9b8c223''' ) self.assertEqual(__snake_case , os.path.join(__snake_case , '''snapshots''' , __snake_case , __snake_case ) ) def a_ ( self ): with self.assertRaisesRegex(__snake_case , '''is not a valid model identifier''' ): snake_case = cached_file('''tiny-random-bert''' , __snake_case ) with self.assertRaisesRegex(__snake_case , '''is not a valid git identifier''' ): snake_case = cached_file(__snake_case , __snake_case , revision='''aaaa''' ) with self.assertRaisesRegex(__snake_case , '''does not appear to have a file named''' ): snake_case = cached_file(__snake_case , '''conf''' ) def a_ ( self ): with self.assertRaisesRegex(__snake_case , '''does not appear to have a file named''' ): snake_case = cached_file(__snake_case , '''conf''' ) with open(os.path.join(__snake_case , '''refs''' , '''main''' ) ) as f: snake_case = f.read() self.assertTrue(os.path.isfile(os.path.join(__snake_case , '''.no_exist''' , __snake_case , '''conf''' ) ) ) snake_case = cached_file(__snake_case , '''conf''' , _raise_exceptions_for_missing_entries=__snake_case ) self.assertIsNone(__snake_case ) snake_case = cached_file(__snake_case , '''conf''' , local_files_only=__snake_case , _raise_exceptions_for_missing_entries=__snake_case ) self.assertIsNone(__snake_case ) snake_case = mock.Mock() snake_case = 5_0_0 snake_case = {} snake_case = HTTPError snake_case = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch('''requests.Session.request''' , return_value=__snake_case ) as mock_head: snake_case = cached_file(__snake_case , '''conf''' , _raise_exceptions_for_connection_errors=__snake_case ) self.assertIsNone(__snake_case ) # This check we did call the fake head request mock_head.assert_called() def a_ ( self ): self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''' , __snake_case ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , __snake_case ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , __snake_case ) ) def a_ ( self ): # `get_file_from_repo` returns None if the file does not exist self.assertIsNone(get_file_from_repo('''bert-base-cased''' , '''ahah.txt''' ) ) # The function raises if the repository does not exist. with self.assertRaisesRegex(__snake_case , '''is not a valid model identifier''' ): get_file_from_repo('''bert-base-case''' , __snake_case ) # The function raises if the revision does not exist. with self.assertRaisesRegex(__snake_case , '''is not a valid git identifier''' ): get_file_from_repo('''bert-base-cased''' , __snake_case , revision='''ahaha''' ) snake_case = get_file_from_repo('''bert-base-cased''' , __snake_case ) # The name is the cached name which is not very easy to test, so instead we load the content. snake_case = json.loads(open(__snake_case , '''r''' ).read() ) self.assertEqual(config['''hidden_size'''] , 7_6_8 ) def a_ ( self ): with tempfile.TemporaryDirectory() as tmp_dir: snake_case = Path(__snake_case ) / '''a.txt''' filename.touch() self.assertEqual(get_file_from_repo(__snake_case , '''a.txt''' ) , str(__snake_case ) ) self.assertIsNone(get_file_from_repo(__snake_case , '''b.txt''' ) )
127
0
"""simple docstring""" from pathlib import Path import fire from tqdm import tqdm def UpperCamelCase ( lowerCAmelCase__="ro" , lowerCAmelCase__="en" , lowerCAmelCase__="wmt16" , lowerCAmelCase__=None ): '''simple docstring''' try: import datasets except (ModuleNotFoundError, ImportError): raise ImportError('''run pip install datasets''' ) lowercase = f'{src_lang}-{tgt_lang}' print(f'Converting {dataset}-{pair}' ) lowercase = datasets.load_dataset(lowerCAmelCase__ , lowerCAmelCase__ ) if save_dir is None: lowercase = f'{dataset}-{pair}' lowercase = Path(lowerCAmelCase__ ) save_dir.mkdir(exist_ok=lowerCAmelCase__ ) for split in ds.keys(): print(f'Splitting {split} with {ds[split].num_rows} records' ) # to save to val.source, val.target like summary datasets lowercase = '''val''' if split == '''validation''' else split lowercase = save_dir.joinpath(f'{fn}.source' ) lowercase = save_dir.joinpath(f'{fn}.target' ) lowercase = src_path.open('''w+''' ) lowercase = tgt_path.open('''w+''' ) # reader is the bottleneck so writing one record at a time doesn't slow things down for x in tqdm(ds[split] ): lowercase = x['''translation'''] src_fp.write(ex[src_lang] + '''\n''' ) tgt_fp.write(ex[tgt_lang] + '''\n''' ) print(f'Saved {dataset} dataset to {save_dir}' ) if __name__ == "__main__": fire.Fire(download_wmt_dataset)
352
import unittest from dataclasses import dataclass import pytest from accelerate.commands.config.config_args import SageMakerConfig from accelerate.utils import ComputeEnvironment from accelerate.utils.launch import _convert_nargs_to_dict @dataclass class lowercase ( SCREAMING_SNAKE_CASE__ ): lowercase_ : int =ComputeEnvironment.AMAZON_SAGEMAKER lowercase_ : Optional[int] =True lowercase_ : Any ='''ml.p3.2xlarge''' lowercase_ : Any ='''accelerate_sagemaker_execution_role''' lowercase_ : Union[str, Any] ='''hf-sm''' lowercase_ : Any ='''us-east-1''' lowercase_ : List[str] =1 lowercase_ : Any ='''accelerate-sagemaker-1''' lowercase_ : Union[str, Any] ='''1.6''' lowercase_ : Any ='''4.4''' lowercase_ : Any ='''train.py''' lowercase_ : int =[ '''--model_name_or_path''', '''bert''', '''--do_train''', '''False''', '''--epochs''', '''3''', '''--learning_rate''', '''5e-5''', '''--max_steps''', '''50.5''', ] lowercase_ : List[Any] =[ '''--model_name_or_path''', '''bert''', '''--do_train''', '''--do_test''', '''False''', '''--do_predict''', '''--epochs''', '''3''', '''--learning_rate''', '''5e-5''', '''--max_steps''', '''50.5''', ] class lowercase ( unittest.TestCase ): def A__ ( self): # If no defaults are changed, `to_kwargs` returns an empty dict. lowercase = _convert_nargs_to_dict(MockLaunchConfig.success_training_script_args) assert isinstance(converted_args['''model_name_or_path'''] ,A__) assert isinstance(converted_args['''do_train'''] ,A__) assert isinstance(converted_args['''epochs'''] ,A__) assert isinstance(converted_args['''learning_rate'''] ,A__) assert isinstance(converted_args['''max_steps'''] ,A__) with pytest.raises(A__): _convert_nargs_to_dict(MockLaunchConfig.fail_training_script_args)
97
0
"""simple docstring""" import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.activations import gelu_new, gelu_python, get_activation @require_torch class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> Tuple: lowerCAmelCase_ :str = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) lowerCAmelCase_ :int = get_activation("""gelu""" ) self.assertTrue(torch.allclose(gelu_python(__A ) , torch_builtin(__A ) ) ) self.assertFalse(torch.allclose(gelu_python(__A ) , gelu_new(__A ) ) ) def __lowerCAmelCase ( self ) -> Any: lowerCAmelCase_ :int = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) lowerCAmelCase_ :List[str] = get_activation("""gelu""" ) lowerCAmelCase_ :Any = get_activation("""gelu_10""" ) lowerCAmelCase_ :Tuple = torch_builtin(__A ) lowerCAmelCase_ :Optional[Any] = geluaa(__A ) lowerCAmelCase_ :Union[str, Any] = torch.where(y_gelu_aa < 1_0.0 , 1 , 0 ) self.assertTrue(torch.max(__A ).item() == 1_0.0 ) self.assertTrue(torch.allclose(y_gelu * clipped_mask , y_gelu_aa * clipped_mask ) ) def __lowerCAmelCase ( self ) -> Optional[Any]: get_activation("""gelu""" ) get_activation("""gelu_10""" ) get_activation("""gelu_fast""" ) get_activation("""gelu_new""" ) get_activation("""gelu_python""" ) get_activation("""gelu_pytorch_tanh""" ) get_activation("""linear""" ) get_activation("""mish""" ) get_activation("""quick_gelu""" ) get_activation("""relu""" ) get_activation("""sigmoid""" ) get_activation("""silu""" ) get_activation("""swish""" ) get_activation("""tanh""" ) with self.assertRaises(__A ): get_activation("""bogus""" ) with self.assertRaises(__A ): get_activation(__A ) def __lowerCAmelCase ( self ) -> List[Any]: lowerCAmelCase_ :Any = get_activation("""gelu""" ) lowerCAmelCase_ :Optional[Any] = 1 lowerCAmelCase_ :int = get_activation("""gelu""" ) self.assertEqual(acta.a , 1 ) with self.assertRaises(__A ): lowerCAmelCase_ :str = acta.a
84
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __A : Tuple = logging.get_logger(__name__) __A : List[str] = { "sail/poolformer_s12": "https://huggingface.co/sail/poolformer_s12/resolve/main/config.json", # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class _a ( lowerCAmelCase): """simple docstring""" UpperCamelCase__ = """poolformer""" def __init__( self : List[str] , __UpperCamelCase : int=3 , __UpperCamelCase : List[Any]=1_6 , __UpperCamelCase : str=1_6 , __UpperCamelCase : List[Any]=3 , __UpperCamelCase : int=4.0 , __UpperCamelCase : str=[2, 2, 6, 2] , __UpperCamelCase : Tuple=[6_4, 1_2_8, 3_2_0, 5_1_2] , __UpperCamelCase : int=[7, 3, 3, 3] , __UpperCamelCase : str=[4, 2, 2, 2] , __UpperCamelCase : Union[str, Any]=[2, 1, 1, 1] , __UpperCamelCase : List[str]=4 , __UpperCamelCase : List[str]=0.0 , __UpperCamelCase : Any="gelu" , __UpperCamelCase : List[str]=True , __UpperCamelCase : Union[str, Any]=1e-5 , __UpperCamelCase : str=0.0_2 , **__UpperCamelCase : List[Any] , )->Dict: _UpperCAmelCase = num_channels _UpperCAmelCase = patch_size _UpperCAmelCase = stride _UpperCAmelCase = padding _UpperCAmelCase = pool_size _UpperCAmelCase = hidden_sizes _UpperCAmelCase = mlp_ratio _UpperCAmelCase = depths _UpperCAmelCase = patch_sizes _UpperCAmelCase = strides _UpperCAmelCase = num_encoder_blocks _UpperCAmelCase = drop_path_rate _UpperCAmelCase = hidden_act _UpperCAmelCase = use_layer_scale _UpperCAmelCase = layer_scale_init_value _UpperCAmelCase = initializer_range super().__init__(**__UpperCamelCase ) class _a ( lowerCAmelCase): """simple docstring""" UpperCamelCase__ = version.parse("""1.11""") @property def lowercase__ ( self : Union[str, Any] )->Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def lowercase__ ( self : Tuple )->float: return 2e-3
260
0
import argparse import hashlib # hashlib is only used inside the Test class import struct class SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : str , lowerCAmelCase : Optional[int] ) -> Optional[Any]: """simple docstring""" __lowerCAmelCase : List[str] = data __lowerCAmelCase : int = [0X67452301, 0Xefcdab89, 0X98badcfe, 0X10325476, 0Xc3d2e1f0] @staticmethod def SCREAMING_SNAKE_CASE ( lowerCAmelCase : int , lowerCAmelCase : Dict ) -> Any: """simple docstring""" return ((n << b) | (n >> (32 - b))) & 0Xffffffff def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: """simple docstring""" __lowerCAmelCase : List[Any] = b'''\x80''' + b'''\x00''' * (63 - (len(self.data ) + 8) % 64) __lowerCAmelCase : List[Any] = self.data + padding + struct.pack(""">Q""" , 8 * len(self.data ) ) return padded_data def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" return [ self.padded_data[i : i + 64] for i in range(0 , len(self.padded_data ) , 64 ) ] def SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase : List[str] ) -> Dict: """simple docstring""" __lowerCAmelCase : List[Any] = list(struct.unpack(""">16L""" , _UpperCAmelCase ) ) + [0] * 64 for i in range(16 , 80 ): __lowerCAmelCase : List[Any] = self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]) , 1 ) return w def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: """simple docstring""" __lowerCAmelCase : int = self.padding() __lowerCAmelCase : List[Any] = self.split_blocks() for block in self.blocks: __lowerCAmelCase : str = self.expand_block(_UpperCAmelCase ) __lowerCAmelCase : int = self.h for i in range(0 , 80 ): if 0 <= i < 20: __lowerCAmelCase : int = (b & c) | ((~b) & d) __lowerCAmelCase : int = 0X5a827999 elif 20 <= i < 40: __lowerCAmelCase : int = b ^ c ^ d __lowerCAmelCase : List[str] = 0X6ed9eba1 elif 40 <= i < 60: __lowerCAmelCase : Tuple = (b & c) | (b & d) | (c & d) __lowerCAmelCase : Dict = 0X8f1bbcdc elif 60 <= i < 80: __lowerCAmelCase : Tuple = b ^ c ^ d __lowerCAmelCase : Optional[Any] = 0Xca62c1d6 __lowerCAmelCase : Any = ( self.rotate(_UpperCAmelCase , 5 ) + f + e + k + expanded_block[i] & 0Xffffffff, a, self.rotate(_UpperCAmelCase , 30 ), c, d, ) __lowerCAmelCase : Tuple = ( self.h[0] + a & 0Xffffffff, self.h[1] + b & 0Xffffffff, self.h[2] + c & 0Xffffffff, self.h[3] + d & 0Xffffffff, self.h[4] + e & 0Xffffffff, ) return ("{:08x}" * 5).format(*self.h ) def snake_case_ () -> Union[str, Any]: __lowerCAmelCase : List[Any] = b'''Test String''' assert SHAaHash(__UpperCAmelCase ).final_hash() == hashlib.shaa(__UpperCAmelCase ).hexdigest() # noqa: S324 def snake_case_ () -> Optional[int]: __lowerCAmelCase : List[Any] = argparse.ArgumentParser(description="""Process some strings or files""" ) parser.add_argument( """--string""" , dest="""input_string""" , default="""Hello World!! Welcome to Cryptography""" , help="""Hash the string""" , ) parser.add_argument("""--file""" , dest="""input_file""" , help="""Hash contents of a file""" ) __lowerCAmelCase : Dict = parser.parse_args() __lowerCAmelCase : List[Any] = args.input_string # In any case hash input should be a bytestring if args.input_file: with open(args.input_file , """rb""" ) as f: __lowerCAmelCase : Union[str, Any] = f.read() else: __lowerCAmelCase : int = bytes(__UpperCAmelCase , """utf-8""" ) print(SHAaHash(__UpperCAmelCase ).final_hash() ) if __name__ == "__main__": main() import doctest doctest.testmod()
352
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position __UpperCAmelCase = """2.13.1""" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("""3.7"""): raise ImportWarning( """To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition.""" ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( """To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n""" """If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.""" ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip __UpperCAmelCase = concatenate_datasets __UpperCAmelCase = DownloadConfig __UpperCAmelCase = DownloadManager __UpperCAmelCase = DownloadMode __UpperCAmelCase = DownloadConfig __UpperCAmelCase = DownloadMode __UpperCAmelCase = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
139
0
'''simple docstring''' import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging __a = logging.get_logger(__name__) def __snake_case( _lowerCAmelCase , _lowerCAmelCase ) -> Any: snake_case__ : Tuple = set() snake_case__ : int = [] def parse_line(_lowerCAmelCase ): for line in fp: if isinstance(_lowerCAmelCase , _lowerCAmelCase ): snake_case__ : Tuple = line.decode("""UTF-8""" ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(""" """ ): # process a single warning and move it to `selected_warnings`. if len(_lowerCAmelCase ) > 0: snake_case__ : List[Any] = """\n""".join(_lowerCAmelCase ) # Only keep the warnings specified in `targets` if any(f": {x}: " in warning for x in targets ): selected_warnings.add(_lowerCAmelCase ) buffer.clear() continue else: snake_case__ : int = line.strip() buffer.append(_lowerCAmelCase ) if from_gh: for filename in os.listdir(_lowerCAmelCase ): snake_case__ : int = os.path.join(_lowerCAmelCase , _lowerCAmelCase ) if not os.path.isdir(_lowerCAmelCase ): # read the file if filename != "warnings.txt": continue with open(_lowerCAmelCase ) as fp: parse_line(_lowerCAmelCase ) else: try: with zipfile.ZipFile(_lowerCAmelCase ) as z: for filename in z.namelist(): if not os.path.isdir(_lowerCAmelCase ): # read the file if filename != "warnings.txt": continue with z.open(_lowerCAmelCase ) as fp: parse_line(_lowerCAmelCase ) except Exception: logger.warning( f"{artifact_path} is either an invalid zip file or something else wrong. This file is skipped." ) return selected_warnings def __snake_case( _lowerCAmelCase , _lowerCAmelCase ) -> Optional[Any]: snake_case__ : int = set() snake_case__ : Dict = [os.path.join(_lowerCAmelCase , _lowerCAmelCase ) for p in os.listdir(_lowerCAmelCase ) if (p.endswith(""".zip""" ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(_lowerCAmelCase , _lowerCAmelCase ) ) return selected_warnings if __name__ == "__main__": def __snake_case( _lowerCAmelCase ) -> Tuple: return values.split(""",""" ) __a = argparse.ArgumentParser() # Required parameters parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.") parser.add_argument( "--output_dir", type=str, required=True, help="Where to store the downloaded artifacts and other result files.", ) parser.add_argument("--token", default=None, type=str, help="A token that has actions:read permission.") # optional parameters parser.add_argument( "--targets", default="DeprecationWarning,UserWarning,FutureWarning", type=list_str, help="Comma-separated list of target warning(s) which we want to extract.", ) parser.add_argument( "--from_gh", action="store_true", help="If running from a GitHub action workflow and collecting warnings from its artifacts.", ) __a = parser.parse_args() __a = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links __a = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, "artifacts.json"), "w", encoding="UTF-8") as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print("=" * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts __a = extract_warnings(args.output_dir, args.targets) __a = sorted(selected_warnings) with open(os.path.join(args.output_dir, "selected_warnings.json"), "w", encoding="UTF-8") as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
35
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node _UpperCamelCase = 4 _UpperCamelCase = 3 class _lowerCamelCase ( a ): """simple docstring""" pass def lowerCAmelCase__( lowercase : List[str] ) -> Any: for shard in shards: for i in range(lowercase ): yield {"i": i, "shard": shard} def lowerCAmelCase__( ) -> Optional[int]: __snake_case : List[Any] = int(os.environ["RANK"] ) __snake_case : Optional[int] = int(os.environ["WORLD_SIZE"] ) __snake_case : List[str] = ArgumentParser() parser.add_argument("--streaming" , type=lowercase ) parser.add_argument("--local_rank" , type=lowercase ) parser.add_argument("--num_workers" , type=lowercase , default=0 ) __snake_case : Any = parser.parse_args() __snake_case : Dict = args.streaming __snake_case : Union[str, Any] = args.num_workers __snake_case : Any = {"shards": [f"""shard_{shard_idx}""" for shard_idx in range(lowercase )]} __snake_case : Optional[int] = IterableDataset.from_generator(lowercase , gen_kwargs=lowercase ) if not streaming: __snake_case : Any = Dataset.from_list(list(lowercase ) ) __snake_case : Dict = split_dataset_by_node(lowercase , rank=lowercase , world_size=lowercase ) __snake_case : Union[str, Any] = torch.utils.data.DataLoader(lowercase , num_workers=lowercase ) __snake_case : Optional[int] = NUM_SHARDS * NUM_ITEMS_PER_SHARD __snake_case : List[str] = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) __snake_case : Dict = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f"""local_size {local_size} != expected_local_size {expected_local_size}""" ) if __name__ == "__main__": main()
326
0
__A = 'Input must be a string of 8 numbers plus letter' __A = 'TRWAGMYFPDXBNJZSQVHLCKE' def __A ( _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ): _A = f"""Expected string as input, found {type(_lowercase ).__name__}""" raise TypeError(_lowercase ) _A = spanish_id.replace('''-''' , '''''' ).upper() if len(_lowercase ) != 9: raise ValueError(_lowercase ) try: _A = int(spanish_id_clean[0:8] ) _A = spanish_id_clean[8] except ValueError as ex: raise ValueError(_lowercase ) from ex if letter.isdigit(): raise ValueError(_lowercase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
357
from __future__ import annotations import math def __A ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ): '''simple docstring''' if depth < 0: raise ValueError('''Depth cannot be less than 0''' ) if not scores: raise ValueError('''Scores cannot be empty''' ) if depth == height: return scores[node_index] return ( max( minimax(depth + 1 , node_index * 2 , _lowercase , _lowercase , _lowercase ) , minimax(depth + 1 , node_index * 2 + 1 , _lowercase , _lowercase , _lowercase ) , ) if is_max else min( minimax(depth + 1 , node_index * 2 , _lowercase , _lowercase , _lowercase ) , minimax(depth + 1 , node_index * 2 + 1 , _lowercase , _lowercase , _lowercase ) , ) ) def __A ( ): '''simple docstring''' _A = [90, 23, 6, 33, 21, 65, 1_23, 3_44_23] _A = math.log(len(_lowercase ) , 2 ) print(f"""Optimal value : {minimax(0 , 0 , _lowercase , _lowercase , _lowercase )}""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
75
0
from torch import nn def A_ ( A__ ) -> Dict: if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(F'Unsupported activation function: {act_fn}' )
99
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _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 ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class A__ : """simple docstring""" def __init__( self , lowercase , lowercase=13 , lowercase=64 , lowercase=2 , lowercase=3 , lowercase=True , lowercase=True , lowercase=32 , lowercase=5 , lowercase=4 , lowercase=37 , lowercase="gelu" , lowercase=0.1 , lowercase=0.1 , lowercase=10 , lowercase=0.02 , lowercase=[1, 16, 4, 4] , lowercase=None , ) -> List[Any]: '''simple docstring''' a__ : Optional[int] = parent a__ : Optional[int] = batch_size a__ : Any = image_size a__ : Optional[Any] = patch_size a__ : Optional[Any] = num_channels a__ : int = is_training a__ : List[str] = use_labels a__ : List[str] = hidden_size a__ : Tuple = num_hidden_layers a__ : Optional[Any] = num_attention_heads a__ : Union[str, Any] = intermediate_size a__ : Optional[int] = hidden_act a__ : Optional[Any] = hidden_dropout_prob a__ : Any = attention_probs_dropout_prob a__ : Any = type_sequence_label_size a__ : Tuple = initializer_range a__ : Tuple = scope a__ : int = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size a__ : Any = (self.image_size // 32) ** 2 a__ : List[Any] = num_patches + 1 def __lowercase ( self) -> Any: '''simple docstring''' a__ : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) a__ : int = None if self.use_labels: a__ : int = ids_tensor([self.batch_size] , self.type_sequence_label_size) a__ : List[str] = self.get_config() return config, pixel_values, labels def __lowercase ( self) -> Dict: '''simple docstring''' a__ : List[str] = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [4, 8, 16, 32], 'num_groups': 2, } return ViTHybridConfig( 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=lowercase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=lowercase , ) def __lowercase ( self , lowercase , lowercase , lowercase) -> List[str]: '''simple docstring''' a__ : List[str] = ViTHybridModel(config=lowercase) model.to(lowercase) model.eval() a__ : Union[str, Any] = model(lowercase) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def __lowercase ( self , lowercase , lowercase , lowercase) -> Union[str, Any]: '''simple docstring''' a__ : Dict = self.type_sequence_label_size a__ : Union[str, Any] = ViTHybridForImageClassification(lowercase) model.to(lowercase) model.eval() a__ : Tuple = model(lowercase , labels=lowercase) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def __lowercase ( self) -> Any: '''simple docstring''' a__ : List[str] = self.prepare_config_and_inputs() a__ , a__ , a__ : Union[str, Any] = config_and_inputs a__ : str = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class A__ ( __UpperCAmelCase , __UpperCAmelCase , unittest.TestCase ): """simple docstring""" __A : Optional[Any] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () __A : List[str] = ( {'''feature-extraction''': ViTHybridModel, '''image-classification''': ViTHybridForImageClassification} if is_torch_available() else {} ) __A : Any = False __A : Optional[int] = False __A : Optional[Any] = False def __lowercase ( self) -> Optional[Any]: '''simple docstring''' a__ : Any = ViTHybridModelTester(self) a__ : Any = ConfigTester(self , config_class=lowercase , has_text_modality=lowercase , hidden_size=37) def __lowercase ( self) -> List[Any]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds') def __lowercase ( self) -> Dict: '''simple docstring''' pass def __lowercase ( self) -> Optional[Any]: '''simple docstring''' a__ , a__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : str = model_class(lowercase) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) a__ : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowercase , nn.Linear)) def __lowercase ( self) -> int: '''simple docstring''' a__ , a__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ : Union[str, Any] = model_class(lowercase) a__ : Union[str, Any] = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ : Optional[Any] = [*signature.parameters.keys()] a__ : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] , lowercase) def __lowercase ( self) -> Any: '''simple docstring''' a__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase) def __lowercase ( self) -> Optional[Any]: '''simple docstring''' a__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowercase) def __lowercase ( self) -> Dict: '''simple docstring''' a__ , a__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a__ : Tuple = _config_zero_init(lowercase) for model_class in self.all_model_classes: a__ : List[Any] = model_class(config=lowercase) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": a__ : Dict = [F'{name}.{key}' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue 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 __lowercase ( self) -> Any: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Optional[Any] = ViTHybridModel.from_pretrained(lowercase) self.assertIsNotNone(lowercase) def A_ ( ) -> int: a__ : Dict = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class A__ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self) -> Optional[Any]: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]) if is_vision_available() else None ) @slow def __lowercase ( self) -> Any: '''simple docstring''' a__ : List[str] = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to( lowercase) a__ : List[str] = self.default_image_processor a__ : List[Any] = prepare_img() a__ : Any = image_processor(images=lowercase , return_tensors='pt').to(lowercase) # forward pass with torch.no_grad(): a__ : Optional[Any] = model(**lowercase) # verify the logits a__ : Optional[Any] = torch.Size((1, 1000)) self.assertEqual(outputs.logits.shape , lowercase) a__ : Any = torch.tensor([-1.90_90, -0.49_93, -0.23_89]).to(lowercase) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowercase , atol=1e-4)) @slow @require_accelerate def __lowercase ( self) -> Optional[int]: '''simple docstring''' a__ : List[str] = ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384') a__ : Union[str, Any] = ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384' , device_map='auto') a__ : Any = prepare_img() a__ : str = image_processor(images=lowercase , return_tensors='pt') a__ : List[Any] = model(**lowercase) a__ : int = outputs.logits # model predicts one of the 1000 ImageNet classes a__ : List[str] = logits.argmax(-1).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , 'tabby, tabby cat')
99
1
from datetime import datetime as dt import os from github import Github UpperCamelCase__ = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def _UpperCamelCase (): """simple docstring""" UpperCamelCase__ = Github(os.environ["""GITHUB_TOKEN"""] ) UpperCamelCase__ = g.get_repo("""huggingface/transformers""" ) UpperCamelCase__ = repo.get_issues(state="""open""" ) for issue in open_issues: UpperCamelCase__ = sorted([comment for comment in issue.get_comments()] , key=lambda a__ : i.created_at , reverse=a__ ) UpperCamelCase__ = comments[0] if len(a__ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="""closed""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
87
import json import os import torch from diffusers import UNetaDModel os.makedirs("hub/hopper-medium-v2/unet/hor32", exist_ok=True) os.makedirs("hub/hopper-medium-v2/unet/hor128", exist_ok=True) os.makedirs("hub/hopper-medium-v2/value_function", exist_ok=True) def _UpperCamelCase (a__ :int ): """simple docstring""" if hor == 128: UpperCamelCase__ = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") UpperCamelCase__ = (32, 128, 256) UpperCamelCase__ = ("""UpResnetBlock1D""", """UpResnetBlock1D""") elif hor == 32: UpperCamelCase__ = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") UpperCamelCase__ = (32, 64, 128, 256) UpperCamelCase__ = ("""UpResnetBlock1D""", """UpResnetBlock1D""", """UpResnetBlock1D""") UpperCamelCase__ = torch.load(f"""/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch""" ) UpperCamelCase__ = model.state_dict() UpperCamelCase__ = { """down_block_types""": down_block_types, """block_out_channels""": block_out_channels, """up_block_types""": up_block_types, """layers_per_block""": 1, """use_timestep_embedding""": True, """out_block_type""": """OutConv1DBlock""", """norm_num_groups""": 8, """downsample_each_block""": False, """in_channels""": 14, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """flip_sin_to_cos""": False, """freq_shift""": 1, """sample_size""": 6_5536, """mid_block_type""": """MidResTemporalBlock1D""", """act_fn""": """mish""", } UpperCamelCase__ = UNetaDModel(**a__ ) print(f"""length of state dict: {len(state_dict.keys() )}""" ) print(f"""length of value function dict: {len(hf_value_function.state_dict().keys() )}""" ) UpperCamelCase__ = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): UpperCamelCase__ = state_dict.pop(a__ ) hf_value_function.load_state_dict(a__ ) torch.save(hf_value_function.state_dict() , f"""hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin""" ) with open(f"""hub/hopper-medium-v2/unet/hor{hor}/config.json""" , """w""" ) as f: json.dump(a__ , a__ ) def _UpperCamelCase (): """simple docstring""" UpperCamelCase__ = { """in_channels""": 14, """down_block_types""": ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D"""), """up_block_types""": (), """out_block_type""": """ValueFunction""", """mid_block_type""": """ValueFunctionMidBlock1D""", """block_out_channels""": (32, 64, 128, 256), """layers_per_block""": 1, """downsample_each_block""": True, """sample_size""": 6_5536, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """use_timestep_embedding""": True, """flip_sin_to_cos""": False, """freq_shift""": 1, """norm_num_groups""": 8, """act_fn""": """mish""", } UpperCamelCase__ = torch.load("""/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch""" ) UpperCamelCase__ = model UpperCamelCase__ = UNetaDModel(**a__ ) print(f"""length of state dict: {len(state_dict.keys() )}""" ) print(f"""length of value function dict: {len(hf_value_function.state_dict().keys() )}""" ) UpperCamelCase__ = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): UpperCamelCase__ = state_dict.pop(a__ ) hf_value_function.load_state_dict(a__ ) torch.save(hf_value_function.state_dict() , """hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin""" ) with open("""hub/hopper-medium-v2/value_function/config.json""" , """w""" ) as f: json.dump(a__ , a__ ) if __name__ == "__main__": unet(32) # unet(128) value_function()
87
1
"""simple docstring""" from __future__ import annotations import typing from collections import Counter def a__ ( __SCREAMING_SNAKE_CASE ) -> typing.Counter[int]: __lowerCAmelCase: typing.Counter[int] = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(__SCREAMING_SNAKE_CASE , max_perimeter + 1 ): __lowerCAmelCase: Union[str, Any] = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(__SCREAMING_SNAKE_CASE ): __lowerCAmelCase: Optional[Any] = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def a__ ( __SCREAMING_SNAKE_CASE = 1_0_0_0 ) -> int: __lowerCAmelCase: str = pythagorean_triple(__SCREAMING_SNAKE_CASE ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(F'''Perimeter {solution()} has maximum solutions''')
217
"""simple docstring""" import json import os import unittest from transformers.models.roc_bert.tokenization_roc_bert import ( VOCAB_FILES_NAMES, RoCBertBasicTokenizer, RoCBertTokenizer, RoCBertWordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class snake_case ( __snake_case, unittest.TestCase ): SCREAMING_SNAKE_CASE_ : List[Any] = RoCBertTokenizer SCREAMING_SNAKE_CASE_ : Any = None SCREAMING_SNAKE_CASE_ : Any = False SCREAMING_SNAKE_CASE_ : Dict = True SCREAMING_SNAKE_CASE_ : Optional[int] = filter_non_english def lowercase_ ( self : Optional[Any])-> Any: '''simple docstring''' super().setUp() __lowerCAmelCase: Optional[Any] = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "你", "好", "是", "谁", "a", "b", "c", "d"] __lowerCAmelCase: List[Any] = {} __lowerCAmelCase: Dict = {} for i, value in enumerate(UpperCamelCase__): __lowerCAmelCase: List[Any] = i __lowerCAmelCase: Union[str, Any] = i __lowerCAmelCase: List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"]) __lowerCAmelCase: int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["word_shape_file"]) __lowerCAmelCase: int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["word_pronunciation_file"]) with open(self.vocab_file , "w" , encoding="utf-8") as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) with open(self.word_shape_file , "w" , encoding="utf-8") as word_shape_writer: json.dump(UpperCamelCase__ , UpperCamelCase__ , ensure_ascii=UpperCamelCase__) with open(self.word_pronunciation_file , "w" , encoding="utf-8") as word_pronunciation_writer: json.dump(UpperCamelCase__ , UpperCamelCase__ , ensure_ascii=UpperCamelCase__) def lowercase_ ( self : Any)-> Tuple: '''simple docstring''' __lowerCAmelCase: Tuple = self.tokenizer_class(self.vocab_file , self.word_shape_file , self.word_pronunciation_file) __lowerCAmelCase: Union[str, Any] = tokenizer.tokenize("你好[SEP]你是谁") self.assertListEqual(UpperCamelCase__ , ["你", "好", "[SEP]", "你", "是", "谁"]) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__) , [5, 6, 2, 5, 7, 8]) self.assertListEqual(tokenizer.convert_tokens_to_shape_ids(UpperCamelCase__) , [5, 6, 2, 5, 7, 8]) self.assertListEqual(tokenizer.convert_tokens_to_pronunciation_ids(UpperCamelCase__) , [5, 6, 2, 5, 7, 8]) def lowercase_ ( self : Optional[Any])-> List[str]: '''simple docstring''' __lowerCAmelCase: int = RoCBertBasicTokenizer() self.assertListEqual(tokenizer.tokenize("ah\u535A\u63A8zz") , ["ah", "\u535A", "\u63A8", "zz"]) def lowercase_ ( self : str)-> Dict: '''simple docstring''' __lowerCAmelCase: int = RoCBertBasicTokenizer(do_lower_case=UpperCamelCase__) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? ") , ["hello", "!", "how", "are", "you", "?"]) self.assertListEqual(tokenizer.tokenize("H\u00E9llo") , ["hello"]) def lowercase_ ( self : Optional[int])-> List[Any]: '''simple docstring''' __lowerCAmelCase: Union[str, Any] = RoCBertBasicTokenizer(do_lower_case=UpperCamelCase__ , strip_accents=UpperCamelCase__) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? ") , ["hällo", "!", "how", "are", "you", "?"]) self.assertListEqual(tokenizer.tokenize("H\u00E9llo") , ["h\u00E9llo"]) def lowercase_ ( self : Optional[Any])-> Any: '''simple docstring''' __lowerCAmelCase: Tuple = RoCBertBasicTokenizer(do_lower_case=UpperCamelCase__ , strip_accents=UpperCamelCase__) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? ") , ["hallo", "!", "how", "are", "you", "?"]) self.assertListEqual(tokenizer.tokenize("H\u00E9llo") , ["hello"]) def lowercase_ ( self : str)-> List[str]: '''simple docstring''' __lowerCAmelCase: List[str] = RoCBertBasicTokenizer(do_lower_case=UpperCamelCase__) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? ") , ["hallo", "!", "how", "are", "you", "?"]) self.assertListEqual(tokenizer.tokenize("H\u00E9llo") , ["hello"]) def lowercase_ ( self : Any)-> Any: '''simple docstring''' __lowerCAmelCase: Union[str, Any] = RoCBertBasicTokenizer(do_lower_case=UpperCamelCase__) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? ") , ["HeLLo", "!", "how", "Are", "yoU", "?"]) def lowercase_ ( self : Optional[int])-> Tuple: '''simple docstring''' __lowerCAmelCase: Optional[int] = RoCBertBasicTokenizer(do_lower_case=UpperCamelCase__ , strip_accents=UpperCamelCase__) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? ") , ["HäLLo", "!", "how", "Are", "yoU", "?"]) def lowercase_ ( self : Optional[Any])-> Tuple: '''simple docstring''' __lowerCAmelCase: Optional[Any] = RoCBertBasicTokenizer(do_lower_case=UpperCamelCase__ , strip_accents=UpperCamelCase__) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? ") , ["HaLLo", "!", "how", "Are", "yoU", "?"]) def lowercase_ ( self : Tuple)-> str: '''simple docstring''' __lowerCAmelCase: Optional[Any] = RoCBertBasicTokenizer(do_lower_case=UpperCamelCase__ , never_split=["[UNK]"]) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? [UNK]") , ["HeLLo", "!", "how", "Are", "yoU", "?", "[UNK]"]) def lowercase_ ( self : List[Any])-> Any: '''simple docstring''' __lowerCAmelCase: List[str] = ["[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing"] __lowerCAmelCase: int = {} for i, token in enumerate(UpperCamelCase__): __lowerCAmelCase: Optional[Any] = i __lowerCAmelCase: str = RoCBertWordpieceTokenizer(vocab=UpperCamelCase__ , 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"]) def lowercase_ ( self : Optional[Any])-> Dict: '''simple docstring''' 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 lowercase_ ( self : Dict)-> Optional[int]: '''simple docstring''' 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 lowercase_ ( self : Union[str, Any])-> str: '''simple docstring''' 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(" ")) def lowercase_ ( self : Dict)-> int: '''simple docstring''' __lowerCAmelCase: Any = self.get_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(UpperCamelCase__) for t in ["Test", "\xad", "test"]] , [["[UNK]"], [], ["[UNK]"]]) if self.test_rust_tokenizer: __lowerCAmelCase: Any = self.get_rust_tokenizer() self.assertListEqual( [rust_tokenizer.tokenize(UpperCamelCase__) for t in ["Test", "\xad", "test"]] , [["[UNK]"], [], ["[UNK]"]]) def lowercase_ ( self : Dict)-> Any: '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"): __lowerCAmelCase: str = self.rust_tokenizer_class.from_pretrained(UpperCamelCase__ , **UpperCamelCase__) __lowerCAmelCase: Optional[Any] = f"A, naïve {tokenizer_r.mask_token} AllenNLP sentence." __lowerCAmelCase: Tuple = tokenizer_r.encode_plus( UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , return_token_type_ids=UpperCamelCase__ , return_offsets_mapping=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , ) __lowerCAmelCase: str = tokenizer_r.do_lower_case if hasattr(UpperCamelCase__ , "do_lower_case") else False __lowerCAmelCase: List[Any] = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), "A"), ((1, 2), ","), ((3, 5), "na"), ((5, 6), "##ï"), ((6, 8), "##ve"), ((9, 1_5), tokenizer_r.mask_token), ((1_6, 2_1), "Allen"), ((2_1, 2_3), "##NL"), ((2_3, 2_4), "##P"), ((2_5, 3_3), "sentence"), ((3_3, 3_4), "."), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), "a"), ((1, 2), ","), ((3, 8), "naive"), ((9, 1_5), tokenizer_r.mask_token), ((1_6, 2_1), "allen"), ((2_1, 2_3), "##nl"), ((2_3, 2_4), "##p"), ((2_5, 3_3), "sentence"), ((3_3, 3_4), "."), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens["input_ids"])) self.assertEqual([e[0] for e in expected_results] , tokens["offset_mapping"]) def lowercase_ ( self : Union[str, Any])-> Optional[int]: '''simple docstring''' __lowerCAmelCase: Optional[Any] = ["的", "人", "有"] __lowerCAmelCase: int = "".join(UpperCamelCase__) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"): __lowerCAmelCase: Tuple = True __lowerCAmelCase: str = self.tokenizer_class.from_pretrained(UpperCamelCase__ , **UpperCamelCase__) __lowerCAmelCase: Dict = self.rust_tokenizer_class.from_pretrained(UpperCamelCase__ , **UpperCamelCase__) __lowerCAmelCase: Union[str, Any] = tokenizer_p.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__) __lowerCAmelCase: List[Any] = tokenizer_r.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__) __lowerCAmelCase: Any = tokenizer_r.convert_ids_to_tokens(UpperCamelCase__) __lowerCAmelCase: List[str] = tokenizer_p.convert_ids_to_tokens(UpperCamelCase__) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(UpperCamelCase__ , UpperCamelCase__) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__) __lowerCAmelCase: int = False __lowerCAmelCase: Any = self.rust_tokenizer_class.from_pretrained(UpperCamelCase__ , **UpperCamelCase__) __lowerCAmelCase: Union[str, Any] = self.tokenizer_class.from_pretrained(UpperCamelCase__ , **UpperCamelCase__) __lowerCAmelCase: str = tokenizer_r.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__) __lowerCAmelCase: str = tokenizer_p.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__) __lowerCAmelCase: str = tokenizer_r.convert_ids_to_tokens(UpperCamelCase__) __lowerCAmelCase: Tuple = tokenizer_p.convert_ids_to_tokens(UpperCamelCase__) # it is expected that only the first Chinese character is not preceded by "##". __lowerCAmelCase: Dict = [ f"##{token}" if idx != 0 else token for idx, token in enumerate(UpperCamelCase__) ] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__) @slow def lowercase_ ( self : Optional[Any])-> Any: '''simple docstring''' __lowerCAmelCase: str = self.tokenizer_class(self.vocab_file , self.word_shape_file , self.word_pronunciation_file) __lowerCAmelCase: Dict = tokenizer.encode("你好" , add_special_tokens=UpperCamelCase__) __lowerCAmelCase: Union[str, Any] = tokenizer.encode("你是谁" , add_special_tokens=UpperCamelCase__) __lowerCAmelCase: Tuple = tokenizer.build_inputs_with_special_tokens(UpperCamelCase__) __lowerCAmelCase: List[Any] = tokenizer.build_inputs_with_special_tokens(UpperCamelCase__ , UpperCamelCase__) assert encoded_sentence == [1] + text + [2] assert encoded_pair == [1] + text + [2] + text_a + [2] def lowercase_ ( self : Tuple)-> Optional[Any]: '''simple docstring''' __lowerCAmelCase: int = self.get_tokenizers(do_lower_case=UpperCamelCase__) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}"): __lowerCAmelCase: str = "你好,你是谁" __lowerCAmelCase: Dict = tokenizer.tokenize(UpperCamelCase__) __lowerCAmelCase: Union[str, Any] = tokenizer.convert_tokens_to_ids(UpperCamelCase__) __lowerCAmelCase: Optional[Any] = tokenizer.convert_tokens_to_shape_ids(UpperCamelCase__) __lowerCAmelCase: Tuple = tokenizer.convert_tokens_to_pronunciation_ids(UpperCamelCase__) __lowerCAmelCase: Dict = tokenizer.prepare_for_model( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , add_special_tokens=UpperCamelCase__) __lowerCAmelCase: Optional[Any] = tokenizer.encode_plus(UpperCamelCase__ , add_special_tokens=UpperCamelCase__) self.assertEqual(UpperCamelCase__ , UpperCamelCase__)
217
1
"""simple docstring""" from math import factorial _a = {str(digit): factorial(digit) for digit in range(10)} def lowerCamelCase__ ( __snake_case ) -> str: """simple docstring""" if not isinstance(A_, A_ ): raise TypeError('''Parameter number must be int''' ) if number < 0: raise ValueError('''Parameter number must be greater than or equal to 0''' ) # Converts number in string to iterate on its digits and adds its factorial. return sum(DIGIT_FACTORIAL[digit] for digit in str(A_ ) ) def lowerCamelCase__ ( __snake_case = 60, __snake_case = 1_00_00_00 ) -> List[Any]: """simple docstring""" if not isinstance(A_, A_ ) or not isinstance(A_, A_ ): raise TypeError('''Parameters chain_length and number_limit must be int''' ) if chain_length <= 0 or number_limit <= 0: raise ValueError( '''Parameters chain_length and number_limit must be greater than 0''' ) # the counter for the chains with the exact desired length _UpperCamelCase = 0 # the cached sizes of the previous chains _UpperCamelCase = {} for start_chain_element in range(1, A_ ): # The temporary set will contain the elements of the chain _UpperCamelCase = set() _UpperCamelCase = 0 # Stop computing the chain when you find a cached size, a repeating item or the # length is greater then the desired one. _UpperCamelCase = start_chain_element while ( chain_element not in chain_sets_lengths and chain_element not in chain_set and chain_set_length <= chain_length ): chain_set.add(A_ ) chain_set_length += 1 _UpperCamelCase = digit_factorial_sum(A_ ) if chain_element in chain_sets_lengths: chain_set_length += chain_sets_lengths[chain_element] _UpperCamelCase = chain_set_length # If chain contains the exact amount of elements increase the counter if chain_set_length == chain_length: chains_counter += 1 return chains_counter if __name__ == "__main__": import doctest doctest.testmod() print(F"""{solution()}""")
358
"""simple docstring""" import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( """The `inpainting.py` script is outdated. Please use directly `from diffusers import""" """ StableDiffusionInpaintPipeline` instead.""" )
100
0
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): raise ValueError('''check_bouncy() accepts only integer arguments''' ) snake_case_ = str(SCREAMING_SNAKE_CASE__ ) snake_case_ = ''''''.join(sorted(SCREAMING_SNAKE_CASE__ ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = 99 ): if not 0 < percent < 100: raise ValueError('''solution() only accepts values from 0 to 100''' ) snake_case_ = 0 snake_case_ = 1 while True: if check_bouncy(SCREAMING_SNAKE_CASE__ ): bouncy_num += 1 if (bouncy_num / num) * 100 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(99)}""")
8
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class __lowerCAmelCase ( _a ): lowerCamelCase_ : int = '''''' lowerCamelCase_ : str = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) lowerCamelCase_ : str = None # compression type in fsspec. ex: "gzip" lowerCamelCase_ : str = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__(self , __magic_name__ = "" , __magic_name__ = None , __magic_name__ = None , **__magic_name__ ) -> Any: '''simple docstring''' super().__init__(self , **__magic_name__ ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode snake_case_ : Union[str, Any] = fsspec.open( __magic_name__ , mode='''rb''' , protocol=__magic_name__ , compression=self.compression , client_kwargs={ '''requote_redirect_url''': False, # see https://github.com/huggingface/datasets/pull/5459 '''trust_env''': True, # Enable reading proxy env variables. **(target_options or {}).pop('''client_kwargs''' , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) snake_case_ : Tuple = os.path.basename(self.file.path.split('''::''' )[0] ) snake_case_ : Optional[Any] = ( self.compressed_name[: self.compressed_name.rindex('''.''' )] if '''.''' in self.compressed_name else self.compressed_name ) snake_case_ : Dict = None @classmethod def lowerCamelCase (cls , __magic_name__ ) -> Optional[int]: '''simple docstring''' return super()._strip_protocol(__magic_name__ ).lstrip('''/''' ) def lowerCamelCase (self ) -> Union[str, Any]: '''simple docstring''' if self.dir_cache is None: snake_case_ : Optional[int] = {**self.file.fs.info(self.file.path ), '''name''': self.uncompressed_name} snake_case_ : List[str] = {f['''name''']: f} def lowerCamelCase (self , __magic_name__ ) -> Optional[Any]: '''simple docstring''' return self.file.open().read() def lowerCamelCase (self , __magic_name__ , __magic_name__ = "rb" , __magic_name__=None , __magic_name__=True , __magic_name__=None , **__magic_name__ , ) -> int: '''simple docstring''' snake_case_ : Union[str, Any] = self._strip_protocol(__magic_name__ ) if mode != "rb": raise ValueError(F'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'''' ) return self.file.open() class __lowerCAmelCase ( _a ): lowerCamelCase_ : Union[str, Any] = '''bz2''' lowerCamelCase_ : Any = '''bz2''' lowerCamelCase_ : int = '''.bz2''' class __lowerCAmelCase ( _a ): lowerCamelCase_ : Union[str, Any] = '''gzip''' lowerCamelCase_ : Dict = '''gzip''' lowerCamelCase_ : int = '''.gz''' class __lowerCAmelCase ( _a ): lowerCamelCase_ : Any = '''lz4''' lowerCamelCase_ : Any = '''lz4''' lowerCamelCase_ : Optional[Any] = '''.lz4''' class __lowerCAmelCase ( _a ): lowerCamelCase_ : Tuple = '''xz''' lowerCamelCase_ : Any = '''xz''' lowerCamelCase_ : int = '''.xz''' class __lowerCAmelCase ( _a ): lowerCamelCase_ : Union[str, Any] = '''zstd''' lowerCamelCase_ : Tuple = '''zstd''' lowerCamelCase_ : Any = '''.zst''' def __init__(self , __magic_name__ , __magic_name__ = "rb" , __magic_name__ = None , __magic_name__ = None , __magic_name__ = DEFAULT_BLOCK_SIZE , **__magic_name__ , ) -> Tuple: '''simple docstring''' super().__init__( fo=__magic_name__ , mode=__magic_name__ , target_protocol=__magic_name__ , target_options=__magic_name__ , block_size=__magic_name__ , **__magic_name__ , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 snake_case_ : Dict = self.file.__enter__ class __lowerCAmelCase : def __init__(self , __magic_name__ ) -> List[Any]: '''simple docstring''' snake_case_ : str = file_ def __enter__(self ) -> List[Any]: '''simple docstring''' self._file.__enter__() return self def __exit__(self , *__magic_name__ , **__magic_name__ ) -> int: '''simple docstring''' self._file.__exit__(*__magic_name__ , **__magic_name__ ) def __iter__(self ) -> Optional[int]: '''simple docstring''' return iter(self._file ) def lowerCamelCase (self ) -> Union[str, Any]: '''simple docstring''' return next(self._file ) def __getattr__(self , __magic_name__ ) -> str: '''simple docstring''' return getattr(self._file , __magic_name__ ) def fixed_enter(*__magic_name__ , **__magic_name__ ): return WrappedFile(_enter(*__magic_name__ , **__magic_name__ ) ) snake_case_ : Tuple = fixed_enter
279
0
from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge _UpperCAmelCase : List[Any] = [ """Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of the""" """ final seconds on board Flight 9525. The Germanwings co-pilot says he had a \"previous episode of severe""" """ depression\" German airline confirms it knew of Andreas Lubitz's depression years before he took control.""", """The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal""" """ accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC's""" """ founding Rome Statute in January. Israel and the United States opposed the Palestinians' efforts to join the""" """ body.""", """Amnesty International releases its annual report on the death penalty. The report catalogs the use of""" """ state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the""" """ world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital""" """ punishment.""", ] _UpperCAmelCase : Dict = [ """Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports .""" """ Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz""" """ had informed his Lufthansa training school of an episode of severe depression, airline says .""", """Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June .""" """ Israel and the United States opposed the move, which could open the door to war crimes investigations against""" """ Israelis .""", """Amnesty's annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to""" """ death . Organization claims that governments around the world are using the threat of terrorism to advance""" """ executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death""" """ sentences up by 28% .""", ] def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , bootstrap_aggregation=UpperCamelCase__ , rouge_keys=['rouge2', 'rougeL'] ) assert isinstance(UpperCamelCase__ , UpperCamelCase__ ) snake_case_ = calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , bootstrap_aggregation=UpperCamelCase__ , rouge_keys=['rouge2'] ) assert ( pd.DataFrame(no_aggregation['rouge2'] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra['rouge2'] ).fmeasure.mean() ) def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = 'rougeLsum' snake_case_ = calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , newline_sep=UpperCamelCase__ , rouge_keys=[k] )[k] snake_case_ = calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , newline_sep=UpperCamelCase__ , rouge_keys=[k] )[k] assert score > score_no_sep def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = ['rouge1', 'rouge2', 'rougeL'] snake_case_ = calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , newline_sep=UpperCamelCase__ , rouge_keys=UpperCamelCase__ ) snake_case_ = calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , newline_sep=UpperCamelCase__ , rouge_keys=UpperCamelCase__ ) assert score_sep == score_no_sep def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = [ 'Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.', 'Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .', ] snake_case_ = [ 'Margot Frank, died in 1945, a month earlier than previously thought.', 'Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of' ' the final seconds on board Flight 9525.', ] assert calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , newline_sep=UpperCamelCase__ ) == calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , newline_sep=UpperCamelCase__ ) def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = [ '" "a person who has such a video needs to immediately give it to the investigators," prosecutor says .<n> "it is a very disturbing scene," editor-in-chief of bild online tells "erin burnett: outfront" ' ] snake_case_ = [ ' Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports . Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .' ] snake_case_ = calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , rouge_keys=['rougeLsum'] , newline_sep=UpperCamelCase__ )['rougeLsum'] snake_case_ = calculate_rouge(UpperCamelCase__ , UpperCamelCase__ , rouge_keys=['rougeLsum'] )['rougeLsum'] assert new_score > prev_score def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = Path('examples/seq2seq/test_data/wmt_en_ro' ) snake_case_ = calculate_rouge_path(data_dir.joinpath('test.source' ) , data_dir.joinpath('test.target' ) ) assert isinstance(UpperCamelCase__ , UpperCamelCase__ ) snake_case_ = calculate_rouge_path( data_dir.joinpath('test.source' ) , data_dir.joinpath('test.target' ) , bootstrap_aggregation=UpperCamelCase__ ) assert isinstance(UpperCamelCase__ , UpperCamelCase__ )
200
def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' return int((input_a, input_a).count(0 ) != 0 ) def __lowerCamelCase ( ): '''simple docstring''' assert nand_gate(0 , 0 ) == 1 assert nand_gate(0 , 1 ) == 1 assert nand_gate(1 , 0 ) == 1 assert nand_gate(1 , 1 ) == 0 if __name__ == "__main__": print(nand_gate(0, 0)) print(nand_gate(0, 1)) print(nand_gate(1, 0)) print(nand_gate(1, 1))
200
1
import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> List[Any]: monkeypatch.setattr('datasets.utils.deprecation_utils._emitted_deprecation_warnings' , set() ) @pytest.fixture def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> List[str]: class A_ : def __init__( self : Dict ,SCREAMING_SNAKE_CASE__ : Any): __lowerCamelCase : Union[str, Any] = metric_id class A_ : _UpperCAmelCase : Optional[Any] = [MetricMock(SCREAMING_SNAKE_CASE ) for metric_id in ['''accuracy''', '''mse''', '''precision''', '''codeparrot/apps_metric''']] def lowerCAmelCase ( self : Dict): return self._metrics monkeypatch.setattr('datasets.inspect.huggingface_hub' , HfhMock() ) @pytest.mark.parametrize( 'func, args' , [(load_metric, ('metrics/mse',)), (list_metrics, ()), (inspect_metric, ('metrics/mse', 'tmp_path'))] ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> str: if "tmp_path" in args: __lowerCamelCase : Any = tuple(arg if arg != 'tmp_path' else tmp_path for arg in args ) with pytest.warns(lowerCamelCase__ , match='https://huggingface.co/docs/evaluate' ): func(*lowerCamelCase__ )
73
from math import isclose, sqrt def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> tuple[float, float, float]: __lowerCamelCase : Tuple = point_y / 4 / point_x __lowerCamelCase : Tuple = 2 * normal_gradient / (1 + normal_gradient * normal_gradient) __lowerCamelCase : List[Any] = (1 - normal_gradient * normal_gradient) / ( 1 + normal_gradient * normal_gradient ) __lowerCamelCase : int = (sa - ca * incoming_gradient) / (ca + sa * incoming_gradient) # to find the next point, solve the simultaeneous equations: # y^2 + 4x^2 = 100 # y - b = m * (x - a) # ==> A x^2 + B x + C = 0 __lowerCamelCase : Any = outgoing_gradient**2 + 4 __lowerCamelCase : Optional[int] = 2 * outgoing_gradient * (point_y - outgoing_gradient * point_x) __lowerCamelCase : str = (point_y - outgoing_gradient * point_x) ** 2 - 1_0_0 __lowerCamelCase : str = ( -linear_term - sqrt(linear_term**2 - 4 * quadratic_term * constant_term ) ) / (2 * quadratic_term) __lowerCamelCase : Optional[Any] = ( -linear_term + sqrt(linear_term**2 - 4 * quadratic_term * constant_term ) ) / (2 * quadratic_term) # two solutions, one of which is our input point __lowerCamelCase : Optional[Any] = x_minus if isclose(lowerCamelCase__ , lowerCamelCase__ ) else x_plus __lowerCamelCase : Tuple = point_y + outgoing_gradient * (next_x - point_x) return next_x, next_y, outgoing_gradient def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ = 1.4 , lowerCamelCase__ = -9.6 ) -> int: __lowerCamelCase : int = 0 __lowerCamelCase : float = first_x_coord __lowerCamelCase : float = first_y_coord __lowerCamelCase : float = (10.1 - point_y) / (0.0 - point_x) while not (-0.01 <= point_x <= 0.01 and point_y > 0): __lowerCamelCase , __lowerCamelCase , __lowerCamelCase : Any = next_point(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) num_reflections += 1 return num_reflections if __name__ == "__main__": print(F"""{solution() = }""")
73
1
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging _lowerCamelCase : List[Any] = logging.get_logger(__name__) _lowerCamelCase : int = { '''Helsinki-NLP/opus-mt-en-de''': '''https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json''', # See all Marian models at https://huggingface.co/models?filter=marian } class lowerCamelCase (__lowerCamelCase ): """simple docstring""" UpperCAmelCase_ = "marian" UpperCAmelCase_ = ["past_key_values"] UpperCAmelCase_ = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : int, _UpperCAmelCase : str=5_8_1_0_1, _UpperCAmelCase : Dict=None, _UpperCAmelCase : Optional[Any]=1_0_2_4, _UpperCAmelCase : Dict=1_2, _UpperCAmelCase : str=4_0_9_6, _UpperCAmelCase : List[str]=1_6, _UpperCAmelCase : str=1_2, _UpperCAmelCase : List[str]=4_0_9_6, _UpperCAmelCase : Union[str, Any]=1_6, _UpperCAmelCase : int=0.0, _UpperCAmelCase : int=0.0, _UpperCAmelCase : str=True, _UpperCAmelCase : List[str]=True, _UpperCAmelCase : Optional[Any]="gelu", _UpperCAmelCase : int=1_0_2_4, _UpperCAmelCase : int=0.1, _UpperCAmelCase : Any=0.0, _UpperCAmelCase : str=0.0, _UpperCAmelCase : Optional[Any]=0.02, _UpperCAmelCase : List[str]=5_8_1_0_0, _UpperCAmelCase : int=False, _UpperCAmelCase : List[str]=5_8_1_0_0, _UpperCAmelCase : Any=0, _UpperCAmelCase : int=0, _UpperCAmelCase : Optional[Any]=True, **_UpperCAmelCase : Optional[int], ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = vocab_size SCREAMING_SNAKE_CASE__ : str = decoder_vocab_size or vocab_size SCREAMING_SNAKE_CASE__ : str = max_position_embeddings SCREAMING_SNAKE_CASE__ : Optional[Any] = d_model SCREAMING_SNAKE_CASE__ : Union[str, Any] = encoder_ffn_dim SCREAMING_SNAKE_CASE__ : Union[str, Any] = encoder_layers SCREAMING_SNAKE_CASE__ : Tuple = encoder_attention_heads SCREAMING_SNAKE_CASE__ : List[str] = decoder_ffn_dim SCREAMING_SNAKE_CASE__ : Tuple = decoder_layers SCREAMING_SNAKE_CASE__ : Optional[int] = decoder_attention_heads SCREAMING_SNAKE_CASE__ : Union[str, Any] = dropout SCREAMING_SNAKE_CASE__ : List[Any] = attention_dropout SCREAMING_SNAKE_CASE__ : int = activation_dropout SCREAMING_SNAKE_CASE__ : List[str] = activation_function SCREAMING_SNAKE_CASE__ : List[Any] = init_std SCREAMING_SNAKE_CASE__ : Optional[int] = encoder_layerdrop SCREAMING_SNAKE_CASE__ : str = decoder_layerdrop SCREAMING_SNAKE_CASE__ : Union[str, Any] = use_cache SCREAMING_SNAKE_CASE__ : Optional[Any] = encoder_layers SCREAMING_SNAKE_CASE__ : str = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE__ : Union[str, Any] = share_encoder_decoder_embeddings super().__init__( pad_token_id=_UpperCAmelCase, eos_token_id=_UpperCAmelCase, is_encoder_decoder=_UpperCAmelCase, decoder_start_token_id=_UpperCAmelCase, forced_eos_token_id=_UpperCAmelCase, **_UpperCAmelCase, ) class lowerCamelCase (__lowerCamelCase ): """simple docstring""" @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs def A_ ( self : Any ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE__ : Optional[int] = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE__ : int = {0: "batch"} SCREAMING_SNAKE_CASE__ : Union[str, Any] = {0: "batch", 1: "past_decoder_sequence + sequence"} else: SCREAMING_SNAKE_CASE__ : List[Any] = {0: "batch", 1: "decoder_sequence"} SCREAMING_SNAKE_CASE__ : Optional[Any] = {0: "batch", 1: "decoder_sequence"} if self.use_past: self.fill_with_past_key_values_(_UpperCAmelCase, direction="inputs" ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE__ : Optional[int] = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE__ : Tuple = self.num_layers for i in range(_UpperCAmelCase ): SCREAMING_SNAKE_CASE__ : List[str] = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE__ : Union[str, Any] = {0: "batch", 2: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE__ : Any = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}), ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}), ] ) return common_inputs @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs def A_ ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE__ : List[str] = super().outputs else: SCREAMING_SNAKE_CASE__ : Optional[Any] = super(_UpperCAmelCase, self ).outputs if self.use_past: SCREAMING_SNAKE_CASE__ : Tuple = self.num_layers for i in range(_UpperCAmelCase ): SCREAMING_SNAKE_CASE__ : Optional[int] = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE__ : Any = {0: "batch", 2: "past_sequence + sequence"} return common_outputs def A_ ( self : Tuple, _UpperCAmelCase : PreTrainedTokenizer, _UpperCAmelCase : int = -1, _UpperCAmelCase : int = -1, _UpperCAmelCase : bool = False, _UpperCAmelCase : Optional[TensorType] = None, ) -> Mapping[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = self._generate_dummy_inputs_for_encoder_and_decoder( _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase ) # Generate decoder inputs SCREAMING_SNAKE_CASE__ : Any = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE__ : int = self._generate_dummy_inputs_for_encoder_and_decoder( _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : str = {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE__ : Dict = dict(**_UpperCAmelCase, **_UpperCAmelCase ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE__ : Union[str, Any] = common_inputs["input_ids"].shape SCREAMING_SNAKE_CASE__ : Union[str, Any] = common_inputs["decoder_input_ids"].shape[1] SCREAMING_SNAKE_CASE__ : Optional[int] = self.num_attention_heads SCREAMING_SNAKE_CASE__ : str = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE__ : str = decoder_seq_length + 3 SCREAMING_SNAKE_CASE__ : str = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = torch.cat( [common_inputs["decoder_attention_mask"], torch.ones(_UpperCAmelCase, _UpperCAmelCase )], dim=1 ) SCREAMING_SNAKE_CASE__ : Any = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE__ : Tuple = self.num_layers SCREAMING_SNAKE_CASE__ : Any = min(_UpperCAmelCase, _UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : Any = max(_UpperCAmelCase, _UpperCAmelCase ) - min_num_layers SCREAMING_SNAKE_CASE__ : Optional[int] = "encoder" if num_encoder_layers > num_decoder_layers else "decoder" for _ in range(_UpperCAmelCase ): common_inputs["past_key_values"].append( ( torch.zeros(_UpperCAmelCase ), torch.zeros(_UpperCAmelCase ), torch.zeros(_UpperCAmelCase ), torch.zeros(_UpperCAmelCase ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE__ : Union[str, Any] = encoder_shape if remaining_side_name == "encoder" else decoder_shape for _ in range(_UpperCAmelCase, _UpperCAmelCase ): common_inputs["past_key_values"].append((torch.zeros(_UpperCAmelCase ), torch.zeros(_UpperCAmelCase )) ) return common_inputs def A_ ( self : List[Any], _UpperCAmelCase : PreTrainedTokenizer, _UpperCAmelCase : int = -1, _UpperCAmelCase : int = -1, _UpperCAmelCase : bool = False, _UpperCAmelCase : Optional[TensorType] = None, ) -> Mapping[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = self._generate_dummy_inputs_for_encoder_and_decoder( _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE__ : Tuple = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE__ : str = seqlen + 2 SCREAMING_SNAKE_CASE__ : int = self.num_layers SCREAMING_SNAKE_CASE__ : List[Any] = self.num_attention_heads SCREAMING_SNAKE_CASE__ : List[Any] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE__ : Any = common_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE__ : int = torch.cat( [common_inputs["attention_mask"], torch.ones(_UpperCAmelCase, _UpperCAmelCase, dtype=_UpperCAmelCase )], dim=1 ) SCREAMING_SNAKE_CASE__ : List[str] = [ (torch.zeros(_UpperCAmelCase ), torch.zeros(_UpperCAmelCase )) for _ in range(_UpperCAmelCase ) ] return common_inputs def A_ ( self : Dict, _UpperCAmelCase : PreTrainedTokenizer, _UpperCAmelCase : int = -1, _UpperCAmelCase : int = -1, _UpperCAmelCase : bool = False, _UpperCAmelCase : Optional[TensorType] = None, ) -> Mapping[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = compute_effective_axis_dimension( _UpperCAmelCase, fixed_dimension=OnnxConfig.default_fixed_batch, num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE__ : Dict = tokenizer.num_special_tokens_to_add(_UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : List[str] = compute_effective_axis_dimension( _UpperCAmelCase, fixed_dimension=OnnxConfig.default_fixed_sequence, num_token_to_add=_UpperCAmelCase ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE__ : List[Any] = [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE__ : List[str] = dict(tokenizer(_UpperCAmelCase, return_tensors=_UpperCAmelCase ) ) return common_inputs def A_ ( self : Optional[int], _UpperCAmelCase : PreTrainedTokenizer, _UpperCAmelCase : int = -1, _UpperCAmelCase : int = -1, _UpperCAmelCase : bool = False, _UpperCAmelCase : Optional[TensorType] = None, ) -> Mapping[str, Any]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE__ : str = self._generate_dummy_inputs_for_default_and_seqaseq_lm( _UpperCAmelCase, batch_size=_UpperCAmelCase, seq_length=_UpperCAmelCase, is_pair=_UpperCAmelCase, framework=_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE__ : Any = self._generate_dummy_inputs_for_causal_lm( _UpperCAmelCase, batch_size=_UpperCAmelCase, seq_length=_UpperCAmelCase, is_pair=_UpperCAmelCase, framework=_UpperCAmelCase ) return common_inputs def A_ ( self : Optional[Any], _UpperCAmelCase : Any, _UpperCAmelCase : Any, _UpperCAmelCase : Union[str, Any], _UpperCAmelCase : List[Any] ) -> Optional[int]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE__ : Optional[Any] = super()._flatten_past_key_values_(_UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase ) else: SCREAMING_SNAKE_CASE__ : Union[str, Any] = super(_UpperCAmelCase, self )._flatten_past_key_values_( _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase, _UpperCAmelCase ) @property def A_ ( self : Any ) -> float: """simple docstring""" return 1E-4
370
def _a ( SCREAMING_SNAKE_CASE__ : int = 50_00_00_00 ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = set() SCREAMING_SNAKE_CASE__ : Dict = int((limit - 24) ** (1 / 2) ) SCREAMING_SNAKE_CASE__ : Any = set(range(3 , prime_square_limit + 1 , 2 ) ) primes.add(2 ) for p in range(3 , prime_square_limit + 1 , 2 ): if p not in primes: continue primes.difference_update(set(range(p * p , prime_square_limit + 1 , SCREAMING_SNAKE_CASE__ ) ) ) for primea in primes: SCREAMING_SNAKE_CASE__ : Optional[int] = primea * primea for primea in primes: SCREAMING_SNAKE_CASE__ : Union[str, Any] = primea * primea * primea if square + cube >= limit - 16: break for primea in primes: SCREAMING_SNAKE_CASE__ : List[str] = primea * primea * primea * primea SCREAMING_SNAKE_CASE__ : Optional[int] = square + cube + tetr if total >= limit: break ret.add(SCREAMING_SNAKE_CASE__ ) return len(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": print(f"{solution() = }")
191
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_dpt import DPTImageProcessor _UpperCamelCase : int = logging.get_logger(__name__) class a ( a_ ): def __init__( self , *_lowerCamelCase , **_lowerCamelCase ): warnings.warn( 'The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use DPTImageProcessor instead.' , _lowerCamelCase , ) super().__init__(*_lowerCamelCase , **_lowerCamelCase )
220
"""simple docstring""" import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class a ( unittest.TestCase ): def __init__( self , _lowerCamelCase , _lowerCamelCase=7 , _lowerCamelCase=3 , _lowerCamelCase=1_8 , _lowerCamelCase=3_0 , _lowerCamelCase=4_0_0 , _lowerCamelCase=True , _lowerCamelCase=None , _lowerCamelCase=True , _lowerCamelCase=[0.5, 0.5, 0.5] , _lowerCamelCase=[0.5, 0.5, 0.5] , ): lowercase = size if size is not None else {'height': 1_8, 'width': 1_8} lowercase = parent lowercase = batch_size lowercase = num_channels lowercase = image_size lowercase = min_resolution lowercase = max_resolution lowercase = do_resize lowercase = size lowercase = do_normalize lowercase = image_mean lowercase = image_std def UpperCamelCase_ ( self ): return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class a ( a_, unittest.TestCase ): UpperCAmelCase_ : Optional[int] =DPTImageProcessor if is_vision_available() else None def UpperCamelCase_ ( self ): lowercase = DPTImageProcessingTester(self ) @property def UpperCamelCase_ ( self ): return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase_ ( self ): lowercase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_lowerCamelCase , 'image_mean' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'image_std' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'do_normalize' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'do_resize' ) ) self.assertTrue(hasattr(_lowerCamelCase , 'size' ) ) def UpperCamelCase_ ( self ): lowercase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 1_8, 'width': 1_8} ) lowercase = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 ) self.assertEqual(image_processor.size , {'height': 4_2, 'width': 4_2} ) def UpperCamelCase_ ( self ): # Initialize image_processing lowercase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase , Image.Image ) # Test not batched input lowercase = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase = image_processing(_lowerCamelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def UpperCamelCase_ ( self ): # Initialize image_processing lowercase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCamelCase , numpify=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase , np.ndarray ) # Test not batched input lowercase = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase = image_processing(_lowerCamelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def UpperCamelCase_ ( self ): # Initialize image_processing lowercase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCamelCase , torchify=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase , torch.Tensor ) # Test not batched input lowercase = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched lowercase = image_processing(_lowerCamelCase , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , )
220
1
"""simple docstring""" import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import SegformerImageProcessor, SwinConfig, UperNetConfig, UperNetForSemanticSegmentation def lowerCamelCase_( _lowerCamelCase ) -> str: '''simple docstring''' _lowerCamelCase : Union[str, Any] = 384 _lowerCamelCase : List[str] = 7 if "tiny" in model_name: _lowerCamelCase : Optional[Any] = 96 _lowerCamelCase : int = (2, 2, 6, 2) _lowerCamelCase : Dict = (3, 6, 12, 24) elif "small" in model_name: _lowerCamelCase : List[Any] = 96 _lowerCamelCase : int = (2, 2, 18, 2) _lowerCamelCase : Tuple = (3, 6, 12, 24) elif "base" in model_name: _lowerCamelCase : List[Any] = 128 _lowerCamelCase : Optional[int] = (2, 2, 18, 2) _lowerCamelCase : str = (4, 8, 16, 32) _lowerCamelCase : Union[str, Any] = 12 _lowerCamelCase : Union[str, Any] = 512 elif "large" in model_name: _lowerCamelCase : List[Any] = 192 _lowerCamelCase : List[Any] = (2, 2, 18, 2) _lowerCamelCase : Tuple = (6, 12, 24, 48) _lowerCamelCase : Union[str, Any] = 12 _lowerCamelCase : Dict = 768 # set label information _lowerCamelCase : int = 150 _lowerCamelCase : List[Any] = "huggingface/label-files" _lowerCamelCase : Tuple = "ade20k-id2label.json" _lowerCamelCase : List[str] = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type="dataset" ) , "r" ) ) _lowerCamelCase : str = {int(_lowerCamelCase ): v for k, v in idalabel.items()} _lowerCamelCase : Tuple = {v: k for k, v in idalabel.items()} _lowerCamelCase : Optional[Any] = SwinConfig( embed_dim=_lowerCamelCase , depths=_lowerCamelCase , num_heads=_lowerCamelCase , window_size=_lowerCamelCase , out_features=["stage1", "stage2", "stage3", "stage4"] , ) _lowerCamelCase : List[Any] = UperNetConfig( backbone_config=_lowerCamelCase , auxiliary_in_channels=_lowerCamelCase , num_labels=_lowerCamelCase , idalabel=_lowerCamelCase , labelaid=_lowerCamelCase , ) return config def lowerCamelCase_( _lowerCamelCase ) -> Optional[Any]: '''simple docstring''' _lowerCamelCase : Optional[Any] = [] # fmt: off # stem rename_keys.append(("backbone.patch_embed.projection.weight", "backbone.embeddings.patch_embeddings.projection.weight") ) rename_keys.append(("backbone.patch_embed.projection.bias", "backbone.embeddings.patch_embeddings.projection.bias") ) rename_keys.append(("backbone.patch_embed.norm.weight", "backbone.embeddings.norm.weight") ) rename_keys.append(("backbone.patch_embed.norm.bias", "backbone.embeddings.norm.bias") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.norm1.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.norm1.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_bias_table""", F"""backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_index""", F"""backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.norm2.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.norm2.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.ffn.layers.1.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.ffn.layers.1.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.stages.{i}.downsample.reduction.weight""", F"""backbone.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.stages.{i}.downsample.norm.weight""", F"""backbone.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.stages.{i}.downsample.norm.bias""", F"""backbone.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""backbone.hidden_states_norms.stage{i+1}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""backbone.hidden_states_norms.stage{i+1}.bias""") ) # decode head rename_keys.extend( [ ("decode_head.conv_seg.weight", "decode_head.classifier.weight"), ("decode_head.conv_seg.bias", "decode_head.classifier.bias"), ("auxiliary_head.conv_seg.weight", "auxiliary_head.classifier.weight"), ("auxiliary_head.conv_seg.bias", "auxiliary_head.classifier.bias"), ] ) # fmt: on return rename_keys def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Any: '''simple docstring''' _lowerCamelCase : Any = dct.pop(_lowerCamelCase ) _lowerCamelCase : List[str] = val def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase ) -> Dict: '''simple docstring''' _lowerCamelCase : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _lowerCamelCase : Optional[Any] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _lowerCamelCase : List[Any] = state_dict.pop(F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.weight""" ) _lowerCamelCase : List[str] = state_dict.pop(F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _lowerCamelCase : Any = in_proj_weight[:dim, :] _lowerCamelCase : str = in_proj_bias[: dim] _lowerCamelCase : Tuple = in_proj_weight[ dim : dim * 2, : ] _lowerCamelCase : Optional[int] = in_proj_bias[ dim : dim * 2 ] _lowerCamelCase : List[Any] = in_proj_weight[ -dim :, : ] _lowerCamelCase : List[Any] = in_proj_bias[-dim :] # fmt: on def lowerCamelCase_( _lowerCamelCase ) -> Dict: '''simple docstring''' _lowerCamelCase : int = x.shape _lowerCamelCase : Optional[int] = x.reshape(_lowerCamelCase , 4 , in_channel // 4 ) _lowerCamelCase : Optional[Any] = x[:, [0, 2, 1, 3], :].transpose(1 , 2 ).reshape(_lowerCamelCase , _lowerCamelCase ) return x def lowerCamelCase_( _lowerCamelCase ) -> Tuple: '''simple docstring''' _lowerCamelCase : Dict = x.shape _lowerCamelCase : int = x.reshape(_lowerCamelCase , in_channel // 4 , 4 ) _lowerCamelCase : Tuple = x[:, :, [0, 2, 1, 3]].transpose(1 , 2 ).reshape(_lowerCamelCase , _lowerCamelCase ) return x def lowerCamelCase_( _lowerCamelCase ) -> str: '''simple docstring''' _lowerCamelCase : Optional[int] = x.shape[0] _lowerCamelCase : List[str] = x.reshape(4 , in_channel // 4 ) _lowerCamelCase : List[str] = x[[0, 2, 1, 3], :].transpose(0 , 1 ).reshape(_lowerCamelCase ) return x def lowerCamelCase_( _lowerCamelCase ) -> List[Any]: '''simple docstring''' _lowerCamelCase : Dict = x.shape[0] _lowerCamelCase : int = x.reshape(in_channel // 4 , 4 ) _lowerCamelCase : str = x[:, [0, 2, 1, 3]].transpose(0 , 1 ).reshape(_lowerCamelCase ) return x def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: '''simple docstring''' _lowerCamelCase : List[str] = { "upernet-swin-tiny": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210531_112542-e380ad3e.pth", "upernet-swin-small": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210526_192015-ee2fff1c.pth", "upernet-swin-base": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K_20210531_125459-429057bf.pth", "upernet-swin-large": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k_20220318_091743-9ba68901.pth", } _lowerCamelCase : Tuple = model_name_to_url[model_name] _lowerCamelCase : int = torch.hub.load_state_dict_from_url(_lowerCamelCase , map_location="cpu" , file_name=_lowerCamelCase )[ "state_dict" ] for name, param in state_dict.items(): print(_lowerCamelCase , param.shape ) _lowerCamelCase : Optional[Any] = get_upernet_config(_lowerCamelCase ) _lowerCamelCase : List[str] = UperNetForSemanticSegmentation(_lowerCamelCase ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): _lowerCamelCase : str = state_dict.pop(_lowerCamelCase ) if "bn" in key: _lowerCamelCase : Any = key.replace("bn" , "batch_norm" ) _lowerCamelCase : Optional[Any] = val # rename keys _lowerCamelCase : Dict = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) read_in_q_k_v(_lowerCamelCase , config.backbone_config ) # fix downsample parameters for key, value in state_dict.items(): if "downsample" in key: if "reduction" in key: _lowerCamelCase : Tuple = reverse_correct_unfold_reduction_order(_lowerCamelCase ) if "norm" in key: _lowerCamelCase : Tuple = reverse_correct_unfold_norm_order(_lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) # verify on image _lowerCamelCase : Tuple = "https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg" _lowerCamelCase : List[Any] = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ).convert("RGB" ) _lowerCamelCase : Optional[int] = SegformerImageProcessor() _lowerCamelCase : Union[str, Any] = processor(_lowerCamelCase , return_tensors="pt" ).pixel_values with torch.no_grad(): _lowerCamelCase : int = model(_lowerCamelCase ) _lowerCamelCase : str = outputs.logits print(logits.shape ) print("First values of logits:" , logits[0, 0, :3, :3] ) # assert values if model_name == "upernet-swin-tiny": _lowerCamelCase : List[Any] = torch.tensor( [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]] ) elif model_name == "upernet-swin-small": _lowerCamelCase : Any = torch.tensor( [[-7.1_9_2_1, -7.1_9_2_1, -6.9_5_3_2], [-7.1_9_2_1, -7.1_9_2_1, -6.9_5_3_2], [-7.0_9_0_8, -7.0_9_0_8, -6.8_5_3_4]] ) elif model_name == "upernet-swin-base": _lowerCamelCase : Any = torch.tensor( [[-6.5_8_5_1, -6.5_8_5_1, -6.4_3_3_0], [-6.5_8_5_1, -6.5_8_5_1, -6.4_3_3_0], [-6.4_7_6_3, -6.4_7_6_3, -6.3_2_5_4]] ) elif model_name == "upernet-swin-large": _lowerCamelCase : str = torch.tensor( [[-7.5_2_9_7, -7.5_2_9_7, -7.3_8_0_2], [-7.5_2_9_7, -7.5_2_9_7, -7.3_8_0_2], [-7.4_0_4_4, -7.4_0_4_4, -7.2_5_8_6]] ) print("Logits:" , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , _lowerCamelCase , atol=1e-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(_lowerCamelCase ) print(F"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(_lowerCamelCase ) if push_to_hub: print(F"""Pushing model and processor for {model_name} to hub""" ) model.push_to_hub(F"""openmmlab/{model_name}""" ) processor.push_to_hub(F"""openmmlab/{model_name}""" ) if __name__ == "__main__": _lowerCAmelCase : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''upernet-swin-tiny''', type=str, choices=[f'''upernet-swin-{size}''' for size in ['''tiny''', '''small''', '''base''', '''large''']], help='''Name of the Swin + UperNet model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) _lowerCAmelCase : List[Any] = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
358
"""simple docstring""" from decimal import Decimal, getcontext from math import ceil, factorial def lowerCamelCase_( _lowerCamelCase ) -> str: '''simple docstring''' if not isinstance(_lowerCamelCase , _lowerCamelCase ): raise TypeError("Undefined for non-integers" ) elif precision < 1: raise ValueError("Undefined for non-natural numbers" ) _lowerCamelCase : int = precision _lowerCamelCase : Dict = ceil(precision / 14 ) _lowerCamelCase : Optional[Any] = 426880 * Decimal(10005 ).sqrt() _lowerCamelCase : int = 1 _lowerCamelCase : Optional[int] = 13591409 _lowerCamelCase : int = Decimal(_lowerCamelCase ) for k in range(1 , _lowerCamelCase ): _lowerCamelCase : Union[str, Any] = factorial(6 * k ) // (factorial(3 * k ) * factorial(_lowerCamelCase ) ** 3) linear_term += 545140134 exponential_term *= -262537412640768000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": _lowerCAmelCase : Union[str, Any] = 50 print(f'''The first {n} digits of pi is: {pi(n)}''')
340
0
from datetime import datetime import matplotlib.pyplot as plt import torch def UpperCamelCase( __UpperCamelCase : Tuple ): for param in module.parameters(): lowerCAmelCase_ : Dict = False def UpperCamelCase( ): lowerCAmelCase_ : List[Any] = '''cuda''' if torch.cuda.is_available() else '''cpu''' if torch.backends.mps.is_available() and torch.backends.mps.is_built(): lowerCAmelCase_ : Optional[int] = '''mps''' if device == "mps": print( '''WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch''' ''' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues''' ''' with generations.''' ) return device def UpperCamelCase( __UpperCamelCase : List[Any] ): lowerCAmelCase_ : Dict = plt.imshow(__a ) fig.axes.get_xaxis().set_visible(__a ) fig.axes.get_yaxis().set_visible(__a ) plt.show() def UpperCamelCase( ): lowerCAmelCase_ : int = datetime.now() lowerCAmelCase_ : str = current_time.strftime('''%H:%M:%S''' ) return timestamp
103
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision.transforms import functional as F from transformers import DetrImageProcessor, TableTransformerConfig, TableTransformerForObjectDetection from transformers.utils import logging logging.set_verbosity_info() __snake_case = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) __snake_case = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F"""transformer.encoder.layers.{i}.self_attn.out_proj.weight""", F"""encoder.layers.{i}.self_attn.out_proj.weight""") ) rename_keys.append( (F"""transformer.encoder.layers.{i}.self_attn.out_proj.bias""", F"""encoder.layers.{i}.self_attn.out_proj.bias""") ) rename_keys.append((F"""transformer.encoder.layers.{i}.linear1.weight""", F"""encoder.layers.{i}.fc1.weight""")) rename_keys.append((F"""transformer.encoder.layers.{i}.linear1.bias""", F"""encoder.layers.{i}.fc1.bias""")) rename_keys.append((F"""transformer.encoder.layers.{i}.linear2.weight""", F"""encoder.layers.{i}.fc2.weight""")) rename_keys.append((F"""transformer.encoder.layers.{i}.linear2.bias""", F"""encoder.layers.{i}.fc2.bias""")) rename_keys.append( (F"""transformer.encoder.layers.{i}.norm1.weight""", F"""encoder.layers.{i}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""transformer.encoder.layers.{i}.norm1.bias""", F"""encoder.layers.{i}.self_attn_layer_norm.bias""")) rename_keys.append((F"""transformer.encoder.layers.{i}.norm2.weight""", F"""encoder.layers.{i}.final_layer_norm.weight""")) rename_keys.append((F"""transformer.encoder.layers.{i}.norm2.bias""", F"""encoder.layers.{i}.final_layer_norm.bias""")) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F"""transformer.decoder.layers.{i}.self_attn.out_proj.weight""", F"""decoder.layers.{i}.self_attn.out_proj.weight""") ) rename_keys.append( (F"""transformer.decoder.layers.{i}.self_attn.out_proj.bias""", F"""decoder.layers.{i}.self_attn.out_proj.bias""") ) rename_keys.append( ( F"""transformer.decoder.layers.{i}.multihead_attn.out_proj.weight""", F"""decoder.layers.{i}.encoder_attn.out_proj.weight""", ) ) rename_keys.append( ( F"""transformer.decoder.layers.{i}.multihead_attn.out_proj.bias""", F"""decoder.layers.{i}.encoder_attn.out_proj.bias""", ) ) rename_keys.append((F"""transformer.decoder.layers.{i}.linear1.weight""", F"""decoder.layers.{i}.fc1.weight""")) rename_keys.append((F"""transformer.decoder.layers.{i}.linear1.bias""", F"""decoder.layers.{i}.fc1.bias""")) rename_keys.append((F"""transformer.decoder.layers.{i}.linear2.weight""", F"""decoder.layers.{i}.fc2.weight""")) rename_keys.append((F"""transformer.decoder.layers.{i}.linear2.bias""", F"""decoder.layers.{i}.fc2.bias""")) rename_keys.append( (F"""transformer.decoder.layers.{i}.norm1.weight""", F"""decoder.layers.{i}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""transformer.decoder.layers.{i}.norm1.bias""", F"""decoder.layers.{i}.self_attn_layer_norm.bias""")) rename_keys.append( (F"""transformer.decoder.layers.{i}.norm2.weight""", F"""decoder.layers.{i}.encoder_attn_layer_norm.weight""") ) rename_keys.append( (F"""transformer.decoder.layers.{i}.norm2.bias""", F"""decoder.layers.{i}.encoder_attn_layer_norm.bias""") ) rename_keys.append((F"""transformer.decoder.layers.{i}.norm3.weight""", F"""decoder.layers.{i}.final_layer_norm.weight""")) rename_keys.append((F"""transformer.decoder.layers.{i}.norm3.bias""", F"""decoder.layers.{i}.final_layer_norm.bias""")) # convolutional projection + query embeddings + layernorm of encoder + layernorm of decoder + class and bounding box heads rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.encoder.norm.weight''', '''encoder.layernorm.weight'''), ('''transformer.encoder.norm.bias''', '''encoder.layernorm.bias'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ] ) def a ( __a , __a , __a ) -> List[str]: '''simple docstring''' UpperCamelCase__ :List[Any] = state_dict.pop(__a ) UpperCamelCase__ :int = val def a ( __a ) -> Any: '''simple docstring''' UpperCamelCase__ :Tuple = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: UpperCamelCase__ :Dict = key.replace('''backbone.0.body''' , '''backbone.conv_encoder.model''' ) UpperCamelCase__ :List[str] = value else: UpperCamelCase__ :Dict = value return new_state_dict def a ( __a ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ :Optional[Any] = '''''' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) UpperCamelCase__ :Optional[Any] = state_dict.pop(f'''{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight''' ) UpperCamelCase__ :str = state_dict.pop(f'''{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict UpperCamelCase__ :Any = in_proj_weight[:256, :] UpperCamelCase__ :Tuple = in_proj_bias[:256] UpperCamelCase__ :Optional[int] = in_proj_weight[256:512, :] UpperCamelCase__ :Optional[Any] = in_proj_bias[256:512] UpperCamelCase__ :Tuple = in_proj_weight[-256:, :] UpperCamelCase__ :Dict = in_proj_bias[-256:] # next: transformer decoder (which is a bit more complex because it also includes cross-attention) for i in range(6 ): # read in weights + bias of input projection layer of self-attention UpperCamelCase__ :List[str] = state_dict.pop(f'''{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_weight''' ) UpperCamelCase__ :Optional[Any] = state_dict.pop(f'''{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict UpperCamelCase__ :Any = in_proj_weight[:256, :] UpperCamelCase__ :Optional[int] = in_proj_bias[:256] UpperCamelCase__ :Tuple = in_proj_weight[256:512, :] UpperCamelCase__ :Dict = in_proj_bias[256:512] UpperCamelCase__ :Any = in_proj_weight[-256:, :] UpperCamelCase__ :Dict = in_proj_bias[-256:] # read in weights + bias of input projection layer of cross-attention UpperCamelCase__ :List[str] = state_dict.pop( f'''{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_weight''' ) UpperCamelCase__ :Any = state_dict.pop(f'''{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) of cross-attention to the state dict UpperCamelCase__ :Optional[Any] = in_proj_weight_cross_attn[:256, :] UpperCamelCase__ :Any = in_proj_bias_cross_attn[:256] UpperCamelCase__ :Any = in_proj_weight_cross_attn[256:512, :] UpperCamelCase__ :Dict = in_proj_bias_cross_attn[256:512] UpperCamelCase__ :str = in_proj_weight_cross_attn[-256:, :] UpperCamelCase__ :Tuple = in_proj_bias_cross_attn[-256:] def a ( __a , __a ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ :str = image.size UpperCamelCase__ :Optional[Any] = max(__a , __a ) UpperCamelCase__ :List[Any] = 800 if '''detection''' in checkpoint_url else 1000 UpperCamelCase__ :Dict = target_max_size / current_max_size UpperCamelCase__ :Any = image.resize((int(round(scale * width ) ), int(round(scale * height ) )) ) return resized_image def a ( __a ) -> int: '''simple docstring''' UpperCamelCase__ :Any = F.to_tensor(__a ) UpperCamelCase__ :int = F.normalize(__a , mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , std=[0.2_2_9, 0.2_2_4, 0.2_2_5] ) return image @torch.no_grad() def a ( __a , __a , __a ) -> Dict: '''simple docstring''' logger.info('''Converting model...''' ) # load original state dict UpperCamelCase__ :Optional[Any] = torch.hub.load_state_dict_from_url(__a , map_location='''cpu''' ) # rename keys for src, dest in rename_keys: rename_key(__a , __a , __a ) UpperCamelCase__ :Any = rename_backbone_keys(__a ) # query, key and value matrices need special treatment read_in_q_k_v(__a ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them UpperCamelCase__ :Dict = '''model.''' for key in state_dict.copy().keys(): if not key.startswith('''class_labels_classifier''' ) and not key.startswith('''bbox_predictor''' ): UpperCamelCase__ :Optional[Any] = state_dict.pop(__a ) UpperCamelCase__ :int = val # create HuggingFace model and load state dict UpperCamelCase__ :str = TableTransformerConfig( backbone='''resnet18''' , mask_loss_coefficient=1 , dice_loss_coefficient=1 , ce_loss_coefficient=1 , bbox_loss_coefficient=5 , giou_loss_coefficient=2 , eos_coefficient=0.4 , class_cost=1 , bbox_cost=5 , giou_cost=2 , ) if "detection" in checkpoint_url: UpperCamelCase__ :List[str] = 15 UpperCamelCase__ :int = 2 UpperCamelCase__ :Tuple = {0: '''table''', 1: '''table rotated'''} UpperCamelCase__ :int = idalabel UpperCamelCase__ :Dict = {v: k for k, v in idalabel.items()} else: UpperCamelCase__ :int = 125 UpperCamelCase__ :List[str] = 6 UpperCamelCase__ :Optional[Any] = { 0: '''table''', 1: '''table column''', 2: '''table row''', 3: '''table column header''', 4: '''table projected row header''', 5: '''table spanning cell''', } UpperCamelCase__ :Dict = idalabel UpperCamelCase__ :Optional[Any] = {v: k for k, v in idalabel.items()} UpperCamelCase__ :List[Any] = DetrImageProcessor( format='''coco_detection''' , max_size=800 if '''detection''' in checkpoint_url else 1000 ) UpperCamelCase__ :int = TableTransformerForObjectDetection(__a ) model.load_state_dict(__a ) model.eval() # verify our conversion UpperCamelCase__ :Dict = '''example_pdf.png''' if '''detection''' in checkpoint_url else '''example_table.png''' UpperCamelCase__ :Optional[Any] = hf_hub_download(repo_id='''nielsr/example-pdf''' , repo_type='''dataset''' , filename=__a ) UpperCamelCase__ :Tuple = Image.open(__a ).convert('''RGB''' ) UpperCamelCase__ :int = normalize(resize(__a , __a ) ).unsqueeze(0 ) UpperCamelCase__ :Optional[int] = model(__a ) if "detection" in checkpoint_url: UpperCamelCase__ :Dict = (1, 15, 3) UpperCamelCase__ :List[Any] = torch.tensor( [[-6.7_8_9_7, -1_6.9_9_8_5, 6.7_9_3_7], [-8.0_1_8_6, -2_2.2_1_9_2, 6.9_6_7_7], [-7.3_1_1_7, -2_1.0_7_0_8, 7.4_0_5_5]] ) UpperCamelCase__ :Tuple = torch.tensor([[0.4_8_6_7, 0.1_7_6_7, 0.6_7_3_2], [0.6_7_1_8, 0.4_4_7_9, 0.3_8_3_0], [0.4_7_1_6, 0.1_7_6_0, 0.6_3_6_4]] ) else: UpperCamelCase__ :Optional[Any] = (1, 125, 7) UpperCamelCase__ :Dict = torch.tensor( [[-1_8.1_4_3_0, -8.3_2_1_4, 4.8_2_7_4], [-1_8.4_6_8_5, -7.1_3_6_1, -4.2_6_6_7], [-2_6.3_6_9_3, -9.3_4_2_9, -4.9_9_6_2]] ) UpperCamelCase__ :List[Any] = torch.tensor([[0.4_9_8_3, 0.5_5_9_5, 0.9_4_4_0], [0.4_9_1_6, 0.6_3_1_5, 0.5_9_5_4], [0.6_1_0_8, 0.8_6_3_7, 0.1_1_3_5]] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, :3, :3] , __a , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , __a , atol=1e-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: # Save model and image processor logger.info(f'''Saving PyTorch model and image processor to {pytorch_dump_folder_path}...''' ) Path(__a ).mkdir(exist_ok=__a ) model.save_pretrained(__a ) image_processor.save_pretrained(__a ) if push_to_hub: # Push model to HF hub logger.info('''Pushing model to the hub...''' ) UpperCamelCase__ :Union[str, Any] = ( '''microsoft/table-transformer-detection''' if '''detection''' in checkpoint_url else '''microsoft/table-transformer-structure-recognition''' ) model.push_to_hub(__a ) image_processor.push_to_hub(__a ) if __name__ == "__main__": __snake_case = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://pubtables1m.blob.core.windows.net/model/pubtables1m_detection_detr_r18.pth''', type=str, choices=[ '''https://pubtables1m.blob.core.windows.net/model/pubtables1m_detection_detr_r18.pth''', '''https://pubtables1m.blob.core.windows.net/model/pubtables1m_structure_detr_r18.pth''', ], help='''URL of the Table Transformer checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) __snake_case = parser.parse_args() convert_table_transformer_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
97
0
"""simple docstring""" import argparse import json import os import re import shutil import torch from transformers import BioGptConfig, BioGptForCausalLM from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() __UpperCAmelCase = 2 class _SCREAMING_SNAKE_CASE : def __init__( self , *, # begin keyword-only arguments __A="<s>" , __A="<pad>" , __A="</s>" , __A="<unk>" , __A=None , ) -> Union[str, Any]: lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ :Union[str, Any] = bos, unk, pad, eos lowerCAmelCase_ :Optional[Any] = [] lowerCAmelCase_ :Dict = [] lowerCAmelCase_ :List[str] = {} lowerCAmelCase_ :int = self.add_symbol(UpperCamelCase__ ) lowerCAmelCase_ :List[str] = self.add_symbol(UpperCamelCase__ ) lowerCAmelCase_ :int = self.add_symbol(UpperCamelCase__ ) lowerCAmelCase_ :Union[str, Any] = self.add_symbol(UpperCamelCase__ ) if extra_special_symbols: for s in extra_special_symbols: self.add_symbol(UpperCamelCase__ ) lowerCAmelCase_ :List[Any] = len(self.symbols ) def __eq__( self , __A ) -> str: return self.indices == other.indices def __getitem__( self , __A ) -> List[Any]: if idx < len(self.symbols ): return self.symbols[idx] return self.unk_word def __len__( self ) -> List[str]: return len(self.symbols ) def __contains__( self , __A ) -> List[Any]: return sym in self.indices @classmethod def __lowerCAmelCase ( cls , __A ) -> Optional[int]: lowerCAmelCase_ :Tuple = cls() d.add_from_file(UpperCamelCase__ ) return d def __lowerCAmelCase ( self , __A , __A=1 , __A=False ) -> Tuple: if word in self.indices and not overwrite: lowerCAmelCase_ :Tuple = self.indices[word] lowerCAmelCase_ :int = self.count[idx] + n return idx else: lowerCAmelCase_ :Any = len(self.symbols ) lowerCAmelCase_ :Union[str, Any] = idx self.symbols.append(UpperCamelCase__ ) self.count.append(UpperCamelCase__ ) return idx def __lowerCAmelCase ( self , __A ) -> int: return 0 def __lowerCAmelCase ( self , __A ) -> str: if isinstance(UpperCamelCase__ , UpperCamelCase__ ): try: with open(UpperCamelCase__ , """r""" , encoding="""utf-8""" ) as fd: self.add_from_file(UpperCamelCase__ ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception("""Incorrect encoding detected in {}, please rebuild the dataset""".format(UpperCamelCase__ ) ) return lowerCAmelCase_ :Dict = f.readlines() lowerCAmelCase_ :int = self._load_meta(UpperCamelCase__ ) for line in lines[indices_start_line:]: try: lowerCAmelCase_ , lowerCAmelCase_ :str = line.rstrip().rsplit(""" """ , 1 ) if field == "#fairseq:overwrite": lowerCAmelCase_ :str = True lowerCAmelCase_ , lowerCAmelCase_ :Tuple = line.rsplit(""" """ , 1 ) else: lowerCAmelCase_ :int = False lowerCAmelCase_ :Union[str, Any] = int(UpperCamelCase__ ) lowerCAmelCase_ :Union[str, Any] = line if word in self and not overwrite: raise RuntimeError( """Duplicate word found when loading Dictionary: '{}'. """ """Duplicate words can overwrite earlier ones by adding the """ """#fairseq:overwrite flag at the end of the corresponding row """ """in the dictionary file. If using the Camembert model, please """ """download an updated copy of the model file.""".format(UpperCamelCase__ ) ) self.add_symbol(UpperCamelCase__ , n=UpperCamelCase__ , overwrite=UpperCamelCase__ ) except ValueError: raise ValueError("""Incorrect dictionary format, expected '<token> <cnt> [flags]'""" ) def _snake_case ( lowercase__ : int ) -> Union[str, Any]: '''simple docstring''' lowerCAmelCase_ :int = dict((re.sub(r"""@@$""" , """""" , UpperCAmelCase__ ), v) if k.endswith("""@@""" ) else (re.sub(r"""$""" , """</w>""" , UpperCAmelCase__ ), v) for k, v in d.items() ) lowerCAmelCase_ :Tuple = """<s> <pad> </s> <unk>""".split() # restore the special tokens for k in keep_keys: del da[F"""{k}</w>"""] lowerCAmelCase_ :Dict = d[k] # restore return da def _snake_case ( lowercase__ : Optional[Any] , lowercase__ : List[str] ) -> Any: '''simple docstring''' if not os.path.exists(UpperCAmelCase__ ): raise ValueError(F"""path {biogpt_checkpoint_path} does not exist!""" ) os.makedirs(UpperCAmelCase__ , exist_ok=UpperCAmelCase__ ) print(F"""Writing results to {pytorch_dump_folder_path}""" ) # handle various types of models lowerCAmelCase_ :List[Any] = os.path.join(UpperCAmelCase__ , """checkpoint.pt""" ) if not os.path.isfile(UpperCAmelCase__ ): raise ValueError(F"""path to the file {checkpoint_file} does not exist!""" ) lowerCAmelCase_ :Tuple = torch.load(UpperCAmelCase__ , map_location="""cpu""" ) lowerCAmelCase_ :Optional[Any] = chkpt["""cfg"""]["""model"""] # dicts lowerCAmelCase_ :Optional[int] = os.path.join(UpperCAmelCase__ , """dict.txt""" ) if not os.path.isfile(UpperCAmelCase__ ): raise ValueError(F"""path to the file {dict_file} does not exist!""" ) lowerCAmelCase_ :Optional[Any] = Dictionary.load(UpperCAmelCase__ ) lowerCAmelCase_ :List[Any] = rewrite_dict_keys(src_dict.indices ) lowerCAmelCase_ :Optional[Any] = len(UpperCAmelCase__ ) lowerCAmelCase_ :int = os.path.join(UpperCAmelCase__ , VOCAB_FILES_NAMES["""vocab_file"""] ) print(F"""Generating {src_vocab_file} of {src_vocab_size} records""" ) with open(UpperCAmelCase__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(UpperCAmelCase__ , ensure_ascii=UpperCAmelCase__ , indent=UpperCAmelCase__ ) ) # merges_file (bpecodes) lowerCAmelCase_ :Tuple = os.path.join(UpperCAmelCase__ , """bpecodes""" ) if not os.path.isfile(UpperCAmelCase__ ): raise ValueError(F"""path to the file {bpecodes_file} does not exist!""" ) lowerCAmelCase_ :int = os.path.join(UpperCAmelCase__ , VOCAB_FILES_NAMES["""merges_file"""] ) shutil.copyfile(UpperCAmelCase__ , UpperCAmelCase__ ) # model config lowerCAmelCase_ :Union[str, Any] = os.path.join(UpperCAmelCase__ , """config.json""" ) lowerCAmelCase_ :List[Any] = { """activation_dropout""": args["""activation_dropout"""], """architectures""": ["""BioGptForCausalLM"""], """attention_probs_dropout_prob""": args["""attention_dropout"""], """bos_token_id""": 0, """eos_token_id""": 2, """hidden_act""": args["""activation_fn"""], """hidden_dropout_prob""": args["""dropout"""], """hidden_size""": args["""decoder_embed_dim"""], """initializer_range""": 0.02, """intermediate_size""": args["""decoder_ffn_embed_dim"""], """layer_norm_eps""": 1E-12, """layerdrop""": args["""decoder_layerdrop"""], """max_position_embeddings""": args["""max_target_positions"""], """model_type""": """biogpt""", """num_attention_heads""": args["""decoder_attention_heads"""], """num_hidden_layers""": args["""decoder_layers"""], """pad_token_id""": 1, """scale_embedding""": not args["""no_scale_embedding"""], """tie_word_embeddings""": args["""share_decoder_input_output_embed"""], """vocab_size""": src_vocab_size, } # good hparam defaults to start with print(F"""Generating {biogpt_model_config_file}""" ) with open(UpperCAmelCase__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(UpperCAmelCase__ , ensure_ascii=UpperCAmelCase__ , indent=UpperCAmelCase__ ) ) # tokenizer config lowerCAmelCase_ :Optional[int] = os.path.join(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase_ :Union[str, Any] = { """bos_token""": """<s>""", """eos_token""": """</s>""", """model_max_length""": 1_0_2_4, """pad_token""": """<pad>""", """special_tokens_map_file""": None, """tokenizer_class""": """BioGptTokenizer""", """unk_token""": """<unk>""", } print(F"""Generating {biogpt_tokenizer_config_file}""" ) with open(UpperCAmelCase__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(UpperCAmelCase__ , ensure_ascii=UpperCAmelCase__ , indent=UpperCAmelCase__ ) ) # model lowerCAmelCase_ :Tuple = chkpt["""model"""] # remove unneeded keys lowerCAmelCase_ :Optional[int] = [ """decoder.version""", ] for k in ignore_keys: model_state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase_ :Optional[int] = list(model_state_dict.keys() ) for layer_name in layer_names: if layer_name.endswith("""output_projection.weight""" ): lowerCAmelCase_ :str = model_state_dict.pop(UpperCAmelCase__ ) else: lowerCAmelCase_ :Dict = model_state_dict.pop(UpperCAmelCase__ ) lowerCAmelCase_ :Optional[Any] = BioGptConfig.from_pretrained(UpperCAmelCase__ ) lowerCAmelCase_ :Union[str, Any] = BioGptForCausalLM(UpperCAmelCase__ ) # check that it loads ok model_new.load_state_dict(UpperCAmelCase__ ) # save lowerCAmelCase_ :Tuple = os.path.join(UpperCAmelCase__ , UpperCAmelCase__ ) print(F"""Generating {pytorch_weights_dump_path}""" ) torch.save(UpperCAmelCase__ , UpperCAmelCase__ ) print("""Conversion is done!""" ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--biogpt_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.' ) __UpperCAmelCase = parser.parse_args() convert_biogpt_checkpoint_to_pytorch(args.biogpt_checkpoint_path, args.pytorch_dump_folder_path)
362
"""simple docstring""" import os from math import logaa def _snake_case ( lowercase__ : str = "base_exp.txt" ) -> int: '''simple docstring''' lowerCAmelCase_ :float = 0 lowerCAmelCase_ :Union[str, Any] = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(lowercase__ ) , lowercase__ ) ) ): lowerCAmelCase_ , lowerCAmelCase_ :Union[str, Any] = list(map(lowercase__ , line.split(""",""" ) ) ) if x * logaa(lowercase__ ) > largest: lowerCAmelCase_ :Any = x * logaa(lowercase__ ) lowerCAmelCase_ :List[Any] = i + 1 return result if __name__ == "__main__": print(solution())
1
0
'''simple docstring''' def A_ ( snake_case ): SCREAMING_SNAKE_CASE:int = set() # edges = list of graph's edges SCREAMING_SNAKE_CASE:Any = get_edges(snake_case ) # While there are still elements in edges list, take an arbitrary edge # (from_node, to_node) and add his extremity to chosen_vertices and then # remove all arcs adjacent to the from_node and to_node while edges: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:Tuple = edges.pop() chosen_vertices.add(snake_case ) chosen_vertices.add(snake_case ) for edge in edges.copy(): if from_node in edge or to_node in edge: edges.discard(snake_case ) return chosen_vertices def A_ ( snake_case ): SCREAMING_SNAKE_CASE:Union[str, Any] = set() for from_node, to_nodes in graph.items(): for to_node in to_nodes: edges.add((from_node, to_node) ) return edges if __name__ == "__main__": import doctest doctest.testmod() # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")
139
'''simple docstring''' from __future__ import annotations A_ = list[list[int]] # assigning initial values to the grid A_ = [ [3, 0, 6, 5, 0, 8, 4, 0, 0], [5, 2, 0, 0, 0, 0, 0, 0, 0], [0, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] # a grid with no solution A_ = [ [5, 0, 6, 5, 0, 8, 4, 0, 3], [5, 2, 0, 0, 0, 0, 0, 0, 2], [1, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] def A_ ( snake_case , snake_case , snake_case , snake_case ): for i in range(9 ): if grid[row][i] == n or grid[i][column] == n: return False for i in range(3 ): for j in range(3 ): if grid[(row - row % 3) + i][(column - column % 3) + j] == n: return False return True def A_ ( snake_case ): for i in range(9 ): for j in range(9 ): if grid[i][j] == 0: return i, j return None def A_ ( snake_case ): if location := find_empty_location(snake_case ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:Optional[int] = location else: # If the location is ``None``, then the grid is solved. return grid for digit in range(1 , 10 ): if is_safe(snake_case , snake_case , snake_case , snake_case ): SCREAMING_SNAKE_CASE:List[str] = digit if sudoku(snake_case ) is not None: return grid SCREAMING_SNAKE_CASE:List[Any] = 0 return None def A_ ( snake_case ): for row in grid: for cell in row: print(snake_case , end=" " ) print() if __name__ == "__main__": # make a copy of grid so that you can compare with the unmodified grid for example_grid in (initial_grid, no_solution): print("\nExample grid:\n" + "=" * 20) print_solution(example_grid) print("\nExample grid solution:") A_ = sudoku(example_grid) if solution is not None: print_solution(solution) else: print("Cannot find a solution.")
139
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 __magic_name__ : """simple docstring""" def __init__( self :Dict , snake_case :int , snake_case :Dict=12 , snake_case :Optional[int]=7 , snake_case :List[str]=True , snake_case :str=True , snake_case :Tuple=True , snake_case :int=99 , snake_case :Union[str, Any]=32 , snake_case :Optional[Any]=32 , snake_case :List[str]=2 , snake_case :Tuple=4 , snake_case :int=37 , snake_case :Dict=0.1 , snake_case :Dict=0.1 , snake_case :Tuple=512 , snake_case :Dict=0.02 , snake_case :Dict=0 , snake_case :List[Any]=None , ): '''simple docstring''' A_ : Optional[Any] = parent A_ : Optional[int] = batch_size A_ : List[Any] = seq_length A_ : int = is_training A_ : str = use_input_mask A_ : Union[str, Any] = use_labels A_ : Dict = vocab_size A_ : List[str] = hidden_size A_ : Union[str, Any] = projection_dim A_ : str = num_hidden_layers A_ : Optional[int] = num_attention_heads A_ : Tuple = intermediate_size A_ : List[str] = dropout A_ : Optional[Any] = attention_dropout A_ : Tuple = max_position_embeddings A_ : Union[str, Any] = initializer_range A_ : List[Any] = scope A_ : Tuple = bos_token_id def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A_ : Dict = None if self.use_input_mask: A_ : Any = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: A_ : Any = input_mask.numpy() A_ , A_ : int = input_mask.shape A_ : List[str] = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case ): A_ : List[Any] = 1 A_ : int = 0 A_ : Optional[int] = self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''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 :Tuple , snake_case :Dict , snake_case :Optional[int] , snake_case :Optional[int] ): '''simple docstring''' A_ : List[Any] = TFBlipTextModel(config=snake_case ) A_ : Dict = model(snake_case , attention_mask=snake_case , training=snake_case ) A_ : Optional[Any] = 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 :Union[str, Any] ): '''simple docstring''' A_ : Tuple = self.prepare_config_and_inputs() A_ , A_ , A_ : Dict = config_and_inputs A_ : Union[str, Any] = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (TFBlipTextModel,) if is_tf_available() else () __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : str = BlipTextModelTester(self ) A_ : Optional[Any] = ConfigTester(self , config_class=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''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 :int ): '''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 @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING" ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' pass @slow def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Optional[Any] = TFBlipTextModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :Union[str, Any]=True ): '''simple docstring''' super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case )
70
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCAmelCase : Union[str, Any] = { '''configuration_xmod''': [ '''XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XmodConfig''', '''XmodOnnxConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase : str = [ '''XMOD_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XmodForCausalLM''', '''XmodForMaskedLM''', '''XmodForMultipleChoice''', '''XmodForQuestionAnswering''', '''XmodForSequenceClassification''', '''XmodForTokenClassification''', '''XmodModel''', '''XmodPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys _lowerCAmelCase : List[str] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
70
1
'''simple docstring''' import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCAmelCase = logging.get_logger(__name__) UpperCAmelCase = """▁""" UpperCAmelCase = {"""vocab_file""": """vocab.txt""", """sentencepiece_model_ckpt""": """sentencepiece.bpe.model"""} UpperCAmelCase = { """sentencepiece_model_file""": """sentencepiece.bpe.model""", """vocab_file""": """vocab.txt""", } UpperCAmelCase = { """vocab_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", }, """sentencepiece_model_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", }, } UpperCAmelCase = { """ernie-m-base""": 514, """ernie-m-large""": 514, } UpperCAmelCase = { """ernie-m-base""": {"""do_lower_case""": False}, """ernie-m-large""": {"""do_lower_case""": False}, } class lowerCAmelCase ( lowerCamelCase__ ): lowerCAmelCase_ = ["input_ids"] lowerCAmelCase_ = VOCAB_FILES_NAMES lowerCAmelCase_ = PRETRAINED_INIT_CONFIGURATION lowerCAmelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase_ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase_ = RESOURCE_FILES_NAMES def __init__( self : Optional[Any] , __lowercase : Optional[int] , __lowercase : str=None , __lowercase : Optional[Any]=False , __lowercase : Any="utf8" , __lowercase : List[Any]="[UNK]" , __lowercase : str="[SEP]" , __lowercase : Any="[PAD]" , __lowercase : List[str]="[CLS]" , __lowercase : int="[MASK]" , __lowercase : Optional[Any] = None , **__lowercase : Any , ): """simple docstring""" __lowercase ={} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__lowercase , unk_token=__lowercase , sep_token=__lowercase , pad_token=__lowercase , cls_token=__lowercase , mask_token=__lowercase , vocab_file=__lowercase , encoding=__lowercase , sp_model_kwargs=self.sp_model_kwargs , **__lowercase , ) __lowercase =do_lower_case __lowercase =sentencepiece_model_ckpt __lowercase =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__lowercase ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: __lowercase =self.load_vocab(filepath=__lowercase ) else: __lowercase ={self.sp_model.id_to_piece(__lowercase ): id for id in range(self.sp_model.get_piece_size() )} __lowercase ={v: k for k, v in self.vocab.items()} def snake_case ( self : Any , __lowercase : Any ): """simple docstring""" if text is None: return None __lowercase =self.tokenize(__lowercase ) __lowercase , __lowercase ='', [] for i, ch in enumerate(__lowercase ): if ch in self.SP_CHAR_MAPPING: __lowercase =self.SP_CHAR_MAPPING.get(__lowercase ) else: __lowercase =unicodedata.normalize('NFKC' , __lowercase ) if self.is_whitespace(__lowercase ): continue normalized_text += ch char_mapping.extend([i] * len(__lowercase ) ) __lowercase , __lowercase , __lowercase =normalized_text, [], 0 if self.do_lower_case: __lowercase =text.lower() for token in split_tokens: if token[:1] == "▁": __lowercase =token[1:] __lowercase =text[offset:].index(__lowercase ) + offset __lowercase =start + len(__lowercase ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) __lowercase =end return token_mapping @property def snake_case ( self : Optional[int] ): """simple docstring""" return len(self.vocab ) def snake_case ( self : Union[str, Any] ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self : List[Any] ): """simple docstring""" __lowercase =self.__dict__.copy() __lowercase =None return state def __setstate__( self : List[Any] , __lowercase : Dict ): """simple docstring""" __lowercase =d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): __lowercase ={} __lowercase =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def snake_case ( self : Union[str, Any] , __lowercase : Union[str, Any] ): """simple docstring""" return "".join((self.SP_CHAR_MAPPING.get(__lowercase , __lowercase ) for c in text) ) def snake_case ( self : List[str] , __lowercase : List[str] , __lowercase : int=False , __lowercase : Any=64 , __lowercase : Optional[int]=0.1 ): """simple docstring""" if self.sp_model_kwargs.get('enable_sampling' ) is True: __lowercase =True if self.sp_model_kwargs.get('alpha' ) is not None: __lowercase =self.sp_model_kwargs.get('alpha' ) if self.sp_model_kwargs.get('nbest_size' ) is not None: __lowercase =self.sp_model_kwargs.get('nbest_size' ) if not enable_sampling: __lowercase =self.sp_model.EncodeAsPieces(__lowercase ) else: __lowercase =self.sp_model.SampleEncodeAsPieces(__lowercase , __lowercase , __lowercase ) __lowercase =[] for pi, piece in enumerate(__lowercase ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(__lowercase ) and pi != 0: new_pieces.append(__lowercase ) continue else: continue __lowercase =0 for i, chunk in enumerate(__lowercase ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(__lowercase ) or self.is_punct(__lowercase ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(__lowercase ) __lowercase =i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) __lowercase =i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) __lowercase =i if len(__lowercase ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def snake_case ( self : List[str] , __lowercase : Tuple ): """simple docstring""" __lowercase =''.join(__lowercase ).replace(__lowercase , ' ' ).strip() return out_string def snake_case ( self : List[Any] , __lowercase : List[Any] ): """simple docstring""" __lowercase =self.convert_ids_to_tokens(__lowercase ) __lowercase =''.join(__lowercase ).replace(__lowercase , ' ' ).strip() return out_string def snake_case ( self : int , __lowercase : Optional[int] ): """simple docstring""" return self.vocab.get(__lowercase , self.vocab.get(self.unk_token ) ) def snake_case ( self : str , __lowercase : Dict ): """simple docstring""" return self.reverse_vocab.get(__lowercase , self.unk_token ) def snake_case ( self : int , __lowercase : Union[str, Any] , __lowercase : Union[str, Any]=None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowercase =[self.cls_token_id] __lowercase =[self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def snake_case ( self : Any , __lowercase : List[str] , __lowercase : Any=None ): """simple docstring""" if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def snake_case ( self : int , __lowercase : Union[str, Any] , __lowercase : Optional[Any]=None , __lowercase : Optional[Any]=False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__lowercase )) + [1, 1] + ([0] * len(__lowercase )) + [1] return [1] + ([0] * len(__lowercase )) + [1] def snake_case ( self : List[str] , __lowercase : List[str] , __lowercase : Tuple = None ): """simple docstring""" if token_ids_a is None: # [CLS] X [SEP] return (len(__lowercase ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(__lowercase ) + 1) + [1] * (len(__lowercase ) + 3) def snake_case ( self : Union[str, Any] , __lowercase : Dict ): """simple docstring""" if "\u4e00" <= char <= "\u9fff": return True return False def snake_case ( self : Optional[Any] , __lowercase : str ): """simple docstring""" if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def snake_case ( self : str , __lowercase : Optional[int] ): """simple docstring""" if char in ",;:.?!~,;:。?!《》【】": return True return False def snake_case ( self : Tuple , __lowercase : int ): """simple docstring""" if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(__lowercase ) == 1: __lowercase =unicodedata.category(__lowercase ) if cat == "Zs": return True return False def snake_case ( self : List[str] , __lowercase : Optional[int] ): """simple docstring""" __lowercase ={} with io.open(__lowercase , 'r' , encoding='utf-8' ) as f: for index, line in enumerate(__lowercase ): __lowercase =line.rstrip('\n' ) __lowercase =int(__lowercase ) return token_to_idx def snake_case ( self : str , __lowercase : List[Any] , __lowercase : Optional[Any] = None ): """simple docstring""" __lowercase =0 if os.path.isdir(__lowercase ): __lowercase =os.path.join( __lowercase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) else: __lowercase =(filename_prefix + '-' if filename_prefix else '') + save_directory with open(__lowercase , 'w' , encoding='utf-8' ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda __lowercase : kv[1] ): if index != token_index: logger.warning( f'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.''' ' Please check that the vocabulary is not corrupted!' ) __lowercase =token_index writer.write(token + '\n' ) index += 1 __lowercase =os.path.join(__lowercase , 'sentencepiece.bpe.model' ) with open(__lowercase , 'wb' ) as fi: __lowercase =self.sp_model.serialized_model_proto() fi.write(__lowercase ) return (vocab_file,)
141
'''simple docstring''' import argparse import os import re import torch from flax.traverse_util import flatten_dict from tax import checkpoints from transformers import ( AutoTokenizer, PixaStructConfig, PixaStructForConditionalGeneration, PixaStructImageProcessor, PixaStructProcessor, PixaStructTextConfig, PixaStructVisionConfig, ) def a_ ( __snake_case : Any ) -> int: """simple docstring""" lowerCamelCase_ =checkpoints.load_tax_checkpoint(__snake_case ) lowerCamelCase_ =flatten_dict(__snake_case ) return flax_params def a_ ( __snake_case : Dict ) -> Optional[int]: """simple docstring""" lowerCamelCase_ ={} lowerCamelCase_ ={ '''token_embedder''': '''embeddings''', '''encoder_norm''': '''layernorm''', '''kernel''': '''weight''', '''.out''': '''.output''', '''scale''': '''weight''', '''embedders_0.pos_embedding''': '''row_embedder.weight''', '''embedders_1.pos_embedding''': '''column_embedder.weight''', } lowerCamelCase_ ={ '''query''': '''attention.query''', '''key''': '''attention.key''', '''value''': '''attention.value''', '''output.dense''': '''output''', '''encoder_decoder_attention.o''': '''encoder_decoder_attention.attention.o''', '''pre_self_attention_layer_norm''': '''self_attention.layer_norm''', '''pre_cross_attention_layer_norm''': '''encoder_decoder_attention.layer_norm''', '''mlp.''': '''mlp.DenseReluDense.''', '''pre_mlp_layer_norm''': '''mlp.layer_norm''', '''self_attention.o''': '''self_attention.attention.o''', '''decoder.embeddings.embedding''': '''decoder.embed_tokens.weight''', '''decoder.relpos_bias.rel_embedding''': '''decoder.layer.0.self_attention.attention.relative_attention_bias.weight''', '''decoder.decoder_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.logits_dense.weight''': '''decoder.lm_head.weight''', } for key in flax_dict.keys(): if "target" in key: # remove the first prefix from the key lowerCamelCase_ ='''.'''.join(key[1:] ) # rename the key for old, new in CONVERSION_MAPPING.items(): lowerCamelCase_ =new_key.replace(__snake_case , __snake_case ) if "decoder" in new_key: for old, new in DECODER_CONVERSION_MAPPING.items(): lowerCamelCase_ =new_key.replace(__snake_case , __snake_case ) if "layers" in new_key and "decoder" not in new_key: # use regex to replace the layer number lowerCamelCase_ =re.sub(r'''layers_(\d+)''' , r'''layer.\1''' , __snake_case ) lowerCamelCase_ =new_key.replace('''encoder''' , '''encoder.encoder''' ) elif "layers" in new_key and "decoder" in new_key: # use regex to replace the layer number lowerCamelCase_ =re.sub(r'''layers_(\d+)''' , r'''layer.\1''' , __snake_case ) lowerCamelCase_ =flax_dict[key] lowerCamelCase_ ={} # convert converted_dict into torch format for key in converted_dict.keys(): if ("embed_tokens" not in key) and ("embedder" not in key): lowerCamelCase_ =torch.from_numpy(converted_dict[key].T ) else: lowerCamelCase_ =torch.from_numpy(converted_dict[key] ) return converted_torch_dict def a_ ( __snake_case : Optional[Any] , __snake_case : List[str] , __snake_case : Any=False , __snake_case : Optional[int]=False ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ =get_flax_param(__snake_case ) if not use_large: lowerCamelCase_ =PixaStructVisionConfig() lowerCamelCase_ =PixaStructTextConfig() else: lowerCamelCase_ =PixaStructVisionConfig( hidden_size=1536 , d_ff=3968 , num_attention_heads=24 , num_hidden_layers=18 ) lowerCamelCase_ =PixaStructTextConfig(hidden_size=1536 , d_ff=3968 , num_heads=24 , num_layers=18 ) lowerCamelCase_ =PixaStructConfig( vision_config=encoder_config.to_dict() , text_config=decoder_config.to_dict() , is_vqa=__snake_case ) lowerCamelCase_ =PixaStructForConditionalGeneration(__snake_case ) lowerCamelCase_ =rename_and_convert_flax_params(__snake_case ) model.load_state_dict(__snake_case ) lowerCamelCase_ =AutoTokenizer.from_pretrained('''ybelkada/test-pix2struct-tokenizer''' ) lowerCamelCase_ =PixaStructImageProcessor() lowerCamelCase_ =PixaStructProcessor(image_processor=__snake_case , tokenizer=__snake_case ) if use_large: lowerCamelCase_ =4096 lowerCamelCase_ =True # mkdir if needed os.makedirs(__snake_case , exist_ok=__snake_case ) model.save_pretrained(__snake_case ) processor.save_pretrained(__snake_case ) print('''Model saved in {}'''.format(__snake_case ) ) if __name__ == "__main__": a_ : Optional[int] = argparse.ArgumentParser() parser.add_argument("""--t5x_checkpoint_path""", default=None, type=str, help="""Path to the original T5x checkpoint.""") parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--use_large""", action="""store_true""", help="""Use large model.""") parser.add_argument("""--is_vqa""", action="""store_true""", help="""Use large model.""") a_ : Tuple = parser.parse_args() convert_pixastruct_original_pytorch_checkpoint_to_hf( args.tax_checkpoint_path, args.pytorch_dump_folder_path, args.use_large )
75
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { '''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 UpperCamelCase_ (__A ): __magic_name__ = '''xlm-roberta''' def __init__( self : List[str] , lowerCAmelCase_ : int=30_522 , lowerCAmelCase_ : Dict=768 , lowerCAmelCase_ : int=12 , lowerCAmelCase_ : Optional[Any]=12 , lowerCAmelCase_ : str=3_072 , lowerCAmelCase_ : List[Any]="gelu" , lowerCAmelCase_ : List[str]=0.1 , lowerCAmelCase_ : List[Any]=0.1 , lowerCAmelCase_ : Tuple=512 , lowerCAmelCase_ : Optional[Any]=2 , lowerCAmelCase_ : str=0.0_2 , lowerCAmelCase_ : Tuple=1e-12 , lowerCAmelCase_ : Tuple=1 , lowerCAmelCase_ : str=0 , lowerCAmelCase_ : List[str]=2 , lowerCAmelCase_ : Optional[Any]="absolute" , lowerCAmelCase_ : Optional[int]=True , lowerCAmelCase_ : Tuple=None , **lowerCAmelCase_ : int , ) -> Tuple: super().__init__(pad_token_id=lowerCAmelCase_ , bos_token_id=lowerCAmelCase_ , eos_token_id=lowerCAmelCase_ , **lowerCAmelCase_ ) UpperCAmelCase_ : Union[str, Any] = vocab_size UpperCAmelCase_ : List[Any] = hidden_size UpperCAmelCase_ : Dict = num_hidden_layers UpperCAmelCase_ : List[str] = num_attention_heads UpperCAmelCase_ : List[str] = hidden_act UpperCAmelCase_ : List[Any] = intermediate_size UpperCAmelCase_ : int = hidden_dropout_prob UpperCAmelCase_ : Union[str, Any] = attention_probs_dropout_prob UpperCAmelCase_ : List[str] = max_position_embeddings UpperCAmelCase_ : List[str] = type_vocab_size UpperCAmelCase_ : Optional[Any] = initializer_range UpperCAmelCase_ : Any = layer_norm_eps UpperCAmelCase_ : Union[str, Any] = position_embedding_type UpperCAmelCase_ : List[Any] = use_cache UpperCAmelCase_ : Optional[Any] = classifier_dropout class UpperCamelCase_ (__A ): @property def _SCREAMING_SNAKE_CASE ( self : List[str] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": UpperCAmelCase_ : Union[str, Any] = {0: "batch", 1: "choice", 2: "sequence"} else: UpperCAmelCase_ : Optional[Any] = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
253
"""simple docstring""" from __future__ import annotations import os from collections.abc import Mapping lowerCamelCase_ = tuple[int, int] class UpperCamelCase_ : def __init__( self : List[Any] , lowerCAmelCase_ : set[int] , lowerCAmelCase_ : Mapping[EdgeT, int] ) -> None: UpperCAmelCase_ : set[int] = vertices UpperCAmelCase_ : dict[EdgeT, int] = { (min(lowerCAmelCase_ ), max(lowerCAmelCase_ )): weight for edge, weight in edges.items() } def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase_ : EdgeT , lowerCAmelCase_ : int ) -> None: self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) UpperCAmelCase_ : Tuple = weight def _SCREAMING_SNAKE_CASE ( self : str ) -> Graph: UpperCAmelCase_ : Graph = Graph({min(self.vertices )} , {} ) UpperCAmelCase_ : EdgeT UpperCAmelCase_ : int UpperCAmelCase_ : EdgeT UpperCAmelCase_ : int while len(subgraph.vertices ) < len(self.vertices ): UpperCAmelCase_ : int = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: UpperCAmelCase_ : Tuple = edge UpperCAmelCase_ : Dict = weight subgraph.add_edge(lowerCAmelCase_ , lowerCAmelCase_ ) return subgraph def snake_case ( A__ = "p107_network.txt" ): UpperCAmelCase_ : str = os.path.abspath(os.path.dirname(A__ ) ) UpperCAmelCase_ : str = os.path.join(A__ ,A__ ) UpperCAmelCase_ : dict[EdgeT, int] = {} UpperCAmelCase_ : list[str] UpperCAmelCase_ : int UpperCAmelCase_ : int with open(A__ ) as f: UpperCAmelCase_ : Dict = f.read().strip().split("\n" ) UpperCAmelCase_ : str = [line.split("," ) for line in data] for edgea in range(1 ,len(A__ ) ): for edgea in range(A__ ): if adjaceny_matrix[edgea][edgea] != "-": UpperCAmelCase_ : Union[str, Any] = int(adjaceny_matrix[edgea][edgea] ) UpperCAmelCase_ : Graph = Graph(set(range(len(A__ ) ) ) ,A__ ) UpperCAmelCase_ : Graph = graph.prims_algorithm() UpperCAmelCase_ : int = sum(graph.edges.values() ) UpperCAmelCase_ : int = sum(subgraph.edges.values() ) return initial_total - optimal_total if __name__ == "__main__": print(f'{solution() = }')
253
1
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''facebook/s2t-wav2vec2-large-en-de''': ( '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json''' ), # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2 } class snake_case_ ( __A ): __A : Optional[int] = "speech_to_text_2" __A : Optional[Any] = ["past_key_values"] __A : List[Any] = {"num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model"} def __init__( self : Optional[Any] , lowercase_ : List[Any]=1_00_00 , lowercase_ : Any=6 , lowercase_ : Union[str, Any]=20_48 , lowercase_ : Any=4 , lowercase_ : Any=0.0 , lowercase_ : Tuple=True , lowercase_ : Union[str, Any]="relu" , lowercase_ : Dict=2_56 , lowercase_ : str=0.1 , lowercase_ : Optional[int]=0.0 , lowercase_ : Optional[int]=0.0 , lowercase_ : str=0.02 , lowercase_ : Optional[Any]=2 , lowercase_ : Tuple=True , lowercase_ : str=1 , lowercase_ : Dict=0 , lowercase_ : str=2 , lowercase_ : Any=10_24 , **lowercase_ : Optional[int] , ) -> Optional[Any]: lowercase__ : Union[str, Any] = vocab_size lowercase__ : List[str] = d_model lowercase__ : Tuple = decoder_ffn_dim lowercase__ : str = decoder_layers lowercase__ : Optional[int] = decoder_attention_heads lowercase__ : Optional[Any] = dropout lowercase__ : Tuple = attention_dropout lowercase__ : int = activation_dropout lowercase__ : Dict = activation_function lowercase__ : Optional[int] = init_std lowercase__ : Optional[int] = decoder_layerdrop lowercase__ : List[str] = use_cache lowercase__ : Optional[int] = decoder_layers lowercase__ : Optional[Any] = scale_embedding # scale factor will be sqrt(d_model) if True lowercase__ : Tuple = max_target_positions super().__init__( pad_token_id=lowercase_ , bos_token_id=lowercase_ , eos_token_id=lowercase_ , decoder_start_token_id=lowercase_ , **lowercase_ , )
87
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''', # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class snake_case_ ( __A ): __A : List[str] = "convbert" def __init__( self : Union[str, Any] , lowercase_ : str=3_05_22 , lowercase_ : Any=7_68 , lowercase_ : Tuple=12 , lowercase_ : List[str]=12 , lowercase_ : Optional[int]=30_72 , lowercase_ : Union[str, Any]="gelu" , lowercase_ : str=0.1 , lowercase_ : List[str]=0.1 , lowercase_ : Optional[Any]=5_12 , lowercase_ : Dict=2 , lowercase_ : Union[str, Any]=0.02 , lowercase_ : Optional[Any]=1E-12 , lowercase_ : Optional[int]=1 , lowercase_ : List[Any]=0 , lowercase_ : Optional[int]=2 , lowercase_ : str=7_68 , lowercase_ : Dict=2 , lowercase_ : Optional[Any]=9 , lowercase_ : Union[str, Any]=1 , lowercase_ : Any=None , **lowercase_ : Optional[Any] , ) -> Dict: super().__init__( pad_token_id=lowercase_ , bos_token_id=lowercase_ , eos_token_id=lowercase_ , **lowercase_ , ) lowercase__ : List[str] = vocab_size lowercase__ : Union[str, Any] = hidden_size lowercase__ : Any = num_hidden_layers lowercase__ : List[str] = num_attention_heads lowercase__ : Union[str, Any] = intermediate_size lowercase__ : Optional[Any] = hidden_act lowercase__ : int = hidden_dropout_prob lowercase__ : str = attention_probs_dropout_prob lowercase__ : Union[str, Any] = max_position_embeddings lowercase__ : Optional[int] = type_vocab_size lowercase__ : Tuple = initializer_range lowercase__ : List[str] = layer_norm_eps lowercase__ : List[Any] = embedding_size lowercase__ : Optional[Any] = head_ratio lowercase__ : Dict = conv_kernel_size lowercase__ : Tuple = num_groups lowercase__ : Optional[int] = classifier_dropout class snake_case_ ( __A ): @property def __UpperCamelCase ( self : Optional[int] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": lowercase__ : Tuple = {0: "batch", 1: "choice", 2: "sequence"} else: lowercase__ : str = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ("token_type_ids", dynamic_axis), ] )
87
1
import unittest import numpy as np import requests from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: UpperCAmelCase__ = False if is_vision_available(): from PIL import Image from transformers import PixaStructImageProcessor class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Any , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Union[str, Any]=7 , __UpperCAmelCase : Any=3 , __UpperCAmelCase : int=18 , __UpperCAmelCase : Any=30 , __UpperCAmelCase : Union[str, Any]=400 , __UpperCAmelCase : Union[str, Any]=None , __UpperCAmelCase : str=True , __UpperCAmelCase : int=True , __UpperCAmelCase : Optional[int]=None , ) ->Optional[Any]: """simple docstring""" a = size if size is not None else {'height': 20, 'width': 20} a = parent a = batch_size a = num_channels a = image_size a = min_resolution a = max_resolution a = size a = do_normalize a = do_convert_rgb a = [512, 1_024, 2_048, 4_096] a = patch_size if patch_size is not None else {'height': 16, 'width': 16} def __lowerCAmelCase ( self : Tuple ) ->Any: """simple docstring""" return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb} def __lowerCAmelCase ( self : Optional[int] ) ->Any: """simple docstring""" a = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg' a = Image.open(requests.get(__UpperCAmelCase , stream=__UpperCAmelCase ).raw ).convert('''RGB''' ) return raw_image @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.''' , ) @require_torch @require_vision class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = PixaStructImageProcessor if is_vision_available() else None def __lowerCAmelCase ( self : Union[str, Any] ) ->int: """simple docstring""" a = PixaStructImageProcessingTester(self ) @property def __lowerCAmelCase ( self : Any ) ->Optional[int]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __lowerCAmelCase ( self : List[Any] ) ->Any: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''do_convert_rgb''' ) ) def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = self.image_processor_tester.prepare_dummy_image() a = self.image_processing_class(**self.image_processor_dict ) a = 2_048 a = image_processor(__UpperCAmelCase , return_tensors='''pt''' , max_patches=__UpperCAmelCase ) self.assertTrue(torch.allclose(inputs.flattened_patches.mean() , torch.tensor(0.0606 ) , atol=1e-3 , rtol=1e-3 ) ) def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , Image.Image ) # Test not batched input a = ( (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a = image_processor( __UpperCAmelCase , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __lowerCAmelCase ( self : Any ) ->List[str]: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , Image.Image ) # Test not batched input a = ( (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width']) * self.image_processor_tester.num_channels ) + 2 a = True for max_patch in self.image_processor_tester.max_patches: # Test not batched input with self.assertRaises(__UpperCAmelCase ): a = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches a = 'Hello' a = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__UpperCAmelCase , header_text=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a = image_processor( __UpperCAmelCase , return_tensors='''pt''' , max_patches=__UpperCAmelCase , header_text=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase , numpify=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , np.ndarray ) a = ( (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a = image_processor( __UpperCAmelCase , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase , torchify=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , torch.Tensor ) # Test not batched input a = ( (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a = image_processor( __UpperCAmelCase , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.''' , ) @require_torch @require_vision class lowercase_ ( lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = PixaStructImageProcessor if is_vision_available() else None def __lowerCAmelCase ( self : List[Any] ) ->Tuple: """simple docstring""" a = PixaStructImageProcessingTester(self , num_channels=4 ) a = 3 @property def __lowerCAmelCase ( self : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(__UpperCAmelCase , '''do_convert_rgb''' ) ) def __lowerCAmelCase ( self : int ) ->Union[str, Any]: """simple docstring""" a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , Image.Image ) # Test not batched input a = ( (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width']) * (self.image_processor_tester.num_channels - 1) ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a = image_processor( __UpperCAmelCase , return_tensors='''pt''' , max_patches=__UpperCAmelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , )
362
from __future__ import annotations import typing from collections import Counter def _a ( a :int ) -> 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 _a ( a :int = 1_000 ) -> int: a = pythagorean_triple(a ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f"""Perimeter {solution()} has maximum solutions""")
26
0
"""simple docstring""" from abc import ABC, abstractmethod from typing import List, Optional class __snake_case ( _lowercase): def __init__( self : str ): """simple docstring""" self.test() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" _lowerCamelCase : Union[str, Any] = 0 _lowerCamelCase : Optional[int] = False while not completed: if counter == 1: self.reset() _lowerCamelCase : Optional[int] = self.advance() if not self.does_advance(__lowerCAmelCase ): raise Exception( '''Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.''' ) _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : int = self.update(__lowerCAmelCase ) counter += 1 if counter > 1_0_0_0_0: raise Exception('''update() does not fulfill the constraint.''' ) if self.remaining() != 0: raise Exception('''Custom Constraint is not defined correctly.''' ) @abstractmethod def SCREAMING_SNAKE_CASE ( self : Optional[int] ): """simple docstring""" raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def SCREAMING_SNAKE_CASE ( self : Tuple , __lowerCAmelCase : int ): """simple docstring""" raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : int ): """simple docstring""" raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def SCREAMING_SNAKE_CASE ( self : List[str] ): """simple docstring""" raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def SCREAMING_SNAKE_CASE ( self : Any ): """simple docstring""" raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __lowerCAmelCase : Tuple=False ): """simple docstring""" raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __snake_case ( _lowercase): def __init__( self : Optional[int] , __lowerCAmelCase : List[int] ): """simple docstring""" super(__lowerCAmelCase , self ).__init__() if not isinstance(__lowerCAmelCase , __lowerCAmelCase ) or len(__lowerCAmelCase ) == 0: raise ValueError(f'''`token_ids` has to be a non-empty list, but is {token_ids}.''' ) if any((not isinstance(__lowerCAmelCase , __lowerCAmelCase ) or token_id < 0) for token_id in token_ids ): raise ValueError(f'''Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.''' ) _lowerCamelCase : Union[str, Any] = token_ids _lowerCamelCase : Dict = len(self.token_ids ) _lowerCamelCase : List[str] = -1 # the index of the currently fulfilled step _lowerCamelCase : List[Any] = False def SCREAMING_SNAKE_CASE ( self : Optional[Any] ): """simple docstring""" if self.completed: return None return self.token_ids[self.fulfilled_idx + 1] def SCREAMING_SNAKE_CASE ( self : int , __lowerCAmelCase : int ): """simple docstring""" if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(__lowerCAmelCase )}''' ) if self.completed: return False return token_id == self.token_ids[self.fulfilled_idx + 1] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __lowerCAmelCase : int ): """simple docstring""" if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(__lowerCAmelCase )}''' ) _lowerCamelCase : List[str] = False _lowerCamelCase : Union[str, Any] = False _lowerCamelCase : Optional[int] = False if self.does_advance(__lowerCAmelCase ): self.fulfilled_idx += 1 _lowerCamelCase : List[Any] = True if self.fulfilled_idx == (self.seqlen - 1): _lowerCamelCase : int = True _lowerCamelCase : Optional[Any] = completed else: # failed to make progress. _lowerCamelCase : str = True self.reset() return stepped, completed, reset def SCREAMING_SNAKE_CASE ( self : Dict ): """simple docstring""" _lowerCamelCase : str = False _lowerCamelCase : Tuple = 0 def SCREAMING_SNAKE_CASE ( self : List[str] ): """simple docstring""" return self.seqlen - (self.fulfilled_idx + 1) def SCREAMING_SNAKE_CASE ( self : Dict , __lowerCAmelCase : Dict=False ): """simple docstring""" _lowerCamelCase : Optional[int] = PhrasalConstraint(self.token_ids ) if stateful: _lowerCamelCase : Any = self.seqlen _lowerCamelCase : List[Any] = self.fulfilled_idx _lowerCamelCase : Dict = self.completed return new_constraint class __snake_case : def __init__( self : Dict , __lowerCAmelCase : List[List[int]] , __lowerCAmelCase : Dict=True ): """simple docstring""" _lowerCamelCase : Any = max([len(__lowerCAmelCase ) for one in nested_token_ids] ) _lowerCamelCase : Any = {} for token_ids in nested_token_ids: _lowerCamelCase : Dict = root for tidx, token_id in enumerate(__lowerCAmelCase ): if token_id not in level: _lowerCamelCase : List[Any] = {} _lowerCamelCase : Union[str, Any] = level[token_id] if no_subsets and self.has_subsets(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError( '''Each list in `nested_token_ids` can\'t be a complete subset of another list, but is''' f''' {nested_token_ids}.''' ) _lowerCamelCase : Dict = root def SCREAMING_SNAKE_CASE ( self : List[Any] , __lowerCAmelCase : int ): """simple docstring""" _lowerCamelCase : List[Any] = self.trie for current_token in current_seq: _lowerCamelCase : str = start[current_token] _lowerCamelCase : Any = list(start.keys() ) return next_tokens def SCREAMING_SNAKE_CASE ( self : Any , __lowerCAmelCase : Union[str, Any] ): """simple docstring""" _lowerCamelCase : Dict = self.next_tokens(__lowerCAmelCase ) return len(__lowerCAmelCase ) == 0 def SCREAMING_SNAKE_CASE ( self : List[Any] , __lowerCAmelCase : int ): """simple docstring""" _lowerCamelCase : Dict = list(root.values() ) if len(__lowerCAmelCase ) == 0: return 1 else: return sum([self.count_leaves(__lowerCAmelCase ) for nn in next_nodes] ) def SCREAMING_SNAKE_CASE ( self : Any , __lowerCAmelCase : int , __lowerCAmelCase : str ): """simple docstring""" _lowerCamelCase : Optional[Any] = self.count_leaves(__lowerCAmelCase ) return len(__lowerCAmelCase ) != leaf_count class __snake_case ( _lowercase): def __init__( self : Dict , __lowerCAmelCase : List[List[int]] ): """simple docstring""" super(__lowerCAmelCase , self ).__init__() if not isinstance(__lowerCAmelCase , __lowerCAmelCase ) or len(__lowerCAmelCase ) == 0: raise ValueError(f'''`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.''' ) if any(not isinstance(__lowerCAmelCase , __lowerCAmelCase ) for token_ids in nested_token_ids ): raise ValueError(f'''`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.''' ) if any( any((not isinstance(__lowerCAmelCase , __lowerCAmelCase ) or token_id < 0) for token_id in token_ids ) for token_ids in nested_token_ids ): raise ValueError( f'''Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.''' ) _lowerCamelCase : str = DisjunctiveTrie(__lowerCAmelCase ) _lowerCamelCase : Any = nested_token_ids _lowerCamelCase : List[Any] = self.trie.max_height _lowerCamelCase : Optional[int] = [] _lowerCamelCase : List[str] = False def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" _lowerCamelCase : Any = self.trie.next_tokens(self.current_seq ) if len(__lowerCAmelCase ) == 0: return None else: return token_list def SCREAMING_SNAKE_CASE ( self : List[Any] , __lowerCAmelCase : int ): """simple docstring""" if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(__lowerCAmelCase )}''' ) _lowerCamelCase : int = self.trie.next_tokens(self.current_seq ) return token_id in next_tokens def SCREAMING_SNAKE_CASE ( self : int , __lowerCAmelCase : int ): """simple docstring""" if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(__lowerCAmelCase )}''' ) _lowerCamelCase : int = False _lowerCamelCase : str = False _lowerCamelCase : Optional[Any] = False if self.does_advance(__lowerCAmelCase ): self.current_seq.append(__lowerCAmelCase ) _lowerCamelCase : List[str] = True else: _lowerCamelCase : List[Any] = True self.reset() _lowerCamelCase : List[str] = self.trie.reached_leaf(self.current_seq ) _lowerCamelCase : int = completed return stepped, completed, reset def SCREAMING_SNAKE_CASE ( self : Optional[Any] ): """simple docstring""" _lowerCamelCase : Dict = False _lowerCamelCase : List[str] = [] def SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" if self.completed: # since this can be completed without reaching max height return 0 else: return self.seqlen - len(self.current_seq ) def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : str=False ): """simple docstring""" _lowerCamelCase : Optional[Any] = DisjunctiveConstraint(self.token_ids ) if stateful: _lowerCamelCase : int = self.seqlen _lowerCamelCase : List[str] = self.current_seq _lowerCamelCase : Union[str, Any] = self.completed return new_constraint class __snake_case : def __init__( self : str , __lowerCAmelCase : List[Constraint] ): """simple docstring""" _lowerCamelCase : Any = constraints # max # of steps required to fulfill a given constraint _lowerCamelCase : str = max([c.seqlen for c in constraints] ) _lowerCamelCase : List[Any] = len(__lowerCAmelCase ) _lowerCamelCase : str = False self.init_state() def SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" _lowerCamelCase : Union[str, Any] = [] _lowerCamelCase : List[str] = None _lowerCamelCase : Any = [constraint.copy(stateful=__lowerCAmelCase ) for constraint in self.constraints] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" _lowerCamelCase : str = 0 if self.inprogress_constraint: # extra points for having a constraint mid-fulfilled add += self.max_seqlen - self.inprogress_constraint.remaining() return (len(self.complete_constraints ) * self.max_seqlen) + add def SCREAMING_SNAKE_CASE ( self : str ): """simple docstring""" _lowerCamelCase : Union[str, Any] = [] if self.inprogress_constraint is None: for constraint in self.pending_constraints: # "pending" == "unfulfilled yet" _lowerCamelCase : Union[str, Any] = constraint.advance() if isinstance(__lowerCAmelCase , __lowerCAmelCase ): token_list.append(__lowerCAmelCase ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): token_list.extend(__lowerCAmelCase ) else: _lowerCamelCase : Any = self.inprogress_constraint.advance() if isinstance(__lowerCAmelCase , __lowerCAmelCase ): token_list.append(__lowerCAmelCase ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): token_list.extend(__lowerCAmelCase ) if len(__lowerCAmelCase ) == 0: return None else: return token_list def SCREAMING_SNAKE_CASE ( self : Dict , __lowerCAmelCase : Optional[List[int]] ): """simple docstring""" self.init_state() if token_ids is not None: for token in token_ids: # completes or steps **one** constraint _lowerCamelCase , _lowerCamelCase : str = self.add(__lowerCAmelCase ) # the entire list of constraints are fulfilled if self.completed: break def SCREAMING_SNAKE_CASE ( self : List[str] , __lowerCAmelCase : int ): """simple docstring""" if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError(f'''`token_id` should be an `int`, but is `{token_id}`.''' ) _lowerCamelCase , _lowerCamelCase : List[str] = False, False if self.completed: _lowerCamelCase : int = True _lowerCamelCase : Optional[Any] = False return complete, stepped if self.inprogress_constraint is not None: # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current # job, simply update the state _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : Tuple = self.inprogress_constraint.update(__lowerCAmelCase ) if reset: # 1. If the next token breaks the progress, then we must restart. # e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books". # But that doesn't mean we self.init_state(), since we only reset the state for this particular # constraint, not the full list of constraints. self.pending_constraints.append(self.inprogress_constraint.copy(stateful=__lowerCAmelCase ) ) _lowerCamelCase : List[str] = None if complete: # 2. If the next token completes the constraint, move it to completed list, set # inprogress to None. If there are no pending constraints either, then this full list of constraints # is complete. self.complete_constraints.append(self.inprogress_constraint ) _lowerCamelCase : str = None if len(self.pending_constraints ) == 0: # we're done! _lowerCamelCase : Any = True else: # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list # of constraints? for cidx, pending_constraint in enumerate(self.pending_constraints ): if pending_constraint.does_advance(__lowerCAmelCase ): _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : Any = pending_constraint.update(__lowerCAmelCase ) if not stepped: raise Exception( '''`constraint.update(token_id)` is not yielding incremental progress, ''' '''even though `constraint.does_advance(token_id)` is true.''' ) if complete: self.complete_constraints.append(__lowerCAmelCase ) _lowerCamelCase : Optional[int] = None if not complete and stepped: _lowerCamelCase : Union[str, Any] = pending_constraint if complete or stepped: # If we made any progress at all, then it's at least not a "pending constraint". _lowerCamelCase : Union[str, Any] = ( self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :] ) if len(self.pending_constraints ) == 0 and self.inprogress_constraint is None: # If there's no longer any pending after this and no inprogress either, then we must be # complete. _lowerCamelCase : List[Any] = True break # prevent accidentally stepping through multiple constraints with just one token. return complete, stepped def SCREAMING_SNAKE_CASE ( self : Dict , __lowerCAmelCase : Dict=True ): """simple docstring""" _lowerCamelCase : Union[str, Any] = ConstraintListState(self.constraints ) # we actually never though self.constraints objects # throughout this process. So it's at initialization state. if stateful: _lowerCamelCase : Optional[Any] = [ constraint.copy(stateful=__lowerCAmelCase ) for constraint in self.complete_constraints ] if self.inprogress_constraint is not None: _lowerCamelCase : List[str] = self.inprogress_constraint.copy(stateful=__lowerCAmelCase ) _lowerCamelCase : Optional[int] = [constraint.copy() for constraint in self.pending_constraints] return new_state
72
"""simple docstring""" from collections import defaultdict def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = first_str.lower().strip() __SCREAMING_SNAKE_CASE = second_str.lower().strip() # Remove whitespace __SCREAMING_SNAKE_CASE = first_str.replace(""" """ , """""" ) __SCREAMING_SNAKE_CASE = second_str.replace(""" """ , """""" ) # Strings of different lengths are not anagrams if len(UpperCamelCase_ ) != len(UpperCamelCase_ ): return False # Default values for count should be 0 __SCREAMING_SNAKE_CASE = defaultdict(UpperCamelCase_ ) # For each character in input strings, # increment count in the corresponding for i in range(len(UpperCamelCase_ ) ): count[first_str[i]] += 1 count[second_str[i]] -= 1 return all(_count == 0 for _count in count.values() ) if __name__ == "__main__": from doctest import testmod testmod() __magic_name__ = input("Enter the first string ").strip() __magic_name__ = input("Enter the second string ").strip() __magic_name__ = check_anagrams(input_a, input_b) print(F"""{input_a} and {input_b} are {"" if status else "not "}anagrams.""")
100
0
import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class UpperCamelCase__ ( __lowercase ,unittest.TestCase ): _SCREAMING_SNAKE_CASE : str = VideoToVideoSDPipeline _SCREAMING_SNAKE_CASE : Optional[Any] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"} ) - {"image", "width", "height"} _SCREAMING_SNAKE_CASE : Optional[int] = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"} ) - {"image"} _SCREAMING_SNAKE_CASE : Any = PipelineTesterMixin.required_optional_params - {"latents"} _SCREAMING_SNAKE_CASE : Union[str, Any] = False # No `output_type`. _SCREAMING_SNAKE_CASE : Any = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback", "callback_steps", ] ) def lowerCAmelCase (self : Any ): torch.manual_seed(0 ) __a : Tuple = UNetaDConditionModel( block_out_channels=(3_2, 6_4, 6_4, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=4 , out_channels=4 , down_block_types=('''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''DownBlock3D''') , up_block_types=('''UpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''') , cross_attention_dim=3_2 , attention_head_dim=4 , ) __a : str = 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 : Optional[Any] = AutoencoderKL( block_out_channels=[3_2, 6_4] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=1_2_8 , ) torch.manual_seed(0 ) __a : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , hidden_act='''gelu''' , projection_dim=5_1_2 , ) __a : List[str] = CLIPTextModel(snake_case_ ) __a : List[Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a : Optional[int] = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, } return components def lowerCAmelCase (self : Optional[Any] , snake_case_ : List[str] , snake_case_ : Union[str, Any]=0 ): # 3 frames __a : Union[str, Any] = floats_tensor((1, 3, 3, 3_2, 3_2) , rng=random.Random(snake_case_ ) ).to(snake_case_ ) if str(snake_case_ ).startswith('''mps''' ): __a : int = torch.manual_seed(snake_case_ ) else: __a : str = torch.Generator(device=snake_case_ ).manual_seed(snake_case_ ) __a : Any = { '''prompt''': '''A painting of a squirrel eating a burger''', '''video''': video, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''pt''', } return inputs def lowerCAmelCase (self : int ): __a : Union[str, Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a : Optional[Any] = self.get_dummy_components() __a : Optional[Any] = VideoToVideoSDPipeline(**snake_case_ ) __a : Optional[int] = sd_pipe.to(snake_case_ ) sd_pipe.set_progress_bar_config(disable=snake_case_ ) __a : Optional[Any] = self.get_dummy_inputs(snake_case_ ) __a : List[str] = '''np''' __a : List[str] = sd_pipe(**snake_case_ ).frames __a : List[Any] = frames[0][-3:, -3:, -1] assert frames[0].shape == (3_2, 3_2, 3) __a : Dict = np.array([1_0_6, 1_1_7, 1_1_3, 1_7_4, 1_3_7, 1_1_2, 1_4_8, 1_5_1, 1_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def lowerCAmelCase (self : Tuple ): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=snake_case_ , expected_max_diff=5E-3 ) @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''' ) def lowerCAmelCase (self : Optional[int] ): pass @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''' ) def lowerCAmelCase (self : int ): pass @unittest.skip(reason='''`num_images_per_prompt` argument is not supported for this pipeline.''' ) def lowerCAmelCase (self : List[str] ): pass def lowerCAmelCase (self : Dict ): return super().test_progress_bar() @slow @skip_mps class UpperCamelCase__ ( unittest.TestCase ): def lowerCAmelCase (self : List[Any] ): __a : List[Any] = VideoToVideoSDPipeline.from_pretrained('''cerspense/zeroscope_v2_XL''' , torch_dtype=torch.floataa ) pipe.enable_model_cpu_offload() # 10 frames __a : Dict = torch.Generator(device='''cpu''' ).manual_seed(0 ) __a : List[Any] = torch.randn((1, 1_0, 3, 1_0_2_4, 5_7_6) , generator=snake_case_ ) __a : str = video.to('''cuda''' ) __a : Union[str, Any] = '''Spiderman is surfing''' __a : Optional[int] = pipe(snake_case_ , video=snake_case_ , generator=snake_case_ , num_inference_steps=3 , output_type='''pt''' ).frames __a : Optional[int] = np.array([-1.045_8984, -1.127_9297, -0.966_3086, -0.9150_3906, -0.7509_7656] ) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array ).sum() < 1E-2
90
def __UpperCamelCase ( lowerCAmelCase__ : list ): def merge(lowerCAmelCase__ : list , lowerCAmelCase__ : list ) -> list: def _merge(): while left and right: yield (left if left[0] <= right[0] else right).pop(0 ) yield from left yield from right return list(_merge() ) if len(lowerCAmelCase__ ) <= 1: return collection __a : str = len(lowerCAmelCase__ ) // 2 return merge(merge_sort(collection[:mid] ) , merge_sort(collection[mid:] ) ) if __name__ == "__main__": import doctest doctest.testmod() lowercase__ =input('Enter numbers separated by a comma:\n').strip() lowercase__ =[int(item) for item in user_input.split(',')] print(*merge_sort(unsorted), sep=',')
90
1