code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" from sympy import diff, lambdify, symbols from sympy.functions import * # noqa: F403 def __UpperCAmelCase ( lowercase ,lowercase ,lowercase = "x" ,lowercase = 10**-10 ,lowercase = 1 ,): """simple docstring""" _UpperCAmelCase = symbols(lowercase ) _UpperCAmelCase = lambdify(lowercase ,lowercase ) _UpperCAmelCase = lambdify(lowercase ,diff(lowercase ,lowercase ) ) _UpperCAmelCase = starting_point while True: if diff_function(lowercase ) != 0: _UpperCAmelCase = prev_guess - multiplicity * func(lowercase ) / diff_function( lowercase ) else: raise ZeroDivisionError("""Could not find root""" ) from None # Precision is checked by comparing the difference of consecutive guesses if abs(next_guess - prev_guess ) < precision: return next_guess _UpperCAmelCase = next_guess # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial # Find fourth Root of 5 print(F'''The root of x**4 - 5 = 0 is {newton_raphson("x**4 -5", 0.4 +5j)}''') # Find value of e print( """The root of log(y) - 1 = 0 is """, F'''{newton_raphson("log(y) - 1", 2, variable="y")}''', ) # Exponential Roots print( """The root of exp(x) - 1 = 0 is""", F'''{newton_raphson("exp(x) - 1", 1_0, precision=0.005)}''', ) # Find root of cos(x) print(F'''The root of cos(x) = 0 is {newton_raphson("cos(x)", 0)}''')
355
"""simple docstring""" import csv import tweepy # Twitter API credentials UpperCAmelCase__ = """""" UpperCAmelCase__ = """""" UpperCAmelCase__ = """""" UpperCAmelCase__ = """""" def __UpperCAmelCase ( lowercase ): """simple docstring""" # authorize twitter, initialize tweepy _UpperCAmelCase = tweepy.OAuthHandler(lowercase ,lowercase ) auth.set_access_token(lowercase ,lowercase ) _UpperCAmelCase = tweepy.API(lowercase ) # initialize a list to hold all the tweepy Tweets _UpperCAmelCase = [] # make initial request for most recent tweets (200 is the maximum allowed count) _UpperCAmelCase = api.user_timeline(screen_name=lowercase ,count=2_00 ) # save most recent tweets alltweets.extend(lowercase ) # save the id of the oldest tweet less one _UpperCAmelCase = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(lowercase ) > 0: print(f'''getting tweets before {oldest}''' ) # all subsequent requests use the max_id param to prevent duplicates _UpperCAmelCase = api.user_timeline( screen_name=lowercase ,count=2_00 ,max_id=lowercase ) # save most recent tweets alltweets.extend(lowercase ) # update the id of the oldest tweet less one _UpperCAmelCase = alltweets[-1].id - 1 print(f'''...{len(lowercase )} tweets downloaded so far''' ) # transform the tweepy tweets into a 2D array that will populate the csv _UpperCAmelCase = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(f'''new_{screen_name}_tweets.csv''' ,"""w""" ) as f: _UpperCAmelCase = csv.writer(lowercase ) writer.writerow(["""id""", """created_at""", """text"""] ) writer.writerows(lowercase ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets("""FirePing32""")
30
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import _LazyModule UpperCAmelCase__ = {"""processing_wav2vec2_with_lm""": ["""Wav2Vec2ProcessorWithLM"""]} if TYPE_CHECKING: from .processing_wavaveca_with_lm import WavaVecaProcessorWithLM else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
356
"""simple docstring""" import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = {} _UpperCAmelCase = tokenizer(example["""content"""] ,truncation=lowercase )["""input_ids"""] _UpperCAmelCase = len(example["""content"""] ) / len(output["""input_ids"""] ) return output UpperCAmelCase__ = HfArgumentParser(PretokenizationArguments) UpperCAmelCase__ = parser.parse_args() if args.num_workers is None: UpperCAmelCase__ = multiprocessing.cpu_count() UpperCAmelCase__ = AutoTokenizer.from_pretrained(args.tokenizer_dir) UpperCAmelCase__ = time.time() UpperCAmelCase__ = load_dataset(args.dataset_name, split="""train""") print(F'''Dataset loaded in {time.time()-t_start:.2f}s''') UpperCAmelCase__ = time.time() UpperCAmelCase__ = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ """repo_name""", """path""", """copies""", """size""", """content""", """license""", """hash""", """line_mean""", """line_max""", """alpha_frac""", """autogenerated""", ], ) print(F'''Dataset tokenized in {time.time()-t_start:.2f}s''') UpperCAmelCase__ = time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
30
0
import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a ( unittest.TestCase ): def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = """ylacombe/bark-small""" _UpperCAmelCase = tempfile.mkdtemp() _UpperCAmelCase = """en_speaker_1""" _UpperCAmelCase = """This is a test string""" _UpperCAmelCase = """speaker_embeddings_path.json""" _UpperCAmelCase = """speaker_embeddings""" def lowerCAmelCase_ ( self : Optional[int] , **__lowerCAmelCase : Dict ): return AutoTokenizer.from_pretrained(self.checkpoint , **__lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): shutil.rmtree(self.tmpdirname ) def lowerCAmelCase_ ( self : int ): _UpperCAmelCase = self.get_tokenizer() _UpperCAmelCase = BarkProcessor(tokenizer=__lowerCAmelCase ) processor.save_pretrained(self.tmpdirname ) _UpperCAmelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) _UpperCAmelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) _UpperCAmelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token="""(BOS)""" , eos_token="""(EOS)""" , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) _UpperCAmelCase = 35 _UpperCAmelCase = 2 _UpperCAmelCase = 8 _UpperCAmelCase = { """semantic_prompt""": np.ones(__lowerCAmelCase ), """coarse_prompt""": np.ones((nb_codebooks_coarse, seq_len) ), """fine_prompt""": np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset _UpperCAmelCase = processor(text=self.input_string , voice_preset=__lowerCAmelCase ) _UpperCAmelCase = inputs["""history_prompt"""] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(__lowerCAmelCase , np.array([] ) ).tolist() ) # test loading voice preset from npz file _UpperCAmelCase = os.path.join(self.tmpdirname , """file.npz""" ) np.savez(__lowerCAmelCase , **__lowerCAmelCase ) _UpperCAmelCase = processor(text=self.input_string , voice_preset=__lowerCAmelCase ) _UpperCAmelCase = inputs["""history_prompt"""] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(__lowerCAmelCase , np.array([] ) ).tolist() ) # test loading voice preset from the hub _UpperCAmelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def lowerCAmelCase_ ( self : List[str] ): _UpperCAmelCase = self.get_tokenizer() _UpperCAmelCase = BarkProcessor(tokenizer=__lowerCAmelCase ) _UpperCAmelCase = processor(text=self.input_string ) _UpperCAmelCase = tokenizer( self.input_string , padding="""max_length""" , max_length=256 , add_special_tokens=__lowerCAmelCase , return_attention_mask=__lowerCAmelCase , return_token_type_ids=__lowerCAmelCase , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
357
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { """microsoft/layoutlmv3-base""": """https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json""", } class a ( lowerCAmelCase_ ): _snake_case : Any = 'layoutlmv3' def __init__( self : Optional[Any] , __lowerCAmelCase : Tuple=5_0265 , __lowerCAmelCase : Union[str, Any]=768 , __lowerCAmelCase : str=12 , __lowerCAmelCase : int=12 , __lowerCAmelCase : Any=3072 , __lowerCAmelCase : Any="gelu" , __lowerCAmelCase : Any=0.1 , __lowerCAmelCase : Tuple=0.1 , __lowerCAmelCase : Any=512 , __lowerCAmelCase : List[str]=2 , __lowerCAmelCase : Optional[Any]=0.02 , __lowerCAmelCase : Optional[int]=1e-5 , __lowerCAmelCase : int=1 , __lowerCAmelCase : Optional[Any]=0 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : List[str]=1024 , __lowerCAmelCase : Any=128 , __lowerCAmelCase : int=128 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : List[Any]=32 , __lowerCAmelCase : Any=128 , __lowerCAmelCase : int=64 , __lowerCAmelCase : List[str]=256 , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : Any=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : Optional[Any]=224 , __lowerCAmelCase : List[Any]=3 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[Any]=None , **__lowerCAmelCase : Union[str, Any] , ): super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) _UpperCAmelCase = max_ad_position_embeddings _UpperCAmelCase = coordinate_size _UpperCAmelCase = shape_size _UpperCAmelCase = has_relative_attention_bias _UpperCAmelCase = rel_pos_bins _UpperCAmelCase = max_rel_pos _UpperCAmelCase = has_spatial_attention_bias _UpperCAmelCase = rel_ad_pos_bins _UpperCAmelCase = max_rel_ad_pos _UpperCAmelCase = text_embed _UpperCAmelCase = visual_embed _UpperCAmelCase = input_size _UpperCAmelCase = num_channels _UpperCAmelCase = patch_size _UpperCAmelCase = classifier_dropout class a ( lowerCAmelCase_ ): _snake_case : str = version.parse('1.12' ) @property def lowerCAmelCase_ ( self : Dict ): # The order of inputs is different for question answering and sequence classification if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def lowerCAmelCase_ ( self : List[Any] ): return 1e-5 @property def lowerCAmelCase_ ( self : List[str] ): return 12 def lowerCAmelCase_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ): setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX _UpperCAmelCase = compute_effective_axis_dimension( __lowerCAmelCase , 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 _UpperCAmelCase = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) _UpperCAmelCase = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence _UpperCAmelCase = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes _UpperCAmelCase = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) _UpperCAmelCase = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
30
0
"""simple docstring""" import collections.abc from typing import Optional, Tuple, Union import torch import torch.utils.checkpoint from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_poolformer import PoolFormerConfig UpperCAmelCase__ = logging.get_logger(__name__) # General docstring UpperCAmelCase__ = """PoolFormerConfig""" # Base docstring UpperCAmelCase__ = """sail/poolformer_s12""" UpperCAmelCase__ = [1, 5_1_2, 7, 7] # Image classification docstring UpperCAmelCase__ = """sail/poolformer_s12""" UpperCAmelCase__ = """tabby, tabby cat""" UpperCAmelCase__ = [ """sail/poolformer_s12""", # See all PoolFormer models at https://huggingface.co/models?filter=poolformer ] def __UpperCAmelCase ( lowercase ,lowercase = 0.0 ,lowercase = False ): """simple docstring""" if drop_prob == 0.0 or not training: return input _UpperCAmelCase = 1 - drop_prob _UpperCAmelCase = (input.shape[0],) + (1,) * (input.ndim - 1) # work with diff dim tensors, not just 2D ConvNets _UpperCAmelCase = keep_prob + torch.rand(lowercase ,dtype=input.dtype ,device=input.device ) random_tensor.floor_() # binarize _UpperCAmelCase = input.div(lowercase ) * random_tensor return output class a ( nn.Module ): def __init__( self : List[Any] , __lowerCAmelCase : Optional[float] = None ): super().__init__() _UpperCAmelCase = drop_prob def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : torch.Tensor ): return drop_path(__lowerCAmelCase , self.drop_prob , self.training ) def lowerCAmelCase_ ( self : Optional[Any] ): return "p={}".format(self.drop_prob ) class a ( nn.Module ): def __init__( self : List[str] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : str , __lowerCAmelCase : Any , __lowerCAmelCase : str=None ): super().__init__() _UpperCAmelCase = patch_size if isinstance(__lowerCAmelCase , collections.abc.Iterable ) else (patch_size, patch_size) _UpperCAmelCase = stride if isinstance(__lowerCAmelCase , collections.abc.Iterable ) else (stride, stride) _UpperCAmelCase = padding if isinstance(__lowerCAmelCase , collections.abc.Iterable ) else (padding, padding) _UpperCAmelCase = nn.Convad(__lowerCAmelCase , __lowerCAmelCase , kernel_size=__lowerCAmelCase , stride=__lowerCAmelCase , padding=__lowerCAmelCase ) _UpperCAmelCase = norm_layer(__lowerCAmelCase ) if norm_layer else nn.Identity() def lowerCAmelCase_ ( self : Optional[int] , __lowerCAmelCase : List[str] ): _UpperCAmelCase = self.projection(__lowerCAmelCase ) _UpperCAmelCase = self.norm(__lowerCAmelCase ) return embeddings class a ( nn.GroupNorm ): def __init__( self : int , __lowerCAmelCase : Dict , **__lowerCAmelCase : Tuple ): super().__init__(1 , __lowerCAmelCase , **__lowerCAmelCase ) class a ( nn.Module ): def __init__( self : Tuple , __lowerCAmelCase : int ): super().__init__() _UpperCAmelCase = nn.AvgPoolad(__lowerCAmelCase , stride=1 , padding=pool_size // 2 , count_include_pad=__lowerCAmelCase ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : Dict ): return self.pool(__lowerCAmelCase ) - hidden_states class a ( nn.Module ): def __init__( self : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any] ): super().__init__() _UpperCAmelCase = nn.Convad(__lowerCAmelCase , __lowerCAmelCase , 1 ) _UpperCAmelCase = nn.Convad(__lowerCAmelCase , __lowerCAmelCase , 1 ) _UpperCAmelCase = PoolFormerDropPath(__lowerCAmelCase ) if isinstance(config.hidden_act , __lowerCAmelCase ): _UpperCAmelCase = ACTaFN[config.hidden_act] else: _UpperCAmelCase = config.hidden_act def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : Optional[Any] ): _UpperCAmelCase = self.conva(__lowerCAmelCase ) _UpperCAmelCase = self.act_fn(__lowerCAmelCase ) _UpperCAmelCase = self.drop(__lowerCAmelCase ) _UpperCAmelCase = self.conva(__lowerCAmelCase ) _UpperCAmelCase = self.drop(__lowerCAmelCase ) return hidden_states class a ( nn.Module ): def __init__( self : Tuple , __lowerCAmelCase : Any , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : int ): super().__init__() _UpperCAmelCase = PoolFormerPooling(__lowerCAmelCase ) _UpperCAmelCase = PoolFormerOutput(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = PoolFormerGroupNorm(__lowerCAmelCase ) _UpperCAmelCase = PoolFormerGroupNorm(__lowerCAmelCase ) # Useful for training neural nets _UpperCAmelCase = PoolFormerDropPath(__lowerCAmelCase ) if drop_path > 0.0 else nn.Identity() _UpperCAmelCase = config.use_layer_scale if config.use_layer_scale: _UpperCAmelCase = nn.Parameter( config.layer_scale_init_value * torch.ones((__lowerCAmelCase) ) , requires_grad=__lowerCAmelCase ) _UpperCAmelCase = nn.Parameter( config.layer_scale_init_value * torch.ones((__lowerCAmelCase) ) , requires_grad=__lowerCAmelCase ) def lowerCAmelCase_ ( self : Any , __lowerCAmelCase : int ): if self.use_layer_scale: _UpperCAmelCase = self.pooling(self.before_norm(__lowerCAmelCase ) ) _UpperCAmelCase = self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * pooling_output # First residual connection _UpperCAmelCase = hidden_states + self.drop_path(__lowerCAmelCase ) _UpperCAmelCase = () _UpperCAmelCase = self.output(self.after_norm(__lowerCAmelCase ) ) _UpperCAmelCase = self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * layer_output # Second residual connection _UpperCAmelCase = hidden_states + self.drop_path(__lowerCAmelCase ) _UpperCAmelCase = (output,) + outputs return outputs else: _UpperCAmelCase = self.drop_path(self.pooling(self.before_norm(__lowerCAmelCase ) ) ) # First residual connection _UpperCAmelCase = pooling_output + hidden_states _UpperCAmelCase = () # Second residual connection inside the PoolFormerOutput block _UpperCAmelCase = self.drop_path(self.output(self.after_norm(__lowerCAmelCase ) ) ) _UpperCAmelCase = hidden_states + layer_output _UpperCAmelCase = (output,) + outputs return outputs class a ( nn.Module ): def __init__( self : Tuple , __lowerCAmelCase : List[Any] ): super().__init__() _UpperCAmelCase = config # stochastic depth decay rule _UpperCAmelCase = [x.item() for x in torch.linspace(0 , config.drop_path_rate , sum(config.depths ) )] # patch embeddings _UpperCAmelCase = [] for i in range(config.num_encoder_blocks ): embeddings.append( PoolFormerEmbeddings( patch_size=config.patch_sizes[i] , stride=config.strides[i] , padding=config.padding[i] , num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1] , hidden_size=config.hidden_sizes[i] , ) ) _UpperCAmelCase = nn.ModuleList(__lowerCAmelCase ) # Transformer blocks _UpperCAmelCase = [] _UpperCAmelCase = 0 for i in range(config.num_encoder_blocks ): # each block consists of layers _UpperCAmelCase = [] if i != 0: cur += config.depths[i - 1] for j in range(config.depths[i] ): layers.append( PoolFormerLayer( __lowerCAmelCase , num_channels=config.hidden_sizes[i] , pool_size=config.pool_size , hidden_size=config.hidden_sizes[i] , intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio ) , drop_path=dpr[cur + j] , ) ) blocks.append(nn.ModuleList(__lowerCAmelCase ) ) _UpperCAmelCase = nn.ModuleList(__lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any]=False , __lowerCAmelCase : Union[str, Any]=True ): _UpperCAmelCase = () if output_hidden_states else None _UpperCAmelCase = pixel_values for idx, layers in enumerate(zip(self.patch_embeddings , self.block ) ): _UpperCAmelCase , _UpperCAmelCase = layers # Get patch embeddings from hidden_states _UpperCAmelCase = embedding_layer(__lowerCAmelCase ) # Send the embeddings through the blocks for _, blk in enumerate(__lowerCAmelCase ): _UpperCAmelCase = blk(__lowerCAmelCase ) _UpperCAmelCase = layer_outputs[0] if output_hidden_states: _UpperCAmelCase = all_hidden_states + (hidden_states,) if not return_dict: return tuple(v for v in [hidden_states, all_hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=__lowerCAmelCase , hidden_states=__lowerCAmelCase ) class a ( lowerCAmelCase_ ): _snake_case : str = PoolFormerConfig _snake_case : Dict = 'poolformer' _snake_case : List[str] = 'pixel_values' _snake_case : str = True def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : Union[str, Any] ): if isinstance(__lowerCAmelCase , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(__lowerCAmelCase , nn.LayerNorm ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[Any]=False ): if isinstance(__lowerCAmelCase , __lowerCAmelCase ): _UpperCAmelCase = value UpperCAmelCase__ = r""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`PoolFormerConfig`]): 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. """ UpperCAmelCase__ = r""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`PoolFormerImageProcessor.__call__`] for details. """ @add_start_docstrings( 'The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.' , lowerCAmelCase_ , ) class a ( lowerCAmelCase_ ): def __init__( self : Union[str, Any] , __lowerCAmelCase : Tuple ): super().__init__(__lowerCAmelCase ) _UpperCAmelCase = config _UpperCAmelCase = PoolFormerEncoder(__lowerCAmelCase ) # Initialize weights and apply final processing self.post_init() def lowerCAmelCase_ ( self : List[Any] ): return self.embeddings.patch_embeddings @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 lowerCAmelCase_ ( self : str , __lowerCAmelCase : Optional[torch.FloatTensor] = None , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Optional[bool] = None , ): _UpperCAmelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _UpperCAmelCase = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("""You have to specify pixel_values""" ) _UpperCAmelCase = self.encoder( __lowerCAmelCase , output_hidden_states=__lowerCAmelCase , return_dict=__lowerCAmelCase , ) _UpperCAmelCase = encoder_outputs[0] if not return_dict: return (sequence_output, None) + encoder_outputs[1:] return BaseModelOutputWithNoAttention( last_hidden_state=__lowerCAmelCase , hidden_states=encoder_outputs.hidden_states , ) class a ( nn.Module ): def __init__( self : Dict , __lowerCAmelCase : str ): super().__init__() _UpperCAmelCase = nn.Linear(config.hidden_size , config.hidden_size ) def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : Optional[Any] ): _UpperCAmelCase = self.dense(__lowerCAmelCase ) return output @add_start_docstrings( '\n PoolFormer Model transformer with an image classification head on top\n ' , lowerCAmelCase_ , ) class a ( lowerCAmelCase_ ): def __init__( self : int , __lowerCAmelCase : Union[str, Any] ): super().__init__(__lowerCAmelCase ) _UpperCAmelCase = config.num_labels _UpperCAmelCase = PoolFormerModel(__lowerCAmelCase ) # Final norm _UpperCAmelCase = PoolFormerGroupNorm(config.hidden_sizes[-1] ) # Classifier head _UpperCAmelCase = ( 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 lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : Optional[torch.FloatTensor] = None , __lowerCAmelCase : Optional[torch.LongTensor] = None , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Optional[bool] = None , ): _UpperCAmelCase = return_dict if return_dict is not None else self.config.use_return_dict _UpperCAmelCase = self.poolformer( __lowerCAmelCase , output_hidden_states=__lowerCAmelCase , return_dict=__lowerCAmelCase , ) _UpperCAmelCase = outputs[0] _UpperCAmelCase = self.classifier(self.norm(__lowerCAmelCase ).mean([-2, -1] ) ) _UpperCAmelCase = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: _UpperCAmelCase = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): _UpperCAmelCase = """single_label_classification""" else: _UpperCAmelCase = """multi_label_classification""" if self.config.problem_type == "regression": _UpperCAmelCase = MSELoss() if self.num_labels == 1: _UpperCAmelCase = loss_fct(logits.squeeze() , labels.squeeze() ) else: _UpperCAmelCase = loss_fct(__lowerCAmelCase , __lowerCAmelCase ) elif self.config.problem_type == "single_label_classification": _UpperCAmelCase = CrossEntropyLoss() _UpperCAmelCase = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": _UpperCAmelCase = BCEWithLogitsLoss() _UpperCAmelCase = loss_fct(__lowerCAmelCase , __lowerCAmelCase ) if not return_dict: _UpperCAmelCase = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=__lowerCAmelCase , logits=__lowerCAmelCase , hidden_states=outputs.hidden_states )
358
"""simple docstring""" import csv from collections import defaultdict from dataclasses import dataclass, field from typing import List, Optional import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import ScalarFormatter from transformers import HfArgumentParser def __UpperCAmelCase ( lowercase=None ,lowercase=None ): """simple docstring""" return field(default_factory=lambda: default ,metadata=lowercase ) @dataclass class a : _snake_case : str = field( metadata={'help': 'The csv file to plot.'} , ) _snake_case : bool = field( default=lowerCAmelCase_ , metadata={'help': 'Whether to plot along batch size or sequence length. Defaults to sequence length.'} , ) _snake_case : bool = field( default=lowerCAmelCase_ , metadata={'help': 'Whether the csv file has time results or memory results. Defaults to memory results.'} , ) _snake_case : bool = field( default=lowerCAmelCase_ , metadata={'help': 'Disable logarithmic scale when plotting'} , ) _snake_case : bool = field( default=lowerCAmelCase_ , metadata={ 'help': 'Whether the csv file has training results or inference results. Defaults to inference results.' } , ) _snake_case : Optional[str] = field( default=lowerCAmelCase_ , metadata={'help': 'Filename under which the plot will be saved. If unused no plot is saved.'} , ) _snake_case : Optional[List[str]] = list_field( default=lowerCAmelCase_ , metadata={'help': 'List of model names that are used instead of the ones in the csv file.'} ) def __UpperCAmelCase ( lowercase ): """simple docstring""" try: int(lowercase ) return True except ValueError: return False def __UpperCAmelCase ( lowercase ): """simple docstring""" try: float(lowercase ) return True except ValueError: return False class a : def __init__( self : int , __lowerCAmelCase : Union[str, Any] ): _UpperCAmelCase = args _UpperCAmelCase = defaultdict(lambda: {"bsz": [], "seq_len": [], "result": {}} ) with open(self.args.csv_file , newline="""""" ) as csv_file: _UpperCAmelCase = csv.DictReader(__lowerCAmelCase ) for row in reader: _UpperCAmelCase = row["""model"""] self.result_dict[model_name]["bsz"].append(int(row["""batch_size"""] ) ) self.result_dict[model_name]["seq_len"].append(int(row["""sequence_length"""] ) ) if can_convert_to_int(row["""result"""] ): # value is not None _UpperCAmelCase = int(row["""result"""] ) elif can_convert_to_float(row["""result"""] ): # value is not None _UpperCAmelCase = float(row["""result"""] ) def lowerCAmelCase_ ( self : Optional[Any] ): _UpperCAmelCase , _UpperCAmelCase = plt.subplots() _UpperCAmelCase = """Time usage""" if self.args.is_time else """Memory usage""" _UpperCAmelCase = title_str + """ for training""" if self.args.is_train else title_str + """ for inference""" if not self.args.no_log_scale: # set logarithm scales ax.set_xscale("""log""" ) ax.set_yscale("""log""" ) for axis in [ax.xaxis, ax.yaxis]: axis.set_major_formatter(ScalarFormatter() ) for model_name_idx, model_name in enumerate(self.result_dict.keys() ): _UpperCAmelCase = sorted(set(self.result_dict[model_name]["""bsz"""] ) ) _UpperCAmelCase = sorted(set(self.result_dict[model_name]["""seq_len"""] ) ) _UpperCAmelCase = self.result_dict[model_name]["""result"""] ((_UpperCAmelCase) , (_UpperCAmelCase)) = ( (batch_sizes, sequence_lengths) if self.args.plot_along_batch else (sequence_lengths, batch_sizes) ) _UpperCAmelCase = ( model_name if self.args.short_model_names is None else self.args.short_model_names[model_name_idx] ) for inner_loop_value in inner_loop_array: if self.args.plot_along_batch: _UpperCAmelCase = np.asarray( [results[(x, inner_loop_value)] for x in x_axis_array if (x, inner_loop_value) in results] , dtype=__lowerCAmelCase , ) else: _UpperCAmelCase = np.asarray( [results[(inner_loop_value, x)] for x in x_axis_array if (inner_loop_value, x) in results] , dtype=np.floataa , ) ((_UpperCAmelCase) , (_UpperCAmelCase)) = ( ("""batch_size""", """len""") if self.args.plot_along_batch else ("""in #tokens""", """bsz""") ) _UpperCAmelCase = np.asarray(__lowerCAmelCase , __lowerCAmelCase )[: len(__lowerCAmelCase )] plt.scatter( __lowerCAmelCase , __lowerCAmelCase , label=f'''{label_model_name} - {inner_loop_label}: {inner_loop_value}''' ) plt.plot(__lowerCAmelCase , __lowerCAmelCase , """--""" ) title_str += f''' {label_model_name} vs.''' _UpperCAmelCase = title_str[:-4] _UpperCAmelCase = """Time in s""" if self.args.is_time else """Memory in MB""" # plot plt.title(__lowerCAmelCase ) plt.xlabel(__lowerCAmelCase ) plt.ylabel(__lowerCAmelCase ) plt.legend() if self.args.figure_png_file is not None: plt.savefig(self.args.figure_png_file ) else: plt.show() def __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = HfArgumentParser(lowercase ) _UpperCAmelCase = parser.parse_args_into_dataclasses()[0] _UpperCAmelCase = Plot(args=lowercase ) plot.plot() if __name__ == "__main__": main()
30
0
"""simple docstring""" def __UpperCAmelCase ( lowercase ): """simple docstring""" 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 = 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=""",""")
359
"""simple docstring""" import os import pytest from attr import dataclass UpperCAmelCase__ = """us-east-1""" # defaults region @dataclass class a : _snake_case : str _snake_case : Tuple = 'arn:aws:iam::558105141721:role/sagemaker_execution_role' _snake_case : List[Any] = { 'task_name': 'mnli', 'per_device_train_batch_size': 16, 'per_device_eval_batch_size': 16, 'do_train': True, 'do_eval': True, 'do_predict': True, 'output_dir': '/opt/ml/model', 'overwrite_output_dir': True, 'max_steps': 5_00, 'save_steps': 55_00, } _snake_case : Optional[Any] = {**hyperparameters, 'max_steps': 10_00} @property def lowerCAmelCase_ ( self : Optional[Any] ): if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def lowerCAmelCase_ ( self : Dict ): return f'''{self.framework}-transfromers-test''' @property def lowerCAmelCase_ ( self : Union[str, Any] ): return f'''./tests/sagemaker/scripts/{self.framework}''' @property def lowerCAmelCase_ ( self : Dict ): if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = SageMakerTestEnvironment(framework=request.cls.framework )
30
0
"""simple docstring""" from ..utils import DummyObject, requires_backends class a ( metaclass=lowerCAmelCase_ ): _snake_case : int = ['flax'] def __init__( self : Dict , *__lowerCAmelCase : Union[str, Any] , **__lowerCAmelCase : List[Any] ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Any , *__lowerCAmelCase : int , **__lowerCAmelCase : Optional[Any] ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : Optional[Any] , **__lowerCAmelCase : Union[str, Any] ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Dict = ['flax'] def __init__( self : Any , *__lowerCAmelCase : str , **__lowerCAmelCase : Optional[int] ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Optional[int] , *__lowerCAmelCase : Tuple , **__lowerCAmelCase : Any ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Dict , *__lowerCAmelCase : str , **__lowerCAmelCase : str ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Optional[Any] = ['flax'] def __init__( self : Optional[Any] , *__lowerCAmelCase : Optional[Any] , **__lowerCAmelCase : Any ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Optional[Any] , *__lowerCAmelCase : Dict , **__lowerCAmelCase : Tuple ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : Dict , **__lowerCAmelCase : List[str] ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Optional[Any] = ['flax'] def __init__( self : str , *__lowerCAmelCase : str , **__lowerCAmelCase : Optional[Any] ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : int , *__lowerCAmelCase : Dict , **__lowerCAmelCase : Optional[int] ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : int , *__lowerCAmelCase : Optional[Any] , **__lowerCAmelCase : List[Any] ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Dict = ['flax'] def __init__( self : Tuple , *__lowerCAmelCase : Union[str, Any] , **__lowerCAmelCase : Dict ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : Optional[int] , **__lowerCAmelCase : List[str] ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Union[str, Any] , *__lowerCAmelCase : Optional[Any] , **__lowerCAmelCase : List[Any] ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Any = ['flax'] def __init__( self : str , *__lowerCAmelCase : Dict , **__lowerCAmelCase : Union[str, Any] ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Dict , *__lowerCAmelCase : int , **__lowerCAmelCase : Tuple ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Dict , *__lowerCAmelCase : Tuple , **__lowerCAmelCase : Dict ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Tuple = ['flax'] def __init__( self : Tuple , *__lowerCAmelCase : Dict , **__lowerCAmelCase : List[str] ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : Union[str, Any] , **__lowerCAmelCase : List[str] ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Union[str, Any] , *__lowerCAmelCase : List[Any] , **__lowerCAmelCase : Any ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Optional[Any] = ['flax'] def __init__( self : Any , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : List[Any] , *__lowerCAmelCase : int , **__lowerCAmelCase : Optional[int] ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : Optional[int] , **__lowerCAmelCase : Optional[int] ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Any = ['flax'] def __init__( self : Dict , *__lowerCAmelCase : str , **__lowerCAmelCase : Optional[Any] ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : str , **__lowerCAmelCase : Any ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Union[str, Any] , *__lowerCAmelCase : Optional[Any] , **__lowerCAmelCase : List[str] ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : List[Any] = ['flax'] def __init__( self : Tuple , *__lowerCAmelCase : Optional[Any] , **__lowerCAmelCase : Any ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Dict , *__lowerCAmelCase : Optional[int] , **__lowerCAmelCase : List[str] ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : List[Any] , **__lowerCAmelCase : Optional[int] ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : List[str] = ['flax'] def __init__( self : Optional[int] , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : Any ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Dict , *__lowerCAmelCase : Optional[int] , **__lowerCAmelCase : Optional[Any] ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : Union[str, Any] , **__lowerCAmelCase : Any ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : Optional[Any] = ['flax'] def __init__( self : Any , *__lowerCAmelCase : Dict , **__lowerCAmelCase : Optional[int] ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : List[Any] , **__lowerCAmelCase : str ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : int , *__lowerCAmelCase : str , **__lowerCAmelCase : int ): requires_backends(cls , ["""flax"""] ) class a ( metaclass=lowerCAmelCase_ ): _snake_case : List[str] = ['flax'] def __init__( self : Optional[Any] , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : Dict ): requires_backends(self , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Tuple , *__lowerCAmelCase : int , **__lowerCAmelCase : int ): requires_backends(cls , ["""flax"""] ) @classmethod def lowerCAmelCase_ ( cls : Union[str, Any] , *__lowerCAmelCase : Dict , **__lowerCAmelCase : Union[str, Any] ): requires_backends(cls , ["""flax"""] )
360
"""simple docstring""" import string from math import logaa def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = document.translate( str.maketrans("""""" ,"""""" ,string.punctuation ) ).replace("""\n""" ,"""""" ) _UpperCAmelCase = document_without_punctuation.split(""" """ ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = corpus.lower().translate( str.maketrans("""""" ,"""""" ,string.punctuation ) ) # strip all punctuation and replace it with '' _UpperCAmelCase = corpus_without_punctuation.split("""\n""" ) _UpperCAmelCase = term.lower() return (len([doc for doc in docs if term in doc] ), len(lowercase )) def __UpperCAmelCase ( lowercase ,lowercase ,lowercase=False ): """simple docstring""" if smoothing: if n == 0: raise ValueError("""log10(0) is undefined.""" ) return round(1 + logaa(n / (1 + df) ) ,3 ) if df == 0: raise ZeroDivisionError("""df must be > 0""" ) elif n == 0: raise ValueError("""log10(0) is undefined.""" ) return round(logaa(n / df ) ,3 ) def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" return round(tf * idf ,3 )
30
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) UpperCAmelCase__ = {"""configuration_beit""": ["""BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BeitConfig""", """BeitOnnxConfig"""]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["""BeitFeatureExtractor"""] UpperCAmelCase__ = ["""BeitImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ """BEIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """BeitForImageClassification""", """BeitForMaskedImageModeling""", """BeitForSemanticSegmentation""", """BeitModel""", """BeitPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ """FlaxBeitForImageClassification""", """FlaxBeitForMaskedImageModeling""", """FlaxBeitModel""", """FlaxBeitPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
361
"""simple docstring""" import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging UpperCAmelCase__ = logging.get_logger(__name__) logging.set_verbosity_info() def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" if "xprophetnet" in prophetnet_checkpoint_path: _UpperCAmelCase = XLMProphetNetForConditionalGenerationOld.from_pretrained(lowercase ) _UpperCAmelCase , _UpperCAmelCase = XLMProphetNetForConditionalGeneration.from_pretrained( lowercase ,output_loading_info=lowercase ) else: _UpperCAmelCase = ProphetNetForConditionalGenerationOld.from_pretrained(lowercase ) _UpperCAmelCase , _UpperCAmelCase = ProphetNetForConditionalGeneration.from_pretrained( lowercase ,output_loading_info=lowercase ) _UpperCAmelCase = ["""key_proj""", """value_proj""", """query_proj"""] _UpperCAmelCase = { """self_attn""": """ngram_self_attn""", """cross_attn""": """encoder_attn""", """cross_attn_layer_norm""": """encoder_attn_layer_norm""", """feed_forward_layer_norm""": """final_layer_norm""", """feed_forward""": """""", """intermediate""": """fc1""", """output""": """fc2""", """key_proj""": """k_proj""", """query_proj""": """q_proj""", """value_proj""": """v_proj""", """word_embeddings""": """embed_tokens""", """embeddings_layer_norm""": """emb_layer_norm""", """relative_pos_embeddings""": """relative_linear""", """ngram_embeddings""": """ngram_input_embed""", """position_embeddings""": """embed_positions""", } for key in loading_info["missing_keys"]: _UpperCAmelCase = key.split(""".""" ) if attributes[0] == "lm_head": _UpperCAmelCase = prophet _UpperCAmelCase = prophet_old else: _UpperCAmelCase = prophet.prophetnet _UpperCAmelCase = prophet_old.model _UpperCAmelCase = False for attribute in attributes: if attribute in mapping: _UpperCAmelCase = mapping[attribute] if not hasattr(lowercase ,lowercase ) and len(lowercase ) > 0: _UpperCAmelCase = attribute elif hasattr(lowercase ,lowercase ): _UpperCAmelCase = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" _UpperCAmelCase = old_model.weight logger.info(f'''{attribute} is initialized.''' ) _UpperCAmelCase = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" _UpperCAmelCase = old_model.bias logger.info(f'''{attribute} is initialized''' ) _UpperCAmelCase = True break elif attribute in special_keys and hasattr(lowercase ,"""in_proj_weight""" ): _UpperCAmelCase = old_model.in_proj_weight.shape[0] // 3 _UpperCAmelCase = getattr(lowercase ,lowercase ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": _UpperCAmelCase = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) _UpperCAmelCase = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": _UpperCAmelCase = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) _UpperCAmelCase = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": _UpperCAmelCase = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) _UpperCAmelCase = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) _UpperCAmelCase = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." _UpperCAmelCase = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) _UpperCAmelCase = True break if attribute.isdigit(): _UpperCAmelCase = model[int(lowercase )] _UpperCAmelCase = old_model[int(lowercase )] else: _UpperCAmelCase = getattr(lowercase ,lowercase ) if old_attribute == "": _UpperCAmelCase = old_model else: if not hasattr(lowercase ,lowercase ): raise ValueError(f'''{old_model} does not have {old_attribute}''' ) _UpperCAmelCase = getattr(lowercase ,lowercase ) if not is_key_init: raise ValueError(f'''{key} was not correctly initialized!''' ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) prophet.save_pretrained(lowercase ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--prophetnet_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.""" ) UpperCAmelCase__ = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
30
0
"""simple docstring""" import argparse from tax import checkpoints from transformers import AutoConfig, FlaxAutoModelForSeqaSeqLM def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = AutoConfig.from_pretrained(lowercase ) _UpperCAmelCase = FlaxAutoModelForSeqaSeqLM.from_config(config=lowercase ) _UpperCAmelCase = checkpoints.load_tax_checkpoint(lowercase ) _UpperCAmelCase = """wi_0""" in tax_model["""target"""]["""encoder"""]["""layers_0"""]["""mlp"""] if config.model_type == "t5": _UpperCAmelCase = """SelfAttention""" if config.model_type == "longt5" and config.encoder_attention_type == "local": _UpperCAmelCase = """LocalSelfAttention""" elif config.model_type == "longt5" and config.encoder_attention_type == "transient-global": _UpperCAmelCase = """TransientGlobalSelfAttention""" else: raise ValueError( """Given config is expected to have `model_type='t5'`, or `model_type='longt5` with `encoder_attention_type`""" """ attribute with a value from ['local', 'transient-global].""" ) # Encoder for layer_index in range(config.num_layers ): _UpperCAmelCase = f'''layers_{str(lowercase )}''' # Self-Attention _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""key"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""out"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""query"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""value"""]["""kernel"""] # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""attention"""]["""T5LayerNorm_0"""]["""scale"""] # Layer Normalization _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""pre_attention_layer_norm"""]["""scale"""] if split_mlp_wi: _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""mlp"""]["""wi_0"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""mlp"""]["""wi_1"""]["""kernel"""] else: _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""mlp"""]["""wi"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""mlp"""]["""wo"""]["""kernel"""] # Layer Normalization _UpperCAmelCase = tax_model["""target"""]["""encoder"""][layer_name]["""pre_mlp_layer_norm"""]["""scale"""] # Assigning _UpperCAmelCase = flax_model.params["""encoder"""]["""block"""][str(lowercase )]["""layer"""] _UpperCAmelCase = tax_attention_key _UpperCAmelCase = tax_attention_out _UpperCAmelCase = tax_attention_query _UpperCAmelCase = tax_attention_value _UpperCAmelCase = tax_attention_layer_norm # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": _UpperCAmelCase = tax_global_layer_norm if split_mlp_wi: _UpperCAmelCase = tax_mlp_wi_a _UpperCAmelCase = tax_mlp_wi_a else: _UpperCAmelCase = tax_mlp_wi _UpperCAmelCase = tax_mlp_wo _UpperCAmelCase = tax_mlp_layer_norm _UpperCAmelCase = flax_model_encoder_layer_block # Only for layer 0: _UpperCAmelCase = tax_model["""target"""]["""encoder"""]["""relpos_bias"""]["""rel_embedding"""].T _UpperCAmelCase = tax_encoder_rel_embedding # Side/global relative position_bias + layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": _UpperCAmelCase = tax_model["""target"""]["""encoder"""]["""side_relpos_bias"""]["""rel_embedding"""].T _UpperCAmelCase = tax_encoder_global_rel_embedding # Assigning _UpperCAmelCase = tax_model["""target"""]["""encoder"""]["""encoder_norm"""]["""scale"""] _UpperCAmelCase = tax_encoder_norm # Decoder for layer_index in range(config.num_layers ): _UpperCAmelCase = f'''layers_{str(lowercase )}''' # Self-Attention _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""self_attention"""]["""key"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""self_attention"""]["""out"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""self_attention"""]["""query"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""self_attention"""]["""value"""]["""kernel"""] # Layer Normalization _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""pre_self_attention_layer_norm"""][ """scale""" ] # Encoder-Decoder-Attention _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""encoder_decoder_attention"""] _UpperCAmelCase = tax_enc_dec_attention_module["""key"""]["""kernel"""] _UpperCAmelCase = tax_enc_dec_attention_module["""out"""]["""kernel"""] _UpperCAmelCase = tax_enc_dec_attention_module["""query"""]["""kernel"""] _UpperCAmelCase = tax_enc_dec_attention_module["""value"""]["""kernel"""] # Layer Normalization _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""pre_cross_attention_layer_norm"""]["""scale"""] # MLP if split_mlp_wi: _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""mlp"""]["""wi_0"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""mlp"""]["""wi_1"""]["""kernel"""] else: _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""mlp"""]["""wi"""]["""kernel"""] _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""mlp"""]["""wo"""]["""kernel"""] # Layer Normalization _UpperCAmelCase = tax_model["""target"""]["""decoder"""][layer_name]["""pre_mlp_layer_norm"""]["""scale"""] # Assigning _UpperCAmelCase = flax_model.params["""decoder"""]["""block"""][str(lowercase )]["""layer"""] _UpperCAmelCase = tax_attention_key _UpperCAmelCase = tax_attention_out _UpperCAmelCase = tax_attention_query _UpperCAmelCase = tax_attention_value _UpperCAmelCase = tax_pre_attention_layer_norm _UpperCAmelCase = tax_enc_dec_attention_key _UpperCAmelCase = tax_enc_dec_attention_out _UpperCAmelCase = tax_enc_dec_attention_query _UpperCAmelCase = tax_enc_dec_attention_value _UpperCAmelCase = tax_cross_layer_norm if split_mlp_wi: _UpperCAmelCase = tax_mlp_wi_a _UpperCAmelCase = tax_mlp_wi_a else: _UpperCAmelCase = tax_mlp_wi _UpperCAmelCase = tax_mlp_wo _UpperCAmelCase = txa_mlp_layer_norm _UpperCAmelCase = flax_model_decoder_layer_block # Decoder Normalization _UpperCAmelCase = tax_model["""target"""]["""decoder"""]["""decoder_norm"""]["""scale"""] _UpperCAmelCase = txa_decoder_norm # Only for layer 0: _UpperCAmelCase = tax_model["""target"""]["""decoder"""]["""relpos_bias"""]["""rel_embedding"""].T _UpperCAmelCase = tax_decoder_rel_embedding # Token Embeddings _UpperCAmelCase = tax_model["""target"""]["""token_embedder"""]["""embedding"""] _UpperCAmelCase = txa_token_embeddings # LM Head (only in v1.1 and LongT5 checkpoints) if "logits_dense" in tax_model["target"]["decoder"]: _UpperCAmelCase = tax_model["""target"""]["""decoder"""]["""logits_dense"""]["""kernel"""] flax_model.save_pretrained(lowercase ) print("""T5X Model was sucessfully converted!""" ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--t5x_checkpoint_path""", default=None, type=str, required=True, help="""Path the T5X checkpoint.""" ) parser.add_argument("""--config_name""", default=None, type=str, required=True, help="""Config name of LongT5/T5 model.""") parser.add_argument( """--flax_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output FLAX model.""" ) UpperCAmelCase__ = parser.parse_args() convert_tax_checkpoint_to_flax(args.tax_checkpoint_path, args.config_name, args.flax_dump_folder_path)
362
"""simple docstring""" import mpmath # for roots of unity import numpy as np class a : def __init__( self : Tuple , __lowerCAmelCase : Dict=None , __lowerCAmelCase : Union[str, Any]=None ): # Input as list _UpperCAmelCase = list(poly_a or [0] )[:] _UpperCAmelCase = list(poly_b or [0] )[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() _UpperCAmelCase = len(self.polyA ) while self.polyB[-1] == 0: self.polyB.pop() _UpperCAmelCase = len(self.polyB ) # Add 0 to make lengths equal a power of 2 _UpperCAmelCase = int( 2 ** np.ceil(np.loga(len(self.polyA ) + len(self.polyB ) - 1 ) ) ) while len(self.polyA ) < self.c_max_length: self.polyA.append(0 ) while len(self.polyB ) < self.c_max_length: self.polyB.append(0 ) # A complex root used for the fourier transform _UpperCAmelCase = complex(mpmath.root(x=1 , n=self.c_max_length , k=1 ) ) # The product _UpperCAmelCase = self.__multiply() def lowerCAmelCase_ ( self : Optional[int] , __lowerCAmelCase : str ): _UpperCAmelCase = [[x] for x in self.polyA] if which == """A""" else [[x] for x in self.polyB] # Corner case if len(__lowerCAmelCase ) <= 1: return dft[0] # _UpperCAmelCase = self.c_max_length // 2 while next_ncol > 0: _UpperCAmelCase = [[] for i in range(__lowerCAmelCase )] _UpperCAmelCase = self.root**next_ncol # First half of next step _UpperCAmelCase = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(__lowerCAmelCase ): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j] ) current_root *= root # Second half of next step _UpperCAmelCase = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(__lowerCAmelCase ): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j] ) current_root *= root # Update _UpperCAmelCase = new_dft _UpperCAmelCase = next_ncol // 2 return dft[0] def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = self.__dft("""A""" ) _UpperCAmelCase = self.__dft("""B""" ) _UpperCAmelCase = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length )]] del dft_a del dft_b # Corner Case if len(inverce_c[0] ) <= 1: return inverce_c[0] # Inverse DFT _UpperCAmelCase = 2 while next_ncol <= self.c_max_length: _UpperCAmelCase = [[] for i in range(__lowerCAmelCase )] _UpperCAmelCase = self.root ** (next_ncol // 2) _UpperCAmelCase = 1 # First half of next step for j in range(self.c_max_length // next_ncol ): for i in range(next_ncol // 2 ): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2 ) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root) ) current_root *= root # Update _UpperCAmelCase = new_inverse_c next_ncol *= 2 # Unpack _UpperCAmelCase = [round(x[0].real , 8 ) + round(x[0].imag , 8 ) * 1j for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self : Dict ): _UpperCAmelCase = """A = """ + """ + """.join( f'''{coef}*x^{i}''' for coef, i in enumerate(self.polyA[: self.len_A] ) ) _UpperCAmelCase = """B = """ + """ + """.join( f'''{coef}*x^{i}''' for coef, i in enumerate(self.polyB[: self.len_B] ) ) _UpperCAmelCase = """A*B = """ + """ + """.join( f'''{coef}*x^{i}''' for coef, i in enumerate(self.product ) ) return f'''{a}\n{b}\n{c}''' # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
30
0
import importlib.metadata import warnings from copy import deepcopy from packaging import version from ..utils import logging from .import_utils import is_accelerate_available, is_bitsandbytes_available if is_bitsandbytes_available(): import bitsandbytes as bnb import torch import torch.nn as nn from ..pytorch_utils import ConvaD if is_accelerate_available(): from accelerate import init_empty_weights from accelerate.utils import find_tied_parameters UpperCAmelCase__ = logging.get_logger(__name__) def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ,lowercase=None ,lowercase=None ): """simple docstring""" # Recurse if needed if "." in tensor_name: _UpperCAmelCase = tensor_name.split(""".""" ) for split in splits[:-1]: _UpperCAmelCase = getattr(lowercase ,lowercase ) if new_module is None: raise ValueError(f'''{module} has no attribute {split}.''' ) _UpperCAmelCase = new_module _UpperCAmelCase = splits[-1] if tensor_name not in module._parameters and tensor_name not in module._buffers: raise ValueError(f'''{module} does not have a parameter or a buffer named {tensor_name}.''' ) _UpperCAmelCase = tensor_name in module._buffers _UpperCAmelCase = getattr(lowercase ,lowercase ) if old_value.device == torch.device("""meta""" ) and device not in ["meta", torch.device("""meta""" )] and value is None: raise ValueError(f'''{tensor_name} is on the meta device, we need a `value` to put in on {device}.''' ) _UpperCAmelCase = False _UpperCAmelCase = False if is_buffer or not is_bitsandbytes_available(): _UpperCAmelCase = False _UpperCAmelCase = False else: _UpperCAmelCase = hasattr(bnb.nn ,"""Params4bit""" ) and isinstance(module._parameters[tensor_name] ,bnb.nn.Paramsabit ) _UpperCAmelCase = isinstance(module._parameters[tensor_name] ,bnb.nn.IntaParams ) if is_abit or is_abit: _UpperCAmelCase = module._parameters[tensor_name] if param.device.type != "cuda": if value is None: _UpperCAmelCase = old_value.to(lowercase ) elif isinstance(lowercase ,torch.Tensor ): _UpperCAmelCase = value.to("""cpu""" ) if value.dtype == torch.inta: _UpperCAmelCase = version.parse(importlib.metadata.version("""bitsandbytes""" ) ) > version.parse( """0.37.2""" ) if not is_abit_serializable: raise ValueError( """Detected int8 weights but the version of bitsandbytes is not compatible with int8 serialization. """ """Make sure to download the latest `bitsandbytes` version. `pip install --upgrade bitsandbytes`.""" ) else: _UpperCAmelCase = torch.tensor(lowercase ,device="""cpu""" ) # Support models using `Conv1D` in place of `nn.Linear` (e.g. gpt2) by transposing the weight matrix prior to quantization. # Since weights are saved in the correct "orientation", we skip transposing when loading. if issubclass(module.source_cls ,lowercase ) and fpaa_statistics is None: _UpperCAmelCase = new_value.T _UpperCAmelCase = old_value.__dict__ if is_abit: _UpperCAmelCase = bnb.nn.IntaParams(lowercase ,requires_grad=lowercase ,**lowercase ).to(lowercase ) elif is_abit: _UpperCAmelCase = bnb.nn.Paramsabit(lowercase ,requires_grad=lowercase ,**lowercase ).to(lowercase ) _UpperCAmelCase = new_value if fpaa_statistics is not None: setattr(module.weight ,"""SCB""" ,fpaa_statistics.to(lowercase ) ) else: if value is None: _UpperCAmelCase = old_value.to(lowercase ) elif isinstance(lowercase ,torch.Tensor ): _UpperCAmelCase = value.to(lowercase ) else: _UpperCAmelCase = torch.tensor(lowercase ,device=lowercase ) if is_buffer: _UpperCAmelCase = new_value else: _UpperCAmelCase = nn.Parameter(lowercase ,requires_grad=old_value.requires_grad ) _UpperCAmelCase = new_value def __UpperCAmelCase ( lowercase ,lowercase=None ,lowercase=None ,lowercase=None ,lowercase=False ): """simple docstring""" for name, module in model.named_children(): if current_key_name is None: _UpperCAmelCase = [] current_key_name.append(lowercase ) if (isinstance(lowercase ,nn.Linear ) or isinstance(lowercase ,lowercase )) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` if not any(key in """.""".join(lowercase ) for key in modules_to_not_convert ): with init_empty_weights(): if isinstance(lowercase ,lowercase ): _UpperCAmelCase , _UpperCAmelCase = module.weight.shape else: _UpperCAmelCase = module.in_features _UpperCAmelCase = module.out_features if quantization_config.quantization_method() == "llm_int8": _UpperCAmelCase = bnb.nn.LinearabitLt( lowercase ,lowercase ,module.bias is not None ,has_fpaa_weights=quantization_config.llm_inta_has_fpaa_weight ,threshold=quantization_config.llm_inta_threshold ,) _UpperCAmelCase = True else: if ( quantization_config.llm_inta_skip_modules is not None and name in quantization_config.llm_inta_skip_modules ): pass else: _UpperCAmelCase = bnb.nn.Linearabit( lowercase ,lowercase ,module.bias is not None ,quantization_config.bnb_abit_compute_dtype ,compress_statistics=quantization_config.bnb_abit_use_double_quant ,quant_type=quantization_config.bnb_abit_quant_type ,) _UpperCAmelCase = True # Store the module class in case we need to transpose the weight later _UpperCAmelCase = type(lowercase ) # Force requires grad to False to avoid unexpected errors model._modules[name].requires_grad_(lowercase ) if len(list(module.children() ) ) > 0: _UpperCAmelCase , _UpperCAmelCase = _replace_with_bnb_linear( lowercase ,lowercase ,lowercase ,lowercase ,has_been_replaced=lowercase ,) # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def __UpperCAmelCase ( lowercase ,lowercase=None ,lowercase=None ,lowercase=None ): """simple docstring""" _UpperCAmelCase = ["""lm_head"""] if modules_to_not_convert is None else modules_to_not_convert _UpperCAmelCase , _UpperCAmelCase = _replace_with_bnb_linear( lowercase ,lowercase ,lowercase ,lowercase ) if not has_been_replaced: logger.warning( """You are loading your model in 8bit or 4bit but no linear modules were found in your model.""" """ Please double check your model architecture, or submit an issue on github if you think this is""" """ a bug.""" ) return model def __UpperCAmelCase ( *lowercase ,**lowercase ): """simple docstring""" warnings.warn( """`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead""" ,lowercase ,) return replace_with_bnb_linear(*lowercase ,**lowercase ) def __UpperCAmelCase ( *lowercase ,**lowercase ): """simple docstring""" warnings.warn( """`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead""" ,lowercase ,) return set_module_quantized_tensor_to_device(*lowercase ,**lowercase ) def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = deepcopy(lowercase ) # this has 0 cost since it is done inside `init_empty_weights` context manager` tied_model.tie_weights() _UpperCAmelCase = find_tied_parameters(lowercase ) # For compatibility with Accelerate < 0.18 if isinstance(lowercase ,lowercase ): _UpperCAmelCase = sum(list(tied_params.values() ) ,[] ) + list(tied_params.keys() ) else: _UpperCAmelCase = sum(lowercase ,[] ) _UpperCAmelCase = len(lowercase ) > 0 # Check if it is a base model _UpperCAmelCase = not hasattr(lowercase ,model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head _UpperCAmelCase = list(model.named_children() ) _UpperCAmelCase = [list_modules[-1][0]] # add last module together with tied weights _UpperCAmelCase = set(lowercase ) - set(lowercase ) _UpperCAmelCase = list(set(lowercase ) ) + list(lowercase ) # remove ".weight" from the keys _UpperCAmelCase = [""".weight""", """.bias"""] _UpperCAmelCase = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: _UpperCAmelCase = name.replace(lowercase ,"""""" ) filtered_module_names.append(lowercase ) return filtered_module_names
363
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto.configuration_auto import CONFIG_MAPPING UpperCAmelCase__ = logging.get_logger(__name__) class a ( lowerCAmelCase_ ): _snake_case : List[str] = 'upernet' def __init__( self : Tuple , __lowerCAmelCase : int=None , __lowerCAmelCase : Tuple=512 , __lowerCAmelCase : Union[str, Any]=0.02 , __lowerCAmelCase : Tuple=[1, 2, 3, 6] , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=0.4 , __lowerCAmelCase : Union[str, Any]=384 , __lowerCAmelCase : Optional[int]=256 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Optional[int]=False , __lowerCAmelCase : Optional[int]=255 , **__lowerCAmelCase : Union[str, Any] , ): super().__init__(**__lowerCAmelCase ) if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) _UpperCAmelCase = CONFIG_MAPPING["""resnet"""](out_features=["""stage1""", """stage2""", """stage3""", """stage4"""] ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): _UpperCAmelCase = backbone_config.get("""model_type""" ) _UpperCAmelCase = CONFIG_MAPPING[backbone_model_type] _UpperCAmelCase = config_class.from_dict(__lowerCAmelCase ) _UpperCAmelCase = backbone_config _UpperCAmelCase = hidden_size _UpperCAmelCase = initializer_range _UpperCAmelCase = pool_scales _UpperCAmelCase = use_auxiliary_head _UpperCAmelCase = auxiliary_loss_weight _UpperCAmelCase = auxiliary_in_channels _UpperCAmelCase = auxiliary_channels _UpperCAmelCase = auxiliary_num_convs _UpperCAmelCase = auxiliary_concat_input _UpperCAmelCase = loss_ignore_index def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = copy.deepcopy(self.__dict__ ) _UpperCAmelCase = self.backbone_config.to_dict() _UpperCAmelCase = self.__class__.model_type return output
30
0
"""simple docstring""" from typing import Dict import numpy as np import torch from . import residue_constants as rc from .tensor_utils import tensor_tree_map, tree_map def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [] _UpperCAmelCase = [] _UpperCAmelCase = [] for rt in rc.restypes: _UpperCAmelCase = rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]] restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names] ) _UpperCAmelCase = {name: i for i, name in enumerate(lowercase )} restype_atomaa_to_atomaa_list.append( [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types] ) restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names] ) # Add dummy mapping for restype 'UNK' restype_atomaa_to_atomaa_list.append([0] * 14 ) restype_atomaa_to_atomaa_list.append([0] * 37 ) restype_atomaa_mask_list.append([0.0] * 14 ) _UpperCAmelCase = torch.tensor( lowercase ,dtype=torch.intaa ,device=protein["""aatype"""].device ,) _UpperCAmelCase = torch.tensor( lowercase ,dtype=torch.intaa ,device=protein["""aatype"""].device ,) _UpperCAmelCase = torch.tensor( lowercase ,dtype=torch.floataa ,device=protein["""aatype"""].device ,) _UpperCAmelCase = protein["""aatype"""].to(torch.long ) # create the mapping for (residx, atom14) --> atom37, i.e. an array # with shape (num_res, 14) containing the atom37 indices for this protein _UpperCAmelCase = restype_atomaa_to_atomaa[protein_aatype] _UpperCAmelCase = restype_atomaa_mask[protein_aatype] _UpperCAmelCase = residx_atomaa_mask _UpperCAmelCase = residx_atomaa_to_atomaa.long() # create the gather indices for mapping back _UpperCAmelCase = restype_atomaa_to_atomaa[protein_aatype] _UpperCAmelCase = residx_atomaa_to_atomaa.long() # create the corresponding mask _UpperCAmelCase = torch.zeros([21, 37] ,dtype=torch.floataa ,device=protein["""aatype"""].device ) for restype, restype_letter in enumerate(rc.restypes ): _UpperCAmelCase = rc.restype_atoa[restype_letter] _UpperCAmelCase = rc.residue_atoms[restype_name] for atom_name in atom_names: _UpperCAmelCase = rc.atom_order[atom_name] _UpperCAmelCase = 1 _UpperCAmelCase = restype_atomaa_mask[protein_aatype] _UpperCAmelCase = residx_atomaa_mask return protein def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = tree_map(lambda lowercase : torch.tensor(lowercase ,device=batch["""aatype"""].device ) ,lowercase ,np.ndarray ) _UpperCAmelCase = tensor_tree_map(lambda lowercase : np.array(lowercase ) ,make_atomaa_masks(lowercase ) ) return out
364
"""simple docstring""" from itertools import product def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = sides_number _UpperCAmelCase = max_face_number * dice_number _UpperCAmelCase = [0] * (max_total + 1) _UpperCAmelCase = 1 _UpperCAmelCase = range(lowercase ,max_face_number + 1 ) for dice_numbers in product(lowercase ,repeat=lowercase ): _UpperCAmelCase = sum(lowercase ) totals_frequencies[total] += 1 return totals_frequencies def __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = total_frequency_distribution( sides_number=4 ,dice_number=9 ) _UpperCAmelCase = total_frequency_distribution( sides_number=6 ,dice_number=6 ) _UpperCAmelCase = 0 _UpperCAmelCase = 9 _UpperCAmelCase = 4 * 9 _UpperCAmelCase = 6 for peter_total in range(lowercase ,max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) _UpperCAmelCase = (4**9) * (6**6) _UpperCAmelCase = peter_wins_count / total_games_number _UpperCAmelCase = round(lowercase ,ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(F'''{solution() = }''')
30
0
def __UpperCAmelCase ( lowercase ): """simple docstring""" if len(lowercase ) <= 1: return [tuple(lowercase )] _UpperCAmelCase = [] def generate(lowercase ,lowercase ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 ,lowercase ) for i in range(k - 1 ): if k % 2 == 0: # k is even _UpperCAmelCase , _UpperCAmelCase = arr[k - 1], arr[i] else: # k is odd _UpperCAmelCase , _UpperCAmelCase = arr[k - 1], arr[0] generate(k - 1 ,lowercase ) generate(len(lowercase ) ,lowercase ) return res if __name__ == "__main__": UpperCAmelCase__ = input("""Enter numbers separated by a comma:\n""").strip() UpperCAmelCase__ = [int(item) for item in user_input.split(""",""")] print(heaps(arr))
365
"""simple docstring""" import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType UpperCAmelCase__ = logging.get_logger(__name__) class a ( lowerCAmelCase_ ): _snake_case : List[Any] = 'vision-encoder-decoder' _snake_case : Optional[int] = True def __init__( self : int , **__lowerCAmelCase : Any ): super().__init__(**__lowerCAmelCase ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( f'''A configuraton of type {self.model_type} cannot be instantiated because ''' f'''not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}''' ) _UpperCAmelCase = kwargs.pop("""encoder""" ) _UpperCAmelCase = encoder_config.pop("""model_type""" ) _UpperCAmelCase = kwargs.pop("""decoder""" ) _UpperCAmelCase = decoder_config.pop("""model_type""" ) _UpperCAmelCase = AutoConfig.for_model(__lowerCAmelCase , **__lowerCAmelCase ) _UpperCAmelCase = AutoConfig.for_model(__lowerCAmelCase , **__lowerCAmelCase ) _UpperCAmelCase = True @classmethod def lowerCAmelCase_ ( cls : int , __lowerCAmelCase : PretrainedConfig , __lowerCAmelCase : PretrainedConfig , **__lowerCAmelCase : str ): logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) _UpperCAmelCase = True _UpperCAmelCase = True return cls(encoder=encoder_config.to_dict() , decoder=decoder_config.to_dict() , **__lowerCAmelCase ) def lowerCAmelCase_ ( self : int ): _UpperCAmelCase = copy.deepcopy(self.__dict__ ) _UpperCAmelCase = self.encoder.to_dict() _UpperCAmelCase = self.decoder.to_dict() _UpperCAmelCase = self.__class__.model_type return output class a ( lowerCAmelCase_ ): _snake_case : Union[str, Any] = version.parse('1.11' ) @property def lowerCAmelCase_ ( self : int ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def lowerCAmelCase_ ( self : Tuple ): return 1e-4 @property def lowerCAmelCase_ ( self : Dict ): return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class a ( lowerCAmelCase_ ): @property def lowerCAmelCase_ ( self : Any ): _UpperCAmelCase = OrderedDict() _UpperCAmelCase = {0: """batch""", 1: """past_decoder_sequence + sequence"""} _UpperCAmelCase = {0: """batch""", 1: """past_decoder_sequence + sequence"""} _UpperCAmelCase = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def lowerCAmelCase_ ( self : Union[str, Any] , __lowerCAmelCase : "PreTrainedTokenizerBase" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , ): import torch _UpperCAmelCase = OrderedDict() _UpperCAmelCase = super().generate_dummy_inputs( __lowerCAmelCase , batch_size=__lowerCAmelCase , seq_length=__lowerCAmelCase , is_pair=__lowerCAmelCase , framework=__lowerCAmelCase ) _UpperCAmelCase , _UpperCAmelCase = dummy_input["""input_ids"""].shape _UpperCAmelCase = (batch, encoder_sequence, self._config.encoder_hidden_size) _UpperCAmelCase = dummy_input.pop("""input_ids""" ) _UpperCAmelCase = dummy_input.pop("""attention_mask""" ) _UpperCAmelCase = torch.zeros(__lowerCAmelCase ) return common_inputs class a ( lowerCAmelCase_ ): @property def lowerCAmelCase_ ( self : Tuple ): pass def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : PretrainedConfig ): return VisionEncoderDecoderEncoderOnnxConfig(__lowerCAmelCase ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : PretrainedConfig , __lowerCAmelCase : PretrainedConfig , __lowerCAmelCase : str = "default" ): _UpperCAmelCase = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(__lowerCAmelCase , __lowerCAmelCase )
30
0
"""simple docstring""" from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
366
"""simple docstring""" import argparse from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument( """--txt2img_unclip""", default="""kakaobrain/karlo-v1-alpha""", type=str, required=False, help="""The pretrained txt2img unclip.""", ) UpperCAmelCase__ = parser.parse_args() UpperCAmelCase__ = UnCLIPPipeline.from_pretrained(args.txtaimg_unclip) UpperCAmelCase__ = CLIPImageProcessor() UpperCAmelCase__ = CLIPVisionModelWithProjection.from_pretrained("""openai/clip-vit-large-patch14""") UpperCAmelCase__ = UnCLIPImageVariationPipeline( decoder=txtaimg.decoder, text_encoder=txtaimg.text_encoder, tokenizer=txtaimg.tokenizer, text_proj=txtaimg.text_proj, feature_extractor=feature_extractor, image_encoder=image_encoder, super_res_first=txtaimg.super_res_first, super_res_last=txtaimg.super_res_last, decoder_scheduler=txtaimg.decoder_scheduler, super_res_scheduler=txtaimg.super_res_scheduler, ) imgaimg.save_pretrained(args.dump_path)
30
0
"""simple docstring""" 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__ = { """andreasmadsen/efficient_mlm_m0.40""": ( """https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json""" ), } class a ( lowerCAmelCase_ ): _snake_case : str = 'roberta-prelayernorm' def __init__( self : str , __lowerCAmelCase : Optional[Any]=5_0265 , __lowerCAmelCase : List[str]=768 , __lowerCAmelCase : int=12 , __lowerCAmelCase : List[str]=12 , __lowerCAmelCase : Dict=3072 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Any=0.1 , __lowerCAmelCase : int=0.1 , __lowerCAmelCase : Optional[int]=512 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Union[str, Any]=0.02 , __lowerCAmelCase : int=1e-1_2 , __lowerCAmelCase : List[Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Union[str, Any]="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : int=None , **__lowerCAmelCase : Optional[int] , ): super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) _UpperCAmelCase = vocab_size _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = hidden_act _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = max_position_embeddings _UpperCAmelCase = type_vocab_size _UpperCAmelCase = initializer_range _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = position_embedding_type _UpperCAmelCase = use_cache _UpperCAmelCase = classifier_dropout class a ( lowerCAmelCase_ ): @property def lowerCAmelCase_ ( self : Dict ): if self.task == "multiple-choice": _UpperCAmelCase = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _UpperCAmelCase = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
367
"""simple docstring""" import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def __UpperCAmelCase ( *lowercase ): """simple docstring""" if not isinstance(lowercase ,lowercase ): _UpperCAmelCase = list(lowercase ) for i in range(len(lowercase ) ): _UpperCAmelCase = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [ """CUDA out of memory.""", # CUDA OOM """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""", # CUDNN SNAFU """DefaultCPUAllocator: can't allocate memory""", # CPU OOM ] if isinstance(lowercase ,lowercase ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def __UpperCAmelCase ( lowercase = None ,lowercase = 1_28 ): """simple docstring""" if function is None: return functools.partial(lowercase ,starting_batch_size=lowercase ) _UpperCAmelCase = starting_batch_size def decorator(*lowercase ,**lowercase ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() _UpperCAmelCase = list(inspect.signature(lowercase ).parameters.keys() ) # Guard against user error if len(lowercase ) < (len(lowercase ) + 1): _UpperCAmelCase = """, """.join([f'''{arg}={value}''' for arg, value in zip(params[1:] ,args[1:] )] ) raise TypeError( f'''Batch size was passed into `{function.__name__}` as the first argument when called.''' f'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError("""No executable batch size found, reached zero.""" ) try: return function(lowercase ,*lowercase ,**lowercase ) except Exception as e: if should_reduce_batch_size(lowercase ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
30
0
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class a ( lowerCAmelCase_ ): def __init__( self : Tuple , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Tuple ): _UpperCAmelCase = dataset _UpperCAmelCase = process _UpperCAmelCase = params def __len__( self : str ): return len(self.dataset ) def __getitem__( self : Union[str, Any] , __lowerCAmelCase : Optional[Any] ): _UpperCAmelCase = self.dataset[i] _UpperCAmelCase = self.process(__lowerCAmelCase , **self.params ) return processed class a ( lowerCAmelCase_ ): def __init__( self : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Tuple=None ): _UpperCAmelCase = loader _UpperCAmelCase = infer _UpperCAmelCase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether _UpperCAmelCase = None _UpperCAmelCase = loader_batch_size # Internal bookkeeping _UpperCAmelCase = None _UpperCAmelCase = None def __len__( self : Dict ): return len(self.loader ) def __iter__( self : List[Any] ): _UpperCAmelCase = iter(self.loader ) return self def lowerCAmelCase_ ( self : Union[str, Any] ): if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice _UpperCAmelCase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) _UpperCAmelCase = {} for k, element in self._loader_batch_data.items(): if isinstance(__lowerCAmelCase , __lowerCAmelCase ): # Convert ModelOutput to tuple first _UpperCAmelCase = element.to_tuple() if isinstance(element[0] , torch.Tensor ): _UpperCAmelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): _UpperCAmelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(__lowerCAmelCase , __lowerCAmelCase ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): _UpperCAmelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): _UpperCAmelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around _UpperCAmelCase = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers _UpperCAmelCase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers _UpperCAmelCase = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. _UpperCAmelCase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 _UpperCAmelCase = self._loader_batch_data.__class__(__lowerCAmelCase ) self._loader_batch_index += 1 return result def lowerCAmelCase_ ( self : str ): if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch _UpperCAmelCase = next(self.iterator ) _UpperCAmelCase = self.infer(__lowerCAmelCase , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(__lowerCAmelCase , torch.Tensor ): _UpperCAmelCase = processed else: _UpperCAmelCase = list(processed.keys() )[0] _UpperCAmelCase = processed[key] if isinstance(__lowerCAmelCase , __lowerCAmelCase ): _UpperCAmelCase = len(__lowerCAmelCase ) else: _UpperCAmelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. _UpperCAmelCase = observed_batch_size # Setting internal index to unwrap the batch _UpperCAmelCase = processed _UpperCAmelCase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class a ( lowerCAmelCase_ ): def __init__( self : Tuple , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : str , __lowerCAmelCase : List[Any]=None ): super().__init__(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) def __iter__( self : str ): _UpperCAmelCase = iter(self.loader ) _UpperCAmelCase = None return self def lowerCAmelCase_ ( self : int ): if self.subiterator is None: _UpperCAmelCase = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item _UpperCAmelCase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators _UpperCAmelCase = self.infer(next(self.iterator ) , **self.params ) _UpperCAmelCase = next(self.subiterator ) return processed class a ( lowerCAmelCase_ ): def __iter__( self : Union[str, Any] ): _UpperCAmelCase = iter(self.loader ) return self def lowerCAmelCase_ ( self : List[str] ): # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. _UpperCAmelCase = False _UpperCAmelCase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: _UpperCAmelCase = self.loader_batch_item() _UpperCAmelCase = item.pop("""is_last""" ) accumulator.append(__lowerCAmelCase ) if is_last: return accumulator while not is_last: _UpperCAmelCase = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(__lowerCAmelCase , torch.Tensor ): _UpperCAmelCase = processed else: _UpperCAmelCase = list(processed.keys() )[0] _UpperCAmelCase = processed[key] if isinstance(__lowerCAmelCase , __lowerCAmelCase ): _UpperCAmelCase = len(__lowerCAmelCase ) else: _UpperCAmelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. _UpperCAmelCase = observed_batch_size _UpperCAmelCase = processed _UpperCAmelCase = 0 while self._loader_batch_index < self.loader_batch_size: _UpperCAmelCase = self.loader_batch_item() _UpperCAmelCase = item.pop("""is_last""" ) accumulator.append(__lowerCAmelCase ) if is_last: return accumulator else: _UpperCAmelCase = processed _UpperCAmelCase = item.pop("""is_last""" ) accumulator.append(__lowerCAmelCase ) return accumulator class a ( lowerCAmelCase_ ): def __init__( self : Any , __lowerCAmelCase : Dataset , __lowerCAmelCase : str ): _UpperCAmelCase = dataset _UpperCAmelCase = key def __len__( self : List[str] ): return len(self.dataset ) def __getitem__( self : Optional[Any] , __lowerCAmelCase : str ): return self.dataset[i][self.key] class a ( lowerCAmelCase_ ): def __init__( self : List[Any] , __lowerCAmelCase : Dataset , __lowerCAmelCase : str , __lowerCAmelCase : str ): _UpperCAmelCase = dataset _UpperCAmelCase = keya _UpperCAmelCase = keya def __len__( self : Optional[Any] ): return len(self.dataset ) def __getitem__( self : List[Any] , __lowerCAmelCase : str ): return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
368
"""simple docstring""" from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class a ( lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase ): _snake_case : str = ( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) _snake_case : Dict = ( { 'feature-extraction': TFMobileBertModel, 'fill-mask': TFMobileBertForMaskedLM, 'question-answering': TFMobileBertForQuestionAnswering, 'text-classification': TFMobileBertForSequenceClassification, 'token-classification': TFMobileBertForTokenClassification, 'zero-shot': TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) _snake_case : Dict = False _snake_case : List[str] = False def lowerCAmelCase_ ( self : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : List[Any] , __lowerCAmelCase : int=False ): _UpperCAmelCase = super()._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase , return_labels=__lowerCAmelCase ) if return_labels: if model_class in get_values(__lowerCAmelCase ): _UpperCAmelCase = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) return inputs_dict class a ( lowerCAmelCase_ ): def __init__( self : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str=13 , __lowerCAmelCase : Any=7 , __lowerCAmelCase : Optional[int]=True , __lowerCAmelCase : str=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : List[str]=99 , __lowerCAmelCase : Optional[int]=32 , __lowerCAmelCase : str=32 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : int=4 , __lowerCAmelCase : Tuple=37 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : int=0.1 , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : int=512 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : Optional[int]=0.02 , __lowerCAmelCase : Optional[int]=3 , __lowerCAmelCase : Dict=4 , __lowerCAmelCase : str=None , ): _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 = embedding_size def lowerCAmelCase_ ( self : int ): _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _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 = MobileBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , embedding_size=self.embedding_size , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Tuple , __lowerCAmelCase : Any ): _UpperCAmelCase = TFMobileBertModel(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) _UpperCAmelCase = [input_ids, input_mask] _UpperCAmelCase = model(__lowerCAmelCase ) _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def lowerCAmelCase_ ( self : Any , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : List[str] , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str] ): _UpperCAmelCase = TFMobileBertForMaskedLM(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : int , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : Dict ): _UpperCAmelCase = TFMobileBertForNextSentencePrediction(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def lowerCAmelCase_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple ): _UpperCAmelCase = TFMobileBertForPreTraining(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual( result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : Any , __lowerCAmelCase : str , __lowerCAmelCase : Any , __lowerCAmelCase : int , __lowerCAmelCase : Tuple ): _UpperCAmelCase = self.num_labels _UpperCAmelCase = TFMobileBertForSequenceClassification(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : Any , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Union[str, Any] ): _UpperCAmelCase = self.num_choices _UpperCAmelCase = TFMobileBertForMultipleChoice(config=__lowerCAmelCase ) _UpperCAmelCase = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Any ): _UpperCAmelCase = self.num_labels _UpperCAmelCase = TFMobileBertForTokenClassification(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase_ ( self : Dict , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int , __lowerCAmelCase : Any , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] ): _UpperCAmelCase = TFMobileBertForQuestionAnswering(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) = config_and_inputs _UpperCAmelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = TFMobileBertModelTest.TFMobileBertModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=__lowerCAmelCase , hidden_size=37 ) def lowerCAmelCase_ ( self : Any ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : Any ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*__lowerCAmelCase ) @slow def lowerCAmelCase_ ( self : int ): # for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["google/mobilebert-uncased"]: _UpperCAmelCase = TFMobileBertModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) @require_tf class a ( unittest.TestCase ): @slow def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased""" ) _UpperCAmelCase = tf.constant([[0, 1, 2, 3, 4, 5]] ) _UpperCAmelCase = model(__lowerCAmelCase )[0] _UpperCAmelCase = [1, 6, 3_0522] self.assertEqual(output.shape , __lowerCAmelCase ) _UpperCAmelCase = tf.constant( [ [ [-4.5_919_547, -9.248_295, -9.645_256], [-6.7_306_175, -6.440_284, -6.6_052_837], [-7.2_743_506, -6.7_847_915, -6.024_673], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __lowerCAmelCase , atol=1e-4 )
30
0
"""simple docstring""" import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available from . import BaseDiffusersCLICommand def __UpperCAmelCase ( lowercase ): """simple docstring""" return EnvironmentCommand() class a ( lowerCAmelCase_ ): @staticmethod def lowerCAmelCase_ ( __lowerCAmelCase : ArgumentParser ): _UpperCAmelCase = parser.add_parser("""env""" ) download_parser.set_defaults(func=__lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): _UpperCAmelCase = huggingface_hub.__version__ _UpperCAmelCase = """not installed""" _UpperCAmelCase = """NA""" if is_torch_available(): import torch _UpperCAmelCase = torch.__version__ _UpperCAmelCase = torch.cuda.is_available() _UpperCAmelCase = """not installed""" if is_transformers_available(): import transformers _UpperCAmelCase = transformers.__version__ _UpperCAmelCase = """not installed""" if is_accelerate_available(): import accelerate _UpperCAmelCase = accelerate.__version__ _UpperCAmelCase = """not installed""" if is_xformers_available(): import xformers _UpperCAmelCase = xformers.__version__ _UpperCAmelCase = { """`diffusers` version""": version, """Platform""": platform.platform(), """Python version""": platform.python_version(), """PyTorch version (GPU?)""": f'''{pt_version} ({pt_cuda_available})''', """Huggingface_hub version""": hub_version, """Transformers version""": transformers_version, """Accelerate version""": accelerate_version, """xFormers version""": xformers_version, """Using GPU in script?""": """<fill in>""", """Using distributed or parallel set-up in script?""": """<fill in>""", } print("""\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n""" ) print(self.format_dict(__lowerCAmelCase ) ) return info @staticmethod def lowerCAmelCase_ ( __lowerCAmelCase : Optional[Any] ): return "\n".join([f'''- {prop}: {val}''' for prop, val in d.items()] ) + "\n"
369
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { """Visual-Attention-Network/van-base""": ( """https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json""" ), } class a ( lowerCAmelCase_ ): _snake_case : int = 'van' def __init__( self : Any , __lowerCAmelCase : Tuple=224 , __lowerCAmelCase : List[Any]=3 , __lowerCAmelCase : Tuple=[7, 3, 3, 3] , __lowerCAmelCase : Dict=[4, 2, 2, 2] , __lowerCAmelCase : Optional[Any]=[64, 128, 320, 512] , __lowerCAmelCase : Optional[int]=[3, 3, 12, 3] , __lowerCAmelCase : Dict=[8, 8, 4, 4] , __lowerCAmelCase : int="gelu" , __lowerCAmelCase : Optional[int]=0.02 , __lowerCAmelCase : List[str]=1e-6 , __lowerCAmelCase : Optional[int]=1e-2 , __lowerCAmelCase : Any=0.0 , __lowerCAmelCase : List[str]=0.0 , **__lowerCAmelCase : Any , ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = image_size _UpperCAmelCase = num_channels _UpperCAmelCase = patch_sizes _UpperCAmelCase = strides _UpperCAmelCase = hidden_sizes _UpperCAmelCase = depths _UpperCAmelCase = mlp_ratios _UpperCAmelCase = hidden_act _UpperCAmelCase = initializer_range _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = layer_scale_init_value _UpperCAmelCase = drop_path_rate _UpperCAmelCase = dropout_rate
30
0
"""simple docstring""" def __UpperCAmelCase ( lowercase = 10_00 ): """simple docstring""" _UpperCAmelCase = 2**power _UpperCAmelCase = 0 while n: _UpperCAmelCase , _UpperCAmelCase = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
370
"""simple docstring""" def __UpperCAmelCase ( lowercase = 10_00 ): """simple docstring""" _UpperCAmelCase = 2**power _UpperCAmelCase = 0 while n: _UpperCAmelCase , _UpperCAmelCase = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
30
0
"""simple docstring""" import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} UpperCAmelCase__ = { """vocab_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), }, """merges_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), }, } UpperCAmelCase__ = { """allenai/longformer-base-4096""": 4_0_9_6, """allenai/longformer-large-4096""": 4_0_9_6, """allenai/longformer-large-4096-finetuned-triviaqa""": 4_0_9_6, """allenai/longformer-base-4096-extra.pos.embd.only""": 4_0_9_6, """allenai/longformer-large-4096-extra.pos.embd.only""": 4_0_9_6, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = ( list(range(ord("""!""" ) ,ord("""~""" ) + 1 ) ) + list(range(ord("""¡""" ) ,ord("""¬""" ) + 1 ) ) + list(range(ord("""®""" ) ,ord("""ÿ""" ) + 1 ) ) ) _UpperCAmelCase = bs[:] _UpperCAmelCase = 0 for b in range(2**8 ): if b not in bs: bs.append(lowercase ) cs.append(2**8 + n ) n += 1 _UpperCAmelCase = [chr(lowercase ) for n in cs] return dict(zip(lowercase ,lowercase ) ) def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = set() _UpperCAmelCase = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _UpperCAmelCase = char return pairs class a ( lowerCAmelCase_ ): _snake_case : Optional[Any] = VOCAB_FILES_NAMES _snake_case : Optional[int] = PRETRAINED_VOCAB_FILES_MAP _snake_case : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _snake_case : List[Any] = ['input_ids', 'attention_mask'] def __init__( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : List[str] , __lowerCAmelCase : List[Any]="replace" , __lowerCAmelCase : Union[str, Any]="<s>" , __lowerCAmelCase : int="</s>" , __lowerCAmelCase : str="</s>" , __lowerCAmelCase : Any="<s>" , __lowerCAmelCase : str="<unk>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : List[Any]="<mask>" , __lowerCAmelCase : Dict=False , **__lowerCAmelCase : Union[str, Any] , ): _UpperCAmelCase = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else bos_token _UpperCAmelCase = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else eos_token _UpperCAmelCase = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else sep_token _UpperCAmelCase = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else cls_token _UpperCAmelCase = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else unk_token _UpperCAmelCase = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else pad_token # Mask token behave like a normal word, i.e. include the space before it _UpperCAmelCase = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token super().__init__( errors=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , add_prefix_space=__lowerCAmelCase , **__lowerCAmelCase , ) with open(__lowerCAmelCase , encoding="""utf-8""" ) as vocab_handle: _UpperCAmelCase = json.load(__lowerCAmelCase ) _UpperCAmelCase = {v: k for k, v in self.encoder.items()} _UpperCAmelCase = errors # how to handle errors in decoding _UpperCAmelCase = bytes_to_unicode() _UpperCAmelCase = {v: k for k, v in self.byte_encoder.items()} with open(__lowerCAmelCase , encoding="""utf-8""" ) as merges_handle: _UpperCAmelCase = merges_handle.read().split("""\n""" )[1:-1] _UpperCAmelCase = [tuple(merge.split() ) for merge in bpe_merges] _UpperCAmelCase = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) _UpperCAmelCase = {} _UpperCAmelCase = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions _UpperCAmelCase = re.compile(R"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""" ) @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.encoder ) def lowerCAmelCase_ ( self : Optional[int] ): return dict(self.encoder , **self.added_tokens_encoder ) def lowerCAmelCase_ ( self : str , __lowerCAmelCase : Tuple ): if token in self.cache: return self.cache[token] _UpperCAmelCase = tuple(__lowerCAmelCase ) _UpperCAmelCase = get_pairs(__lowerCAmelCase ) if not pairs: return token while True: _UpperCAmelCase = min(__lowerCAmelCase , key=lambda __lowerCAmelCase : self.bpe_ranks.get(__lowerCAmelCase , float("""inf""" ) ) ) if bigram not in self.bpe_ranks: break _UpperCAmelCase , _UpperCAmelCase = bigram _UpperCAmelCase = [] _UpperCAmelCase = 0 while i < len(__lowerCAmelCase ): try: _UpperCAmelCase = word.index(__lowerCAmelCase , __lowerCAmelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _UpperCAmelCase = j if word[i] == first and i < len(__lowerCAmelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _UpperCAmelCase = tuple(__lowerCAmelCase ) _UpperCAmelCase = new_word if len(__lowerCAmelCase ) == 1: break else: _UpperCAmelCase = get_pairs(__lowerCAmelCase ) _UpperCAmelCase = """ """.join(__lowerCAmelCase ) _UpperCAmelCase = word return word def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : Dict ): _UpperCAmelCase = [] for token in re.findall(self.pat , __lowerCAmelCase ): _UpperCAmelCase = """""".join( self.byte_encoder[b] for b in token.encode("""utf-8""" ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(__lowerCAmelCase ).split(""" """ ) ) return bpe_tokens def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : Union[str, Any] ): return self.encoder.get(__lowerCAmelCase , self.encoder.get(self.unk_token ) ) def lowerCAmelCase_ ( self : str , __lowerCAmelCase : Optional[Any] ): return self.decoder.get(__lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : List[Any] ): _UpperCAmelCase = """""".join(__lowerCAmelCase ) _UpperCAmelCase = bytearray([self.byte_decoder[c] for c in text] ).decode("""utf-8""" , errors=self.errors ) return text def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ): if not os.path.isdir(__lowerCAmelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return _UpperCAmelCase = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) _UpperCAmelCase = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""] ) with open(__lowerCAmelCase , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=__lowerCAmelCase , ensure_ascii=__lowerCAmelCase ) + """\n""" ) _UpperCAmelCase = 0 with open(__lowerCAmelCase , """w""" , encoding="""utf-8""" ) as writer: writer.write("""#version: 0.2\n""" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda __lowerCAmelCase : kv[1] ): if index != token_index: logger.warning( f'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' """ Please check that the tokenizer is not corrupted!""" ) _UpperCAmelCase = token_index writer.write(""" """.join(__lowerCAmelCase ) + """\n""" ) index += 1 return vocab_file, merge_file def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] _UpperCAmelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def lowerCAmelCase_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) if token_ids_a is None: return [1] + ([0] * len(__lowerCAmelCase )) + [1] return [1] + ([0] * len(__lowerCAmelCase )) + [1, 1] + ([0] * len(__lowerCAmelCase )) + [1] def lowerCAmelCase_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ): _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 + sep + token_ids_a + sep ) * [0] def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : Tuple , __lowerCAmelCase : str=False , **__lowerCAmelCase : Dict ): _UpperCAmelCase = kwargs.pop("""add_prefix_space""" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(__lowerCAmelCase ) > 0 and not text[0].isspace()): _UpperCAmelCase = """ """ + text return (text, kwargs)
371
"""simple docstring""" from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { """linear""": get_linear_schedule_with_warmup, """cosine""": get_cosine_schedule_with_warmup, """cosine_w_restarts""": get_cosine_with_hard_restarts_schedule_with_warmup, """polynomial""": get_polynomial_decay_schedule_with_warmup, """constant""": get_constant_schedule, """constant_w_warmup""": get_constant_schedule_with_warmup, } class a ( lowerCAmelCase_ ): def __init__( self : Optional[int] , __lowerCAmelCase : Any=None , __lowerCAmelCase : Any=None , *__lowerCAmelCase : Union[str, Any] , **__lowerCAmelCase : Optional[int] ): super().__init__(*__lowerCAmelCase , **__lowerCAmelCase ) if config is None: assert isinstance(self.model , __lowerCAmelCase ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f''' {self.model.__class__}''' ) _UpperCAmelCase = self.model.config else: _UpperCAmelCase = config _UpperCAmelCase = data_args _UpperCAmelCase = self.config.tgt_vocab_size if isinstance(self.config , __lowerCAmelCase ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' """ padding..""" ) if self.args.label_smoothing == 0: _UpperCAmelCase = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _UpperCAmelCase = label_smoothed_nll_loss def lowerCAmelCase_ ( self : Dict , __lowerCAmelCase : int ): if self.optimizer is None: _UpperCAmelCase = ["""bias""", """LayerNorm.weight"""] _UpperCAmelCase = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _UpperCAmelCase = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _UpperCAmelCase = Adafactor _UpperCAmelCase = {"""scale_parameter""": False, """relative_step""": False} else: _UpperCAmelCase = AdamW _UpperCAmelCase = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _UpperCAmelCase = self.args.learning_rate if self.sharded_ddp: _UpperCAmelCase = OSS( params=__lowerCAmelCase , optim=__lowerCAmelCase , **__lowerCAmelCase , ) else: _UpperCAmelCase = optimizer_cls(__lowerCAmelCase , **__lowerCAmelCase ) if self.lr_scheduler is None: _UpperCAmelCase = self._get_lr_scheduler(__lowerCAmelCase ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : List[str] ): _UpperCAmelCase = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _UpperCAmelCase = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _UpperCAmelCase = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _UpperCAmelCase = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=__lowerCAmelCase ) return scheduler def lowerCAmelCase_ ( self : Optional[int] ): if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def lowerCAmelCase_ ( self : Optional[int] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Tuple ): if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _UpperCAmelCase = model(**__lowerCAmelCase , use_cache=__lowerCAmelCase )[0] _UpperCAmelCase = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _UpperCAmelCase , _UpperCAmelCase = model(**__lowerCAmelCase , labels=__lowerCAmelCase , use_cache=__lowerCAmelCase )[:2] else: # compute label smoothed loss _UpperCAmelCase = model(**__lowerCAmelCase , use_cache=__lowerCAmelCase )[0] _UpperCAmelCase = torch.nn.functional.log_softmax(__lowerCAmelCase , dim=-1 ) _UpperCAmelCase , _UpperCAmelCase = self.loss_fn(__lowerCAmelCase , __lowerCAmelCase , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : int ): _UpperCAmelCase = inputs.pop("""labels""" ) _UpperCAmelCase , _UpperCAmelCase = self._compute_loss(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) return loss def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : nn.Module , __lowerCAmelCase : Dict[str, Union[torch.Tensor, Any]] , __lowerCAmelCase : bool , __lowerCAmelCase : Optional[List[str]] = None , ): _UpperCAmelCase = self._prepare_inputs(__lowerCAmelCase ) _UpperCAmelCase = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _UpperCAmelCase = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **__lowerCAmelCase , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase = self._pad_tensors_to_max_len(__lowerCAmelCase , gen_kwargs["""max_length"""] ) _UpperCAmelCase = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _UpperCAmelCase , _UpperCAmelCase = self._compute_loss(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _UpperCAmelCase = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase = self._pad_tensors_to_max_len(__lowerCAmelCase , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def lowerCAmelCase_ ( self : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[str] ): # If PAD token is not defined at least EOS token has to be defined _UpperCAmelCase = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" f''' padded to `max_length`={max_length}''' ) _UpperCAmelCase = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _UpperCAmelCase = tensor return padded_tensor
30
0
"""simple docstring""" from __future__ import annotations from dataclasses import dataclass @dataclass class a : _snake_case : float _snake_case : TreeNode | None = None _snake_case : TreeNode | None = None def __UpperCAmelCase ( lowercase ): """simple docstring""" # Validation def is_valid_tree(lowercase ) -> bool: if node is None: return True if not isinstance(lowercase ,lowercase ): return False try: float(node.data ) except (TypeError, ValueError): return False return is_valid_tree(node.left ) and is_valid_tree(node.right ) if not is_valid_tree(lowercase ): raise ValueError( """Each node should be type of TreeNode and data should be float.""" ) def is_binary_search_tree_recursive_check( lowercase ,lowercase ,lowercase ) -> bool: if node is None: return True return ( left_bound < node.data < right_bound and is_binary_search_tree_recursive_check(node.left ,lowercase ,node.data ) and is_binary_search_tree_recursive_check( node.right ,node.data ,lowercase ) ) return is_binary_search_tree_recursive_check(lowercase ,-float("""inf""" ) ,float("""inf""" ) ) if __name__ == "__main__": import doctest doctest.testmod()
350
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { """configuration_git""": ["""GIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GitConfig""", """GitVisionConfig"""], """processing_git""": ["""GitProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ """GIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """GitForCausalLM""", """GitModel""", """GitPreTrainedModel""", """GitVisionModel""", ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
30
0
"""simple docstring""" def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [0] * len(lowercase ) _UpperCAmelCase = [] _UpperCAmelCase = [1] * len(lowercase ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(lowercase ) ): if indegree[i] == 0: queue.append(lowercase ) while queue: _UpperCAmelCase = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: _UpperCAmelCase = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(lowercase ) print(max(lowercase ) ) # Adjacency list of Graph UpperCAmelCase__ = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
351
"""simple docstring""" import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def __UpperCAmelCase ( lowercase ,lowercase=False ): """simple docstring""" _UpperCAmelCase = [] # fmt: off # stem: rename_keys.append(("""cls_token""", """vit.embeddings.cls_token""") ) rename_keys.append(("""pos_embed""", """vit.embeddings.position_embeddings""") ) rename_keys.append(("""patch_embed.proj.weight""", """vit.embeddings.patch_embeddings.projection.weight""") ) rename_keys.append(("""patch_embed.proj.bias""", """vit.embeddings.patch_embeddings.projection.bias""") ) # backbone rename_keys.append(("""patch_embed.backbone.stem.conv.weight""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight""") ) rename_keys.append(("""patch_embed.backbone.stem.norm.weight""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight""") ) rename_keys.append(("""patch_embed.backbone.stem.norm.bias""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias""") ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight''') ) rename_keys.append((f'''patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias''', f'''vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias''') ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f'''blocks.{i}.norm1.weight''', f'''vit.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((f'''blocks.{i}.norm1.bias''', f'''vit.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append((f'''blocks.{i}.attn.proj.weight''', f'''vit.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append((f'''blocks.{i}.attn.proj.bias''', f'''vit.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append((f'''blocks.{i}.norm2.weight''', f'''vit.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((f'''blocks.{i}.norm2.bias''', f'''vit.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append((f'''blocks.{i}.mlp.fc1.weight''', f'''vit.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append((f'''blocks.{i}.mlp.fc1.bias''', f'''vit.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append((f'''blocks.{i}.mlp.fc2.weight''', f'''vit.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((f'''blocks.{i}.mlp.fc2.bias''', f'''vit.encoder.layer.{i}.output.dense.bias''') ) if base_model: # layernorm + pooler rename_keys.extend( [ ("""norm.weight""", """layernorm.weight"""), ("""norm.bias""", """layernorm.bias"""), ("""pre_logits.fc.weight""", """pooler.dense.weight"""), ("""pre_logits.fc.bias""", """pooler.dense.bias"""), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" _UpperCAmelCase = [(pair[0], pair[1][4:]) if pair[1].startswith("""vit""" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("""norm.weight""", """vit.layernorm.weight"""), ("""norm.bias""", """vit.layernorm.bias"""), ("""head.weight""", """classifier.weight"""), ("""head.bias""", """classifier.bias"""), ] ) # fmt: on return rename_keys def __UpperCAmelCase ( lowercase ,lowercase ,lowercase=False ): """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: _UpperCAmelCase = """""" else: _UpperCAmelCase = """vit.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) _UpperCAmelCase = state_dict.pop(f'''blocks.{i}.attn.qkv.weight''' ) _UpperCAmelCase = state_dict.pop(f'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase = in_proj_weight[ : config.hidden_size, : ] _UpperCAmelCase = in_proj_bias[: config.hidden_size] _UpperCAmelCase = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] _UpperCAmelCase = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] _UpperCAmelCase = in_proj_weight[ -config.hidden_size :, : ] _UpperCAmelCase = in_proj_bias[-config.hidden_size :] def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = ["""head.weight""", """head.bias"""] for k in ignore_keys: state_dict.pop(lowercase ,lowercase ) def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = dct.pop(lowercase ) _UpperCAmelCase = val def __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = """http://images.cocodataset.org/val2017/000000039769.jpg""" _UpperCAmelCase = Image.open(requests.get(lowercase ,stream=lowercase ).raw ) return im @torch.no_grad() def __UpperCAmelCase ( lowercase ,lowercase ,lowercase=False ): """simple docstring""" _UpperCAmelCase = BitConfig( global_padding="""same""" ,layer_type="""bottleneck""" ,depths=(3, 4, 9) ,out_features=["""stage3"""] ,embedding_dynamic_padding=lowercase ,) _UpperCAmelCase = ViTHybridConfig(backbone_config=lowercase ,image_size=3_84 ,num_labels=10_00 ) _UpperCAmelCase = False # load original model from timm _UpperCAmelCase = timm.create_model(lowercase ,pretrained=lowercase ) timm_model.eval() # load state_dict of original model, remove and rename some keys _UpperCAmelCase = timm_model.state_dict() if base_model: remove_classification_head_(lowercase ) _UpperCAmelCase = create_rename_keys(lowercase ,lowercase ) for src, dest in rename_keys: rename_key(lowercase ,lowercase ,lowercase ) read_in_q_k_v(lowercase ,lowercase ,lowercase ) _UpperCAmelCase = """huggingface/label-files""" _UpperCAmelCase = """imagenet-1k-id2label.json""" _UpperCAmelCase = json.load(open(hf_hub_download(lowercase ,lowercase ,repo_type="""dataset""" ) ,"""r""" ) ) _UpperCAmelCase = {int(lowercase ): v for k, v in idalabel.items()} _UpperCAmelCase = idalabel _UpperCAmelCase = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": _UpperCAmelCase = ViTHybridModel(lowercase ).eval() else: _UpperCAmelCase = ViTHybridForImageClassification(lowercase ).eval() model.load_state_dict(lowercase ) # create image processor _UpperCAmelCase = create_transform(**resolve_data_config({} ,model=lowercase ) ) _UpperCAmelCase = transform.transforms _UpperCAmelCase = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } _UpperCAmelCase = ViTHybridImageProcessor( do_resize=lowercase ,size={"""shortest_edge""": timm_transforms[0].size} ,resample=pillow_resamplings[timm_transforms[0].interpolation.value] ,do_center_crop=lowercase ,crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} ,do_normalize=lowercase ,image_mean=timm_transforms[-1].mean.tolist() ,image_std=timm_transforms[-1].std.tolist() ,) _UpperCAmelCase = prepare_img() _UpperCAmelCase = transform(lowercase ).unsqueeze(0 ) _UpperCAmelCase = processor(lowercase ,return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(lowercase ,lowercase ) # verify logits with torch.no_grad(): _UpperCAmelCase = model(lowercase ) _UpperCAmelCase = outputs.logits print("""Predicted class:""" ,logits.argmax(-1 ).item() ) if base_model: _UpperCAmelCase = timm_model.forward_features(lowercase ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(lowercase ,outputs.pooler_output ,atol=1E-3 ) else: _UpperCAmelCase = timm_model(lowercase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(lowercase ,outputs.logits ,atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(lowercase ).mkdir(exist_ok=lowercase ) print(f'''Saving model {vit_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(lowercase ) print(f'''Saving processor to {pytorch_dump_folder_path}''' ) processor.save_pretrained(lowercase ) if push_to_hub: print(f'''Pushing model and processor to the hub {vit_name}''' ) model.push_to_hub(f'''ybelkada/{vit_name}''' ) processor.push_to_hub(f'''ybelkada/{vit_name}''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--vit_name""", default="""vit_base_r50_s16_384""", type=str, help="""Name of the hybrid ViT timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) UpperCAmelCase__ = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
30
0
"""simple docstring""" import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class a ( unittest.TestCase ): def lowerCAmelCase_ ( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : int ): _UpperCAmelCase = jnp.ones((batch_size, length) ) / length return scores def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = None _UpperCAmelCase = 20 _UpperCAmelCase = self._get_uniform_logits(batch_size=2 , length=__lowerCAmelCase ) # tweak scores to not be uniform anymore _UpperCAmelCase = scores.at[1, 5].set((1 / length) + 0.1 ) # peak, 1st batch _UpperCAmelCase = scores.at[1, 10].set((1 / length) - 0.4 ) # valley, 1st batch # compute softmax _UpperCAmelCase = jax.nn.softmax(__lowerCAmelCase , axis=-1 ) _UpperCAmelCase = FlaxTemperatureLogitsWarper(temperature=0.5 ) _UpperCAmelCase = FlaxTemperatureLogitsWarper(temperature=1.3 ) _UpperCAmelCase = jax.nn.softmax(temp_dist_warper_sharper(__lowerCAmelCase , scores.copy() , cur_len=__lowerCAmelCase ) , axis=-1 ) _UpperCAmelCase = jax.nn.softmax(temp_dist_warper_smoother(__lowerCAmelCase , scores.copy() , cur_len=__lowerCAmelCase ) , axis=-1 ) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1e-3 ) ) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1e-3 ) ) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max() ) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min() ) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max() ) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min() ) def lowerCAmelCase_ ( self : Optional[Any] ): _UpperCAmelCase = None _UpperCAmelCase = 10 _UpperCAmelCase = 2 # create ramp distribution _UpperCAmelCase = np.broadcast_to(np.arange(__lowerCAmelCase )[None, :] , (batch_size, vocab_size) ).copy() _UpperCAmelCase = ramp_logits[1:, : vocab_size // 2] + vocab_size _UpperCAmelCase = FlaxTopKLogitsWarper(3 ) _UpperCAmelCase = top_k_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0] ).tolist() , 7 * [True] + 3 * [False] ) self.assertListEqual(jnp.isinf(scores[1] ).tolist() , 2 * [True] + 3 * [False] + 5 * [True] ) # check special case _UpperCAmelCase = 5 _UpperCAmelCase = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3 ) _UpperCAmelCase = np.broadcast_to(np.arange(__lowerCAmelCase )[None, :] , (batch_size, length) ).copy() _UpperCAmelCase = top_k_warp_safety_check(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1 ).tolist() , [2, 2] ) def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = None _UpperCAmelCase = 10 _UpperCAmelCase = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) _UpperCAmelCase = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]] ) ) _UpperCAmelCase = FlaxTopPLogitsWarper(0.8 ) _UpperCAmelCase = np.exp(top_p_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) ) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 _UpperCAmelCase = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]] ) self.assertTrue(np.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) ) # check edge cases with negative and extreme logits _UpperCAmelCase = np.broadcast_to(np.arange(__lowerCAmelCase )[None, :] , (batch_size, vocab_size) ).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme _UpperCAmelCase = ramp_logits[1] * 100.0 # make sure at least 2 tokens are kept _UpperCAmelCase = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0 ) _UpperCAmelCase = top_p_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1 ).tolist() , [3, 2] ) def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = 20 _UpperCAmelCase = 4 _UpperCAmelCase = 0 _UpperCAmelCase = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=__lowerCAmelCase ) # check that min length is applied at length 5 _UpperCAmelCase = ids_tensor((batch_size, 20) , vocab_size=20 ) _UpperCAmelCase = 5 _UpperCAmelCase = self._get_uniform_logits(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = min_dist_processor(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("""inf""" )] ) # check that min length is not applied anymore at length 15 _UpperCAmelCase = self._get_uniform_logits(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = 15 _UpperCAmelCase = min_dist_processor(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) self.assertFalse(jnp.isinf(__lowerCAmelCase ).any() ) def lowerCAmelCase_ ( self : Optional[Any] ): _UpperCAmelCase = 20 _UpperCAmelCase = 4 _UpperCAmelCase = 0 _UpperCAmelCase = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__lowerCAmelCase ) # check that all scores are -inf except the bos_token_id score _UpperCAmelCase = ids_tensor((batch_size, 1) , vocab_size=20 ) _UpperCAmelCase = 1 _UpperCAmelCase = self._get_uniform_logits(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = logits_processor(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0] ) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 _UpperCAmelCase = 3 _UpperCAmelCase = self._get_uniform_logits(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = logits_processor(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) self.assertFalse(jnp.isinf(__lowerCAmelCase ).any() ) def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = 20 _UpperCAmelCase = 4 _UpperCAmelCase = 0 _UpperCAmelCase = 5 _UpperCAmelCase = FlaxForcedEOSTokenLogitsProcessor(max_length=__lowerCAmelCase , eos_token_id=__lowerCAmelCase ) # check that all scores are -inf except the eos_token_id when max_length is reached _UpperCAmelCase = ids_tensor((batch_size, 4) , vocab_size=20 ) _UpperCAmelCase = 4 _UpperCAmelCase = self._get_uniform_logits(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = logits_processor(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0] ) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached _UpperCAmelCase = 3 _UpperCAmelCase = self._get_uniform_logits(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = logits_processor(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) self.assertFalse(jnp.isinf(__lowerCAmelCase ).any() ) def lowerCAmelCase_ ( self : Dict ): _UpperCAmelCase = 4 _UpperCAmelCase = 10 _UpperCAmelCase = 15 _UpperCAmelCase = 2 _UpperCAmelCase = 1 _UpperCAmelCase = 15 # dummy input_ids and scores _UpperCAmelCase = ids_tensor((batch_size, sequence_length) , __lowerCAmelCase ) _UpperCAmelCase = input_ids.copy() _UpperCAmelCase = self._get_uniform_logits(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = scores.copy() # instantiate all dist processors _UpperCAmelCase = FlaxTemperatureLogitsWarper(temperature=0.5 ) _UpperCAmelCase = FlaxTopKLogitsWarper(3 ) _UpperCAmelCase = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors _UpperCAmelCase = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=__lowerCAmelCase ) _UpperCAmelCase = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__lowerCAmelCase ) _UpperCAmelCase = FlaxForcedEOSTokenLogitsProcessor(max_length=__lowerCAmelCase , eos_token_id=__lowerCAmelCase ) _UpperCAmelCase = 10 # no processor list _UpperCAmelCase = temp_dist_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = top_k_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = top_p_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = min_dist_proc(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = bos_dist_proc(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = eos_dist_proc(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) # with processor list _UpperCAmelCase = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) _UpperCAmelCase = processor(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) # scores should be equal self.assertTrue(jnp.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() ) def lowerCAmelCase_ ( self : int ): _UpperCAmelCase = 4 _UpperCAmelCase = 10 _UpperCAmelCase = 15 _UpperCAmelCase = 2 _UpperCAmelCase = 1 _UpperCAmelCase = 15 # dummy input_ids and scores _UpperCAmelCase = ids_tensor((batch_size, sequence_length) , __lowerCAmelCase ) _UpperCAmelCase = input_ids.copy() _UpperCAmelCase = self._get_uniform_logits(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = scores.copy() # instantiate all dist processors _UpperCAmelCase = FlaxTemperatureLogitsWarper(temperature=0.5 ) _UpperCAmelCase = FlaxTopKLogitsWarper(3 ) _UpperCAmelCase = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors _UpperCAmelCase = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=__lowerCAmelCase ) _UpperCAmelCase = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__lowerCAmelCase ) _UpperCAmelCase = FlaxForcedEOSTokenLogitsProcessor(max_length=__lowerCAmelCase , eos_token_id=__lowerCAmelCase ) _UpperCAmelCase = 10 # no processor list def run_no_processor_list(__lowerCAmelCase : Dict , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[str] ): _UpperCAmelCase = temp_dist_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = top_k_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = top_p_warp(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = min_dist_proc(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = bos_dist_proc(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) _UpperCAmelCase = eos_dist_proc(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) return scores # with processor list def run_processor_list(__lowerCAmelCase : Any , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] ): _UpperCAmelCase = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) _UpperCAmelCase = processor(__lowerCAmelCase , __lowerCAmelCase , cur_len=__lowerCAmelCase ) return scores _UpperCAmelCase = jax.jit(__lowerCAmelCase ) _UpperCAmelCase = jax.jit(__lowerCAmelCase ) _UpperCAmelCase = jitted_run_no_processor_list(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = jitted_run_processor_list(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) # scores should be equal self.assertTrue(jnp.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() )
352
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) UpperCAmelCase__ = { """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""], """processing_trocr""": ["""TrOCRProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ """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 UpperCAmelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
30
0
"""simple docstring""" 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 a ( unittest.TestCase ): def __init__( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : List[Any]=2 , __lowerCAmelCase : Optional[int]=56 , __lowerCAmelCase : int=True , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : Optional[int]=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Any=99 , __lowerCAmelCase : Any=32 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : int=2 , __lowerCAmelCase : Tuple=7 , __lowerCAmelCase : Optional[Any]="gelu_new" , __lowerCAmelCase : int=0.1 , __lowerCAmelCase : int=0.1 , __lowerCAmelCase : List[Any]=512 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Any=0.02 , __lowerCAmelCase : Dict=4 , __lowerCAmelCase : Dict="block_sparse" , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=False , __lowerCAmelCase : List[str]=2 , __lowerCAmelCase : Dict=3 , ): _UpperCAmelCase = parent _UpperCAmelCase = batch_size _UpperCAmelCase = seq_length _UpperCAmelCase = is_training _UpperCAmelCase = use_attention_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_choices _UpperCAmelCase = rescale_embeddings _UpperCAmelCase = attention_type _UpperCAmelCase = use_bias _UpperCAmelCase = block_size _UpperCAmelCase = num_random_blocks def lowerCAmelCase_ ( self : List[str] ): _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase = None if self.use_attention_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 = 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=__lowerCAmelCase , 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 lowerCAmelCase_ ( self : Optional[int] ): _UpperCAmelCase = self.prepare_config_and_inputs() _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = config_and_inputs _UpperCAmelCase = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask, } return config, inputs_dict @require_flax class a ( lowerCAmelCase_ , unittest.TestCase ): _snake_case : str = ( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) _snake_case : List[Any] = False _snake_case : str = False def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def lowerCAmelCase_ ( self : str ): super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def lowerCAmelCase_ ( 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 lowerCAmelCase_ ( self : str ): super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def lowerCAmelCase_ ( self : str ): super().test_hidden_states_output() @slow def lowerCAmelCase_ ( self : List[str] ): for model_class_name in self.all_model_classes: _UpperCAmelCase = model_class_name.from_pretrained("""google/bigbird-roberta-base""" ) self.assertIsNotNone(__lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, 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 lowerCAmelCase_ ( self : int ): _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): _UpperCAmelCase = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = model_class(__lowerCAmelCase ) @jax.jit def model_jitted(__lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str=None , **__lowerCAmelCase : List[Any] ): return model(input_ids=__lowerCAmelCase , attention_mask=__lowerCAmelCase , **__lowerCAmelCase ) with self.subTest("""JIT Enabled""" ): _UpperCAmelCase = model_jitted(**__lowerCAmelCase ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): _UpperCAmelCase = model_jitted(**__lowerCAmelCase ).to_tuple() self.assertEqual(len(__lowerCAmelCase ) , len(__lowerCAmelCase ) ) for jitted_output, output in zip(__lowerCAmelCase , __lowerCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int=1e-5 , __lowerCAmelCase : List[str]="outputs" , __lowerCAmelCase : 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(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )
353
"""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 __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = ArgumentParser("""Accelerate CLI tool""" ,usage="""accelerate <command> [<args>]""" ,allow_abbrev=lowercase ) _UpperCAmelCase = parser.add_subparsers(help="""accelerate command helpers""" ) # Register commands get_config_parser(subparsers=lowercase ) env_command_parser(subparsers=lowercase ) launch_command_parser(subparsers=lowercase ) tpu_command_parser(subparsers=lowercase ) test_command_parser(subparsers=lowercase ) # Let's go _UpperCAmelCase = parser.parse_args() if not hasattr(lowercase ,"""func""" ): parser.print_help() exit(1 ) # Run args.func(lowercase ) if __name__ == "__main__": main()
30
0
"""simple docstring""" import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """_float_tensor""", """decoder.output_projection.weight""", ] for k in ignore_keys: state_dict.pop(lowercase ,lowercase ) def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = emb.weight.shape _UpperCAmelCase = nn.Linear(lowercase ,lowercase ,bias=lowercase ) _UpperCAmelCase = emb.weight.data return lin_layer def __UpperCAmelCase ( lowercase ,lowercase="facebook/mbart-large-en-ro" ,lowercase=False ,lowercase=False ): """simple docstring""" _UpperCAmelCase = torch.load(lowercase ,map_location="""cpu""" )["""model"""] remove_ignore_keys_(lowercase ) _UpperCAmelCase = state_dict["""encoder.embed_tokens.weight"""].shape[0] _UpperCAmelCase = MBartConfig.from_pretrained(lowercase ,vocab_size=lowercase ) if mbart_aa and finetuned: _UpperCAmelCase = """relu""" _UpperCAmelCase = state_dict["""decoder.embed_tokens.weight"""] _UpperCAmelCase = MBartForConditionalGeneration(lowercase ) model.model.load_state_dict(lowercase ) if finetuned: _UpperCAmelCase = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """fairseq_path""", type=str, help="""bart.large, bart.large.cnn or a path to a model.pt on local filesystem.""" ) parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--hf_config""", default="""facebook/mbart-large-cc25""", type=str, help="""Which huggingface architecture to use: mbart-large""", ) parser.add_argument("""--mbart_50""", action="""store_true""", help="""whether the model is mMART-50 checkpoint""") parser.add_argument("""--finetuned""", action="""store_true""", help="""whether the model is a fine-tuned checkpoint""") UpperCAmelCase__ = parser.parse_args() UpperCAmelCase__ = convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
354
"""simple docstring""" def __UpperCAmelCase ( lowercase ): """simple docstring""" return " ".join(input_str.split()[::-1] ) if __name__ == "__main__": import doctest doctest.testmod()
30
0
"""simple docstring""" from transformers import BertTokenizerFast from .custom_tokenization import CustomTokenizer class a ( lowerCAmelCase_ ): _snake_case : Optional[int] = CustomTokenizer pass
355
"""simple docstring""" import csv import tweepy # Twitter API credentials UpperCAmelCase__ = """""" UpperCAmelCase__ = """""" UpperCAmelCase__ = """""" UpperCAmelCase__ = """""" def __UpperCAmelCase ( lowercase ): """simple docstring""" # authorize twitter, initialize tweepy _UpperCAmelCase = tweepy.OAuthHandler(lowercase ,lowercase ) auth.set_access_token(lowercase ,lowercase ) _UpperCAmelCase = tweepy.API(lowercase ) # initialize a list to hold all the tweepy Tweets _UpperCAmelCase = [] # make initial request for most recent tweets (200 is the maximum allowed count) _UpperCAmelCase = api.user_timeline(screen_name=lowercase ,count=2_00 ) # save most recent tweets alltweets.extend(lowercase ) # save the id of the oldest tweet less one _UpperCAmelCase = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(lowercase ) > 0: print(f'''getting tweets before {oldest}''' ) # all subsequent requests use the max_id param to prevent duplicates _UpperCAmelCase = api.user_timeline( screen_name=lowercase ,count=2_00 ,max_id=lowercase ) # save most recent tweets alltweets.extend(lowercase ) # update the id of the oldest tweet less one _UpperCAmelCase = alltweets[-1].id - 1 print(f'''...{len(lowercase )} tweets downloaded so far''' ) # transform the tweepy tweets into a 2D array that will populate the csv _UpperCAmelCase = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(f'''new_{screen_name}_tweets.csv''' ,"""w""" ) as f: _UpperCAmelCase = csv.writer(lowercase ) writer.writerow(["""id""", """created_at""", """text"""] ) writer.writerows(lowercase ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets("""FirePing32""")
30
0
"""simple docstring""" from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class a : def __init__( self : Optional[int] , __lowerCAmelCase : Union[str, Any] , ): _UpperCAmelCase = parent _UpperCAmelCase = 13 _UpperCAmelCase = 7 _UpperCAmelCase = True _UpperCAmelCase = True _UpperCAmelCase = True _UpperCAmelCase = 99 _UpperCAmelCase = 32 _UpperCAmelCase = 2 _UpperCAmelCase = 4 _UpperCAmelCase = 37 _UpperCAmelCase = """gelu""" _UpperCAmelCase = 0.1 _UpperCAmelCase = 0.1 _UpperCAmelCase = 512 _UpperCAmelCase = 16 _UpperCAmelCase = 2 _UpperCAmelCase = 0.02 _UpperCAmelCase = 3 _UpperCAmelCase = 4 _UpperCAmelCase = None def lowerCAmelCase_ ( self : List[str] ): _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase = None if self.use_input_mask: _UpperCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) _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 = EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , 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, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase_ ( self : Dict ): ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) = self.prepare_config_and_inputs() _UpperCAmelCase = True _UpperCAmelCase = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : int ): _UpperCAmelCase = TFEsmModel(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask} _UpperCAmelCase = model(__lowerCAmelCase ) _UpperCAmelCase = [input_ids, input_mask] _UpperCAmelCase = model(__lowerCAmelCase ) _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase_ ( self : str , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : int , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Dict , __lowerCAmelCase : str , __lowerCAmelCase : int , ): _UpperCAmelCase = True _UpperCAmelCase = TFEsmModel(config=__lowerCAmelCase ) _UpperCAmelCase = { """input_ids""": input_ids, """attention_mask""": input_mask, """encoder_hidden_states""": encoder_hidden_states, """encoder_attention_mask""": encoder_attention_mask, } _UpperCAmelCase = model(__lowerCAmelCase ) _UpperCAmelCase = [input_ids, input_mask] _UpperCAmelCase = model(__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase ) # Also check the case where encoder outputs are not passed _UpperCAmelCase = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : int ): _UpperCAmelCase = TFEsmForMaskedLM(config=__lowerCAmelCase ) _UpperCAmelCase = model([input_ids, input_mask] ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : Tuple , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[int] ): _UpperCAmelCase = self.num_labels _UpperCAmelCase = TFEsmForTokenClassification(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase_ ( self : List[str] ): _UpperCAmelCase = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) = config_and_inputs _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class a ( lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase ): _snake_case : List[Any] = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) _snake_case : List[str] = ( { 'feature-extraction': TFEsmModel, 'fill-mask': TFEsmForMaskedLM, 'text-classification': TFEsmForSequenceClassification, 'token-classification': TFEsmForTokenClassification, 'zero-shot': TFEsmForSequenceClassification, } if is_tf_available() else {} ) _snake_case : str = False _snake_case : Optional[int] = False def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = TFEsmModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=__lowerCAmelCase , hidden_size=37 ) def lowerCAmelCase_ ( self : int ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : List[str] ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase ) @slow def lowerCAmelCase_ ( self : Optional[int] ): for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase = TFEsmModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) @unittest.skip("""Protein models do not support embedding resizing.""" ) def lowerCAmelCase_ ( self : List[Any] ): pass @unittest.skip("""Protein models do not support embedding resizing.""" ) def lowerCAmelCase_ ( self : str ): pass def lowerCAmelCase_ ( self : Dict ): _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(__lowerCAmelCase ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer _UpperCAmelCase = model.get_bias() assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) for k, v in name.items(): assert isinstance(__lowerCAmelCase , tf.Variable ) else: _UpperCAmelCase = model.get_output_embeddings() assert x is None _UpperCAmelCase = model.get_bias() assert name is None @require_tf class a ( unittest.TestCase ): @slow def lowerCAmelCase_ ( self : Union[str, Any] ): _UpperCAmelCase = TFEsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D""" ) _UpperCAmelCase = tf.constant([[0, 1, 2, 3, 4, 5]] ) _UpperCAmelCase = model(__lowerCAmelCase )[0] _UpperCAmelCase = [1, 6, 33] self.assertEqual(list(output.numpy().shape ) , __lowerCAmelCase ) # compare the actual values for a slice. _UpperCAmelCase = tf.constant( [ [ [8.921_518, -10.589_814, -6.4_671_307], [-6.3_967_156, -13.911_377, -1.1_211_915], [-7.781_247, -13.951_557, -3.740_592], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-2 ) ) @slow def lowerCAmelCase_ ( self : List[str] ): _UpperCAmelCase = TFEsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D""" ) _UpperCAmelCase = tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) _UpperCAmelCase = model(__lowerCAmelCase )[0] # compare the actual values for a slice. _UpperCAmelCase = tf.constant( [ [ [0.14_443_092, 0.54_125_327, 0.3_247_739], [0.30_340_484, 0.00_526_676, 0.31_077_722], [0.32_278_043, -0.24_987_096, 0.3_414_628], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
356
"""simple docstring""" import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = {} _UpperCAmelCase = tokenizer(example["""content"""] ,truncation=lowercase )["""input_ids"""] _UpperCAmelCase = len(example["""content"""] ) / len(output["""input_ids"""] ) return output UpperCAmelCase__ = HfArgumentParser(PretokenizationArguments) UpperCAmelCase__ = parser.parse_args() if args.num_workers is None: UpperCAmelCase__ = multiprocessing.cpu_count() UpperCAmelCase__ = AutoTokenizer.from_pretrained(args.tokenizer_dir) UpperCAmelCase__ = time.time() UpperCAmelCase__ = load_dataset(args.dataset_name, split="""train""") print(F'''Dataset loaded in {time.time()-t_start:.2f}s''') UpperCAmelCase__ = time.time() UpperCAmelCase__ = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ """repo_name""", """path""", """copies""", """size""", """content""", """license""", """hash""", """line_mean""", """line_max""", """alpha_frac""", """autogenerated""", ], ) print(F'''Dataset tokenized in {time.time()-t_start:.2f}s''') UpperCAmelCase__ = time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
30
0
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 UpperCAmelCase__ = """src/transformers""" # This is to make sure the transformers module imported is the one in the repo. UpperCAmelCase__ = direct_transformers_import(PATH_TO_TRANSFORMERS) UpperCAmelCase__ = transformers.models.auto.configuration_auto.CONFIG_MAPPING UpperCAmelCase__ = { # 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 __UpperCAmelCase ( lowercase ,lowercase ,lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = 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 ): _UpperCAmelCase = 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}"''' ,lowercase ,) is not None ): _UpperCAmelCase = 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: _UpperCAmelCase = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files _UpperCAmelCase = [ """bos_index""", """eos_index""", """pad_index""", """unk_index""", """mask_index""", """image_size""", """use_cache""", """out_features""", """out_indices""", ] _UpperCAmelCase = ["""encoder_no_repeat_ngram_size"""] # Special cases to be allowed _UpperCAmelCase = True if not attribute_used: _UpperCAmelCase = 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: _UpperCAmelCase = True elif attribute in ["tie_word_embeddings"] and default_value is False: _UpperCAmelCase = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: _UpperCAmelCase = True elif attribute.endswith("""_token_id""" ): _UpperCAmelCase = True # configuration class specific cases if not case_allowed: _UpperCAmelCase = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ ,[] ) _UpperCAmelCase = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = dict(inspect.signature(config_class.__init__ ).parameters ) _UpperCAmelCase = [x for x in list(signature.keys() ) if x not in ["""self""", """kwargs"""]] _UpperCAmelCase = [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 _UpperCAmelCase = {} if len(config_class.attribute_map ) > 0: _UpperCAmelCase = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files _UpperCAmelCase = inspect.getsourcefile(lowercase ) _UpperCAmelCase = os.path.dirname(lowercase ) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. _UpperCAmelCase = [os.path.join(lowercase ,lowercase ) for fn in os.listdir(lowercase ) if fn.startswith("""modeling_""" )] # Get the source code strings _UpperCAmelCase = [] for path in modeling_paths: if os.path.isfile(lowercase ): with open(lowercase ) as fp: modeling_sources.append(fp.read() ) _UpperCAmelCase = [] for config_param, default_value in zip(lowercase ,lowercase ): # `attributes` here is all the variant names for `config_param` _UpperCAmelCase = [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(lowercase ,lowercase ,lowercase ,lowercase ): unused_attributes.append(attributes[0] ) return sorted(lowercase ) def __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = {} 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.) _UpperCAmelCase = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class ) ,lambda lowercase : inspect.isclass(lowercase ) and issubclass(lowercase ,lowercase ) and inspect.getmodule(lowercase ) == inspect.getmodule(_config_class ) ,) ] for config_class in config_classes_in_module: _UpperCAmelCase = check_config_attributes_being_used(lowercase ) if len(lowercase ) > 0: _UpperCAmelCase = unused_attributes if len(lowercase ) > 0: _UpperCAmelCase = """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(lowercase ) if __name__ == "__main__": check_config_attributes()
357
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { """microsoft/layoutlmv3-base""": """https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json""", } class a ( lowerCAmelCase_ ): _snake_case : Any = 'layoutlmv3' def __init__( self : Optional[Any] , __lowerCAmelCase : Tuple=5_0265 , __lowerCAmelCase : Union[str, Any]=768 , __lowerCAmelCase : str=12 , __lowerCAmelCase : int=12 , __lowerCAmelCase : Any=3072 , __lowerCAmelCase : Any="gelu" , __lowerCAmelCase : Any=0.1 , __lowerCAmelCase : Tuple=0.1 , __lowerCAmelCase : Any=512 , __lowerCAmelCase : List[str]=2 , __lowerCAmelCase : Optional[Any]=0.02 , __lowerCAmelCase : Optional[int]=1e-5 , __lowerCAmelCase : int=1 , __lowerCAmelCase : Optional[Any]=0 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : List[str]=1024 , __lowerCAmelCase : Any=128 , __lowerCAmelCase : int=128 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : List[Any]=32 , __lowerCAmelCase : Any=128 , __lowerCAmelCase : int=64 , __lowerCAmelCase : List[str]=256 , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : Any=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : Optional[Any]=224 , __lowerCAmelCase : List[Any]=3 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[Any]=None , **__lowerCAmelCase : Union[str, Any] , ): super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) _UpperCAmelCase = max_ad_position_embeddings _UpperCAmelCase = coordinate_size _UpperCAmelCase = shape_size _UpperCAmelCase = has_relative_attention_bias _UpperCAmelCase = rel_pos_bins _UpperCAmelCase = max_rel_pos _UpperCAmelCase = has_spatial_attention_bias _UpperCAmelCase = rel_ad_pos_bins _UpperCAmelCase = max_rel_ad_pos _UpperCAmelCase = text_embed _UpperCAmelCase = visual_embed _UpperCAmelCase = input_size _UpperCAmelCase = num_channels _UpperCAmelCase = patch_size _UpperCAmelCase = classifier_dropout class a ( lowerCAmelCase_ ): _snake_case : str = version.parse('1.12' ) @property def lowerCAmelCase_ ( self : Dict ): # The order of inputs is different for question answering and sequence classification if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def lowerCAmelCase_ ( self : List[Any] ): return 1e-5 @property def lowerCAmelCase_ ( self : List[str] ): return 12 def lowerCAmelCase_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ): setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX _UpperCAmelCase = compute_effective_axis_dimension( __lowerCAmelCase , 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 _UpperCAmelCase = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) _UpperCAmelCase = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence _UpperCAmelCase = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes _UpperCAmelCase = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) _UpperCAmelCase = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
30
0
"""simple docstring""" import argparse import datetime def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = { """0""": """Sunday""", """1""": """Monday""", """2""": """Tuesday""", """3""": """Wednesday""", """4""": """Thursday""", """5""": """Friday""", """6""": """Saturday""", } _UpperCAmelCase = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(lowercase ) < 11: raise ValueError("""Must be 10 characters long""" ) # Get month _UpperCAmelCase = int(date_input[0] + date_input[1] ) # Validate if not 0 < m < 13: raise ValueError("""Month must be between 1 - 12""" ) _UpperCAmelCase = date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError("""Date separator must be '-' or '/'""" ) # Get day _UpperCAmelCase = int(date_input[3] + date_input[4] ) # Validate if not 0 < d < 32: raise ValueError("""Date must be between 1 - 31""" ) # Get second separator _UpperCAmelCase = date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError("""Date separator must be '-' or '/'""" ) # Get year _UpperCAmelCase = int(date_input[6] + date_input[7] + date_input[8] + date_input[9] ) # Arbitrary year range if not 45 < y < 85_00: raise ValueError( """Year out of range. There has to be some sort of limit...right?""" ) # Get datetime obj for validation _UpperCAmelCase = datetime.date(int(lowercase ) ,int(lowercase ) ,int(lowercase ) ) # Start math if m <= 2: _UpperCAmelCase = y - 1 _UpperCAmelCase = m + 12 # maths var _UpperCAmelCase = int(str(lowercase )[:2] ) _UpperCAmelCase = int(str(lowercase )[2:] ) _UpperCAmelCase = int(2.6 * m - 5.39 ) _UpperCAmelCase = int(c / 4 ) _UpperCAmelCase = int(k / 4 ) _UpperCAmelCase = int(d + k ) _UpperCAmelCase = int(t + u + v + x ) _UpperCAmelCase = int(z - (2 * c) ) _UpperCAmelCase = 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 _UpperCAmelCase = f'''Your date {date_input}, is a {days[str(lowercase )]}!''' 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)
358
"""simple docstring""" import csv from collections import defaultdict from dataclasses import dataclass, field from typing import List, Optional import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import ScalarFormatter from transformers import HfArgumentParser def __UpperCAmelCase ( lowercase=None ,lowercase=None ): """simple docstring""" return field(default_factory=lambda: default ,metadata=lowercase ) @dataclass class a : _snake_case : str = field( metadata={'help': 'The csv file to plot.'} , ) _snake_case : bool = field( default=lowerCAmelCase_ , metadata={'help': 'Whether to plot along batch size or sequence length. Defaults to sequence length.'} , ) _snake_case : bool = field( default=lowerCAmelCase_ , metadata={'help': 'Whether the csv file has time results or memory results. Defaults to memory results.'} , ) _snake_case : bool = field( default=lowerCAmelCase_ , metadata={'help': 'Disable logarithmic scale when plotting'} , ) _snake_case : bool = field( default=lowerCAmelCase_ , metadata={ 'help': 'Whether the csv file has training results or inference results. Defaults to inference results.' } , ) _snake_case : Optional[str] = field( default=lowerCAmelCase_ , metadata={'help': 'Filename under which the plot will be saved. If unused no plot is saved.'} , ) _snake_case : Optional[List[str]] = list_field( default=lowerCAmelCase_ , metadata={'help': 'List of model names that are used instead of the ones in the csv file.'} ) def __UpperCAmelCase ( lowercase ): """simple docstring""" try: int(lowercase ) return True except ValueError: return False def __UpperCAmelCase ( lowercase ): """simple docstring""" try: float(lowercase ) return True except ValueError: return False class a : def __init__( self : int , __lowerCAmelCase : Union[str, Any] ): _UpperCAmelCase = args _UpperCAmelCase = defaultdict(lambda: {"bsz": [], "seq_len": [], "result": {}} ) with open(self.args.csv_file , newline="""""" ) as csv_file: _UpperCAmelCase = csv.DictReader(__lowerCAmelCase ) for row in reader: _UpperCAmelCase = row["""model"""] self.result_dict[model_name]["bsz"].append(int(row["""batch_size"""] ) ) self.result_dict[model_name]["seq_len"].append(int(row["""sequence_length"""] ) ) if can_convert_to_int(row["""result"""] ): # value is not None _UpperCAmelCase = int(row["""result"""] ) elif can_convert_to_float(row["""result"""] ): # value is not None _UpperCAmelCase = float(row["""result"""] ) def lowerCAmelCase_ ( self : Optional[Any] ): _UpperCAmelCase , _UpperCAmelCase = plt.subplots() _UpperCAmelCase = """Time usage""" if self.args.is_time else """Memory usage""" _UpperCAmelCase = title_str + """ for training""" if self.args.is_train else title_str + """ for inference""" if not self.args.no_log_scale: # set logarithm scales ax.set_xscale("""log""" ) ax.set_yscale("""log""" ) for axis in [ax.xaxis, ax.yaxis]: axis.set_major_formatter(ScalarFormatter() ) for model_name_idx, model_name in enumerate(self.result_dict.keys() ): _UpperCAmelCase = sorted(set(self.result_dict[model_name]["""bsz"""] ) ) _UpperCAmelCase = sorted(set(self.result_dict[model_name]["""seq_len"""] ) ) _UpperCAmelCase = self.result_dict[model_name]["""result"""] ((_UpperCAmelCase) , (_UpperCAmelCase)) = ( (batch_sizes, sequence_lengths) if self.args.plot_along_batch else (sequence_lengths, batch_sizes) ) _UpperCAmelCase = ( model_name if self.args.short_model_names is None else self.args.short_model_names[model_name_idx] ) for inner_loop_value in inner_loop_array: if self.args.plot_along_batch: _UpperCAmelCase = np.asarray( [results[(x, inner_loop_value)] for x in x_axis_array if (x, inner_loop_value) in results] , dtype=__lowerCAmelCase , ) else: _UpperCAmelCase = np.asarray( [results[(inner_loop_value, x)] for x in x_axis_array if (inner_loop_value, x) in results] , dtype=np.floataa , ) ((_UpperCAmelCase) , (_UpperCAmelCase)) = ( ("""batch_size""", """len""") if self.args.plot_along_batch else ("""in #tokens""", """bsz""") ) _UpperCAmelCase = np.asarray(__lowerCAmelCase , __lowerCAmelCase )[: len(__lowerCAmelCase )] plt.scatter( __lowerCAmelCase , __lowerCAmelCase , label=f'''{label_model_name} - {inner_loop_label}: {inner_loop_value}''' ) plt.plot(__lowerCAmelCase , __lowerCAmelCase , """--""" ) title_str += f''' {label_model_name} vs.''' _UpperCAmelCase = title_str[:-4] _UpperCAmelCase = """Time in s""" if self.args.is_time else """Memory in MB""" # plot plt.title(__lowerCAmelCase ) plt.xlabel(__lowerCAmelCase ) plt.ylabel(__lowerCAmelCase ) plt.legend() if self.args.figure_png_file is not None: plt.savefig(self.args.figure_png_file ) else: plt.show() def __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = HfArgumentParser(lowercase ) _UpperCAmelCase = parser.parse_args_into_dataclasses()[0] _UpperCAmelCase = Plot(args=lowercase ) plot.plot() if __name__ == "__main__": main()
30
0
"""simple docstring""" import flax.linen as nn import jax import jax.numpy as jnp class a ( nn.Module ): _snake_case : int _snake_case : jnp.dtype = jnp.floataa def lowerCAmelCase_ ( self : Dict ): _UpperCAmelCase = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self : Any , __lowerCAmelCase : Optional[Any] ): _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = hidden_states.shape _UpperCAmelCase = jax.image.resize( __lowerCAmelCase , shape=(batch, height * 2, width * 2, channels) , method="""nearest""" , ) _UpperCAmelCase = self.conv(__lowerCAmelCase ) return hidden_states class a ( nn.Module ): _snake_case : int _snake_case : jnp.dtype = jnp.floataa def lowerCAmelCase_ ( self : Dict ): _UpperCAmelCase = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self : int , __lowerCAmelCase : List[Any] ): # pad = ((0, 0), (0, 1), (0, 1), (0, 0)) # pad height and width dim # hidden_states = jnp.pad(hidden_states, pad_width=pad) _UpperCAmelCase = self.conv(__lowerCAmelCase ) return hidden_states class a ( nn.Module ): _snake_case : int _snake_case : int = None _snake_case : float = 0.0 _snake_case : bool = None _snake_case : jnp.dtype = jnp.floataa def lowerCAmelCase_ ( self : Union[str, Any] ): _UpperCAmelCase = self.in_channels if self.out_channels is None else self.out_channels _UpperCAmelCase = nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) _UpperCAmelCase = nn.Conv( __lowerCAmelCase , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) _UpperCAmelCase = nn.Dense(__lowerCAmelCase , dtype=self.dtype ) _UpperCAmelCase = nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) _UpperCAmelCase = nn.Dropout(self.dropout_prob ) _UpperCAmelCase = nn.Conv( __lowerCAmelCase , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) _UpperCAmelCase = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut _UpperCAmelCase = None if use_nin_shortcut: _UpperCAmelCase = nn.Conv( __lowerCAmelCase , kernel_size=(1, 1) , strides=(1, 1) , padding="""VALID""" , dtype=self.dtype , ) def __call__( self : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any]=True ): _UpperCAmelCase = hidden_states _UpperCAmelCase = self.norma(__lowerCAmelCase ) _UpperCAmelCase = nn.swish(__lowerCAmelCase ) _UpperCAmelCase = self.conva(__lowerCAmelCase ) _UpperCAmelCase = self.time_emb_proj(nn.swish(__lowerCAmelCase ) ) _UpperCAmelCase = jnp.expand_dims(jnp.expand_dims(__lowerCAmelCase , 1 ) , 1 ) _UpperCAmelCase = hidden_states + temb _UpperCAmelCase = self.norma(__lowerCAmelCase ) _UpperCAmelCase = nn.swish(__lowerCAmelCase ) _UpperCAmelCase = self.dropout(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = self.conva(__lowerCAmelCase ) if self.conv_shortcut is not None: _UpperCAmelCase = self.conv_shortcut(__lowerCAmelCase ) return hidden_states + residual
359
"""simple docstring""" import os import pytest from attr import dataclass UpperCAmelCase__ = """us-east-1""" # defaults region @dataclass class a : _snake_case : str _snake_case : Tuple = 'arn:aws:iam::558105141721:role/sagemaker_execution_role' _snake_case : List[Any] = { 'task_name': 'mnli', 'per_device_train_batch_size': 16, 'per_device_eval_batch_size': 16, 'do_train': True, 'do_eval': True, 'do_predict': True, 'output_dir': '/opt/ml/model', 'overwrite_output_dir': True, 'max_steps': 5_00, 'save_steps': 55_00, } _snake_case : Optional[Any] = {**hyperparameters, 'max_steps': 10_00} @property def lowerCAmelCase_ ( self : Optional[Any] ): if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def lowerCAmelCase_ ( self : Dict ): return f'''{self.framework}-transfromers-test''' @property def lowerCAmelCase_ ( self : Union[str, Any] ): return f'''./tests/sagemaker/scripts/{self.framework}''' @property def lowerCAmelCase_ ( self : Dict ): if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = SageMakerTestEnvironment(framework=request.cls.framework )
30
0
"""simple docstring""" def __UpperCAmelCase ( lowercase ): """simple docstring""" try: _UpperCAmelCase = float(lowercase ) except ValueError: raise ValueError("""Please enter a valid number""" ) _UpperCAmelCase = decimal - int(lowercase ) if fractional_part == 0: return int(lowercase ), 1 else: _UpperCAmelCase = len(str(lowercase ).split(""".""" )[1] ) _UpperCAmelCase = int(decimal * (10**number_of_frac_digits) ) _UpperCAmelCase = 10**number_of_frac_digits _UpperCAmelCase , _UpperCAmelCase = denominator, numerator while True: _UpperCAmelCase = dividend % divisor if remainder == 0: break _UpperCAmelCase , _UpperCAmelCase = divisor, remainder _UpperCAmelCase , _UpperCAmelCase = numerator / divisor, denominator / divisor return int(lowercase ), int(lowercase ) if __name__ == "__main__": print(F'''{decimal_to_fraction(2) = }''') print(F'''{decimal_to_fraction(89.0) = }''') print(F'''{decimal_to_fraction("67") = }''') print(F'''{decimal_to_fraction("45.0") = }''') print(F'''{decimal_to_fraction(1.5) = }''') print(F'''{decimal_to_fraction("6.25") = }''') print(F'''{decimal_to_fraction("78td") = }''')
360
"""simple docstring""" import string from math import logaa def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = document.translate( str.maketrans("""""" ,"""""" ,string.punctuation ) ).replace("""\n""" ,"""""" ) _UpperCAmelCase = document_without_punctuation.split(""" """ ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = corpus.lower().translate( str.maketrans("""""" ,"""""" ,string.punctuation ) ) # strip all punctuation and replace it with '' _UpperCAmelCase = corpus_without_punctuation.split("""\n""" ) _UpperCAmelCase = term.lower() return (len([doc for doc in docs if term in doc] ), len(lowercase )) def __UpperCAmelCase ( lowercase ,lowercase ,lowercase=False ): """simple docstring""" if smoothing: if n == 0: raise ValueError("""log10(0) is undefined.""" ) return round(1 + logaa(n / (1 + df) ) ,3 ) if df == 0: raise ZeroDivisionError("""df must be > 0""" ) elif n == 0: raise ValueError("""log10(0) is undefined.""" ) return round(logaa(n / df ) ,3 ) def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" return round(tf * idf ,3 )
30
0
"""simple docstring""" def __UpperCAmelCase ( lowercase ): """simple docstring""" if num <= 0: raise ValueError("""Input must be a positive integer""" ) _UpperCAmelCase = [True] * (num + 1) _UpperCAmelCase = 2 while p * p <= num: if primes[p]: for i in range(p * p ,num + 1 ,lowercase ): _UpperCAmelCase = False p += 1 return [prime for prime in range(2 ,num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase__ = int(input("""Enter a positive integer: """).strip()) print(prime_sieve_eratosthenes(user_num))
361
"""simple docstring""" import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging UpperCAmelCase__ = logging.get_logger(__name__) logging.set_verbosity_info() def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" if "xprophetnet" in prophetnet_checkpoint_path: _UpperCAmelCase = XLMProphetNetForConditionalGenerationOld.from_pretrained(lowercase ) _UpperCAmelCase , _UpperCAmelCase = XLMProphetNetForConditionalGeneration.from_pretrained( lowercase ,output_loading_info=lowercase ) else: _UpperCAmelCase = ProphetNetForConditionalGenerationOld.from_pretrained(lowercase ) _UpperCAmelCase , _UpperCAmelCase = ProphetNetForConditionalGeneration.from_pretrained( lowercase ,output_loading_info=lowercase ) _UpperCAmelCase = ["""key_proj""", """value_proj""", """query_proj"""] _UpperCAmelCase = { """self_attn""": """ngram_self_attn""", """cross_attn""": """encoder_attn""", """cross_attn_layer_norm""": """encoder_attn_layer_norm""", """feed_forward_layer_norm""": """final_layer_norm""", """feed_forward""": """""", """intermediate""": """fc1""", """output""": """fc2""", """key_proj""": """k_proj""", """query_proj""": """q_proj""", """value_proj""": """v_proj""", """word_embeddings""": """embed_tokens""", """embeddings_layer_norm""": """emb_layer_norm""", """relative_pos_embeddings""": """relative_linear""", """ngram_embeddings""": """ngram_input_embed""", """position_embeddings""": """embed_positions""", } for key in loading_info["missing_keys"]: _UpperCAmelCase = key.split(""".""" ) if attributes[0] == "lm_head": _UpperCAmelCase = prophet _UpperCAmelCase = prophet_old else: _UpperCAmelCase = prophet.prophetnet _UpperCAmelCase = prophet_old.model _UpperCAmelCase = False for attribute in attributes: if attribute in mapping: _UpperCAmelCase = mapping[attribute] if not hasattr(lowercase ,lowercase ) and len(lowercase ) > 0: _UpperCAmelCase = attribute elif hasattr(lowercase ,lowercase ): _UpperCAmelCase = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" _UpperCAmelCase = old_model.weight logger.info(f'''{attribute} is initialized.''' ) _UpperCAmelCase = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" _UpperCAmelCase = old_model.bias logger.info(f'''{attribute} is initialized''' ) _UpperCAmelCase = True break elif attribute in special_keys and hasattr(lowercase ,"""in_proj_weight""" ): _UpperCAmelCase = old_model.in_proj_weight.shape[0] // 3 _UpperCAmelCase = getattr(lowercase ,lowercase ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": _UpperCAmelCase = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) _UpperCAmelCase = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": _UpperCAmelCase = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) _UpperCAmelCase = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": _UpperCAmelCase = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) _UpperCAmelCase = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) _UpperCAmelCase = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." _UpperCAmelCase = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) _UpperCAmelCase = True break if attribute.isdigit(): _UpperCAmelCase = model[int(lowercase )] _UpperCAmelCase = old_model[int(lowercase )] else: _UpperCAmelCase = getattr(lowercase ,lowercase ) if old_attribute == "": _UpperCAmelCase = old_model else: if not hasattr(lowercase ,lowercase ): raise ValueError(f'''{old_model} does not have {old_attribute}''' ) _UpperCAmelCase = getattr(lowercase ,lowercase ) if not is_key_init: raise ValueError(f'''{key} was not correctly initialized!''' ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) prophet.save_pretrained(lowercase ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--prophetnet_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.""" ) UpperCAmelCase__ = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
30
0
"""simple docstring""" import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline 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_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class a ( lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase ): _snake_case : Optional[int] = IFInpaintingSuperResolutionPipeline _snake_case : Union[str, Any] = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} _snake_case : Union[str, Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) _snake_case : List[str] = PipelineTesterMixin.required_optional_params - {'latents'} def lowerCAmelCase_ ( self : List[Any] ): return self._get_superresolution_dummy_components() def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[Any]=0 ): if str(__lowerCAmelCase ).startswith("""mps""" ): _UpperCAmelCase = torch.manual_seed(__lowerCAmelCase ) else: _UpperCAmelCase = torch.Generator(device=__lowerCAmelCase ).manual_seed(__lowerCAmelCase ) _UpperCAmelCase = floats_tensor((1, 3, 16, 16) , rng=random.Random(__lowerCAmelCase ) ).to(__lowerCAmelCase ) _UpperCAmelCase = floats_tensor((1, 3, 32, 32) , rng=random.Random(__lowerCAmelCase ) ).to(__lowerCAmelCase ) _UpperCAmelCase = floats_tensor((1, 3, 32, 32) , rng=random.Random(__lowerCAmelCase ) ).to(__lowerCAmelCase ) _UpperCAmelCase = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """original_image""": original_image, """mask_image""": mask_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 lowerCAmelCase_ ( self : Tuple ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def lowerCAmelCase_ ( self : str ): self._test_save_load_optional_components() @unittest.skipIf(torch_device != """cuda""" , reason="""float16 requires CUDA""" ) def lowerCAmelCase_ ( self : List[Any] ): # 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 lowerCAmelCase_ ( self : List[Any] ): self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def lowerCAmelCase_ ( self : Tuple ): self._test_save_load_local() def lowerCAmelCase_ ( self : List[str] ): self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
362
"""simple docstring""" import mpmath # for roots of unity import numpy as np class a : def __init__( self : Tuple , __lowerCAmelCase : Dict=None , __lowerCAmelCase : Union[str, Any]=None ): # Input as list _UpperCAmelCase = list(poly_a or [0] )[:] _UpperCAmelCase = list(poly_b or [0] )[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() _UpperCAmelCase = len(self.polyA ) while self.polyB[-1] == 0: self.polyB.pop() _UpperCAmelCase = len(self.polyB ) # Add 0 to make lengths equal a power of 2 _UpperCAmelCase = int( 2 ** np.ceil(np.loga(len(self.polyA ) + len(self.polyB ) - 1 ) ) ) while len(self.polyA ) < self.c_max_length: self.polyA.append(0 ) while len(self.polyB ) < self.c_max_length: self.polyB.append(0 ) # A complex root used for the fourier transform _UpperCAmelCase = complex(mpmath.root(x=1 , n=self.c_max_length , k=1 ) ) # The product _UpperCAmelCase = self.__multiply() def lowerCAmelCase_ ( self : Optional[int] , __lowerCAmelCase : str ): _UpperCAmelCase = [[x] for x in self.polyA] if which == """A""" else [[x] for x in self.polyB] # Corner case if len(__lowerCAmelCase ) <= 1: return dft[0] # _UpperCAmelCase = self.c_max_length // 2 while next_ncol > 0: _UpperCAmelCase = [[] for i in range(__lowerCAmelCase )] _UpperCAmelCase = self.root**next_ncol # First half of next step _UpperCAmelCase = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(__lowerCAmelCase ): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j] ) current_root *= root # Second half of next step _UpperCAmelCase = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(__lowerCAmelCase ): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j] ) current_root *= root # Update _UpperCAmelCase = new_dft _UpperCAmelCase = next_ncol // 2 return dft[0] def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = self.__dft("""A""" ) _UpperCAmelCase = self.__dft("""B""" ) _UpperCAmelCase = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length )]] del dft_a del dft_b # Corner Case if len(inverce_c[0] ) <= 1: return inverce_c[0] # Inverse DFT _UpperCAmelCase = 2 while next_ncol <= self.c_max_length: _UpperCAmelCase = [[] for i in range(__lowerCAmelCase )] _UpperCAmelCase = self.root ** (next_ncol // 2) _UpperCAmelCase = 1 # First half of next step for j in range(self.c_max_length // next_ncol ): for i in range(next_ncol // 2 ): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2 ) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root) ) current_root *= root # Update _UpperCAmelCase = new_inverse_c next_ncol *= 2 # Unpack _UpperCAmelCase = [round(x[0].real , 8 ) + round(x[0].imag , 8 ) * 1j for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self : Dict ): _UpperCAmelCase = """A = """ + """ + """.join( f'''{coef}*x^{i}''' for coef, i in enumerate(self.polyA[: self.len_A] ) ) _UpperCAmelCase = """B = """ + """ + """.join( f'''{coef}*x^{i}''' for coef, i in enumerate(self.polyB[: self.len_B] ) ) _UpperCAmelCase = """A*B = """ + """ + """.join( f'''{coef}*x^{i}''' for coef, i in enumerate(self.product ) ) return f'''{a}\n{b}\n{c}''' # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
30
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase__ = { """configuration_xlm""": ["""XLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XLMConfig""", """XLMOnnxConfig"""], """tokenization_xlm""": ["""XLMTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ """XLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """XLMForMultipleChoice""", """XLMForQuestionAnswering""", """XLMForQuestionAnsweringSimple""", """XLMForSequenceClassification""", """XLMForTokenClassification""", """XLMModel""", """XLMPreTrainedModel""", """XLMWithLMHeadModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ """TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFXLMForMultipleChoice""", """TFXLMForQuestionAnsweringSimple""", """TFXLMForSequenceClassification""", """TFXLMForTokenClassification""", """TFXLMMainLayer""", """TFXLMModel""", """TFXLMPreTrainedModel""", """TFXLMWithLMHeadModel""", ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
363
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto.configuration_auto import CONFIG_MAPPING UpperCAmelCase__ = logging.get_logger(__name__) class a ( lowerCAmelCase_ ): _snake_case : List[str] = 'upernet' def __init__( self : Tuple , __lowerCAmelCase : int=None , __lowerCAmelCase : Tuple=512 , __lowerCAmelCase : Union[str, Any]=0.02 , __lowerCAmelCase : Tuple=[1, 2, 3, 6] , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=0.4 , __lowerCAmelCase : Union[str, Any]=384 , __lowerCAmelCase : Optional[int]=256 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Optional[int]=False , __lowerCAmelCase : Optional[int]=255 , **__lowerCAmelCase : Union[str, Any] , ): super().__init__(**__lowerCAmelCase ) if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) _UpperCAmelCase = CONFIG_MAPPING["""resnet"""](out_features=["""stage1""", """stage2""", """stage3""", """stage4"""] ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): _UpperCAmelCase = backbone_config.get("""model_type""" ) _UpperCAmelCase = CONFIG_MAPPING[backbone_model_type] _UpperCAmelCase = config_class.from_dict(__lowerCAmelCase ) _UpperCAmelCase = backbone_config _UpperCAmelCase = hidden_size _UpperCAmelCase = initializer_range _UpperCAmelCase = pool_scales _UpperCAmelCase = use_auxiliary_head _UpperCAmelCase = auxiliary_loss_weight _UpperCAmelCase = auxiliary_in_channels _UpperCAmelCase = auxiliary_channels _UpperCAmelCase = auxiliary_num_convs _UpperCAmelCase = auxiliary_concat_input _UpperCAmelCase = loss_ignore_index def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = copy.deepcopy(self.__dict__ ) _UpperCAmelCase = self.backbone_config.to_dict() _UpperCAmelCase = self.__class__.model_type return output
30
0
"""simple docstring""" from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig UpperCAmelCase__ = logging.get_logger(__name__) # General docstring UpperCAmelCase__ = """RegNetConfig""" # Base docstring UpperCAmelCase__ = """facebook/regnet-y-040""" UpperCAmelCase__ = [1, 1_0_8_8, 7, 7] # Image classification docstring UpperCAmelCase__ = """facebook/regnet-y-040""" UpperCAmelCase__ = """tabby, tabby cat""" UpperCAmelCase__ = [ """facebook/regnet-y-040""", # See all regnet models at https://huggingface.co/models?filter=regnet ] class a ( tf.keras.layers.Layer ): def __init__( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 1 , __lowerCAmelCase : int = 1 , __lowerCAmelCase : Optional[str] = "relu" , **__lowerCAmelCase : Optional[Any] , ): super().__init__(**__lowerCAmelCase ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb _UpperCAmelCase = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) _UpperCAmelCase = tf.keras.layers.ConvaD( filters=__lowerCAmelCase , kernel_size=__lowerCAmelCase , strides=__lowerCAmelCase , padding="""VALID""" , groups=__lowerCAmelCase , use_bias=__lowerCAmelCase , name="""convolution""" , ) _UpperCAmelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name="""normalization""" ) _UpperCAmelCase = ACTaFN[activation] if activation is not None else tf.identity def lowerCAmelCase_ ( self : Any , __lowerCAmelCase : Optional[int] ): _UpperCAmelCase = self.convolution(self.padding(__lowerCAmelCase ) ) _UpperCAmelCase = self.normalization(__lowerCAmelCase ) _UpperCAmelCase = self.activation(__lowerCAmelCase ) return hidden_state class a ( tf.keras.layers.Layer ): def __init__( self : Optional[int] , __lowerCAmelCase : RegNetConfig , **__lowerCAmelCase : Dict ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = config.num_channels _UpperCAmelCase = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name="""embedder""" , ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : Optional[int] ): _UpperCAmelCase = shape_list(__lowerCAmelCase )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( """Make sure that the channel dimension of the pixel values match with the one set in the configuration.""" ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) _UpperCAmelCase = tf.transpose(__lowerCAmelCase , perm=(0, 2, 3, 1) ) _UpperCAmelCase = self.embedder(__lowerCAmelCase ) return hidden_state class a ( tf.keras.layers.Layer ): def __init__( self : str , __lowerCAmelCase : int , __lowerCAmelCase : int = 2 , **__lowerCAmelCase : List[str] ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = tf.keras.layers.ConvaD( filters=__lowerCAmelCase , kernel_size=1 , strides=__lowerCAmelCase , use_bias=__lowerCAmelCase , name="""convolution""" ) _UpperCAmelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name="""normalization""" ) def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : tf.Tensor , __lowerCAmelCase : bool = False ): return self.normalization(self.convolution(__lowerCAmelCase ) , training=__lowerCAmelCase ) class a ( tf.keras.layers.Layer ): def __init__( self : Union[str, Any] , __lowerCAmelCase : int , __lowerCAmelCase : int , **__lowerCAmelCase : int ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=__lowerCAmelCase , name="""pooler""" ) _UpperCAmelCase = [ tf.keras.layers.ConvaD(filters=__lowerCAmelCase , kernel_size=1 , activation="""relu""" , name="""attention.0""" ), tf.keras.layers.ConvaD(filters=__lowerCAmelCase , kernel_size=1 , activation="""sigmoid""" , name="""attention.2""" ), ] def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : Optional[int] ): # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] _UpperCAmelCase = self.pooler(__lowerCAmelCase ) for layer_module in self.attention: _UpperCAmelCase = layer_module(__lowerCAmelCase ) _UpperCAmelCase = hidden_state * pooled return hidden_state class a ( tf.keras.layers.Layer ): def __init__( self : Any , __lowerCAmelCase : RegNetConfig , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int = 1 , **__lowerCAmelCase : List[str] ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = in_channels != out_channels or stride != 1 _UpperCAmelCase = max(1 , out_channels // config.groups_width ) _UpperCAmelCase = ( TFRegNetShortCut(__lowerCAmelCase , stride=__lowerCAmelCase , name="""shortcut""" ) if should_apply_shortcut else tf.keras.layers.Activation("""linear""" , name="""shortcut""" ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. _UpperCAmelCase = [ TFRegNetConvLayer(__lowerCAmelCase , kernel_size=1 , activation=config.hidden_act , name="""layer.0""" ), TFRegNetConvLayer( __lowerCAmelCase , stride=__lowerCAmelCase , groups=__lowerCAmelCase , activation=config.hidden_act , name="""layer.1""" ), TFRegNetConvLayer(__lowerCAmelCase , kernel_size=1 , activation=__lowerCAmelCase , name="""layer.2""" ), ] _UpperCAmelCase = ACTaFN[config.hidden_act] def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : Any ): _UpperCAmelCase = hidden_state for layer_module in self.layers: _UpperCAmelCase = layer_module(__lowerCAmelCase ) _UpperCAmelCase = self.shortcut(__lowerCAmelCase ) hidden_state += residual _UpperCAmelCase = self.activation(__lowerCAmelCase ) return hidden_state class a ( tf.keras.layers.Layer ): def __init__( self : Tuple , __lowerCAmelCase : RegNetConfig , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int = 1 , **__lowerCAmelCase : List[Any] ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = in_channels != out_channels or stride != 1 _UpperCAmelCase = max(1 , out_channels // config.groups_width ) _UpperCAmelCase = ( TFRegNetShortCut(__lowerCAmelCase , stride=__lowerCAmelCase , name="""shortcut""" ) if should_apply_shortcut else tf.keras.layers.Activation("""linear""" , name="""shortcut""" ) ) _UpperCAmelCase = [ TFRegNetConvLayer(__lowerCAmelCase , kernel_size=1 , activation=config.hidden_act , name="""layer.0""" ), TFRegNetConvLayer( __lowerCAmelCase , stride=__lowerCAmelCase , groups=__lowerCAmelCase , activation=config.hidden_act , name="""layer.1""" ), TFRegNetSELayer(__lowerCAmelCase , reduced_channels=int(round(in_channels / 4 ) ) , name="""layer.2""" ), TFRegNetConvLayer(__lowerCAmelCase , kernel_size=1 , activation=__lowerCAmelCase , name="""layer.3""" ), ] _UpperCAmelCase = ACTaFN[config.hidden_act] def lowerCAmelCase_ ( self : Any , __lowerCAmelCase : Dict ): _UpperCAmelCase = hidden_state for layer_module in self.layers: _UpperCAmelCase = layer_module(__lowerCAmelCase ) _UpperCAmelCase = self.shortcut(__lowerCAmelCase ) hidden_state += residual _UpperCAmelCase = self.activation(__lowerCAmelCase ) return hidden_state class a ( tf.keras.layers.Layer ): def __init__( self : Any , __lowerCAmelCase : RegNetConfig , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int = 2 , __lowerCAmelCase : int = 2 , **__lowerCAmelCase : Dict ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = TFRegNetXLayer if config.layer_type == """x""" else TFRegNetYLayer _UpperCAmelCase = [ # downsampling is done in the first layer with stride of 2 layer(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , stride=__lowerCAmelCase , name="""layers.0""" ), *[layer(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def lowerCAmelCase_ ( self : int , __lowerCAmelCase : Optional[Any] ): for layer_module in self.layers: _UpperCAmelCase = layer_module(__lowerCAmelCase ) return hidden_state class a ( tf.keras.layers.Layer ): def __init__( self : str , __lowerCAmelCase : RegNetConfig , **__lowerCAmelCase : Optional[Any] ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( __lowerCAmelCase , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name="""stages.0""" , ) ) _UpperCAmelCase = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(__lowerCAmelCase , config.depths[1:] ) ): self.stages.append(TFRegNetStage(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , depth=__lowerCAmelCase , name=f'''stages.{i+1}''' ) ) def lowerCAmelCase_ ( self : Tuple , __lowerCAmelCase : tf.Tensor , __lowerCAmelCase : bool = False , __lowerCAmelCase : bool = True ): _UpperCAmelCase = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: _UpperCAmelCase = hidden_states + (hidden_state,) _UpperCAmelCase = stage_module(__lowerCAmelCase ) if output_hidden_states: _UpperCAmelCase = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=__lowerCAmelCase , hidden_states=__lowerCAmelCase ) @keras_serializable class a ( tf.keras.layers.Layer ): _snake_case : List[Any] = RegNetConfig def __init__( self : Optional[int] , __lowerCAmelCase : Optional[int] , **__lowerCAmelCase : Optional[int] ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = config _UpperCAmelCase = TFRegNetEmbeddings(__lowerCAmelCase , name="""embedder""" ) _UpperCAmelCase = TFRegNetEncoder(__lowerCAmelCase , name="""encoder""" ) _UpperCAmelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=__lowerCAmelCase , name="""pooler""" ) @unpack_inputs def lowerCAmelCase_ ( self : Any , __lowerCAmelCase : tf.Tensor , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : bool = False , ): _UpperCAmelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _UpperCAmelCase = return_dict if return_dict is not None else self.config.use_return_dict _UpperCAmelCase = self.embedder(__lowerCAmelCase , training=__lowerCAmelCase ) _UpperCAmelCase = self.encoder( __lowerCAmelCase , output_hidden_states=__lowerCAmelCase , return_dict=__lowerCAmelCase , training=__lowerCAmelCase ) _UpperCAmelCase = encoder_outputs[0] _UpperCAmelCase = self.pooler(__lowerCAmelCase ) # Change to NCHW output format have uniformity in the modules _UpperCAmelCase = tf.transpose(__lowerCAmelCase , perm=(0, 3, 1, 2) ) _UpperCAmelCase = tf.transpose(__lowerCAmelCase , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: _UpperCAmelCase = tuple([tf.transpose(__lowerCAmelCase , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=__lowerCAmelCase , pooler_output=__lowerCAmelCase , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class a ( lowerCAmelCase_ ): _snake_case : List[str] = RegNetConfig _snake_case : Optional[Any] = 'regnet' _snake_case : Union[str, Any] = 'pixel_values' @property def lowerCAmelCase_ ( self : Any ): return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa )} UpperCAmelCase__ = r""" Parameters: This model is a Tensorflow [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and behavior. 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 [`~TFPreTrainedModel.from_pretrained`] method to load the model weights. """ UpperCAmelCase__ = r""" Args: pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConveNextImageProcessor.__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 [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( 'The bare RegNet model outputting raw features without any specific head on top.' , lowerCAmelCase_ , ) class a ( lowerCAmelCase_ ): def __init__( self : int , __lowerCAmelCase : RegNetConfig , *__lowerCAmelCase : Dict , **__lowerCAmelCase : List[str] ): super().__init__(__lowerCAmelCase , *__lowerCAmelCase , **__lowerCAmelCase ) _UpperCAmelCase = TFRegNetMainLayer(__lowerCAmelCase , name="""regnet""" ) @unpack_inputs @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 lowerCAmelCase_ ( self : Union[str, Any] , __lowerCAmelCase : tf.Tensor , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Any=False , ): _UpperCAmelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _UpperCAmelCase = return_dict if return_dict is not None else self.config.use_return_dict _UpperCAmelCase = self.regnet( pixel_values=__lowerCAmelCase , output_hidden_states=__lowerCAmelCase , return_dict=__lowerCAmelCase , training=__lowerCAmelCase , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( '\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , lowerCAmelCase_ , ) class a ( lowerCAmelCase_ , lowerCAmelCase_ ): def __init__( self : Dict , __lowerCAmelCase : RegNetConfig , *__lowerCAmelCase : List[Any] , **__lowerCAmelCase : List[Any] ): super().__init__(__lowerCAmelCase , *__lowerCAmelCase , **__lowerCAmelCase ) _UpperCAmelCase = config.num_labels _UpperCAmelCase = TFRegNetMainLayer(__lowerCAmelCase , name="""regnet""" ) # classification head _UpperCAmelCase = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name="""classifier.1""" ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(__lowerCAmelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=__lowerCAmelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def lowerCAmelCase_ ( self : str , __lowerCAmelCase : tf.Tensor = None , __lowerCAmelCase : tf.Tensor = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : str=False , ): _UpperCAmelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _UpperCAmelCase = return_dict if return_dict is not None else self.config.use_return_dict _UpperCAmelCase = self.regnet( __lowerCAmelCase , output_hidden_states=__lowerCAmelCase , return_dict=__lowerCAmelCase , training=__lowerCAmelCase ) _UpperCAmelCase = outputs.pooler_output if return_dict else outputs[1] _UpperCAmelCase = self.classifier[0](__lowerCAmelCase ) _UpperCAmelCase = self.classifier[1](__lowerCAmelCase ) _UpperCAmelCase = None if labels is None else self.hf_compute_loss(labels=__lowerCAmelCase , logits=__lowerCAmelCase ) if not return_dict: _UpperCAmelCase = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=__lowerCAmelCase , logits=__lowerCAmelCase , hidden_states=outputs.hidden_states )
364
"""simple docstring""" from itertools import product def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = sides_number _UpperCAmelCase = max_face_number * dice_number _UpperCAmelCase = [0] * (max_total + 1) _UpperCAmelCase = 1 _UpperCAmelCase = range(lowercase ,max_face_number + 1 ) for dice_numbers in product(lowercase ,repeat=lowercase ): _UpperCAmelCase = sum(lowercase ) totals_frequencies[total] += 1 return totals_frequencies def __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = total_frequency_distribution( sides_number=4 ,dice_number=9 ) _UpperCAmelCase = total_frequency_distribution( sides_number=6 ,dice_number=6 ) _UpperCAmelCase = 0 _UpperCAmelCase = 9 _UpperCAmelCase = 4 * 9 _UpperCAmelCase = 6 for peter_total in range(lowercase ,max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) _UpperCAmelCase = (4**9) * (6**6) _UpperCAmelCase = peter_wins_count / total_games_number _UpperCAmelCase = round(lowercase ,ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(F'''{solution() = }''')
30
0
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class a : def lowerCAmelCase_ ( self : Dict ): torch.manual_seed(0 ) _UpperCAmelCase = TaEncoderModel.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) _UpperCAmelCase = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) _UpperCAmelCase = UNetaDConditionModel( sample_size=32 , layers_per_block=1 , block_out_channels=[32, 64] , down_block_types=[ """ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D""", ] , mid_block_type="""UNetMidBlock2DSimpleCrossAttn""" , up_block_types=["""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""] , in_channels=3 , out_channels=6 , cross_attention_dim=32 , encoder_hid_dim=32 , attention_head_dim=8 , addition_embed_type="""text""" , addition_embed_type_num_heads=2 , cross_attention_norm="""group_norm""" , resnet_time_scale_shift="""scale_shift""" , act_fn="""gelu""" , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) _UpperCAmelCase = DDPMScheduler( num_train_timesteps=1000 , beta_schedule="""squaredcos_cap_v2""" , beta_start=0.0_001 , beta_end=0.02 , thresholding=__lowerCAmelCase , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type="""epsilon""" , variance_type="""learned_range""" , ) torch.manual_seed(0 ) _UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def lowerCAmelCase_ ( self : int ): torch.manual_seed(0 ) _UpperCAmelCase = TaEncoderModel.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) _UpperCAmelCase = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) _UpperCAmelCase = UNetaDConditionModel( sample_size=32 , layers_per_block=[1, 2] , block_out_channels=[32, 64] , down_block_types=[ """ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D""", ] , mid_block_type="""UNetMidBlock2DSimpleCrossAttn""" , up_block_types=["""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""] , in_channels=6 , out_channels=6 , cross_attention_dim=32 , encoder_hid_dim=32 , attention_head_dim=8 , addition_embed_type="""text""" , addition_embed_type_num_heads=2 , cross_attention_norm="""group_norm""" , resnet_time_scale_shift="""scale_shift""" , act_fn="""gelu""" , class_embed_type="""timestep""" , mid_block_scale_factor=1.414 , time_embedding_act_fn="""gelu""" , time_embedding_dim=32 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) _UpperCAmelCase = DDPMScheduler( num_train_timesteps=1000 , beta_schedule="""squaredcos_cap_v2""" , beta_start=0.0_001 , beta_end=0.02 , thresholding=__lowerCAmelCase , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type="""epsilon""" , variance_type="""learned_range""" , ) torch.manual_seed(0 ) _UpperCAmelCase = DDPMScheduler( num_train_timesteps=1000 , beta_schedule="""squaredcos_cap_v2""" , beta_start=0.0_001 , beta_end=0.02 , ) torch.manual_seed(0 ) _UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = self.get_dummy_components() _UpperCAmelCase = self.pipeline_class(**__lowerCAmelCase ) pipe.to(__lowerCAmelCase ) pipe.set_progress_bar_config(disable=__lowerCAmelCase ) _UpperCAmelCase = self.get_dummy_inputs(__lowerCAmelCase ) _UpperCAmelCase = inputs["""prompt"""] _UpperCAmelCase = inputs["""generator"""] _UpperCAmelCase = inputs["""num_inference_steps"""] _UpperCAmelCase = inputs["""output_type"""] if "image" in inputs: _UpperCAmelCase = inputs["""image"""] else: _UpperCAmelCase = None if "mask_image" in inputs: _UpperCAmelCase = inputs["""mask_image"""] else: _UpperCAmelCase = None if "original_image" in inputs: _UpperCAmelCase = inputs["""original_image"""] else: _UpperCAmelCase = None _UpperCAmelCase , _UpperCAmelCase = pipe.encode_prompt(__lowerCAmelCase ) # inputs with prompt converted to embeddings _UpperCAmelCase = { """prompt_embeds""": prompt_embeds, """negative_prompt_embeds""": negative_prompt_embeds, """generator""": generator, """num_inference_steps""": num_inference_steps, """output_type""": output_type, } if image is not None: _UpperCAmelCase = image if mask_image is not None: _UpperCAmelCase = mask_image if original_image is not None: _UpperCAmelCase = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = pipe(**__lowerCAmelCase )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(__lowerCAmelCase ) _UpperCAmelCase = self.pipeline_class.from_pretrained(__lowerCAmelCase ) pipe_loaded.to(__lowerCAmelCase ) pipe_loaded.set_progress_bar_config(disable=__lowerCAmelCase ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(__lowerCAmelCase , __lowerCAmelCase ) is None , f'''`{optional_component}` did not stay set to None after loading.''' , ) _UpperCAmelCase = self.get_dummy_inputs(__lowerCAmelCase ) _UpperCAmelCase = inputs["""generator"""] _UpperCAmelCase = inputs["""num_inference_steps"""] _UpperCAmelCase = inputs["""output_type"""] # inputs with prompt converted to embeddings _UpperCAmelCase = { """prompt_embeds""": prompt_embeds, """negative_prompt_embeds""": negative_prompt_embeds, """generator""": generator, """num_inference_steps""": num_inference_steps, """output_type""": output_type, } if image is not None: _UpperCAmelCase = image if mask_image is not None: _UpperCAmelCase = mask_image if original_image is not None: _UpperCAmelCase = original_image _UpperCAmelCase = pipe_loaded(**__lowerCAmelCase )[0] _UpperCAmelCase = np.abs(to_np(__lowerCAmelCase ) - to_np(__lowerCAmelCase ) ).max() self.assertLess(__lowerCAmelCase , 1e-4 ) def lowerCAmelCase_ ( self : Any ): _UpperCAmelCase = self.get_dummy_components() _UpperCAmelCase = self.pipeline_class(**__lowerCAmelCase ) pipe.to(__lowerCAmelCase ) pipe.set_progress_bar_config(disable=__lowerCAmelCase ) _UpperCAmelCase = self.get_dummy_inputs(__lowerCAmelCase ) _UpperCAmelCase = pipe(**__lowerCAmelCase )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(__lowerCAmelCase ) _UpperCAmelCase = self.pipeline_class.from_pretrained(__lowerCAmelCase ) pipe_loaded.to(__lowerCAmelCase ) pipe_loaded.set_progress_bar_config(disable=__lowerCAmelCase ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests _UpperCAmelCase = self.get_dummy_inputs(__lowerCAmelCase ) _UpperCAmelCase = pipe_loaded(**__lowerCAmelCase )[0] _UpperCAmelCase = np.abs(to_np(__lowerCAmelCase ) - to_np(__lowerCAmelCase ) ).max() self.assertLess(__lowerCAmelCase , 1e-4 )
365
"""simple docstring""" import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType UpperCAmelCase__ = logging.get_logger(__name__) class a ( lowerCAmelCase_ ): _snake_case : List[Any] = 'vision-encoder-decoder' _snake_case : Optional[int] = True def __init__( self : int , **__lowerCAmelCase : Any ): super().__init__(**__lowerCAmelCase ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( f'''A configuraton of type {self.model_type} cannot be instantiated because ''' f'''not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}''' ) _UpperCAmelCase = kwargs.pop("""encoder""" ) _UpperCAmelCase = encoder_config.pop("""model_type""" ) _UpperCAmelCase = kwargs.pop("""decoder""" ) _UpperCAmelCase = decoder_config.pop("""model_type""" ) _UpperCAmelCase = AutoConfig.for_model(__lowerCAmelCase , **__lowerCAmelCase ) _UpperCAmelCase = AutoConfig.for_model(__lowerCAmelCase , **__lowerCAmelCase ) _UpperCAmelCase = True @classmethod def lowerCAmelCase_ ( cls : int , __lowerCAmelCase : PretrainedConfig , __lowerCAmelCase : PretrainedConfig , **__lowerCAmelCase : str ): logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) _UpperCAmelCase = True _UpperCAmelCase = True return cls(encoder=encoder_config.to_dict() , decoder=decoder_config.to_dict() , **__lowerCAmelCase ) def lowerCAmelCase_ ( self : int ): _UpperCAmelCase = copy.deepcopy(self.__dict__ ) _UpperCAmelCase = self.encoder.to_dict() _UpperCAmelCase = self.decoder.to_dict() _UpperCAmelCase = self.__class__.model_type return output class a ( lowerCAmelCase_ ): _snake_case : Union[str, Any] = version.parse('1.11' ) @property def lowerCAmelCase_ ( self : int ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def lowerCAmelCase_ ( self : Tuple ): return 1e-4 @property def lowerCAmelCase_ ( self : Dict ): return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class a ( lowerCAmelCase_ ): @property def lowerCAmelCase_ ( self : Any ): _UpperCAmelCase = OrderedDict() _UpperCAmelCase = {0: """batch""", 1: """past_decoder_sequence + sequence"""} _UpperCAmelCase = {0: """batch""", 1: """past_decoder_sequence + sequence"""} _UpperCAmelCase = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def lowerCAmelCase_ ( self : Union[str, Any] , __lowerCAmelCase : "PreTrainedTokenizerBase" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , ): import torch _UpperCAmelCase = OrderedDict() _UpperCAmelCase = super().generate_dummy_inputs( __lowerCAmelCase , batch_size=__lowerCAmelCase , seq_length=__lowerCAmelCase , is_pair=__lowerCAmelCase , framework=__lowerCAmelCase ) _UpperCAmelCase , _UpperCAmelCase = dummy_input["""input_ids"""].shape _UpperCAmelCase = (batch, encoder_sequence, self._config.encoder_hidden_size) _UpperCAmelCase = dummy_input.pop("""input_ids""" ) _UpperCAmelCase = dummy_input.pop("""attention_mask""" ) _UpperCAmelCase = torch.zeros(__lowerCAmelCase ) return common_inputs class a ( lowerCAmelCase_ ): @property def lowerCAmelCase_ ( self : Tuple ): pass def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : PretrainedConfig ): return VisionEncoderDecoderEncoderOnnxConfig(__lowerCAmelCase ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : PretrainedConfig , __lowerCAmelCase : PretrainedConfig , __lowerCAmelCase : str = "default" ): _UpperCAmelCase = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(__lowerCAmelCase , __lowerCAmelCase )
30
0
"""simple docstring""" def __UpperCAmelCase ( lowercase ): """simple docstring""" for i in range(len(lowercase ) - 1 ,0 ,-1 ): _UpperCAmelCase = False for j in range(lowercase ,0 ,-1 ): if unsorted[j] < unsorted[j - 1]: _UpperCAmelCase , _UpperCAmelCase = unsorted[j - 1], unsorted[j] _UpperCAmelCase = True for j in range(lowercase ): if unsorted[j] > unsorted[j + 1]: _UpperCAmelCase , _UpperCAmelCase = unsorted[j + 1], unsorted[j] _UpperCAmelCase = True if not swapped: break return unsorted 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(F'''{cocktail_shaker_sort(unsorted) = }''')
366
"""simple docstring""" import argparse from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument( """--txt2img_unclip""", default="""kakaobrain/karlo-v1-alpha""", type=str, required=False, help="""The pretrained txt2img unclip.""", ) UpperCAmelCase__ = parser.parse_args() UpperCAmelCase__ = UnCLIPPipeline.from_pretrained(args.txtaimg_unclip) UpperCAmelCase__ = CLIPImageProcessor() UpperCAmelCase__ = CLIPVisionModelWithProjection.from_pretrained("""openai/clip-vit-large-patch14""") UpperCAmelCase__ = UnCLIPImageVariationPipeline( decoder=txtaimg.decoder, text_encoder=txtaimg.text_encoder, tokenizer=txtaimg.tokenizer, text_proj=txtaimg.text_proj, feature_extractor=feature_extractor, image_encoder=image_encoder, super_res_first=txtaimg.super_res_first, super_res_last=txtaimg.super_res_last, decoder_scheduler=txtaimg.decoder_scheduler, super_res_scheduler=txtaimg.super_res_scheduler, ) imgaimg.save_pretrained(args.dump_path)
30
0
"""simple docstring""" import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { """facebook/encodec_24khz""": """https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json""", """facebook/encodec_48khz""": """https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json""", } class a ( lowerCAmelCase_ ): _snake_case : int = 'encodec' def __init__( self : int , __lowerCAmelCase : str=[1.5, 3.0, 6.0, 12.0, 24.0] , __lowerCAmelCase : Union[str, Any]=2_4000 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : List[str]=False , __lowerCAmelCase : Optional[int]=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : int=128 , __lowerCAmelCase : List[str]=32 , __lowerCAmelCase : int=1 , __lowerCAmelCase : List[Any]=[8, 5, 4, 2] , __lowerCAmelCase : List[str]="weight_norm" , __lowerCAmelCase : str=7 , __lowerCAmelCase : Optional[Any]=7 , __lowerCAmelCase : Union[str, Any]=3 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : str=True , __lowerCAmelCase : List[str]="reflect" , __lowerCAmelCase : int=2 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Optional[Any]=1.0 , __lowerCAmelCase : List[str]=1024 , __lowerCAmelCase : str=None , __lowerCAmelCase : Union[str, Any]=True , **__lowerCAmelCase : Optional[Any] , ): _UpperCAmelCase = target_bandwidths _UpperCAmelCase = sampling_rate _UpperCAmelCase = audio_channels _UpperCAmelCase = normalize _UpperCAmelCase = chunk_length_s _UpperCAmelCase = overlap _UpperCAmelCase = hidden_size _UpperCAmelCase = num_filters _UpperCAmelCase = num_residual_layers _UpperCAmelCase = upsampling_ratios _UpperCAmelCase = norm_type _UpperCAmelCase = kernel_size _UpperCAmelCase = last_kernel_size _UpperCAmelCase = residual_kernel_size _UpperCAmelCase = dilation_growth_rate _UpperCAmelCase = use_causal_conv _UpperCAmelCase = pad_mode _UpperCAmelCase = compress _UpperCAmelCase = num_lstm_layers _UpperCAmelCase = trim_right_ratio _UpperCAmelCase = codebook_size _UpperCAmelCase = codebook_dim if codebook_dim is not None else hidden_size _UpperCAmelCase = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f'''self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}''' ) super().__init__(**__lowerCAmelCase ) @property def lowerCAmelCase_ ( self : Optional[int] ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def lowerCAmelCase_ ( self : Tuple ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def lowerCAmelCase_ ( self : List[str] ): return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
367
"""simple docstring""" import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def __UpperCAmelCase ( *lowercase ): """simple docstring""" if not isinstance(lowercase ,lowercase ): _UpperCAmelCase = list(lowercase ) for i in range(len(lowercase ) ): _UpperCAmelCase = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [ """CUDA out of memory.""", # CUDA OOM """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""", # CUDNN SNAFU """DefaultCPUAllocator: can't allocate memory""", # CPU OOM ] if isinstance(lowercase ,lowercase ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def __UpperCAmelCase ( lowercase = None ,lowercase = 1_28 ): """simple docstring""" if function is None: return functools.partial(lowercase ,starting_batch_size=lowercase ) _UpperCAmelCase = starting_batch_size def decorator(*lowercase ,**lowercase ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() _UpperCAmelCase = list(inspect.signature(lowercase ).parameters.keys() ) # Guard against user error if len(lowercase ) < (len(lowercase ) + 1): _UpperCAmelCase = """, """.join([f'''{arg}={value}''' for arg, value in zip(params[1:] ,args[1:] )] ) raise TypeError( f'''Batch size was passed into `{function.__name__}` as the first argument when called.''' f'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError("""No executable batch size found, reached zero.""" ) try: return function(lowercase ,*lowercase ,**lowercase ) except Exception as e: if should_reduce_batch_size(lowercase ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
30
0
"""simple docstring""" import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel UpperCAmelCase__ = """0.12""" # assumed parallelism: 8 @require_flax @is_staging_test class a ( unittest.TestCase ): @classmethod def lowerCAmelCase_ ( cls : str ): _UpperCAmelCase = TOKEN HfFolder.save_token(__lowerCAmelCase ) @classmethod def lowerCAmelCase_ ( cls : int ): try: delete_repo(token=cls._token , repo_id="""test-model-flax""" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="""valid_org/test-model-flax-org""" ) except HTTPError: pass def lowerCAmelCase_ ( self : int ): _UpperCAmelCase = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) _UpperCAmelCase = FlaxBertModel(__lowerCAmelCase ) model.push_to_hub("""test-model-flax""" , use_auth_token=self._token ) _UpperCAmelCase = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) _UpperCAmelCase = flatten_dict(unfreeze(model.params ) ) _UpperCAmelCase = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): _UpperCAmelCase = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__lowerCAmelCase , 1e-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="""test-model-flax""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(__lowerCAmelCase , repo_id="""test-model-flax""" , push_to_hub=__lowerCAmelCase , use_auth_token=self._token ) _UpperCAmelCase = FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax''' ) _UpperCAmelCase = flatten_dict(unfreeze(model.params ) ) _UpperCAmelCase = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): _UpperCAmelCase = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__lowerCAmelCase , 1e-3 , msg=f'''{key} not identical''' ) def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) _UpperCAmelCase = FlaxBertModel(__lowerCAmelCase ) model.push_to_hub("""valid_org/test-model-flax-org""" , use_auth_token=self._token ) _UpperCAmelCase = FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org""" ) _UpperCAmelCase = flatten_dict(unfreeze(model.params ) ) _UpperCAmelCase = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): _UpperCAmelCase = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__lowerCAmelCase , 1e-3 , msg=f'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="""valid_org/test-model-flax-org""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( __lowerCAmelCase , repo_id="""valid_org/test-model-flax-org""" , push_to_hub=__lowerCAmelCase , use_auth_token=self._token ) _UpperCAmelCase = FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org""" ) _UpperCAmelCase = flatten_dict(unfreeze(model.params ) ) _UpperCAmelCase = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): _UpperCAmelCase = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__lowerCAmelCase , 1e-3 , msg=f'''{key} not identical''' ) def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = True _UpperCAmelCase = flatten_dict(modela.params ) _UpperCAmelCase = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: _UpperCAmelCase = False return models_are_equal @require_flax class a ( unittest.TestCase ): def lowerCAmelCase_ ( self : Dict ): _UpperCAmelCase = BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only""" ) _UpperCAmelCase = FlaxBertModel(__lowerCAmelCase ) _UpperCAmelCase = """bert""" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__lowerCAmelCase , __lowerCAmelCase ) ) with self.assertRaises(__lowerCAmelCase ): _UpperCAmelCase = FlaxBertModel.from_pretrained(__lowerCAmelCase ) _UpperCAmelCase = FlaxBertModel.from_pretrained(__lowerCAmelCase , subfolder=__lowerCAmelCase ) self.assertTrue(check_models_equal(__lowerCAmelCase , __lowerCAmelCase ) ) def lowerCAmelCase_ ( self : Optional[Any] ): _UpperCAmelCase = BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only""" ) _UpperCAmelCase = FlaxBertModel(__lowerCAmelCase ) _UpperCAmelCase = """bert""" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__lowerCAmelCase , __lowerCAmelCase ) , max_shard_size="""10KB""" ) with self.assertRaises(__lowerCAmelCase ): _UpperCAmelCase = FlaxBertModel.from_pretrained(__lowerCAmelCase ) _UpperCAmelCase = FlaxBertModel.from_pretrained(__lowerCAmelCase , subfolder=__lowerCAmelCase ) self.assertTrue(check_models_equal(__lowerCAmelCase , __lowerCAmelCase ) ) def lowerCAmelCase_ ( self : int ): _UpperCAmelCase = """bert""" _UpperCAmelCase = """hf-internal-testing/tiny-random-bert-subfolder""" with self.assertRaises(__lowerCAmelCase ): _UpperCAmelCase = FlaxBertModel.from_pretrained(__lowerCAmelCase ) _UpperCAmelCase = FlaxBertModel.from_pretrained(__lowerCAmelCase , subfolder=__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) def lowerCAmelCase_ ( self : List[str] ): _UpperCAmelCase = """bert""" _UpperCAmelCase = """hf-internal-testing/tiny-random-bert-sharded-subfolder""" with self.assertRaises(__lowerCAmelCase ): _UpperCAmelCase = FlaxBertModel.from_pretrained(__lowerCAmelCase ) _UpperCAmelCase = FlaxBertModel.from_pretrained(__lowerCAmelCase , subfolder=__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase )
368
"""simple docstring""" from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class a ( lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase ): _snake_case : str = ( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) _snake_case : Dict = ( { 'feature-extraction': TFMobileBertModel, 'fill-mask': TFMobileBertForMaskedLM, 'question-answering': TFMobileBertForQuestionAnswering, 'text-classification': TFMobileBertForSequenceClassification, 'token-classification': TFMobileBertForTokenClassification, 'zero-shot': TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) _snake_case : Dict = False _snake_case : List[str] = False def lowerCAmelCase_ ( self : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : List[Any] , __lowerCAmelCase : int=False ): _UpperCAmelCase = super()._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase , return_labels=__lowerCAmelCase ) if return_labels: if model_class in get_values(__lowerCAmelCase ): _UpperCAmelCase = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) return inputs_dict class a ( lowerCAmelCase_ ): def __init__( self : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str=13 , __lowerCAmelCase : Any=7 , __lowerCAmelCase : Optional[int]=True , __lowerCAmelCase : str=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : List[str]=99 , __lowerCAmelCase : Optional[int]=32 , __lowerCAmelCase : str=32 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : int=4 , __lowerCAmelCase : Tuple=37 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : int=0.1 , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : int=512 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : Optional[int]=0.02 , __lowerCAmelCase : Optional[int]=3 , __lowerCAmelCase : Dict=4 , __lowerCAmelCase : str=None , ): _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 = embedding_size def lowerCAmelCase_ ( self : int ): _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _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 = MobileBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , embedding_size=self.embedding_size , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Tuple , __lowerCAmelCase : Any ): _UpperCAmelCase = TFMobileBertModel(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) _UpperCAmelCase = [input_ids, input_mask] _UpperCAmelCase = model(__lowerCAmelCase ) _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def lowerCAmelCase_ ( self : Any , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : List[str] , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str] ): _UpperCAmelCase = TFMobileBertForMaskedLM(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : int , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : Dict ): _UpperCAmelCase = TFMobileBertForNextSentencePrediction(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def lowerCAmelCase_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple ): _UpperCAmelCase = TFMobileBertForPreTraining(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual( result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def lowerCAmelCase_ ( self : int , __lowerCAmelCase : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : Any , __lowerCAmelCase : str , __lowerCAmelCase : Any , __lowerCAmelCase : int , __lowerCAmelCase : Tuple ): _UpperCAmelCase = self.num_labels _UpperCAmelCase = TFMobileBertForSequenceClassification(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : Any , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Union[str, Any] ): _UpperCAmelCase = self.num_choices _UpperCAmelCase = TFMobileBertForMultipleChoice(config=__lowerCAmelCase ) _UpperCAmelCase = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Any ): _UpperCAmelCase = self.num_labels _UpperCAmelCase = TFMobileBertForTokenClassification(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase_ ( self : Dict , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int , __lowerCAmelCase : Any , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] ): _UpperCAmelCase = TFMobileBertForQuestionAnswering(config=__lowerCAmelCase ) _UpperCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase = model(__lowerCAmelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCAmelCase_ ( self : Tuple ): _UpperCAmelCase = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) = config_and_inputs _UpperCAmelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = TFMobileBertModelTest.TFMobileBertModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=__lowerCAmelCase , hidden_size=37 ) def lowerCAmelCase_ ( self : Any ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : Any ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Any ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__lowerCAmelCase ) def lowerCAmelCase_ ( self : str ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*__lowerCAmelCase ) @slow def lowerCAmelCase_ ( self : int ): # for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["google/mobilebert-uncased"]: _UpperCAmelCase = TFMobileBertModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) @require_tf class a ( unittest.TestCase ): @slow def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased""" ) _UpperCAmelCase = tf.constant([[0, 1, 2, 3, 4, 5]] ) _UpperCAmelCase = model(__lowerCAmelCase )[0] _UpperCAmelCase = [1, 6, 3_0522] self.assertEqual(output.shape , __lowerCAmelCase ) _UpperCAmelCase = tf.constant( [ [ [-4.5_919_547, -9.248_295, -9.645_256], [-6.7_306_175, -6.440_284, -6.6_052_837], [-7.2_743_506, -6.7_847_915, -6.024_673], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __lowerCAmelCase , atol=1e-4 )
30
0
"""simple docstring""" import torch import torch.nn as nn from transformers.modeling_utils import ModuleUtilsMixin from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class a ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): @register_to_config def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : float , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : str , __lowerCAmelCase : bool = False , ): super().__init__() _UpperCAmelCase = nn.Embedding(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = nn.Embedding(__lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = False _UpperCAmelCase = nn.Dropout(p=__lowerCAmelCase ) _UpperCAmelCase = TaConfig( vocab_size=__lowerCAmelCase , d_model=__lowerCAmelCase , num_heads=__lowerCAmelCase , d_kv=__lowerCAmelCase , d_ff=__lowerCAmelCase , dropout_rate=__lowerCAmelCase , feed_forward_proj=__lowerCAmelCase , is_decoder=__lowerCAmelCase , is_encoder_decoder=__lowerCAmelCase , ) _UpperCAmelCase = nn.ModuleList() for lyr_num in range(__lowerCAmelCase ): _UpperCAmelCase = TaBlock(__lowerCAmelCase ) self.encoders.append(__lowerCAmelCase ) _UpperCAmelCase = TaLayerNorm(__lowerCAmelCase ) _UpperCAmelCase = nn.Dropout(p=__lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : List[str] ): _UpperCAmelCase = self.token_embedder(__lowerCAmelCase ) _UpperCAmelCase = encoder_input_tokens.shape[1] _UpperCAmelCase = torch.arange(__lowerCAmelCase , device=encoder_input_tokens.device ) x += self.position_encoding(__lowerCAmelCase ) _UpperCAmelCase = self.dropout_pre(__lowerCAmelCase ) # inverted the attention mask _UpperCAmelCase = encoder_input_tokens.size() _UpperCAmelCase = self.get_extended_attention_mask(__lowerCAmelCase , __lowerCAmelCase ) for lyr in self.encoders: _UpperCAmelCase = lyr(__lowerCAmelCase , __lowerCAmelCase )[0] _UpperCAmelCase = self.layer_norm(__lowerCAmelCase ) return self.dropout_post(__lowerCAmelCase ), encoder_inputs_mask
369
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { """Visual-Attention-Network/van-base""": ( """https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json""" ), } class a ( lowerCAmelCase_ ): _snake_case : int = 'van' def __init__( self : Any , __lowerCAmelCase : Tuple=224 , __lowerCAmelCase : List[Any]=3 , __lowerCAmelCase : Tuple=[7, 3, 3, 3] , __lowerCAmelCase : Dict=[4, 2, 2, 2] , __lowerCAmelCase : Optional[Any]=[64, 128, 320, 512] , __lowerCAmelCase : Optional[int]=[3, 3, 12, 3] , __lowerCAmelCase : Dict=[8, 8, 4, 4] , __lowerCAmelCase : int="gelu" , __lowerCAmelCase : Optional[int]=0.02 , __lowerCAmelCase : List[str]=1e-6 , __lowerCAmelCase : Optional[int]=1e-2 , __lowerCAmelCase : Any=0.0 , __lowerCAmelCase : List[str]=0.0 , **__lowerCAmelCase : Any , ): super().__init__(**__lowerCAmelCase ) _UpperCAmelCase = image_size _UpperCAmelCase = num_channels _UpperCAmelCase = patch_sizes _UpperCAmelCase = strides _UpperCAmelCase = hidden_sizes _UpperCAmelCase = depths _UpperCAmelCase = mlp_ratios _UpperCAmelCase = hidden_act _UpperCAmelCase = initializer_range _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = layer_scale_init_value _UpperCAmelCase = drop_path_rate _UpperCAmelCase = dropout_rate
30
0
"""simple docstring""" UpperCAmelCase__ = { """joule""": 1.0, """kilojoule""": 1_0_0_0, """megajoule""": 1_0_0_0_0_0_0, """gigajoule""": 1_0_0_0_0_0_0_0_0_0, """wattsecond""": 1.0, """watthour""": 3_6_0_0, """kilowatthour""": 3_6_0_0_0_0_0, """newtonmeter""": 1.0, """calorie_nutr""": 4_1_8_6.8, """kilocalorie_nutr""": 4_1_8_6_8_0_0.0_0, """electronvolt""": 1.6_0217_6634E-19, """britishthermalunit_it""": 1_0_5_5.0_5_5_8_5, """footpound""": 1.35_5818, } def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ): """simple docstring""" if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: _UpperCAmelCase = ( f'''Incorrect \'from_type\' or \'to_type\' value: {from_type!r}, {to_type!r}\n''' f'''Valid values are: {", ".join(lowercase )}''' ) raise ValueError(lowercase ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
370
"""simple docstring""" def __UpperCAmelCase ( lowercase = 10_00 ): """simple docstring""" _UpperCAmelCase = 2**power _UpperCAmelCase = 0 while n: _UpperCAmelCase , _UpperCAmelCase = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
30
0
"""simple docstring""" from ...utils import logging from ..ta.modeling_tf_ta import TFTaEncoderModel, TFTaForConditionalGeneration, TFTaModel from .configuration_mta import MTaConfig UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = """T5Config""" class a ( lowerCAmelCase_ ): _snake_case : Optional[int] = 'mt5' _snake_case : Any = MTaConfig class a ( lowerCAmelCase_ ): _snake_case : Optional[int] = 'mt5' _snake_case : int = MTaConfig class a ( lowerCAmelCase_ ): _snake_case : Tuple = 'mt5' _snake_case : Union[str, Any] = MTaConfig
371
"""simple docstring""" from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { """linear""": get_linear_schedule_with_warmup, """cosine""": get_cosine_schedule_with_warmup, """cosine_w_restarts""": get_cosine_with_hard_restarts_schedule_with_warmup, """polynomial""": get_polynomial_decay_schedule_with_warmup, """constant""": get_constant_schedule, """constant_w_warmup""": get_constant_schedule_with_warmup, } class a ( lowerCAmelCase_ ): def __init__( self : Optional[int] , __lowerCAmelCase : Any=None , __lowerCAmelCase : Any=None , *__lowerCAmelCase : Union[str, Any] , **__lowerCAmelCase : Optional[int] ): super().__init__(*__lowerCAmelCase , **__lowerCAmelCase ) if config is None: assert isinstance(self.model , __lowerCAmelCase ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f''' {self.model.__class__}''' ) _UpperCAmelCase = self.model.config else: _UpperCAmelCase = config _UpperCAmelCase = data_args _UpperCAmelCase = self.config.tgt_vocab_size if isinstance(self.config , __lowerCAmelCase ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' """ padding..""" ) if self.args.label_smoothing == 0: _UpperCAmelCase = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _UpperCAmelCase = label_smoothed_nll_loss def lowerCAmelCase_ ( self : Dict , __lowerCAmelCase : int ): if self.optimizer is None: _UpperCAmelCase = ["""bias""", """LayerNorm.weight"""] _UpperCAmelCase = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _UpperCAmelCase = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _UpperCAmelCase = Adafactor _UpperCAmelCase = {"""scale_parameter""": False, """relative_step""": False} else: _UpperCAmelCase = AdamW _UpperCAmelCase = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _UpperCAmelCase = self.args.learning_rate if self.sharded_ddp: _UpperCAmelCase = OSS( params=__lowerCAmelCase , optim=__lowerCAmelCase , **__lowerCAmelCase , ) else: _UpperCAmelCase = optimizer_cls(__lowerCAmelCase , **__lowerCAmelCase ) if self.lr_scheduler is None: _UpperCAmelCase = self._get_lr_scheduler(__lowerCAmelCase ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : List[str] ): _UpperCAmelCase = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _UpperCAmelCase = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _UpperCAmelCase = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _UpperCAmelCase = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=__lowerCAmelCase ) return scheduler def lowerCAmelCase_ ( self : Optional[int] ): if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def lowerCAmelCase_ ( self : Optional[int] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Tuple ): if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _UpperCAmelCase = model(**__lowerCAmelCase , use_cache=__lowerCAmelCase )[0] _UpperCAmelCase = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _UpperCAmelCase , _UpperCAmelCase = model(**__lowerCAmelCase , labels=__lowerCAmelCase , use_cache=__lowerCAmelCase )[:2] else: # compute label smoothed loss _UpperCAmelCase = model(**__lowerCAmelCase , use_cache=__lowerCAmelCase )[0] _UpperCAmelCase = torch.nn.functional.log_softmax(__lowerCAmelCase , dim=-1 ) _UpperCAmelCase , _UpperCAmelCase = self.loss_fn(__lowerCAmelCase , __lowerCAmelCase , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def lowerCAmelCase_ ( self : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : int ): _UpperCAmelCase = inputs.pop("""labels""" ) _UpperCAmelCase , _UpperCAmelCase = self._compute_loss(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) return loss def lowerCAmelCase_ ( self : List[Any] , __lowerCAmelCase : nn.Module , __lowerCAmelCase : Dict[str, Union[torch.Tensor, Any]] , __lowerCAmelCase : bool , __lowerCAmelCase : Optional[List[str]] = None , ): _UpperCAmelCase = self._prepare_inputs(__lowerCAmelCase ) _UpperCAmelCase = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _UpperCAmelCase = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **__lowerCAmelCase , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase = self._pad_tensors_to_max_len(__lowerCAmelCase , gen_kwargs["""max_length"""] ) _UpperCAmelCase = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _UpperCAmelCase , _UpperCAmelCase = self._compute_loss(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) _UpperCAmelCase = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _UpperCAmelCase = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase = self._pad_tensors_to_max_len(__lowerCAmelCase , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def lowerCAmelCase_ ( self : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[str] ): # If PAD token is not defined at least EOS token has to be defined _UpperCAmelCase = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" f''' padded to `max_length`={max_length}''' ) _UpperCAmelCase = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _UpperCAmelCase = tensor return padded_tensor
30
0
'''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 MobileViTImageProcessor class UpperCAmelCase ( unittest.TestCase): def __init__( self : Optional[Any], a_ : int, a_ : Union[str, Any]=7, a_ : List[Any]=3, a_ : Optional[int]=18, a_ : Union[str, Any]=30, a_ : Any=400, a_ : Union[str, Any]=True, a_ : Any=None, a_ : Union[str, Any]=True, a_ : str=None, a_ : List[Any]=True, ): """simple docstring""" UpperCamelCase__ = size if size is not None else {"shortest_edge": 20} UpperCamelCase__ = crop_size if crop_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__ = do_center_crop UpperCamelCase__ = crop_size UpperCamelCase__ = do_flip_channel_order def lowercase_ ( self : Dict ): """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : str = MobileViTImageProcessor if is_vision_available() else None def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = MobileViTImageProcessingTester(self ) @property def lowercase_ ( self : List[Any] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def lowercase_ ( self : Optional[int] ): """simple docstring""" UpperCamelCase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a_, "do_resize" ) ) self.assertTrue(hasattr(a_, "size" ) ) self.assertTrue(hasattr(a_, "do_center_crop" ) ) self.assertTrue(hasattr(a_, "center_crop" ) ) self.assertTrue(hasattr(a_, "do_flip_channel_order" ) ) def lowercase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size, {"shortest_edge": 20} ) self.assertEqual(image_processor.crop_size, {"height": 18, "width": 18} ) UpperCamelCase__ = 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 lowercase_ ( self : Tuple ): """simple docstring""" pass def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase__ = prepare_image_inputs(self.image_processor_tester, equal_resolution=a_ ) for image in image_inputs: self.assertIsInstance(a_, Image.Image ) # 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ), ) # Test batched UpperCamelCase__ = 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 lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase__ = 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 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ), ) # Test batched UpperCamelCase__ = 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 lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase__ = 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 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ), ) # Test batched UpperCamelCase__ = 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"], ), )
31
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase: str = logging.get_logger(__name__) __lowercase: Optional[Any] = { "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Any = 'gpt_neox_japanese' def __init__( self : List[str], a_ : Union[str, Any]=3_2000, a_ : str=2560, a_ : Dict=32, a_ : Tuple=32, a_ : Union[str, Any]=4, a_ : Union[str, Any]="gelu", a_ : int=1.00, a_ : Dict=1_0000, a_ : Any=2048, a_ : Optional[int]=0.02, a_ : int=1e-5, a_ : int=True, a_ : Optional[int]=3_1996, a_ : List[str]=3_1999, a_ : List[str]=0.1, a_ : Optional[int]=0.0, **a_ : Tuple, ): """simple docstring""" super().__init__(bos_token_id=a_, eos_token_id=a_, **a_ ) UpperCamelCase__ = vocab_size UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = hidden_size UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = intermediate_multiple_size UpperCamelCase__ = hidden_act UpperCamelCase__ = rotary_pct UpperCamelCase__ = rotary_emb_base UpperCamelCase__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = use_cache UpperCamelCase__ = attention_dropout UpperCamelCase__ = hidden_dropout
31
1
'''simple docstring''' from typing import List, Optional, Union import torch from transformers import ( XLMRobertaTokenizer, ) from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) from .text_encoder import MultilingualCLIP __lowercase: Tuple = logging.get_logger(__name__) # pylint: disable=invalid-name __lowercase: List[str] = "\n Examples:\n ```py\n >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline\n >>> import torch\n\n >>> pipe_prior = KandinskyPriorPipeline.from_pretrained(\"kandinsky-community/Kandinsky-2-1-prior\")\n >>> pipe_prior.to(\"cuda\")\n\n >>> prompt = \"red cat, 4k photo\"\n >>> out = pipe_prior(prompt)\n >>> image_emb = out.image_embeds\n >>> negative_image_emb = out.negative_image_embeds\n\n >>> pipe = KandinskyPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-1\")\n >>> pipe.to(\"cuda\")\n\n >>> image = pipe(\n ... prompt,\n ... image_embeds=image_emb,\n ... negative_image_embeds=negative_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=100,\n ... ).images\n\n >>> image[0].save(\"cat.png\")\n ```\n" def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Any , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Any=8 ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = h // scale_factor**2 if h % scale_factor**2 != 0: new_h += 1 UpperCamelCase__ = w // scale_factor**2 if w % scale_factor**2 != 0: new_w += 1 return new_h * scale_factor, new_w * scale_factor class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : List[Any], a_ : MultilingualCLIP, a_ : XLMRobertaTokenizer, a_ : UNetaDConditionModel, a_ : Union[DDIMScheduler, DDPMScheduler], a_ : VQModel, ): """simple docstring""" super().__init__() self.register_modules( text_encoder=a_, tokenizer=a_, unet=a_, scheduler=a_, movq=a_, ) UpperCamelCase__ = 2 ** (len(self.movq.config.block_out_channels ) - 1) def lowercase_ ( self : Tuple, a_ : Optional[int], a_ : Union[str, Any], a_ : Optional[Any], a_ : List[Any], a_ : Any, a_ : str ): """simple docstring""" if latents is None: UpperCamelCase__ = randn_tensor(a_, generator=a_, device=a_, dtype=a_ ) else: if latents.shape != shape: raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {shape}' ) UpperCamelCase__ = latents.to(a_ ) UpperCamelCase__ = latents * scheduler.init_noise_sigma return latents def lowercase_ ( self : int, a_ : Optional[Any], a_ : Union[str, Any], a_ : List[Any], a_ : List[Any], a_ : Tuple=None, ): """simple docstring""" UpperCamelCase__ = len(a_ ) if isinstance(a_, a_ ) else 1 # get prompt text embeddings UpperCamelCase__ = self.tokenizer( a_, padding="max_length", truncation=a_, max_length=77, return_attention_mask=a_, add_special_tokens=a_, return_tensors="pt", ) UpperCamelCase__ = text_inputs.input_ids UpperCamelCase__ = self.tokenizer(a_, padding="longest", return_tensors="pt" ).input_ids if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(a_, a_ ): UpperCamelCase__ = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1] ) logger.warning( "The following part of your input was truncated because CLIP can only handle sequences up to" f' {self.tokenizer.model_max_length} tokens: {removed_text}' ) UpperCamelCase__ = text_input_ids.to(a_ ) UpperCamelCase__ = text_inputs.attention_mask.to(a_ ) UpperCamelCase__ , UpperCamelCase__ = self.text_encoder( input_ids=a_, attention_mask=a_ ) UpperCamelCase__ = prompt_embeds.repeat_interleave(a_, dim=0 ) UpperCamelCase__ = text_encoder_hidden_states.repeat_interleave(a_, dim=0 ) UpperCamelCase__ = text_mask.repeat_interleave(a_, dim=0 ) if do_classifier_free_guidance: UpperCamelCase__ = 42 if negative_prompt is None: UpperCamelCase__ = [""] * batch_size elif type(a_ ) is not type(a_ ): raise TypeError( f'`negative_prompt` should be the same type to `prompt`, but got {type(a_ )} !=' f' {type(a_ )}.' ) elif isinstance(a_, a_ ): UpperCamelCase__ = [negative_prompt] elif batch_size != len(a_ ): raise ValueError( f'`negative_prompt`: {negative_prompt} has batch size {len(a_ )}, but `prompt`:' f' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches' " the batch size of `prompt`." ) else: UpperCamelCase__ = negative_prompt UpperCamelCase__ = self.tokenizer( a_, padding="max_length", max_length=77, truncation=a_, return_attention_mask=a_, add_special_tokens=a_, return_tensors="pt", ) UpperCamelCase__ = uncond_input.input_ids.to(a_ ) UpperCamelCase__ = uncond_input.attention_mask.to(a_ ) UpperCamelCase__ , UpperCamelCase__ = self.text_encoder( input_ids=a_, attention_mask=a_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase__ = negative_prompt_embeds.shape[1] UpperCamelCase__ = negative_prompt_embeds.repeat(1, a_ ) UpperCamelCase__ = negative_prompt_embeds.view(batch_size * num_images_per_prompt, a_ ) UpperCamelCase__ = uncond_text_encoder_hidden_states.shape[1] UpperCamelCase__ = uncond_text_encoder_hidden_states.repeat(1, a_, 1 ) UpperCamelCase__ = uncond_text_encoder_hidden_states.view( batch_size * num_images_per_prompt, a_, -1 ) UpperCamelCase__ = uncond_text_mask.repeat_interleave(a_, dim=0 ) # done duplicates # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCamelCase__ = torch.cat([negative_prompt_embeds, prompt_embeds] ) UpperCamelCase__ = torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states] ) UpperCamelCase__ = torch.cat([uncond_text_mask, text_mask] ) return prompt_embeds, text_encoder_hidden_states, text_mask def lowercase_ ( self : Tuple, a_ : Union[str, Any]=0 ): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) UpperCamelCase__ = torch.device(f'cuda:{gpu_id}' ) UpperCamelCase__ = [ self.unet, self.text_encoder, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(a_, a_ ) def lowercase_ ( self : List[Any], a_ : Tuple=0 ): """simple docstring""" if is_accelerate_available() and is_accelerate_version(">=", "0.17.0.dev0" ): from accelerate import cpu_offload_with_hook else: raise ImportError("`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher." ) UpperCamelCase__ = torch.device(f'cuda:{gpu_id}' ) if self.device.type != "cpu": self.to("cpu", silence_dtype_warnings=a_ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) UpperCamelCase__ = None for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]: UpperCamelCase__ , UpperCamelCase__ = cpu_offload_with_hook(a_, a_, prev_module_hook=a_ ) if self.safety_checker is not None: UpperCamelCase__ , UpperCamelCase__ = cpu_offload_with_hook(self.safety_checker, a_, prev_module_hook=a_ ) # We'll offload the last model manually. UpperCamelCase__ = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def lowercase_ ( self : Tuple ): """simple docstring""" if not hasattr(self.unet, "_hf_hook" ): return self.device for module in self.unet.modules(): if ( hasattr(a_, "_hf_hook" ) and hasattr(module._hf_hook, "execution_device" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(a_ ) def __call__( self : str, a_ : Union[str, List[str]], a_ : Union[torch.FloatTensor, List[torch.FloatTensor]], a_ : Union[torch.FloatTensor, List[torch.FloatTensor]], a_ : Optional[Union[str, List[str]]] = None, a_ : int = 512, a_ : int = 512, a_ : int = 100, a_ : float = 4.0, a_ : int = 1, a_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None, a_ : Optional[torch.FloatTensor] = None, a_ : Optional[str] = "pil", a_ : bool = True, ): """simple docstring""" if isinstance(a_, a_ ): UpperCamelCase__ = 1 elif isinstance(a_, a_ ): UpperCamelCase__ = len(a_ ) else: raise ValueError(f'`prompt` has to be of type `str` or `list` but is {type(a_ )}' ) UpperCamelCase__ = self._execution_device UpperCamelCase__ = batch_size * num_images_per_prompt UpperCamelCase__ = guidance_scale > 1.0 UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._encode_prompt( a_, a_, a_, a_, a_ ) if isinstance(a_, a_ ): UpperCamelCase__ = torch.cat(a_, dim=0 ) if isinstance(a_, a_ ): UpperCamelCase__ = torch.cat(a_, dim=0 ) if do_classifier_free_guidance: UpperCamelCase__ = image_embeds.repeat_interleave(a_, dim=0 ) UpperCamelCase__ = negative_image_embeds.repeat_interleave(a_, dim=0 ) UpperCamelCase__ = torch.cat([negative_image_embeds, image_embeds], dim=0 ).to( dtype=prompt_embeds.dtype, device=a_ ) self.scheduler.set_timesteps(a_, device=a_ ) UpperCamelCase__ = self.scheduler.timesteps UpperCamelCase__ = self.unet.config.in_channels UpperCamelCase__ , UpperCamelCase__ = get_new_h_w(a_, a_, self.movq_scale_factor ) # create initial latent UpperCamelCase__ = self.prepare_latents( (batch_size, num_channels_latents, height, width), text_encoder_hidden_states.dtype, a_, a_, a_, self.scheduler, ) for i, t in enumerate(self.progress_bar(a_ ) ): # expand the latents if we are doing classifier free guidance UpperCamelCase__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents UpperCamelCase__ = {"text_embeds": prompt_embeds, "image_embeds": image_embeds} UpperCamelCase__ = self.unet( sample=a_, timestep=a_, encoder_hidden_states=a_, added_cond_kwargs=a_, return_dict=a_, )[0] if do_classifier_free_guidance: UpperCamelCase__ , UpperCamelCase__ = noise_pred.split(latents.shape[1], dim=1 ) UpperCamelCase__ , UpperCamelCase__ = noise_pred.chunk(2 ) UpperCamelCase__ , UpperCamelCase__ = variance_pred.chunk(2 ) UpperCamelCase__ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) UpperCamelCase__ = torch.cat([noise_pred, variance_pred_text], dim=1 ) if not ( hasattr(self.scheduler.config, "variance_type" ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): UpperCamelCase__ , UpperCamelCase__ = noise_pred.split(latents.shape[1], dim=1 ) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase__ = self.scheduler.step( a_, a_, a_, generator=a_, ).prev_sample # post-processing UpperCamelCase__ = self.movq.decode(a_, force_not_quantize=a_ )["sample"] if output_type not in ["pt", "np", "pil"]: raise ValueError(f'Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}' ) if output_type in ["np", "pil"]: UpperCamelCase__ = image * 0.5 + 0.5 UpperCamelCase__ = image.clamp(0, 1 ) UpperCamelCase__ = image.cpu().permute(0, 2, 3, 1 ).float().numpy() if output_type == "pil": UpperCamelCase__ = self.numpy_to_pil(a_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a_ )
31
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : float , _UpperCamelCase : float ) -> float: '''simple docstring''' if density <= 0: raise ValueError("Impossible fluid density" ) if bulk_modulus <= 0: raise ValueError("Impossible bulk modulus" ) return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' import qiskit def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : int ) -> qiskit.result.counts.Counts: '''simple docstring''' UpperCamelCase__ = qiskit.Aer.get_backend("aer_simulator" ) # Create a Quantum Circuit acting on the q register UpperCamelCase__ = qiskit.QuantumCircuit(_UpperCamelCase , _UpperCamelCase ) # Apply X (NOT) Gate to Qubits 0 & 1 circuit.x(0 ) circuit.x(1 ) # Map the quantum measurement to the classical bits circuit.measure([0, 1] , [0, 1] ) # Execute the circuit on the qasm simulator UpperCamelCase__ = qiskit.execute(_UpperCamelCase , _UpperCamelCase , shots=10_00 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(_UpperCamelCase ) if __name__ == "__main__": __lowercase: List[str] = single_qubit_measure(2, 2) print(F"""Total count for various states are: {counts}""")
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int | float | str , _UpperCamelCase : int | float | str ) -> list[str]: '''simple docstring''' if nth_term == "": return [""] UpperCamelCase__ = int(_UpperCamelCase ) UpperCamelCase__ = int(_UpperCamelCase ) UpperCamelCase__ = [] for temp in range(int(_UpperCamelCase ) ): series.append(F'1 / {pow(temp + 1 , int(_UpperCamelCase ) )}' if series else "1" ) return series if __name__ == "__main__": import doctest doctest.testmod() __lowercase: Dict = int(input("Enter the last number (nth term) of the P-Series")) __lowercase: Optional[int] = int(input("Enter the power for P-Series")) print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") print(p_series(nth_term, power))
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __lowercase: str = { "configuration_falcon": ["FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP", "FalconConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: int = [ "FALCON_PRETRAINED_MODEL_ARCHIVE_LIST", "FalconForCausalLM", "FalconModel", "FalconPreTrainedModel", "FalconForSequenceClassification", "FalconForTokenClassification", "FalconForQuestionAnswering", ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys __lowercase: str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
31
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE__) class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True}) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()}) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel}) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def lowercase_ ( self : str, a_ : Union[str, Any] ): """simple docstring""" if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column], a_ ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) UpperCamelCase__ = copy.deepcopy(self ) UpperCamelCase__ = self.label_schema.copy() UpperCamelCase__ = features[self.label_column] UpperCamelCase__ = label_schema return task_template @property def lowercase_ ( self : Any ): """simple docstring""" return { self.audio_column: "audio", self.label_column: "labels", }
31
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : int ) -> int: '''simple docstring''' UpperCamelCase__ = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): UpperCamelCase__ = n - k # Calculate C(n,k) for i in range(_UpperCamelCase ): result *= n - i result //= i + 1 return result def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return binomial_coefficient(2 * node_count , _UpperCamelCase ) // (node_count + 1) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' if n < 0: raise ValueError("factorial() not defined for negative values" ) UpperCamelCase__ = 1 for i in range(1 , n + 1 ): result *= i return result def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return catalan_number(_UpperCamelCase ) * factorial(_UpperCamelCase ) if __name__ == "__main__": __lowercase: List[str] = int(input("Enter the number of nodes: ").strip() or 0) if node_count <= 0: raise ValueError("We need some nodes to work with.") print( F"""Given {node_count} nodes, there are {binary_tree_count(node_count)} """ F"""binary trees and {catalan_number(node_count)} binary search trees.""" )
31
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import is_speech_available from transformers.testing_utils import require_torch, require_torchaudio from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import SpeechaTextFeatureExtractor __lowercase: str = random.Random() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int]=1.0 , _UpperCamelCase : Dict=None , _UpperCamelCase : List[str]=None ) -> Union[str, Any]: '''simple docstring''' if rng is None: UpperCamelCase__ = global_rng UpperCamelCase__ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class UpperCAmelCase ( unittest.TestCase): def __init__( self : List[Any], a_ : List[str], a_ : Any=7, a_ : Dict=400, a_ : str=2000, a_ : List[Any]=24, a_ : int=24, a_ : int=0.0, a_ : Union[str, Any]=1_6000, a_ : Union[str, Any]=True, a_ : Optional[Any]=True, ): """simple docstring""" UpperCamelCase__ = parent UpperCamelCase__ = batch_size UpperCamelCase__ = min_seq_length UpperCamelCase__ = max_seq_length UpperCamelCase__ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) UpperCamelCase__ = feature_size UpperCamelCase__ = num_mel_bins UpperCamelCase__ = padding_value UpperCamelCase__ = sampling_rate UpperCamelCase__ = return_attention_mask UpperCamelCase__ = do_normalize def lowercase_ ( self : Tuple ): """simple docstring""" return { "feature_size": self.feature_size, "num_mel_bins": self.num_mel_bins, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def lowercase_ ( self : Optional[Any], a_ : Union[str, Any]=False, a_ : Optional[int]=False ): """simple docstring""" def _flatten(a_ : Dict ): return list(itertools.chain(*a_ ) ) if equal_length: UpperCamelCase__ = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size UpperCamelCase__ = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length, self.max_seq_length, self.seq_length_diff ) ] if numpify: UpperCamelCase__ = [np.asarray(a_ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Dict = SpeechaTextFeatureExtractor if is_speech_available() else None def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = SpeechaTextFeatureExtractionTester(self ) def lowercase_ ( self : Optional[int], a_ : Tuple ): """simple docstring""" self.assertTrue(np.all(np.mean(a_, axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(a_, axis=0 ) - 1 ) < 1e-3 ) ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = [np.asarray(a_ ) for speech_input in speech_inputs] # Test feature size UpperCamelCase__ = feature_extractor(a_, padding=a_, return_tensors="np" ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size ) # Test not batched input UpperCamelCase__ = feature_extractor(speech_inputs[0], return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(np_speech_inputs[0], return_tensors="np" ).input_features self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) # Test batched UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features for enc_seq_a, enc_seq_a in zip(a_, a_ ): self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) # Test 2-D numpy arrays are batched. UpperCamelCase__ = [floats_list((1, x) )[0] for x in (800, 800, 800)] UpperCamelCase__ = np.asarray(a_ ) UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features for enc_seq_a, enc_seq_a in zip(a_, a_ ): self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = ["longest", "max_length", "do_not_pad"] UpperCamelCase__ = [None, 16, None] for max_length, padding in zip(a_, a_ ): UpperCamelCase__ = feature_extractor( a_, padding=a_, max_length=a_, return_attention_mask=a_ ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = [np.sum(a_ ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = ["longest", "max_length", "do_not_pad"] UpperCamelCase__ = [None, 16, None] for max_length, padding in zip(a_, a_ ): UpperCamelCase__ = feature_extractor( a_, max_length=a_, padding=a_, return_tensors="np", return_attention_mask=a_ ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = [np.sum(a_ ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="max_length", max_length=4, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1] ) self._check_zero_mean_unit_variance(input_features[2] ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="longest", max_length=4, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 4, 24) ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="longest", max_length=16, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 6, 24) ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" import torch UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = np.random.rand(100, 32 ).astype(np.floataa ) UpperCamelCase__ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: UpperCamelCase__ = feature_extractor.pad([{"input_features": inputs}], return_tensors="np" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) UpperCamelCase__ = feature_extractor.pad([{"input_features": inputs}], return_tensors="pt" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def lowercase_ ( self : List[str], a_ : int ): """simple docstring""" from datasets import load_dataset UpperCamelCase__ = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation" ) # automatic decoding with librispeech UpperCamelCase__ = ds.sort("id" ).select(range(a_ ) )[:num_samples]["audio"] return [x["array"] for x in speech_samples] def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = np.array([ -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241, -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128, -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625, ] ) # fmt: on UpperCamelCase__ = self._load_datasamples(1 ) UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = feature_extractor(a_, return_tensors="pt" ).input_features self.assertEquals(input_features.shape, (1, 584, 24) ) self.assertTrue(np.allclose(input_features[0, 0, :30], a_, atol=1e-4 ) )
31
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__( ) -> int: '''simple docstring''' return 1 def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return 0 if x < 0 else two_pence(x - 2 ) + one_pence() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return 0 if x < 0 else five_pence(x - 5 ) + two_pence(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return 0 if x < 0 else ten_pence(x - 10 ) + five_pence(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return 0 if x < 0 else twenty_pence(x - 20 ) + ten_pence(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return 0 if x < 0 else fifty_pence(x - 50 ) + twenty_pence(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return 0 if x < 0 else one_pound(x - 1_00 ) + fifty_pence(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' return 0 if x < 0 else two_pound(x - 2_00 ) + one_pound(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int = 2_00 ) -> int: '''simple docstring''' return two_pound(_UpperCamelCase ) if __name__ == "__main__": print(solution(int(input().strip())))
31
'''simple docstring''' import logging import numpy as np import pytest from scipy.linalg import eigh logging.basicConfig(level=logging.INFO, format="%(message)s") def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray ) -> np.ndarray: '''simple docstring''' return input_array.reshape((input_array.size, 1) ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' UpperCamelCase__ = np.nan for i in range(_UpperCamelCase ): UpperCamelCase__ = features[:, labels == i] UpperCamelCase__ = data.mean(1 ) # Centralize the data of class i UpperCamelCase__ = data - column_reshape(_UpperCamelCase ) if i > 0: # If covariance_sum is not None covariance_sum += np.dot(_UpperCamelCase , centered_data.T ) else: # If covariance_sum is np.nan (i.e. first loop) UpperCamelCase__ = np.dot(_UpperCamelCase , centered_data.T ) return covariance_sum / features.shape[1] def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' UpperCamelCase__ = features.mean(1 ) UpperCamelCase__ = np.nan for i in range(_UpperCamelCase ): UpperCamelCase__ = features[:, labels == i] UpperCamelCase__ = data.shape[1] UpperCamelCase__ = data.mean(1 ) if i > 0: # If covariance_sum is not None covariance_sum += device_data * np.dot( column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase ) , (column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase )).T , ) else: # If covariance_sum is np.nan (i.e. first loop) UpperCamelCase__ = device_data * np.dot( column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase ) , (column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase )).T , ) return covariance_sum / features.shape[1] def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' if features.any(): UpperCamelCase__ = features.mean(1 ) # Center the dataset UpperCamelCase__ = features - np.reshape(_UpperCamelCase , (data_mean.size, 1) ) UpperCamelCase__ = np.dot(_UpperCamelCase , centered_data.T ) / features.shape[1] UpperCamelCase__ , UpperCamelCase__ = np.linalg.eigh(_UpperCamelCase ) # Take all the columns in the reverse order (-1), and then takes only the first UpperCamelCase__ = eigenvectors[:, ::-1][:, 0:dimensions] # Project the database on the new space UpperCamelCase__ = np.dot(filtered_eigenvectors.T , _UpperCamelCase ) logging.info("Principal Component Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=_UpperCamelCase ) logging.error("Dataset empty" ) raise AssertionError def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' assert classes > dimensions # Check if features have been already loaded if features.any: UpperCamelCase__ , UpperCamelCase__ = eigh( covariance_between_classes(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) , covariance_within_classes(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) , ) UpperCamelCase__ = eigenvectors[:, ::-1][:, :dimensions] UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = np.linalg.svd(_UpperCamelCase ) UpperCamelCase__ = svd_matrix[:, 0:dimensions] UpperCamelCase__ = np.dot(filtered_svd_matrix.T , _UpperCamelCase ) logging.info("Linear Discriminant Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=_UpperCamelCase ) logging.error("Dataset empty" ) raise AssertionError def SCREAMING_SNAKE_CASE__( ) -> None: '''simple docstring''' UpperCamelCase__ = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]] ) UpperCamelCase__ = np.array([0, 0, 0, 1, 1] ) UpperCamelCase__ = 2 UpperCamelCase__ = 2 # Assert that the function raises an AssertionError if dimensions > classes with pytest.raises(_UpperCamelCase ) as error_info: UpperCamelCase__ = linear_discriminant_analysis( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if isinstance(_UpperCamelCase , np.ndarray ): raise AssertionError( "Did not raise AssertionError for dimensions > classes" ) assert error_info.type is AssertionError def SCREAMING_SNAKE_CASE__( ) -> None: '''simple docstring''' UpperCamelCase__ = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) UpperCamelCase__ = 2 UpperCamelCase__ = np.array([[6.9_2_8_2_0_3_2_3, 8.6_6_0_2_5_4_0_4, 1_0.3_9_2_3_0_4_8_5], [3.0, 3.0, 3.0]] ) with pytest.raises(_UpperCamelCase ) as error_info: UpperCamelCase__ = principal_component_analysis(_UpperCamelCase , _UpperCamelCase ) if not np.allclose(_UpperCamelCase , _UpperCamelCase ): raise AssertionError assert error_info.type is AssertionError if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : str = 'bert-generation' def __init__( self : Optional[int], a_ : Any=5_0358, a_ : Dict=1024, a_ : Tuple=24, a_ : Tuple=16, a_ : Union[str, Any]=4096, a_ : Union[str, Any]="gelu", a_ : List[Any]=0.1, a_ : int=0.1, a_ : Tuple=512, a_ : Optional[Any]=0.02, a_ : Union[str, Any]=1e-1_2, a_ : Any=0, a_ : List[Any]=2, a_ : Optional[int]=1, a_ : Tuple="absolute", a_ : List[Any]=True, **a_ : List[str], ): """simple docstring""" super().__init__(pad_token_id=a_, bos_token_id=a_, eos_token_id=a_, **a_ ) UpperCamelCase__ = vocab_size UpperCamelCase__ = hidden_size UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = hidden_act UpperCamelCase__ = intermediate_size UpperCamelCase__ = hidden_dropout_prob UpperCamelCase__ = attention_probs_dropout_prob UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = position_embedding_type UpperCamelCase__ = use_cache
31
'''simple docstring''' import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __lowercase: Optional[List[str]] = None __lowercase: List[Any] = "<" if sys.byteorder == "little" else ">" # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __lowercase: Tuple = [ np.dtype("|b1"), np.dtype("|u1"), np.dtype("<u2"), np.dtype(">u2"), np.dtype("<i2"), np.dtype(">i2"), np.dtype("<u4"), np.dtype(">u4"), np.dtype("<i4"), np.dtype(">i4"), np.dtype("<f4"), np.dtype(">f4"), np.dtype("<f8"), np.dtype(">f8"), ] @dataclass class UpperCAmelCase : _lowerCamelCase : bool = True _lowerCamelCase : Optional[str] = None # Automatically constructed _lowerCamelCase : ClassVar[str] = "PIL.Image.Image" _lowerCamelCase : ClassVar[Any] = pa.struct({'bytes': pa.binary(), 'path': pa.string()}) _lowerCamelCase : str = field(default='Image' , init=SCREAMING_SNAKE_CASE__ , repr=SCREAMING_SNAKE_CASE__) def __call__( self : Union[str, Any] ): """simple docstring""" return self.pa_type def lowercase_ ( self : Optional[Any], a_ : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if isinstance(a_, a_ ): UpperCamelCase__ = np.array(a_ ) if isinstance(a_, a_ ): return {"path": value, "bytes": None} elif isinstance(a_, a_ ): return {"path": None, "bytes": value} elif isinstance(a_, np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(a_ ) elif isinstance(a_, PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(a_ ) elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.' ) def lowercase_ ( self : Dict, a_ : dict, a_ : Dict=None ): """simple docstring""" if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead." ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support decoding images, please install 'Pillow'." ) if token_per_repo_id is None: UpperCamelCase__ = {} UpperCamelCase__ , UpperCamelCase__ = value["path"], value["bytes"] if bytes_ is None: if path is None: raise ValueError(f'An image should have one of \'path\' or \'bytes\' but both are None in {value}.' ) else: if is_local_path(a_ ): UpperCamelCase__ = PIL.Image.open(a_ ) else: UpperCamelCase__ = path.split("::" )[-1] try: UpperCamelCase__ = string_to_dict(a_, config.HUB_DATASETS_URL )["repo_id"] UpperCamelCase__ = token_per_repo_id.get(a_ ) except ValueError: UpperCamelCase__ = None with xopen(a_, "rb", use_auth_token=a_ ) as f: UpperCamelCase__ = BytesIO(f.read() ) UpperCamelCase__ = PIL.Image.open(bytes_ ) else: UpperCamelCase__ = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def lowercase_ ( self : List[str] ): """simple docstring""" from .features import Value return ( self if self.decode else { "bytes": Value("binary" ), "path": Value("string" ), } ) def lowercase_ ( self : List[Any], a_ : Union[pa.StringArray, pa.StructArray, pa.ListArray] ): """simple docstring""" if pa.types.is_string(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: UpperCamelCase__ = storage.field("bytes" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: UpperCamelCase__ = storage.field("path" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCamelCase__ = pa.array( [encode_np_array(np.array(a_ ) )["bytes"] if arr is not None else None for arr in storage.to_pylist()], type=pa.binary(), ) UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays( [bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def lowercase_ ( self : str, a_ : pa.StructArray ): """simple docstring""" @no_op_if_value_is_null def path_to_bytes(a_ : Dict ): with xopen(a_, "rb" ) as f: UpperCamelCase__ = f.read() return bytes_ UpperCamelCase__ = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) UpperCamelCase__ = pa.array( [os.path.basename(a_ ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def SCREAMING_SNAKE_CASE__( ) -> List[str]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() UpperCamelCase__ = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> bytes: '''simple docstring''' UpperCamelCase__ = BytesIO() if image.format in list_image_compression_formats(): UpperCamelCase__ = image.format else: UpperCamelCase__ = "PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF" image.save(_UpperCamelCase , format=_UpperCamelCase ) return buffer.getvalue() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> dict: '''simple docstring''' if hasattr(_UpperCamelCase , "filename" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray ) -> dict: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) UpperCamelCase__ = array.dtype UpperCamelCase__ = dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER UpperCamelCase__ = dtype.kind UpperCamelCase__ = dtype.itemsize UpperCamelCase__ = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCamelCase__ = np.dtype("|u1" ) if dtype_kind not in ["u", "i"]: raise TypeError( F'Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.' ) if dtype is not dest_dtype: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: UpperCamelCase__ = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: UpperCamelCase__ = dtype_byteorder + dtype_kind + str(_UpperCamelCase ) UpperCamelCase__ = np.dtype(_UpperCamelCase ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( F'Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}' ) UpperCamelCase__ = PIL.Image.fromarray(array.astype(_UpperCamelCase ) ) return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]] ) -> List[dict]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if objs: UpperCamelCase__ , UpperCamelCase__ = first_non_null_value(_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_UpperCamelCase , np.ndarray ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] elif isinstance(_UpperCamelCase , PIL.Image.Image ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] else: return objs else: return objs
31
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase: Tuple = logging.get_logger(__name__) __lowercase: Any = { "studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json", "studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json", } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Dict = 'luke' def __init__( self : Any, a_ : Optional[Any]=5_0267, a_ : Optional[int]=50_0000, a_ : Union[str, Any]=768, a_ : List[str]=256, a_ : Optional[int]=12, a_ : Union[str, Any]=12, a_ : Dict=3072, a_ : List[Any]="gelu", a_ : Tuple=0.1, a_ : str=0.1, a_ : Optional[Any]=512, a_ : int=2, a_ : Any=0.02, a_ : int=1e-1_2, a_ : int=True, a_ : List[str]=None, a_ : Optional[int]=1, a_ : Union[str, Any]=0, a_ : str=2, **a_ : Optional[Any], ): """simple docstring""" super().__init__(pad_token_id=a_, bos_token_id=a_, eos_token_id=a_, **a_ ) UpperCamelCase__ = vocab_size UpperCamelCase__ = entity_vocab_size UpperCamelCase__ = hidden_size UpperCamelCase__ = entity_emb_size UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = hidden_act UpperCamelCase__ = intermediate_size UpperCamelCase__ = hidden_dropout_prob UpperCamelCase__ = attention_probs_dropout_prob UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = type_vocab_size UpperCamelCase__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = use_entity_aware_attention UpperCamelCase__ = classifier_dropout
31
'''simple docstring''' import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Union[str, Any] = CLIPTokenizer _lowerCamelCase : Dict = CLIPTokenizerFast _lowerCamelCase : int = True _lowerCamelCase : Tuple = {} _lowerCamelCase : Tuple = False def lowercase_ ( self : Tuple ): """simple docstring""" super().setUp() # fmt: off UpperCamelCase__ = ["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 UpperCamelCase__ = dict(zip(a_, range(len(a_ ) ) ) ) UpperCamelCase__ = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] UpperCamelCase__ = {"unk_token": "<unk>"} UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as fp: fp.write(json.dumps(a_ ) + "\n" ) with open(self.merges_file, "w", encoding="utf-8" ) as fp: fp.write("\n".join(a_ ) ) def lowercase_ ( self : Optional[Any], **a_ : str ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : str, **a_ : str ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : List[Any], a_ : Dict ): """simple docstring""" UpperCamelCase__ = "lower newer" UpperCamelCase__ = "lower newer" return input_text, output_text def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = CLIPTokenizer(self.vocab_file, self.merges_file, **self.special_tokens_map ) UpperCamelCase__ = "lower newer" UpperCamelCase__ = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] UpperCamelCase__ = tokenizer.tokenize(a_ ) self.assertListEqual(a_, a_ ) UpperCamelCase__ = tokens + [tokenizer.unk_token] UpperCamelCase__ = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(a_ ), a_ ) @require_ftfy def lowercase_ ( self : Dict ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCamelCase__ = self.tokenizer_class.from_pretrained(a_, **a_ ) UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained(a_, **a_ ) UpperCamelCase__ = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways UpperCamelCase__ = "xa\u0303y" + " " + "x\xe3y" UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on unicode of space type UpperCamelCase__ = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on unicode of line break type UpperCamelCase__ = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) def lowercase_ ( self : Tuple ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCamelCase__ = "hello" # `hello` is a token in the vocabulary of `pretrained_name` UpperCamelCase__ = f'{text_of_1_token} {text_of_1_token}' UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained( a_, use_fast=a_, ) UpperCamelCase__ = tokenizer_r(a_, return_offsets_mapping=a_, add_special_tokens=a_ ) self.assertEqual(encoding.offset_mapping[0], (0, len(a_ )) ) self.assertEqual( encoding.offset_mapping[1], (len(a_ ) + 1, len(a_ ) + 1 + len(a_ )), ) UpperCamelCase__ = f' {text}' UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained( a_, use_fast=a_, ) UpperCamelCase__ = tokenizer_r(a_, return_offsets_mapping=a_, add_special_tokens=a_ ) self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(a_ )) ) self.assertEqual( encoding.offset_mapping[1], (1 + len(a_ ) + 1, 1 + len(a_ ) + 1 + len(a_ )), ) def lowercase_ ( self : Tuple ): """simple docstring""" with self.assertRaises(a_ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def lowercase_ ( self : Union[str, Any] ): """simple docstring""" super().test_tokenization_python_rust_equals() def lowercase_ ( self : List[str] ): """simple docstring""" pass
31
1
'''simple docstring''' import math import numpy as np import qiskit from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int = 3 ) -> qiskit.result.counts.Counts: '''simple docstring''' if isinstance(_UpperCamelCase , _UpperCamelCase ): raise TypeError("number of qubits must be a integer." ) if number_of_qubits <= 0: raise ValueError("number of qubits must be > 0." ) if math.floor(_UpperCamelCase ) != number_of_qubits: raise ValueError("number of qubits must be exact integer." ) if number_of_qubits > 10: raise ValueError("number of qubits too large to simulate(>10)." ) UpperCamelCase__ = QuantumRegister(_UpperCamelCase , "qr" ) UpperCamelCase__ = ClassicalRegister(_UpperCamelCase , "cr" ) UpperCamelCase__ = QuantumCircuit(_UpperCamelCase , _UpperCamelCase ) UpperCamelCase__ = number_of_qubits for i in range(_UpperCamelCase ): quantum_circuit.h(number_of_qubits - i - 1 ) counter -= 1 for j in range(_UpperCamelCase ): quantum_circuit.cp(np.pi / 2 ** (counter - j) , _UpperCamelCase , _UpperCamelCase ) for k in range(number_of_qubits // 2 ): quantum_circuit.swap(_UpperCamelCase , number_of_qubits - k - 1 ) # measure all the qubits quantum_circuit.measure(_UpperCamelCase , _UpperCamelCase ) # simulate with 10000 shots UpperCamelCase__ = Aer.get_backend("qasm_simulator" ) UpperCamelCase__ = execute(_UpperCamelCase , _UpperCamelCase , shots=1_00_00 ) return job.result().get_counts(_UpperCamelCase ) if __name__ == "__main__": print( F"""Total count for quantum fourier transform state is: \ {quantum_fourier_transform(3)}""" )
31
'''simple docstring''' import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __lowercase: Any = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.14.0", "To fix: pip install -r examples/pytorch/audio-classification/requirements.txt") def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : float , _UpperCamelCase : int = 1_60_00 ) -> str: '''simple docstring''' UpperCamelCase__ = int(round(sample_rate * max_length ) ) if len(_UpperCamelCase ) <= sample_length: return wav UpperCamelCase__ = randint(0 , len(_UpperCamelCase ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class UpperCAmelCase : _lowerCamelCase : Optional[str] = field(default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Name of a dataset from the datasets package'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'A file containing the training audio paths and labels.'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'A file containing the validation audio paths and labels.'}) _lowerCamelCase : str = field( default='train' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) _lowerCamelCase : str = field( default='validation' , metadata={ 'help': ( 'The name of the training data set split to use (via the datasets library). Defaults to \'validation\'' ) } , ) _lowerCamelCase : str = field( default='audio' , metadata={'help': 'The name of the dataset column containing the audio data. Defaults to \'audio\''} , ) _lowerCamelCase : str = field( default='label' , metadata={'help': 'The name of the dataset column containing the labels. Defaults to \'label\''}) _lowerCamelCase : Optional[int] = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) _lowerCamelCase : Optional[int] = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) _lowerCamelCase : float = field( default=20 , metadata={'help': 'Audio clips will be randomly cut to this length during training if the value is set.'} , ) @dataclass class UpperCAmelCase : _lowerCamelCase : str = field( default='facebook/wav2vec2-base' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Pretrained config name or path if not the same as model_name'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from the Hub'}) _lowerCamelCase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Name or path of preprocessor config.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to freeze the feature encoder layers of the model.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to generate an attention mask in the feature extractor.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) _lowerCamelCase : Optional[bool] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def lowercase_ ( self : int ): """simple docstring""" if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( "The argument `--freeze_feature_extractor` is deprecated and " "will be removed in a future version. Use `--freeze_feature_encoder`" "instead. Setting `freeze_feature_encoder==True`.", a_, ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( "The argument `--freeze_feature_extractor` is deprecated and " "should not be used in combination with `--freeze_feature_encoder`." "Only make use of `--freeze_feature_encoder`." ) def SCREAMING_SNAKE_CASE__( ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_audio_classification" , _UpperCamelCase , _UpperCamelCase ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} ' + F'distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}' ) logger.info(F'Training/evaluation parameters {training_args}' ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. UpperCamelCase__ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'Output directory ({training_args.output_dir}) already exists and is not empty. ' "Use --overwrite_output_dir to train from scratch." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Initialize our dataset and prepare it for the audio classification task. UpperCamelCase__ = DatasetDict() UpperCamelCase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--audio_column_name {data_args.audio_column_name} not found in dataset \'{data_args.dataset_name}\'. ' "Make sure to set `--audio_column_name` to the correct audio column - one of " F'{", ".join(raw_datasets["train"].column_names )}.' ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--label_column_name {data_args.label_column_name} not found in dataset \'{data_args.dataset_name}\'. ' "Make sure to set `--label_column_name` to the correct text column - one of " F'{", ".join(raw_datasets["train"].column_names )}.' ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy UpperCamelCase__ = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. UpperCamelCase__ = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) UpperCamelCase__ = feature_extractor.model_input_names[0] def train_transforms(_UpperCamelCase : Any ): UpperCamelCase__ = [] for audio in batch[data_args.audio_column_name]: UpperCamelCase__ = random_subsample( audio["array"] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(_UpperCamelCase ) UpperCamelCase__ = feature_extractor(_UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCamelCase__ = {model_input_name: inputs.get(_UpperCamelCase )} UpperCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(_UpperCamelCase : List[Any] ): UpperCamelCase__ = [audio["array"] for audio in batch[data_args.audio_column_name]] UpperCamelCase__ = feature_extractor(_UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCamelCase__ = {model_input_name: inputs.get(_UpperCamelCase )} UpperCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. UpperCamelCase__ = raw_datasets["train"].features[data_args.label_column_name].names UpperCamelCase__ , UpperCamelCase__ = {}, {} for i, label in enumerate(_UpperCamelCase ): UpperCamelCase__ = str(_UpperCamelCase ) UpperCamelCase__ = label # Load the accuracy metric from the datasets package UpperCamelCase__ = evaluate.load("accuracy" ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Any ): UpperCamelCase__ = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=_UpperCamelCase , references=eval_pred.label_ids ) UpperCamelCase__ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(_UpperCamelCase ) , labelaid=_UpperCamelCase , idalabel=_UpperCamelCase , finetuning_task="audio-classification" , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=_UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: UpperCamelCase__ = ( raw_datasets["train"].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(_UpperCamelCase , output_all_columns=_UpperCamelCase ) if training_args.do_eval: if data_args.max_eval_samples is not None: UpperCamelCase__ = ( raw_datasets["eval"].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(_UpperCamelCase , output_all_columns=_UpperCamelCase ) # Initialize our trainer UpperCamelCase__ = Trainer( model=_UpperCamelCase , args=_UpperCamelCase , train_dataset=raw_datasets["train"] if training_args.do_train else None , eval_dataset=raw_datasets["eval"] if training_args.do_eval else None , compute_metrics=_UpperCamelCase , tokenizer=_UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase__ = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ = last_checkpoint UpperCamelCase__ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics("train" , train_result.metrics ) trainer.save_metrics("train" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ = trainer.evaluate() trainer.log_metrics("eval" , _UpperCamelCase ) trainer.save_metrics("eval" , _UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase__ = { "finetuned_from": model_args.model_name_or_path, "tasks": "audio-classification", "dataset": data_args.dataset_name, "tags": ["audio-classification"], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __lowercase: Tuple = { "configuration_mobilevit": ["MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MobileViTConfig", "MobileViTOnnxConfig"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: int = ["MobileViTFeatureExtractor"] __lowercase: Union[str, Any] = ["MobileViTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Any = [ "MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "MobileViTForImageClassification", "MobileViTForSemanticSegmentation", "MobileViTModel", "MobileViTPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Dict = [ "TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFMobileViTForImageClassification", "TFMobileViTForSemanticSegmentation", "TFMobileViTModel", "TFMobileViTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilevit import MobileViTFeatureExtractor from .image_processing_mobilevit import MobileViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilevit import ( MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel, MobileViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilevit import ( TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel, TFMobileViTPreTrainedModel, ) else: import sys __lowercase: Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
31
'''simple docstring''' import math import sys def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = "" try: with open(_UpperCamelCase , "rb" ) as binary_file: UpperCamelCase__ = binary_file.read() for dat in data: UpperCamelCase__ = F'{dat:08b}' result += curr_byte return result except OSError: print("File not accessible" ) sys.exit() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = {"0": "0", "1": "1"} UpperCamelCase__ , UpperCamelCase__ = "", "" UpperCamelCase__ = len(_UpperCamelCase ) for i in range(len(_UpperCamelCase ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue UpperCamelCase__ = lexicon[curr_string] result += last_match_id UpperCamelCase__ = last_match_id + "0" if math.loga(_UpperCamelCase ).is_integer(): UpperCamelCase__ = {} for curr_key in list(_UpperCamelCase ): UpperCamelCase__ = lexicon.pop(_UpperCamelCase ) UpperCamelCase__ = new_lex UpperCamelCase__ = last_match_id + "1" index += 1 UpperCamelCase__ = "" return result def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str ) -> None: '''simple docstring''' UpperCamelCase__ = 8 try: with open(_UpperCamelCase , "wb" ) as opened_file: UpperCamelCase__ = [ to_write[i : i + byte_length] for i in range(0 , len(_UpperCamelCase ) , _UpperCamelCase ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("10000000" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(_UpperCamelCase , 2 ).to_bytes(1 , byteorder="big" ) ) except OSError: print("File not accessible" ) sys.exit() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = 0 for letter in data_bits: if letter == "1": break counter += 1 UpperCamelCase__ = data_bits[counter:] UpperCamelCase__ = data_bits[counter + 1 :] return data_bits def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str ) -> None: '''simple docstring''' UpperCamelCase__ = read_file_binary(_UpperCamelCase ) UpperCamelCase__ = remove_prefix(_UpperCamelCase ) UpperCamelCase__ = decompress_data(_UpperCamelCase ) write_file_binary(_UpperCamelCase , _UpperCamelCase ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
31
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' return credit_card_number.startswith(("34", "35", "37", "4", "5", "6") ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' UpperCamelCase__ = credit_card_number UpperCamelCase__ = 0 UpperCamelCase__ = len(_UpperCamelCase ) - 2 for i in range(_UpperCamelCase , -1 , -2 ): # double the value of every second digit UpperCamelCase__ = int(cc_number[i] ) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 UpperCamelCase__ = cc_number[:i] + str(_UpperCamelCase ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(_UpperCamelCase ) - 1 , -1 , -2 ): total += int(cc_number[i] ) return total % 10 == 0 def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' UpperCamelCase__ = F'{credit_card_number} is an invalid credit card number because' if not credit_card_number.isdigit(): print(F'{error_message} it has nonnumerical characters.' ) return False if not 13 <= len(_UpperCamelCase ) <= 16: print(F'{error_message} of its length.' ) return False if not validate_initial_digits(_UpperCamelCase ): print(F'{error_message} of its first two digits.' ) return False if not luhn_validation(_UpperCamelCase ): print(F'{error_message} it fails the Luhn check.' ) return False print(F'{credit_card_number} is a valid credit card number.' ) return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
31
'''simple docstring''' import inspect from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel, VQModel from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : Any, a_ : VQModel, a_ : UNetaDModel, a_ : DDIMScheduler ): """simple docstring""" super().__init__() self.register_modules(vqvae=a_, unet=a_, scheduler=a_ ) @torch.no_grad() def __call__( self : Union[str, Any], a_ : int = 1, a_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None, a_ : float = 0.0, a_ : int = 50, a_ : Optional[str] = "pil", a_ : bool = True, **a_ : Tuple, ): """simple docstring""" UpperCamelCase__ = randn_tensor( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size), generator=a_, ) UpperCamelCase__ = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler UpperCamelCase__ = latents * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(a_ ) # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature UpperCamelCase__ = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) UpperCamelCase__ = {} if accepts_eta: UpperCamelCase__ = eta for t in self.progress_bar(self.scheduler.timesteps ): UpperCamelCase__ = self.scheduler.scale_model_input(a_, a_ ) # predict the noise residual UpperCamelCase__ = self.unet(a_, a_ ).sample # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase__ = self.scheduler.step(a_, a_, a_, **a_ ).prev_sample # decode the image latents with the VAE UpperCamelCase__ = self.vqvae.decode(a_ ).sample UpperCamelCase__ = (image / 2 + 0.5).clamp(0, 1 ) UpperCamelCase__ = image.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": UpperCamelCase__ = self.numpy_to_pil(a_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a_ )
31
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor __lowercase: int = logging.get_logger(__name__) class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : List[Any], *a_ : Optional[Any], **a_ : Tuple ): """simple docstring""" warnings.warn( "The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use BeitImageProcessor instead.", a_, ) super().__init__(*a_, **a_ )
31
'''simple docstring''' import argparse import json import subprocess def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : Tuple ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = [] UpperCamelCase__ = ( F'curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer {token}"' " https://api.github.com/repos/huggingface/transformers/actions/runners" ) UpperCamelCase__ = subprocess.run(_UpperCamelCase , shell=_UpperCamelCase , stdout=subprocess.PIPE ) UpperCamelCase__ = output.stdout.decode("utf-8" ) UpperCamelCase__ = json.loads(_UpperCamelCase ) UpperCamelCase__ = status["runners"] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_UpperCamelCase ) # save the result so we can report them on Slack with open("offline_runners.txt" , "w" ) as fp: fp.write(json.dumps(_UpperCamelCase ) ) if len(_UpperCamelCase ) > 0: UpperCamelCase__ = "\n".join([x["name"] for x in offline_runners] ) raise ValueError(F'The following runners are offline:\n{failed}' ) if __name__ == "__main__": def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' return values.split("," ) __lowercase: str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--target_runners", default=None, type=list_str, required=True, help="Comma-separated list of runners to check status.", ) parser.add_argument( "--token", default=None, type=str, required=True, help="A token that has actions:read permission." ) __lowercase: str = parser.parse_args() get_runner_status(args.target_runners, args.token)
31
1
'''simple docstring''' from collections import defaultdict def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> int: '''simple docstring''' UpperCamelCase__ = 1 UpperCamelCase__ = True for v in tree[start]: if v not in visited: ret += dfs(_UpperCamelCase ) if ret % 2 == 0: cuts.append(_UpperCamelCase ) return ret def SCREAMING_SNAKE_CASE__( ) -> List[Any]: '''simple docstring''' dfs(1 ) if __name__ == "__main__": __lowercase ,__lowercase: List[str] = 10, 9 __lowercase: Optional[Any] = defaultdict(list) __lowercase: dict[int, bool] = {} __lowercase: list[int] = [] __lowercase: Dict = 0 __lowercase: Tuple = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
31
'''simple docstring''' from typing import List, Optional, TypeVar from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .info import DatasetInfo from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets from .splits import NamedSplit from .utils import logging from .utils.py_utils import Literal __lowercase: str = logging.get_logger(__name__) __lowercase: Tuple = TypeVar("DatasetType", Dataset, IterableDataset) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[DatasetType] , _UpperCamelCase : Optional[List[float]] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[DatasetInfo] = None , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : Literal["first_exhausted", "all_exhausted"] = "first_exhausted" , ) -> DatasetType: '''simple docstring''' from .arrow_dataset import Dataset from .iterable_dataset import IterableDataset if not datasets: raise ValueError("Unable to interleave an empty list of datasets." ) for i, dataset in enumerate(_UpperCamelCase ): if not isinstance(_UpperCamelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCamelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} ' "is an empty dataset dictionary." ) raise ValueError( F'Dataset at position {i} has at least one split: {list(_UpperCamelCase )}\n' F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(_UpperCamelCase ) )}\']' ) raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase ).__name__}.' ) if i == 0: UpperCamelCase__ , UpperCamelCase__ = ( (Dataset, IterableDataset) if isinstance(_UpperCamelCase , _UpperCamelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError( F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.' ) if stopping_strategy not in ["first_exhausted", "all_exhausted"]: raise ValueError(F'{stopping_strategy} is not supported. Please enter a valid stopping_strategy.' ) if dataset_type is Dataset: return _interleave_map_style_datasets( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , stopping_strategy=_UpperCamelCase ) else: return _interleave_iterable_datasets( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , stopping_strategy=_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[DatasetType] , _UpperCamelCase : Optional[DatasetInfo] = None , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : int = 0 , ) -> DatasetType: '''simple docstring''' if not dsets: raise ValueError("Unable to concatenate an empty list of datasets." ) for i, dataset in enumerate(_UpperCamelCase ): if not isinstance(_UpperCamelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCamelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} ' "is an empty dataset dictionary." ) raise ValueError( F'Dataset at position {i} has at least one split: {list(_UpperCamelCase )}\n' F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(_UpperCamelCase ) )}\']' ) raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase ).__name__}.' ) if i == 0: UpperCamelCase__ , UpperCamelCase__ = ( (Dataset, IterableDataset) if isinstance(_UpperCamelCase , _UpperCamelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError( F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.' ) if dataset_type is Dataset: return _concatenate_map_style_datasets(_UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , axis=_UpperCamelCase ) else: return _concatenate_iterable_datasets(_UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , axis=_UpperCamelCase )
31
1
'''simple docstring''' import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def SCREAMING_SNAKE_CASE__( ) -> Dict: '''simple docstring''' UpperCamelCase__ = argparse.ArgumentParser() parser.add_argument("--model_ckpt" , type=_UpperCamelCase , default="microsoft/unixcoder-base-nine" ) parser.add_argument("--num_epochs" , type=_UpperCamelCase , default=5 ) parser.add_argument("--batch_size" , type=_UpperCamelCase , default=6 ) parser.add_argument("--gradient_accumulation_steps" , type=_UpperCamelCase , default=1 ) parser.add_argument("--freeze" , type=_UpperCamelCase , default=_UpperCamelCase ) parser.add_argument("--learning_rate" , type=_UpperCamelCase , default=5e-4 ) parser.add_argument("--seed" , type=_UpperCamelCase , default=0 ) parser.add_argument("--lr_scheduler_type" , type=_UpperCamelCase , default="cosine" ) parser.add_argument("--num_warmup_steps" , type=_UpperCamelCase , default=10 ) parser.add_argument("--weight_decay" , type=_UpperCamelCase , default=0.0_1 ) parser.add_argument("--output_dir" , type=_UpperCamelCase , default="./results" ) return parser.parse_args() __lowercase: Union[str, Any] = load("accuracy") def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Optional[int] ) -> Tuple: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ = eval_pred UpperCamelCase__ = np.argmax(_UpperCamelCase , axis=1 ) return metric.compute(predictions=_UpperCamelCase , references=_UpperCamelCase ) class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : List[str], a_ : int ): """simple docstring""" super().__init__() UpperCamelCase__ = trainer def lowercase_ ( self : List[Any], a_ : Union[str, Any], a_ : List[str], a_ : int, **a_ : List[str] ): """simple docstring""" if control.should_evaluate: UpperCamelCase__ = deepcopy(a_ ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset, metric_key_prefix="train" ) return control_copy def SCREAMING_SNAKE_CASE__( ) -> int: '''simple docstring''' UpperCamelCase__ = get_args() set_seed(args.seed ) UpperCamelCase__ = load_dataset("codeparrot/codecomplex" , split="train" ) UpperCamelCase__ = dataset.train_test_split(test_size=0.2 ) UpperCamelCase__ = train_test["test"].train_test_split(test_size=0.5 ) UpperCamelCase__ = DatasetDict( { "train": train_test["train"], "test": test_validation["train"], "valid": test_validation["test"], } ) print("Loading tokenizer and model" ) UpperCamelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt ) UpperCamelCase__ = tokenizer.eos_token UpperCamelCase__ = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) UpperCamelCase__ = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): UpperCamelCase__ = False UpperCamelCase__ = ClassLabel(num_classes=7 , names=list(set(train_test_validation["train"]["complexity"] ) ) ) def tokenize(_UpperCamelCase : Tuple ): UpperCamelCase__ = tokenizer(example["src"] , truncation=_UpperCamelCase , max_length=10_24 ) UpperCamelCase__ = labels.straint(example["complexity"] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } UpperCamelCase__ = train_test_validation.map( _UpperCamelCase , batched=_UpperCamelCase , remove_columns=train_test_validation["train"].column_names , ) UpperCamelCase__ = DataCollatorWithPadding(tokenizer=_UpperCamelCase ) UpperCamelCase__ = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy="epoch" , save_strategy="epoch" , logging_strategy="epoch" , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.0_1 , metric_for_best_model="accuracy" , run_name="complexity-java" , report_to="wandb" , ) UpperCamelCase__ = Trainer( model=_UpperCamelCase , args=_UpperCamelCase , train_dataset=tokenized_datasets["train"] , eval_dataset=tokenized_datasets["valid"] , tokenizer=_UpperCamelCase , data_collator=_UpperCamelCase , compute_metrics=_UpperCamelCase , ) print("Training..." ) trainer.add_callback(CustomCallback(_UpperCamelCase ) ) trainer.train() if __name__ == "__main__": main()
31
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list ) -> float: '''simple docstring''' UpperCamelCase__ = 0 while len(_UpperCamelCase ) > 1: UpperCamelCase__ = 0 # Consider two files with minimum cost to be merged for _ in range(2 ): UpperCamelCase__ = files.index(min(_UpperCamelCase ) ) temp += files[min_index] files.pop(_UpperCamelCase ) files.append(_UpperCamelCase ) optimal_merge_cost += temp return optimal_merge_cost if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''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 __lowercase: Dict = get_logger() __lowercase: Optional[dict] = None class UpperCAmelCase ( TensorFormatter[Mapping, 'jax.Array', Mapping]): def __init__( self : Tuple, a_ : Dict=None, a_ : Union[str, Any]=None, **a_ : Dict ): """simple docstring""" super().__init__(features=a_ ) import jax from jaxlib.xla_client import Device if isinstance(a_, a_ ): raise ValueError( f'Expected {device} to be a `str` not {type(a_ )}, as `jaxlib.xla_extension.Device` ' "is not serializable neither with `pickle` nor with `dill`. Instead you can surround " "the device with `str()` to get its string identifier that will be internally mapped " "to the actual `jaxlib.xla_extension.Device`." ) UpperCamelCase__ = device if isinstance(a_, a_ ) else str(jax.devices()[0] ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: UpperCamelCase__ = 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] )}.' ) UpperCamelCase__ = str(jax.devices()[0] ) UpperCamelCase__ = jnp_array_kwargs @staticmethod def lowercase_ ( ): """simple docstring""" import jax return {str(a_ ): device for device in jax.devices()} def lowercase_ ( self : Optional[int], a_ : Any ): """simple docstring""" import jax import jax.numpy as jnp if isinstance(a_, a_ ) and column: if all( isinstance(a_, jax.Array ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return jnp.stack(a_, axis=0 ) return column def lowercase_ ( self : Optional[Any], a_ : List[str] ): """simple docstring""" import jax import jax.numpy as jnp if isinstance(a_, (str, bytes, type(a_ )) ): return value elif isinstance(a_, (np.character, np.ndarray) ) and np.issubdtype(value.dtype, np.character ): return value.tolist() UpperCamelCase__ = {} if isinstance(a_, (np.number, np.ndarray) ) and np.issubdtype(value.dtype, np.integer ): # the default int precision depends on the jax config # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision if jax.config.jax_enable_xaa: UpperCamelCase__ = {"dtype": jnp.intaa} else: UpperCamelCase__ = {"dtype": jnp.intaa} elif isinstance(a_, (np.number, np.ndarray) ) and np.issubdtype(value.dtype, np.floating ): UpperCamelCase__ = {"dtype": jnp.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(a_, PIL.Image.Image ): UpperCamelCase__ = np.asarray(a_ ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: UpperCamelCase__ = self._map_devices_to_str() with jax.default_device(DEVICE_MAPPING[self.device] ): # calling jnp.array on a np.ndarray does copy the data # see https://github.com/google/jax/issues/4486 return jnp.array(a_, **{**default_dtype, **self.jnp_array_kwargs} ) def lowercase_ ( self : Any, a_ : Tuple ): """simple docstring""" import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(a_, torch.Tensor ): return self._tensorize(data_struct.detach().cpu().numpy()[()] ) if hasattr(a_, "__array__" ) and not isinstance(a_, jax.Array ): UpperCamelCase__ = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(a_, np.ndarray ): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(a_ ) for substruct in data_struct] ) elif isinstance(a_, (list, tuple) ): return self._consolidate([self.recursive_tensorize(a_ ) for substruct in data_struct] ) return self._tensorize(a_ ) def lowercase_ ( self : str, a_ : dict ): """simple docstring""" return map_nested(self._recursive_tensorize, a_, map_list=a_ ) def lowercase_ ( self : Any, a_ : pa.Table ): """simple docstring""" UpperCamelCase__ = self.numpy_arrow_extractor().extract_row(a_ ) UpperCamelCase__ = self.python_features_decoder.decode_row(a_ ) return self.recursive_tensorize(a_ ) def lowercase_ ( self : Tuple, a_ : pa.Table ): """simple docstring""" UpperCamelCase__ = self.numpy_arrow_extractor().extract_column(a_ ) UpperCamelCase__ = self.python_features_decoder.decode_column(a_, pa_table.column_names[0] ) UpperCamelCase__ = self.recursive_tensorize(a_ ) UpperCamelCase__ = self._consolidate(a_ ) return column def lowercase_ ( self : str, a_ : pa.Table ): """simple docstring""" UpperCamelCase__ = self.numpy_arrow_extractor().extract_batch(a_ ) UpperCamelCase__ = self.python_features_decoder.decode_batch(a_ ) UpperCamelCase__ = self.recursive_tensorize(a_ ) for column_name in batch: UpperCamelCase__ = self._consolidate(batch[column_name] ) return batch
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list[int | str] ) -> None: '''simple docstring''' create_state_space_tree(_UpperCamelCase , [] , 0 , [0 for i in range(len(_UpperCamelCase ) )] ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list[int | str] , _UpperCamelCase : list[int | str] , _UpperCamelCase : int , _UpperCamelCase : list[int] , ) -> None: '''simple docstring''' if index == len(_UpperCamelCase ): print(_UpperCamelCase ) return for i in range(len(_UpperCamelCase ) ): if not index_used[i]: current_sequence.append(sequence[i] ) UpperCamelCase__ = True create_state_space_tree(_UpperCamelCase , _UpperCamelCase , index + 1 , _UpperCamelCase ) current_sequence.pop() UpperCamelCase__ = False __lowercase: list[int | str] = [3, 1, 2, 4] generate_all_permutations(sequence) __lowercase: list[int | str] = ["A", "B", "C"] generate_all_permutations(sequence_a)
31
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : list[str] ) -> str: '''simple docstring''' UpperCamelCase__ = "" for word_or_phrase in separated: if not isinstance(_UpperCamelCase , _UpperCamelCase ): raise Exception("join() accepts only strings to be joined" ) joined += word_or_phrase + separator return joined.strip(_UpperCamelCase ) if __name__ == "__main__": from doctest import testmod testmod()
31
'''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 __lowercase: int = logging.get_logger(__name__) __lowercase: str = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : List[str] = 'yolos' def __init__( self : List[str], a_ : Optional[int]=768, a_ : Optional[int]=12, a_ : Any=12, a_ : List[str]=3072, a_ : Any="gelu", a_ : int=0.0, a_ : List[Any]=0.0, a_ : Dict=0.02, a_ : Optional[int]=1e-1_2, a_ : List[Any]=[512, 864], a_ : Any=16, a_ : Any=3, a_ : Tuple=True, a_ : List[str]=100, a_ : Union[str, Any]=True, a_ : Any=False, a_ : List[str]=1, a_ : Tuple=5, a_ : Union[str, Any]=2, a_ : int=5, a_ : Union[str, Any]=2, a_ : Dict=0.1, **a_ : Dict, ): """simple docstring""" super().__init__(**a_ ) 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__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = image_size UpperCamelCase__ = patch_size UpperCamelCase__ = num_channels UpperCamelCase__ = qkv_bias UpperCamelCase__ = num_detection_tokens UpperCamelCase__ = use_mid_position_embeddings UpperCamelCase__ = auxiliary_loss # Hungarian matcher UpperCamelCase__ = class_cost UpperCamelCase__ = bbox_cost UpperCamelCase__ = giou_cost # Loss coefficients UpperCamelCase__ = bbox_loss_coefficient UpperCamelCase__ = giou_loss_coefficient UpperCamelCase__ = eos_coefficient class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Union[str, Any] = version.parse('1.11') @property def lowercase_ ( self : str ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase_ ( self : Tuple ): """simple docstring""" return 1e-4 @property def lowercase_ ( self : Optional[int] ): """simple docstring""" return 12
31
1
'''simple docstring''' import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __lowercase: Any = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.14.0", "To fix: pip install -r examples/pytorch/audio-classification/requirements.txt") def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : float , _UpperCamelCase : int = 1_60_00 ) -> str: '''simple docstring''' UpperCamelCase__ = int(round(sample_rate * max_length ) ) if len(_UpperCamelCase ) <= sample_length: return wav UpperCamelCase__ = randint(0 , len(_UpperCamelCase ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class UpperCAmelCase : _lowerCamelCase : Optional[str] = field(default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Name of a dataset from the datasets package'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'A file containing the training audio paths and labels.'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'A file containing the validation audio paths and labels.'}) _lowerCamelCase : str = field( default='train' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) _lowerCamelCase : str = field( default='validation' , metadata={ 'help': ( 'The name of the training data set split to use (via the datasets library). Defaults to \'validation\'' ) } , ) _lowerCamelCase : str = field( default='audio' , metadata={'help': 'The name of the dataset column containing the audio data. Defaults to \'audio\''} , ) _lowerCamelCase : str = field( default='label' , metadata={'help': 'The name of the dataset column containing the labels. Defaults to \'label\''}) _lowerCamelCase : Optional[int] = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) _lowerCamelCase : Optional[int] = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) _lowerCamelCase : float = field( default=20 , metadata={'help': 'Audio clips will be randomly cut to this length during training if the value is set.'} , ) @dataclass class UpperCAmelCase : _lowerCamelCase : str = field( default='facebook/wav2vec2-base' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Pretrained config name or path if not the same as model_name'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from the Hub'}) _lowerCamelCase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Name or path of preprocessor config.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to freeze the feature encoder layers of the model.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to generate an attention mask in the feature extractor.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) _lowerCamelCase : Optional[bool] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def lowercase_ ( self : int ): """simple docstring""" if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( "The argument `--freeze_feature_extractor` is deprecated and " "will be removed in a future version. Use `--freeze_feature_encoder`" "instead. Setting `freeze_feature_encoder==True`.", a_, ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( "The argument `--freeze_feature_extractor` is deprecated and " "should not be used in combination with `--freeze_feature_encoder`." "Only make use of `--freeze_feature_encoder`." ) def SCREAMING_SNAKE_CASE__( ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_audio_classification" , _UpperCamelCase , _UpperCamelCase ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} ' + F'distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}' ) logger.info(F'Training/evaluation parameters {training_args}' ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. UpperCamelCase__ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'Output directory ({training_args.output_dir}) already exists and is not empty. ' "Use --overwrite_output_dir to train from scratch." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Initialize our dataset and prepare it for the audio classification task. UpperCamelCase__ = DatasetDict() UpperCamelCase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--audio_column_name {data_args.audio_column_name} not found in dataset \'{data_args.dataset_name}\'. ' "Make sure to set `--audio_column_name` to the correct audio column - one of " F'{", ".join(raw_datasets["train"].column_names )}.' ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--label_column_name {data_args.label_column_name} not found in dataset \'{data_args.dataset_name}\'. ' "Make sure to set `--label_column_name` to the correct text column - one of " F'{", ".join(raw_datasets["train"].column_names )}.' ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy UpperCamelCase__ = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. UpperCamelCase__ = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) UpperCamelCase__ = feature_extractor.model_input_names[0] def train_transforms(_UpperCamelCase : Any ): UpperCamelCase__ = [] for audio in batch[data_args.audio_column_name]: UpperCamelCase__ = random_subsample( audio["array"] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(_UpperCamelCase ) UpperCamelCase__ = feature_extractor(_UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCamelCase__ = {model_input_name: inputs.get(_UpperCamelCase )} UpperCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(_UpperCamelCase : List[Any] ): UpperCamelCase__ = [audio["array"] for audio in batch[data_args.audio_column_name]] UpperCamelCase__ = feature_extractor(_UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCamelCase__ = {model_input_name: inputs.get(_UpperCamelCase )} UpperCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. UpperCamelCase__ = raw_datasets["train"].features[data_args.label_column_name].names UpperCamelCase__ , UpperCamelCase__ = {}, {} for i, label in enumerate(_UpperCamelCase ): UpperCamelCase__ = str(_UpperCamelCase ) UpperCamelCase__ = label # Load the accuracy metric from the datasets package UpperCamelCase__ = evaluate.load("accuracy" ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Any ): UpperCamelCase__ = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=_UpperCamelCase , references=eval_pred.label_ids ) UpperCamelCase__ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(_UpperCamelCase ) , labelaid=_UpperCamelCase , idalabel=_UpperCamelCase , finetuning_task="audio-classification" , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=_UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: UpperCamelCase__ = ( raw_datasets["train"].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(_UpperCamelCase , output_all_columns=_UpperCamelCase ) if training_args.do_eval: if data_args.max_eval_samples is not None: UpperCamelCase__ = ( raw_datasets["eval"].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(_UpperCamelCase , output_all_columns=_UpperCamelCase ) # Initialize our trainer UpperCamelCase__ = Trainer( model=_UpperCamelCase , args=_UpperCamelCase , train_dataset=raw_datasets["train"] if training_args.do_train else None , eval_dataset=raw_datasets["eval"] if training_args.do_eval else None , compute_metrics=_UpperCamelCase , tokenizer=_UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase__ = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ = last_checkpoint UpperCamelCase__ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics("train" , train_result.metrics ) trainer.save_metrics("train" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ = trainer.evaluate() trainer.log_metrics("eval" , _UpperCamelCase ) trainer.save_metrics("eval" , _UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase__ = { "finetuned_from": model_args.model_name_or_path, "tasks": "audio-classification", "dataset": data_args.dataset_name, "tags": ["audio-classification"], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
31
'''simple docstring''' import os from pickle import UnpicklingError from typing import Dict, Tuple import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict, unflatten_dict import transformers from .utils import logging __lowercase: int = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple=False ) -> Union[str, Any]: '''simple docstring''' try: import torch # noqa: F401 except ImportError: logger.error( "Loading a PyTorch model in Flax, requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise if not is_sharded: UpperCamelCase__ = os.path.abspath(_UpperCamelCase ) logger.info(F'Loading PyTorch weights from {pt_path}' ) UpperCamelCase__ = torch.load(_UpperCamelCase , map_location="cpu" ) logger.info(F'PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters.' ) UpperCamelCase__ = convert_pytorch_state_dict_to_flax(_UpperCamelCase , _UpperCamelCase ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files UpperCamelCase__ = convert_pytorch_sharded_state_dict_to_flax(_UpperCamelCase , _UpperCamelCase ) return flax_state_dict def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple[str] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, jnp.ndarray] , _UpperCamelCase : str , ) -> (Tuple[str], np.ndarray): '''simple docstring''' def is_key_or_prefix_key_in_dict(_UpperCamelCase : Tuple[str] ) -> bool: return len(set(_UpperCamelCase ) & {key, (model_prefix,) + key} ) > 0 # layer norm UpperCamelCase__ = pt_tuple_key[:-1] + ("scale",) if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # batch norm layer mean UpperCamelCase__ = pt_tuple_key[:-1] + ("mean",) if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # batch norm layer var UpperCamelCase__ = pt_tuple_key[:-1] + ("var",) if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # embedding UpperCamelCase__ = pt_tuple_key[:-1] + ("embedding",) if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # conv layer UpperCamelCase__ = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): UpperCamelCase__ = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer UpperCamelCase__ = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): UpperCamelCase__ = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight UpperCamelCase__ = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias UpperCamelCase__ = pt_tuple_key[:-1] + ("bias",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 UpperCamelCase__ = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): UpperCamelCase__ = pt_tuple_key[-2] + "_g" elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): UpperCamelCase__ = pt_tuple_key[-2] + "_v" if name is not None: UpperCamelCase__ = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = {k: v.numpy() for k, v in pt_state_dict.items()} UpperCamelCase__ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: UpperCamelCase__ = flax_model.params["params"] else: UpperCamelCase__ = flax_model.params UpperCamelCase__ = flatten_dict(_UpperCamelCase ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: UpperCamelCase__ = flatten_dict(flax_model.params["batch_stats"] ) random_flax_state_dict.update(_UpperCamelCase ) UpperCamelCase__ = {} UpperCamelCase__ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split("." )[0] for k in pt_state_dict.keys()} ) UpperCamelCase__ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split("." )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCamelCase__ = tuple(pt_key.split("." ) ) # remove base model prefix if necessary UpperCamelCase__ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = pt_tuple_key[1:] # Correctly rename weight parameters UpperCamelCase__ , UpperCamelCase__ = rename_key_and_reshape_tensor( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # add model prefix if necessary UpperCamelCase__ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ' F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1] or "var" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(_UpperCamelCase , _UpperCamelCase ) continue # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) else: # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) return unflatten_dict(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any ) -> Any: '''simple docstring''' import torch # Load the index UpperCamelCase__ = {} for shard_file in shard_filenames: # load using msgpack utils UpperCamelCase__ = torch.load(_UpperCamelCase ) UpperCamelCase__ = {k: v.numpy() for k, v in pt_state_dict.items()} UpperCamelCase__ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict if "batch_stats" in flax_model.params: UpperCamelCase__ = flax_model.params["params"] UpperCamelCase__ = flatten_dict(_UpperCamelCase ) random_flax_state_dict.update(flatten_dict(flax_model.params["batch_stats"] ) ) else: UpperCamelCase__ = flax_model.params UpperCamelCase__ = flatten_dict(_UpperCamelCase ) UpperCamelCase__ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split("." )[0] for k in pt_state_dict.keys()} ) UpperCamelCase__ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split("." )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCamelCase__ = tuple(pt_key.split("." ) ) # remove base model prefix if necessary UpperCamelCase__ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = pt_tuple_key[1:] # Correctly rename weight parameters UpperCamelCase__ , UpperCamelCase__ = rename_key_and_reshape_tensor( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # add model prefix if necessary UpperCamelCase__ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ' F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue if "var" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(_UpperCamelCase , _UpperCamelCase ) continue # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) else: # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) return unflatten_dict(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : Optional[int] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = os.path.abspath(_UpperCamelCase ) logger.info(F'Loading Flax weights from {flax_checkpoint_path}' ) # import correct flax class UpperCamelCase__ = getattr(_UpperCamelCase , "Flax" + model.__class__.__name__ ) # load flax weight dict with open(_UpperCamelCase , "rb" ) as state_f: try: UpperCamelCase__ = from_bytes(_UpperCamelCase , state_f.read() ) except UnpicklingError: raise EnvironmentError(F'Unable to convert {flax_checkpoint_path} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(_UpperCamelCase , _UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple , _UpperCamelCase : Any ) -> Optional[Any]: '''simple docstring''' try: import torch # noqa: F401 except ImportError: logger.error( "Loading a Flax weights in PyTorch, requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise # check if we have bf16 weights UpperCamelCase__ = flatten_dict(jax.tree_util.tree_map(lambda _UpperCamelCase : x.dtype == jnp.bfloataa , _UpperCamelCase ) ).values() if any(_UpperCamelCase ): # convert all weights to fp32 if the are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( "Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` " "before loading those in PyTorch model." ) UpperCamelCase__ = jax.tree_util.tree_map( lambda _UpperCamelCase : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , _UpperCamelCase ) UpperCamelCase__ = flatten_dict(_UpperCamelCase ) UpperCamelCase__ = pt_model.state_dict() UpperCamelCase__ = (pt_model.base_model_prefix in flax_state) and ( pt_model.base_model_prefix not in {k.split("." )[0] for k in pt_model_dict.keys()} ) UpperCamelCase__ = (pt_model.base_model_prefix not in flax_state) and ( pt_model.base_model_prefix in {k.split("." )[0] for k in pt_model_dict.keys()} ) # keep track of unexpected & missing keys UpperCamelCase__ = [] UpperCamelCase__ = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): UpperCamelCase__ = flax_key_tuple[0] == pt_model.base_model_prefix UpperCamelCase__ = ".".join((pt_model.base_model_prefix,) + flax_key_tuple ) in pt_model_dict # adapt flax_key to prepare for loading from/to base model only if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (pt_model.base_model_prefix,) + flax_key_tuple # rename flax weights to PyTorch format if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(_UpperCamelCase ) not in pt_model_dict: # conv layer UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) UpperCamelCase__ = jnp.transpose(_UpperCamelCase , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(_UpperCamelCase ) not in pt_model_dict: # linear layer UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) UpperCamelCase__ = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: UpperCamelCase__ = flax_key_tuple[:-1] + ("running_mean",) elif "var" in flax_key_tuple[-1]: UpperCamelCase__ = flax_key_tuple[:-1] + ("running_var",) if "batch_stats" in flax_state: UpperCamelCase__ = ".".join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: UpperCamelCase__ = ".".join(_UpperCamelCase ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. UpperCamelCase__ = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: UpperCamelCase__ = key.split("." ) UpperCamelCase__ = None if key_components[-3::2] == ["parametrizations", "original0"]: UpperCamelCase__ = key_components[-2] + "_g" elif key_components[-3::2] == ["parametrizations", "original1"]: UpperCamelCase__ = key_components[-2] + "_v" if name is not None: UpperCamelCase__ = key_components[:-3] + [name] UpperCamelCase__ = ".".join(_UpperCamelCase ) UpperCamelCase__ = key if flax_key in special_pt_names: UpperCamelCase__ = special_pt_names[flax_key] if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict UpperCamelCase__ = np.asarray(_UpperCamelCase ) if not isinstance(_UpperCamelCase , np.ndarray ) else flax_tensor UpperCamelCase__ = torch.from_numpy(_UpperCamelCase ) # remove from missing keys missing_keys.remove(_UpperCamelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(_UpperCamelCase ) pt_model.load_state_dict(_UpperCamelCase ) # re-transform missing_keys to list UpperCamelCase__ = list(_UpperCamelCase ) if len(_UpperCamelCase ) > 0: logger.warning( "Some weights of the Flax model were not used when initializing the PyTorch model" F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' " (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This" F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' " to be exactly identical (e.g. initializing a BertForSequenceClassification model from a" " FlaxBertForSequenceClassification model)." ) else: logger.warning(F'All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n' ) if len(_UpperCamelCase ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' " use it for predictions and inference." ) else: logger.warning( F'All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n' "If your task is similar to the task the model of the checkpoint was trained on, " F'you can already use {pt_model.__class__.__name__} for predictions without further training.' ) return pt_model
31
1
'''simple docstring''' import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration __lowercase: Optional[Any] = { "tiny.en": "https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt", "tiny": "https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt", "base.en": "https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt", "base": "https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt", "small.en": "https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt", "small": "https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt", "medium.en": "https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt", "medium": "https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt", "large": "https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt", "large-v2": "https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt", } def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[str] ) -> Tuple: '''simple docstring''' UpperCamelCase__ = ["layers", "blocks"] for k in ignore_keys: state_dict.pop(_UpperCamelCase , _UpperCamelCase ) __lowercase: Any = { "blocks": "layers", "mlp.0": "fc1", "mlp.2": "fc2", "mlp_ln": "final_layer_norm", ".attn.query": ".self_attn.q_proj", ".attn.key": ".self_attn.k_proj", ".attn.value": ".self_attn.v_proj", ".attn_ln": ".self_attn_layer_norm", ".attn.out": ".self_attn.out_proj", ".cross_attn.query": ".encoder_attn.q_proj", ".cross_attn.key": ".encoder_attn.k_proj", ".cross_attn.value": ".encoder_attn.v_proj", ".cross_attn_ln": ".encoder_attn_layer_norm", ".cross_attn.out": ".encoder_attn.out_proj", "decoder.ln.": "decoder.layer_norm.", "encoder.ln.": "encoder.layer_norm.", "token_embedding": "embed_tokens", "encoder.positional_embedding": "encoder.embed_positions.weight", "decoder.positional_embedding": "decoder.embed_positions.weight", "ln_post": "layer_norm", } def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ = list(s_dict.keys() ) for key in keys: UpperCamelCase__ = key for k, v in WHISPER_MAPPING.items(): if k in key: UpperCamelCase__ = new_key.replace(_UpperCamelCase , _UpperCamelCase ) print(F'{key} -> {new_key}' ) UpperCamelCase__ = s_dict.pop(_UpperCamelCase ) return s_dict def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[Any] ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ = emb.weight.shape UpperCamelCase__ = nn.Linear(_UpperCamelCase , _UpperCamelCase , bias=_UpperCamelCase ) UpperCamelCase__ = emb.weight.data return lin_layer def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str ) -> bytes: '''simple docstring''' os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) UpperCamelCase__ = os.path.basename(_UpperCamelCase ) UpperCamelCase__ = url.split("/" )[-2] UpperCamelCase__ = os.path.join(_UpperCamelCase , _UpperCamelCase ) if os.path.exists(_UpperCamelCase ) and not os.path.isfile(_UpperCamelCase ): raise RuntimeError(F'{download_target} exists and is not a regular file' ) if os.path.isfile(_UpperCamelCase ): UpperCamelCase__ = open(_UpperCamelCase , "rb" ).read() if hashlib.shaaaa(_UpperCamelCase ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(F'{download_target} exists, but the SHA256 checksum does not match; re-downloading the file' ) with urllib.request.urlopen(_UpperCamelCase ) as source, open(_UpperCamelCase , "wb" ) as output: with tqdm( total=int(source.info().get("Content-Length" ) ) , ncols=80 , unit="iB" , unit_scale=_UpperCamelCase , unit_divisor=10_24 ) as loop: while True: UpperCamelCase__ = source.read(81_92 ) if not buffer: break output.write(_UpperCamelCase ) loop.update(len(_UpperCamelCase ) ) UpperCamelCase__ = open(_UpperCamelCase , "rb" ).read() if hashlib.shaaaa(_UpperCamelCase ).hexdigest() != expected_shaaaa: raise RuntimeError( "Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model." ) return model_bytes def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Any , _UpperCamelCase : Optional[Any] ) -> Tuple: '''simple docstring''' if ".pt" not in checkpoint_path: UpperCamelCase__ = _download(_MODELS[checkpoint_path] ) else: UpperCamelCase__ = torch.load(_UpperCamelCase , map_location="cpu" ) UpperCamelCase__ = original_checkpoint["dims"] UpperCamelCase__ = original_checkpoint["model_state_dict"] UpperCamelCase__ = state_dict["decoder.token_embedding.weight"] remove_ignore_keys_(_UpperCamelCase ) rename_keys(_UpperCamelCase ) UpperCamelCase__ = True UpperCamelCase__ = state_dict["decoder.layers.0.fc1.weight"].shape[0] UpperCamelCase__ = WhisperConfig( vocab_size=dimensions["n_vocab"] , encoder_ffn_dim=_UpperCamelCase , decoder_ffn_dim=_UpperCamelCase , num_mel_bins=dimensions["n_mels"] , d_model=dimensions["n_audio_state"] , max_target_positions=dimensions["n_text_ctx"] , encoder_layers=dimensions["n_audio_layer"] , encoder_attention_heads=dimensions["n_audio_head"] , decoder_layers=dimensions["n_text_layer"] , decoder_attention_heads=dimensions["n_text_state"] , max_source_positions=dimensions["n_audio_ctx"] , ) UpperCamelCase__ = WhisperForConditionalGeneration(_UpperCamelCase ) UpperCamelCase__ , UpperCamelCase__ = model.model.load_state_dict(_UpperCamelCase , strict=_UpperCamelCase ) if len(_UpperCamelCase ) > 0 and not set(_UpperCamelCase ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( "Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing," F' but all the following weights are missing {missing}' ) if tie_embeds: UpperCamelCase__ = make_linear_from_emb(model.model.decoder.embed_tokens ) else: UpperCamelCase__ = proj_out_weights model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": __lowercase: int = argparse.ArgumentParser() # # Required parameters parser.add_argument("--checkpoint_path", type=str, help="Patht to the downloaded checkpoints") parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") __lowercase: List[str] = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
31
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' return credit_card_number.startswith(("34", "35", "37", "4", "5", "6") ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' UpperCamelCase__ = credit_card_number UpperCamelCase__ = 0 UpperCamelCase__ = len(_UpperCamelCase ) - 2 for i in range(_UpperCamelCase , -1 , -2 ): # double the value of every second digit UpperCamelCase__ = int(cc_number[i] ) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 UpperCamelCase__ = cc_number[:i] + str(_UpperCamelCase ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(_UpperCamelCase ) - 1 , -1 , -2 ): total += int(cc_number[i] ) return total % 10 == 0 def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' UpperCamelCase__ = F'{credit_card_number} is an invalid credit card number because' if not credit_card_number.isdigit(): print(F'{error_message} it has nonnumerical characters.' ) return False if not 13 <= len(_UpperCamelCase ) <= 16: print(F'{error_message} of its length.' ) return False if not validate_initial_digits(_UpperCamelCase ): print(F'{error_message} of its first two digits.' ) return False if not luhn_validation(_UpperCamelCase ): print(F'{error_message} it fails the Luhn check.' ) return False print(F'{credit_card_number} is a valid credit card number.' ) return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
31
1
'''simple docstring''' import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class UpperCAmelCase ( unittest.TestCase): @slow def lowercase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase__ = FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small" ) UpperCamelCase__ = AutoTokenizer.from_pretrained("google/mt5-small" ) UpperCamelCase__ = tokenizer("Hello there", return_tensors="np" ).input_ids UpperCamelCase__ = tokenizer("Hi I am", return_tensors="np" ).input_ids UpperCamelCase__ = shift_tokens_right(a_, model.config.pad_token_id, model.config.decoder_start_token_id ) UpperCamelCase__ = model(a_, decoder_input_ids=a_ ).logits UpperCamelCase__ = optax.softmax_cross_entropy(a_, onehot(a_, logits.shape[-1] ) ).mean() UpperCamelCase__ = -(labels.shape[-1] * loss.item()) UpperCamelCase__ = -84.9_127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : float , _UpperCamelCase : float , _UpperCamelCase : float , ) -> tuple: '''simple docstring''' if (electron_conc, hole_conc, intrinsic_conc).count(0 ) != 1: raise ValueError("You cannot supply more or less than 2 values" ) elif electron_conc < 0: raise ValueError("Electron concentration cannot be negative in a semiconductor" ) elif hole_conc < 0: raise ValueError("Hole concentration cannot be negative in a semiconductor" ) elif intrinsic_conc < 0: raise ValueError( "Intrinsic concentration cannot be negative in a semiconductor" ) elif electron_conc == 0: return ( "electron_conc", intrinsic_conc**2 / hole_conc, ) elif hole_conc == 0: return ( "hole_conc", intrinsic_conc**2 / electron_conc, ) elif intrinsic_conc == 0: return ( "intrinsic_conc", (electron_conc * hole_conc) ** 0.5, ) else: return (-1, -1) if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase: str = logging.get_logger(__name__) __lowercase: Optional[Any] = { "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Any = 'gpt_neox_japanese' def __init__( self : List[str], a_ : Union[str, Any]=3_2000, a_ : str=2560, a_ : Dict=32, a_ : Tuple=32, a_ : Union[str, Any]=4, a_ : Union[str, Any]="gelu", a_ : int=1.00, a_ : Dict=1_0000, a_ : Any=2048, a_ : Optional[int]=0.02, a_ : int=1e-5, a_ : int=True, a_ : Optional[int]=3_1996, a_ : List[str]=3_1999, a_ : List[str]=0.1, a_ : Optional[int]=0.0, **a_ : Tuple, ): """simple docstring""" super().__init__(bos_token_id=a_, eos_token_id=a_, **a_ ) UpperCamelCase__ = vocab_size UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = hidden_size UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = intermediate_multiple_size UpperCamelCase__ = hidden_act UpperCamelCase__ = rotary_pct UpperCamelCase__ = rotary_emb_base UpperCamelCase__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = use_cache UpperCamelCase__ = attention_dropout UpperCamelCase__ = hidden_dropout
31
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowercase: Dict = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Optional[int] = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys __lowercase: Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
31
1
'''simple docstring''' import unittest from transformers import AlbertTokenizer, AlbertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin __lowercase: Dict = get_tests_dir("fixtures/spiece.model") @require_sentencepiece @require_tokenizers class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Union[str, Any] = AlbertTokenizer _lowerCamelCase : str = AlbertTokenizerFast _lowerCamelCase : Tuple = True _lowerCamelCase : List[str] = True _lowerCamelCase : List[str] = True def lowercase_ ( self : Any ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing UpperCamelCase__ = AlbertTokenizer(a_ ) tokenizer.save_pretrained(self.tmpdirname ) def lowercase_ ( self : List[Any], a_ : Optional[Any] ): """simple docstring""" UpperCamelCase__ = "this is a test" UpperCamelCase__ = "this is a test" return input_text, output_text def lowercase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase__ = "<pad>" UpperCamelCase__ = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(a_ ), a_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(a_ ), a_ ) def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0], "<pad>" ) self.assertEqual(vocab_keys[1], "<unk>" ) self.assertEqual(vocab_keys[-1], "▁eloquent" ) self.assertEqual(len(a_ ), 3_0000 ) def lowercase_ ( self : Tuple ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size, 3_0000 ) def lowercase_ ( self : int ): """simple docstring""" if not self.test_rust_tokenizer: return UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_rust_tokenizer() UpperCamelCase__ = "I was born in 92000, and this is falsé." UpperCamelCase__ = tokenizer.tokenize(a_ ) UpperCamelCase__ = rust_tokenizer.tokenize(a_ ) self.assertListEqual(a_, a_ ) UpperCamelCase__ = tokenizer.encode(a_, add_special_tokens=a_ ) UpperCamelCase__ = rust_tokenizer.encode(a_, add_special_tokens=a_ ) self.assertListEqual(a_, a_ ) UpperCamelCase__ = self.get_rust_tokenizer() UpperCamelCase__ = tokenizer.encode(a_ ) UpperCamelCase__ = rust_tokenizer.encode(a_ ) self.assertListEqual(a_, a_ ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = AlbertTokenizer(a_, keep_accents=a_ ) UpperCamelCase__ = tokenizer.tokenize("This is a test" ) self.assertListEqual(a_, ["▁this", "▁is", "▁a", "▁test"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(a_ ), [48, 25, 21, 1289] ) UpperCamelCase__ = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( a_, ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", "."] ) UpperCamelCase__ = tokenizer.convert_tokens_to_ids(a_ ) self.assertListEqual(a_, [31, 23, 386, 19, 561, 3050, 15, 17, 48, 25, 8256, 18, 1, 9] ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(a_ ) self.assertListEqual( a_, ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "."], ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = AlbertTokenizer(a_ ) UpperCamelCase__ = tokenizer.encode("sequence builders" ) UpperCamelCase__ = tokenizer.encode("multi-sequence build" ) UpperCamelCase__ = tokenizer.build_inputs_with_special_tokens(a_ ) UpperCamelCase__ = tokenizer.build_inputs_with_special_tokens(a_, a_ ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ] @slow def lowercase_ ( self : Tuple ): """simple docstring""" UpperCamelCase__ = {"attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "input_ids": [[2, 2_1970, 13, 5, 6092, 167, 28, 7103, 2153, 673, 8, 7028, 1_2051, 18, 17, 7103, 2153, 673, 8, 3515, 1_8684, 8, 4461, 6, 1927, 297, 8, 1_2060, 2607, 18, 13, 5, 4461, 15, 1_0538, 38, 8, 135, 15, 822, 58, 15, 993, 1_0363, 15, 1460, 8005, 4461, 15, 993, 255, 2328, 9, 9, 9, 6, 26, 1112, 816, 3260, 13, 5, 103, 2377, 6, 17, 1112, 816, 2782, 13, 5, 103, 1_0641, 6, 29, 84, 2512, 2430, 782, 1_8684, 2761, 19, 808, 2430, 2556, 17, 855, 1480, 9477, 4091, 128, 1_1712, 15, 7103, 2153, 673, 17, 2_4883, 9990, 9, 3], [2, 1_1502, 25, 1006, 20, 782, 8, 1_1809, 855, 1732, 1_9393, 1_8667, 37, 367, 2_1018, 69, 1854, 34, 1_1860, 1_9124, 27, 156, 225, 17, 193, 4141, 19, 65, 9124, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 14, 2231, 886, 2385, 1_7659, 84, 14, 1_6792, 1952, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "token_type_ids": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=a_, model_name="albert-base-v2", revision="6b6560eaf5ff2e250b00c50f380c5389a9c2d82e", )
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict , _UpperCamelCase : str , _UpperCamelCase : Optional[int] , _UpperCamelCase : str ) -> Dict: # noqa: E741 '''simple docstring''' while r - l > 1: UpperCamelCase__ = (l + r) // 2 if v[m] >= key: UpperCamelCase__ = m else: UpperCamelCase__ = m # noqa: E741 return r def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list[int] ) -> int: '''simple docstring''' if len(_UpperCamelCase ) == 0: return 0 UpperCamelCase__ = [0] * len(_UpperCamelCase ) UpperCamelCase__ = 1 UpperCamelCase__ = v[0] for i in range(1 , len(_UpperCamelCase ) ): if v[i] < tail[0]: UpperCamelCase__ = v[i] elif v[i] > tail[length - 1]: UpperCamelCase__ = v[i] length += 1 else: UpperCamelCase__ = v[i] return length if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' import json import logging import os import sys from time import time from unittest.mock import patch from transformers.testing_utils import TestCasePlus, require_torch_tpu logging.basicConfig(level=logging.DEBUG) __lowercase: Union[str, Any] = logging.getLogger() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[str] ) -> int: '''simple docstring''' UpperCamelCase__ = {} UpperCamelCase__ = os.path.join(_UpperCamelCase , "all_results.json" ) if os.path.exists(_UpperCamelCase ): with open(_UpperCamelCase , "r" ) as f: UpperCamelCase__ = json.load(_UpperCamelCase ) else: raise ValueError(F'can\'t find {path}' ) return results __lowercase: List[str] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) @require_torch_tpu class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def lowercase_ ( self : Tuple ): """simple docstring""" import xla_spawn UpperCamelCase__ = self.get_auto_remove_tmp_dir() UpperCamelCase__ = f'\n ./examples/pytorch/text-classification/run_glue.py\n --num_cores=8\n ./examples/pytorch/text-classification/run_glue.py\n --model_name_or_path distilbert-base-uncased\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n --do_train\n --do_eval\n --debug tpu_metrics_debug\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --learning_rate=1e-4\n --max_steps=10\n --warmup_steps=2\n --seed=42\n --max_seq_length=128\n '.split() with patch.object(a_, "argv", a_ ): UpperCamelCase__ = time() xla_spawn.main() UpperCamelCase__ = time() UpperCamelCase__ = get_results(a_ ) self.assertGreaterEqual(result["eval_accuracy"], 0.75 ) # Assert that the script takes less than 500 seconds to make sure it doesn't hang. self.assertLess(end - start, 500 ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" import xla_spawn UpperCamelCase__ = "\n ./tests/test_trainer_tpu.py\n --num_cores=8\n ./tests/test_trainer_tpu.py\n ".split() with patch.object(a_, "argv", a_ ): xla_spawn.main()
31
'''simple docstring''' from ..utils import DummyObject, requires_backends class UpperCAmelCase ( metaclass=SCREAMING_SNAKE_CASE__): _lowerCamelCase : str = ['torch', 'scipy'] def __init__( self : List[str], *a_ : Optional[int], **a_ : int ): """simple docstring""" requires_backends(self, ["torch", "scipy"] ) @classmethod def lowercase_ ( cls : Dict, *a_ : Tuple, **a_ : Dict ): """simple docstring""" requires_backends(cls, ["torch", "scipy"] ) @classmethod def lowercase_ ( cls : Optional[Any], *a_ : List[Any], **a_ : Any ): """simple docstring""" requires_backends(cls, ["torch", "scipy"] )
31
1
'''simple docstring''' import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase: Any = logging.getLogger(__name__) @dataclass(frozen=SCREAMING_SNAKE_CASE__) class UpperCAmelCase : _lowerCamelCase : str _lowerCamelCase : str _lowerCamelCase : Optional[str] = None _lowerCamelCase : Optional[str] = None _lowerCamelCase : Optional[str] = None @dataclass(frozen=SCREAMING_SNAKE_CASE__) class UpperCAmelCase : _lowerCamelCase : List[int] _lowerCamelCase : Optional[List[int]] = None _lowerCamelCase : Optional[List[int]] = None _lowerCamelCase : Optional[Union[int, float]] = None _lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : List[InputFeatures] def __init__( self : str, a_ : str, a_ : PreTrainedTokenizer, a_ : str, a_ : Optional[int] = None, a_ : Optional[Any]=False, a_ : bool = False, ): """simple docstring""" UpperCamelCase__ = hans_processors[task]() UpperCamelCase__ = os.path.join( a_, "cached_{}_{}_{}_{}".format( "dev" if evaluate else "train", tokenizer.__class__.__name__, str(a_ ), a_, ), ) UpperCamelCase__ = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) UpperCamelCase__ , UpperCamelCase__ = label_list[2], label_list[1] UpperCamelCase__ = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. UpperCamelCase__ = cached_features_file + ".lock" with FileLock(a_ ): if os.path.exists(a_ ) and not overwrite_cache: logger.info(f'Loading features from cached file {cached_features_file}' ) UpperCamelCase__ = torch.load(a_ ) else: logger.info(f'Creating features from dataset file at {data_dir}' ) UpperCamelCase__ = ( processor.get_dev_examples(a_ ) if evaluate else processor.get_train_examples(a_ ) ) logger.info("Training examples: %s", len(a_ ) ) UpperCamelCase__ = hans_convert_examples_to_features(a_, a_, a_, a_ ) logger.info("Saving features into cached file %s", a_ ) torch.save(self.features, a_ ) def __len__( self : List[Any] ): """simple docstring""" return len(self.features ) def __getitem__( self : Dict, a_ : List[Any] ): """simple docstring""" return self.features[i] def lowercase_ ( self : Optional[int] ): """simple docstring""" return self.label_list if is_tf_available(): import tensorflow as tf class UpperCAmelCase : _lowerCamelCase : List[InputFeatures] def __init__( self : str, a_ : str, a_ : PreTrainedTokenizer, a_ : str, a_ : Optional[int] = 128, a_ : Union[str, Any]=False, a_ : bool = False, ): """simple docstring""" UpperCamelCase__ = hans_processors[task]() UpperCamelCase__ = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) UpperCamelCase__ , UpperCamelCase__ = label_list[2], label_list[1] UpperCamelCase__ = label_list UpperCamelCase__ = processor.get_dev_examples(a_ ) if evaluate else processor.get_train_examples(a_ ) UpperCamelCase__ = hans_convert_examples_to_features(a_, a_, a_, a_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ), desc="convert examples to features" ): if ex_index % 1_0000 == 0: logger.info("Writing example %d of %d" % (ex_index, len(a_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) UpperCamelCase__ = tf.data.Dataset.from_generator( a_, ( { "example_id": tf.intaa, "input_ids": tf.intaa, "attention_mask": tf.intaa, "token_type_ids": tf.intaa, }, tf.intaa, ), ( { "example_id": tf.TensorShape([] ), "input_ids": tf.TensorShape([None, None] ), "attention_mask": tf.TensorShape([None, None] ), "token_type_ids": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ), ) def lowercase_ ( self : Union[str, Any] ): """simple docstring""" return self.dataset def __len__( self : List[str] ): """simple docstring""" return len(self.features ) def __getitem__( self : Union[str, Any], a_ : int ): """simple docstring""" return self.features[i] def lowercase_ ( self : int ): """simple docstring""" return self.label_list class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def lowercase_ ( self : Any, a_ : Dict ): """simple docstring""" return self._create_examples(self._read_tsv(os.path.join(a_, "heuristics_train_set.txt" ) ), "train" ) def lowercase_ ( self : Dict, a_ : Tuple ): """simple docstring""" return self._create_examples(self._read_tsv(os.path.join(a_, "heuristics_evaluation_set.txt" ) ), "dev" ) def lowercase_ ( self : Optional[int] ): """simple docstring""" return ["contradiction", "entailment", "neutral"] def lowercase_ ( self : Dict, a_ : Optional[int], a_ : List[str] ): """simple docstring""" UpperCamelCase__ = [] for i, line in enumerate(a_ ): if i == 0: continue UpperCamelCase__ = "%s-%s" % (set_type, line[0]) UpperCamelCase__ = line[5] UpperCamelCase__ = line[6] UpperCamelCase__ = line[7][2:] if line[7].startswith("ex" ) else line[7] UpperCamelCase__ = line[0] examples.append(InputExample(guid=a_, text_a=a_, text_b=a_, label=a_, pairID=a_ ) ) return examples def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[InputExample] , _UpperCamelCase : List[str] , _UpperCamelCase : int , _UpperCamelCase : PreTrainedTokenizer , ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = {label: i for i, label in enumerate(_UpperCamelCase )} UpperCamelCase__ = [] for ex_index, example in tqdm.tqdm(enumerate(_UpperCamelCase ) , desc="convert examples to features" ): if ex_index % 1_00_00 == 0: logger.info("Writing example %d" % (ex_index) ) UpperCamelCase__ = tokenizer( example.text_a , example.text_b , add_special_tokens=_UpperCamelCase , max_length=_UpperCamelCase , padding="max_length" , truncation=_UpperCamelCase , return_overflowing_tokens=_UpperCamelCase , ) UpperCamelCase__ = label_map[example.label] if example.label in label_map else 0 UpperCamelCase__ = int(example.pairID ) features.append(InputFeatures(**_UpperCamelCase , label=_UpperCamelCase , pairID=_UpperCamelCase ) ) for i, example in enumerate(examples[:5] ): logger.info("*** Example ***" ) logger.info(F'guid: {example}' ) logger.info(F'features: {features[i]}' ) return features __lowercase: Dict = { "hans": 3, } __lowercase: Tuple = { "hans": HansProcessor, }
31
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase: str = logging.get_logger(__name__) __lowercase: Optional[Any] = { "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Any = 'gpt_neox_japanese' def __init__( self : List[str], a_ : Union[str, Any]=3_2000, a_ : str=2560, a_ : Dict=32, a_ : Tuple=32, a_ : Union[str, Any]=4, a_ : Union[str, Any]="gelu", a_ : int=1.00, a_ : Dict=1_0000, a_ : Any=2048, a_ : Optional[int]=0.02, a_ : int=1e-5, a_ : int=True, a_ : Optional[int]=3_1996, a_ : List[str]=3_1999, a_ : List[str]=0.1, a_ : Optional[int]=0.0, **a_ : Tuple, ): """simple docstring""" super().__init__(bos_token_id=a_, eos_token_id=a_, **a_ ) UpperCamelCase__ = vocab_size UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = hidden_size UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = intermediate_multiple_size UpperCamelCase__ = hidden_act UpperCamelCase__ = rotary_pct UpperCamelCase__ = rotary_emb_base UpperCamelCase__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = use_cache UpperCamelCase__ = attention_dropout UpperCamelCase__ = hidden_dropout
31
1
'''simple docstring''' import os from typing import Dict, List, Tuple, TypeVar, Union __lowercase: int = TypeVar("T") __lowercase: List[str] = Union[List[T], Tuple[T, ...]] __lowercase: List[Any] = Union[T, List[T], Dict[str, T]] __lowercase: Tuple = Union[str, bytes, os.PathLike]
31
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : float , _UpperCamelCase : float ) -> float: '''simple docstring''' if density <= 0: raise ValueError("Impossible fluid density" ) if bulk_modulus <= 0: raise ValueError("Impossible bulk modulus" ) return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : torch.FloatTensor class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__): @register_to_config def __init__( self : List[Any], a_ : int = 3, a_ : int = 3, a_ : Tuple[str] = ("DownEncoderBlock2D",), a_ : Tuple[str] = ("UpDecoderBlock2D",), a_ : Tuple[int] = (64,), a_ : int = 1, a_ : str = "silu", a_ : int = 3, a_ : int = 32, a_ : int = 256, a_ : int = 32, a_ : Optional[int] = None, a_ : float = 0.18_215, a_ : str = "group", ): """simple docstring""" super().__init__() # pass init params to Encoder UpperCamelCase__ = Encoder( in_channels=a_, out_channels=a_, down_block_types=a_, block_out_channels=a_, layers_per_block=a_, act_fn=a_, norm_num_groups=a_, double_z=a_, ) UpperCamelCase__ = vq_embed_dim if vq_embed_dim is not None else latent_channels UpperCamelCase__ = nn.Convad(a_, a_, 1 ) UpperCamelCase__ = VectorQuantizer(a_, a_, beta=0.25, remap=a_, sane_index_shape=a_ ) UpperCamelCase__ = nn.Convad(a_, a_, 1 ) # pass init params to Decoder UpperCamelCase__ = Decoder( in_channels=a_, out_channels=a_, up_block_types=a_, block_out_channels=a_, layers_per_block=a_, act_fn=a_, norm_num_groups=a_, norm_type=a_, ) @apply_forward_hook def lowercase_ ( self : List[Any], a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" UpperCamelCase__ = self.encoder(a_ ) UpperCamelCase__ = self.quant_conv(a_ ) if not return_dict: return (h,) return VQEncoderOutput(latents=a_ ) @apply_forward_hook def lowercase_ ( self : int, a_ : torch.FloatTensor, a_ : bool = False, a_ : bool = True ): """simple docstring""" if not force_not_quantize: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self.quantize(a_ ) else: UpperCamelCase__ = h UpperCamelCase__ = self.post_quant_conv(a_ ) UpperCamelCase__ = self.decoder(a_, quant if self.config.norm_type == "spatial" else None ) if not return_dict: return (dec,) return DecoderOutput(sample=a_ ) def lowercase_ ( self : List[str], a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" UpperCamelCase__ = sample UpperCamelCase__ = self.encode(a_ ).latents UpperCamelCase__ = self.decode(a_ ).sample if not return_dict: return (dec,) return DecoderOutput(sample=a_ )
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int | float | str , _UpperCamelCase : int | float | str ) -> list[str]: '''simple docstring''' if nth_term == "": return [""] UpperCamelCase__ = int(_UpperCamelCase ) UpperCamelCase__ = int(_UpperCamelCase ) UpperCamelCase__ = [] for temp in range(int(_UpperCamelCase ) ): series.append(F'1 / {pow(temp + 1 , int(_UpperCamelCase ) )}' if series else "1" ) return series if __name__ == "__main__": import doctest doctest.testmod() __lowercase: Dict = int(input("Enter the last number (nth term) of the P-Series")) __lowercase: Optional[int] = int(input("Enter the power for P-Series")) print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") print(p_series(nth_term, power))
31
1
'''simple docstring''' class UpperCAmelCase : def __init__( self : Union[str, Any], a_ : str, a_ : Dict, a_ : List[str] ): """simple docstring""" UpperCamelCase__ = None UpperCamelCase__ = None UpperCamelCase__ = graph self._normalize_graph(a_, a_ ) UpperCamelCase__ = len(a_ ) UpperCamelCase__ = None def lowercase_ ( self : Tuple, a_ : int, a_ : Union[str, Any] ): """simple docstring""" if sources is int: UpperCamelCase__ = [sources] if sinks is int: UpperCamelCase__ = [sinks] if len(a_ ) == 0 or len(a_ ) == 0: return UpperCamelCase__ = sources[0] UpperCamelCase__ = sinks[0] # make fake vertex if there are more # than one source or sink if len(a_ ) > 1 or len(a_ ) > 1: UpperCamelCase__ = 0 for i in sources: max_input_flow += sum(self.graph[i] ) UpperCamelCase__ = len(self.graph ) + 1 for room in self.graph: room.insert(0, 0 ) self.graph.insert(0, [0] * size ) for i in sources: UpperCamelCase__ = max_input_flow UpperCamelCase__ = 0 UpperCamelCase__ = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: UpperCamelCase__ = max_input_flow UpperCamelCase__ = size - 1 def lowercase_ ( self : Optional[int] ): """simple docstring""" if self.maximum_flow_algorithm is None: raise Exception("You need to set maximum flow algorithm before." ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def lowercase_ ( self : Optional[int], a_ : List[str] ): """simple docstring""" UpperCamelCase__ = algorithm(self ) class UpperCAmelCase : def __init__( self : int, a_ : Optional[int] ): """simple docstring""" UpperCamelCase__ = flow_network UpperCamelCase__ = flow_network.verticesCount UpperCamelCase__ = flow_network.sourceIndex UpperCamelCase__ = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that UpperCamelCase__ = flow_network.graph UpperCamelCase__ = False def lowercase_ ( self : Union[str, Any] ): """simple docstring""" if not self.executed: self._algorithm() UpperCamelCase__ = True def lowercase_ ( self : Union[str, Any] ): """simple docstring""" pass class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : List[str], a_ : Optional[int] ): """simple docstring""" super().__init__(a_ ) # use this to save your result UpperCamelCase__ = -1 def lowercase_ ( self : Optional[Any] ): """simple docstring""" if not self.executed: raise Exception("You should execute algorithm before using its result!" ) return self.maximum_flow class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : List[Any], a_ : Union[str, Any] ): """simple docstring""" super().__init__(a_ ) UpperCamelCase__ = [[0] * self.verticies_count for i in range(self.verticies_count )] UpperCamelCase__ = [0] * self.verticies_count UpperCamelCase__ = [0] * self.verticies_count def lowercase_ ( self : Tuple ): """simple docstring""" UpperCamelCase__ = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule UpperCamelCase__ = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list UpperCamelCase__ = 0 while i < len(a_ ): UpperCamelCase__ = vertices_list[i] UpperCamelCase__ = self.heights[vertex_index] self.process_vertex(a_ ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0, vertices_list.pop(a_ ) ) UpperCamelCase__ = 0 else: i += 1 UpperCamelCase__ = sum(self.preflow[self.source_index] ) def lowercase_ ( self : Optional[int], a_ : Dict ): """simple docstring""" while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(a_, a_ ) self.relabel(a_ ) def lowercase_ ( self : List[str], a_ : Union[str, Any], a_ : List[Any] ): """simple docstring""" UpperCamelCase__ = min( self.excesses[from_index], self.graph[from_index][to_index] - self.preflow[from_index][to_index], ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def lowercase_ ( self : Optional[int], a_ : List[str] ): """simple docstring""" UpperCamelCase__ = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): UpperCamelCase__ = self.heights[to_index] if min_height is not None: UpperCamelCase__ = min_height + 1 if __name__ == "__main__": __lowercase: Any = [0] __lowercase: Tuple = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] __lowercase: Dict = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network __lowercase: int = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate __lowercase: int = flow_network.find_maximum_flow() print(F"""maximum flow is {maximum_flow}""")
31
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE__) class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True}) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()}) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel}) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def lowercase_ ( self : str, a_ : Union[str, Any] ): """simple docstring""" if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column], a_ ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) UpperCamelCase__ = copy.deepcopy(self ) UpperCamelCase__ = self.label_schema.copy() UpperCamelCase__ = features[self.label_column] UpperCamelCase__ = label_schema return task_template @property def lowercase_ ( self : Any ): """simple docstring""" return { self.audio_column: "audio", self.label_column: "labels", }
31
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str ) -> float: '''simple docstring''' def get_matched_characters(_UpperCamelCase : str , _UpperCamelCase : str ) -> str: UpperCamelCase__ = [] UpperCamelCase__ = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): UpperCamelCase__ = int(max(0 , i - limit ) ) UpperCamelCase__ = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(_UpperCamelCase ) UpperCamelCase__ = F'{_stra[0:_stra.index(_UpperCamelCase )]} {_stra[_stra.index(_UpperCamelCase ) + 1:]}' return "".join(_UpperCamelCase ) # matching characters UpperCamelCase__ = get_matched_characters(_UpperCamelCase , _UpperCamelCase ) UpperCamelCase__ = get_matched_characters(_UpperCamelCase , _UpperCamelCase ) UpperCamelCase__ = len(_UpperCamelCase ) # transposition UpperCamelCase__ = ( len([(ca, ca) for ca, ca in zip(_UpperCamelCase , _UpperCamelCase ) if ca != ca] ) // 2 ) if not match_count: UpperCamelCase__ = 0.0 else: UpperCamelCase__ = ( 1 / 3 * ( match_count / len(_UpperCamelCase ) + match_count / len(_UpperCamelCase ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters UpperCamelCase__ = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("hello", "world"))
31
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import is_speech_available from transformers.testing_utils import require_torch, require_torchaudio from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import SpeechaTextFeatureExtractor __lowercase: str = random.Random() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int]=1.0 , _UpperCamelCase : Dict=None , _UpperCamelCase : List[str]=None ) -> Union[str, Any]: '''simple docstring''' if rng is None: UpperCamelCase__ = global_rng UpperCamelCase__ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class UpperCAmelCase ( unittest.TestCase): def __init__( self : List[Any], a_ : List[str], a_ : Any=7, a_ : Dict=400, a_ : str=2000, a_ : List[Any]=24, a_ : int=24, a_ : int=0.0, a_ : Union[str, Any]=1_6000, a_ : Union[str, Any]=True, a_ : Optional[Any]=True, ): """simple docstring""" UpperCamelCase__ = parent UpperCamelCase__ = batch_size UpperCamelCase__ = min_seq_length UpperCamelCase__ = max_seq_length UpperCamelCase__ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) UpperCamelCase__ = feature_size UpperCamelCase__ = num_mel_bins UpperCamelCase__ = padding_value UpperCamelCase__ = sampling_rate UpperCamelCase__ = return_attention_mask UpperCamelCase__ = do_normalize def lowercase_ ( self : Tuple ): """simple docstring""" return { "feature_size": self.feature_size, "num_mel_bins": self.num_mel_bins, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def lowercase_ ( self : Optional[Any], a_ : Union[str, Any]=False, a_ : Optional[int]=False ): """simple docstring""" def _flatten(a_ : Dict ): return list(itertools.chain(*a_ ) ) if equal_length: UpperCamelCase__ = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size UpperCamelCase__ = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length, self.max_seq_length, self.seq_length_diff ) ] if numpify: UpperCamelCase__ = [np.asarray(a_ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Dict = SpeechaTextFeatureExtractor if is_speech_available() else None def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = SpeechaTextFeatureExtractionTester(self ) def lowercase_ ( self : Optional[int], a_ : Tuple ): """simple docstring""" self.assertTrue(np.all(np.mean(a_, axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(a_, axis=0 ) - 1 ) < 1e-3 ) ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = [np.asarray(a_ ) for speech_input in speech_inputs] # Test feature size UpperCamelCase__ = feature_extractor(a_, padding=a_, return_tensors="np" ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size ) # Test not batched input UpperCamelCase__ = feature_extractor(speech_inputs[0], return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(np_speech_inputs[0], return_tensors="np" ).input_features self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) # Test batched UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features for enc_seq_a, enc_seq_a in zip(a_, a_ ): self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) # Test 2-D numpy arrays are batched. UpperCamelCase__ = [floats_list((1, x) )[0] for x in (800, 800, 800)] UpperCamelCase__ = np.asarray(a_ ) UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features for enc_seq_a, enc_seq_a in zip(a_, a_ ): self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = ["longest", "max_length", "do_not_pad"] UpperCamelCase__ = [None, 16, None] for max_length, padding in zip(a_, a_ ): UpperCamelCase__ = feature_extractor( a_, padding=a_, max_length=a_, return_attention_mask=a_ ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = [np.sum(a_ ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = ["longest", "max_length", "do_not_pad"] UpperCamelCase__ = [None, 16, None] for max_length, padding in zip(a_, a_ ): UpperCamelCase__ = feature_extractor( a_, max_length=a_, padding=a_, return_tensors="np", return_attention_mask=a_ ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = [np.sum(a_ ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="max_length", max_length=4, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1] ) self._check_zero_mean_unit_variance(input_features[2] ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="longest", max_length=4, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 4, 24) ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="longest", max_length=16, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 6, 24) ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" import torch UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = np.random.rand(100, 32 ).astype(np.floataa ) UpperCamelCase__ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: UpperCamelCase__ = feature_extractor.pad([{"input_features": inputs}], return_tensors="np" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) UpperCamelCase__ = feature_extractor.pad([{"input_features": inputs}], return_tensors="pt" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def lowercase_ ( self : List[str], a_ : int ): """simple docstring""" from datasets import load_dataset UpperCamelCase__ = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation" ) # automatic decoding with librispeech UpperCamelCase__ = ds.sort("id" ).select(range(a_ ) )[:num_samples]["audio"] return [x["array"] for x in speech_samples] def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = np.array([ -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241, -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128, -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625, ] ) # fmt: on UpperCamelCase__ = self._load_datasamples(1 ) UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = feature_extractor(a_, return_tensors="pt" ).input_features self.assertEquals(input_features.shape, (1, 584, 24) ) self.assertTrue(np.allclose(input_features[0, 0, :30], a_, atol=1e-4 ) )
31
1
'''simple docstring''' from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : float | Decimal , _UpperCamelCase : float = 10**-10 ) -> float: '''simple docstring''' UpperCamelCase__ = a while True: UpperCamelCase__ = Decimal(_UpperCamelCase ) - ( Decimal(eval(_UpperCamelCase ) ) / Decimal(eval(str(diff(_UpperCamelCase ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(_UpperCamelCase ) ) < precision: # noqa: S307 return float(_UpperCamelCase ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F"""The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}""") # Find root of polynomial print(F"""The root of x**2 - 5*x + 2 = 0 is {newton_raphson('x**2 - 5*x + 2', 0.4)}""") # Find Square Root of 5 print(F"""The root of log(x) - 1 = 0 is {newton_raphson('log(x) - 1', 2)}""") # Exponential Roots print(F"""The root of exp(x) - 1 = 0 is {newton_raphson('exp(x) - 1', 0)}""")
31
'''simple docstring''' import logging import numpy as np import pytest from scipy.linalg import eigh logging.basicConfig(level=logging.INFO, format="%(message)s") def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray ) -> np.ndarray: '''simple docstring''' return input_array.reshape((input_array.size, 1) ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' UpperCamelCase__ = np.nan for i in range(_UpperCamelCase ): UpperCamelCase__ = features[:, labels == i] UpperCamelCase__ = data.mean(1 ) # Centralize the data of class i UpperCamelCase__ = data - column_reshape(_UpperCamelCase ) if i > 0: # If covariance_sum is not None covariance_sum += np.dot(_UpperCamelCase , centered_data.T ) else: # If covariance_sum is np.nan (i.e. first loop) UpperCamelCase__ = np.dot(_UpperCamelCase , centered_data.T ) return covariance_sum / features.shape[1] def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' UpperCamelCase__ = features.mean(1 ) UpperCamelCase__ = np.nan for i in range(_UpperCamelCase ): UpperCamelCase__ = features[:, labels == i] UpperCamelCase__ = data.shape[1] UpperCamelCase__ = data.mean(1 ) if i > 0: # If covariance_sum is not None covariance_sum += device_data * np.dot( column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase ) , (column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase )).T , ) else: # If covariance_sum is np.nan (i.e. first loop) UpperCamelCase__ = device_data * np.dot( column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase ) , (column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase )).T , ) return covariance_sum / features.shape[1] def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' if features.any(): UpperCamelCase__ = features.mean(1 ) # Center the dataset UpperCamelCase__ = features - np.reshape(_UpperCamelCase , (data_mean.size, 1) ) UpperCamelCase__ = np.dot(_UpperCamelCase , centered_data.T ) / features.shape[1] UpperCamelCase__ , UpperCamelCase__ = np.linalg.eigh(_UpperCamelCase ) # Take all the columns in the reverse order (-1), and then takes only the first UpperCamelCase__ = eigenvectors[:, ::-1][:, 0:dimensions] # Project the database on the new space UpperCamelCase__ = np.dot(filtered_eigenvectors.T , _UpperCamelCase ) logging.info("Principal Component Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=_UpperCamelCase ) logging.error("Dataset empty" ) raise AssertionError def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' assert classes > dimensions # Check if features have been already loaded if features.any: UpperCamelCase__ , UpperCamelCase__ = eigh( covariance_between_classes(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) , covariance_within_classes(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) , ) UpperCamelCase__ = eigenvectors[:, ::-1][:, :dimensions] UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = np.linalg.svd(_UpperCamelCase ) UpperCamelCase__ = svd_matrix[:, 0:dimensions] UpperCamelCase__ = np.dot(filtered_svd_matrix.T , _UpperCamelCase ) logging.info("Linear Discriminant Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=_UpperCamelCase ) logging.error("Dataset empty" ) raise AssertionError def SCREAMING_SNAKE_CASE__( ) -> None: '''simple docstring''' UpperCamelCase__ = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]] ) UpperCamelCase__ = np.array([0, 0, 0, 1, 1] ) UpperCamelCase__ = 2 UpperCamelCase__ = 2 # Assert that the function raises an AssertionError if dimensions > classes with pytest.raises(_UpperCamelCase ) as error_info: UpperCamelCase__ = linear_discriminant_analysis( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if isinstance(_UpperCamelCase , np.ndarray ): raise AssertionError( "Did not raise AssertionError for dimensions > classes" ) assert error_info.type is AssertionError def SCREAMING_SNAKE_CASE__( ) -> None: '''simple docstring''' UpperCamelCase__ = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) UpperCamelCase__ = 2 UpperCamelCase__ = np.array([[6.9_2_8_2_0_3_2_3, 8.6_6_0_2_5_4_0_4, 1_0.3_9_2_3_0_4_8_5], [3.0, 3.0, 3.0]] ) with pytest.raises(_UpperCamelCase ) as error_info: UpperCamelCase__ = principal_component_analysis(_UpperCamelCase , _UpperCamelCase ) if not np.allclose(_UpperCamelCase , _UpperCamelCase ): raise AssertionError assert error_info.type is AssertionError if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder __lowercase: int = "__DUMMY_TRANSFORMERS_USER__" __lowercase: Optional[Any] = "Dummy User" __lowercase: List[str] = "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" __lowercase: Any = "https://hub-ci.huggingface.co" __lowercase: Optional[int] = CI_HUB_ENDPOINT + "/datasets/{repo_id}/resolve/{revision}/{path}" __lowercase: List[str] = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}" __lowercase: List[str] = Path("~/.huggingface/hub_ci_token").expanduser() @pytest.fixture def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple ) -> List[Any]: '''simple docstring''' monkeypatch.setattr( "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE" , _UpperCamelCase ) @pytest.fixture def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' monkeypatch.setattr("datasets.config.HF_ENDPOINT" , _UpperCamelCase ) monkeypatch.setattr("datasets.config.HUB_DATASETS_URL" , _UpperCamelCase ) @pytest.fixture def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Optional[Any] ) -> Any: '''simple docstring''' monkeypatch.setattr("huggingface_hub.hf_api.HfFolder.path_token" , _UpperCamelCase ) @pytest.fixture def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' HfFolder.save_token(_UpperCamelCase ) yield HfFolder.delete_token() @pytest.fixture(scope="session" ) def SCREAMING_SNAKE_CASE__( ) -> List[Any]: '''simple docstring''' return HfApi(endpoint=_UpperCamelCase ) @pytest.fixture(scope="session" ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : HfApi ) -> str: '''simple docstring''' UpperCamelCase__ = HfFolder.get_token() HfFolder.save_token(_UpperCamelCase ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(_UpperCamelCase ) @pytest.fixture def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple ) -> List[Any]: '''simple docstring''' def _cleanup_repo(_UpperCamelCase : str ): hf_api.delete_repo(_UpperCamelCase , token=_UpperCamelCase , repo_type="dataset" ) return _cleanup_repo @pytest.fixture def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Optional[Any] ) -> Optional[Any]: '''simple docstring''' @contextmanager def _temporary_repo(_UpperCamelCase : List[Any] ): try: yield repo_id finally: cleanup_repo(_UpperCamelCase ) return _temporary_repo @pytest.fixture(scope="session" ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : HfApi , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] ) -> int: '''simple docstring''' UpperCamelCase__ = F'repo_txt_data-{int(time.time() * 10e3 )}' UpperCamelCase__ = F'{CI_HUB_USER}/{repo_name}' hf_api.create_repo(_UpperCamelCase , token=_UpperCamelCase , repo_type="dataset" , private=_UpperCamelCase ) hf_api.upload_file( token=_UpperCamelCase , path_or_fileobj=str(_UpperCamelCase ) , path_in_repo="data/text_data.txt" , repo_id=_UpperCamelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_UpperCamelCase , token=_UpperCamelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Any ) -> Tuple: '''simple docstring''' return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="session" ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : HfApi , _UpperCamelCase : int , _UpperCamelCase : List[str] ) -> Tuple: '''simple docstring''' UpperCamelCase__ = F'repo_zipped_txt_data-{int(time.time() * 10e3 )}' UpperCamelCase__ = F'{CI_HUB_USER}/{repo_name}' hf_api.create_repo(_UpperCamelCase , token=_UpperCamelCase , repo_type="dataset" , private=_UpperCamelCase ) hf_api.upload_file( token=_UpperCamelCase , path_or_fileobj=str(_UpperCamelCase ) , path_in_repo="data.zip" , repo_id=_UpperCamelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_UpperCamelCase , token=_UpperCamelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] ) -> Optional[int]: '''simple docstring''' return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="session" ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : HfApi , _UpperCamelCase : Optional[int] , _UpperCamelCase : Tuple ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ = F'repo_zipped_img_data-{int(time.time() * 10e3 )}' UpperCamelCase__ = F'{CI_HUB_USER}/{repo_name}' hf_api.create_repo(_UpperCamelCase , token=_UpperCamelCase , repo_type="dataset" , private=_UpperCamelCase ) hf_api.upload_file( token=_UpperCamelCase , path_or_fileobj=str(_UpperCamelCase ) , path_in_repo="data.zip" , repo_id=_UpperCamelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_UpperCamelCase , token=_UpperCamelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] ) -> List[Any]: '''simple docstring''' return hf_private_dataset_repo_zipped_img_data_
31
'''simple docstring''' import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __lowercase: Optional[List[str]] = None __lowercase: List[Any] = "<" if sys.byteorder == "little" else ">" # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __lowercase: Tuple = [ np.dtype("|b1"), np.dtype("|u1"), np.dtype("<u2"), np.dtype(">u2"), np.dtype("<i2"), np.dtype(">i2"), np.dtype("<u4"), np.dtype(">u4"), np.dtype("<i4"), np.dtype(">i4"), np.dtype("<f4"), np.dtype(">f4"), np.dtype("<f8"), np.dtype(">f8"), ] @dataclass class UpperCAmelCase : _lowerCamelCase : bool = True _lowerCamelCase : Optional[str] = None # Automatically constructed _lowerCamelCase : ClassVar[str] = "PIL.Image.Image" _lowerCamelCase : ClassVar[Any] = pa.struct({'bytes': pa.binary(), 'path': pa.string()}) _lowerCamelCase : str = field(default='Image' , init=SCREAMING_SNAKE_CASE__ , repr=SCREAMING_SNAKE_CASE__) def __call__( self : Union[str, Any] ): """simple docstring""" return self.pa_type def lowercase_ ( self : Optional[Any], a_ : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if isinstance(a_, a_ ): UpperCamelCase__ = np.array(a_ ) if isinstance(a_, a_ ): return {"path": value, "bytes": None} elif isinstance(a_, a_ ): return {"path": None, "bytes": value} elif isinstance(a_, np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(a_ ) elif isinstance(a_, PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(a_ ) elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.' ) def lowercase_ ( self : Dict, a_ : dict, a_ : Dict=None ): """simple docstring""" if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead." ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support decoding images, please install 'Pillow'." ) if token_per_repo_id is None: UpperCamelCase__ = {} UpperCamelCase__ , UpperCamelCase__ = value["path"], value["bytes"] if bytes_ is None: if path is None: raise ValueError(f'An image should have one of \'path\' or \'bytes\' but both are None in {value}.' ) else: if is_local_path(a_ ): UpperCamelCase__ = PIL.Image.open(a_ ) else: UpperCamelCase__ = path.split("::" )[-1] try: UpperCamelCase__ = string_to_dict(a_, config.HUB_DATASETS_URL )["repo_id"] UpperCamelCase__ = token_per_repo_id.get(a_ ) except ValueError: UpperCamelCase__ = None with xopen(a_, "rb", use_auth_token=a_ ) as f: UpperCamelCase__ = BytesIO(f.read() ) UpperCamelCase__ = PIL.Image.open(bytes_ ) else: UpperCamelCase__ = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def lowercase_ ( self : List[str] ): """simple docstring""" from .features import Value return ( self if self.decode else { "bytes": Value("binary" ), "path": Value("string" ), } ) def lowercase_ ( self : List[Any], a_ : Union[pa.StringArray, pa.StructArray, pa.ListArray] ): """simple docstring""" if pa.types.is_string(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: UpperCamelCase__ = storage.field("bytes" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: UpperCamelCase__ = storage.field("path" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCamelCase__ = pa.array( [encode_np_array(np.array(a_ ) )["bytes"] if arr is not None else None for arr in storage.to_pylist()], type=pa.binary(), ) UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays( [bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def lowercase_ ( self : str, a_ : pa.StructArray ): """simple docstring""" @no_op_if_value_is_null def path_to_bytes(a_ : Dict ): with xopen(a_, "rb" ) as f: UpperCamelCase__ = f.read() return bytes_ UpperCamelCase__ = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) UpperCamelCase__ = pa.array( [os.path.basename(a_ ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def SCREAMING_SNAKE_CASE__( ) -> List[str]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() UpperCamelCase__ = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> bytes: '''simple docstring''' UpperCamelCase__ = BytesIO() if image.format in list_image_compression_formats(): UpperCamelCase__ = image.format else: UpperCamelCase__ = "PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF" image.save(_UpperCamelCase , format=_UpperCamelCase ) return buffer.getvalue() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> dict: '''simple docstring''' if hasattr(_UpperCamelCase , "filename" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray ) -> dict: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) UpperCamelCase__ = array.dtype UpperCamelCase__ = dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER UpperCamelCase__ = dtype.kind UpperCamelCase__ = dtype.itemsize UpperCamelCase__ = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCamelCase__ = np.dtype("|u1" ) if dtype_kind not in ["u", "i"]: raise TypeError( F'Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.' ) if dtype is not dest_dtype: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: UpperCamelCase__ = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: UpperCamelCase__ = dtype_byteorder + dtype_kind + str(_UpperCamelCase ) UpperCamelCase__ = np.dtype(_UpperCamelCase ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( F'Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}' ) UpperCamelCase__ = PIL.Image.fromarray(array.astype(_UpperCamelCase ) ) return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]] ) -> List[dict]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if objs: UpperCamelCase__ , UpperCamelCase__ = first_non_null_value(_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_UpperCamelCase , np.ndarray ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] elif isinstance(_UpperCamelCase , PIL.Image.Image ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] else: return objs else: return objs
31
1
'''simple docstring''' import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotSmallConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html __lowercase: Tuple = "platform" import jax import jax.numpy as jnp from transformers.models.blenderbot_small.modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, shift_tokens_right, ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict , _UpperCamelCase : str , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : List[Any]=None , _UpperCamelCase : Dict=None , _UpperCamelCase : int=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Tuple=None , ) -> List[str]: '''simple docstring''' if attention_mask is None: UpperCamelCase__ = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: UpperCamelCase__ = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: UpperCamelCase__ = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCamelCase__ = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCamelCase__ = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class UpperCAmelCase : def __init__( self : Optional[Any], a_ : List[Any], a_ : Union[str, Any]=13, a_ : Optional[Any]=7, a_ : Any=True, a_ : Optional[Any]=False, a_ : Union[str, Any]=99, a_ : int=16, a_ : List[str]=2, a_ : List[Any]=4, a_ : List[Any]=4, a_ : str="gelu", a_ : Any=0.1, a_ : Optional[int]=0.1, a_ : Optional[Any]=32, a_ : Optional[Any]=2, a_ : Tuple=1, a_ : List[str]=0, a_ : Dict=0.02, ): """simple docstring""" UpperCamelCase__ = parent UpperCamelCase__ = batch_size UpperCamelCase__ = seq_length UpperCamelCase__ = is_training 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__ = eos_token_id UpperCamelCase__ = pad_token_id UpperCamelCase__ = bos_token_id UpperCamelCase__ = initializer_range def lowercase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase__ = np.clip(ids_tensor([self.batch_size, self.seq_length - 1], self.vocab_size ), 3, self.vocab_size ) UpperCamelCase__ = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1), dtype=np.intaa )), -1 ) UpperCamelCase__ = shift_tokens_right(a_, 1, 2 ) UpperCamelCase__ = BlenderbotSmallConfig( vocab_size=self.vocab_size, d_model=self.hidden_size, encoder_layers=self.num_hidden_layers, decoder_layers=self.num_hidden_layers, encoder_attention_heads=self.num_attention_heads, decoder_attention_heads=self.num_attention_heads, encoder_ffn_dim=self.intermediate_size, decoder_ffn_dim=self.intermediate_size, dropout=self.hidden_dropout_prob, attention_dropout=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, eos_token_id=self.eos_token_id, bos_token_id=self.bos_token_id, pad_token_id=self.pad_token_id, initializer_range=self.initializer_range, use_cache=a_, ) UpperCamelCase__ = prepare_blenderbot_inputs_dict(a_, a_, a_ ) return config, inputs_dict def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ , UpperCamelCase__ = self.prepare_config_and_inputs() return config, inputs_dict def lowercase_ ( self : Optional[Any], a_ : int, a_ : Any, a_ : Optional[int] ): """simple docstring""" UpperCamelCase__ = 20 UpperCamelCase__ = model_class_name(a_ ) UpperCamelCase__ = model.encode(inputs_dict["input_ids"] ) UpperCamelCase__ , UpperCamelCase__ = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) UpperCamelCase__ = model.init_cache(decoder_input_ids.shape[0], a_, a_ ) UpperCamelCase__ = jnp.ones((decoder_input_ids.shape[0], max_decoder_length), dtype="i4" ) UpperCamelCase__ = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :], (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1), ) UpperCamelCase__ = model.decode( decoder_input_ids[:, :-1], a_, decoder_attention_mask=a_, past_key_values=a_, decoder_position_ids=a_, ) UpperCamelCase__ = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]], dtype="i4" ) UpperCamelCase__ = model.decode( decoder_input_ids[:, -1:], a_, decoder_attention_mask=a_, past_key_values=outputs_cache.past_key_values, decoder_position_ids=a_, ) UpperCamelCase__ = model.decode(a_, a_ ) UpperCamelCase__ = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3, msg=f'Max diff is {diff}' ) def lowercase_ ( self : List[Any], a_ : Optional[int], a_ : Optional[int], a_ : Optional[Any] ): """simple docstring""" UpperCamelCase__ = 20 UpperCamelCase__ = model_class_name(a_ ) UpperCamelCase__ = model.encode(inputs_dict["input_ids"] ) UpperCamelCase__ , UpperCamelCase__ = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) UpperCamelCase__ = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ], axis=-1, ) UpperCamelCase__ = model.init_cache(decoder_input_ids.shape[0], a_, a_ ) UpperCamelCase__ = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :], (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1), ) UpperCamelCase__ = model.decode( decoder_input_ids[:, :-1], a_, decoder_attention_mask=a_, past_key_values=a_, decoder_position_ids=a_, ) UpperCamelCase__ = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]], dtype="i4" ) UpperCamelCase__ = model.decode( decoder_input_ids[:, -1:], a_, past_key_values=outputs_cache.past_key_values, decoder_attention_mask=a_, decoder_position_ids=a_, ) UpperCamelCase__ = model.decode(a_, a_, decoder_attention_mask=a_ ) UpperCamelCase__ = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3, msg=f'Max diff is {diff}' ) @require_flax class UpperCAmelCase ( unittest.TestCase): _lowerCamelCase : Optional[Any] = 99 def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ], dtype=np.intaa, ) UpperCamelCase__ = input_ids.shape[0] UpperCamelCase__ = BlenderbotSmallConfig( vocab_size=self.vocab_size, d_model=24, encoder_layers=2, decoder_layers=2, encoder_attention_heads=2, decoder_attention_heads=2, encoder_ffn_dim=32, decoder_ffn_dim=32, max_position_embeddings=48, eos_token_id=2, pad_token_id=1, bos_token_id=0, ) return config, input_ids, batch_size def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_config_and_data() UpperCamelCase__ = FlaxBlenderbotSmallForConditionalGeneration(a_ ) UpperCamelCase__ = lm_model(input_ids=a_ ) UpperCamelCase__ = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs["logits"].shape, a_ ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = BlenderbotSmallConfig( vocab_size=self.vocab_size, d_model=14, encoder_layers=2, decoder_layers=2, encoder_attention_heads=2, decoder_attention_heads=2, encoder_ffn_dim=8, decoder_ffn_dim=8, max_position_embeddings=48, ) UpperCamelCase__ = FlaxBlenderbotSmallForConditionalGeneration(a_ ) UpperCamelCase__ = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]], dtype=np.intaa ) UpperCamelCase__ = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]], dtype=np.intaa ) UpperCamelCase__ = lm_model(input_ids=a_, decoder_input_ids=a_ ) UpperCamelCase__ = (*summary.shape, config.vocab_size) self.assertEqual(outputs["logits"].shape, a_ ) def lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]], dtype=np.intaa ) UpperCamelCase__ = shift_tokens_right(a_, 1, 2 ) UpperCamelCase__ = np.equal(a_, 1 ).astype(np.floataa ).sum() UpperCamelCase__ = np.equal(a_, 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape, input_ids.shape ) self.assertEqual(a_, n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0], 2 ).all() ) @require_flax class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase , SCREAMING_SNAKE_CASE__): _lowerCamelCase : Any = True _lowerCamelCase : Union[str, Any] = ( ( FlaxBlenderbotSmallModel, FlaxBlenderbotSmallForConditionalGeneration, ) if is_flax_available() else () ) _lowerCamelCase : List[Any] = (FlaxBlenderbotSmallForConditionalGeneration,) if is_flax_available() else () def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = FlaxBlenderbotSmallModelTester(self ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(a_, a_, a_ ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(a_, a_, a_ ) def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): UpperCamelCase__ = self._prepare_for_class(a_, a_ ) UpperCamelCase__ = model_class(a_ ) @jax.jit def encode_jitted(a_ : List[str], a_ : Optional[Any]=None, **a_ : Any ): return model.encode(input_ids=a_, attention_mask=a_ ) with self.subTest("JIT Enabled" ): UpperCamelCase__ = encode_jitted(**a_ ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): UpperCamelCase__ = encode_jitted(**a_ ).to_tuple() self.assertEqual(len(a_ ), len(a_ ) ) for jitted_output, output in zip(a_, a_ ): self.assertEqual(jitted_output.shape, output.shape ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): UpperCamelCase__ = model_class(a_ ) UpperCamelCase__ = model.encode(inputs_dict["input_ids"], inputs_dict["attention_mask"] ) UpperCamelCase__ = { "decoder_input_ids": inputs_dict["decoder_input_ids"], "decoder_attention_mask": inputs_dict["decoder_attention_mask"], "encoder_outputs": encoder_outputs, } @jax.jit def decode_jitted(a_ : List[Any], a_ : List[Any], a_ : Any ): return model.decode( decoder_input_ids=a_, decoder_attention_mask=a_, encoder_outputs=a_, ) with self.subTest("JIT Enabled" ): UpperCamelCase__ = decode_jitted(**a_ ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): UpperCamelCase__ = decode_jitted(**a_ ).to_tuple() self.assertEqual(len(a_ ), len(a_ ) ) for jitted_output, output in zip(a_, a_ ): self.assertEqual(jitted_output.shape, output.shape ) @slow def lowercase_ ( self : List[str] ): """simple docstring""" for model_class_name in self.all_model_classes: UpperCamelCase__ = model_class_name.from_pretrained("facebook/blenderbot_small-90M" ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids UpperCamelCase__ = np.ones((1, 1) ) * model.config.eos_token_id UpperCamelCase__ = model(a_ ) self.assertIsNotNone(a_ )
31
'''simple docstring''' import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Union[str, Any] = CLIPTokenizer _lowerCamelCase : Dict = CLIPTokenizerFast _lowerCamelCase : int = True _lowerCamelCase : Tuple = {} _lowerCamelCase : Tuple = False def lowercase_ ( self : Tuple ): """simple docstring""" super().setUp() # fmt: off UpperCamelCase__ = ["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 UpperCamelCase__ = dict(zip(a_, range(len(a_ ) ) ) ) UpperCamelCase__ = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] UpperCamelCase__ = {"unk_token": "<unk>"} UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as fp: fp.write(json.dumps(a_ ) + "\n" ) with open(self.merges_file, "w", encoding="utf-8" ) as fp: fp.write("\n".join(a_ ) ) def lowercase_ ( self : Optional[Any], **a_ : str ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : str, **a_ : str ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : List[Any], a_ : Dict ): """simple docstring""" UpperCamelCase__ = "lower newer" UpperCamelCase__ = "lower newer" return input_text, output_text def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = CLIPTokenizer(self.vocab_file, self.merges_file, **self.special_tokens_map ) UpperCamelCase__ = "lower newer" UpperCamelCase__ = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] UpperCamelCase__ = tokenizer.tokenize(a_ ) self.assertListEqual(a_, a_ ) UpperCamelCase__ = tokens + [tokenizer.unk_token] UpperCamelCase__ = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(a_ ), a_ ) @require_ftfy def lowercase_ ( self : Dict ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCamelCase__ = self.tokenizer_class.from_pretrained(a_, **a_ ) UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained(a_, **a_ ) UpperCamelCase__ = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways UpperCamelCase__ = "xa\u0303y" + " " + "x\xe3y" UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on unicode of space type UpperCamelCase__ = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on unicode of line break type UpperCamelCase__ = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) def lowercase_ ( self : Tuple ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCamelCase__ = "hello" # `hello` is a token in the vocabulary of `pretrained_name` UpperCamelCase__ = f'{text_of_1_token} {text_of_1_token}' UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained( a_, use_fast=a_, ) UpperCamelCase__ = tokenizer_r(a_, return_offsets_mapping=a_, add_special_tokens=a_ ) self.assertEqual(encoding.offset_mapping[0], (0, len(a_ )) ) self.assertEqual( encoding.offset_mapping[1], (len(a_ ) + 1, len(a_ ) + 1 + len(a_ )), ) UpperCamelCase__ = f' {text}' UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained( a_, use_fast=a_, ) UpperCamelCase__ = tokenizer_r(a_, return_offsets_mapping=a_, add_special_tokens=a_ ) self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(a_ )) ) self.assertEqual( encoding.offset_mapping[1], (1 + len(a_ ) + 1, 1 + len(a_ ) + 1 + len(a_ )), ) def lowercase_ ( self : Tuple ): """simple docstring""" with self.assertRaises(a_ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def lowercase_ ( self : Union[str, Any] ): """simple docstring""" super().test_tokenization_python_rust_equals() def lowercase_ ( self : List[str] ): """simple docstring""" pass
31
1
'''simple docstring''' import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __lowercase: Optional[List[str]] = None __lowercase: List[Any] = "<" if sys.byteorder == "little" else ">" # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __lowercase: Tuple = [ np.dtype("|b1"), np.dtype("|u1"), np.dtype("<u2"), np.dtype(">u2"), np.dtype("<i2"), np.dtype(">i2"), np.dtype("<u4"), np.dtype(">u4"), np.dtype("<i4"), np.dtype(">i4"), np.dtype("<f4"), np.dtype(">f4"), np.dtype("<f8"), np.dtype(">f8"), ] @dataclass class UpperCAmelCase : _lowerCamelCase : bool = True _lowerCamelCase : Optional[str] = None # Automatically constructed _lowerCamelCase : ClassVar[str] = "PIL.Image.Image" _lowerCamelCase : ClassVar[Any] = pa.struct({'bytes': pa.binary(), 'path': pa.string()}) _lowerCamelCase : str = field(default='Image' , init=SCREAMING_SNAKE_CASE__ , repr=SCREAMING_SNAKE_CASE__) def __call__( self : Union[str, Any] ): """simple docstring""" return self.pa_type def lowercase_ ( self : Optional[Any], a_ : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if isinstance(a_, a_ ): UpperCamelCase__ = np.array(a_ ) if isinstance(a_, a_ ): return {"path": value, "bytes": None} elif isinstance(a_, a_ ): return {"path": None, "bytes": value} elif isinstance(a_, np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(a_ ) elif isinstance(a_, PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(a_ ) elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.' ) def lowercase_ ( self : Dict, a_ : dict, a_ : Dict=None ): """simple docstring""" if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead." ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support decoding images, please install 'Pillow'." ) if token_per_repo_id is None: UpperCamelCase__ = {} UpperCamelCase__ , UpperCamelCase__ = value["path"], value["bytes"] if bytes_ is None: if path is None: raise ValueError(f'An image should have one of \'path\' or \'bytes\' but both are None in {value}.' ) else: if is_local_path(a_ ): UpperCamelCase__ = PIL.Image.open(a_ ) else: UpperCamelCase__ = path.split("::" )[-1] try: UpperCamelCase__ = string_to_dict(a_, config.HUB_DATASETS_URL )["repo_id"] UpperCamelCase__ = token_per_repo_id.get(a_ ) except ValueError: UpperCamelCase__ = None with xopen(a_, "rb", use_auth_token=a_ ) as f: UpperCamelCase__ = BytesIO(f.read() ) UpperCamelCase__ = PIL.Image.open(bytes_ ) else: UpperCamelCase__ = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def lowercase_ ( self : List[str] ): """simple docstring""" from .features import Value return ( self if self.decode else { "bytes": Value("binary" ), "path": Value("string" ), } ) def lowercase_ ( self : List[Any], a_ : Union[pa.StringArray, pa.StructArray, pa.ListArray] ): """simple docstring""" if pa.types.is_string(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: UpperCamelCase__ = storage.field("bytes" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: UpperCamelCase__ = storage.field("path" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCamelCase__ = pa.array( [encode_np_array(np.array(a_ ) )["bytes"] if arr is not None else None for arr in storage.to_pylist()], type=pa.binary(), ) UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays( [bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def lowercase_ ( self : str, a_ : pa.StructArray ): """simple docstring""" @no_op_if_value_is_null def path_to_bytes(a_ : Dict ): with xopen(a_, "rb" ) as f: UpperCamelCase__ = f.read() return bytes_ UpperCamelCase__ = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) UpperCamelCase__ = pa.array( [os.path.basename(a_ ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def SCREAMING_SNAKE_CASE__( ) -> List[str]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() UpperCamelCase__ = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> bytes: '''simple docstring''' UpperCamelCase__ = BytesIO() if image.format in list_image_compression_formats(): UpperCamelCase__ = image.format else: UpperCamelCase__ = "PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF" image.save(_UpperCamelCase , format=_UpperCamelCase ) return buffer.getvalue() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> dict: '''simple docstring''' if hasattr(_UpperCamelCase , "filename" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray ) -> dict: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) UpperCamelCase__ = array.dtype UpperCamelCase__ = dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER UpperCamelCase__ = dtype.kind UpperCamelCase__ = dtype.itemsize UpperCamelCase__ = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCamelCase__ = np.dtype("|u1" ) if dtype_kind not in ["u", "i"]: raise TypeError( F'Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.' ) if dtype is not dest_dtype: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: UpperCamelCase__ = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: UpperCamelCase__ = dtype_byteorder + dtype_kind + str(_UpperCamelCase ) UpperCamelCase__ = np.dtype(_UpperCamelCase ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( F'Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}' ) UpperCamelCase__ = PIL.Image.fromarray(array.astype(_UpperCamelCase ) ) return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]] ) -> List[dict]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if objs: UpperCamelCase__ , UpperCamelCase__ = first_non_null_value(_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_UpperCamelCase , np.ndarray ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] elif isinstance(_UpperCamelCase , PIL.Image.Image ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] else: return objs else: return objs
31
'''simple docstring''' import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __lowercase: Any = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.14.0", "To fix: pip install -r examples/pytorch/audio-classification/requirements.txt") def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : float , _UpperCamelCase : int = 1_60_00 ) -> str: '''simple docstring''' UpperCamelCase__ = int(round(sample_rate * max_length ) ) if len(_UpperCamelCase ) <= sample_length: return wav UpperCamelCase__ = randint(0 , len(_UpperCamelCase ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class UpperCAmelCase : _lowerCamelCase : Optional[str] = field(default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Name of a dataset from the datasets package'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'A file containing the training audio paths and labels.'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'A file containing the validation audio paths and labels.'}) _lowerCamelCase : str = field( default='train' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) _lowerCamelCase : str = field( default='validation' , metadata={ 'help': ( 'The name of the training data set split to use (via the datasets library). Defaults to \'validation\'' ) } , ) _lowerCamelCase : str = field( default='audio' , metadata={'help': 'The name of the dataset column containing the audio data. Defaults to \'audio\''} , ) _lowerCamelCase : str = field( default='label' , metadata={'help': 'The name of the dataset column containing the labels. Defaults to \'label\''}) _lowerCamelCase : Optional[int] = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) _lowerCamelCase : Optional[int] = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) _lowerCamelCase : float = field( default=20 , metadata={'help': 'Audio clips will be randomly cut to this length during training if the value is set.'} , ) @dataclass class UpperCAmelCase : _lowerCamelCase : str = field( default='facebook/wav2vec2-base' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Pretrained config name or path if not the same as model_name'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from the Hub'}) _lowerCamelCase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Name or path of preprocessor config.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to freeze the feature encoder layers of the model.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to generate an attention mask in the feature extractor.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) _lowerCamelCase : Optional[bool] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def lowercase_ ( self : int ): """simple docstring""" if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( "The argument `--freeze_feature_extractor` is deprecated and " "will be removed in a future version. Use `--freeze_feature_encoder`" "instead. Setting `freeze_feature_encoder==True`.", a_, ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( "The argument `--freeze_feature_extractor` is deprecated and " "should not be used in combination with `--freeze_feature_encoder`." "Only make use of `--freeze_feature_encoder`." ) def SCREAMING_SNAKE_CASE__( ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_audio_classification" , _UpperCamelCase , _UpperCamelCase ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} ' + F'distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}' ) logger.info(F'Training/evaluation parameters {training_args}' ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. UpperCamelCase__ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'Output directory ({training_args.output_dir}) already exists and is not empty. ' "Use --overwrite_output_dir to train from scratch." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Initialize our dataset and prepare it for the audio classification task. UpperCamelCase__ = DatasetDict() UpperCamelCase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--audio_column_name {data_args.audio_column_name} not found in dataset \'{data_args.dataset_name}\'. ' "Make sure to set `--audio_column_name` to the correct audio column - one of " F'{", ".join(raw_datasets["train"].column_names )}.' ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--label_column_name {data_args.label_column_name} not found in dataset \'{data_args.dataset_name}\'. ' "Make sure to set `--label_column_name` to the correct text column - one of " F'{", ".join(raw_datasets["train"].column_names )}.' ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy UpperCamelCase__ = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. UpperCamelCase__ = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) UpperCamelCase__ = feature_extractor.model_input_names[0] def train_transforms(_UpperCamelCase : Any ): UpperCamelCase__ = [] for audio in batch[data_args.audio_column_name]: UpperCamelCase__ = random_subsample( audio["array"] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(_UpperCamelCase ) UpperCamelCase__ = feature_extractor(_UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCamelCase__ = {model_input_name: inputs.get(_UpperCamelCase )} UpperCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(_UpperCamelCase : List[Any] ): UpperCamelCase__ = [audio["array"] for audio in batch[data_args.audio_column_name]] UpperCamelCase__ = feature_extractor(_UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCamelCase__ = {model_input_name: inputs.get(_UpperCamelCase )} UpperCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. UpperCamelCase__ = raw_datasets["train"].features[data_args.label_column_name].names UpperCamelCase__ , UpperCamelCase__ = {}, {} for i, label in enumerate(_UpperCamelCase ): UpperCamelCase__ = str(_UpperCamelCase ) UpperCamelCase__ = label # Load the accuracy metric from the datasets package UpperCamelCase__ = evaluate.load("accuracy" ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Any ): UpperCamelCase__ = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=_UpperCamelCase , references=eval_pred.label_ids ) UpperCamelCase__ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(_UpperCamelCase ) , labelaid=_UpperCamelCase , idalabel=_UpperCamelCase , finetuning_task="audio-classification" , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=_UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: UpperCamelCase__ = ( raw_datasets["train"].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(_UpperCamelCase , output_all_columns=_UpperCamelCase ) if training_args.do_eval: if data_args.max_eval_samples is not None: UpperCamelCase__ = ( raw_datasets["eval"].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(_UpperCamelCase , output_all_columns=_UpperCamelCase ) # Initialize our trainer UpperCamelCase__ = Trainer( model=_UpperCamelCase , args=_UpperCamelCase , train_dataset=raw_datasets["train"] if training_args.do_train else None , eval_dataset=raw_datasets["eval"] if training_args.do_eval else None , compute_metrics=_UpperCamelCase , tokenizer=_UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase__ = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ = last_checkpoint UpperCamelCase__ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics("train" , train_result.metrics ) trainer.save_metrics("train" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ = trainer.evaluate() trainer.log_metrics("eval" , _UpperCamelCase ) trainer.save_metrics("eval" , _UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase__ = { "finetuned_from": model_args.model_name_or_path, "tasks": "audio-classification", "dataset": data_args.dataset_name, "tags": ["audio-classification"], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) __lowercase: Any = {"configuration_beit": ["BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BeitConfig", "BeitOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Optional[int] = ["BeitFeatureExtractor"] __lowercase: str = ["BeitImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Tuple = [ "BEIT_PRETRAINED_MODEL_ARCHIVE_LIST", "BeitForImageClassification", "BeitForMaskedImageModeling", "BeitForSemanticSegmentation", "BeitModel", "BeitPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: str = [ "FlaxBeitForImageClassification", "FlaxBeitForMaskedImageModeling", "FlaxBeitModel", "FlaxBeitPreTrainedModel", ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys __lowercase: int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
31
'''simple docstring''' import math import sys def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = "" try: with open(_UpperCamelCase , "rb" ) as binary_file: UpperCamelCase__ = binary_file.read() for dat in data: UpperCamelCase__ = F'{dat:08b}' result += curr_byte return result except OSError: print("File not accessible" ) sys.exit() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = {"0": "0", "1": "1"} UpperCamelCase__ , UpperCamelCase__ = "", "" UpperCamelCase__ = len(_UpperCamelCase ) for i in range(len(_UpperCamelCase ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue UpperCamelCase__ = lexicon[curr_string] result += last_match_id UpperCamelCase__ = last_match_id + "0" if math.loga(_UpperCamelCase ).is_integer(): UpperCamelCase__ = {} for curr_key in list(_UpperCamelCase ): UpperCamelCase__ = lexicon.pop(_UpperCamelCase ) UpperCamelCase__ = new_lex UpperCamelCase__ = last_match_id + "1" index += 1 UpperCamelCase__ = "" return result def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str ) -> None: '''simple docstring''' UpperCamelCase__ = 8 try: with open(_UpperCamelCase , "wb" ) as opened_file: UpperCamelCase__ = [ to_write[i : i + byte_length] for i in range(0 , len(_UpperCamelCase ) , _UpperCamelCase ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("10000000" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(_UpperCamelCase , 2 ).to_bytes(1 , byteorder="big" ) ) except OSError: print("File not accessible" ) sys.exit() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = 0 for letter in data_bits: if letter == "1": break counter += 1 UpperCamelCase__ = data_bits[counter:] UpperCamelCase__ = data_bits[counter + 1 :] return data_bits def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str ) -> None: '''simple docstring''' UpperCamelCase__ = read_file_binary(_UpperCamelCase ) UpperCamelCase__ = remove_prefix(_UpperCamelCase ) UpperCamelCase__ = decompress_data(_UpperCamelCase ) write_file_binary(_UpperCamelCase , _UpperCamelCase ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
31
1
'''simple docstring''' import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : Dict=5 ) -> Optional[int]: '''simple docstring''' assert masked_input.count("<mask>" ) == 1 UpperCamelCase__ = torch.tensor(tokenizer.encode(_UpperCamelCase , add_special_tokens=_UpperCamelCase ) ).unsqueeze(0 ) # Batch size 1 UpperCamelCase__ = model(_UpperCamelCase )[0] # The last hidden-state is the first element of the output tuple UpperCamelCase__ = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() UpperCamelCase__ = logits[0, masked_index, :] UpperCamelCase__ = logits.softmax(dim=0 ) UpperCamelCase__ , UpperCamelCase__ = prob.topk(k=_UpperCamelCase , dim=0 ) UpperCamelCase__ = " ".join( [tokenizer.convert_ids_to_tokens(indices[i].item() ) for i in range(len(_UpperCamelCase ) )] ) UpperCamelCase__ = tokenizer.mask_token UpperCamelCase__ = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(" " ) ): UpperCamelCase__ = predicted_token_bpe.replace("\u2581" , " " ) if " {0}".format(_UpperCamelCase ) in masked_input: topk_filled_outputs.append( ( masked_input.replace(" {0}".format(_UpperCamelCase ) , _UpperCamelCase ), values[index].item(), predicted_token, ) ) else: topk_filled_outputs.append( ( masked_input.replace(_UpperCamelCase , _UpperCamelCase ), values[index].item(), predicted_token, ) ) return topk_filled_outputs __lowercase: str = CamembertTokenizer.from_pretrained("camembert-base") __lowercase: List[str] = CamembertForMaskedLM.from_pretrained("camembert-base") model.eval() __lowercase: Optional[Any] = "Le camembert est <mask> :)" print(fill_mask(masked_input, model, tokenizer, topk=3))
31
'''simple docstring''' import inspect from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel, VQModel from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : Any, a_ : VQModel, a_ : UNetaDModel, a_ : DDIMScheduler ): """simple docstring""" super().__init__() self.register_modules(vqvae=a_, unet=a_, scheduler=a_ ) @torch.no_grad() def __call__( self : Union[str, Any], a_ : int = 1, a_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None, a_ : float = 0.0, a_ : int = 50, a_ : Optional[str] = "pil", a_ : bool = True, **a_ : Tuple, ): """simple docstring""" UpperCamelCase__ = randn_tensor( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size), generator=a_, ) UpperCamelCase__ = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler UpperCamelCase__ = latents * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(a_ ) # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature UpperCamelCase__ = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) UpperCamelCase__ = {} if accepts_eta: UpperCamelCase__ = eta for t in self.progress_bar(self.scheduler.timesteps ): UpperCamelCase__ = self.scheduler.scale_model_input(a_, a_ ) # predict the noise residual UpperCamelCase__ = self.unet(a_, a_ ).sample # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase__ = self.scheduler.step(a_, a_, a_, **a_ ).prev_sample # decode the image latents with the VAE UpperCamelCase__ = self.vqvae.decode(a_ ).sample UpperCamelCase__ = (image / 2 + 0.5).clamp(0, 1 ) UpperCamelCase__ = image.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": UpperCamelCase__ = self.numpy_to_pil(a_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a_ )
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable __lowercase: List[Any] = {"configuration_gpt_neox": ["GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTNeoXConfig"]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Any = ["GPTNeoXTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Union[str, Any] = [ "GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTNeoXForCausalLM", "GPTNeoXForQuestionAnswering", "GPTNeoXForSequenceClassification", "GPTNeoXForTokenClassification", "GPTNeoXLayer", "GPTNeoXModel", "GPTNeoXPreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys __lowercase: Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
31
'''simple docstring''' import argparse import json import subprocess def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : Tuple ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = [] UpperCamelCase__ = ( F'curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer {token}"' " https://api.github.com/repos/huggingface/transformers/actions/runners" ) UpperCamelCase__ = subprocess.run(_UpperCamelCase , shell=_UpperCamelCase , stdout=subprocess.PIPE ) UpperCamelCase__ = output.stdout.decode("utf-8" ) UpperCamelCase__ = json.loads(_UpperCamelCase ) UpperCamelCase__ = status["runners"] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_UpperCamelCase ) # save the result so we can report them on Slack with open("offline_runners.txt" , "w" ) as fp: fp.write(json.dumps(_UpperCamelCase ) ) if len(_UpperCamelCase ) > 0: UpperCamelCase__ = "\n".join([x["name"] for x in offline_runners] ) raise ValueError(F'The following runners are offline:\n{failed}' ) if __name__ == "__main__": def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' return values.split("," ) __lowercase: str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--target_runners", default=None, type=list_str, required=True, help="Comma-separated list of runners to check status.", ) parser.add_argument( "--token", default=None, type=str, required=True, help="A token that has actions:read permission." ) __lowercase: str = parser.parse_args() get_runner_status(args.target_runners, args.token)
31
1
'''simple docstring''' from typing import List, Optional, TypeVar from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .info import DatasetInfo from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets from .splits import NamedSplit from .utils import logging from .utils.py_utils import Literal __lowercase: str = logging.get_logger(__name__) __lowercase: Tuple = TypeVar("DatasetType", Dataset, IterableDataset) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[DatasetType] , _UpperCamelCase : Optional[List[float]] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[DatasetInfo] = None , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : Literal["first_exhausted", "all_exhausted"] = "first_exhausted" , ) -> DatasetType: '''simple docstring''' from .arrow_dataset import Dataset from .iterable_dataset import IterableDataset if not datasets: raise ValueError("Unable to interleave an empty list of datasets." ) for i, dataset in enumerate(_UpperCamelCase ): if not isinstance(_UpperCamelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCamelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} ' "is an empty dataset dictionary." ) raise ValueError( F'Dataset at position {i} has at least one split: {list(_UpperCamelCase )}\n' F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(_UpperCamelCase ) )}\']' ) raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase ).__name__}.' ) if i == 0: UpperCamelCase__ , UpperCamelCase__ = ( (Dataset, IterableDataset) if isinstance(_UpperCamelCase , _UpperCamelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError( F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.' ) if stopping_strategy not in ["first_exhausted", "all_exhausted"]: raise ValueError(F'{stopping_strategy} is not supported. Please enter a valid stopping_strategy.' ) if dataset_type is Dataset: return _interleave_map_style_datasets( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , stopping_strategy=_UpperCamelCase ) else: return _interleave_iterable_datasets( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , stopping_strategy=_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[DatasetType] , _UpperCamelCase : Optional[DatasetInfo] = None , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : int = 0 , ) -> DatasetType: '''simple docstring''' if not dsets: raise ValueError("Unable to concatenate an empty list of datasets." ) for i, dataset in enumerate(_UpperCamelCase ): if not isinstance(_UpperCamelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCamelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} ' "is an empty dataset dictionary." ) raise ValueError( F'Dataset at position {i} has at least one split: {list(_UpperCamelCase )}\n' F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(_UpperCamelCase ) )}\']' ) raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase ).__name__}.' ) if i == 0: UpperCamelCase__ , UpperCamelCase__ = ( (Dataset, IterableDataset) if isinstance(_UpperCamelCase , _UpperCamelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError( F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.' ) if dataset_type is Dataset: return _concatenate_map_style_datasets(_UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , axis=_UpperCamelCase ) else: return _concatenate_iterable_datasets(_UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , axis=_UpperCamelCase )
31
'''simple docstring''' from typing import List, Optional, TypeVar from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .info import DatasetInfo from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets from .splits import NamedSplit from .utils import logging from .utils.py_utils import Literal __lowercase: str = logging.get_logger(__name__) __lowercase: Tuple = TypeVar("DatasetType", Dataset, IterableDataset) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[DatasetType] , _UpperCamelCase : Optional[List[float]] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[DatasetInfo] = None , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : Literal["first_exhausted", "all_exhausted"] = "first_exhausted" , ) -> DatasetType: '''simple docstring''' from .arrow_dataset import Dataset from .iterable_dataset import IterableDataset if not datasets: raise ValueError("Unable to interleave an empty list of datasets." ) for i, dataset in enumerate(_UpperCamelCase ): if not isinstance(_UpperCamelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCamelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} ' "is an empty dataset dictionary." ) raise ValueError( F'Dataset at position {i} has at least one split: {list(_UpperCamelCase )}\n' F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(_UpperCamelCase ) )}\']' ) raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase ).__name__}.' ) if i == 0: UpperCamelCase__ , UpperCamelCase__ = ( (Dataset, IterableDataset) if isinstance(_UpperCamelCase , _UpperCamelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError( F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.' ) if stopping_strategy not in ["first_exhausted", "all_exhausted"]: raise ValueError(F'{stopping_strategy} is not supported. Please enter a valid stopping_strategy.' ) if dataset_type is Dataset: return _interleave_map_style_datasets( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , stopping_strategy=_UpperCamelCase ) else: return _interleave_iterable_datasets( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , stopping_strategy=_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[DatasetType] , _UpperCamelCase : Optional[DatasetInfo] = None , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : int = 0 , ) -> DatasetType: '''simple docstring''' if not dsets: raise ValueError("Unable to concatenate an empty list of datasets." ) for i, dataset in enumerate(_UpperCamelCase ): if not isinstance(_UpperCamelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCamelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} ' "is an empty dataset dictionary." ) raise ValueError( F'Dataset at position {i} has at least one split: {list(_UpperCamelCase )}\n' F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(_UpperCamelCase ) )}\']' ) raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase ).__name__}.' ) if i == 0: UpperCamelCase__ , UpperCamelCase__ = ( (Dataset, IterableDataset) if isinstance(_UpperCamelCase , _UpperCamelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError( F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.' ) if dataset_type is Dataset: return _concatenate_map_style_datasets(_UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , axis=_UpperCamelCase ) else: return _concatenate_iterable_datasets(_UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , axis=_UpperCamelCase )
31
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__( ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ = 0 for i in range(1 , 10_01 ): total += i**i return str(_UpperCamelCase )[-10:] if __name__ == "__main__": print(solution())
31
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list ) -> float: '''simple docstring''' UpperCamelCase__ = 0 while len(_UpperCamelCase ) > 1: UpperCamelCase__ = 0 # Consider two files with minimum cost to be merged for _ in range(2 ): UpperCamelCase__ = files.index(min(_UpperCamelCase ) ) temp += files[min_index] files.pop(_UpperCamelCase ) files.append(_UpperCamelCase ) optimal_merge_cost += temp return optimal_merge_cost if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' from typing import List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase: Optional[Any] = logging.get_logger(__name__) __lowercase: int = { "huggingface/autoformer-tourism-monthly": "https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json", } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : List[Any] = 'autoformer' _lowerCamelCase : Dict = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', 'num_hidden_layers': 'encoder_layers', } def __init__( self : Dict, a_ : Optional[int] = None, a_ : Optional[int] = None, a_ : str = "student_t", a_ : str = "nll", a_ : int = 1, a_ : List[int] = [1, 2, 3, 4, 5, 6, 7], a_ : bool = True, a_ : int = 0, a_ : int = 0, a_ : int = 0, a_ : int = 0, a_ : Optional[List[int]] = None, a_ : Optional[List[int]] = None, a_ : int = 64, a_ : int = 2, a_ : int = 2, a_ : int = 2, a_ : int = 2, a_ : int = 32, a_ : int = 32, a_ : str = "gelu", a_ : float = 0.1, a_ : float = 0.1, a_ : float = 0.1, a_ : float = 0.1, a_ : float = 0.1, a_ : int = 100, a_ : float = 0.02, a_ : bool = True, a_ : List[Any]=True, a_ : int = 10, a_ : int = 25, a_ : int = 3, **a_ : Optional[int], ): """simple docstring""" UpperCamelCase__ = prediction_length UpperCamelCase__ = context_length if context_length is not None else prediction_length UpperCamelCase__ = distribution_output UpperCamelCase__ = loss UpperCamelCase__ = input_size UpperCamelCase__ = num_time_features UpperCamelCase__ = lags_sequence UpperCamelCase__ = scaling UpperCamelCase__ = num_dynamic_real_features UpperCamelCase__ = num_static_real_features UpperCamelCase__ = num_static_categorical_features if cardinality is not None and num_static_categorical_features > 0: if len(a_ ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) UpperCamelCase__ = cardinality else: UpperCamelCase__ = [0] if embedding_dimension is not None and num_static_categorical_features > 0: if len(a_ ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) UpperCamelCase__ = embedding_dimension else: UpperCamelCase__ = [min(50, (cat + 1) // 2 ) for cat in self.cardinality] UpperCamelCase__ = num_parallel_samples # Transformer architecture configuration UpperCamelCase__ = input_size * len(self.lags_sequence ) + self._number_of_features UpperCamelCase__ = d_model UpperCamelCase__ = encoder_attention_heads UpperCamelCase__ = decoder_attention_heads UpperCamelCase__ = encoder_ffn_dim UpperCamelCase__ = decoder_ffn_dim UpperCamelCase__ = encoder_layers UpperCamelCase__ = decoder_layers UpperCamelCase__ = dropout UpperCamelCase__ = attention_dropout UpperCamelCase__ = activation_dropout UpperCamelCase__ = encoder_layerdrop UpperCamelCase__ = decoder_layerdrop UpperCamelCase__ = activation_function UpperCamelCase__ = init_std UpperCamelCase__ = use_cache # Autoformer UpperCamelCase__ = label_length UpperCamelCase__ = moving_average UpperCamelCase__ = autocorrelation_factor super().__init__(is_encoder_decoder=a_, **a_ ) @property def lowercase_ ( self : Any ): """simple docstring""" return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list[int | str] ) -> None: '''simple docstring''' create_state_space_tree(_UpperCamelCase , [] , 0 , [0 for i in range(len(_UpperCamelCase ) )] ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list[int | str] , _UpperCamelCase : list[int | str] , _UpperCamelCase : int , _UpperCamelCase : list[int] , ) -> None: '''simple docstring''' if index == len(_UpperCamelCase ): print(_UpperCamelCase ) return for i in range(len(_UpperCamelCase ) ): if not index_used[i]: current_sequence.append(sequence[i] ) UpperCamelCase__ = True create_state_space_tree(_UpperCamelCase , _UpperCamelCase , index + 1 , _UpperCamelCase ) current_sequence.pop() UpperCamelCase__ = False __lowercase: list[int | str] = [3, 1, 2, 4] generate_all_permutations(sequence) __lowercase: list[int | str] = ["A", "B", "C"] generate_all_permutations(sequence_a)
31
1
'''simple docstring''' from __future__ import annotations import typing from collections import Counter def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> typing.Counter[int]: '''simple docstring''' UpperCamelCase__ = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(_UpperCamelCase , max_perimeter + 1 ): UpperCamelCase__ = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(_UpperCamelCase ): UpperCamelCase__ = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int = 10_00 ) -> int: '''simple docstring''' UpperCamelCase__ = pythagorean_triple(_UpperCamelCase ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(F"""Perimeter {solution()} has maximum solutions""")
31
'''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 __lowercase: int = logging.get_logger(__name__) __lowercase: str = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : List[str] = 'yolos' def __init__( self : List[str], a_ : Optional[int]=768, a_ : Optional[int]=12, a_ : Any=12, a_ : List[str]=3072, a_ : Any="gelu", a_ : int=0.0, a_ : List[Any]=0.0, a_ : Dict=0.02, a_ : Optional[int]=1e-1_2, a_ : List[Any]=[512, 864], a_ : Any=16, a_ : Any=3, a_ : Tuple=True, a_ : List[str]=100, a_ : Union[str, Any]=True, a_ : Any=False, a_ : List[str]=1, a_ : Tuple=5, a_ : Union[str, Any]=2, a_ : int=5, a_ : Union[str, Any]=2, a_ : Dict=0.1, **a_ : Dict, ): """simple docstring""" super().__init__(**a_ ) 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__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = image_size UpperCamelCase__ = patch_size UpperCamelCase__ = num_channels UpperCamelCase__ = qkv_bias UpperCamelCase__ = num_detection_tokens UpperCamelCase__ = use_mid_position_embeddings UpperCamelCase__ = auxiliary_loss # Hungarian matcher UpperCamelCase__ = class_cost UpperCamelCase__ = bbox_cost UpperCamelCase__ = giou_cost # Loss coefficients UpperCamelCase__ = bbox_loss_coefficient UpperCamelCase__ = giou_loss_coefficient UpperCamelCase__ = eos_coefficient class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Union[str, Any] = version.parse('1.11') @property def lowercase_ ( self : str ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase_ ( self : Tuple ): """simple docstring""" return 1e-4 @property def lowercase_ ( self : Optional[int] ): """simple docstring""" return 12
31
1
'''simple docstring''' import inspect from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel, VQModel from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : Any, a_ : VQModel, a_ : UNetaDModel, a_ : DDIMScheduler ): """simple docstring""" super().__init__() self.register_modules(vqvae=a_, unet=a_, scheduler=a_ ) @torch.no_grad() def __call__( self : Union[str, Any], a_ : int = 1, a_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None, a_ : float = 0.0, a_ : int = 50, a_ : Optional[str] = "pil", a_ : bool = True, **a_ : Tuple, ): """simple docstring""" UpperCamelCase__ = randn_tensor( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size), generator=a_, ) UpperCamelCase__ = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler UpperCamelCase__ = latents * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(a_ ) # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature UpperCamelCase__ = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) UpperCamelCase__ = {} if accepts_eta: UpperCamelCase__ = eta for t in self.progress_bar(self.scheduler.timesteps ): UpperCamelCase__ = self.scheduler.scale_model_input(a_, a_ ) # predict the noise residual UpperCamelCase__ = self.unet(a_, a_ ).sample # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase__ = self.scheduler.step(a_, a_, a_, **a_ ).prev_sample # decode the image latents with the VAE UpperCamelCase__ = self.vqvae.decode(a_ ).sample UpperCamelCase__ = (image / 2 + 0.5).clamp(0, 1 ) UpperCamelCase__ = image.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": UpperCamelCase__ = self.numpy_to_pil(a_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a_ )
31
'''simple docstring''' import os from pickle import UnpicklingError from typing import Dict, Tuple import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict, unflatten_dict import transformers from .utils import logging __lowercase: int = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple=False ) -> Union[str, Any]: '''simple docstring''' try: import torch # noqa: F401 except ImportError: logger.error( "Loading a PyTorch model in Flax, requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise if not is_sharded: UpperCamelCase__ = os.path.abspath(_UpperCamelCase ) logger.info(F'Loading PyTorch weights from {pt_path}' ) UpperCamelCase__ = torch.load(_UpperCamelCase , map_location="cpu" ) logger.info(F'PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters.' ) UpperCamelCase__ = convert_pytorch_state_dict_to_flax(_UpperCamelCase , _UpperCamelCase ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files UpperCamelCase__ = convert_pytorch_sharded_state_dict_to_flax(_UpperCamelCase , _UpperCamelCase ) return flax_state_dict def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple[str] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, jnp.ndarray] , _UpperCamelCase : str , ) -> (Tuple[str], np.ndarray): '''simple docstring''' def is_key_or_prefix_key_in_dict(_UpperCamelCase : Tuple[str] ) -> bool: return len(set(_UpperCamelCase ) & {key, (model_prefix,) + key} ) > 0 # layer norm UpperCamelCase__ = pt_tuple_key[:-1] + ("scale",) if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # batch norm layer mean UpperCamelCase__ = pt_tuple_key[:-1] + ("mean",) if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # batch norm layer var UpperCamelCase__ = pt_tuple_key[:-1] + ("var",) if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # embedding UpperCamelCase__ = pt_tuple_key[:-1] + ("embedding",) if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # conv layer UpperCamelCase__ = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): UpperCamelCase__ = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer UpperCamelCase__ = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): UpperCamelCase__ = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight UpperCamelCase__ = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias UpperCamelCase__ = pt_tuple_key[:-1] + ("bias",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 UpperCamelCase__ = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): UpperCamelCase__ = pt_tuple_key[-2] + "_g" elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): UpperCamelCase__ = pt_tuple_key[-2] + "_v" if name is not None: UpperCamelCase__ = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = {k: v.numpy() for k, v in pt_state_dict.items()} UpperCamelCase__ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: UpperCamelCase__ = flax_model.params["params"] else: UpperCamelCase__ = flax_model.params UpperCamelCase__ = flatten_dict(_UpperCamelCase ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: UpperCamelCase__ = flatten_dict(flax_model.params["batch_stats"] ) random_flax_state_dict.update(_UpperCamelCase ) UpperCamelCase__ = {} UpperCamelCase__ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split("." )[0] for k in pt_state_dict.keys()} ) UpperCamelCase__ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split("." )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCamelCase__ = tuple(pt_key.split("." ) ) # remove base model prefix if necessary UpperCamelCase__ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = pt_tuple_key[1:] # Correctly rename weight parameters UpperCamelCase__ , UpperCamelCase__ = rename_key_and_reshape_tensor( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # add model prefix if necessary UpperCamelCase__ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ' F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1] or "var" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(_UpperCamelCase , _UpperCamelCase ) continue # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) else: # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) return unflatten_dict(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any ) -> Any: '''simple docstring''' import torch # Load the index UpperCamelCase__ = {} for shard_file in shard_filenames: # load using msgpack utils UpperCamelCase__ = torch.load(_UpperCamelCase ) UpperCamelCase__ = {k: v.numpy() for k, v in pt_state_dict.items()} UpperCamelCase__ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict if "batch_stats" in flax_model.params: UpperCamelCase__ = flax_model.params["params"] UpperCamelCase__ = flatten_dict(_UpperCamelCase ) random_flax_state_dict.update(flatten_dict(flax_model.params["batch_stats"] ) ) else: UpperCamelCase__ = flax_model.params UpperCamelCase__ = flatten_dict(_UpperCamelCase ) UpperCamelCase__ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split("." )[0] for k in pt_state_dict.keys()} ) UpperCamelCase__ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split("." )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCamelCase__ = tuple(pt_key.split("." ) ) # remove base model prefix if necessary UpperCamelCase__ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = pt_tuple_key[1:] # Correctly rename weight parameters UpperCamelCase__ , UpperCamelCase__ = rename_key_and_reshape_tensor( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # add model prefix if necessary UpperCamelCase__ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ' F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue if "var" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(_UpperCamelCase , _UpperCamelCase ) continue # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) else: # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) return unflatten_dict(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : Optional[int] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = os.path.abspath(_UpperCamelCase ) logger.info(F'Loading Flax weights from {flax_checkpoint_path}' ) # import correct flax class UpperCamelCase__ = getattr(_UpperCamelCase , "Flax" + model.__class__.__name__ ) # load flax weight dict with open(_UpperCamelCase , "rb" ) as state_f: try: UpperCamelCase__ = from_bytes(_UpperCamelCase , state_f.read() ) except UnpicklingError: raise EnvironmentError(F'Unable to convert {flax_checkpoint_path} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(_UpperCamelCase , _UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple , _UpperCamelCase : Any ) -> Optional[Any]: '''simple docstring''' try: import torch # noqa: F401 except ImportError: logger.error( "Loading a Flax weights in PyTorch, requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise # check if we have bf16 weights UpperCamelCase__ = flatten_dict(jax.tree_util.tree_map(lambda _UpperCamelCase : x.dtype == jnp.bfloataa , _UpperCamelCase ) ).values() if any(_UpperCamelCase ): # convert all weights to fp32 if the are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( "Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` " "before loading those in PyTorch model." ) UpperCamelCase__ = jax.tree_util.tree_map( lambda _UpperCamelCase : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , _UpperCamelCase ) UpperCamelCase__ = flatten_dict(_UpperCamelCase ) UpperCamelCase__ = pt_model.state_dict() UpperCamelCase__ = (pt_model.base_model_prefix in flax_state) and ( pt_model.base_model_prefix not in {k.split("." )[0] for k in pt_model_dict.keys()} ) UpperCamelCase__ = (pt_model.base_model_prefix not in flax_state) and ( pt_model.base_model_prefix in {k.split("." )[0] for k in pt_model_dict.keys()} ) # keep track of unexpected & missing keys UpperCamelCase__ = [] UpperCamelCase__ = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): UpperCamelCase__ = flax_key_tuple[0] == pt_model.base_model_prefix UpperCamelCase__ = ".".join((pt_model.base_model_prefix,) + flax_key_tuple ) in pt_model_dict # adapt flax_key to prepare for loading from/to base model only if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (pt_model.base_model_prefix,) + flax_key_tuple # rename flax weights to PyTorch format if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(_UpperCamelCase ) not in pt_model_dict: # conv layer UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) UpperCamelCase__ = jnp.transpose(_UpperCamelCase , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(_UpperCamelCase ) not in pt_model_dict: # linear layer UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) UpperCamelCase__ = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: UpperCamelCase__ = flax_key_tuple[:-1] + ("running_mean",) elif "var" in flax_key_tuple[-1]: UpperCamelCase__ = flax_key_tuple[:-1] + ("running_var",) if "batch_stats" in flax_state: UpperCamelCase__ = ".".join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: UpperCamelCase__ = ".".join(_UpperCamelCase ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. UpperCamelCase__ = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: UpperCamelCase__ = key.split("." ) UpperCamelCase__ = None if key_components[-3::2] == ["parametrizations", "original0"]: UpperCamelCase__ = key_components[-2] + "_g" elif key_components[-3::2] == ["parametrizations", "original1"]: UpperCamelCase__ = key_components[-2] + "_v" if name is not None: UpperCamelCase__ = key_components[:-3] + [name] UpperCamelCase__ = ".".join(_UpperCamelCase ) UpperCamelCase__ = key if flax_key in special_pt_names: UpperCamelCase__ = special_pt_names[flax_key] if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict UpperCamelCase__ = np.asarray(_UpperCamelCase ) if not isinstance(_UpperCamelCase , np.ndarray ) else flax_tensor UpperCamelCase__ = torch.from_numpy(_UpperCamelCase ) # remove from missing keys missing_keys.remove(_UpperCamelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(_UpperCamelCase ) pt_model.load_state_dict(_UpperCamelCase ) # re-transform missing_keys to list UpperCamelCase__ = list(_UpperCamelCase ) if len(_UpperCamelCase ) > 0: logger.warning( "Some weights of the Flax model were not used when initializing the PyTorch model" F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' " (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This" F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' " to be exactly identical (e.g. initializing a BertForSequenceClassification model from a" " FlaxBertForSequenceClassification model)." ) else: logger.warning(F'All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n' ) if len(_UpperCamelCase ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' " use it for predictions and inference." ) else: logger.warning( F'All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n' "If your task is similar to the task the model of the checkpoint was trained on, " F'you can already use {pt_model.__class__.__name__} for predictions without further training.' ) return pt_model
31
1
'''simple docstring''' import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Union[str, Any] = CLIPTokenizer _lowerCamelCase : Dict = CLIPTokenizerFast _lowerCamelCase : int = True _lowerCamelCase : Tuple = {} _lowerCamelCase : Tuple = False def lowercase_ ( self : Tuple ): """simple docstring""" super().setUp() # fmt: off UpperCamelCase__ = ["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 UpperCamelCase__ = dict(zip(a_, range(len(a_ ) ) ) ) UpperCamelCase__ = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] UpperCamelCase__ = {"unk_token": "<unk>"} UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as fp: fp.write(json.dumps(a_ ) + "\n" ) with open(self.merges_file, "w", encoding="utf-8" ) as fp: fp.write("\n".join(a_ ) ) def lowercase_ ( self : Optional[Any], **a_ : str ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : str, **a_ : str ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : List[Any], a_ : Dict ): """simple docstring""" UpperCamelCase__ = "lower newer" UpperCamelCase__ = "lower newer" return input_text, output_text def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = CLIPTokenizer(self.vocab_file, self.merges_file, **self.special_tokens_map ) UpperCamelCase__ = "lower newer" UpperCamelCase__ = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] UpperCamelCase__ = tokenizer.tokenize(a_ ) self.assertListEqual(a_, a_ ) UpperCamelCase__ = tokens + [tokenizer.unk_token] UpperCamelCase__ = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(a_ ), a_ ) @require_ftfy def lowercase_ ( self : Dict ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCamelCase__ = self.tokenizer_class.from_pretrained(a_, **a_ ) UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained(a_, **a_ ) UpperCamelCase__ = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways UpperCamelCase__ = "xa\u0303y" + " " + "x\xe3y" UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on unicode of space type UpperCamelCase__ = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on unicode of line break type UpperCamelCase__ = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) def lowercase_ ( self : Tuple ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCamelCase__ = "hello" # `hello` is a token in the vocabulary of `pretrained_name` UpperCamelCase__ = f'{text_of_1_token} {text_of_1_token}' UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained( a_, use_fast=a_, ) UpperCamelCase__ = tokenizer_r(a_, return_offsets_mapping=a_, add_special_tokens=a_ ) self.assertEqual(encoding.offset_mapping[0], (0, len(a_ )) ) self.assertEqual( encoding.offset_mapping[1], (len(a_ ) + 1, len(a_ ) + 1 + len(a_ )), ) UpperCamelCase__ = f' {text}' UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained( a_, use_fast=a_, ) UpperCamelCase__ = tokenizer_r(a_, return_offsets_mapping=a_, add_special_tokens=a_ ) self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(a_ )) ) self.assertEqual( encoding.offset_mapping[1], (1 + len(a_ ) + 1, 1 + len(a_ ) + 1 + len(a_ )), ) def lowercase_ ( self : Tuple ): """simple docstring""" with self.assertRaises(a_ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def lowercase_ ( self : Union[str, Any] ): """simple docstring""" super().test_tokenization_python_rust_equals() def lowercase_ ( self : List[str] ): """simple docstring""" pass
31
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' return credit_card_number.startswith(("34", "35", "37", "4", "5", "6") ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' UpperCamelCase__ = credit_card_number UpperCamelCase__ = 0 UpperCamelCase__ = len(_UpperCamelCase ) - 2 for i in range(_UpperCamelCase , -1 , -2 ): # double the value of every second digit UpperCamelCase__ = int(cc_number[i] ) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 UpperCamelCase__ = cc_number[:i] + str(_UpperCamelCase ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(_UpperCamelCase ) - 1 , -1 , -2 ): total += int(cc_number[i] ) return total % 10 == 0 def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> bool: '''simple docstring''' UpperCamelCase__ = F'{credit_card_number} is an invalid credit card number because' if not credit_card_number.isdigit(): print(F'{error_message} it has nonnumerical characters.' ) return False if not 13 <= len(_UpperCamelCase ) <= 16: print(F'{error_message} of its length.' ) return False if not validate_initial_digits(_UpperCamelCase ): print(F'{error_message} of its first two digits.' ) return False if not luhn_validation(_UpperCamelCase ): print(F'{error_message} it fails the Luhn check.' ) return False print(F'{credit_card_number} is a valid credit card number.' ) return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowercase: List[str] = { "configuration_swinv2": ["SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Swinv2Config"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Optional[int] = [ "SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST", "Swinv2ForImageClassification", "Swinv2ForMaskedImageModeling", "Swinv2Model", "Swinv2PreTrainedModel", ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys __lowercase: Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : float , _UpperCamelCase : float , _UpperCamelCase : float , ) -> tuple: '''simple docstring''' if (electron_conc, hole_conc, intrinsic_conc).count(0 ) != 1: raise ValueError("You cannot supply more or less than 2 values" ) elif electron_conc < 0: raise ValueError("Electron concentration cannot be negative in a semiconductor" ) elif hole_conc < 0: raise ValueError("Hole concentration cannot be negative in a semiconductor" ) elif intrinsic_conc < 0: raise ValueError( "Intrinsic concentration cannot be negative in a semiconductor" ) elif electron_conc == 0: return ( "electron_conc", intrinsic_conc**2 / hole_conc, ) elif hole_conc == 0: return ( "hole_conc", intrinsic_conc**2 / electron_conc, ) elif intrinsic_conc == 0: return ( "intrinsic_conc", (electron_conc * hole_conc) ** 0.5, ) else: return (-1, -1) if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin __lowercase: List[str] = "\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n" class UpperCAmelCase ( unittest.TestCase , SCREAMING_SNAKE_CASE__): def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = load_tool("text-question-answering" ) self.tool.setup() UpperCamelCase__ = load_tool("text-question-answering", remote=a_ ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = self.tool(a_, "What did Hugging Face do in April 2021?" ) self.assertEqual(a_, "launched the BigScience Research Workshop" ) def lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = self.remote_tool(a_, "What did Hugging Face do in April 2021?" ) self.assertEqual(a_, "launched the BigScience Research Workshop" ) def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = self.tool(text=a_, question="What did Hugging Face do in April 2021?" ) self.assertEqual(a_, "launched the BigScience Research Workshop" ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = self.remote_tool(text=a_, question="What did Hugging Face do in April 2021?" ) self.assertEqual(a_, "launched the BigScience Research Workshop" )
31
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowercase: Dict = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase: Optional[int] = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys __lowercase: Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
31
1
'''simple docstring''' import datasets from .evaluate import evaluate __lowercase: Any = "\\n@article{hendrycks2021cuad,\n title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n journal={arXiv preprint arXiv:2103.06268},\n year={2021}\n}\n" __lowercase: List[Any] = "\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n" __lowercase: Optional[Any] = "\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - 'id': id of the question-answer pair as given in the references (see below)\n - 'prediction_text': list of possible texts for the answer, as a list of strings\n depending on a threshold on the confidence probability of each prediction.\n references: List of question-answers dictionaries with the following key-values:\n - 'id': id of the question-answer pair (see above),\n - 'answers': a Dict in the CUAD dataset format\n {\n 'text': list of possible texts for the answer, as a list of strings\n 'answer_start': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n 'exact_match': Exact match (the normalized answer exactly match the gold answer)\n 'f1': The F-score of predicted tokens versus the gold answer\n 'aupr': Area Under the Precision-Recall curve\n 'prec_at_80_recall': Precision at 80% recall\n 'prec_at_90_recall': Precision at 90% recall\nExamples:\n >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}]\n >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}]\n >>> cuad_metric = datasets.load_metric(\"cuad\")\n >>> results = cuad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class UpperCAmelCase ( datasets.Metric): def lowercase_ ( self : int ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { "predictions": { "id": datasets.Value("string" ), "prediction_text": datasets.features.Sequence(datasets.Value("string" ) ), }, "references": { "id": datasets.Value("string" ), "answers": datasets.features.Sequence( { "text": datasets.Value("string" ), "answer_start": datasets.Value("int32" ), } ), }, } ), codebase_urls=["https://www.atticusprojectai.org/cuad"], reference_urls=["https://www.atticusprojectai.org/cuad"], ) def lowercase_ ( self : Dict, a_ : str, a_ : int ): """simple docstring""" UpperCamelCase__ = {prediction["id"]: prediction["prediction_text"] for prediction in predictions} UpperCamelCase__ = [ { "paragraphs": [ { "qas": [ { "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]], "id": ref["id"], } for ref in references ] } ] } ] UpperCamelCase__ = evaluate(dataset=a_, predictions=a_ ) return score
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict , _UpperCamelCase : str , _UpperCamelCase : Optional[int] , _UpperCamelCase : str ) -> Dict: # noqa: E741 '''simple docstring''' while r - l > 1: UpperCamelCase__ = (l + r) // 2 if v[m] >= key: UpperCamelCase__ = m else: UpperCamelCase__ = m # noqa: E741 return r def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list[int] ) -> int: '''simple docstring''' if len(_UpperCamelCase ) == 0: return 0 UpperCamelCase__ = [0] * len(_UpperCamelCase ) UpperCamelCase__ = 1 UpperCamelCase__ = v[0] for i in range(1 , len(_UpperCamelCase ) ): if v[i] < tail[0]: UpperCamelCase__ = v[i] elif v[i] > tail[length - 1]: UpperCamelCase__ = v[i] length += 1 else: UpperCamelCase__ = v[i] return length if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' import unittest from transformers import MPNetConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) class UpperCAmelCase : def __init__( self : Any, a_ : Union[str, Any], a_ : Tuple=13, a_ : Any=7, a_ : Any=True, a_ : Dict=True, a_ : Any=False, a_ : str=True, a_ : List[Any]=99, a_ : Union[str, Any]=64, a_ : List[Any]=5, a_ : Dict=4, a_ : Dict=64, a_ : List[Any]="gelu", a_ : List[Any]=0.1, a_ : Tuple=0.1, a_ : Union[str, Any]=512, a_ : int=16, a_ : Optional[Any]=2, a_ : Dict=0.02, a_ : List[Any]=3, a_ : int=4, a_ : Any=None, ): """simple docstring""" 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 def lowercase_ ( self : Optional[int] ): """simple docstring""" return MPNetConfig.from_pretrained("microsoft/mpnet-base" ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) UpperCamelCase__ = None if self.use_input_mask: UpperCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) 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__ = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ ( self : Optional[Any] ): """simple docstring""" return MPNetConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, initializer_range=self.initializer_range, ) def lowercase_ ( self : Optional[int], a_ : Dict, a_ : Any, a_ : Optional[int], a_ : Union[str, Any], a_ : Optional[int], a_ : Optional[int] ): """simple docstring""" UpperCamelCase__ = MPNetModel(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase__ = model(a_, a_ ) UpperCamelCase__ = model(a_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.hidden_size) ) def lowercase_ ( self : Union[str, Any], a_ : Optional[int], a_ : Dict, a_ : Dict, a_ : int, a_ : Any, a_ : Optional[Any] ): """simple docstring""" UpperCamelCase__ = MPNetForQuestionAnswering(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase__ = model( a_, attention_mask=a_, start_positions=a_, end_positions=a_, ) self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length) ) def lowercase_ ( self : Optional[Any], a_ : List[str], a_ : Optional[Any], a_ : Union[str, Any], a_ : Dict, a_ : List[Any], a_ : Dict ): """simple docstring""" UpperCamelCase__ = self.num_labels UpperCamelCase__ = MPNetForSequenceClassification(a_ ) model.to(a_ ) model.eval() UpperCamelCase__ = model(a_, attention_mask=a_, labels=a_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels) ) def lowercase_ ( self : Union[str, Any], a_ : Optional[Any], a_ : List[Any], a_ : Tuple, a_ : Tuple, a_ : Any, a_ : Dict ): """simple docstring""" UpperCamelCase__ = self.num_choices UpperCamelCase__ = MPNetForMultipleChoice(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase__ = input_ids.unsqueeze(1 ).expand(-1, self.num_choices, -1 ).contiguous() UpperCamelCase__ = input_mask.unsqueeze(1 ).expand(-1, self.num_choices, -1 ).contiguous() UpperCamelCase__ = model( a_, attention_mask=a_, labels=a_, ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_choices) ) def lowercase_ ( self : List[str], a_ : Union[str, Any], a_ : Dict, a_ : List[Any], a_ : Any, a_ : List[Any], a_ : Any ): """simple docstring""" UpperCamelCase__ = self.num_labels UpperCamelCase__ = MPNetForTokenClassification(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase__ = model(a_, attention_mask=a_, labels=a_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.prepare_config_and_inputs() ((UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__)) = config_and_inputs UpperCamelCase__ = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : int = ( ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) if is_torch_available() else () ) _lowerCamelCase : str = ( { 'feature-extraction': MPNetModel, 'fill-mask': MPNetForMaskedLM, 'question-answering': MPNetForQuestionAnswering, 'text-classification': MPNetForSequenceClassification, 'token-classification': MPNetForTokenClassification, 'zero-shot': MPNetForSequenceClassification, } if is_torch_available() else {} ) _lowerCamelCase : Any = False _lowerCamelCase : Any = True def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = MPNetModelTester(self ) UpperCamelCase__ = ConfigTester(self, config_class=a_, hidden_size=37 ) def lowercase_ ( self : Tuple ): """simple docstring""" self.config_tester.run_common_tests() def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_model(*a_ ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_sequence_classification(*a_ ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_multiple_choice(*a_ ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_token_classification(*a_ ) def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_question_answering(*a_ ) @require_torch class UpperCAmelCase ( unittest.TestCase): @slow def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = MPNetModel.from_pretrained("microsoft/mpnet-base" ) UpperCamelCase__ = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) UpperCamelCase__ = model(a_ )[0] UpperCamelCase__ = torch.Size((1, 11, 768) ) self.assertEqual(output.shape, a_ ) UpperCamelCase__ = torch.tensor( [[[-0.0_550, 0.1_943, -0.0_740], [-0.0_562, 0.2_211, -0.0_579], [-0.0_437, 0.3_337, -0.0_641]]] ) # compare the actual values for a slice. self.assertTrue(torch.allclose(output[:, :3, :3], a_, atol=1e-4 ) )
31
'''simple docstring''' from ..utils import DummyObject, requires_backends class UpperCAmelCase ( metaclass=SCREAMING_SNAKE_CASE__): _lowerCamelCase : str = ['torch', 'scipy'] def __init__( self : List[str], *a_ : Optional[int], **a_ : int ): """simple docstring""" requires_backends(self, ["torch", "scipy"] ) @classmethod def lowercase_ ( cls : Dict, *a_ : Tuple, **a_ : Dict ): """simple docstring""" requires_backends(cls, ["torch", "scipy"] ) @classmethod def lowercase_ ( cls : Optional[Any], *a_ : List[Any], **a_ : Any ): """simple docstring""" requires_backends(cls, ["torch", "scipy"] )
31
1
'''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() __lowercase: Optional[Any] = 2 class UpperCAmelCase : def __init__( self : int, *, # begin keyword-only arguments a_ : Optional[int]="<s>", a_ : str="<pad>", a_ : Optional[int]="</s>", a_ : Optional[int]="<unk>", a_ : List[Any]=None, ): """simple docstring""" UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = bos, unk, pad, eos UpperCamelCase__ = [] UpperCamelCase__ = [] UpperCamelCase__ = {} UpperCamelCase__ = self.add_symbol(a_ ) UpperCamelCase__ = self.add_symbol(a_ ) UpperCamelCase__ = self.add_symbol(a_ ) UpperCamelCase__ = self.add_symbol(a_ ) if extra_special_symbols: for s in extra_special_symbols: self.add_symbol(a_ ) UpperCamelCase__ = len(self.symbols ) def __eq__( self : Tuple, a_ : List[str] ): """simple docstring""" return self.indices == other.indices def __getitem__( self : Tuple, a_ : Tuple ): """simple docstring""" if idx < len(self.symbols ): return self.symbols[idx] return self.unk_word def __len__( self : Optional[int] ): """simple docstring""" return len(self.symbols ) def __contains__( self : int, a_ : Optional[int] ): """simple docstring""" return sym in self.indices @classmethod def lowercase_ ( cls : str, a_ : List[str] ): """simple docstring""" UpperCamelCase__ = cls() d.add_from_file(a_ ) return d def lowercase_ ( self : Optional[int], a_ : Optional[Any], a_ : Tuple=1, a_ : int=False ): """simple docstring""" if word in self.indices and not overwrite: UpperCamelCase__ = self.indices[word] UpperCamelCase__ = self.count[idx] + n return idx else: UpperCamelCase__ = len(self.symbols ) UpperCamelCase__ = idx self.symbols.append(a_ ) self.count.append(a_ ) return idx def lowercase_ ( self : Union[str, Any], a_ : Any ): """simple docstring""" return 0 def lowercase_ ( self : Dict, a_ : Tuple ): """simple docstring""" if isinstance(a_, a_ ): try: with open(a_, "r", encoding="utf-8" ) as fd: self.add_from_file(a_ ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception("Incorrect encoding detected in {}, please rebuild the dataset".format(a_ ) ) return UpperCamelCase__ = f.readlines() UpperCamelCase__ = self._load_meta(a_ ) for line in lines[indices_start_line:]: try: UpperCamelCase__ , UpperCamelCase__ = line.rstrip().rsplit(" ", 1 ) if field == "#fairseq:overwrite": UpperCamelCase__ = True UpperCamelCase__ , UpperCamelCase__ = line.rsplit(" ", 1 ) else: UpperCamelCase__ = False UpperCamelCase__ = int(a_ ) UpperCamelCase__ = 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(a_ ) ) self.add_symbol(a_, n=a_, overwrite=a_ ) except ValueError: raise ValueError("Incorrect dictionary format, expected '<token> <cnt> [flags]'" ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> List[Any]: '''simple docstring''' UpperCamelCase__ = dict((re.sub(r"@@$" , "" , _UpperCamelCase ), v) if k.endswith("@@" ) else (re.sub(r"$" , "</w>" , _UpperCamelCase ), v) for k, v in d.items() ) UpperCamelCase__ = "<s> <pad> </s> <unk>".split() # restore the special tokens for k in keep_keys: del da[F'{k}</w>'] UpperCamelCase__ = d[k] # restore return da def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : str ) -> List[str]: '''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 UpperCamelCase__ = os.path.join(_UpperCamelCase , "checkpoint.pt" ) if not os.path.isfile(_UpperCamelCase ): raise ValueError(F'path to the file {checkpoint_file} does not exist!' ) UpperCamelCase__ = torch.load(_UpperCamelCase , map_location="cpu" ) UpperCamelCase__ = chkpt["cfg"]["model"] # dicts UpperCamelCase__ = os.path.join(_UpperCamelCase , "dict.txt" ) if not os.path.isfile(_UpperCamelCase ): raise ValueError(F'path to the file {dict_file} does not exist!' ) UpperCamelCase__ = Dictionary.load(_UpperCamelCase ) UpperCamelCase__ = rewrite_dict_keys(src_dict.indices ) UpperCamelCase__ = len(_UpperCamelCase ) UpperCamelCase__ = 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) UpperCamelCase__ = os.path.join(_UpperCamelCase , "bpecodes" ) if not os.path.isfile(_UpperCamelCase ): raise ValueError(F'path to the file {bpecodes_file} does not exist!' ) UpperCamelCase__ = os.path.join(_UpperCamelCase , VOCAB_FILES_NAMES["merges_file"] ) shutil.copyfile(_UpperCamelCase , _UpperCamelCase ) # model config UpperCamelCase__ = os.path.join(_UpperCamelCase , "config.json" ) UpperCamelCase__ = { "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.0_2, "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 UpperCamelCase__ = os.path.join(_UpperCamelCase , _UpperCamelCase ) UpperCamelCase__ = { "bos_token": "<s>", "eos_token": "</s>", "model_max_length": 10_24, "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 UpperCamelCase__ = chkpt["model"] # remove unneeded keys UpperCamelCase__ = [ "decoder.version", ] for k in ignore_keys: model_state_dict.pop(_UpperCamelCase , _UpperCamelCase ) UpperCamelCase__ = list(model_state_dict.keys() ) for layer_name in layer_names: if layer_name.endswith("output_projection.weight" ): UpperCamelCase__ = model_state_dict.pop(_UpperCamelCase ) else: UpperCamelCase__ = model_state_dict.pop(_UpperCamelCase ) UpperCamelCase__ = BioGptConfig.from_pretrained(_UpperCamelCase ) UpperCamelCase__ = BioGptForCausalLM(_UpperCamelCase ) # check that it loads ok model_new.load_state_dict(_UpperCamelCase ) # save UpperCamelCase__ = os.path.join(_UpperCamelCase , _UpperCamelCase ) print(F'Generating {pytorch_weights_dump_path}' ) torch.save(_UpperCamelCase , _UpperCamelCase ) print("Conversion is done!" ) if __name__ == "__main__": __lowercase: Optional[Any] = 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." ) __lowercase: Union[str, Any] = parser.parse_args() convert_biogpt_checkpoint_to_pytorch(args.biogpt_checkpoint_path, args.pytorch_dump_folder_path)
31
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase: str = logging.get_logger(__name__) __lowercase: Optional[Any] = { "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Any = 'gpt_neox_japanese' def __init__( self : List[str], a_ : Union[str, Any]=3_2000, a_ : str=2560, a_ : Dict=32, a_ : Tuple=32, a_ : Union[str, Any]=4, a_ : Union[str, Any]="gelu", a_ : int=1.00, a_ : Dict=1_0000, a_ : Any=2048, a_ : Optional[int]=0.02, a_ : int=1e-5, a_ : int=True, a_ : Optional[int]=3_1996, a_ : List[str]=3_1999, a_ : List[str]=0.1, a_ : Optional[int]=0.0, **a_ : Tuple, ): """simple docstring""" super().__init__(bos_token_id=a_, eos_token_id=a_, **a_ ) UpperCamelCase__ = vocab_size UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = hidden_size UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = intermediate_multiple_size UpperCamelCase__ = hidden_act UpperCamelCase__ = rotary_pct UpperCamelCase__ = rotary_emb_base UpperCamelCase__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = use_cache UpperCamelCase__ = attention_dropout UpperCamelCase__ = hidden_dropout
31
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list , _UpperCamelCase : int = 0 ) -> list: '''simple docstring''' UpperCamelCase__ = length or len(_UpperCamelCase ) UpperCamelCase__ = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: UpperCamelCase__ , UpperCamelCase__ = list_data[i + 1], list_data[i] UpperCamelCase__ = True return list_data if not swapped else bubble_sort(_UpperCamelCase , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : float , _UpperCamelCase : float ) -> float: '''simple docstring''' if density <= 0: raise ValueError("Impossible fluid density" ) if bulk_modulus <= 0: raise ValueError("Impossible bulk modulus" ) return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowercase: str = logging.get_logger(__name__) __lowercase: str = {"vocab_file": "vocab.json", "merges_file": "merges.txt"} __lowercase: Optional[int] = { "vocab_file": { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json", "allenai/longformer-large-4096": ( "https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json" ), "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json" ), }, "merges_file": { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt", "allenai/longformer-large-4096": ( "https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt" ), "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt" ), }, } __lowercase: str = { "allenai/longformer-base-4096": 4_096, "allenai/longformer-large-4096": 4_096, "allenai/longformer-large-4096-finetuned-triviaqa": 4_096, "allenai/longformer-base-4096-extra.pos.embd.only": 4_096, "allenai/longformer-large-4096-extra.pos.embd.only": 4_096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def SCREAMING_SNAKE_CASE__( ) -> str: '''simple docstring''' UpperCamelCase__ = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) UpperCamelCase__ = bs[:] UpperCamelCase__ = 0 for b in range(2**8 ): if b not in bs: bs.append(_UpperCamelCase ) cs.append(2**8 + n ) n += 1 UpperCamelCase__ = [chr(_UpperCamelCase ) for n in cs] return dict(zip(_UpperCamelCase , _UpperCamelCase ) ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = set() UpperCamelCase__ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) UpperCamelCase__ = char return pairs class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Any = VOCAB_FILES_NAMES _lowerCamelCase : int = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : Dict = ['input_ids', 'attention_mask'] def __init__( self : Union[str, Any], a_ : Tuple, a_ : Tuple, a_ : str="replace", a_ : int="<s>", a_ : Any="</s>", a_ : Union[str, Any]="</s>", a_ : List[str]="<s>", a_ : Optional[Any]="<unk>", a_ : Tuple="<pad>", a_ : Dict="<mask>", a_ : Any=False, **a_ : Dict, ): """simple docstring""" UpperCamelCase__ = AddedToken(a_, lstrip=a_, rstrip=a_ ) if isinstance(a_, a_ ) else bos_token UpperCamelCase__ = AddedToken(a_, lstrip=a_, rstrip=a_ ) if isinstance(a_, a_ ) else eos_token UpperCamelCase__ = AddedToken(a_, lstrip=a_, rstrip=a_ ) if isinstance(a_, a_ ) else sep_token UpperCamelCase__ = AddedToken(a_, lstrip=a_, rstrip=a_ ) if isinstance(a_, a_ ) else cls_token UpperCamelCase__ = AddedToken(a_, lstrip=a_, rstrip=a_ ) if isinstance(a_, a_ ) else unk_token UpperCamelCase__ = AddedToken(a_, lstrip=a_, rstrip=a_ ) if isinstance(a_, a_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it UpperCamelCase__ = AddedToken(a_, lstrip=a_, rstrip=a_ ) if isinstance(a_, a_ ) else mask_token super().__init__( errors=a_, bos_token=a_, eos_token=a_, unk_token=a_, sep_token=a_, cls_token=a_, pad_token=a_, mask_token=a_, add_prefix_space=a_, **a_, ) with open(a_, encoding="utf-8" ) as vocab_handle: UpperCamelCase__ = json.load(a_ ) UpperCamelCase__ = {v: k for k, v in self.encoder.items()} UpperCamelCase__ = errors # how to handle errors in decoding UpperCamelCase__ = bytes_to_unicode() UpperCamelCase__ = {v: k for k, v in self.byte_encoder.items()} with open(a_, encoding="utf-8" ) as merges_handle: UpperCamelCase__ = merges_handle.read().split("\n" )[1:-1] UpperCamelCase__ = [tuple(merge.split() ) for merge in bpe_merges] UpperCamelCase__ = dict(zip(a_, range(len(a_ ) ) ) ) UpperCamelCase__ = {} UpperCamelCase__ = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions UpperCamelCase__ = re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+" ) @property def lowercase_ ( self : Dict ): """simple docstring""" return len(self.encoder ) def lowercase_ ( self : Optional[int] ): """simple docstring""" return dict(self.encoder, **self.added_tokens_encoder ) def lowercase_ ( self : Any, a_ : Union[str, Any] ): """simple docstring""" if token in self.cache: return self.cache[token] UpperCamelCase__ = tuple(a_ ) UpperCamelCase__ = get_pairs(a_ ) if not pairs: return token while True: UpperCamelCase__ = min(a_, key=lambda a_ : self.bpe_ranks.get(a_, float("inf" ) ) ) if bigram not in self.bpe_ranks: break UpperCamelCase__ , UpperCamelCase__ = bigram UpperCamelCase__ = [] UpperCamelCase__ = 0 while i < len(a_ ): try: UpperCamelCase__ = word.index(a_, a_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) UpperCamelCase__ = j if word[i] == first and i < len(a_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 UpperCamelCase__ = tuple(a_ ) UpperCamelCase__ = new_word if len(a_ ) == 1: break else: UpperCamelCase__ = get_pairs(a_ ) UpperCamelCase__ = " ".join(a_ ) UpperCamelCase__ = word return word def lowercase_ ( self : List[Any], a_ : List[str] ): """simple docstring""" UpperCamelCase__ = [] for token in re.findall(self.pat, a_ ): UpperCamelCase__ = "".join( self.byte_encoder[b] for b in token.encode("utf-8" ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(a_ ).split(" " ) ) return bpe_tokens def lowercase_ ( self : int, a_ : Any ): """simple docstring""" return self.encoder.get(a_, self.encoder.get(self.unk_token ) ) def lowercase_ ( self : Union[str, Any], a_ : Tuple ): """simple docstring""" return self.decoder.get(a_ ) def lowercase_ ( self : Tuple, a_ : Tuple ): """simple docstring""" UpperCamelCase__ = "".join(a_ ) UpperCamelCase__ = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8", errors=self.errors ) return text def lowercase_ ( self : Optional[int], a_ : str, a_ : Optional[str] = None ): """simple docstring""" if not os.path.isdir(a_ ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return UpperCamelCase__ = os.path.join( a_, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) UpperCamelCase__ = os.path.join( a_, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(a_, "w", encoding="utf-8" ) as f: f.write(json.dumps(self.encoder, indent=2, sort_keys=a_, ensure_ascii=a_ ) + "\n" ) UpperCamelCase__ = 0 with open(a_, "w", encoding="utf-8" ) as writer: writer.write("#version: 0.2\n" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda a_ : kv[1] ): if index != token_index: logger.warning( f'Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.' " Please check that the tokenizer is not corrupted!" ) UpperCamelCase__ = token_index writer.write(" ".join(a_ ) + "\n" ) index += 1 return vocab_file, merge_file def lowercase_ ( self : List[str], 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] UpperCamelCase__ = [self.cls_token_id] UpperCamelCase__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def lowercase_ ( self : List[Any], a_ : List[int], a_ : Optional[List[int]] = None, a_ : bool = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=a_, token_ids_a=a_, already_has_special_tokens=a_ ) if token_ids_a is None: return [1] + ([0] * len(a_ )) + [1] return [1] + ([0] * len(a_ )) + [1, 1] + ([0] * len(a_ )) + [1] def lowercase_ ( self : Any, a_ : List[int], a_ : Optional[List[int]] = None ): """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 + sep + token_ids_a + sep ) * [0] def lowercase_ ( self : int, a_ : str, a_ : Optional[Any]=False, **a_ : List[str] ): """simple docstring""" UpperCamelCase__ = kwargs.pop("add_prefix_space", self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(a_ ) > 0 and not text[0].isspace()): UpperCamelCase__ = " " + text return (text, kwargs)
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int | float | str , _UpperCamelCase : int | float | str ) -> list[str]: '''simple docstring''' if nth_term == "": return [""] UpperCamelCase__ = int(_UpperCamelCase ) UpperCamelCase__ = int(_UpperCamelCase ) UpperCamelCase__ = [] for temp in range(int(_UpperCamelCase ) ): series.append(F'1 / {pow(temp + 1 , int(_UpperCamelCase ) )}' if series else "1" ) return series if __name__ == "__main__": import doctest doctest.testmod() __lowercase: Dict = int(input("Enter the last number (nth term) of the P-Series")) __lowercase: Optional[int] = int(input("Enter the power for P-Series")) print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") print(p_series(nth_term, power))
31
1
'''simple docstring''' from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int ) -> bool: '''simple docstring''' UpperCamelCase__ = int(number**0.5 ) return number == sq * sq def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int ) -> tuple[int, int]: '''simple docstring''' UpperCamelCase__ = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den UpperCamelCase__ = x_den * y_den * z_den UpperCamelCase__ = gcd(_UpperCamelCase , _UpperCamelCase ) top //= hcf bottom //= hcf return top, bottom def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int = 35 ) -> int: '''simple docstring''' UpperCamelCase__ = set() UpperCamelCase__ = 42 UpperCamelCase__ = Fraction(0 ) UpperCamelCase__ = 42 for x_num in range(1 , order + 1 ): for x_den in range(x_num + 1 , order + 1 ): for y_num in range(1 , order + 1 ): for y_den in range(y_num + 1 , order + 1 ): # n=1 UpperCamelCase__ = x_num * y_den + x_den * y_num UpperCamelCase__ = x_den * y_den UpperCamelCase__ = gcd(_UpperCamelCase , _UpperCamelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: UpperCamelCase__ = add_three( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) unique_s.add(_UpperCamelCase ) # n=2 UpperCamelCase__ = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) UpperCamelCase__ = x_den * x_den * y_den * y_den if is_sq(_UpperCamelCase ) and is_sq(_UpperCamelCase ): UpperCamelCase__ = int(sqrt(_UpperCamelCase ) ) UpperCamelCase__ = int(sqrt(_UpperCamelCase ) ) UpperCamelCase__ = gcd(_UpperCamelCase , _UpperCamelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: UpperCamelCase__ = add_three( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) unique_s.add(_UpperCamelCase ) # n=-1 UpperCamelCase__ = x_num * y_num UpperCamelCase__ = x_den * y_num + x_num * y_den UpperCamelCase__ = gcd(_UpperCamelCase , _UpperCamelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: UpperCamelCase__ = add_three( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) unique_s.add(_UpperCamelCase ) # n=2 UpperCamelCase__ = x_num * x_num * y_num * y_num UpperCamelCase__ = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(_UpperCamelCase ) and is_sq(_UpperCamelCase ): UpperCamelCase__ = int(sqrt(_UpperCamelCase ) ) UpperCamelCase__ = int(sqrt(_UpperCamelCase ) ) UpperCamelCase__ = gcd(_UpperCamelCase , _UpperCamelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: UpperCamelCase__ = add_three( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) unique_s.add(_UpperCamelCase ) for num, den in unique_s: total += Fraction(_UpperCamelCase , _UpperCamelCase ) return total.denominator + total.numerator if __name__ == "__main__": print(F"""{solution() = }""")
31
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE__) class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True}) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()}) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel}) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def lowercase_ ( self : str, a_ : Union[str, Any] ): """simple docstring""" if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column], a_ ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) UpperCamelCase__ = copy.deepcopy(self ) UpperCamelCase__ = self.label_schema.copy() UpperCamelCase__ = features[self.label_column] UpperCamelCase__ = label_schema return task_template @property def lowercase_ ( self : Any ): """simple docstring""" return { self.audio_column: "audio", self.label_column: "labels", }
31
1
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class UpperCAmelCase ( unittest.TestCase): _lowerCamelCase : List[Any] = MODEL_FOR_CAUSAL_LM_MAPPING _lowerCamelCase : Any = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def lowercase_ ( self : Tuple ): """simple docstring""" UpperCamelCase__ = pipeline(task="text-generation", model="sshleifer/tiny-ctrl", framework="pt" ) # Using `do_sample=False` to force deterministic output UpperCamelCase__ = text_generator("This is a test", do_sample=a_ ) self.assertEqual( a_, [ { "generated_text": ( "This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ], ) UpperCamelCase__ = text_generator(["This is a test", "This is a second test"] ) self.assertEqual( a_, [ [ { "generated_text": ( "This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ], [ { "generated_text": ( "This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy" " oscope. oscope. FiliFili@@" ) } ], ], ) UpperCamelCase__ = text_generator("This is a test", do_sample=a_, num_return_sequences=2, return_tensors=a_ ) self.assertEqual( a_, [ {"generated_token_ids": ANY(a_ )}, {"generated_token_ids": ANY(a_ )}, ], ) UpperCamelCase__ = text_generator.model.config.eos_token_id UpperCamelCase__ = "<pad>" UpperCamelCase__ = text_generator( ["This is a test", "This is a second test"], do_sample=a_, num_return_sequences=2, batch_size=2, return_tensors=a_, ) self.assertEqual( a_, [ [ {"generated_token_ids": ANY(a_ )}, {"generated_token_ids": ANY(a_ )}, ], [ {"generated_token_ids": ANY(a_ )}, {"generated_token_ids": ANY(a_ )}, ], ], ) @require_tf def lowercase_ ( self : Dict ): """simple docstring""" UpperCamelCase__ = pipeline(task="text-generation", model="sshleifer/tiny-ctrl", framework="tf" ) # Using `do_sample=False` to force deterministic output UpperCamelCase__ = text_generator("This is a test", do_sample=a_ ) self.assertEqual( a_, [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵" " please," ) } ], ) UpperCamelCase__ = text_generator(["This is a test", "This is a second test"], do_sample=a_ ) self.assertEqual( a_, [ [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵" " please," ) } ], [ { "generated_text": ( "This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes" " Cannes 閲閲Cannes Cannes Cannes 攵 please," ) } ], ], ) def lowercase_ ( self : Union[str, Any], a_ : Optional[Any], a_ : Any, a_ : List[str] ): """simple docstring""" UpperCamelCase__ = TextGenerationPipeline(model=a_, tokenizer=a_ ) return text_generator, ["This is a test", "Another test"] def lowercase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase__ = "Hello I believe in" UpperCamelCase__ = pipeline("text-generation", model="hf-internal-testing/tiny-random-gpt2" ) UpperCamelCase__ = text_generator(a_ ) self.assertEqual( a_, [{"generated_text": "Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"}], ) UpperCamelCase__ = text_generator(a_, stop_sequence=" fe" ) self.assertEqual(a_, [{"generated_text": "Hello I believe in fe"}] ) def lowercase_ ( self : int, a_ : Dict, a_ : Optional[Any] ): """simple docstring""" UpperCamelCase__ = text_generator.model UpperCamelCase__ = text_generator.tokenizer UpperCamelCase__ = text_generator("This is a test" ) self.assertEqual(a_, [{"generated_text": ANY(a_ )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) UpperCamelCase__ = text_generator("This is a test", return_full_text=a_ ) self.assertEqual(a_, [{"generated_text": ANY(a_ )}] ) self.assertNotIn("This is a test", outputs[0]["generated_text"] ) UpperCamelCase__ = pipeline(task="text-generation", model=a_, tokenizer=a_, return_full_text=a_ ) UpperCamelCase__ = text_generator("This is a test" ) self.assertEqual(a_, [{"generated_text": ANY(a_ )}] ) self.assertNotIn("This is a test", outputs[0]["generated_text"] ) UpperCamelCase__ = text_generator("This is a test", return_full_text=a_ ) self.assertEqual(a_, [{"generated_text": ANY(a_ )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) UpperCamelCase__ = text_generator(["This is great !", "Something else"], num_return_sequences=2, do_sample=a_ ) self.assertEqual( a_, [ [{"generated_text": ANY(a_ )}, {"generated_text": ANY(a_ )}], [{"generated_text": ANY(a_ )}, {"generated_text": ANY(a_ )}], ], ) if text_generator.tokenizer.pad_token is not None: UpperCamelCase__ = text_generator( ["This is great !", "Something else"], num_return_sequences=2, batch_size=2, do_sample=a_ ) self.assertEqual( a_, [ [{"generated_text": ANY(a_ )}, {"generated_text": ANY(a_ )}], [{"generated_text": ANY(a_ )}, {"generated_text": ANY(a_ )}], ], ) with self.assertRaises(a_ ): UpperCamelCase__ = text_generator("test", return_full_text=a_, return_text=a_ ) with self.assertRaises(a_ ): UpperCamelCase__ = text_generator("test", return_full_text=a_, return_tensors=a_ ) with self.assertRaises(a_ ): UpperCamelCase__ = text_generator("test", return_text=a_, return_tensors=a_ ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): UpperCamelCase__ = text_generator("" ) self.assertEqual(a_, [{"generated_text": ANY(a_ )}] ) else: with self.assertRaises((ValueError, AssertionError) ): UpperCamelCase__ = text_generator("" ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. UpperCamelCase__ = ["RwkvForCausalLM", "XGLMForCausalLM", "GPTNeoXForCausalLM"] if ( tokenizer.model_max_length < 1_0000 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator("This is a test" * 500, max_new_tokens=20 ) UpperCamelCase__ = text_generator("This is a test" * 500, handle_long_generation="hole", max_new_tokens=20 ) # Hole strategy cannot work with self.assertRaises(a_ ): text_generator( "This is a test" * 500, handle_long_generation="hole", max_new_tokens=tokenizer.model_max_length + 10, ) @require_torch @require_accelerate @require_torch_gpu def lowercase_ ( self : Optional[Any] ): """simple docstring""" import torch # Classic `model_kwargs` UpperCamelCase__ = pipeline( model="hf-internal-testing/tiny-random-bloom", model_kwargs={"device_map": "auto", "torch_dtype": torch.bfloataa}, ) self.assertEqual(pipe.model.device, torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype, torch.bfloataa ) UpperCamelCase__ = pipe("This is a test" ) self.assertEqual( a_, [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ], ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) UpperCamelCase__ = pipeline(model="hf-internal-testing/tiny-random-bloom", device_map="auto", torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device, torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype, torch.bfloataa ) UpperCamelCase__ = pipe("This is a test" ) self.assertEqual( a_, [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ], ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 UpperCamelCase__ = pipeline(model="hf-internal-testing/tiny-random-bloom", device_map="auto" ) self.assertEqual(pipe.model.device, torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype, torch.floataa ) UpperCamelCase__ = pipe("This is a test" ) self.assertEqual( a_, [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ], ) @require_torch @require_torch_gpu def lowercase_ ( self : Tuple ): """simple docstring""" import torch UpperCamelCase__ = pipeline(model="hf-internal-testing/tiny-random-bloom", device=0, torch_dtype=torch.floataa ) pipe("This is a test" ) @require_torch @require_accelerate @require_torch_gpu def lowercase_ ( self : Any ): """simple docstring""" import torch UpperCamelCase__ = pipeline(model="hf-internal-testing/tiny-random-bloom", device_map="auto", torch_dtype=torch.floataa ) pipe("This is a test", do_sample=a_, top_p=0.5 ) def lowercase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase__ = "Hello world" UpperCamelCase__ = pipeline("text-generation", model="hf-internal-testing/tiny-random-gpt2" ) if text_generator.model.framework == "tf": UpperCamelCase__ = logging.get_logger("transformers.generation.tf_utils" ) else: UpperCamelCase__ = logging.get_logger("transformers.generation.utils" ) UpperCamelCase__ = "Both `max_new_tokens`" # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(a_ ) as cl: UpperCamelCase__ = text_generator(a_, max_length=10, max_new_tokens=1 ) self.assertIn(a_, cl.out ) # The user only sets one -> no warning with CaptureLogger(a_ ) as cl: UpperCamelCase__ = text_generator(a_, max_new_tokens=1 ) self.assertNotIn(a_, cl.out ) with CaptureLogger(a_ ) as cl: UpperCamelCase__ = text_generator(a_, max_length=10 ) self.assertNotIn(a_, cl.out )
31
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import is_speech_available from transformers.testing_utils import require_torch, require_torchaudio from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import SpeechaTextFeatureExtractor __lowercase: str = random.Random() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int]=1.0 , _UpperCamelCase : Dict=None , _UpperCamelCase : List[str]=None ) -> Union[str, Any]: '''simple docstring''' if rng is None: UpperCamelCase__ = global_rng UpperCamelCase__ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class UpperCAmelCase ( unittest.TestCase): def __init__( self : List[Any], a_ : List[str], a_ : Any=7, a_ : Dict=400, a_ : str=2000, a_ : List[Any]=24, a_ : int=24, a_ : int=0.0, a_ : Union[str, Any]=1_6000, a_ : Union[str, Any]=True, a_ : Optional[Any]=True, ): """simple docstring""" UpperCamelCase__ = parent UpperCamelCase__ = batch_size UpperCamelCase__ = min_seq_length UpperCamelCase__ = max_seq_length UpperCamelCase__ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) UpperCamelCase__ = feature_size UpperCamelCase__ = num_mel_bins UpperCamelCase__ = padding_value UpperCamelCase__ = sampling_rate UpperCamelCase__ = return_attention_mask UpperCamelCase__ = do_normalize def lowercase_ ( self : Tuple ): """simple docstring""" return { "feature_size": self.feature_size, "num_mel_bins": self.num_mel_bins, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def lowercase_ ( self : Optional[Any], a_ : Union[str, Any]=False, a_ : Optional[int]=False ): """simple docstring""" def _flatten(a_ : Dict ): return list(itertools.chain(*a_ ) ) if equal_length: UpperCamelCase__ = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size UpperCamelCase__ = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length, self.max_seq_length, self.seq_length_diff ) ] if numpify: UpperCamelCase__ = [np.asarray(a_ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Dict = SpeechaTextFeatureExtractor if is_speech_available() else None def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = SpeechaTextFeatureExtractionTester(self ) def lowercase_ ( self : Optional[int], a_ : Tuple ): """simple docstring""" self.assertTrue(np.all(np.mean(a_, axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(a_, axis=0 ) - 1 ) < 1e-3 ) ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = [np.asarray(a_ ) for speech_input in speech_inputs] # Test feature size UpperCamelCase__ = feature_extractor(a_, padding=a_, return_tensors="np" ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size ) # Test not batched input UpperCamelCase__ = feature_extractor(speech_inputs[0], return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(np_speech_inputs[0], return_tensors="np" ).input_features self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) # Test batched UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features for enc_seq_a, enc_seq_a in zip(a_, a_ ): self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) # Test 2-D numpy arrays are batched. UpperCamelCase__ = [floats_list((1, x) )[0] for x in (800, 800, 800)] UpperCamelCase__ = np.asarray(a_ ) UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features for enc_seq_a, enc_seq_a in zip(a_, a_ ): self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = ["longest", "max_length", "do_not_pad"] UpperCamelCase__ = [None, 16, None] for max_length, padding in zip(a_, a_ ): UpperCamelCase__ = feature_extractor( a_, padding=a_, max_length=a_, return_attention_mask=a_ ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = [np.sum(a_ ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = ["longest", "max_length", "do_not_pad"] UpperCamelCase__ = [None, 16, None] for max_length, padding in zip(a_, a_ ): UpperCamelCase__ = feature_extractor( a_, max_length=a_, padding=a_, return_tensors="np", return_attention_mask=a_ ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = [np.sum(a_ ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="max_length", max_length=4, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1] ) self._check_zero_mean_unit_variance(input_features[2] ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="longest", max_length=4, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 4, 24) ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="longest", max_length=16, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 6, 24) ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" import torch UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = np.random.rand(100, 32 ).astype(np.floataa ) UpperCamelCase__ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: UpperCamelCase__ = feature_extractor.pad([{"input_features": inputs}], return_tensors="np" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) UpperCamelCase__ = feature_extractor.pad([{"input_features": inputs}], return_tensors="pt" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def lowercase_ ( self : List[str], a_ : int ): """simple docstring""" from datasets import load_dataset UpperCamelCase__ = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation" ) # automatic decoding with librispeech UpperCamelCase__ = ds.sort("id" ).select(range(a_ ) )[:num_samples]["audio"] return [x["array"] for x in speech_samples] def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = np.array([ -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241, -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128, -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625, ] ) # fmt: on UpperCamelCase__ = self._load_datasamples(1 ) UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = feature_extractor(a_, return_tensors="pt" ).input_features self.assertEquals(input_features.shape, (1, 584, 24) ) self.assertTrue(np.allclose(input_features[0, 0, :30], a_, atol=1e-4 ) )
31
1
'''simple docstring''' import os import sys __lowercase: Optional[Any] = os.path.join(os.path.dirname(__file__), "src") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) __lowercase: List[str] = [ "torch", "numpy", "tokenizers", "filelock", "requests", "tqdm", "regex", "sentencepiece", "sacremoses", "importlib_metadata", "huggingface_hub", ] @add_start_docstrings(AutoConfig.__doc__ ) def SCREAMING_SNAKE_CASE__( *_UpperCamelCase : Tuple , **_UpperCamelCase : int ) -> Optional[int]: '''simple docstring''' return AutoConfig.from_pretrained(*_UpperCamelCase , **_UpperCamelCase ) @add_start_docstrings(AutoTokenizer.__doc__ ) def SCREAMING_SNAKE_CASE__( *_UpperCamelCase : List[str] , **_UpperCamelCase : Tuple ) -> Optional[int]: '''simple docstring''' return AutoTokenizer.from_pretrained(*_UpperCamelCase , **_UpperCamelCase ) @add_start_docstrings(AutoModel.__doc__ ) def SCREAMING_SNAKE_CASE__( *_UpperCamelCase : List[Any] , **_UpperCamelCase : Any ) -> int: '''simple docstring''' return AutoModel.from_pretrained(*_UpperCamelCase , **_UpperCamelCase ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def SCREAMING_SNAKE_CASE__( *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' return AutoModelForCausalLM.from_pretrained(*_UpperCamelCase , **_UpperCamelCase ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def SCREAMING_SNAKE_CASE__( *_UpperCamelCase : List[str] , **_UpperCamelCase : Optional[int] ) -> Optional[int]: '''simple docstring''' return AutoModelForMaskedLM.from_pretrained(*_UpperCamelCase , **_UpperCamelCase ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def SCREAMING_SNAKE_CASE__( *_UpperCamelCase : List[Any] , **_UpperCamelCase : List[Any] ) -> Dict: '''simple docstring''' return AutoModelForSequenceClassification.from_pretrained(*_UpperCamelCase , **_UpperCamelCase ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def SCREAMING_SNAKE_CASE__( *_UpperCamelCase : Dict , **_UpperCamelCase : Optional[int] ) -> Optional[int]: '''simple docstring''' return AutoModelForQuestionAnswering.from_pretrained(*_UpperCamelCase , **_UpperCamelCase )
31
'''simple docstring''' import logging import numpy as np import pytest from scipy.linalg import eigh logging.basicConfig(level=logging.INFO, format="%(message)s") def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray ) -> np.ndarray: '''simple docstring''' return input_array.reshape((input_array.size, 1) ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' UpperCamelCase__ = np.nan for i in range(_UpperCamelCase ): UpperCamelCase__ = features[:, labels == i] UpperCamelCase__ = data.mean(1 ) # Centralize the data of class i UpperCamelCase__ = data - column_reshape(_UpperCamelCase ) if i > 0: # If covariance_sum is not None covariance_sum += np.dot(_UpperCamelCase , centered_data.T ) else: # If covariance_sum is np.nan (i.e. first loop) UpperCamelCase__ = np.dot(_UpperCamelCase , centered_data.T ) return covariance_sum / features.shape[1] def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' UpperCamelCase__ = features.mean(1 ) UpperCamelCase__ = np.nan for i in range(_UpperCamelCase ): UpperCamelCase__ = features[:, labels == i] UpperCamelCase__ = data.shape[1] UpperCamelCase__ = data.mean(1 ) if i > 0: # If covariance_sum is not None covariance_sum += device_data * np.dot( column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase ) , (column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase )).T , ) else: # If covariance_sum is np.nan (i.e. first loop) UpperCamelCase__ = device_data * np.dot( column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase ) , (column_reshape(_UpperCamelCase ) - column_reshape(_UpperCamelCase )).T , ) return covariance_sum / features.shape[1] def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' if features.any(): UpperCamelCase__ = features.mean(1 ) # Center the dataset UpperCamelCase__ = features - np.reshape(_UpperCamelCase , (data_mean.size, 1) ) UpperCamelCase__ = np.dot(_UpperCamelCase , centered_data.T ) / features.shape[1] UpperCamelCase__ , UpperCamelCase__ = np.linalg.eigh(_UpperCamelCase ) # Take all the columns in the reverse order (-1), and then takes only the first UpperCamelCase__ = eigenvectors[:, ::-1][:, 0:dimensions] # Project the database on the new space UpperCamelCase__ = np.dot(filtered_eigenvectors.T , _UpperCamelCase ) logging.info("Principal Component Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=_UpperCamelCase ) logging.error("Dataset empty" ) raise AssertionError def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : np.ndarray , _UpperCamelCase : int , _UpperCamelCase : int ) -> np.ndarray: '''simple docstring''' assert classes > dimensions # Check if features have been already loaded if features.any: UpperCamelCase__ , UpperCamelCase__ = eigh( covariance_between_classes(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) , covariance_within_classes(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) , ) UpperCamelCase__ = eigenvectors[:, ::-1][:, :dimensions] UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = np.linalg.svd(_UpperCamelCase ) UpperCamelCase__ = svd_matrix[:, 0:dimensions] UpperCamelCase__ = np.dot(filtered_svd_matrix.T , _UpperCamelCase ) logging.info("Linear Discriminant Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=_UpperCamelCase ) logging.error("Dataset empty" ) raise AssertionError def SCREAMING_SNAKE_CASE__( ) -> None: '''simple docstring''' UpperCamelCase__ = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]] ) UpperCamelCase__ = np.array([0, 0, 0, 1, 1] ) UpperCamelCase__ = 2 UpperCamelCase__ = 2 # Assert that the function raises an AssertionError if dimensions > classes with pytest.raises(_UpperCamelCase ) as error_info: UpperCamelCase__ = linear_discriminant_analysis( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if isinstance(_UpperCamelCase , np.ndarray ): raise AssertionError( "Did not raise AssertionError for dimensions > classes" ) assert error_info.type is AssertionError def SCREAMING_SNAKE_CASE__( ) -> None: '''simple docstring''' UpperCamelCase__ = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) UpperCamelCase__ = 2 UpperCamelCase__ = np.array([[6.9_2_8_2_0_3_2_3, 8.6_6_0_2_5_4_0_4, 1_0.3_9_2_3_0_4_8_5], [3.0, 3.0, 3.0]] ) with pytest.raises(_UpperCamelCase ) as error_info: UpperCamelCase__ = principal_component_analysis(_UpperCamelCase , _UpperCamelCase ) if not np.allclose(_UpperCamelCase , _UpperCamelCase ): raise AssertionError assert error_info.type is AssertionError if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : List[str] = BarthezTokenizer _lowerCamelCase : Optional[int] = BarthezTokenizerFast _lowerCamelCase : Any = True _lowerCamelCase : Any = True def lowercase_ ( self : Any ): """simple docstring""" super().setUp() UpperCamelCase__ = BarthezTokenizerFast.from_pretrained("moussaKam/mbarthez" ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname, legacy_format=a_ ) UpperCamelCase__ = tokenizer def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = "<pad>" UpperCamelCase__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(a_ ), a_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(a_ ), a_ ) def lowercase_ ( self : Optional[int] ): """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], "<mask>" ) self.assertEqual(len(a_ ), 10_1122 ) def lowercase_ ( self : List[str] ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size, 10_1122 ) @require_torch def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCamelCase__ = [0, 57, 3018, 7_0307, 91, 2] UpperCamelCase__ = self.tokenizer( a_, max_length=len(a_ ), padding=a_, truncation=a_, return_tensors="pt" ) self.assertIsInstance(a_, a_ ) self.assertEqual((2, 6), batch.input_ids.shape ) self.assertEqual((2, 6), batch.attention_mask.shape ) UpperCamelCase__ = batch.input_ids.tolist()[0] self.assertListEqual(a_, a_ ) def lowercase_ ( self : int ): """simple docstring""" if not self.test_rust_tokenizer: return UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_rust_tokenizer() UpperCamelCase__ = "I was born in 92000, and this is falsé." UpperCamelCase__ = tokenizer.tokenize(a_ ) UpperCamelCase__ = rust_tokenizer.tokenize(a_ ) self.assertListEqual(a_, a_ ) UpperCamelCase__ = tokenizer.encode(a_, add_special_tokens=a_ ) UpperCamelCase__ = rust_tokenizer.encode(a_, add_special_tokens=a_ ) self.assertListEqual(a_, a_ ) UpperCamelCase__ = self.get_rust_tokenizer() UpperCamelCase__ = tokenizer.encode(a_ ) UpperCamelCase__ = rust_tokenizer.encode(a_ ) self.assertListEqual(a_, a_ ) @slow def lowercase_ ( self : Dict ): """simple docstring""" UpperCamelCase__ = {"input_ids": [[0, 490, 1_4328, 4507, 354, 47, 4_3669, 95, 25, 7_8117, 2_0215, 1_9779, 190, 22, 400, 4, 3_5343, 8_0310, 603, 86, 2_4937, 105, 3_3438, 9_4762, 196, 3_9642, 7, 15, 1_5933, 173, 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], [0, 1_0534, 87, 25, 66, 3358, 196, 5_5289, 8, 8_2961, 81, 2204, 7_5203, 7, 15, 763, 1_2956, 216, 178, 1_4328, 9595, 1377, 6_9693, 7, 448, 7_1021, 196, 1_8106, 1437, 1_3974, 108, 9083, 4, 4_9315, 7, 39, 86, 1326, 2793, 4_6333, 4, 448, 196, 7_4588, 7, 4_9315, 7, 39, 21, 822, 3_8470, 74, 21, 6_6723, 6_2480, 8, 2_2050, 5, 2]], "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, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 # moussaKam/mbarthez is a french model. So we also use french texts. UpperCamelCase__ = [ "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=a_, model_name="moussaKam/mbarthez", revision="c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6", sequences=a_, )
31
'''simple docstring''' import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __lowercase: Optional[List[str]] = None __lowercase: List[Any] = "<" if sys.byteorder == "little" else ">" # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __lowercase: Tuple = [ np.dtype("|b1"), np.dtype("|u1"), np.dtype("<u2"), np.dtype(">u2"), np.dtype("<i2"), np.dtype(">i2"), np.dtype("<u4"), np.dtype(">u4"), np.dtype("<i4"), np.dtype(">i4"), np.dtype("<f4"), np.dtype(">f4"), np.dtype("<f8"), np.dtype(">f8"), ] @dataclass class UpperCAmelCase : _lowerCamelCase : bool = True _lowerCamelCase : Optional[str] = None # Automatically constructed _lowerCamelCase : ClassVar[str] = "PIL.Image.Image" _lowerCamelCase : ClassVar[Any] = pa.struct({'bytes': pa.binary(), 'path': pa.string()}) _lowerCamelCase : str = field(default='Image' , init=SCREAMING_SNAKE_CASE__ , repr=SCREAMING_SNAKE_CASE__) def __call__( self : Union[str, Any] ): """simple docstring""" return self.pa_type def lowercase_ ( self : Optional[Any], a_ : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if isinstance(a_, a_ ): UpperCamelCase__ = np.array(a_ ) if isinstance(a_, a_ ): return {"path": value, "bytes": None} elif isinstance(a_, a_ ): return {"path": None, "bytes": value} elif isinstance(a_, np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(a_ ) elif isinstance(a_, PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(a_ ) elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.' ) def lowercase_ ( self : Dict, a_ : dict, a_ : Dict=None ): """simple docstring""" if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead." ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support decoding images, please install 'Pillow'." ) if token_per_repo_id is None: UpperCamelCase__ = {} UpperCamelCase__ , UpperCamelCase__ = value["path"], value["bytes"] if bytes_ is None: if path is None: raise ValueError(f'An image should have one of \'path\' or \'bytes\' but both are None in {value}.' ) else: if is_local_path(a_ ): UpperCamelCase__ = PIL.Image.open(a_ ) else: UpperCamelCase__ = path.split("::" )[-1] try: UpperCamelCase__ = string_to_dict(a_, config.HUB_DATASETS_URL )["repo_id"] UpperCamelCase__ = token_per_repo_id.get(a_ ) except ValueError: UpperCamelCase__ = None with xopen(a_, "rb", use_auth_token=a_ ) as f: UpperCamelCase__ = BytesIO(f.read() ) UpperCamelCase__ = PIL.Image.open(bytes_ ) else: UpperCamelCase__ = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def lowercase_ ( self : List[str] ): """simple docstring""" from .features import Value return ( self if self.decode else { "bytes": Value("binary" ), "path": Value("string" ), } ) def lowercase_ ( self : List[Any], a_ : Union[pa.StringArray, pa.StructArray, pa.ListArray] ): """simple docstring""" if pa.types.is_string(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: UpperCamelCase__ = storage.field("bytes" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: UpperCamelCase__ = storage.field("path" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCamelCase__ = pa.array( [encode_np_array(np.array(a_ ) )["bytes"] if arr is not None else None for arr in storage.to_pylist()], type=pa.binary(), ) UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays( [bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def lowercase_ ( self : str, a_ : pa.StructArray ): """simple docstring""" @no_op_if_value_is_null def path_to_bytes(a_ : Dict ): with xopen(a_, "rb" ) as f: UpperCamelCase__ = f.read() return bytes_ UpperCamelCase__ = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) UpperCamelCase__ = pa.array( [os.path.basename(a_ ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def SCREAMING_SNAKE_CASE__( ) -> List[str]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() UpperCamelCase__ = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> bytes: '''simple docstring''' UpperCamelCase__ = BytesIO() if image.format in list_image_compression_formats(): UpperCamelCase__ = image.format else: UpperCamelCase__ = "PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF" image.save(_UpperCamelCase , format=_UpperCamelCase ) return buffer.getvalue() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> dict: '''simple docstring''' if hasattr(_UpperCamelCase , "filename" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray ) -> dict: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) UpperCamelCase__ = array.dtype UpperCamelCase__ = dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER UpperCamelCase__ = dtype.kind UpperCamelCase__ = dtype.itemsize UpperCamelCase__ = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCamelCase__ = np.dtype("|u1" ) if dtype_kind not in ["u", "i"]: raise TypeError( F'Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.' ) if dtype is not dest_dtype: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: UpperCamelCase__ = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: UpperCamelCase__ = dtype_byteorder + dtype_kind + str(_UpperCamelCase ) UpperCamelCase__ = np.dtype(_UpperCamelCase ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( F'Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}' ) UpperCamelCase__ = PIL.Image.fromarray(array.astype(_UpperCamelCase ) ) return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]] ) -> List[dict]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if objs: UpperCamelCase__ , UpperCamelCase__ = first_non_null_value(_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_UpperCamelCase , np.ndarray ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] elif isinstance(_UpperCamelCase , PIL.Image.Image ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] else: return objs else: return objs
31
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list ) -> list: '''simple docstring''' UpperCamelCase__ = len(_UpperCamelCase ) for _ in range(_UpperCamelCase ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: UpperCamelCase__ , UpperCamelCase__ = arr[i + 1], arr[i] return arr if __name__ == "__main__": __lowercase: List[str] = list(range(10, 0, -1)) print(F"""Original: {arr}. Sorted: {odd_even_transposition(arr)}""")
31
'''simple docstring''' import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Union[str, Any] = CLIPTokenizer _lowerCamelCase : Dict = CLIPTokenizerFast _lowerCamelCase : int = True _lowerCamelCase : Tuple = {} _lowerCamelCase : Tuple = False def lowercase_ ( self : Tuple ): """simple docstring""" super().setUp() # fmt: off UpperCamelCase__ = ["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 UpperCamelCase__ = dict(zip(a_, range(len(a_ ) ) ) ) UpperCamelCase__ = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] UpperCamelCase__ = {"unk_token": "<unk>"} UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as fp: fp.write(json.dumps(a_ ) + "\n" ) with open(self.merges_file, "w", encoding="utf-8" ) as fp: fp.write("\n".join(a_ ) ) def lowercase_ ( self : Optional[Any], **a_ : str ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : str, **a_ : str ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : List[Any], a_ : Dict ): """simple docstring""" UpperCamelCase__ = "lower newer" UpperCamelCase__ = "lower newer" return input_text, output_text def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = CLIPTokenizer(self.vocab_file, self.merges_file, **self.special_tokens_map ) UpperCamelCase__ = "lower newer" UpperCamelCase__ = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] UpperCamelCase__ = tokenizer.tokenize(a_ ) self.assertListEqual(a_, a_ ) UpperCamelCase__ = tokens + [tokenizer.unk_token] UpperCamelCase__ = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(a_ ), a_ ) @require_ftfy def lowercase_ ( self : Dict ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCamelCase__ = self.tokenizer_class.from_pretrained(a_, **a_ ) UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained(a_, **a_ ) UpperCamelCase__ = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways UpperCamelCase__ = "xa\u0303y" + " " + "x\xe3y" UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on unicode of space type UpperCamelCase__ = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) # Test that the tokenization is identical on unicode of line break type UpperCamelCase__ = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: UpperCamelCase__ = tokenizer_s.tokenize(a_ ) UpperCamelCase__ = tokenizer_r.tokenize(a_ ) self.assertListEqual(a_, a_ ) def lowercase_ ( self : Tuple ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCamelCase__ = "hello" # `hello` is a token in the vocabulary of `pretrained_name` UpperCamelCase__ = f'{text_of_1_token} {text_of_1_token}' UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained( a_, use_fast=a_, ) UpperCamelCase__ = tokenizer_r(a_, return_offsets_mapping=a_, add_special_tokens=a_ ) self.assertEqual(encoding.offset_mapping[0], (0, len(a_ )) ) self.assertEqual( encoding.offset_mapping[1], (len(a_ ) + 1, len(a_ ) + 1 + len(a_ )), ) UpperCamelCase__ = f' {text}' UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained( a_, use_fast=a_, ) UpperCamelCase__ = tokenizer_r(a_, return_offsets_mapping=a_, add_special_tokens=a_ ) self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(a_ )) ) self.assertEqual( encoding.offset_mapping[1], (1 + len(a_ ) + 1, 1 + len(a_ ) + 1 + len(a_ )), ) def lowercase_ ( self : Tuple ): """simple docstring""" with self.assertRaises(a_ ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def lowercase_ ( self : Union[str, Any] ): """simple docstring""" super().test_tokenization_python_rust_equals() def lowercase_ ( self : List[str] ): """simple docstring""" pass
31
1
'''simple docstring''' import unittest from transformers import PegasusTokenizer, PegasusTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __lowercase: str = get_tests_dir("fixtures/test_sentencepiece_no_bos.model") @require_sentencepiece @require_tokenizers class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Optional[int] = PegasusTokenizer _lowerCamelCase : Optional[Any] = PegasusTokenizerFast _lowerCamelCase : int = True _lowerCamelCase : List[Any] = True def lowercase_ ( self : List[str] ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing UpperCamelCase__ = PegasusTokenizer(a_ ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def lowercase_ ( self : Union[str, Any] ): """simple docstring""" return PegasusTokenizer.from_pretrained("google/pegasus-large" ) def lowercase_ ( self : str, **a_ : Any ): """simple docstring""" return PegasusTokenizer.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : List[str], a_ : List[Any] ): """simple docstring""" return ("This is a test", "This is a test") def lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = "</s>" UpperCamelCase__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(a_ ), a_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(a_ ), a_ ) def lowercase_ ( self : Tuple ): """simple docstring""" UpperCamelCase__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0], "<pad>" ) self.assertEqual(vocab_keys[1], "</s>" ) self.assertEqual(vocab_keys[-1], "v" ) self.assertEqual(len(a_ ), 1103 ) def lowercase_ ( self : str ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size, 1103 ) def lowercase_ ( self : Optional[int] ): """simple docstring""" UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) UpperCamelCase__ = self.tokenizer_class.from_pretrained(self.tmpdirname ) UpperCamelCase__ = ( "Let's see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important" " </s> <pad> <pad> <pad>" ) UpperCamelCase__ = rust_tokenizer([raw_input_str], return_tensors=a_, add_special_tokens=a_ ).input_ids[0] UpperCamelCase__ = py_tokenizer([raw_input_str], return_tensors=a_, add_special_tokens=a_ ).input_ids[0] self.assertListEqual(a_, a_ ) def lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word UpperCamelCase__ = "<mask_1> To ensure a <mask_2> flow of bank resolutions." UpperCamelCase__ = [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1] UpperCamelCase__ = tokenizer([raw_input_str], return_tensors=a_ ).input_ids[0] self.assertListEqual(a_, a_ ) def lowercase_ ( self : Dict ): """simple docstring""" UpperCamelCase__ = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6103 assert tokenizer.pad_token_id == 0 assert tokenizer.eos_token_id == 1 assert tokenizer.offset == 103 assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105 assert tokenizer.unk_token == "<unk>" assert tokenizer.model_max_length == 1024 UpperCamelCase__ = "To ensure a smooth flow of bank resolutions." UpperCamelCase__ = [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1] UpperCamelCase__ = tokenizer([raw_input_str], return_tensors=a_ ).input_ids[0] self.assertListEqual(a_, a_ ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = ["This is going to be way too long." * 150, "short example"] UpperCamelCase__ = ["not super long but more than 5 tokens", "tiny"] UpperCamelCase__ = self._large_tokenizer(a_, padding=a_, truncation=a_, return_tensors="pt" ) UpperCamelCase__ = self._large_tokenizer( text_target=a_, max_length=5, padding=a_, truncation=a_, return_tensors="pt" ) assert batch.input_ids.shape == (2, 1024) assert batch.attention_mask.shape == (2, 1024) assert targets["input_ids"].shape == (2, 5) assert len(a_ ) == 2 # input_ids, attention_mask. @slow def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = {"input_ids": [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 5542, 124, 302, 518, 111, 268, 2956, 115, 149, 4427, 107, 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], [139, 1235, 2799, 1_8289, 1_7780, 204, 109, 9474, 1296, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=a_, model_name="google/bigbird-pegasus-large-arxiv", revision="ba85d0851d708441f91440d509690f1ab6353415", ) @require_sentencepiece @require_tokenizers class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Optional[int] = PegasusTokenizer _lowerCamelCase : List[str] = PegasusTokenizerFast _lowerCamelCase : List[Any] = True _lowerCamelCase : Union[str, Any] = True def lowercase_ ( self : Any ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing UpperCamelCase__ = PegasusTokenizer(a_, offset=0, mask_token_sent=a_, mask_token="[MASK]" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def lowercase_ ( self : List[Any] ): """simple docstring""" return PegasusTokenizer.from_pretrained("google/bigbird-pegasus-large-arxiv" ) def lowercase_ ( self : Optional[Any], **a_ : int ): """simple docstring""" return PegasusTokenizer.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : Optional[int], a_ : str ): """simple docstring""" return ("This is a test", "This is a test") def lowercase_ ( self : Optional[int] ): """simple docstring""" UpperCamelCase__ = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) UpperCamelCase__ = self.tokenizer_class.from_pretrained(self.tmpdirname ) UpperCamelCase__ = ( "Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>" " <pad> <pad> <pad>" ) UpperCamelCase__ = rust_tokenizer([raw_input_str], return_tensors=a_, add_special_tokens=a_ ).input_ids[0] UpperCamelCase__ = py_tokenizer([raw_input_str], return_tensors=a_, add_special_tokens=a_ ).input_ids[0] self.assertListEqual(a_, a_ ) @require_torch def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = ["This is going to be way too long." * 1000, "short example"] UpperCamelCase__ = ["not super long but more than 5 tokens", "tiny"] UpperCamelCase__ = self._large_tokenizer(a_, padding=a_, truncation=a_, return_tensors="pt" ) UpperCamelCase__ = self._large_tokenizer( text_target=a_, max_length=5, padding=a_, truncation=a_, return_tensors="pt" ) assert batch.input_ids.shape == (2, 4096) assert batch.attention_mask.shape == (2, 4096) assert targets["input_ids"].shape == (2, 5) assert len(a_ ) == 2 # input_ids, attention_mask. def lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = ( "This is an example string that is used to test the original TF implementation against the HF" " implementation" ) UpperCamelCase__ = self._large_tokenizer(a_ ).input_ids self.assertListEqual( a_, [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1], )
31
'''simple docstring''' import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __lowercase: Any = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.14.0", "To fix: pip install -r examples/pytorch/audio-classification/requirements.txt") def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray , _UpperCamelCase : float , _UpperCamelCase : int = 1_60_00 ) -> str: '''simple docstring''' UpperCamelCase__ = int(round(sample_rate * max_length ) ) if len(_UpperCamelCase ) <= sample_length: return wav UpperCamelCase__ = randint(0 , len(_UpperCamelCase ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class UpperCAmelCase : _lowerCamelCase : Optional[str] = field(default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Name of a dataset from the datasets package'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'A file containing the training audio paths and labels.'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'A file containing the validation audio paths and labels.'}) _lowerCamelCase : str = field( default='train' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) _lowerCamelCase : str = field( default='validation' , metadata={ 'help': ( 'The name of the training data set split to use (via the datasets library). Defaults to \'validation\'' ) } , ) _lowerCamelCase : str = field( default='audio' , metadata={'help': 'The name of the dataset column containing the audio data. Defaults to \'audio\''} , ) _lowerCamelCase : str = field( default='label' , metadata={'help': 'The name of the dataset column containing the labels. Defaults to \'label\''}) _lowerCamelCase : Optional[int] = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) _lowerCamelCase : Optional[int] = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) _lowerCamelCase : float = field( default=20 , metadata={'help': 'Audio clips will be randomly cut to this length during training if the value is set.'} , ) @dataclass class UpperCAmelCase : _lowerCamelCase : str = field( default='facebook/wav2vec2-base' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Pretrained config name or path if not the same as model_name'}) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from the Hub'}) _lowerCamelCase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) _lowerCamelCase : Optional[str] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Name or path of preprocessor config.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to freeze the feature encoder layers of the model.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to generate an attention mask in the feature extractor.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) _lowerCamelCase : Optional[bool] = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}) _lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE__ , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def lowercase_ ( self : int ): """simple docstring""" if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( "The argument `--freeze_feature_extractor` is deprecated and " "will be removed in a future version. Use `--freeze_feature_encoder`" "instead. Setting `freeze_feature_encoder==True`.", a_, ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( "The argument `--freeze_feature_extractor` is deprecated and " "should not be used in combination with `--freeze_feature_encoder`." "Only make use of `--freeze_feature_encoder`." ) def SCREAMING_SNAKE_CASE__( ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_audio_classification" , _UpperCamelCase , _UpperCamelCase ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} ' + F'distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}' ) logger.info(F'Training/evaluation parameters {training_args}' ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. UpperCamelCase__ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'Output directory ({training_args.output_dir}) already exists and is not empty. ' "Use --overwrite_output_dir to train from scratch." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Initialize our dataset and prepare it for the audio classification task. UpperCamelCase__ = DatasetDict() UpperCamelCase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--audio_column_name {data_args.audio_column_name} not found in dataset \'{data_args.dataset_name}\'. ' "Make sure to set `--audio_column_name` to the correct audio column - one of " F'{", ".join(raw_datasets["train"].column_names )}.' ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--label_column_name {data_args.label_column_name} not found in dataset \'{data_args.dataset_name}\'. ' "Make sure to set `--label_column_name` to the correct text column - one of " F'{", ".join(raw_datasets["train"].column_names )}.' ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy UpperCamelCase__ = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. UpperCamelCase__ = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) UpperCamelCase__ = feature_extractor.model_input_names[0] def train_transforms(_UpperCamelCase : Any ): UpperCamelCase__ = [] for audio in batch[data_args.audio_column_name]: UpperCamelCase__ = random_subsample( audio["array"] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(_UpperCamelCase ) UpperCamelCase__ = feature_extractor(_UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCamelCase__ = {model_input_name: inputs.get(_UpperCamelCase )} UpperCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(_UpperCamelCase : List[Any] ): UpperCamelCase__ = [audio["array"] for audio in batch[data_args.audio_column_name]] UpperCamelCase__ = feature_extractor(_UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCamelCase__ = {model_input_name: inputs.get(_UpperCamelCase )} UpperCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. UpperCamelCase__ = raw_datasets["train"].features[data_args.label_column_name].names UpperCamelCase__ , UpperCamelCase__ = {}, {} for i, label in enumerate(_UpperCamelCase ): UpperCamelCase__ = str(_UpperCamelCase ) UpperCamelCase__ = label # Load the accuracy metric from the datasets package UpperCamelCase__ = evaluate.load("accuracy" ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Any ): UpperCamelCase__ = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=_UpperCamelCase , references=eval_pred.label_ids ) UpperCamelCase__ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(_UpperCamelCase ) , labelaid=_UpperCamelCase , idalabel=_UpperCamelCase , finetuning_task="audio-classification" , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=_UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: UpperCamelCase__ = ( raw_datasets["train"].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(_UpperCamelCase , output_all_columns=_UpperCamelCase ) if training_args.do_eval: if data_args.max_eval_samples is not None: UpperCamelCase__ = ( raw_datasets["eval"].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(_UpperCamelCase , output_all_columns=_UpperCamelCase ) # Initialize our trainer UpperCamelCase__ = Trainer( model=_UpperCamelCase , args=_UpperCamelCase , train_dataset=raw_datasets["train"] if training_args.do_train else None , eval_dataset=raw_datasets["eval"] if training_args.do_eval else None , compute_metrics=_UpperCamelCase , tokenizer=_UpperCamelCase , ) # Training if training_args.do_train: UpperCamelCase__ = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ = last_checkpoint UpperCamelCase__ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics("train" , train_result.metrics ) trainer.save_metrics("train" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ = trainer.evaluate() trainer.log_metrics("eval" , _UpperCamelCase ) trainer.save_metrics("eval" , _UpperCamelCase ) # Write model card and (optionally) push to hub UpperCamelCase__ = { "finetuned_from": model_args.model_name_or_path, "tasks": "audio-classification", "dataset": data_args.dataset_name, "tags": ["audio-classification"], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
31
1
'''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. __lowercase: List[str] = {"LayoutLMv2Config", "LayoutLMv3Config"} @is_pipeline_test class UpperCAmelCase ( unittest.TestCase): _lowerCamelCase : int = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING _lowerCamelCase : Optional[Any] = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: _lowerCamelCase : Dict = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: _lowerCamelCase : str = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } def lowercase_ ( self : str, a_ : Dict, a_ : Any, a_ : int ): """simple docstring""" UpperCamelCase__ = ZeroShotClassificationPipeline( model=a_, tokenizer=a_, candidate_labels=["polics", "health"] ) return classifier, ["Who are you voting for in 2020?", "My stomach hurts."] def lowercase_ ( self : Tuple, a_ : int, a_ : Any ): """simple docstring""" UpperCamelCase__ = classifier("Who are you voting for in 2020?", candidate_labels="politics" ) self.assertEqual(a_, {"sequence": ANY(a_ ), "labels": [ANY(a_ )], "scores": [ANY(a_ )]} ) # No kwarg UpperCamelCase__ = classifier("Who are you voting for in 2020?", ["politics"] ) self.assertEqual(a_, {"sequence": ANY(a_ ), "labels": [ANY(a_ )], "scores": [ANY(a_ )]} ) UpperCamelCase__ = classifier("Who are you voting for in 2020?", candidate_labels=["politics"] ) self.assertEqual(a_, {"sequence": ANY(a_ ), "labels": [ANY(a_ )], "scores": [ANY(a_ )]} ) UpperCamelCase__ = classifier("Who are you voting for in 2020?", candidate_labels="politics, public health" ) self.assertEqual( a_, {"sequence": ANY(a_ ), "labels": [ANY(a_ ), ANY(a_ )], "scores": [ANY(a_ ), ANY(a_ )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["scores"] ) ), 1.0 ) UpperCamelCase__ = classifier("Who are you voting for in 2020?", candidate_labels=["politics", "public health"] ) self.assertEqual( a_, {"sequence": ANY(a_ ), "labels": [ANY(a_ ), ANY(a_ )], "scores": [ANY(a_ ), ANY(a_ )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["scores"] ) ), 1.0 ) UpperCamelCase__ = classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template="This text is about {}" ) self.assertEqual(a_, {"sequence": ANY(a_ ), "labels": [ANY(a_ )], "scores": [ANY(a_ )]} ) # https://github.com/huggingface/transformers/issues/13846 UpperCamelCase__ = classifier(["I am happy"], ["positive", "negative"] ) self.assertEqual( a_, [ {"sequence": ANY(a_ ), "labels": [ANY(a_ ), ANY(a_ )], "scores": [ANY(a_ ), ANY(a_ )]} for i in range(1 ) ], ) UpperCamelCase__ = classifier(["I am happy", "I am sad"], ["positive", "negative"] ) self.assertEqual( a_, [ {"sequence": ANY(a_ ), "labels": [ANY(a_ ), ANY(a_ )], "scores": [ANY(a_ ), ANY(a_ )]} for i in range(2 ) ], ) with self.assertRaises(a_ ): classifier("", candidate_labels="politics" ) with self.assertRaises(a_ ): classifier(a_, candidate_labels="politics" ) with self.assertRaises(a_ ): classifier("Who are you voting for in 2020?", candidate_labels="" ) with self.assertRaises(a_ ): classifier("Who are you voting for in 2020?", candidate_labels=a_ ) with self.assertRaises(a_ ): classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template="Not formatting template", ) with self.assertRaises(a_ ): classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template=a_, ) self.run_entailment_id(a_ ) def lowercase_ ( self : List[str], a_ : Pipeline ): """simple docstring""" UpperCamelCase__ = zero_shot_classifier.model.config UpperCamelCase__ = config.labelaid UpperCamelCase__ = zero_shot_classifier.entailment_id UpperCamelCase__ = {"LABEL_0": 0, "LABEL_1": 1, "LABEL_2": 2} self.assertEqual(zero_shot_classifier.entailment_id, -1 ) UpperCamelCase__ = {"entailment": 0, "neutral": 1, "contradiction": 2} self.assertEqual(zero_shot_classifier.entailment_id, 0 ) UpperCamelCase__ = {"ENTAIL": 0, "NON-ENTAIL": 1} self.assertEqual(zero_shot_classifier.entailment_id, 0 ) UpperCamelCase__ = {"ENTAIL": 2, "NEUTRAL": 1, "CONTR": 0} self.assertEqual(zero_shot_classifier.entailment_id, 2 ) UpperCamelCase__ = original_labelaid self.assertEqual(a_, zero_shot_classifier.entailment_id ) @require_torch def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = 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?" * 100, candidate_labels=["politics", "public health", "science"] ) @require_torch def lowercase_ ( self : Tuple ): """simple docstring""" UpperCamelCase__ = pipeline( "zero-shot-classification", model="sshleifer/tiny-distilbert-base-cased-distilled-squad", framework="pt", ) UpperCamelCase__ = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(a_ ), { "sequence": "Who are you voting for in 2020?", "labels": ["science", "public health", "politics"], "scores": [0.333, 0.333, 0.333], }, ) @require_tf def lowercase_ ( self : Optional[Any] ): """simple docstring""" UpperCamelCase__ = pipeline( "zero-shot-classification", model="sshleifer/tiny-distilbert-base-cased-distilled-squad", framework="tf", ) UpperCamelCase__ = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(a_ ), { "sequence": "Who are you voting for in 2020?", "labels": ["science", "public health", "politics"], "scores": [0.333, 0.333, 0.333], }, ) @slow @require_torch def lowercase_ ( self : Dict ): """simple docstring""" UpperCamelCase__ = pipeline("zero-shot-classification", model="roberta-large-mnli", framework="pt" ) UpperCamelCase__ = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(a_ ), { "sequence": "Who are you voting for in 2020?", "labels": ["politics", "public health", "science"], "scores": [0.976, 0.015, 0.009], }, ) UpperCamelCase__ = 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=a_, ) self.assertEqual( nested_simplify(a_ ), { "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.817, 0.713, 0.018, 0.018], }, ) @slow @require_tf def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = pipeline("zero-shot-classification", model="roberta-large-mnli", framework="tf" ) UpperCamelCase__ = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(a_ ), { "sequence": "Who are you voting for in 2020?", "labels": ["politics", "public health", "science"], "scores": [0.976, 0.015, 0.009], }, ) UpperCamelCase__ = 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=a_, ) self.assertEqual( nested_simplify(a_ ), { "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.817, 0.713, 0.018, 0.018], }, )
31
'''simple docstring''' import math import sys def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = "" try: with open(_UpperCamelCase , "rb" ) as binary_file: UpperCamelCase__ = binary_file.read() for dat in data: UpperCamelCase__ = F'{dat:08b}' result += curr_byte return result except OSError: print("File not accessible" ) sys.exit() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = {"0": "0", "1": "1"} UpperCamelCase__ , UpperCamelCase__ = "", "" UpperCamelCase__ = len(_UpperCamelCase ) for i in range(len(_UpperCamelCase ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue UpperCamelCase__ = lexicon[curr_string] result += last_match_id UpperCamelCase__ = last_match_id + "0" if math.loga(_UpperCamelCase ).is_integer(): UpperCamelCase__ = {} for curr_key in list(_UpperCamelCase ): UpperCamelCase__ = lexicon.pop(_UpperCamelCase ) UpperCamelCase__ = new_lex UpperCamelCase__ = last_match_id + "1" index += 1 UpperCamelCase__ = "" return result def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str ) -> None: '''simple docstring''' UpperCamelCase__ = 8 try: with open(_UpperCamelCase , "wb" ) as opened_file: UpperCamelCase__ = [ to_write[i : i + byte_length] for i in range(0 , len(_UpperCamelCase ) , _UpperCamelCase ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("10000000" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(_UpperCamelCase , 2 ).to_bytes(1 , byteorder="big" ) ) except OSError: print("File not accessible" ) sys.exit() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str ) -> str: '''simple docstring''' UpperCamelCase__ = 0 for letter in data_bits: if letter == "1": break counter += 1 UpperCamelCase__ = data_bits[counter:] UpperCamelCase__ = data_bits[counter + 1 :] return data_bits def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str ) -> None: '''simple docstring''' UpperCamelCase__ = read_file_binary(_UpperCamelCase ) UpperCamelCase__ = remove_prefix(_UpperCamelCase ) UpperCamelCase__ = decompress_data(_UpperCamelCase ) write_file_binary(_UpperCamelCase , _UpperCamelCase ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
31
1
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class UpperCAmelCase ( unittest.TestCase): _lowerCamelCase : List[str] = ViTImageProcessor if is_vision_available() else None @property def lowercase_ ( self : Optional[int] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def lowercase_ ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = (3, 32, 128) UpperCamelCase__ = tempfile.mkdtemp() # fmt: off UpperCamelCase__ = ["[GO]", "[s]", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] # fmt: on UpperCamelCase__ = dict(zip(a_, range(len(a_ ) ) ) ) UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as fp: fp.write(json.dumps(a_ ) + "\n" ) UpperCamelCase__ = { "do_normalize": False, "do_resize": True, "image_processor_type": "ViTImageProcessor", "resample": 3, "size": {"height": 32, "width": 128}, } UpperCamelCase__ = os.path.join(self.tmpdirname, a_ ) with open(self.image_processor_file, "w", encoding="utf-8" ) as fp: json.dump(a_, a_ ) def lowercase_ ( self : int, **a_ : int ): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : Optional[int], **a_ : str ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def lowercase_ ( self : Tuple ): """simple docstring""" UpperCamelCase__ = np.random.randint(255, size=(3, 30, 400), dtype=np.uinta ) UpperCamelCase__ = Image.fromarray(np.moveaxis(a_, 0, -1 ) ) return image_input def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = MgpstrProcessor(tokenizer=a_, image_processor=a_ ) processor.save_pretrained(self.tmpdirname ) UpperCamelCase__ = MgpstrProcessor.from_pretrained(self.tmpdirname, use_fast=a_ ) self.assertEqual(processor.char_tokenizer.get_vocab(), tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer, a_ ) self.assertEqual(processor.image_processor.to_json_string(), image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor, a_ ) def lowercase_ ( self : Optional[int] ): """simple docstring""" UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = MgpstrProcessor(tokenizer=a_, image_processor=a_ ) processor.save_pretrained(self.tmpdirname ) UpperCamelCase__ = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)" ) UpperCamelCase__ = self.get_image_processor(do_normalize=a_, padding_value=1.0 ) UpperCamelCase__ = MgpstrProcessor.from_pretrained( self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=a_, padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer, a_ ) self.assertEqual(processor.image_processor.to_json_string(), image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor, a_ ) def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = MgpstrProcessor(tokenizer=a_, image_processor=a_ ) UpperCamelCase__ = self.prepare_image_inputs() UpperCamelCase__ = image_processor(a_, return_tensors="np" ) UpperCamelCase__ = processor(images=a_, return_tensors="np" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum(), input_processor[key].sum(), delta=1e-2 ) def lowercase_ ( self : Dict ): """simple docstring""" UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = MgpstrProcessor(tokenizer=a_, image_processor=a_ ) UpperCamelCase__ = "test" UpperCamelCase__ = processor(text=a_ ) UpperCamelCase__ = tokenizer(a_ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key], encoded_processor[key] ) def lowercase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = MgpstrProcessor(tokenizer=a_, image_processor=a_ ) UpperCamelCase__ = "test" UpperCamelCase__ = self.prepare_image_inputs() UpperCamelCase__ = processor(text=a_, images=a_ ) self.assertListEqual(list(inputs.keys() ), ["pixel_values", "labels"] ) # test if it raises when no input is passed with pytest.raises(a_ ): processor() def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = MgpstrProcessor(tokenizer=a_, image_processor=a_ ) UpperCamelCase__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] UpperCamelCase__ = processor.char_decode(a_ ) UpperCamelCase__ = tokenizer.batch_decode(a_ ) UpperCamelCase__ = [seq.replace(" ", "" ) for seq in decoded_tok] self.assertListEqual(a_, a_ ) def lowercase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = MgpstrProcessor(tokenizer=a_, image_processor=a_ ) UpperCamelCase__ = None UpperCamelCase__ = self.prepare_image_inputs() UpperCamelCase__ = processor(text=a_, images=a_ ) self.assertListEqual(list(inputs.keys() ), processor.model_input_names ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = MgpstrProcessor(tokenizer=a_, image_processor=a_ ) UpperCamelCase__ = torch.randn(1, 27, 38 ) UpperCamelCase__ = torch.randn(1, 27, 5_0257 ) UpperCamelCase__ = torch.randn(1, 27, 3_0522 ) UpperCamelCase__ = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ), ["generated_text", "scores", "char_preds", "bpe_preds", "wp_preds"] )
31
'''simple docstring''' import inspect from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel, VQModel from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : Any, a_ : VQModel, a_ : UNetaDModel, a_ : DDIMScheduler ): """simple docstring""" super().__init__() self.register_modules(vqvae=a_, unet=a_, scheduler=a_ ) @torch.no_grad() def __call__( self : Union[str, Any], a_ : int = 1, a_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None, a_ : float = 0.0, a_ : int = 50, a_ : Optional[str] = "pil", a_ : bool = True, **a_ : Tuple, ): """simple docstring""" UpperCamelCase__ = randn_tensor( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size), generator=a_, ) UpperCamelCase__ = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler UpperCamelCase__ = latents * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(a_ ) # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature UpperCamelCase__ = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) UpperCamelCase__ = {} if accepts_eta: UpperCamelCase__ = eta for t in self.progress_bar(self.scheduler.timesteps ): UpperCamelCase__ = self.scheduler.scale_model_input(a_, a_ ) # predict the noise residual UpperCamelCase__ = self.unet(a_, a_ ).sample # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase__ = self.scheduler.step(a_, a_, a_, **a_ ).prev_sample # decode the image latents with the VAE UpperCamelCase__ = self.vqvae.decode(a_ ).sample UpperCamelCase__ = (image / 2 + 0.5).clamp(0, 1 ) UpperCamelCase__ = image.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": UpperCamelCase__ = self.numpy_to_pil(a_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a_ )
31
1
'''simple docstring''' from collections import defaultdict from typing import Optional from ..image_utils import load_image from ..utils import ( add_end_docstrings, is_torch_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING __lowercase: List[Any] = logging.get_logger(__name__) @add_end_docstrings(SCREAMING_SNAKE_CASE__) class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : Optional[Any], **a_ : str ): """simple docstring""" super().__init__(**a_ ) requires_backends(self, "vision" ) requires_backends(self, "torch" ) if self.framework != "pt": raise ValueError(f'The {self.__class__} is only available in PyTorch.' ) self.check_model_type(a_ ) def lowercase_ ( self : Optional[int], **a_ : Optional[Any] ): """simple docstring""" UpperCamelCase__ = {} UpperCamelCase__ = {} UpperCamelCase__ = {} # preprocess args if "points_per_batch" in kwargs: UpperCamelCase__ = kwargs["points_per_batch"] if "points_per_crop" in kwargs: UpperCamelCase__ = kwargs["points_per_crop"] if "crops_n_layers" in kwargs: UpperCamelCase__ = kwargs["crops_n_layers"] if "crop_overlap_ratio" in kwargs: UpperCamelCase__ = kwargs["crop_overlap_ratio"] if "crop_n_points_downscale_factor" in kwargs: UpperCamelCase__ = kwargs["crop_n_points_downscale_factor"] # postprocess args if "pred_iou_thresh" in kwargs: UpperCamelCase__ = kwargs["pred_iou_thresh"] if "stability_score_offset" in kwargs: UpperCamelCase__ = kwargs["stability_score_offset"] if "mask_threshold" in kwargs: UpperCamelCase__ = kwargs["mask_threshold"] if "stability_score_thresh" in kwargs: UpperCamelCase__ = kwargs["stability_score_thresh"] if "crops_nms_thresh" in kwargs: UpperCamelCase__ = kwargs["crops_nms_thresh"] if "output_rle_mask" in kwargs: UpperCamelCase__ = kwargs["output_rle_mask"] if "output_bboxes_mask" in kwargs: UpperCamelCase__ = kwargs["output_bboxes_mask"] return preprocess_kwargs, forward_params, postprocess_kwargs def __call__( self : Tuple, a_ : Union[str, Any], *a_ : Dict, a_ : Dict=None, a_ : List[Any]=None, **a_ : str ): """simple docstring""" return super().__call__(a_, *a_, num_workers=a_, batch_size=a_, **a_ ) def lowercase_ ( self : Union[str, Any], a_ : Any, a_ : Optional[int]=64, a_ : int = 0, a_ : float = 512 / 1500, a_ : Optional[int] = 32, a_ : Optional[int] = 1, ): """simple docstring""" UpperCamelCase__ = load_image(a_ ) UpperCamelCase__ = self.image_processor.size["longest_edge"] UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self.image_processor.generate_crop_boxes( a_, a_, a_, a_, a_, a_ ) UpperCamelCase__ = self.image_processor(images=a_, return_tensors="pt" ) with self.device_placement(): if self.framework == "pt": UpperCamelCase__ = self.get_inference_context() with inference_context(): UpperCamelCase__ = self._ensure_tensor_on_device(a_, device=self.device ) UpperCamelCase__ = self.model.get_image_embeddings(model_inputs.pop("pixel_values" ) ) UpperCamelCase__ = image_embeddings UpperCamelCase__ = grid_points.shape[1] UpperCamelCase__ = points_per_batch if points_per_batch is not None else n_points if points_per_batch <= 0: raise ValueError( "Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. " "To return all points at once, set points_per_batch to None" ) for i in range(0, a_, a_ ): UpperCamelCase__ = grid_points[:, i : i + points_per_batch, :, :] UpperCamelCase__ = input_labels[:, i : i + points_per_batch] UpperCamelCase__ = i == n_points - points_per_batch yield { "input_points": batched_points, "input_labels": labels, "input_boxes": crop_boxes, "is_last": is_last, **model_inputs, } def lowercase_ ( self : str, a_ : Optional[Any], a_ : Any=0.88, a_ : Any=0.95, a_ : str=0, a_ : str=1, ): """simple docstring""" UpperCamelCase__ = model_inputs.pop("input_boxes" ) UpperCamelCase__ = model_inputs.pop("is_last" ) UpperCamelCase__ = model_inputs.pop("original_sizes" ).tolist() UpperCamelCase__ = model_inputs.pop("reshaped_input_sizes" ).tolist() UpperCamelCase__ = self.model(**a_ ) # post processing happens here in order to avoid CPU GPU copies of ALL the masks UpperCamelCase__ = model_outputs["pred_masks"] UpperCamelCase__ = self.image_processor.post_process_masks( a_, a_, a_, a_, binarize=a_ ) UpperCamelCase__ = model_outputs["iou_scores"] UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self.image_processor.filter_masks( masks[0], iou_scores[0], original_sizes[0], input_boxes[0], a_, a_, a_, a_, ) return { "masks": masks, "is_last": is_last, "boxes": boxes, "iou_scores": iou_scores, } def lowercase_ ( self : List[str], a_ : List[Any], a_ : str=False, a_ : List[Any]=False, a_ : List[Any]=0.7, ): """simple docstring""" UpperCamelCase__ = [] UpperCamelCase__ = [] UpperCamelCase__ = [] for model_output in model_outputs: all_scores.append(model_output.pop("iou_scores" ) ) all_masks.extend(model_output.pop("masks" ) ) all_boxes.append(model_output.pop("boxes" ) ) UpperCamelCase__ = torch.cat(a_ ) UpperCamelCase__ = torch.cat(a_ ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self.image_processor.post_process_for_mask_generation( a_, a_, a_, a_ ) UpperCamelCase__ = defaultdict(a_ ) for output in model_outputs: for k, v in output.items(): extra[k].append(a_ ) UpperCamelCase__ = {} if output_rle_mask: UpperCamelCase__ = rle_mask if output_bboxes_mask: UpperCamelCase__ = bounding_boxes return {"masks": output_masks, "scores": iou_scores, **optional, **extra}
31
'''simple docstring''' import argparse import json import subprocess def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : Tuple ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = [] UpperCamelCase__ = ( F'curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer {token}"' " https://api.github.com/repos/huggingface/transformers/actions/runners" ) UpperCamelCase__ = subprocess.run(_UpperCamelCase , shell=_UpperCamelCase , stdout=subprocess.PIPE ) UpperCamelCase__ = output.stdout.decode("utf-8" ) UpperCamelCase__ = json.loads(_UpperCamelCase ) UpperCamelCase__ = status["runners"] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_UpperCamelCase ) # save the result so we can report them on Slack with open("offline_runners.txt" , "w" ) as fp: fp.write(json.dumps(_UpperCamelCase ) ) if len(_UpperCamelCase ) > 0: UpperCamelCase__ = "\n".join([x["name"] for x in offline_runners] ) raise ValueError(F'The following runners are offline:\n{failed}' ) if __name__ == "__main__": def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' return values.split("," ) __lowercase: str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--target_runners", default=None, type=list_str, required=True, help="Comma-separated list of runners to check status.", ) parser.add_argument( "--token", default=None, type=str, required=True, help="A token that has actions:read permission." ) __lowercase: str = parser.parse_args() get_runner_status(args.target_runners, args.token)
31
1
'''simple docstring''' import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup __lowercase: List[str] = logging.get_logger(__name__) class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): def __init__( self : Optional[int], **a_ : Tuple ): """simple docstring""" requires_backends(self, ["bs4"] ) super().__init__(**a_ ) def lowercase_ ( self : List[Any], a_ : Union[str, Any] ): """simple docstring""" UpperCamelCase__ = [] UpperCamelCase__ = [] UpperCamelCase__ = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag UpperCamelCase__ = parent.find_all(child.name, recursive=a_ ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(a_ ) else next(i for i, s in enumerate(a_, 1 ) if s is child ) ) UpperCamelCase__ = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def lowercase_ ( self : Dict, a_ : int ): """simple docstring""" UpperCamelCase__ = BeautifulSoup(a_, "html.parser" ) UpperCamelCase__ = [] UpperCamelCase__ = [] UpperCamelCase__ = [] for element in html_code.descendants: if type(a_ ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue UpperCamelCase__ = html.unescape(a_ ).strip() if not text_in_this_tag: continue all_doc_strings.append(a_ ) UpperCamelCase__ , UpperCamelCase__ = self.xpath_soup(a_ ) stringaxtag_seq.append(a_ ) stringaxsubs_seq.append(a_ ) if len(a_ ) != len(a_ ): raise ValueError("Number of doc strings and xtags does not correspond" ) if len(a_ ) != len(a_ ): raise ValueError("Number of doc strings and xsubs does not correspond" ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def lowercase_ ( self : List[Any], a_ : str, a_ : Tuple ): """simple docstring""" UpperCamelCase__ = "" for tagname, subs in zip(a_, a_ ): xpath += f'/{tagname}' if subs != 0: xpath += f'[{subs}]' return xpath def __call__( self : Tuple, a_ : List[str] ): """simple docstring""" UpperCamelCase__ = False # Check that strings has a valid type if isinstance(a_, a_ ): UpperCamelCase__ = True elif isinstance(a_, (list, tuple) ): if len(a_ ) == 0 or isinstance(html_strings[0], a_ ): UpperCamelCase__ = True if not valid_strings: raise ValueError( "HTML strings must of type `str`, `List[str]` (batch of examples), " f'but is of type {type(a_ )}.' ) UpperCamelCase__ = bool(isinstance(a_, (list, tuple) ) and (isinstance(html_strings[0], a_ )) ) if not is_batched: UpperCamelCase__ = [html_strings] # Get nodes + xpaths UpperCamelCase__ = [] UpperCamelCase__ = [] for html_string in html_strings: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self.get_three_from_single(a_ ) nodes.append(a_ ) UpperCamelCase__ = [] for node, tag_list, sub_list in zip(a_, a_, a_ ): UpperCamelCase__ = self.construct_xpath(a_, a_ ) xpath_strings.append(a_ ) xpaths.append(a_ ) # return as Dict UpperCamelCase__ = {"nodes": nodes, "xpaths": xpaths} UpperCamelCase__ = BatchFeature(data=a_, tensor_type=a_ ) return encoded_inputs
31
'''simple docstring''' from typing import List, Optional, TypeVar from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .info import DatasetInfo from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets from .splits import NamedSplit from .utils import logging from .utils.py_utils import Literal __lowercase: str = logging.get_logger(__name__) __lowercase: Tuple = TypeVar("DatasetType", Dataset, IterableDataset) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[DatasetType] , _UpperCamelCase : Optional[List[float]] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[DatasetInfo] = None , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : Literal["first_exhausted", "all_exhausted"] = "first_exhausted" , ) -> DatasetType: '''simple docstring''' from .arrow_dataset import Dataset from .iterable_dataset import IterableDataset if not datasets: raise ValueError("Unable to interleave an empty list of datasets." ) for i, dataset in enumerate(_UpperCamelCase ): if not isinstance(_UpperCamelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCamelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} ' "is an empty dataset dictionary." ) raise ValueError( F'Dataset at position {i} has at least one split: {list(_UpperCamelCase )}\n' F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(_UpperCamelCase ) )}\']' ) raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase ).__name__}.' ) if i == 0: UpperCamelCase__ , UpperCamelCase__ = ( (Dataset, IterableDataset) if isinstance(_UpperCamelCase , _UpperCamelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError( F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.' ) if stopping_strategy not in ["first_exhausted", "all_exhausted"]: raise ValueError(F'{stopping_strategy} is not supported. Please enter a valid stopping_strategy.' ) if dataset_type is Dataset: return _interleave_map_style_datasets( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , stopping_strategy=_UpperCamelCase ) else: return _interleave_iterable_datasets( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , stopping_strategy=_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[DatasetType] , _UpperCamelCase : Optional[DatasetInfo] = None , _UpperCamelCase : Optional[NamedSplit] = None , _UpperCamelCase : int = 0 , ) -> DatasetType: '''simple docstring''' if not dsets: raise ValueError("Unable to concatenate an empty list of datasets." ) for i, dataset in enumerate(_UpperCamelCase ): if not isinstance(_UpperCamelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCamelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} ' "is an empty dataset dictionary." ) raise ValueError( F'Dataset at position {i} has at least one split: {list(_UpperCamelCase )}\n' F'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(_UpperCamelCase ) )}\']' ) raise ValueError( F'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase ).__name__}.' ) if i == 0: UpperCamelCase__ , UpperCamelCase__ = ( (Dataset, IterableDataset) if isinstance(_UpperCamelCase , _UpperCamelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError( F'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.' ) if dataset_type is Dataset: return _concatenate_map_style_datasets(_UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , axis=_UpperCamelCase ) else: return _concatenate_iterable_datasets(_UpperCamelCase , info=_UpperCamelCase , split=_UpperCamelCase , axis=_UpperCamelCase )
31
1
'''simple docstring''' import argparse from collections import defaultdict def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[Any] ) -> List[Any]: '''simple docstring''' UpperCamelCase__ = F'{file}_{class_name}_{test_name}' done_test[_id] += 1 with open(_UpperCamelCase , "r" ) as f: UpperCamelCase__ = f.readlines() UpperCamelCase__ = F'class {class_name}(' UpperCamelCase__ = F'{4 * " "}def {test_name}(' UpperCamelCase__ = F'{8 * " "}{correct_line.split()[0]}' UpperCamelCase__ = F'{16 * " "}{correct_line.split()[0]}' UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = 0 UpperCamelCase__ = 0 UpperCamelCase__ = [] for line in lines: if line.startswith(_UpperCamelCase ): UpperCamelCase__ = True elif in_class and line.startswith(_UpperCamelCase ): UpperCamelCase__ = True elif in_class and in_func and (line.startswith(_UpperCamelCase ) or line.startswith(_UpperCamelCase )): UpperCamelCase__ = len(line.split(correct_line.split()[0] )[0] ) count += 1 if count == done_test[_id]: UpperCamelCase__ = True if in_class and in_func and in_line: if ")" not in line: continue else: UpperCamelCase__ = True if in_class and in_func and in_line and insert_line: new_lines.append(F'{spaces * " "}{correct_line}' ) UpperCamelCase__ = UpperCamelCase__ = UpperCamelCase__ = UpperCamelCase__ = False else: new_lines.append(_UpperCamelCase ) with open(_UpperCamelCase , "w" ) as f: for line in new_lines: f.write(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[Any] , _UpperCamelCase : Any=None ) -> str: '''simple docstring''' if fail is not None: with open(_UpperCamelCase , "r" ) as f: UpperCamelCase__ = {l.strip() for l in f.readlines()} else: UpperCamelCase__ = None with open(_UpperCamelCase , "r" ) as f: UpperCamelCase__ = f.readlines() UpperCamelCase__ = defaultdict(_UpperCamelCase ) for line in correct_lines: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = line.split(";" ) if test_failures is None or "::".join([file, class_name, test_name] ) in test_failures: overwrite_file(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if __name__ == "__main__": __lowercase: Any = argparse.ArgumentParser() parser.add_argument("--correct_filename", help="filename of tests with expected result") parser.add_argument("--fail_filename", help="filename of test failures", type=str, default=None) __lowercase: int = parser.parse_args() main(args.correct_filename, args.fail_filename)
31
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list ) -> float: '''simple docstring''' UpperCamelCase__ = 0 while len(_UpperCamelCase ) > 1: UpperCamelCase__ = 0 # Consider two files with minimum cost to be merged for _ in range(2 ): UpperCamelCase__ = files.index(min(_UpperCamelCase ) ) temp += files[min_index] files.pop(_UpperCamelCase ) files.append(_UpperCamelCase ) optimal_merge_cost += temp return optimal_merge_cost if __name__ == "__main__": import doctest doctest.testmod()
31
1
'''simple docstring''' import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Dict = LayoutLMTokenizer _lowerCamelCase : int = LayoutLMTokenizerFast _lowerCamelCase : Optional[Any] = True _lowerCamelCase : Union[str, Any] = True def lowercase_ ( self : Dict ): """simple docstring""" super().setUp() UpperCamelCase__ = [ "[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] UpperCamelCase__ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file, "w", encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) def lowercase_ ( self : Optional[Any], **a_ : Any ): """simple docstring""" return LayoutLMTokenizer.from_pretrained(self.tmpdirname, **a_ ) def lowercase_ ( self : Optional[int], a_ : Dict ): """simple docstring""" UpperCamelCase__ = "UNwant\u00E9d,running" UpperCamelCase__ = "unwanted, running" return input_text, output_text def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = self.tokenizer_class(self.vocab_file ) UpperCamelCase__ = tokenizer.tokenize("UNwant\u00E9d,running" ) self.assertListEqual(a_, ["un", "##want", "##ed", ",", "runn", "##ing"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(a_ ), [7, 4, 5, 10, 8, 9] ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" pass
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list[int | str] ) -> None: '''simple docstring''' create_state_space_tree(_UpperCamelCase , [] , 0 , [0 for i in range(len(_UpperCamelCase ) )] ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : list[int | str] , _UpperCamelCase : list[int | str] , _UpperCamelCase : int , _UpperCamelCase : list[int] , ) -> None: '''simple docstring''' if index == len(_UpperCamelCase ): print(_UpperCamelCase ) return for i in range(len(_UpperCamelCase ) ): if not index_used[i]: current_sequence.append(sequence[i] ) UpperCamelCase__ = True create_state_space_tree(_UpperCamelCase , _UpperCamelCase , index + 1 , _UpperCamelCase ) current_sequence.pop() UpperCamelCase__ = False __lowercase: list[int | str] = [3, 1, 2, 4] generate_all_permutations(sequence) __lowercase: list[int | str] = ["A", "B", "C"] generate_all_permutations(sequence_a)
31
1
'''simple docstring''' from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase: Tuple = logging.get_logger(__name__) __lowercase: Optional[int] = { "snap-research/efficientformer-l1-300": ( "https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json" ), } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Union[str, Any] = 'efficientformer' def __init__( self : Tuple, a_ : List[int] = [3, 2, 6, 4], a_ : List[int] = [48, 96, 224, 448], a_ : List[bool] = [True, True, True, True], a_ : int = 448, a_ : int = 32, a_ : int = 4, a_ : int = 7, a_ : int = 5, a_ : int = 8, a_ : int = 4, a_ : float = 0.0, a_ : int = 16, a_ : int = 3, a_ : int = 3, a_ : int = 3, a_ : int = 2, a_ : int = 1, a_ : float = 0.0, a_ : int = 1, a_ : bool = True, a_ : bool = True, a_ : float = 1e-5, a_ : str = "gelu", a_ : float = 0.02, a_ : float = 1e-1_2, a_ : int = 224, a_ : float = 1e-0_5, **a_ : List[Any], ): """simple docstring""" super().__init__(**a_ ) UpperCamelCase__ = hidden_act UpperCamelCase__ = hidden_dropout_prob UpperCamelCase__ = hidden_sizes UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = patch_size UpperCamelCase__ = num_channels UpperCamelCase__ = depths UpperCamelCase__ = mlp_expansion_ratio UpperCamelCase__ = downsamples UpperCamelCase__ = dim UpperCamelCase__ = key_dim UpperCamelCase__ = attention_ratio UpperCamelCase__ = resolution UpperCamelCase__ = pool_size UpperCamelCase__ = downsample_patch_size UpperCamelCase__ = downsample_stride UpperCamelCase__ = downsample_pad UpperCamelCase__ = drop_path_rate UpperCamelCase__ = num_metaad_blocks UpperCamelCase__ = distillation UpperCamelCase__ = use_layer_scale UpperCamelCase__ = layer_scale_init_value UpperCamelCase__ = image_size UpperCamelCase__ = batch_norm_eps
31
'''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 __lowercase: int = logging.get_logger(__name__) __lowercase: str = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : List[str] = 'yolos' def __init__( self : List[str], a_ : Optional[int]=768, a_ : Optional[int]=12, a_ : Any=12, a_ : List[str]=3072, a_ : Any="gelu", a_ : int=0.0, a_ : List[Any]=0.0, a_ : Dict=0.02, a_ : Optional[int]=1e-1_2, a_ : List[Any]=[512, 864], a_ : Any=16, a_ : Any=3, a_ : Tuple=True, a_ : List[str]=100, a_ : Union[str, Any]=True, a_ : Any=False, a_ : List[str]=1, a_ : Tuple=5, a_ : Union[str, Any]=2, a_ : int=5, a_ : Union[str, Any]=2, a_ : Dict=0.1, **a_ : Dict, ): """simple docstring""" super().__init__(**a_ ) 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__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = image_size UpperCamelCase__ = patch_size UpperCamelCase__ = num_channels UpperCamelCase__ = qkv_bias UpperCamelCase__ = num_detection_tokens UpperCamelCase__ = use_mid_position_embeddings UpperCamelCase__ = auxiliary_loss # Hungarian matcher UpperCamelCase__ = class_cost UpperCamelCase__ = bbox_cost UpperCamelCase__ = giou_cost # Loss coefficients UpperCamelCase__ = bbox_loss_coefficient UpperCamelCase__ = giou_loss_coefficient UpperCamelCase__ = eos_coefficient class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : Union[str, Any] = version.parse('1.11') @property def lowercase_ ( self : str ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase_ ( self : Tuple ): """simple docstring""" return 1e-4 @property def lowercase_ ( self : Optional[int] ): """simple docstring""" return 12
31
1
'''simple docstring''' from dataclasses import dataclass from typing import Dict, Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .attention_processor import AttentionProcessor, AttnProcessor from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder @dataclass class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : "DiagonalGaussianDistribution" class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__): _lowerCamelCase : Any = True @register_to_config def __init__( self : Dict, a_ : int = 3, a_ : int = 3, a_ : Tuple[str] = ("DownEncoderBlock2D",), a_ : Tuple[str] = ("UpDecoderBlock2D",), a_ : Tuple[int] = (64,), a_ : int = 1, a_ : str = "silu", a_ : int = 4, a_ : int = 32, a_ : int = 32, a_ : float = 0.18_215, ): """simple docstring""" super().__init__() # pass init params to Encoder UpperCamelCase__ = Encoder( in_channels=a_, out_channels=a_, down_block_types=a_, block_out_channels=a_, layers_per_block=a_, act_fn=a_, norm_num_groups=a_, double_z=a_, ) # pass init params to Decoder UpperCamelCase__ = Decoder( in_channels=a_, out_channels=a_, up_block_types=a_, block_out_channels=a_, layers_per_block=a_, norm_num_groups=a_, act_fn=a_, ) UpperCamelCase__ = nn.Convad(2 * latent_channels, 2 * latent_channels, 1 ) UpperCamelCase__ = nn.Convad(a_, a_, 1 ) UpperCamelCase__ = False UpperCamelCase__ = False # only relevant if vae tiling is enabled UpperCamelCase__ = self.config.sample_size UpperCamelCase__ = ( self.config.sample_size[0] if isinstance(self.config.sample_size, (list, tuple) ) else self.config.sample_size ) UpperCamelCase__ = int(sample_size / (2 ** (len(self.config.block_out_channels ) - 1)) ) UpperCamelCase__ = 0.25 def lowercase_ ( self : Tuple, a_ : Tuple, a_ : List[str]=False ): """simple docstring""" if isinstance(a_, (Encoder, Decoder) ): UpperCamelCase__ = value def lowercase_ ( self : Optional[Any], a_ : bool = True ): """simple docstring""" UpperCamelCase__ = use_tiling def lowercase_ ( self : List[str] ): """simple docstring""" self.enable_tiling(a_ ) def lowercase_ ( self : Optional[int] ): """simple docstring""" UpperCamelCase__ = True def lowercase_ ( self : Tuple ): """simple docstring""" UpperCamelCase__ = False @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def lowercase_ ( self : Optional[int] ): """simple docstring""" UpperCamelCase__ = {} def fn_recursive_add_processors(a_ : str, a_ : torch.nn.Module, a_ : Dict[str, AttentionProcessor] ): if hasattr(a_, "set_processor" ): UpperCamelCase__ = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(f'{name}.{sub_name}', a_, a_ ) return processors for name, module in self.named_children(): fn_recursive_add_processors(a_, a_, a_ ) return processors def lowercase_ ( self : List[str], a_ : Union[AttentionProcessor, Dict[str, AttentionProcessor]] ): """simple docstring""" UpperCamelCase__ = len(self.attn_processors.keys() ) if isinstance(a_, a_ ) and len(a_ ) != count: raise ValueError( f'A dict of processors was passed, but the number of processors {len(a_ )} does not match the' f' number of attention layers: {count}. Please make sure to pass {count} processor classes.' ) def fn_recursive_attn_processor(a_ : str, a_ : torch.nn.Module, a_ : int ): if hasattr(a_, "set_processor" ): if not isinstance(a_, a_ ): module.set_processor(a_ ) else: module.set_processor(processor.pop(f'{name}.processor' ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(f'{name}.{sub_name}', a_, a_ ) for name, module in self.named_children(): fn_recursive_attn_processor(a_, a_, a_ ) def lowercase_ ( self : Any ): """simple docstring""" self.set_attn_processor(AttnProcessor() ) @apply_forward_hook def lowercase_ ( self : Any, a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size): return self.tiled_encode(a_, return_dict=a_ ) if self.use_slicing and x.shape[0] > 1: UpperCamelCase__ = [self.encoder(a_ ) for x_slice in x.split(1 )] UpperCamelCase__ = torch.cat(a_ ) else: UpperCamelCase__ = self.encoder(a_ ) UpperCamelCase__ = self.quant_conv(a_ ) UpperCamelCase__ = DiagonalGaussianDistribution(a_ ) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=a_ ) def lowercase_ ( self : str, a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size): return self.tiled_decode(a_, return_dict=a_ ) UpperCamelCase__ = self.post_quant_conv(a_ ) UpperCamelCase__ = self.decoder(a_ ) if not return_dict: return (dec,) return DecoderOutput(sample=a_ ) @apply_forward_hook def lowercase_ ( self : Dict, a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" if self.use_slicing and z.shape[0] > 1: UpperCamelCase__ = [self._decode(a_ ).sample for z_slice in z.split(1 )] UpperCamelCase__ = torch.cat(a_ ) else: UpperCamelCase__ = self._decode(a_ ).sample if not return_dict: return (decoded,) return DecoderOutput(sample=a_ ) def lowercase_ ( self : Optional[int], a_ : Optional[int], a_ : Tuple, a_ : Dict ): """simple docstring""" UpperCamelCase__ = min(a.shape[2], b.shape[2], a_ ) for y in range(a_ ): UpperCamelCase__ = a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent) return b def lowercase_ ( self : Optional[Any], a_ : Dict, a_ : Tuple, a_ : Any ): """simple docstring""" UpperCamelCase__ = min(a.shape[3], b.shape[3], a_ ) for x in range(a_ ): UpperCamelCase__ = a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent) return b def lowercase_ ( self : List[Any], a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" UpperCamelCase__ = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor) ) UpperCamelCase__ = int(self.tile_latent_min_size * self.tile_overlap_factor ) UpperCamelCase__ = self.tile_latent_min_size - blend_extent # Split the image into 512x512 tiles and encode them separately. UpperCamelCase__ = [] for i in range(0, x.shape[2], a_ ): UpperCamelCase__ = [] for j in range(0, x.shape[3], a_ ): UpperCamelCase__ = x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size] UpperCamelCase__ = self.encoder(a_ ) UpperCamelCase__ = self.quant_conv(a_ ) row.append(a_ ) rows.append(a_ ) UpperCamelCase__ = [] for i, row in enumerate(a_ ): UpperCamelCase__ = [] for j, tile in enumerate(a_ ): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: UpperCamelCase__ = self.blend_v(rows[i - 1][j], a_, a_ ) if j > 0: UpperCamelCase__ = self.blend_h(row[j - 1], a_, a_ ) result_row.append(tile[:, :, :row_limit, :row_limit] ) result_rows.append(torch.cat(a_, dim=3 ) ) UpperCamelCase__ = torch.cat(a_, dim=2 ) UpperCamelCase__ = DiagonalGaussianDistribution(a_ ) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=a_ ) def lowercase_ ( self : str, a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" UpperCamelCase__ = int(self.tile_latent_min_size * (1 - self.tile_overlap_factor) ) UpperCamelCase__ = int(self.tile_sample_min_size * self.tile_overlap_factor ) UpperCamelCase__ = self.tile_sample_min_size - blend_extent # Split z into overlapping 64x64 tiles and decode them separately. # The tiles have an overlap to avoid seams between tiles. UpperCamelCase__ = [] for i in range(0, z.shape[2], a_ ): UpperCamelCase__ = [] for j in range(0, z.shape[3], a_ ): UpperCamelCase__ = z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size] UpperCamelCase__ = self.post_quant_conv(a_ ) UpperCamelCase__ = self.decoder(a_ ) row.append(a_ ) rows.append(a_ ) UpperCamelCase__ = [] for i, row in enumerate(a_ ): UpperCamelCase__ = [] for j, tile in enumerate(a_ ): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: UpperCamelCase__ = self.blend_v(rows[i - 1][j], a_, a_ ) if j > 0: UpperCamelCase__ = self.blend_h(row[j - 1], a_, a_ ) result_row.append(tile[:, :, :row_limit, :row_limit] ) result_rows.append(torch.cat(a_, dim=3 ) ) UpperCamelCase__ = torch.cat(a_, dim=2 ) if not return_dict: return (dec,) return DecoderOutput(sample=a_ ) def lowercase_ ( self : Tuple, a_ : torch.FloatTensor, a_ : bool = False, a_ : bool = True, a_ : Optional[torch.Generator] = None, ): """simple docstring""" UpperCamelCase__ = sample UpperCamelCase__ = self.encode(a_ ).latent_dist if sample_posterior: UpperCamelCase__ = posterior.sample(generator=a_ ) else: UpperCamelCase__ = posterior.mode() UpperCamelCase__ = self.decode(a_ ).sample if not return_dict: return (dec,) return DecoderOutput(sample=a_ )
31
'''simple docstring''' import os from pickle import UnpicklingError from typing import Dict, Tuple import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict, unflatten_dict import transformers from .utils import logging __lowercase: int = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple=False ) -> Union[str, Any]: '''simple docstring''' try: import torch # noqa: F401 except ImportError: logger.error( "Loading a PyTorch model in Flax, requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise if not is_sharded: UpperCamelCase__ = os.path.abspath(_UpperCamelCase ) logger.info(F'Loading PyTorch weights from {pt_path}' ) UpperCamelCase__ = torch.load(_UpperCamelCase , map_location="cpu" ) logger.info(F'PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters.' ) UpperCamelCase__ = convert_pytorch_state_dict_to_flax(_UpperCamelCase , _UpperCamelCase ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files UpperCamelCase__ = convert_pytorch_sharded_state_dict_to_flax(_UpperCamelCase , _UpperCamelCase ) return flax_state_dict def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple[str] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, jnp.ndarray] , _UpperCamelCase : str , ) -> (Tuple[str], np.ndarray): '''simple docstring''' def is_key_or_prefix_key_in_dict(_UpperCamelCase : Tuple[str] ) -> bool: return len(set(_UpperCamelCase ) & {key, (model_prefix,) + key} ) > 0 # layer norm UpperCamelCase__ = pt_tuple_key[:-1] + ("scale",) if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # batch norm layer mean UpperCamelCase__ = pt_tuple_key[:-1] + ("mean",) if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # batch norm layer var UpperCamelCase__ = pt_tuple_key[:-1] + ("var",) if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # embedding UpperCamelCase__ = pt_tuple_key[:-1] + ("embedding",) if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(_UpperCamelCase ): return renamed_pt_tuple_key, pt_tensor # conv layer UpperCamelCase__ = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): UpperCamelCase__ = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer UpperCamelCase__ = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(_UpperCamelCase ): UpperCamelCase__ = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight UpperCamelCase__ = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias UpperCamelCase__ = pt_tuple_key[:-1] + ("bias",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 UpperCamelCase__ = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): UpperCamelCase__ = pt_tuple_key[-2] + "_g" elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): UpperCamelCase__ = pt_tuple_key[-2] + "_v" if name is not None: UpperCamelCase__ = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = {k: v.numpy() for k, v in pt_state_dict.items()} UpperCamelCase__ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: UpperCamelCase__ = flax_model.params["params"] else: UpperCamelCase__ = flax_model.params UpperCamelCase__ = flatten_dict(_UpperCamelCase ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: UpperCamelCase__ = flatten_dict(flax_model.params["batch_stats"] ) random_flax_state_dict.update(_UpperCamelCase ) UpperCamelCase__ = {} UpperCamelCase__ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split("." )[0] for k in pt_state_dict.keys()} ) UpperCamelCase__ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split("." )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCamelCase__ = tuple(pt_key.split("." ) ) # remove base model prefix if necessary UpperCamelCase__ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = pt_tuple_key[1:] # Correctly rename weight parameters UpperCamelCase__ , UpperCamelCase__ = rename_key_and_reshape_tensor( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # add model prefix if necessary UpperCamelCase__ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ' F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1] or "var" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(_UpperCamelCase , _UpperCamelCase ) continue # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) else: # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) return unflatten_dict(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any ) -> Any: '''simple docstring''' import torch # Load the index UpperCamelCase__ = {} for shard_file in shard_filenames: # load using msgpack utils UpperCamelCase__ = torch.load(_UpperCamelCase ) UpperCamelCase__ = {k: v.numpy() for k, v in pt_state_dict.items()} UpperCamelCase__ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict if "batch_stats" in flax_model.params: UpperCamelCase__ = flax_model.params["params"] UpperCamelCase__ = flatten_dict(_UpperCamelCase ) random_flax_state_dict.update(flatten_dict(flax_model.params["batch_stats"] ) ) else: UpperCamelCase__ = flax_model.params UpperCamelCase__ = flatten_dict(_UpperCamelCase ) UpperCamelCase__ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split("." )[0] for k in pt_state_dict.keys()} ) UpperCamelCase__ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split("." )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCamelCase__ = tuple(pt_key.split("." ) ) # remove base model prefix if necessary UpperCamelCase__ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = pt_tuple_key[1:] # Correctly rename weight parameters UpperCamelCase__ , UpperCamelCase__ = rename_key_and_reshape_tensor( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # add model prefix if necessary UpperCamelCase__ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ' F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue if "var" in flax_key[-1]: UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(_UpperCamelCase , _UpperCamelCase ) continue # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) else: # also add unexpected weight so that warning is thrown UpperCamelCase__ = jnp.asarray(_UpperCamelCase ) return unflatten_dict(_UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : Optional[int] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ = os.path.abspath(_UpperCamelCase ) logger.info(F'Loading Flax weights from {flax_checkpoint_path}' ) # import correct flax class UpperCamelCase__ = getattr(_UpperCamelCase , "Flax" + model.__class__.__name__ ) # load flax weight dict with open(_UpperCamelCase , "rb" ) as state_f: try: UpperCamelCase__ = from_bytes(_UpperCamelCase , state_f.read() ) except UnpicklingError: raise EnvironmentError(F'Unable to convert {flax_checkpoint_path} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(_UpperCamelCase , _UpperCamelCase ) def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Tuple , _UpperCamelCase : Any ) -> Optional[Any]: '''simple docstring''' try: import torch # noqa: F401 except ImportError: logger.error( "Loading a Flax weights in PyTorch, requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise # check if we have bf16 weights UpperCamelCase__ = flatten_dict(jax.tree_util.tree_map(lambda _UpperCamelCase : x.dtype == jnp.bfloataa , _UpperCamelCase ) ).values() if any(_UpperCamelCase ): # convert all weights to fp32 if the are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( "Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` " "before loading those in PyTorch model." ) UpperCamelCase__ = jax.tree_util.tree_map( lambda _UpperCamelCase : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , _UpperCamelCase ) UpperCamelCase__ = flatten_dict(_UpperCamelCase ) UpperCamelCase__ = pt_model.state_dict() UpperCamelCase__ = (pt_model.base_model_prefix in flax_state) and ( pt_model.base_model_prefix not in {k.split("." )[0] for k in pt_model_dict.keys()} ) UpperCamelCase__ = (pt_model.base_model_prefix not in flax_state) and ( pt_model.base_model_prefix in {k.split("." )[0] for k in pt_model_dict.keys()} ) # keep track of unexpected & missing keys UpperCamelCase__ = [] UpperCamelCase__ = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): UpperCamelCase__ = flax_key_tuple[0] == pt_model.base_model_prefix UpperCamelCase__ = ".".join((pt_model.base_model_prefix,) + flax_key_tuple ) in pt_model_dict # adapt flax_key to prepare for loading from/to base model only if load_model_with_head_into_base_model and has_base_model_prefix: UpperCamelCase__ = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: UpperCamelCase__ = (pt_model.base_model_prefix,) + flax_key_tuple # rename flax weights to PyTorch format if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(_UpperCamelCase ) not in pt_model_dict: # conv layer UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) UpperCamelCase__ = jnp.transpose(_UpperCamelCase , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(_UpperCamelCase ) not in pt_model_dict: # linear layer UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) UpperCamelCase__ = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: UpperCamelCase__ = flax_key_tuple[:-1] + ("weight",) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: UpperCamelCase__ = flax_key_tuple[:-1] + ("running_mean",) elif "var" in flax_key_tuple[-1]: UpperCamelCase__ = flax_key_tuple[:-1] + ("running_var",) if "batch_stats" in flax_state: UpperCamelCase__ = ".".join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: UpperCamelCase__ = ".".join(_UpperCamelCase ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. UpperCamelCase__ = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: UpperCamelCase__ = key.split("." ) UpperCamelCase__ = None if key_components[-3::2] == ["parametrizations", "original0"]: UpperCamelCase__ = key_components[-2] + "_g" elif key_components[-3::2] == ["parametrizations", "original1"]: UpperCamelCase__ = key_components[-2] + "_v" if name is not None: UpperCamelCase__ = key_components[:-3] + [name] UpperCamelCase__ = ".".join(_UpperCamelCase ) UpperCamelCase__ = key if flax_key in special_pt_names: UpperCamelCase__ = special_pt_names[flax_key] if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict UpperCamelCase__ = np.asarray(_UpperCamelCase ) if not isinstance(_UpperCamelCase , np.ndarray ) else flax_tensor UpperCamelCase__ = torch.from_numpy(_UpperCamelCase ) # remove from missing keys missing_keys.remove(_UpperCamelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(_UpperCamelCase ) pt_model.load_state_dict(_UpperCamelCase ) # re-transform missing_keys to list UpperCamelCase__ = list(_UpperCamelCase ) if len(_UpperCamelCase ) > 0: logger.warning( "Some weights of the Flax model were not used when initializing the PyTorch model" F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' " (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This" F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' " to be exactly identical (e.g. initializing a BertForSequenceClassification model from a" " FlaxBertForSequenceClassification model)." ) else: logger.warning(F'All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n' ) if len(_UpperCamelCase ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' " use it for predictions and inference." ) else: logger.warning( F'All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n' "If your task is similar to the task the model of the checkpoint was trained on, " F'you can already use {pt_model.__class__.__name__} for predictions without further training.' ) return pt_model
31
1