code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase : int = logging.get_logger(__name__) # TODO Update this _lowerCamelCase : int = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class lowercase ( a ): lowercase__ : Tuple = """esm""" def __init__( self : Optional[Any] , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Any=None , _UpperCamelCase : Dict=None , _UpperCamelCase : int=768 , _UpperCamelCase : str=12 , _UpperCamelCase : int=12 , _UpperCamelCase : Union[str, Any]=3_072 , _UpperCamelCase : Union[str, Any]=0.1 , _UpperCamelCase : Tuple=0.1 , _UpperCamelCase : int=1_026 , _UpperCamelCase : Optional[Any]=0.0_2 , _UpperCamelCase : str=1e-12 , _UpperCamelCase : Dict="absolute" , _UpperCamelCase : int=True , _UpperCamelCase : Any=None , _UpperCamelCase : List[str]=False , _UpperCamelCase : Dict=False , _UpperCamelCase : Dict=None , _UpperCamelCase : Tuple=None , **_UpperCamelCase : str , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , mask_token_id=_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = position_embedding_type SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = emb_layer_norm_before SCREAMING_SNAKE_CASE = token_dropout SCREAMING_SNAKE_CASE = is_folding_model if is_folding_model: if esmfold_config is None: logger.info("No esmfold_config supplied for folding model, using default values." ) SCREAMING_SNAKE_CASE = EsmFoldConfig() elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = EsmFoldConfig(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = esmfold_config if vocab_list is None: logger.warning("No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!" ) SCREAMING_SNAKE_CASE = get_default_vocab_list() else: SCREAMING_SNAKE_CASE = vocab_list else: SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None if self.esmfold_config is not None and getattr(self.esmfold_config , "use_esm_attn_map" , _UpperCamelCase ): raise ValueError("The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!" ) def __snake_case( self : int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = super().to_dict() if isinstance(self.esmfold_config , _UpperCamelCase ): SCREAMING_SNAKE_CASE = self.esmfold_config.to_dict() return output @dataclass class lowercase : lowercase__ : str = None lowercase__ : bool = True lowercase__ : bool = False lowercase__ : bool = False lowercase__ : bool = False lowercase__ : float = 0 lowercase__ : bool = True lowercase__ : bool = False lowercase__ : int = 128 lowercase__ : "TrunkConfig" = None def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' if self.trunk is None: SCREAMING_SNAKE_CASE = TrunkConfig() elif isinstance(self.trunk , _UpperCamelCase ): SCREAMING_SNAKE_CASE = TrunkConfig(**self.trunk ) def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = asdict(self ) SCREAMING_SNAKE_CASE = self.trunk.to_dict() return output @dataclass class lowercase : lowercase__ : int = 48 lowercase__ : int = 1_024 lowercase__ : int = 128 lowercase__ : int = 32 lowercase__ : int = 32 lowercase__ : int = 32 lowercase__ : float = 0 lowercase__ : float = 0 lowercase__ : bool = False lowercase__ : int = 4 lowercase__ : Optional[int] = 128 lowercase__ : "StructureModuleConfig" = None def __snake_case( self : Any ) -> Tuple: '''simple docstring''' if self.structure_module is None: SCREAMING_SNAKE_CASE = StructureModuleConfig() elif isinstance(self.structure_module , _UpperCamelCase ): SCREAMING_SNAKE_CASE = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(F"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( "`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got" F" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( "`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got" F" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) SCREAMING_SNAKE_CASE = self.sequence_state_dim // self.sequence_head_width SCREAMING_SNAKE_CASE = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( "`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got" F" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( "`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got" F" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(F"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(F"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = asdict(self ) SCREAMING_SNAKE_CASE = self.structure_module.to_dict() return output @dataclass class lowercase : lowercase__ : int = 384 lowercase__ : int = 128 lowercase__ : int = 16 lowercase__ : int = 128 lowercase__ : int = 12 lowercase__ : int = 4 lowercase__ : int = 8 lowercase__ : float = 0.1 lowercase__ : int = 8 lowercase__ : int = 1 lowercase__ : int = 2 lowercase__ : int = 7 lowercase__ : int = 10 lowercase__ : float = 1e-8 lowercase__ : float = 1e5 def __snake_case( self : int ) -> str: '''simple docstring''' return asdict(self ) def __lowerCamelCase (): return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
647
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
1
import argparse from transformers import ( TapasConfig, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasTokenizer, load_tf_weights_in_tapas, ) from transformers.utils import logging logging.set_verbosity_info() def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : List[str] ): # Initialise PyTorch model. # If you want to convert a checkpoint that uses absolute position embeddings, make sure to set reset_position_index_per_cell of # TapasConfig to False. # initialize configuration from json file SCREAMING_SNAKE_CASE = TapasConfig.from_json_file(UpperCAmelCase__ ) # set absolute/relative position embeddings parameter SCREAMING_SNAKE_CASE = reset_position_index_per_cell # set remaining parameters of TapasConfig as well as the model based on the task if task == "SQA": SCREAMING_SNAKE_CASE = TapasForQuestionAnswering(config=UpperCAmelCase__ ) elif task == "WTQ": # run_task_main.py hparams SCREAMING_SNAKE_CASE = 4 SCREAMING_SNAKE_CASE = True # hparam_utils.py hparams SCREAMING_SNAKE_CASE = 0.66_4694 SCREAMING_SNAKE_CASE = 0.20_7951 SCREAMING_SNAKE_CASE = 0.12_1194 SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0.035_2513 SCREAMING_SNAKE_CASE = TapasForQuestionAnswering(config=UpperCAmelCase__ ) elif task == "WIKISQL_SUPERVISED": # run_task_main.py hparams SCREAMING_SNAKE_CASE = 4 SCREAMING_SNAKE_CASE = False # hparam_utils.py hparams SCREAMING_SNAKE_CASE = 36.4519 SCREAMING_SNAKE_CASE = 0.90_3421 SCREAMING_SNAKE_CASE = 222.088 SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = 0.76_3141 SCREAMING_SNAKE_CASE = TapasForQuestionAnswering(config=UpperCAmelCase__ ) elif task == "TABFACT": SCREAMING_SNAKE_CASE = TapasForSequenceClassification(config=UpperCAmelCase__ ) elif task == "MLM": SCREAMING_SNAKE_CASE = TapasForMaskedLM(config=UpperCAmelCase__ ) elif task == "INTERMEDIATE_PRETRAINING": SCREAMING_SNAKE_CASE = TapasModel(config=UpperCAmelCase__ ) else: raise ValueError(F"Task {task} not supported." ) print(F"Building PyTorch model from configuration: {config}" ) # Load weights from tf checkpoint load_tf_weights_in_tapas(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) # Save pytorch-model (weights and configuration) print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(UpperCAmelCase__ ) # Save tokenizer files print(F"Save tokenizer files to {pytorch_dump_path}" ) SCREAMING_SNAKE_CASE = TapasTokenizer(vocab_file=tf_checkpoint_path[:-1_0] + "vocab.txt" , model_max_length=5_1_2 ) tokenizer.save_pretrained(UpperCAmelCase__ ) print("Used relative position embeddings:" , model.config.reset_position_index_per_cell ) if __name__ == "__main__": _lowerCamelCase : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''SQA''', type=str, help='''Model task for which to convert a checkpoint. Defaults to SQA.''' ) parser.add_argument( '''--reset_position_index_per_cell''', default=False, action='''store_true''', help='''Whether to use relative position embeddings or not. Defaults to True.''', ) parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--tapas_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained TAPAS model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) _lowerCamelCase : Union[str, Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.task, args.reset_position_index_per_cell, args.tf_checkpoint_path, args.tapas_config_file, args.pytorch_dump_path, )
647
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"Only mono-channel audio is supported for input to {self}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
1
import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import Callable, Dict, List, Tuple import timm import torch import torch.nn as nn from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf from huggingface_hub import cached_download, hf_hub_url from torch import Tensor from vissl.models.model_helpers import get_trunk_forward_outputs from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : Union[str, Any] = logging.get_logger() @dataclass class lowercase : lowercase__ : nn.Module lowercase__ : List[nn.Module] = field(default_factory=a ) lowercase__ : list = field(default_factory=a ) def __snake_case( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : Tensor , _UpperCamelCase : Tensor ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = len(list(m.modules() ) ) == 1 or isinstance(_UpperCamelCase , nn.Convad ) or isinstance(_UpperCamelCase , nn.BatchNormad ) if has_not_submodules: self.traced.append(_UpperCamelCase ) def __call__( self : Optional[Any] , _UpperCamelCase : Tensor ) -> Optional[int]: '''simple docstring''' for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(_UpperCamelCase ) [x.remove() for x in self.handles] return self @property def __snake_case( self : int ) -> int: '''simple docstring''' return list(filter(lambda _UpperCamelCase : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class lowercase : lowercase__ : nn.Module lowercase__ : nn.Module lowercase__ : int = 1 lowercase__ : List = field(default_factory=a ) lowercase__ : List = field(default_factory=a ) lowercase__ : bool = True def __call__( self : int , _UpperCamelCase : Tensor ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Tracker(self.dest )(_UpperCamelCase ).parametrized SCREAMING_SNAKE_CASE = Tracker(self.src )(_UpperCamelCase ).parametrized SCREAMING_SNAKE_CASE = list(filter(lambda _UpperCamelCase : type(_UpperCamelCase ) not in self.src_skip , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = list(filter(lambda _UpperCamelCase : type(_UpperCamelCase ) not in self.dest_skip , _UpperCamelCase ) ) if len(_UpperCamelCase ) != len(_UpperCamelCase ) and self.raise_if_mismatch: raise Exception( F"Numbers of operations are different. Source module has {len(_UpperCamelCase )} operations while" F" destination module has {len(_UpperCamelCase )}." ) for dest_m, src_m in zip(_UpperCamelCase , _UpperCamelCase ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(F"Transfered from={src_m} to={dest_m}" ) class lowercase ( nn.Module ): def __init__( self : str , _UpperCamelCase : nn.Module ) -> Optional[Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE = [] # - get the stem feature_blocks.append(("conv1", model.stem) ) # - get all the feature blocks for k, v in model.trunk_output.named_children(): assert k.startswith("block" ), F"Unexpected layer name {k}" SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) + 1 feature_blocks.append((F"res{block_index}", v) ) SCREAMING_SNAKE_CASE = nn.ModuleDict(_UpperCamelCase ) def __snake_case( self : Dict , _UpperCamelCase : Tensor ) -> str: '''simple docstring''' return get_trunk_forward_outputs( _UpperCamelCase , out_feat_keys=_UpperCamelCase , feature_blocks=self._feature_blocks , ) class lowercase ( a ): def __snake_case( self : int , _UpperCamelCase : str ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = x.split("-" ) return x_split[0] + x_split[1] + "_" + "".join(x_split[2:] ) def __getitem__( self : Optional[int] , _UpperCamelCase : str ) -> Callable[[], Tuple[nn.Module, Dict]]: '''simple docstring''' if x not in self: SCREAMING_SNAKE_CASE = self.convert_name_to_timm(_UpperCamelCase ) SCREAMING_SNAKE_CASE = partial(lambda: (timm.create_model(_UpperCamelCase , pretrained=_UpperCamelCase ).eval(), None) ) else: SCREAMING_SNAKE_CASE = super().__getitem__(_UpperCamelCase ) return val class lowercase ( a ): def __getitem__( self : List[str] , _UpperCamelCase : str ) -> Callable[[], nn.Module]: '''simple docstring''' if "seer" in x and "in1k" not in x: SCREAMING_SNAKE_CASE = RegNetModel else: SCREAMING_SNAKE_CASE = RegNetForImageClassification return val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : List[Tuple[str, str]] ): for from_key, to_key in keys: SCREAMING_SNAKE_CASE = from_state_dict[from_key].clone() print(F"Copied key={from_key} to={to_key}" ) return to_state_dict def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Callable[[], nn.Module] , UpperCAmelCase__ : Callable[[], nn.Module] , UpperCAmelCase__ : RegNetConfig , UpperCAmelCase__ : Path , UpperCAmelCase__ : bool = True , ): print(F"Converting {name}..." ) with torch.no_grad(): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = from_model_func() SCREAMING_SNAKE_CASE = our_model_func(UpperCAmelCase__ ).eval() SCREAMING_SNAKE_CASE = ModuleTransfer(src=UpperCAmelCase__ , dest=UpperCAmelCase__ , raise_if_mismatch=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.randn((1, 3, 2_2_4, 2_2_4) ) module_transfer(UpperCAmelCase__ ) if from_state_dict is not None: SCREAMING_SNAKE_CASE = [] # for seer - in1k finetuned we have to manually copy the head if "seer" in name and "in1k" in name: SCREAMING_SNAKE_CASE = [("0.clf.0.weight", "classifier.1.weight"), ("0.clf.0.bias", "classifier.1.bias")] SCREAMING_SNAKE_CASE = manually_copy_vissl_head(UpperCAmelCase__ , our_model.state_dict() , UpperCAmelCase__ ) our_model.load_state_dict(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = our_model(UpperCAmelCase__ , output_hidden_states=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = ( our_outputs.logits if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else our_outputs.last_hidden_state ) SCREAMING_SNAKE_CASE = from_model(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = from_output[-1] if type(UpperCAmelCase__ ) is list else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: SCREAMING_SNAKE_CASE = our_outputs.hidden_states[-1] assert torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ ), "The model logits don't match the original one." if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / name , commit_message="Add model" , use_temp_dir=UpperCAmelCase__ , ) SCREAMING_SNAKE_CASE = 2_2_4 if "seer" not in name else 3_8_4 # we can use the convnext one SCREAMING_SNAKE_CASE = AutoImageProcessor.from_pretrained("facebook/convnext-base-224-22k-1k" , size=UpperCAmelCase__ ) image_processor.push_to_hub( repo_path_or_name=save_directory / name , commit_message="Add image processor" , use_temp_dir=UpperCAmelCase__ , ) print(F"Pushed {name}" ) def __lowerCamelCase (UpperCAmelCase__ : Path , UpperCAmelCase__ : str = None , UpperCAmelCase__ : bool = True ): SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = (1, num_labels) SCREAMING_SNAKE_CASE = "huggingface/label-files" SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = json.load(open(cached_download(hf_hub_url(UpperCAmelCase__ , UpperCAmelCase__ , repo_type="dataset" ) ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = partial(UpperCAmelCase__ , num_labels=UpperCAmelCase__ , idalabel=UpperCAmelCase__ , labelaid=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = { "regnet-x-002": ImageNetPreTrainedConfig( depths=[1, 1, 4, 7] , hidden_sizes=[2_4, 5_6, 1_5_2, 3_6_8] , groups_width=8 , layer_type="x" ), "regnet-x-004": ImageNetPreTrainedConfig( depths=[1, 2, 7, 1_2] , hidden_sizes=[3_2, 6_4, 1_6_0, 3_8_4] , groups_width=1_6 , layer_type="x" ), "regnet-x-006": ImageNetPreTrainedConfig( depths=[1, 3, 5, 7] , hidden_sizes=[4_8, 9_6, 2_4_0, 5_2_8] , groups_width=2_4 , layer_type="x" ), "regnet-x-008": ImageNetPreTrainedConfig( depths=[1, 3, 7, 5] , hidden_sizes=[6_4, 1_2_8, 2_8_8, 6_7_2] , groups_width=1_6 , layer_type="x" ), "regnet-x-016": ImageNetPreTrainedConfig( depths=[2, 4, 1_0, 2] , hidden_sizes=[7_2, 1_6_8, 4_0_8, 9_1_2] , groups_width=2_4 , layer_type="x" ), "regnet-x-032": ImageNetPreTrainedConfig( depths=[2, 6, 1_5, 2] , hidden_sizes=[9_6, 1_9_2, 4_3_2, 1_0_0_8] , groups_width=4_8 , layer_type="x" ), "regnet-x-040": ImageNetPreTrainedConfig( depths=[2, 5, 1_4, 2] , hidden_sizes=[8_0, 2_4_0, 5_6_0, 1_3_6_0] , groups_width=4_0 , layer_type="x" ), "regnet-x-064": ImageNetPreTrainedConfig( depths=[2, 4, 1_0, 1] , hidden_sizes=[1_6_8, 3_9_2, 7_8_4, 1_6_2_4] , groups_width=5_6 , layer_type="x" ), "regnet-x-080": ImageNetPreTrainedConfig( depths=[2, 5, 1_5, 1] , hidden_sizes=[8_0, 2_4_0, 7_2_0, 1_9_2_0] , groups_width=1_2_0 , layer_type="x" ), "regnet-x-120": ImageNetPreTrainedConfig( depths=[2, 5, 1_1, 1] , hidden_sizes=[2_2_4, 4_4_8, 8_9_6, 2_2_4_0] , groups_width=1_1_2 , layer_type="x" ), "regnet-x-160": ImageNetPreTrainedConfig( depths=[2, 6, 1_3, 1] , hidden_sizes=[2_5_6, 5_1_2, 8_9_6, 2_0_4_8] , groups_width=1_2_8 , layer_type="x" ), "regnet-x-320": ImageNetPreTrainedConfig( depths=[2, 7, 1_3, 1] , hidden_sizes=[3_3_6, 6_7_2, 1_3_4_4, 2_5_2_0] , groups_width=1_6_8 , layer_type="x" ), # y variant "regnet-y-002": ImageNetPreTrainedConfig(depths=[1, 1, 4, 7] , hidden_sizes=[2_4, 5_6, 1_5_2, 3_6_8] , groups_width=8 ), "regnet-y-004": ImageNetPreTrainedConfig( depths=[1, 3, 6, 6] , hidden_sizes=[4_8, 1_0_4, 2_0_8, 4_4_0] , groups_width=8 ), "regnet-y-006": ImageNetPreTrainedConfig( depths=[1, 3, 7, 4] , hidden_sizes=[4_8, 1_1_2, 2_5_6, 6_0_8] , groups_width=1_6 ), "regnet-y-008": ImageNetPreTrainedConfig( depths=[1, 3, 8, 2] , hidden_sizes=[6_4, 1_2_8, 3_2_0, 7_6_8] , groups_width=1_6 ), "regnet-y-016": ImageNetPreTrainedConfig( depths=[2, 6, 1_7, 2] , hidden_sizes=[4_8, 1_2_0, 3_3_6, 8_8_8] , groups_width=2_4 ), "regnet-y-032": ImageNetPreTrainedConfig( depths=[2, 5, 1_3, 1] , hidden_sizes=[7_2, 2_1_6, 5_7_6, 1_5_1_2] , groups_width=2_4 ), "regnet-y-040": ImageNetPreTrainedConfig( depths=[2, 6, 1_2, 2] , hidden_sizes=[1_2_8, 1_9_2, 5_1_2, 1_0_8_8] , groups_width=6_4 ), "regnet-y-064": ImageNetPreTrainedConfig( depths=[2, 7, 1_4, 2] , hidden_sizes=[1_4_4, 2_8_8, 5_7_6, 1_2_9_6] , groups_width=7_2 ), "regnet-y-080": ImageNetPreTrainedConfig( depths=[2, 4, 1_0, 1] , hidden_sizes=[1_6_8, 4_4_8, 8_9_6, 2_0_1_6] , groups_width=5_6 ), "regnet-y-120": ImageNetPreTrainedConfig( depths=[2, 5, 1_1, 1] , hidden_sizes=[2_2_4, 4_4_8, 8_9_6, 2_2_4_0] , groups_width=1_1_2 ), "regnet-y-160": ImageNetPreTrainedConfig( depths=[2, 4, 1_1, 1] , hidden_sizes=[2_2_4, 4_4_8, 1_2_3_2, 3_0_2_4] , groups_width=1_1_2 ), "regnet-y-320": ImageNetPreTrainedConfig( depths=[2, 5, 1_2, 1] , hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2] , groups_width=2_3_2 ), # models created by SEER -> https://arxiv.org/abs/2202.08360 "regnet-y-320-seer": RegNetConfig(depths=[2, 5, 1_2, 1] , hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2] , groups_width=2_3_2 ), "regnet-y-640-seer": RegNetConfig(depths=[2, 5, 1_2, 1] , hidden_sizes=[3_2_8, 9_8_4, 1_9_6_8, 4_9_2_0] , groups_width=3_2_8 ), "regnet-y-1280-seer": RegNetConfig( depths=[2, 7, 1_7, 1] , hidden_sizes=[5_2_8, 1_0_5_6, 2_9_0_4, 7_3_9_2] , groups_width=2_6_4 ), "regnet-y-2560-seer": RegNetConfig( depths=[3, 7, 1_6, 1] , hidden_sizes=[6_4_0, 1_6_9_6, 2_5_4_4, 5_0_8_8] , groups_width=6_4_0 ), "regnet-y-10b-seer": ImageNetPreTrainedConfig( depths=[2, 7, 1_7, 1] , hidden_sizes=[2_0_2_0, 4_0_4_0, 1_1_1_1_0, 2_8_2_8_0] , groups_width=1_0_1_0 ), # finetuned on imagenet "regnet-y-320-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 5, 1_2, 1] , hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2] , groups_width=2_3_2 ), "regnet-y-640-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 5, 1_2, 1] , hidden_sizes=[3_2_8, 9_8_4, 1_9_6_8, 4_9_2_0] , groups_width=3_2_8 ), "regnet-y-1280-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 7, 1_7, 1] , hidden_sizes=[5_2_8, 1_0_5_6, 2_9_0_4, 7_3_9_2] , groups_width=2_6_4 ), "regnet-y-2560-seer-in1k": ImageNetPreTrainedConfig( depths=[3, 7, 1_6, 1] , hidden_sizes=[6_4_0, 1_6_9_6, 2_5_4_4, 5_0_8_8] , groups_width=6_4_0 ), "regnet-y-10b-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 7, 1_7, 1] , hidden_sizes=[2_0_2_0, 4_0_4_0, 1_1_1_1_0, 2_8_2_8_0] , groups_width=1_0_1_0 ), } SCREAMING_SNAKE_CASE = NameToOurModelFuncMap() SCREAMING_SNAKE_CASE = NameToFromModelFuncMap() # add seer weights logic def load_using_classy_vision(UpperCAmelCase__ : str , UpperCAmelCase__ : Callable[[], nn.Module] ) -> Tuple[nn.Module, Dict]: SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , model_dir=str(UpperCAmelCase__ ) , map_location="cpu" ) SCREAMING_SNAKE_CASE = model_func() # check if we have a head, if yes add it SCREAMING_SNAKE_CASE = files["classy_state_dict"]["base_model"]["model"] SCREAMING_SNAKE_CASE = model_state_dict["trunk"] model.load_state_dict(UpperCAmelCase__ ) return model.eval(), model_state_dict["heads"] # pretrained SCREAMING_SNAKE_CASE = partial( UpperCAmelCase__ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) SCREAMING_SNAKE_CASE = partial( UpperCAmelCase__ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) SCREAMING_SNAKE_CASE = partial( UpperCAmelCase__ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) SCREAMING_SNAKE_CASE = partial( UpperCAmelCase__ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=2_7 , group_width=1_0_1_0 , w_a=1_7_4_4 , w_a=620.83 , w_m=2.52 ) ) ) , ) # IN1K finetuned SCREAMING_SNAKE_CASE = partial( UpperCAmelCase__ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) SCREAMING_SNAKE_CASE = partial( UpperCAmelCase__ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) SCREAMING_SNAKE_CASE = partial( UpperCAmelCase__ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) SCREAMING_SNAKE_CASE = partial( UpperCAmelCase__ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=2_7 , group_width=1_0_1_0 , w_a=1_7_4_4 , w_a=620.83 , w_m=2.52 ) ) ) , ) if model_name: convert_weight_and_push( UpperCAmelCase__ , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , names_to_config[model_name] , UpperCAmelCase__ , UpperCAmelCase__ , ) else: for model_name, config in names_to_config.items(): convert_weight_and_push( UpperCAmelCase__ , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , ) return config, expected_shape if __name__ == "__main__": _lowerCamelCase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help=( '''The name of the model you wish to convert, it must be one of the supported regnet* architecture,''' ''' currently: regnetx-*, regnety-*. If `None`, all of them will the converted.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=Path, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=True, type=bool, required=False, help='''If True, push model and image processor to the hub.''', ) _lowerCamelCase : Optional[Any] = parser.parse_args() _lowerCamelCase : Path = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
647
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer _lowerCamelCase : Any = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast _lowerCamelCase : Optional[int] = TaTokenizerFast _lowerCamelCase : Optional[int] = {'''configuration_mt5''': ['''MT5Config''', '''MT5OnnxConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''MT5EncoderModel''', '''MT5ForConditionalGeneration''', '''MT5ForQuestionAnswering''', '''MT5Model''', '''MT5PreTrainedModel''', '''MT5Stack''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : str = ['''TFMT5EncoderModel''', '''TFMT5ForConditionalGeneration''', '''TFMT5Model'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = ['''FlaxMT5EncoderModel''', '''FlaxMT5ForConditionalGeneration''', '''FlaxMT5Model'''] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys _lowerCamelCase : Any = _LazyModule( __name__, globals()['''__file__'''], _import_structure, extra_objects={'''MT5Tokenizer''': MTaTokenizer, '''MT5TokenizerFast''': MTaTokenizerFast}, module_spec=__spec__, )
647
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
1
from typing import Tuple, Union from ...modeling_outputs import BackboneOutput from ...modeling_utils import PreTrainedModel from ...utils import is_timm_available, is_torch_available, requires_backends from ...utils.backbone_utils import BackboneMixin from .configuration_timm_backbone import TimmBackboneConfig if is_timm_available(): import timm if is_torch_available(): from torch import Tensor class lowercase ( a , a ): lowercase__ : Tuple = """pixel_values""" lowercase__ : Union[str, Any] = False lowercase__ : Dict = TimmBackboneConfig def __init__( self : Dict , _UpperCamelCase : Tuple , **_UpperCamelCase : int ) -> Optional[Any]: '''simple docstring''' requires_backends(self , "timm" ) super().__init__(_UpperCamelCase ) SCREAMING_SNAKE_CASE = config if config.backbone is None: raise ValueError("backbone is not set in the config. Please set it to a timm model name." ) if config.backbone not in timm.list_models(): raise ValueError(F"backbone {config.backbone} is not supported by timm." ) if hasattr(_UpperCamelCase , "out_features" ) and config.out_features is not None: raise ValueError("out_features is not supported by TimmBackbone. Please use out_indices instead." ) SCREAMING_SNAKE_CASE = getattr(_UpperCamelCase , "use_pretrained_backbone" , _UpperCamelCase ) if pretrained is None: raise ValueError("use_pretrained_backbone is not set in the config. Please set it to True or False." ) # We just take the final layer by default. This matches the default for the transformers models. SCREAMING_SNAKE_CASE = config.out_indices if getattr(_UpperCamelCase , "out_indices" , _UpperCamelCase ) is not None else (-1,) SCREAMING_SNAKE_CASE = timm.create_model( config.backbone , pretrained=_UpperCamelCase , features_only=config.features_only , in_chans=config.num_channels , out_indices=_UpperCamelCase , **_UpperCamelCase , ) # These are used to control the output of the model when called. If output_hidden_states is True, then # return_layers is modified to include all layers. SCREAMING_SNAKE_CASE = self._backbone.return_layers SCREAMING_SNAKE_CASE = {layer["module"]: str(_UpperCamelCase ) for i, layer in enumerate(self._backbone.feature_info.info )} super()._init_backbone(_UpperCamelCase ) @classmethod def __snake_case( cls : int , _UpperCamelCase : Dict , *_UpperCamelCase : List[str] , **_UpperCamelCase : List[Any] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["vision", "timm"] ) from ...models.timm_backbone import TimmBackboneConfig SCREAMING_SNAKE_CASE = kwargs.pop("config" , TimmBackboneConfig() ) SCREAMING_SNAKE_CASE = kwargs.pop("use_timm_backbone" , _UpperCamelCase ) if not use_timm: raise ValueError("use_timm_backbone must be True for timm backbones" ) SCREAMING_SNAKE_CASE = kwargs.pop("num_channels" , config.num_channels ) SCREAMING_SNAKE_CASE = kwargs.pop("features_only" , config.features_only ) SCREAMING_SNAKE_CASE = kwargs.pop("use_pretrained_backbone" , config.use_pretrained_backbone ) SCREAMING_SNAKE_CASE = kwargs.pop("out_indices" , config.out_indices ) SCREAMING_SNAKE_CASE = TimmBackboneConfig( backbone=_UpperCamelCase , num_channels=_UpperCamelCase , features_only=_UpperCamelCase , use_pretrained_backbone=_UpperCamelCase , out_indices=_UpperCamelCase , ) return super()._from_config(_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : int ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : Dict=None , _UpperCamelCase : List[str]=None , _UpperCamelCase : str=None , **_UpperCamelCase : List[Any] ) -> Union[BackboneOutput, Tuple[Tensor, ...]]: '''simple docstring''' SCREAMING_SNAKE_CASE = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE = output_attentions if output_attentions is not None else self.config.output_attentions if output_attentions: raise ValueError("Cannot output attentions for timm backbones at the moment" ) if output_hidden_states: # We modify the return layers to include all the stages of the backbone SCREAMING_SNAKE_CASE = self._all_layers SCREAMING_SNAKE_CASE = self._backbone(_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = self._return_layers SCREAMING_SNAKE_CASE = tuple(hidden_states[i] for i in self.out_indices ) else: SCREAMING_SNAKE_CASE = self._backbone(_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = tuple(_UpperCamelCase ) SCREAMING_SNAKE_CASE = tuple(_UpperCamelCase ) if hidden_states is not None else None if not return_dict: SCREAMING_SNAKE_CASE = (feature_maps,) if output_hidden_states: SCREAMING_SNAKE_CASE = output + (hidden_states,) return output return BackboneOutput(feature_maps=_UpperCamelCase , hidden_states=_UpperCamelCase , attentions=_UpperCamelCase )
647
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
1
import argparse import torch # Step 1. clone https://github.com/microsoft/unilm # Step 2. git checkout to https://github.com/microsoft/unilm/commit/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd # Step 3. cd unilm # Step 4. ln -s $(realpath wavlm/modules.py) ./ # create simlink # import classes from unilm.wavlm.WavLM import WavLM as WavLMOrig from unilm.wavlm.WavLM import WavLMConfig as WavLMConfigOrig from transformers import WavLMConfig, WavLMModel, logging logging.set_verbosity_info() _lowerCamelCase : str = logging.get_logger(__name__) _lowerCamelCase : Union[str, Any] = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn.grep_linear''': '''encoder.layers.*.attention.gru_rel_pos_linear''', '''self_attn.relative_attention_bias''': '''encoder.layers.*.attention.rel_attn_embed''', '''self_attn.grep_a''': '''encoder.layers.*.attention.gru_rel_pos_const''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''ctc_proj''', '''mask_emb''': '''masked_spec_embed''', } _lowerCamelCase : List[Any] = [ '''ctc_proj''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Tuple ): for attribute in key.split("." ): SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , UpperCAmelCase__ ) if weight_type is not None: SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , UpperCAmelCase__ ).shape else: SCREAMING_SNAKE_CASE = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": SCREAMING_SNAKE_CASE = value elif weight_type == "weight_g": SCREAMING_SNAKE_CASE = value elif weight_type == "weight_v": SCREAMING_SNAKE_CASE = value elif weight_type == "bias": SCREAMING_SNAKE_CASE = value else: SCREAMING_SNAKE_CASE = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = fairseq_model.state_dict() SCREAMING_SNAKE_CASE = hf_model.feature_extractor for name, value in fairseq_dict.items(): SCREAMING_SNAKE_CASE = False if "conv_layers" in name: load_conv_layer( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , hf_model.config.feat_extract_norm == "group" , ) SCREAMING_SNAKE_CASE = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: SCREAMING_SNAKE_CASE = True if "*" in mapped_key: SCREAMING_SNAKE_CASE = name.split(UpperCAmelCase__ )[0].split("." )[-2] SCREAMING_SNAKE_CASE = mapped_key.replace("*" , UpperCAmelCase__ ) if "weight_g" in name: SCREAMING_SNAKE_CASE = "weight_g" elif "weight_v" in name: SCREAMING_SNAKE_CASE = "weight_v" elif "bias" in name and "relative_attention_bias" not in name: SCREAMING_SNAKE_CASE = "bias" elif "weight" in name: # TODO: don't match quantizer.weight_proj SCREAMING_SNAKE_CASE = "weight" else: SCREAMING_SNAKE_CASE = None set_recursively(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) continue if not is_used: unused_weights.append(UpperCAmelCase__ ) logger.warning(F"Unused weights: {unused_weights}" ) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Dict , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = full_name.split("conv_layers." )[-1] SCREAMING_SNAKE_CASE = name.split("." ) SCREAMING_SNAKE_CASE = int(items[0] ) SCREAMING_SNAKE_CASE = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) SCREAMING_SNAKE_CASE = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(UpperCAmelCase__ ) @torch.no_grad() def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int=None ): # load the pre-trained checkpoints SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = WavLMConfigOrig(checkpoint["cfg"] ) SCREAMING_SNAKE_CASE = WavLMOrig(UpperCAmelCase__ ) model.load_state_dict(checkpoint["model"] ) model.eval() if config_path is not None: SCREAMING_SNAKE_CASE = WavLMConfig.from_pretrained(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = WavLMConfig() SCREAMING_SNAKE_CASE = WavLMModel(UpperCAmelCase__ ) recursively_load_weights(UpperCAmelCase__ , UpperCAmelCase__ ) hf_wavlm.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') _lowerCamelCase : Optional[Any] = parser.parse_args() convert_wavlm_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
647
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
1
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def __lowerCamelCase (UpperCAmelCase__ : Optional[int] ): # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items() ) def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : List[Any] ): SCREAMING_SNAKE_CASE = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue SCREAMING_SNAKE_CASE = key.replace("heads.cmd.mim_head.cls.predictions" , "mmm_image_head" ) SCREAMING_SNAKE_CASE = key.replace("heads.cmd.mlm_head.cls.predictions" , "mmm_text_head" ) SCREAMING_SNAKE_CASE = key.replace("heads.cmd.itm_head.cls" , "itm_head" ) SCREAMING_SNAKE_CASE = key.replace("heads.cmd.itm_head.pooler" , "itm_head.pooler" ) SCREAMING_SNAKE_CASE = key.replace("heads.cmd.clip_head.logit_scale" , "flava.logit_scale" ) SCREAMING_SNAKE_CASE = key.replace("heads.fairseq_mlm.cls.predictions" , "mlm_head" ) SCREAMING_SNAKE_CASE = key.replace("heads.imagenet.mim_head.cls.predictions" , "mim_head" ) SCREAMING_SNAKE_CASE = key.replace("mm_text_projection" , "flava.text_to_mm_projection" ) SCREAMING_SNAKE_CASE = key.replace("mm_image_projection" , "flava.image_to_mm_projection" ) SCREAMING_SNAKE_CASE = key.replace("image_encoder.module" , "flava.image_model" ) SCREAMING_SNAKE_CASE = key.replace("text_encoder.module" , "flava.text_model" ) SCREAMING_SNAKE_CASE = key.replace("mm_encoder.module.encoder.cls_token" , "flava.multimodal_model.cls_token" ) SCREAMING_SNAKE_CASE = key.replace("mm_encoder.module" , "flava.multimodal_model" ) SCREAMING_SNAKE_CASE = key.replace("text_projection" , "flava.text_projection" ) SCREAMING_SNAKE_CASE = key.replace("image_projection" , "flava.image_projection" ) SCREAMING_SNAKE_CASE = value.float() for key, value in codebook_state_dict.items(): SCREAMING_SNAKE_CASE = value return upgrade @torch.no_grad() def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[Any]=None ): if config_path is not None: SCREAMING_SNAKE_CASE = FlavaConfig.from_pretrained(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = FlavaConfig() SCREAMING_SNAKE_CASE = FlavaForPreTraining(UpperCAmelCase__ ).eval() SCREAMING_SNAKE_CASE = convert_dalle_checkpoint(UpperCAmelCase__ , UpperCAmelCase__ , save_checkpoint=UpperCAmelCase__ ) if os.path.exists(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , map_location="cpu" ) else: SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" ) SCREAMING_SNAKE_CASE = upgrade_state_dict(UpperCAmelCase__ , UpperCAmelCase__ ) hf_model.load_state_dict(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = hf_model.state_dict() SCREAMING_SNAKE_CASE = count_parameters(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = count_parameters(UpperCAmelCase__ ) + count_parameters(UpperCAmelCase__ ) assert torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) hf_model.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : List[str] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to flava checkpoint''') parser.add_argument('''--codebook_path''', default=None, type=str, help='''Path to flava codebook checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') _lowerCamelCase : Dict = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
647
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
1
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowercase ( unittest.TestCase ): def __snake_case( self : Dict ) -> Tuple: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @property def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = 3 SCREAMING_SNAKE_CASE = (32, 32) SCREAMING_SNAKE_CASE = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_UpperCamelCase ) return image @property def __snake_case( self : Dict ) -> int: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , ) return model @property def __snake_case( self : Tuple ) -> Any: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) return model @property def __snake_case( self : List[Any] ) -> Tuple: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_UpperCamelCase ) @property def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' def extract(*_UpperCamelCase : List[str] , **_UpperCamelCase : Dict ): class lowercase : def __init__( self : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = torch.ones([0] ) def __snake_case( self : List[str] , _UpperCamelCase : Optional[Any] ) -> Optional[int]: '''simple docstring''' self.pixel_values.to(_UpperCamelCase ) return self return Out() return extract def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE = self.dummy_cond_unet SCREAMING_SNAKE_CASE = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_vae SCREAMING_SNAKE_CASE = self.dummy_text_encoder SCREAMING_SNAKE_CASE = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" ) SCREAMING_SNAKE_CASE = 77 SCREAMING_SNAKE_CASE = self.dummy_image.to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE = AltDiffusionImgaImgPipeline( unet=_UpperCamelCase , scheduler=_UpperCamelCase , vae=_UpperCamelCase , text_encoder=_UpperCamelCase , tokenizer=_UpperCamelCase , safety_checker=_UpperCamelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_UpperCamelCase ) SCREAMING_SNAKE_CASE = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = "A painting of a squirrel eating a burger" SCREAMING_SNAKE_CASE = torch.Generator(device=_UpperCamelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE = alt_pipe( [prompt] , generator=_UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" , image=_UpperCamelCase , ) SCREAMING_SNAKE_CASE = output.images SCREAMING_SNAKE_CASE = torch.Generator(device=_UpperCamelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE = alt_pipe( [prompt] , generator=_UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" , image=_UpperCamelCase , return_dict=_UpperCamelCase , )[0] SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE = np.array([0.4_4_2_7, 0.3_7_3_1, 0.4_2_4_9, 0.4_9_4_1, 0.4_5_4_6, 0.4_1_4_8, 0.4_1_9_3, 0.4_6_6_6, 0.4_4_9_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5e-3 @unittest.skipIf(torch_device != "cuda" , "This test requires a GPU" ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.dummy_cond_unet SCREAMING_SNAKE_CASE = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_vae SCREAMING_SNAKE_CASE = self.dummy_text_encoder SCREAMING_SNAKE_CASE = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" ) SCREAMING_SNAKE_CASE = 77 SCREAMING_SNAKE_CASE = self.dummy_image.to(_UpperCamelCase ) # put models in fp16 SCREAMING_SNAKE_CASE = unet.half() SCREAMING_SNAKE_CASE = vae.half() SCREAMING_SNAKE_CASE = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE = AltDiffusionImgaImgPipeline( unet=_UpperCamelCase , scheduler=_UpperCamelCase , vae=_UpperCamelCase , text_encoder=_UpperCamelCase , tokenizer=_UpperCamelCase , safety_checker=_UpperCamelCase , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_UpperCamelCase ) SCREAMING_SNAKE_CASE = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = "A painting of a squirrel eating a burger" SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = alt_pipe( [prompt] , generator=_UpperCamelCase , num_inference_steps=2 , output_type="np" , image=_UpperCamelCase , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != "cuda" , "This test requires a GPU" ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE = "BAAI/AltDiffusion" SCREAMING_SNAKE_CASE = AltDiffusionImgaImgPipeline.from_pretrained( _UpperCamelCase , safety_checker=_UpperCamelCase , ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE = "A fantasy landscape, trending on artstation" SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = pipe( prompt=_UpperCamelCase , image=_UpperCamelCase , strength=0.7_5 , guidance_scale=7.5 , generator=_UpperCamelCase , output_type="np" , ) SCREAMING_SNAKE_CASE = output.images[0] SCREAMING_SNAKE_CASE = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE = np.array([0.9_3_5_8, 0.9_3_9_7, 0.9_5_9_9, 0.9_9_0_1, 1.0_0_0_0, 1.0_0_0_0, 0.9_8_8_2, 1.0_0_0_0, 1.0_0_0_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def __snake_case( self : Tuple ) -> Optional[int]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __snake_case( self : str ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) SCREAMING_SNAKE_CASE = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy" ) SCREAMING_SNAKE_CASE = "BAAI/AltDiffusion" SCREAMING_SNAKE_CASE = AltDiffusionImgaImgPipeline.from_pretrained( _UpperCamelCase , safety_checker=_UpperCamelCase , ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE = "A fantasy landscape, trending on artstation" SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = pipe( prompt=_UpperCamelCase , image=_UpperCamelCase , strength=0.7_5 , guidance_scale=7.5 , generator=_UpperCamelCase , output_type="np" , ) SCREAMING_SNAKE_CASE = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1e-2
647
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
1
from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( """The RoBERTa Model transformer with early exiting (DeeRoBERTa). """ , a , ) class lowercase ( a ): lowercase__ : Optional[int] = RobertaConfig lowercase__ : Optional[Any] = """roberta""" def __init__( self : int , _UpperCamelCase : Tuple ) -> int: '''simple docstring''' super().__init__(_UpperCamelCase ) SCREAMING_SNAKE_CASE = RobertaEmbeddings(_UpperCamelCase ) self.init_weights() @add_start_docstrings( """RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top, also takes care of multi-layer training. """ , a , ) class lowercase ( a ): lowercase__ : Optional[Any] = RobertaConfig lowercase__ : List[str] = """roberta""" def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' super().__init__(_UpperCamelCase ) SCREAMING_SNAKE_CASE = config.num_labels SCREAMING_SNAKE_CASE = config.num_hidden_layers SCREAMING_SNAKE_CASE = DeeRobertaModel(_UpperCamelCase ) SCREAMING_SNAKE_CASE = nn.Dropout(config.hidden_dropout_prob ) SCREAMING_SNAKE_CASE = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(_UpperCamelCase ) def __snake_case( self : List[str] , _UpperCamelCase : str=None , _UpperCamelCase : Dict=None , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Optional[int]=None , _UpperCamelCase : Tuple=-1 , _UpperCamelCase : int=False , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_layers try: SCREAMING_SNAKE_CASE = self.roberta( _UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , position_ids=_UpperCamelCase , head_mask=_UpperCamelCase , inputs_embeds=_UpperCamelCase , ) SCREAMING_SNAKE_CASE = outputs[1] SCREAMING_SNAKE_CASE = self.dropout(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.classifier(_UpperCamelCase ) SCREAMING_SNAKE_CASE = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: SCREAMING_SNAKE_CASE = e.message SCREAMING_SNAKE_CASE = e.exit_layer SCREAMING_SNAKE_CASE = outputs[0] if not self.training: SCREAMING_SNAKE_CASE = entropy(_UpperCamelCase ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] if labels is not None: if self.num_labels == 1: # We are doing regression SCREAMING_SNAKE_CASE = MSELoss() SCREAMING_SNAKE_CASE = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: SCREAMING_SNAKE_CASE = CrossEntropyLoss() SCREAMING_SNAKE_CASE = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits SCREAMING_SNAKE_CASE = [] for highway_exit in outputs[-1]: SCREAMING_SNAKE_CASE = highway_exit[0] if not self.training: highway_logits_all.append(_UpperCamelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression SCREAMING_SNAKE_CASE = MSELoss() SCREAMING_SNAKE_CASE = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: SCREAMING_SNAKE_CASE = CrossEntropyLoss() SCREAMING_SNAKE_CASE = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_UpperCamelCase ) if train_highway: SCREAMING_SNAKE_CASE = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: SCREAMING_SNAKE_CASE = (loss,) + outputs if not self.training: SCREAMING_SNAKE_CASE = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: SCREAMING_SNAKE_CASE = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
647
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
1
import argparse import os import re import tensorflow as tf import torch from transformers import BertConfig, BertModel from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : Any = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = os.path.abspath(UpperCAmelCase__ ) logger.info(F"Converting TensorFlow checkpoint from {tf_path}" ) # Load weights from TF model SCREAMING_SNAKE_CASE = tf.train.list_variables(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for full_name, shape in init_vars: # logger.info(f"Loading TF weight {name} with shape {shape}") SCREAMING_SNAKE_CASE = full_name.split("/" ) if full_name == "_CHECKPOINTABLE_OBJECT_GRAPH" or name[0] in ["global_step", "save_counter"]: logger.info(F"Skipping non-model layer {full_name}" ) continue if "optimizer" in full_name: logger.info(F"Skipping optimization layer {full_name}" ) continue if name[0] == "model": # ignore initial 'model' SCREAMING_SNAKE_CASE = name[1:] # figure out how many levels deep the name is SCREAMING_SNAKE_CASE = 0 for _name in name: if _name.startswith("layer_with_weights" ): depth += 1 else: break layer_depth.append(UpperCAmelCase__ ) # read data SCREAMING_SNAKE_CASE = tf.train.load_variable(UpperCAmelCase__ , UpperCAmelCase__ ) names.append("/".join(UpperCAmelCase__ ) ) arrays.append(UpperCAmelCase__ ) logger.info(F"Read a total of {len(UpperCAmelCase__ ):,} layers" ) # Sanity check if len(set(UpperCAmelCase__ ) ) != 1: raise ValueError(F"Found layer names with different depths (layer depth {list(set(UpperCAmelCase__ ) )})" ) SCREAMING_SNAKE_CASE = list(set(UpperCAmelCase__ ) )[0] if layer_depth != 1: raise ValueError( "The model contains more than just the embedding/encoder layers. This script does not handle MLM/NSP" " heads." ) # convert layers logger.info("Converting weights..." ) for full_name, array in zip(UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = full_name.split("/" ) SCREAMING_SNAKE_CASE = model SCREAMING_SNAKE_CASE = [] for i, m_name in enumerate(UpperCAmelCase__ ): if m_name == ".ATTRIBUTES": # variable names end with .ATTRIBUTES/VARIABLE_VALUE break if m_name.startswith("layer_with_weights" ): SCREAMING_SNAKE_CASE = int(m_name.split("-" )[-1] ) if layer_num <= 2: # embedding layers # layer_num 0: word_embeddings # layer_num 1: position_embeddings # layer_num 2: token_type_embeddings continue elif layer_num == 3: # embedding LayerNorm trace.extend(["embeddings", "LayerNorm"] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "embeddings" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "LayerNorm" ) elif layer_num > 3 and layer_num < config.num_hidden_layers + 4: # encoder layers trace.extend(["encoder", "layer", str(layer_num - 4 )] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "encoder" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "layer" ) SCREAMING_SNAKE_CASE = pointer[layer_num - 4] elif layer_num == config.num_hidden_layers + 4: # pooler layer trace.extend(["pooler", "dense"] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "pooler" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "dense" ) elif m_name == "embeddings": trace.append("embeddings" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "embeddings" ) if layer_num == 0: trace.append("word_embeddings" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "word_embeddings" ) elif layer_num == 1: trace.append("position_embeddings" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "position_embeddings" ) elif layer_num == 2: trace.append("token_type_embeddings" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "token_type_embeddings" ) else: raise ValueError(F"Unknown embedding layer with name {full_name}" ) trace.append("weight" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "weight" ) elif m_name == "_attention_layer": # self-attention layer trace.extend(["attention", "self"] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "attention" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "self" ) elif m_name == "_attention_layer_norm": # output attention norm trace.extend(["attention", "output", "LayerNorm"] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "attention" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "output" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "LayerNorm" ) elif m_name == "_attention_output_dense": # output attention dense trace.extend(["attention", "output", "dense"] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "attention" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "output" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "dense" ) elif m_name == "_output_dense": # output dense trace.extend(["output", "dense"] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "output" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "dense" ) elif m_name == "_output_layer_norm": # output dense trace.extend(["output", "LayerNorm"] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "output" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "LayerNorm" ) elif m_name == "_key_dense": # attention key trace.append("key" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "key" ) elif m_name == "_query_dense": # attention query trace.append("query" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "query" ) elif m_name == "_value_dense": # attention value trace.append("value" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "value" ) elif m_name == "_intermediate_dense": # attention intermediate dense trace.extend(["intermediate", "dense"] ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "intermediate" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "dense" ) elif m_name == "_output_layer_norm": # output layer norm trace.append("output" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "output" ) # weights & biases elif m_name in ["bias", "beta"]: trace.append("bias" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "bias" ) elif m_name in ["kernel", "gamma"]: trace.append("weight" ) SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "weight" ) else: logger.warning(F"Ignored {m_name}" ) # for certain layers reshape is necessary SCREAMING_SNAKE_CASE = ".".join(UpperCAmelCase__ ) if re.match(r"(\S+)\.attention\.self\.(key|value|query)\.(bias|weight)" , UpperCAmelCase__ ) or re.match( r"(\S+)\.attention\.output\.dense\.weight" , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = array.reshape(pointer.data.shape ) if "kernel" in full_name: SCREAMING_SNAKE_CASE = array.transpose() if pointer.shape == array.shape: SCREAMING_SNAKE_CASE = torch.from_numpy(UpperCAmelCase__ ) else: raise ValueError( F"Shape mismatch in layer {full_name}: Model expects shape {pointer.shape} but layer contains shape:" F" {array.shape}" ) logger.info(F"Successfully set variable {full_name} to PyTorch layer {trace}" ) return model def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : int ): # Instantiate model logger.info(F"Loading model based on config from {config_path}..." ) SCREAMING_SNAKE_CASE = BertConfig.from_json_file(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = BertModel(UpperCAmelCase__ ) # Load weights from checkpoint logger.info(F"Loading weights from checkpoint {tf_checkpoint_path}..." ) load_tfa_weights_in_bert(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) # Save pytorch-model logger.info(F"Saving PyTorch model to {pytorch_dump_path}..." ) torch.save(model.state_dict() , UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : str = argparse.ArgumentParser() parser.add_argument( '''--tf_checkpoint_path''', type=str, required=True, help='''Path to the TensorFlow 2.x checkpoint path.''' ) parser.add_argument( '''--bert_config_file''', type=str, required=True, help='''The config json file corresponding to the BERT model. This specifies the model architecture.''', ) parser.add_argument( '''--pytorch_dump_path''', type=str, required=True, help='''Path to the output PyTorch model (must include filename).''', ) _lowerCamelCase : Tuple = parser.parse_args() convert_tfa_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
647
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
1
from __future__ import annotations import numpy as np def __lowerCamelCase (UpperCAmelCase__ : list[float] ): return np.maximum(0 , UpperCAmelCase__ ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
647
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
1
import unittest from transformers import BertGenerationConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import BertGenerationDecoder, BertGenerationEncoder class lowercase : def __init__( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any]=13 , _UpperCamelCase : Any=7 , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : Any=True , _UpperCamelCase : str=99 , _UpperCamelCase : str=32 , _UpperCamelCase : Optional[int]=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Optional[int]=37 , _UpperCamelCase : Optional[Any]="gelu" , _UpperCamelCase : Dict=0.1 , _UpperCamelCase : List[Any]=0.1 , _UpperCamelCase : Optional[int]=50 , _UpperCamelCase : List[str]=0.0_2 , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : str=None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = seq_length SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_input_mask SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = scope def __snake_case( self : Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE = None if self.use_input_mask: SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, input_ids, input_mask, token_labels def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' return BertGenerationConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , ) def __snake_case( self : Any ) -> str: '''simple docstring''' ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, token_labels, encoder_hidden_states, encoder_attention_mask, ) def __snake_case( self : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : int , **_UpperCamelCase : Optional[Any] , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = BertGenerationEncoder(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : Dict , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Dict , **_UpperCamelCase : Optional[Any] , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = BertGenerationEncoder(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , encoder_attention_mask=_UpperCamelCase , ) SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] , **_UpperCamelCase : Any , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = BertGenerationDecoder(config=_UpperCamelCase ).to(_UpperCamelCase ).eval() # first forward pass SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , encoder_attention_mask=_UpperCamelCase , use_cache=_UpperCamelCase , ) SCREAMING_SNAKE_CASE = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and SCREAMING_SNAKE_CASE = torch.cat([input_ids, next_tokens] , dim=-1 ) SCREAMING_SNAKE_CASE = torch.cat([input_mask, next_mask] , dim=-1 ) SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , encoder_attention_mask=_UpperCamelCase , output_hidden_states=_UpperCamelCase , )["hidden_states"][0] SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , encoder_attention_mask=_UpperCamelCase , past_key_values=_UpperCamelCase , output_hidden_states=_UpperCamelCase , )["hidden_states"][0] # select random slice SCREAMING_SNAKE_CASE = ids_tensor((1,) , output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_SNAKE_CASE = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) def __snake_case( self : Any , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : str , *_UpperCamelCase : Tuple , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = BertGenerationDecoder(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __snake_case( self : List[str] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowercase ( a , a , a , unittest.TestCase ): lowercase__ : str = (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else () lowercase__ : Dict = (BertGenerationDecoder,) if is_torch_available() else () lowercase__ : str = ( {"""feature-extraction""": BertGenerationEncoder, """text-generation""": BertGenerationDecoder} if is_torch_available() else {} ) def __snake_case( self : Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = BertGenerationEncoderTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Dict ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def __snake_case( self : Tuple ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : List[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE = "bert" self.model_tester.create_and_check_model(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def __snake_case( self : int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_UpperCamelCase ) def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_decoder_model_past_large_inputs(*_UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = self.model_tester.prepare_config_and_inputs_for_decoder() SCREAMING_SNAKE_CASE = None self.model_tester.create_and_check_model_as_decoder( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_for_causal_lm(*_UpperCamelCase ) @slow def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" ) self.assertIsNotNone(_UpperCamelCase ) @require_torch class lowercase ( unittest.TestCase ): @slow def __snake_case( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" ) SCREAMING_SNAKE_CASE = torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(_UpperCamelCase )[0] SCREAMING_SNAKE_CASE = torch.Size([1, 8, 1_024] ) self.assertEqual(output.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor( [[[0.1_7_7_5, 0.0_0_8_3, -0.0_3_2_1], [1.6_0_0_2, 0.1_2_8_7, 0.3_9_1_2], [2.1_4_7_3, 0.5_7_9_1, 0.6_0_6_6]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _UpperCamelCase , atol=1e-4 ) ) @require_torch class lowercase ( unittest.TestCase ): @slow def __snake_case( self : Tuple ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" ) SCREAMING_SNAKE_CASE = torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(_UpperCamelCase )[0] SCREAMING_SNAKE_CASE = torch.Size([1, 8, 50_358] ) self.assertEqual(output.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor( [[[-0.5_7_8_8, -2.5_9_9_4, -3.7_0_5_4], [0.0_4_3_8, 4.7_9_9_7, 1.8_7_9_5], [1.5_8_6_2, 6.6_4_0_9, 4.4_6_3_8]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _UpperCamelCase , atol=1e-4 ) )
647
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
1
import argparse _lowerCamelCase : List[Any] = '''docs/source/_static/js/custom.js''' def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] ): with open(UpperCAmelCase__ , encoding="utf-8" , newline="\n" ) as f: SCREAMING_SNAKE_CASE = f.readlines() SCREAMING_SNAKE_CASE = 0 # First let's put the right version while not lines[index].startswith("const stableVersion =" ): index += 1 SCREAMING_SNAKE_CASE = F"const stableVersion = \"v{version}\"\n" # Then update the dictionary while not lines[index].startswith("const versionMapping = {" ): index += 1 # We go until the end while not lines[index].startswith("}" ): index += 1 # We add the new version at the end lines[index - 1] += F" \"v{version}\": \"v{version}\",\n" with open(UpperCAmelCase__ , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : str = argparse.ArgumentParser() parser.add_argument('''--version''', help='''Release version.''') _lowerCamelCase : Union[str, Any] = parser.parse_args() update_custom_js(args.version)
647
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
1
import unittest from queue import Empty from threading import Thread from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available from transformers.testing_utils import CaptureStdout, require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers import AutoModelForCausalLM @require_torch class lowercase ( unittest.TestCase ): def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = model.generate(_UpperCamelCase , max_new_tokens=10 , do_sample=_UpperCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.decode(greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(_UpperCamelCase ) model.generate(_UpperCamelCase , max_new_tokens=10 , do_sample=_UpperCamelCase , streamer=_UpperCamelCase ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE = cs.out[:-1] self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def __snake_case( self : List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = model.generate(_UpperCamelCase , max_new_tokens=10 , do_sample=_UpperCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.decode(greedy_ids[0] ) SCREAMING_SNAKE_CASE = TextIteratorStreamer(_UpperCamelCase ) SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer} SCREAMING_SNAKE_CASE = Thread(target=model.generate , kwargs=_UpperCamelCase ) thread.start() SCREAMING_SNAKE_CASE = "" for new_text in streamer: streamer_text += new_text self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def __snake_case( self : List[str] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = model.generate(_UpperCamelCase , max_new_tokens=10 , do_sample=_UpperCamelCase ) SCREAMING_SNAKE_CASE = greedy_ids[:, input_ids.shape[1] :] SCREAMING_SNAKE_CASE = tokenizer.decode(new_greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(_UpperCamelCase , skip_prompt=_UpperCamelCase ) model.generate(_UpperCamelCase , max_new_tokens=10 , do_sample=_UpperCamelCase , streamer=_UpperCamelCase ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE = cs.out[:-1] self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def __snake_case( self : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("distilgpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("distilgpt2" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = torch.ones((1, 5) , device=_UpperCamelCase ).long() * model.config.bos_token_id with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) model.generate(_UpperCamelCase , max_new_tokens=1 , do_sample=_UpperCamelCase , streamer=_UpperCamelCase ) # The prompt contains a special token, so the streamer should not print it. As such, the output text, when # re-tokenized, must only contain one token SCREAMING_SNAKE_CASE = cs.out[:-1] # Remove the final "\n" SCREAMING_SNAKE_CASE = tokenizer(_UpperCamelCase , return_tensors="pt" ) self.assertEqual(streamer_text_tokenized.input_ids.shape , (1, 1) ) def __snake_case( self : int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = TextIteratorStreamer(_UpperCamelCase , timeout=0.0_0_1 ) SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer} SCREAMING_SNAKE_CASE = Thread(target=model.generate , kwargs=_UpperCamelCase ) thread.start() # The streamer will timeout after 0.001 seconds, so an exception will be raised with self.assertRaises(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "" for new_text in streamer: streamer_text += new_text
647
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_encodec''': [ '''ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EncodecConfig''', ], '''feature_extraction_encodec''': ['''EncodecFeatureExtractor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[Any] = [ '''ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EncodecModel''', '''EncodecPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_encodec import ( ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP, EncodecConfig, ) from .feature_extraction_encodec import EncodecFeatureExtractor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encodec import ( ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST, EncodecModel, EncodecPreTrainedModel, ) else: import sys _lowerCamelCase : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(F"The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
1
from bisect import bisect from itertools import accumulate def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : str , UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = sorted(zip(UpperCAmelCase__ , UpperCAmelCase__ ) , key=lambda UpperCAmelCase__ : x[0] / x[1] , reverse=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = [i[0] for i in r], [i[1] for i in r] SCREAMING_SNAKE_CASE = list(accumulate(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = bisect(UpperCAmelCase__ , UpperCAmelCase__ ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
647
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow 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 DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
1
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch _lowerCamelCase : str = random.Random() def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[int]=1.0 , UpperCAmelCase__ : Any=None , UpperCAmelCase__ : Dict=None ): if rng is None: SCREAMING_SNAKE_CASE = global_rng SCREAMING_SNAKE_CASE = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class lowercase ( unittest.TestCase ): def __init__( self : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Dict=400 , _UpperCamelCase : Tuple=2_000 , _UpperCamelCase : Any=1 , _UpperCamelCase : Union[str, Any]=0.0 , _UpperCamelCase : List[Any]=16_000 , _UpperCamelCase : Dict=True , _UpperCamelCase : Any=80 , _UpperCamelCase : str=16 , _UpperCamelCase : Tuple=64 , _UpperCamelCase : Optional[Any]="hann_window" , _UpperCamelCase : Dict=80 , _UpperCamelCase : List[str]=7_600 , _UpperCamelCase : Union[str, Any]=1e-10 , _UpperCamelCase : Optional[Any]=True , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = min_seq_length SCREAMING_SNAKE_CASE = max_seq_length SCREAMING_SNAKE_CASE = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = num_mel_bins SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = win_length SCREAMING_SNAKE_CASE = win_function SCREAMING_SNAKE_CASE = fmin SCREAMING_SNAKE_CASE = fmax SCREAMING_SNAKE_CASE = mel_floor SCREAMING_SNAKE_CASE = return_attention_mask def __snake_case( self : Dict ) -> Union[str, Any]: '''simple docstring''' return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def __snake_case( self : List[Any] , _UpperCamelCase : List[Any]=False , _UpperCamelCase : List[Any]=False ) -> Optional[int]: '''simple docstring''' def _flatten(_UpperCamelCase : List[Any] ): return list(itertools.chain(*_UpperCamelCase ) ) if equal_length: SCREAMING_SNAKE_CASE = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase ) for x in speech_inputs] return speech_inputs def __snake_case( self : int , _UpperCamelCase : Any=False , _UpperCamelCase : Dict=False ) -> List[Any]: '''simple docstring''' if equal_length: SCREAMING_SNAKE_CASE = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase ) for x in speech_inputs] return speech_inputs @require_torch class lowercase ( a , unittest.TestCase ): lowercase__ : List[Any] = SpeechTaFeatureExtractor def __snake_case( self : str ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = SpeechTaFeatureExtractionTester(self ) def __snake_case( self : Any , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' self.assertTrue(np.all(np.mean(_UpperCamelCase , axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_UpperCamelCase , axis=0 ) - 1 ) < 1e-3 ) ) def __snake_case( self : List[str] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE = feat_extract(speech_inputs[0] , return_tensors="np" ).input_values SCREAMING_SNAKE_CASE = feat_extract(np_speech_inputs[0] , return_tensors="np" ).input_values self.assertTrue(np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) # Test batched SCREAMING_SNAKE_CASE = feat_extract(_UpperCamelCase , return_tensors="np" ).input_values SCREAMING_SNAKE_CASE = feat_extract(_UpperCamelCase , return_tensors="np" ).input_values for enc_seq_a, enc_seq_a in zip(_UpperCamelCase , _UpperCamelCase ): self.assertTrue(np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) def __snake_case( self : Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE = ["longest", "max_length", "do_not_pad"] SCREAMING_SNAKE_CASE = [None, 1_600, None] for max_length, padding in zip(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = feat_extract(_UpperCamelCase , padding=_UpperCamelCase , max_length=_UpperCamelCase , return_tensors="np" ) SCREAMING_SNAKE_CASE = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self.assertTrue(input_values[0][1_000:].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE = range(800 , 1_400 , 200 ) SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in lengths] SCREAMING_SNAKE_CASE = ["longest", "max_length", "do_not_pad"] SCREAMING_SNAKE_CASE = [None, 1_600, None] for max_length, padding in zip(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = feat_extract(_UpperCamelCase , max_length=_UpperCamelCase , padding=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1_000] ) self._check_zero_mean_unit_variance(input_values[2][:1_200] ) def __snake_case( self : List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE = feat_extract( _UpperCamelCase , truncation=_UpperCamelCase , max_length=1_000 , padding="max_length" , return_tensors="np" ) SCREAMING_SNAKE_CASE = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE = feat_extract( _UpperCamelCase , truncation=_UpperCamelCase , max_length=1_000 , padding="longest" , return_tensors="np" ) SCREAMING_SNAKE_CASE = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_000) ) SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE = feat_extract( _UpperCamelCase , truncation=_UpperCamelCase , max_length=2_000 , padding="longest" , return_tensors="np" ) SCREAMING_SNAKE_CASE = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1_000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_200) ) def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE = feature_extractor.pad([{"input_values": inputs}] , return_tensors="np" ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE = feature_extractor.pad([{"input_values": inputs}] , return_tensors="pt" ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase ) for speech_input in speech_inputs] # Test feature size SCREAMING_SNAKE_CASE = feature_extractor(audio_target=_UpperCamelCase , padding=_UpperCamelCase , return_tensors="np" ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input SCREAMING_SNAKE_CASE = feature_extractor(speech_inputs[0] , return_tensors="np" ).input_values SCREAMING_SNAKE_CASE = feature_extractor(np_speech_inputs[0] , return_tensors="np" ).input_values self.assertTrue(np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) # Test batched SCREAMING_SNAKE_CASE = feature_extractor(_UpperCamelCase , return_tensors="np" ).input_values SCREAMING_SNAKE_CASE = feature_extractor(_UpperCamelCase , return_tensors="np" ).input_values for enc_seq_a, enc_seq_a in zip(_UpperCamelCase , _UpperCamelCase ): self.assertTrue(np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase ) SCREAMING_SNAKE_CASE = feature_extractor(_UpperCamelCase , return_tensors="np" ).input_values SCREAMING_SNAKE_CASE = feature_extractor(_UpperCamelCase , return_tensors="np" ).input_values for enc_seq_a, enc_seq_a in zip(_UpperCamelCase , _UpperCamelCase ): self.assertTrue(np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) def __snake_case( self : Optional[int] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_UpperCamelCase ) == len(_UpperCamelCase ) for x, y in zip(_UpperCamelCase , processed_features[input_name] ) ) ) SCREAMING_SNAKE_CASE = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = BatchFeature({input_name: speech_inputs} , tensor_type="np" ) SCREAMING_SNAKE_CASE = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def __snake_case( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE = BatchFeature({input_name: speech_inputs} , tensor_type="pt" ) SCREAMING_SNAKE_CASE = processed_features[input_name] if len(batch_features_input.shape ) < 3: SCREAMING_SNAKE_CASE = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def __snake_case( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE = feat_extract.pad(_UpperCamelCase , padding="longest" , return_tensors="np" )[input_name] SCREAMING_SNAKE_CASE = feat_extract.pad(_UpperCamelCase , padding="longest" , return_tensors="pt" )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1e-2 ) def __snake_case( self : str ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feat_extract_dict SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = self.feature_extraction_class(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE = [len(_UpperCamelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE = feat_extract.pad(_UpperCamelCase , padding="longest" , return_tensors="np" ) self.assertIn("attention_mask" , _UpperCamelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _UpperCamelCase ) def __snake_case( self : int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feat_extract_dict SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = self.feature_extraction_class(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.feat_extract_tester.prepare_inputs_for_target() SCREAMING_SNAKE_CASE = [len(_UpperCamelCase ) for x in speech_inputs] SCREAMING_SNAKE_CASE = feat_extract.model_input_names[0] SCREAMING_SNAKE_CASE = BatchFeature({input_name: speech_inputs} ) SCREAMING_SNAKE_CASE = min(_UpperCamelCase ) SCREAMING_SNAKE_CASE = feat_extract.num_mel_bins # hack! SCREAMING_SNAKE_CASE = feat_extract.pad( _UpperCamelCase , padding="max_length" , max_length=_UpperCamelCase , truncation=_UpperCamelCase , return_tensors="np" ) self.assertIn("attention_mask" , _UpperCamelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def __snake_case( self : Dict , _UpperCamelCase : Union[str, Any] ) -> str: '''simple docstring''' from datasets import load_dataset SCREAMING_SNAKE_CASE = load_dataset("hf-internal-testing/librispeech_asr_dummy" , "clean" , split="validation" ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE = ds.sort("id" ).select(range(_UpperCamelCase ) )[:num_samples]["audio"] return [x["array"] for x in speech_samples] def __snake_case( self : str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = torch.tensor( [2.3_804e-03, 2.0_752e-03, 1.9_836e-03, 2.1_057e-03, 1.6_174e-03, 3.0_518e-04, 9.1_553e-05, 3.3_569e-04, 9.7_656e-04, 1.8_311e-03, 2.0_142e-03, 2.1_057e-03, 1.7_395e-03, 4.5_776e-04, -3.9_673e-04, 4.5_776e-04, 1.0_071e-03, 9.1_553e-05, 4.8_828e-04, 1.1_597e-03, 7.3_242e-04, 9.4_604e-04, 1.8_005e-03, 1.8_311e-03, 8.8_501e-04, 4.2_725e-04, 4.8_828e-04, 7.3_242e-04, 1.0_986e-03, 2.1_057e-03] ) # fmt: on SCREAMING_SNAKE_CASE = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE = feature_extractor(_UpperCamelCase , return_tensors="pt" ).input_values self.assertEquals(input_values.shape , (1, 93_680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _UpperCamelCase , atol=1e-6 ) ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = torch.tensor( [-2.6_8_7_0, -3.0_1_0_4, -3.1_3_5_6, -3.5_3_5_2, -3.0_0_4_4, -3.0_3_5_3, -3.4_7_1_9, -3.6_7_7_7, -3.1_5_2_0, -2.9_4_3_5, -2.6_5_5_3, -2.8_7_9_5, -2.9_9_4_4, -2.5_9_2_1, -3.0_2_7_9, -3.0_3_8_6, -3.0_8_6_4, -3.1_2_9_1, -3.2_3_5_3, -2.7_4_4_4, -2.6_8_3_1, -2.7_2_8_7, -3.1_7_6_1, -3.1_5_7_1, -3.2_7_2_6, -3.0_5_8_2, -3.1_0_0_7, -3.4_5_3_3, -3.4_6_9_5, -3.0_9_9_8] ) # fmt: on SCREAMING_SNAKE_CASE = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE = SpeechTaFeatureExtractor() SCREAMING_SNAKE_CASE = feature_extractor(audio_target=_UpperCamelCase , return_tensors="pt" ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _UpperCamelCase , atol=1e-4 ) )
647
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
1
def __lowerCamelCase (UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = [], [] while len(UpperCAmelCase__ ) > 1: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ ), max(UpperCAmelCase__ ) start.append(UpperCAmelCase__ ) end.append(UpperCAmelCase__ ) collection.remove(UpperCAmelCase__ ) collection.remove(UpperCAmelCase__ ) end.reverse() return start + collection + end if __name__ == "__main__": _lowerCamelCase : str = input('''Enter numbers separated by a comma:\n''').strip() _lowerCamelCase : List[str] = [int(item) for item in user_input.split(''',''')] print(*merge_sort(unsorted), sep=''',''')
647
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # Validation if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(UpperCAmelCase__ ) != 3 or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(UpperCAmelCase__ ) == 0: return 0 if min(UpperCAmelCase__ ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(UpperCAmelCase__ ) >= 3_6_6: raise ValueError("All days elements should be less than 366" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> int: if index > 3_6_5: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 3_0 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
647
1
import copy import inspect import unittest from transformers import PretrainedConfig, SwiftFormerConfig from transformers.testing_utils import ( require_torch, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwiftFormerForImageClassification, SwiftFormerModel from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowercase : def __init__( self : int , _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[int]=13 , _UpperCamelCase : Dict=3 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Union[str, Any]=0.1 , _UpperCamelCase : str=0.1 , _UpperCamelCase : Dict=224 , _UpperCamelCase : Optional[Any]=1_000 , _UpperCamelCase : Optional[Any]=[3, 3, 6, 4] , _UpperCamelCase : Union[str, Any]=[48, 56, 112, 220] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = layer_depths SCREAMING_SNAKE_CASE = embed_dims def __snake_case( self : Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_labels ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : List[str] ) -> Tuple: '''simple docstring''' return SwiftFormerConfig( depths=self.layer_depths , embed_dims=self.embed_dims , mlp_ratio=4 , downsamples=[True, True, True, True] , hidden_act="gelu" , num_labels=self.num_labels , down_patch_size=3 , down_stride=2 , down_pad=1 , drop_rate=0.0 , drop_path_rate=0.0 , use_layer_scale=_UpperCamelCase , layer_scale_init_value=1e-5 , ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : str ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = SwiftFormerModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dims[-1], 7, 7) ) def __snake_case( self : int , _UpperCamelCase : int , _UpperCamelCase : str , _UpperCamelCase : str ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = SwiftFormerForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) SCREAMING_SNAKE_CASE = SwiftFormerForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case( self : Any ) -> Tuple: '''simple docstring''' ((SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE)) = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : List[str] = (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else () lowercase__ : str = ( {"""feature-extraction""": SwiftFormerModel, """image-classification""": SwiftFormerForImageClassification} if is_torch_available() else {} ) lowercase__ : Optional[Any] = False lowercase__ : Optional[Any] = False lowercase__ : Any = False lowercase__ : Union[str, Any] = False lowercase__ : str = False def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = SwiftFormerModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester( self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 , num_attention_heads=12 , num_hidden_layers=12 , ) def __snake_case( self : str ) -> int: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="SwiftFormer does not use inputs_embeds" ) def __snake_case( self : List[str] ) -> Dict: '''simple docstring''' pass def __snake_case( self : Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) @slow def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = SwiftFormerModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) @unittest.skip(reason="SwiftFormer does not output attentions" ) def __snake_case( self : Dict ) -> Union[str, Any]: '''simple docstring''' pass def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' def check_hidden_states_output(_UpperCamelCase : List[str] , _UpperCamelCase : Any , _UpperCamelCase : Dict ): SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.hidden_states SCREAMING_SNAKE_CASE = 8 self.assertEqual(len(_UpperCamelCase ) , _UpperCamelCase ) # TODO # SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width) # with the width and height being successively divided by 2, after every 2 blocks for i in range(len(_UpperCamelCase ) ): self.assertEqual( hidden_states[i].shape , torch.Size( [ self.model_tester.batch_size, self.model_tester.embed_dims[i // 2], (self.model_tester.image_size // 4) // 2 ** (i // 2), (self.model_tester.image_size // 4) // 2 ** (i // 2), ] ) , ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' def _config_zero_init(_UpperCamelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE = copy.deepcopy(_UpperCamelCase ) for key in configs_no_init.__dict__.keys(): if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key: setattr(_UpperCamelCase , _UpperCamelCase , 1e-10 ) if isinstance(getattr(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) , _UpperCamelCase ): SCREAMING_SNAKE_CASE = _config_zero_init(getattr(_UpperCamelCase , _UpperCamelCase ) ) setattr(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) return configs_no_init SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9) / 1e9).round().item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def __snake_case( self : Optional[Any] ) -> List[str]: '''simple docstring''' pass def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : Optional[Any] ) -> List[str]: '''simple docstring''' return ViTImageProcessor.from_pretrained("MBZUAI/swiftformer-xs" ) if is_vision_available() else None @slow def __snake_case( self : Tuple ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = SwiftFormerForImageClassification.from_pretrained("MBZUAI/swiftformer-xs" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([[-2.1_703e00, 2.1_107e00, -2.0_811e00]] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) )
647
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : int ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
1
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
1
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 lowercase ( unittest.TestCase ): def __init__( self : List[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str=7 , _UpperCamelCase : str=3 , _UpperCamelCase : Tuple=18 , _UpperCamelCase : List[Any]=30 , _UpperCamelCase : Tuple=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : str=None , _UpperCamelCase : Dict=True , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 20} SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_center_crop SCREAMING_SNAKE_CASE = crop_size SCREAMING_SNAKE_CASE = do_flip_channel_order def __snake_case( self : Optional[Any] ) -> str: '''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 lowercase ( a , unittest.TestCase ): lowercase__ : Any = MobileViTImageProcessor if is_vision_available() else None def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = MobileViTImageProcessingTester(self ) @property def __snake_case( self : str ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_center_crop" ) ) self.assertTrue(hasattr(_UpperCamelCase , "center_crop" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_flip_channel_order" ) ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = 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} ) SCREAMING_SNAKE_CASE = 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 __snake_case( self : int ) -> Tuple: '''simple docstring''' pass def __snake_case( self : Tuple ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def __snake_case( self : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def __snake_case( self : Optional[int] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
647
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(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"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(UpperCAmelCase__ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
1
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Tuple=0.999 , UpperCAmelCase__ : int="cosine" , ): if alpha_transform_type == "cosine": def alpha_bar_fn(UpperCAmelCase__ : str ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(UpperCAmelCase__ : Union[str, Any] ): return math.exp(t * -12.0 ) else: raise ValueError(F"Unsupported alpha_tranform_type: {alpha_transform_type}" ) SCREAMING_SNAKE_CASE = [] for i in range(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = i / num_diffusion_timesteps SCREAMING_SNAKE_CASE = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(UpperCAmelCase__ ) / alpha_bar_fn(UpperCAmelCase__ ) , UpperCAmelCase__ ) ) return torch.tensor(UpperCAmelCase__ , dtype=torch.floataa ) class lowercase ( a , a ): lowercase__ : Union[str, Any] = [e.name for e in KarrasDiffusionSchedulers] lowercase__ : str = 2 @register_to_config def __init__( self : str , _UpperCamelCase : int = 1_000 , _UpperCamelCase : float = 0.0_0_0_8_5 , _UpperCamelCase : float = 0.0_1_2 , _UpperCamelCase : str = "linear" , _UpperCamelCase : Optional[Union[np.ndarray, List[float]]] = None , _UpperCamelCase : str = "epsilon" , _UpperCamelCase : str = "linspace" , _UpperCamelCase : int = 0 , ) -> str: '''simple docstring''' if trained_betas is not None: SCREAMING_SNAKE_CASE = torch.tensor(_UpperCamelCase , dtype=torch.floataa ) elif beta_schedule == "linear": SCREAMING_SNAKE_CASE = torch.linspace(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. SCREAMING_SNAKE_CASE = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , _UpperCamelCase , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule SCREAMING_SNAKE_CASE = betas_for_alpha_bar(_UpperCamelCase ) else: raise NotImplementedError(F"{beta_schedule} does is not implemented for {self.__class__}" ) SCREAMING_SNAKE_CASE = 1.0 - self.betas SCREAMING_SNAKE_CASE = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[Any]=None ) -> Dict: '''simple docstring''' if schedule_timesteps is None: SCREAMING_SNAKE_CASE = self.timesteps SCREAMING_SNAKE_CASE = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: SCREAMING_SNAKE_CASE = 1 if len(_UpperCamelCase ) > 1 else 0 else: SCREAMING_SNAKE_CASE = timestep.cpu().item() if torch.is_tensor(_UpperCamelCase ) else timestep SCREAMING_SNAKE_CASE = self._index_counter[timestep_int] return indices[pos].item() @property def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def __snake_case( self : int , _UpperCamelCase : torch.FloatTensor , _UpperCamelCase : Union[float, torch.FloatTensor] , ) -> torch.FloatTensor: '''simple docstring''' SCREAMING_SNAKE_CASE = self.index_for_timestep(_UpperCamelCase ) if self.state_in_first_order: SCREAMING_SNAKE_CASE = self.sigmas[step_index] else: SCREAMING_SNAKE_CASE = self.sigmas_interpol[step_index] SCREAMING_SNAKE_CASE = sample / ((sigma**2 + 1) ** 0.5) return sample def __snake_case( self : Any , _UpperCamelCase : int , _UpperCamelCase : Union[str, torch.device] = None , _UpperCamelCase : Optional[int] = None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = num_inference_steps SCREAMING_SNAKE_CASE = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": SCREAMING_SNAKE_CASE = np.linspace(0 , num_train_timesteps - 1 , _UpperCamelCase , dtype=_UpperCamelCase )[::-1].copy() elif self.config.timestep_spacing == "leading": SCREAMING_SNAKE_CASE = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 SCREAMING_SNAKE_CASE = (np.arange(0 , _UpperCamelCase ) * step_ratio).round()[::-1].copy().astype(_UpperCamelCase ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": SCREAMING_SNAKE_CASE = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 SCREAMING_SNAKE_CASE = (np.arange(_UpperCamelCase , 0 , -step_ratio )).round().copy().astype(_UpperCamelCase ) timesteps -= 1 else: raise ValueError( F"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'." ) SCREAMING_SNAKE_CASE = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) SCREAMING_SNAKE_CASE = torch.from_numpy(np.log(_UpperCamelCase ) ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = np.interp(_UpperCamelCase , np.arange(0 , len(_UpperCamelCase ) ) , _UpperCamelCase ) SCREAMING_SNAKE_CASE = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) SCREAMING_SNAKE_CASE = torch.from_numpy(_UpperCamelCase ).to(device=_UpperCamelCase ) # interpolate sigmas SCREAMING_SNAKE_CASE = sigmas.log().lerp(sigmas.roll(1 ).log() , 0.5 ).exp() SCREAMING_SNAKE_CASE = torch.cat([sigmas[:1], sigmas[1:].repeat_interleave(2 ), sigmas[-1:]] ) SCREAMING_SNAKE_CASE = torch.cat( [sigmas_interpol[:1], sigmas_interpol[1:].repeat_interleave(2 ), sigmas_interpol[-1:]] ) if str(_UpperCamelCase ).startswith("mps" ): # mps does not support float64 SCREAMING_SNAKE_CASE = torch.from_numpy(_UpperCamelCase ).to(_UpperCamelCase , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE = torch.from_numpy(_UpperCamelCase ).to(_UpperCamelCase ) # interpolate timesteps SCREAMING_SNAKE_CASE = self.sigma_to_t(_UpperCamelCase ).to(_UpperCamelCase , dtype=timesteps.dtype ) SCREAMING_SNAKE_CASE = torch.stack((timesteps_interpol[1:-1, None], timesteps[1:, None]) , dim=-1 ).flatten() SCREAMING_SNAKE_CASE = torch.cat([timesteps[:1], interleaved_timesteps] ) SCREAMING_SNAKE_CASE = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter SCREAMING_SNAKE_CASE = defaultdict(_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = sigma.log() # get distribution SCREAMING_SNAKE_CASE = log_sigma - self.log_sigmas[:, None] # get sigmas range SCREAMING_SNAKE_CASE = dists.ge(0 ).cumsum(dim=0 ).argmax(dim=0 ).clamp(max=self.log_sigmas.shape[0] - 2 ) SCREAMING_SNAKE_CASE = low_idx + 1 SCREAMING_SNAKE_CASE = self.log_sigmas[low_idx] SCREAMING_SNAKE_CASE = self.log_sigmas[high_idx] # interpolate sigmas SCREAMING_SNAKE_CASE = (low - log_sigma) / (low - high) SCREAMING_SNAKE_CASE = w.clamp(0 , 1 ) # transform interpolation to time range SCREAMING_SNAKE_CASE = (1 - w) * low_idx + w * high_idx SCREAMING_SNAKE_CASE = t.view(sigma.shape ) return t @property def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' return self.sample is None def __snake_case( self : Dict , _UpperCamelCase : Union[torch.FloatTensor, np.ndarray] , _UpperCamelCase : Union[float, torch.FloatTensor] , _UpperCamelCase : Union[torch.FloatTensor, np.ndarray] , _UpperCamelCase : bool = True , ) -> Union[SchedulerOutput, Tuple]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.index_for_timestep(_UpperCamelCase ) # advance index counter by 1 SCREAMING_SNAKE_CASE = timestep.cpu().item() if torch.is_tensor(_UpperCamelCase ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: SCREAMING_SNAKE_CASE = self.sigmas[step_index] SCREAMING_SNAKE_CASE = self.sigmas_interpol[step_index + 1] SCREAMING_SNAKE_CASE = self.sigmas[step_index + 1] else: # 2nd order / KDPM2's method SCREAMING_SNAKE_CASE = self.sigmas[step_index - 1] SCREAMING_SNAKE_CASE = self.sigmas_interpol[step_index] SCREAMING_SNAKE_CASE = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": SCREAMING_SNAKE_CASE = sigma_hat if self.state_in_first_order else sigma_interpol SCREAMING_SNAKE_CASE = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": SCREAMING_SNAKE_CASE = sigma_hat if self.state_in_first_order else sigma_interpol SCREAMING_SNAKE_CASE = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": raise NotImplementedError("prediction_type not implemented yet: sample" ) else: raise ValueError( F"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`" ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order SCREAMING_SNAKE_CASE = (sample - pred_original_sample) / sigma_hat # 3. delta timestep SCREAMING_SNAKE_CASE = sigma_interpol - sigma_hat # store for 2nd order step SCREAMING_SNAKE_CASE = sample else: # DPM-Solver-2 # 2. Convert to an ODE derivative for 2nd order SCREAMING_SNAKE_CASE = (sample - pred_original_sample) / sigma_interpol # 3. delta timestep SCREAMING_SNAKE_CASE = sigma_next - sigma_hat SCREAMING_SNAKE_CASE = self.sample SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : torch.FloatTensor , _UpperCamelCase : torch.FloatTensor , _UpperCamelCase : torch.FloatTensor , ) -> torch.FloatTensor: '''simple docstring''' SCREAMING_SNAKE_CASE = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(_UpperCamelCase ): # mps does not support float64 SCREAMING_SNAKE_CASE = self.timesteps.to(original_samples.device , dtype=torch.floataa ) SCREAMING_SNAKE_CASE = timesteps.to(original_samples.device , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE = self.timesteps.to(original_samples.device ) SCREAMING_SNAKE_CASE = timesteps.to(original_samples.device ) SCREAMING_SNAKE_CASE = [self.index_for_timestep(_UpperCamelCase , _UpperCamelCase ) for t in timesteps] SCREAMING_SNAKE_CASE = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): SCREAMING_SNAKE_CASE = sigma.unsqueeze(-1 ) SCREAMING_SNAKE_CASE = original_samples + noise * sigma return noisy_samples def __len__( self : str ) -> Optional[Any]: '''simple docstring''' return self.config.num_train_timesteps
647
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __snake_case( self : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
1
import argparse import os import torch from transformers.utils import WEIGHTS_NAME _lowerCamelCase : Dict = ['''small''', '''medium''', '''large'''] _lowerCamelCase : Optional[Any] = '''lm_head.decoder.weight''' _lowerCamelCase : Dict = '''lm_head.weight''' def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = d.pop(UpperCAmelCase__ ) os.makedirs(UpperCAmelCase__ , exist_ok=UpperCAmelCase__ ) torch.save(UpperCAmelCase__ , os.path.join(UpperCAmelCase__ , UpperCAmelCase__ ) ) if __name__ == "__main__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''--dialogpt_path''', default='''.''', type=str) _lowerCamelCase : int = parser.parse_args() for MODEL in DIALOGPT_MODELS: _lowerCamelCase : List[Any] = os.path.join(args.dialogpt_path, f"""{MODEL}_ft.pkl""") _lowerCamelCase : str = f"""./DialoGPT-{MODEL}""" convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
647
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
1
import os import tempfile import unittest from transformers import NezhaConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, NezhaModel, ) from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST class lowercase : def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : Any=13 , _UpperCamelCase : int=7 , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : Tuple=True , _UpperCamelCase : Any=True , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : Optional[int]=99 , _UpperCamelCase : str=32 , _UpperCamelCase : Tuple=5 , _UpperCamelCase : Optional[int]=4 , _UpperCamelCase : Optional[int]=37 , _UpperCamelCase : List[Any]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : Union[str, Any]=0.1 , _UpperCamelCase : int=128 , _UpperCamelCase : Any=32 , _UpperCamelCase : Dict=16 , _UpperCamelCase : List[str]=2 , _UpperCamelCase : Optional[Any]=0.0_2 , _UpperCamelCase : Optional[int]=3 , _UpperCamelCase : Any=4 , _UpperCamelCase : int=None , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = seq_length SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_input_mask SCREAMING_SNAKE_CASE = use_token_type_ids SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = type_vocab_size SCREAMING_SNAKE_CASE = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = num_choices SCREAMING_SNAKE_CASE = scope def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE = None if self.use_input_mask: SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __snake_case( self : List[str] ) -> int: '''simple docstring''' return NezhaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , ) def __snake_case( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __snake_case( self : Dict , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = NezhaModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , token_type_ids=_UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __snake_case( self : Dict , _UpperCamelCase : Any , _UpperCamelCase : Optional[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[int] , _UpperCamelCase : Dict , _UpperCamelCase : List[Any] , _UpperCamelCase : Any , _UpperCamelCase : int , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = NezhaModel(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , encoder_attention_mask=_UpperCamelCase , ) SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __snake_case( self : Tuple , _UpperCamelCase : Optional[int] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = NezhaForMaskedLM(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __snake_case( self : int , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Tuple , _UpperCamelCase : List[str] , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = NezhaForNextSentencePrediction(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , labels=_UpperCamelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def __snake_case( self : int , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : int , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = NezhaForPreTraining(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , labels=_UpperCamelCase , next_sentence_label=_UpperCamelCase , ) 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 __snake_case( self : int , _UpperCamelCase : int , _UpperCamelCase : Any , _UpperCamelCase : Dict , _UpperCamelCase : Dict , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[Any] , _UpperCamelCase : List[str] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = NezhaForQuestionAnswering(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , start_positions=_UpperCamelCase , end_positions=_UpperCamelCase , ) 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 __snake_case( self : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : List[str] , _UpperCamelCase : str , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = NezhaForSequenceClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case( self : Tuple , _UpperCamelCase : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Tuple ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = NezhaForTokenClassification(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : List[Any] , _UpperCamelCase : Dict , _UpperCamelCase : Optional[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_choices SCREAMING_SNAKE_CASE = NezhaForMultipleChoice(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE = model( _UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , labels=_UpperCamelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __snake_case( self : Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = config_and_inputs SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowercase ( a , a , a , unittest.TestCase ): lowercase__ : List[Any] = ( ( NezhaModel, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, ) if is_torch_available() else () ) lowercase__ : int = ( { """feature-extraction""": NezhaModel, """fill-mask""": NezhaForMaskedLM, """question-answering""": NezhaForQuestionAnswering, """text-classification""": NezhaForSequenceClassification, """token-classification""": NezhaForTokenClassification, """zero-shot""": NezhaForSequenceClassification, } if is_torch_available() else {} ) lowercase__ : Any = True def __snake_case( self : Optional[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = super()._prepare_for_class(_UpperCamelCase , _UpperCamelCase , return_labels=_UpperCamelCase ) if return_labels: if model_class in get_values(_UpperCamelCase ): SCREAMING_SNAKE_CASE = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_UpperCamelCase ) return inputs_dict def __snake_case( self : Tuple ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = NezhaModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : List[str] ) -> str: '''simple docstring''' self.config_tester.run_common_tests() def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_UpperCamelCase ) def __snake_case( self : Dict ) -> Any: '''simple docstring''' ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = self.model_tester.prepare_config_and_inputs_for_decoder() SCREAMING_SNAKE_CASE = None self.model_tester.create_and_check_model_as_decoder( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , ) def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_UpperCamelCase ) def __snake_case( self : int ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_UpperCamelCase ) def __snake_case( self : Tuple ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_next_sequence_prediction(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_UpperCamelCase ) def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_UpperCamelCase ) def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_UpperCamelCase ) def __snake_case( self : int ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_UpperCamelCase ) @slow def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = NezhaModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) @slow @require_torch_gpu def __snake_case( self : Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # NezhaForMultipleChoice behaves incorrectly in JIT environments. if model_class == NezhaForMultipleChoice: return SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.jit.trace( _UpperCamelCase , (inputs_dict["input_ids"].to("cpu" ), inputs_dict["attention_mask"].to("cpu" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_UpperCamelCase , os.path.join(_UpperCamelCase , "bert.pt" ) ) SCREAMING_SNAKE_CASE = torch.jit.load(os.path.join(_UpperCamelCase , "bert.pt" ) , map_location=_UpperCamelCase ) loaded(inputs_dict["input_ids"].to(_UpperCamelCase ) , inputs_dict["attention_mask"].to(_UpperCamelCase ) ) @require_torch class lowercase ( unittest.TestCase ): @slow def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = NezhaModel.from_pretrained("sijunhe/nezha-cn-base" ) SCREAMING_SNAKE_CASE = torch.tensor([[0, 1, 2, 3, 4, 5]] ) SCREAMING_SNAKE_CASE = torch.tensor([[0, 1, 1, 1, 1, 1]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase )[0] SCREAMING_SNAKE_CASE = torch.Size((1, 6, 768) ) self.assertEqual(output.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([[[0.0_6_8_5, 0.2_4_4_1, 0.1_1_0_2], [0.0_6_0_0, 0.1_9_0_6, 0.1_3_4_9], [0.0_2_2_1, 0.0_8_1_9, 0.0_5_8_6]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _UpperCamelCase , atol=1e-4 ) ) @slow def __snake_case( self : Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = NezhaForMaskedLM.from_pretrained("sijunhe/nezha-cn-base" ) SCREAMING_SNAKE_CASE = torch.tensor([[0, 1, 2, 3, 4, 5]] ) SCREAMING_SNAKE_CASE = torch.tensor([[1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase )[0] SCREAMING_SNAKE_CASE = torch.Size((1, 6, 21_128) ) self.assertEqual(output.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor( [[-2.7_9_3_9, -1.7_9_0_2, -2.2_1_8_9], [-2.8_5_8_5, -1.8_9_0_8, -2.3_7_2_3], [-2.6_4_9_9, -1.7_7_5_0, -2.2_5_5_8]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _UpperCamelCase , atol=1e-4 ) )
647
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"Only mono-channel audio is supported for input to {self}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
1
import argparse import glob import logging import os import sys import time from collections import defaultdict from pathlib import Path from typing import Dict, List, Tuple import numpy as np import pytorch_lightning as pl import torch from callbacks import SeqaSeqLoggingCallback, get_checkpoint_callback, get_early_stopping_callback from torch import nn from torch.utils.data import DataLoader from transformers import MBartTokenizer, TaForConditionalGeneration from transformers.models.bart.modeling_bart import shift_tokens_right from utils import ( ROUGE_KEYS, LegacySeqaSeqDataset, SeqaSeqDataset, assert_all_frozen, calculate_bleu, calculate_rouge, check_output_dir, flatten_list, freeze_embeds, freeze_params, get_git_info, label_smoothed_nll_loss, lmap, pickle_save, save_git_info, save_json, use_task_specific_params, ) # need the parent dir module sys.path.insert(2, str(Path(__file__).resolve().parents[1])) from lightning_base import BaseTransformer, add_generic_args, generic_train # noqa _lowerCamelCase : List[str] = logging.getLogger(__name__) class lowercase ( a ): lowercase__ : List[str] = """summarization""" lowercase__ : Optional[Any] = ["""loss"""] lowercase__ : str = ROUGE_KEYS lowercase__ : Optional[Any] = """rouge2""" def __init__( self : Any , _UpperCamelCase : str , **_UpperCamelCase : Optional[Any] ) -> List[Any]: '''simple docstring''' if hparams.sortish_sampler and hparams.gpus > 1: SCREAMING_SNAKE_CASE = False elif hparams.max_tokens_per_batch is not None: if hparams.gpus > 1: raise NotImplementedError("Dynamic Batch size does not work for multi-gpu training" ) if hparams.sortish_sampler: raise ValueError("--sortish_sampler and --max_tokens_per_batch may not be used simultaneously" ) super().__init__(_UpperCamelCase , num_labels=_UpperCamelCase , mode=self.mode , **_UpperCamelCase ) use_task_specific_params(self.model , "summarization" ) save_git_info(self.hparams.output_dir ) SCREAMING_SNAKE_CASE = Path(self.output_dir ) / "metrics.json" SCREAMING_SNAKE_CASE = Path(self.output_dir ) / "hparams.pkl" pickle_save(self.hparams , self.hparams_save_path ) SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = defaultdict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.config.model_type SCREAMING_SNAKE_CASE = self.config.tgt_vocab_size if self.model_type == "fsmt" else self.config.vocab_size SCREAMING_SNAKE_CASE = { "data_dir": self.hparams.data_dir, "max_source_length": self.hparams.max_source_length, "prefix": self.model.config.prefix or "", } SCREAMING_SNAKE_CASE = { "train": self.hparams.n_train, "val": self.hparams.n_val, "test": self.hparams.n_test, } SCREAMING_SNAKE_CASE = {k: v if v >= 0 else None for k, v in n_observations_per_split.items()} SCREAMING_SNAKE_CASE = { "train": self.hparams.max_target_length, "val": self.hparams.val_max_target_length, "test": self.hparams.test_max_target_length, } assert self.target_lens["train"] <= self.target_lens["val"], F"target_lens: {self.target_lens}" assert self.target_lens["train"] <= self.target_lens["test"], F"target_lens: {self.target_lens}" if self.hparams.freeze_embeds: freeze_embeds(self.model ) if self.hparams.freeze_encoder: freeze_params(self.model.get_encoder() ) assert_all_frozen(self.model.get_encoder() ) SCREAMING_SNAKE_CASE = get_git_info()["repo_sha"] SCREAMING_SNAKE_CASE = hparams.num_workers SCREAMING_SNAKE_CASE = None # default to config if self.model.config.decoder_start_token_id is None and isinstance(self.tokenizer , _UpperCamelCase ): SCREAMING_SNAKE_CASE = self.tokenizer.lang_code_to_id[hparams.tgt_lang] SCREAMING_SNAKE_CASE = self.decoder_start_token_id SCREAMING_SNAKE_CASE = ( SeqaSeqDataset if hasattr(self.tokenizer , "prepare_seq2seq_batch" ) else LegacySeqaSeqDataset ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams if self.hparams.eval_max_gen_length is not None: SCREAMING_SNAKE_CASE = self.hparams.eval_max_gen_length else: SCREAMING_SNAKE_CASE = self.model.config.max_length SCREAMING_SNAKE_CASE = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric def __snake_case( self : str , _UpperCamelCase : Dict[str, torch.Tensor] ) -> Dict[str, List[str]]: '''simple docstring''' SCREAMING_SNAKE_CASE = { k: self.tokenizer.batch_decode(v.tolist() ) if "mask" not in k else v.shape for k, v in batch.items() } save_json(_UpperCamelCase , Path(self.output_dir ) / "text_batch.json" ) save_json({k: v.tolist() for k, v in batch.items()} , Path(self.output_dir ) / "tok_batch.json" ) SCREAMING_SNAKE_CASE = True return readable_batch def __snake_case( self : int , _UpperCamelCase : Dict , **_UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' return self.model(_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Any , _UpperCamelCase : List[int] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.tokenizer.batch_decode( _UpperCamelCase , skip_special_tokens=_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase ) return lmap(str.strip , _UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.tokenizer.pad_token_id SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = batch["input_ids"], batch["attention_mask"] SCREAMING_SNAKE_CASE = batch["labels"] if isinstance(self.model , _UpperCamelCase ): SCREAMING_SNAKE_CASE = self.model._shift_right(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = shift_tokens_right(_UpperCamelCase , _UpperCamelCase ) if not self.already_saved_batch: # This would be slightly better if it only happened on rank zero SCREAMING_SNAKE_CASE = decoder_input_ids self.save_readable_batch(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self(_UpperCamelCase , attention_mask=_UpperCamelCase , decoder_input_ids=_UpperCamelCase , use_cache=_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs["logits"] if self.hparams.label_smoothing == 0: # Same behavior as modeling_bart.py, besides ignoring pad_token_id SCREAMING_SNAKE_CASE = nn.CrossEntropyLoss(ignore_index=_UpperCamelCase ) assert lm_logits.shape[-1] == self.vocab_size SCREAMING_SNAKE_CASE = ce_loss_fct(lm_logits.view(-1 , lm_logits.shape[-1] ) , tgt_ids.view(-1 ) ) else: SCREAMING_SNAKE_CASE = nn.functional.log_softmax(_UpperCamelCase , dim=-1 ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = label_smoothed_nll_loss( _UpperCamelCase , _UpperCamelCase , self.hparams.label_smoothing , ignore_index=_UpperCamelCase ) return (loss,) @property def __snake_case( self : int ) -> int: '''simple docstring''' return self.tokenizer.pad_token_id def __snake_case( self : List[str] , _UpperCamelCase : Any , _UpperCamelCase : Optional[int] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self._step(_UpperCamelCase ) SCREAMING_SNAKE_CASE = dict(zip(self.loss_names , _UpperCamelCase ) ) # tokens per batch SCREAMING_SNAKE_CASE = batch["input_ids"].ne(self.pad ).sum() + batch["labels"].ne(self.pad ).sum() SCREAMING_SNAKE_CASE = batch["input_ids"].shape[0] SCREAMING_SNAKE_CASE = batch["input_ids"].eq(self.pad ).sum() SCREAMING_SNAKE_CASE = batch["input_ids"].eq(self.pad ).float().mean() # TODO(SS): make a wandb summary metric for this return {"loss": loss_tensors[0], "log": logs} def __snake_case( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : int ) -> Dict: '''simple docstring''' return self._generative_step(_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Dict , _UpperCamelCase : Any="val" ) -> Dict: '''simple docstring''' self.step_count += 1 SCREAMING_SNAKE_CASE = {k: torch.stack([x[k] for x in outputs] ).mean() for k in self.loss_names} SCREAMING_SNAKE_CASE = losses["loss"] SCREAMING_SNAKE_CASE = { k: np.array([x[k] for x in outputs] ).mean() for k in self.metric_names + ["gen_time", "gen_len"] } SCREAMING_SNAKE_CASE = ( generative_metrics[self.val_metric] if self.val_metric in generative_metrics else losses[self.val_metric] ) SCREAMING_SNAKE_CASE = torch.tensor(_UpperCamelCase ).type_as(_UpperCamelCase ) generative_metrics.update({k: v.item() for k, v in losses.items()} ) losses.update(_UpperCamelCase ) SCREAMING_SNAKE_CASE = {F"{prefix}_avg_{k}": x for k, x in losses.items()} SCREAMING_SNAKE_CASE = self.step_count self.metrics[prefix].append(_UpperCamelCase ) # callback writes this to self.metrics_save_path SCREAMING_SNAKE_CASE = flatten_list([x["preds"] for x in outputs] ) return { "log": all_metrics, "preds": preds, F"{prefix}_loss": loss, F"{prefix}_{self.val_metric}": metric_tensor, } def __snake_case( self : Dict , _UpperCamelCase : Optional[Any] , _UpperCamelCase : int ) -> Dict: '''simple docstring''' return calculate_rouge(_UpperCamelCase , _UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : dict ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = time.time() # parser.add_argument('--eval_max_gen_length', type=int, default=None, help='never generate more than n tokens') SCREAMING_SNAKE_CASE = self.model.generate( batch["input_ids"] , attention_mask=batch["attention_mask"] , use_cache=_UpperCamelCase , decoder_start_token_id=self.decoder_start_token_id , num_beams=self.eval_beams , max_length=self.eval_max_length , ) SCREAMING_SNAKE_CASE = (time.time() - ta) / batch["input_ids"].shape[0] SCREAMING_SNAKE_CASE = self.ids_to_clean_text(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.ids_to_clean_text(batch["labels"] ) SCREAMING_SNAKE_CASE = self._step(_UpperCamelCase ) SCREAMING_SNAKE_CASE = dict(zip(self.loss_names , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = self.calc_generative_metrics(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = np.mean(lmap(_UpperCamelCase , _UpperCamelCase ) ) base_metrics.update(gen_time=_UpperCamelCase , gen_len=_UpperCamelCase , preds=_UpperCamelCase , target=_UpperCamelCase , **_UpperCamelCase ) return base_metrics def __snake_case( self : List[str] , _UpperCamelCase : str , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' return self._generative_step(_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> Dict: '''simple docstring''' return self.validation_epoch_end(_UpperCamelCase , prefix="test" ) def __snake_case( self : List[Any] , _UpperCamelCase : List[str] ) -> SeqaSeqDataset: '''simple docstring''' SCREAMING_SNAKE_CASE = self.n_obs[type_path] SCREAMING_SNAKE_CASE = self.target_lens[type_path] SCREAMING_SNAKE_CASE = self.dataset_class( self.tokenizer , type_path=_UpperCamelCase , n_obs=_UpperCamelCase , max_target_length=_UpperCamelCase , **self.dataset_kwargs , ) return dataset def __snake_case( self : int , _UpperCamelCase : str , _UpperCamelCase : int , _UpperCamelCase : bool = False ) -> DataLoader: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_dataset(_UpperCamelCase ) if self.hparams.sortish_sampler and type_path != "test" and type_path != "val": SCREAMING_SNAKE_CASE = dataset.make_sortish_sampler(_UpperCamelCase , distributed=self.hparams.gpus > 1 ) return DataLoader( _UpperCamelCase , batch_size=_UpperCamelCase , collate_fn=dataset.collate_fn , shuffle=_UpperCamelCase , num_workers=self.num_workers , sampler=_UpperCamelCase , ) elif self.hparams.max_tokens_per_batch is not None and type_path != "test" and type_path != "val": SCREAMING_SNAKE_CASE = dataset.make_dynamic_sampler( self.hparams.max_tokens_per_batch , distributed=self.hparams.gpus > 1 ) return DataLoader( _UpperCamelCase , batch_sampler=_UpperCamelCase , collate_fn=dataset.collate_fn , num_workers=self.num_workers , ) else: return DataLoader( _UpperCamelCase , batch_size=_UpperCamelCase , collate_fn=dataset.collate_fn , shuffle=_UpperCamelCase , num_workers=self.num_workers , sampler=_UpperCamelCase , ) def __snake_case( self : Dict ) -> DataLoader: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_dataloader("train" , batch_size=self.hparams.train_batch_size , shuffle=_UpperCamelCase ) return dataloader def __snake_case( self : Any ) -> DataLoader: '''simple docstring''' return self.get_dataloader("val" , batch_size=self.hparams.eval_batch_size ) def __snake_case( self : int ) -> DataLoader: '''simple docstring''' return self.get_dataloader("test" , batch_size=self.hparams.eval_batch_size ) @staticmethod def __snake_case( _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[Any] ) -> int: '''simple docstring''' BaseTransformer.add_model_specific_args(_UpperCamelCase , _UpperCamelCase ) add_generic_args(_UpperCamelCase , _UpperCamelCase ) parser.add_argument( "--max_source_length" , default=1_024 , type=_UpperCamelCase , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--max_target_length" , default=56 , type=_UpperCamelCase , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--val_max_target_length" , default=142 , type=_UpperCamelCase , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--test_max_target_length" , default=142 , type=_UpperCamelCase , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument("--freeze_encoder" , action="store_true" ) parser.add_argument("--freeze_embeds" , action="store_true" ) parser.add_argument("--sortish_sampler" , action="store_true" , default=_UpperCamelCase ) parser.add_argument("--overwrite_output_dir" , action="store_true" , default=_UpperCamelCase ) parser.add_argument("--max_tokens_per_batch" , type=_UpperCamelCase , default=_UpperCamelCase ) parser.add_argument("--logger_name" , type=_UpperCamelCase , choices=["default", "wandb", "wandb_shared"] , default="default" ) parser.add_argument("--n_train" , type=_UpperCamelCase , default=-1 , required=_UpperCamelCase , help="# examples. -1 means use all." ) parser.add_argument("--n_val" , type=_UpperCamelCase , default=500 , required=_UpperCamelCase , help="# examples. -1 means use all." ) parser.add_argument("--n_test" , type=_UpperCamelCase , default=-1 , required=_UpperCamelCase , help="# examples. -1 means use all." ) parser.add_argument( "--task" , type=_UpperCamelCase , default="summarization" , required=_UpperCamelCase , help="# examples. -1 means use all." ) parser.add_argument("--label_smoothing" , type=_UpperCamelCase , default=0.0 , required=_UpperCamelCase ) parser.add_argument("--src_lang" , type=_UpperCamelCase , default="" , required=_UpperCamelCase ) parser.add_argument("--tgt_lang" , type=_UpperCamelCase , default="" , required=_UpperCamelCase ) parser.add_argument("--eval_beams" , type=_UpperCamelCase , default=_UpperCamelCase , required=_UpperCamelCase ) parser.add_argument( "--val_metric" , type=_UpperCamelCase , default=_UpperCamelCase , required=_UpperCamelCase , choices=["bleu", "rouge2", "loss", None] ) parser.add_argument("--eval_max_gen_length" , type=_UpperCamelCase , default=_UpperCamelCase , help="never generate more than n tokens" ) parser.add_argument("--save_top_k" , type=_UpperCamelCase , default=1 , required=_UpperCamelCase , help="How many checkpoints to save" ) parser.add_argument( "--early_stopping_patience" , type=_UpperCamelCase , default=-1 , required=_UpperCamelCase , help=( "-1 means never early stop. early_stopping_patience is measured in validation checks, not epochs. So" " val_check_interval will effect it." ) , ) return parser class lowercase ( a ): lowercase__ : int = """translation""" lowercase__ : Tuple = ["""loss"""] lowercase__ : Tuple = ["""bleu"""] lowercase__ : List[Any] = """bleu""" def __init__( self : List[str] , _UpperCamelCase : int , **_UpperCamelCase : List[str] ) -> Any: '''simple docstring''' super().__init__(_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = hparams.src_lang SCREAMING_SNAKE_CASE = hparams.tgt_lang def __snake_case( self : str , _UpperCamelCase : int , _UpperCamelCase : List[str] ) -> dict: '''simple docstring''' return calculate_bleu(_UpperCamelCase , _UpperCamelCase ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Tuple=None ): Path(args.output_dir ).mkdir(exist_ok=UpperCAmelCase__ ) check_output_dir(UpperCAmelCase__ , expected_items=3 ) if model is None: if "summarization" in args.task: SCREAMING_SNAKE_CASE = SummarizationModule(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = TranslationModule(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = Path(args.data_dir ).name if ( args.logger_name == "default" or args.fast_dev_run or str(args.output_dir ).startswith("/tmp" ) or str(args.output_dir ).startswith("/var" ) ): SCREAMING_SNAKE_CASE = True # don't pollute wandb logs unnecessarily elif args.logger_name == "wandb": from pytorch_lightning.loggers import WandbLogger SCREAMING_SNAKE_CASE = os.environ.get("WANDB_PROJECT" , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = WandbLogger(name=model.output_dir.name , project=UpperCAmelCase__ ) elif args.logger_name == "wandb_shared": from pytorch_lightning.loggers import WandbLogger SCREAMING_SNAKE_CASE = WandbLogger(name=model.output_dir.name , project=F"hf_{dataset}" ) if args.early_stopping_patience >= 0: SCREAMING_SNAKE_CASE = get_early_stopping_callback(model.val_metric , args.early_stopping_patience ) else: SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = args.val_metric == "loss" SCREAMING_SNAKE_CASE = generic_train( UpperCAmelCase__ , UpperCAmelCase__ , logging_callback=SeqaSeqLoggingCallback() , checkpoint_callback=get_checkpoint_callback( args.output_dir , model.val_metric , args.save_top_k , UpperCAmelCase__ ) , early_stopping_callback=UpperCAmelCase__ , logger=UpperCAmelCase__ , ) pickle_save(model.hparams , model.output_dir / "hparams.pkl" ) if not args.do_predict: return model SCREAMING_SNAKE_CASE = "" SCREAMING_SNAKE_CASE = sorted(glob.glob(os.path.join(args.output_dir , "*.ckpt" ) , recursive=UpperCAmelCase__ ) ) if checkpoints: SCREAMING_SNAKE_CASE = checkpoints[-1] SCREAMING_SNAKE_CASE = checkpoints[-1] trainer.logger.log_hyperparams(model.hparams ) # test() without a model tests using the best checkpoint automatically trainer.test() return model if __name__ == "__main__": _lowerCamelCase : int = argparse.ArgumentParser() _lowerCamelCase : List[str] = pl.Trainer.add_argparse_args(parser) _lowerCamelCase : int = SummarizationModule.add_model_specific_args(parser, os.getcwd()) _lowerCamelCase : Any = parser.parse_args() main(args)
647
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
1
import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : Tuple = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = [] for i in range(encoder_config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F"encoder.deit.blocks.{i}.norm1.weight", F"encoder.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"encoder.deit.blocks.{i}.norm1.bias", F"encoder.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"encoder.deit.blocks.{i}.attn.proj.weight", F"encoder.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append( (F"encoder.deit.blocks.{i}.attn.proj.bias", F"encoder.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append( (F"encoder.deit.blocks.{i}.norm2.weight", F"encoder.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"encoder.deit.blocks.{i}.norm2.bias", F"encoder.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append( (F"encoder.deit.blocks.{i}.mlp.fc1.weight", F"encoder.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append( (F"encoder.deit.blocks.{i}.mlp.fc1.bias", F"encoder.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append( (F"encoder.deit.blocks.{i}.mlp.fc2.weight", F"encoder.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"encoder.deit.blocks.{i}.mlp.fc2.bias", F"encoder.encoder.layer.{i}.output.dense.bias") ) # cls token, position embeddings and patch embeddings of encoder rename_keys.extend( [ ("encoder.deit.cls_token", "encoder.embeddings.cls_token"), ("encoder.deit.pos_embed", "encoder.embeddings.position_embeddings"), ("encoder.deit.patch_embed.proj.weight", "encoder.embeddings.patch_embeddings.projection.weight"), ("encoder.deit.patch_embed.proj.bias", "encoder.embeddings.patch_embeddings.projection.bias"), ("encoder.deit.norm.weight", "encoder.layernorm.weight"), ("encoder.deit.norm.bias", "encoder.layernorm.bias"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Any ): for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) SCREAMING_SNAKE_CASE = state_dict.pop(F"encoder.deit.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = in_proj_weight[ : encoder_config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_weight[ -encoder_config.hidden_size :, : ] def __lowerCamelCase (UpperCAmelCase__ : Dict , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : str ): if "handwritten" in checkpoint_url: SCREAMING_SNAKE_CASE = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg" # industry # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-12.jpg" # have # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-10.jpg" # let # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg" # # url = "https://fki.tic.heia-fr.ch/static/img/a01-122.jpg" elif "printed" in checkpoint_url or "stage1" in checkpoint_url: SCREAMING_SNAKE_CASE = "https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ).convert("RGB" ) return im @torch.no_grad() def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = ViTConfig(image_size=3_8_4 , qkv_bias=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: SCREAMING_SNAKE_CASE = 7_6_8 elif "large" in checkpoint_url: # use ViT-large encoder SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 1_0_2_4 else: raise ValueError("Should either find 'base' or 'large' in checkpoint URL" ) # the large-printed + stage1 checkpoints uses sinusoidal position embeddings, no layernorm afterwards if "large-printed" in checkpoint_url or "stage1" in checkpoint_url: SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = "relu" SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False # load HuggingFace model SCREAMING_SNAKE_CASE = ViTModel(UpperCAmelCase__ , add_pooling_layer=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = TrOCRForCausalLM(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = VisionEncoderDecoderModel(encoder=UpperCAmelCase__ , decoder=UpperCAmelCase__ ) model.eval() # load state_dict of original model, rename some keys SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" , check_hash=UpperCAmelCase__ )["model"] SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ ) # remove parameters we don't need del state_dict["encoder.deit.head.weight"] del state_dict["encoder.deit.head.bias"] del state_dict["decoder.version"] # add prefix to decoder keys for key, val in state_dict.copy().items(): SCREAMING_SNAKE_CASE = state_dict.pop(UpperCAmelCase__ ) if key.startswith("decoder" ) and "output_projection" not in key: SCREAMING_SNAKE_CASE = val else: SCREAMING_SNAKE_CASE = val # load state dict model.load_state_dict(UpperCAmelCase__ ) # Check outputs on an image SCREAMING_SNAKE_CASE = ViTImageProcessor(size=encoder_config.image_size ) SCREAMING_SNAKE_CASE = RobertaTokenizer.from_pretrained("roberta-large" ) SCREAMING_SNAKE_CASE = TrOCRProcessor(UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = processor(images=prepare_img(UpperCAmelCase__ ) , return_tensors="pt" ).pixel_values # verify logits SCREAMING_SNAKE_CASE = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) SCREAMING_SNAKE_CASE = model(pixel_values=UpperCAmelCase__ , decoder_input_ids=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.logits SCREAMING_SNAKE_CASE = torch.Size([1, 1, 5_0_2_6_5] ) if "trocr-base-handwritten" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor( [-1.4502, -4.6683, -0.5347, -2.9291, 9.1435, -3.0571, 8.9764, 1.7560, 8.7358, -1.5311] ) elif "trocr-large-handwritten" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor( [-2.6437, -1.3129, -2.2596, -5.3455, 6.3539, 1.7604, 5.4991, 1.4702, 5.6113, 2.0170] ) elif "trocr-base-printed" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor( [-5.6816, -5.8388, 1.1398, -6.9034, 6.8505, -2.4393, 1.2284, -1.0232, -1.9661, -3.9210] ) elif "trocr-large-printed" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor( [-6.0162, -7.0959, 4.4155, -5.1063, 7.0468, -3.1631, 2.6466, -0.3081, -0.8106, -1.7535] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :1_0] , UpperCAmelCase__ , atol=1e-3 ), "First elements of logits not as expected" Path(UpperCAmelCase__ ).mkdir(exist_ok=UpperCAmelCase__ ) print(F"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(UpperCAmelCase__ ) print(F"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : Any = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) _lowerCamelCase : List[str] = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
1
import os from shutil import copyfile from typing import List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : Tuple = {'''vocab_file''': '''sentencepiece.model'''} _lowerCamelCase : Any = { '''vocab_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''', }, } _lowerCamelCase : List[str] = { '''google/rembert''': 2_56, } class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Tuple = PRETRAINED_VOCAB_FILES_MAP lowercase__ : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : Any , _UpperCamelCase : str , _UpperCamelCase : List[Any]=False , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : List[Any]=True , _UpperCamelCase : Union[str, Any]="[CLS]" , _UpperCamelCase : List[Any]="[SEP]" , _UpperCamelCase : Dict="[UNK]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Union[str, Any]="[PAD]" , _UpperCamelCase : Union[str, Any]="[CLS]" , _UpperCamelCase : Optional[int]="[MASK]" , **_UpperCamelCase : int , ) -> Optional[Any]: '''simple docstring''' super().__init__( do_lower_case=_UpperCamelCase , remove_space=_UpperCamelCase , keep_accents=_UpperCamelCase , bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , sep_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = do_lower_case SCREAMING_SNAKE_CASE = remove_space SCREAMING_SNAKE_CASE = keep_accents SCREAMING_SNAKE_CASE = vocab_file SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor() self.sp_model.Load(_UpperCamelCase ) @property def __snake_case( self : Optional[int] ) -> Dict: '''simple docstring''' return len(self.sp_model ) def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : Optional[Any] , _UpperCamelCase : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = d SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor() self.sp_model.Load(self.vocab_file ) def __snake_case( self : List[Any] , _UpperCamelCase : Dict , _UpperCamelCase : Optional[int]=False ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.sp_model.EncodeAsPieces(_UpperCamelCase ) return pieces def __snake_case( self : Dict , _UpperCamelCase : List[str] ) -> Any: '''simple docstring''' return self.sp_model.PieceToId(_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' return self.sp_model.IdToPiece(_UpperCamelCase ) def __snake_case( self : Optional[int] , _UpperCamelCase : List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.sp_model.decode_pieces(_UpperCamelCase ) return out_string def __snake_case( self : List[str] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( "You should not supply a second sequence if the provided sequence of " "ids is already formatted with special tokens for the model." ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] return [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __snake_case( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error("Vocabulary path ({}) should be a directory".format(_UpperCamelCase ) ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ): copyfile(self.vocab_file , _UpperCamelCase ) return (out_vocab_file,)
647
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) _lowerCamelCase : Optional[int] = { '''configuration_layoutlmv2''': ['''LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LayoutLMv2Config'''], '''processing_layoutlmv2''': ['''LayoutLMv2Processor'''], '''tokenization_layoutlmv2''': ['''LayoutLMv2Tokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = ['''LayoutLMv2TokenizerFast'''] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = ['''LayoutLMv2FeatureExtractor'''] _lowerCamelCase : Tuple = ['''LayoutLMv2ImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : int = [ '''LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''LayoutLMv2ForQuestionAnswering''', '''LayoutLMv2ForSequenceClassification''', '''LayoutLMv2ForTokenClassification''', '''LayoutLMv2Layer''', '''LayoutLMv2Model''', '''LayoutLMv2PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys _lowerCamelCase : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
1
import requests _lowerCamelCase : List[Any] = '''YOUR API KEY''' def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : str = giphy_api_key ): SCREAMING_SNAKE_CASE = "+".join(query.split() ) SCREAMING_SNAKE_CASE = F"https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}" SCREAMING_SNAKE_CASE = requests.get(UpperCAmelCase__ ).json()["data"] return [gif["url"] for gif in gifs] if __name__ == "__main__": print('''\n'''.join(get_gifs('''space ship''')))
647
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
1
import os import time import numpy as np import onnxruntime as ort _lowerCamelCase : Union[str, Any] = '''1''' _lowerCamelCase : Any = '''0''' _lowerCamelCase : int = '''1''' _lowerCamelCase : List[str] = ort.SessionOptions() _lowerCamelCase : List[Any] = ort.GraphOptimizationLevel.ORT_DISABLE_ALL print('''Create inference session...''') _lowerCamelCase : str = ['''TensorrtExecutionProvider''', '''CUDAExecutionProvider'''] _lowerCamelCase : Optional[int] = ort.InferenceSession('''model.onnx''', sess_options=sess_opt, providers=execution_provider) _lowerCamelCase : Optional[Any] = ort.RunOptions() _lowerCamelCase : Dict = 1_28 _lowerCamelCase : List[Any] = 1 _lowerCamelCase : Any = np.ones((batch, sequence), dtype=np.intaa) _lowerCamelCase : Union[str, Any] = np.ones((batch, sequence), dtype=np.intaa) _lowerCamelCase : Dict = np.ones((batch, sequence), dtype=np.intaa) print('''Warm up phase...''') sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('''Start inference...''') _lowerCamelCase : List[Any] = time.time() _lowerCamelCase : List[Any] = 20_00 _lowerCamelCase : Union[str, Any] = {} for iter in range(max_iters): _lowerCamelCase : Any = sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('''Average Inference Time = {:.3f} ms'''.format((time.time() - start_time) * 10_00 / max_iters))
647
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
1
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCamelCase : Tuple = { '''configuration_mctct''': ['''MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MCTCTConfig'''], '''feature_extraction_mctct''': ['''MCTCTFeatureExtractor'''], '''processing_mctct''': ['''MCTCTProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Dict = [ '''MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MCTCTForCTC''', '''MCTCTModel''', '''MCTCTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
1
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class lowercase : @staticmethod def __snake_case( *_UpperCamelCase : Tuple , **_UpperCamelCase : List[Any] ) -> Any: '''simple docstring''' pass def __lowerCamelCase (UpperCAmelCase__ : Dict ): return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. _lowerCamelCase : Dict = ( '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png''' ) @is_pipeline_test @require_torch @require_vision class lowercase ( unittest.TestCase ): lowercase__ : Union[str, Any] = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model=_UpperCamelCase , tokenizer=_UpperCamelCase , image_processor=_UpperCamelCase ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(_UpperCamelCase ) , _UpperCamelCase , "" ) ) ) SCREAMING_SNAKE_CASE = "What is the placebo?" SCREAMING_SNAKE_CASE = [ { "image": load_image(_UpperCamelCase ), "question": question, }, { "image": image, "question": question, }, { "image": image, "question": question, "word_boxes": word_boxes, }, ] return dqa_pipeline, examples def __snake_case( self : Optional[Any] , _UpperCamelCase : int , _UpperCamelCase : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = dqa_pipeline(_UpperCamelCase , top_k=2 ) self.assertEqual( _UpperCamelCase , [ [ {"score": ANY(_UpperCamelCase ), "answer": ANY(_UpperCamelCase ), "start": ANY(_UpperCamelCase ), "end": ANY(_UpperCamelCase )}, {"score": ANY(_UpperCamelCase ), "answer": ANY(_UpperCamelCase ), "start": ANY(_UpperCamelCase ), "end": ANY(_UpperCamelCase )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def __snake_case( self : List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = pipeline("document-question-answering" , model="hf-internal-testing/tiny-random-layoutlmv2" ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "How many cats are there?" SCREAMING_SNAKE_CASE = [ {"score": 0.0_0_0_1, "answer": "oy 2312/2019", "start": 38, "end": 39}, {"score": 0.0_0_0_1, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40}, ] SCREAMING_SNAKE_CASE = dqa_pipeline(image=_UpperCamelCase , question=_UpperCamelCase , top_k=2 ) self.assertEqual(nested_simplify(_UpperCamelCase , decimals=4 ) , _UpperCamelCase ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual(nested_simplify(_UpperCamelCase , decimals=4 ) , _UpperCamelCase ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably SCREAMING_SNAKE_CASE = "./tests/fixtures/tests_samples/COCO/000000039769.png" SCREAMING_SNAKE_CASE = dqa_pipeline(image=_UpperCamelCase , question=_UpperCamelCase , top_k=2 ) self.assertEqual(_UpperCamelCase , [] ) # We can optionnally pass directly the words and bounding boxes SCREAMING_SNAKE_CASE = "./tests/fixtures/tests_samples/COCO/000000039769.png" SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = dqa_pipeline(image=_UpperCamelCase , question=_UpperCamelCase , words=_UpperCamelCase , boxes=_UpperCamelCase , top_k=2 ) self.assertEqual(_UpperCamelCase , [] ) @slow @require_torch @require_detectrona @require_pytesseract def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" , revision="9977165" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=_UpperCamelCase , question=_UpperCamelCase , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.9_9_4_4, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_0_0_9, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.9_9_4_4, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_0_0_9, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ [ {"score": 0.9_9_4_4, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_0_0_9, "answer": "us-001", "start": 16, "end": 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" , revision="9977165" , max_seq_len=50 , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=_UpperCamelCase , question=_UpperCamelCase , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.9_9_7_4, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_9_4_8, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.9_9_7_4, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_9_4_8, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ [ {"score": 0.9_9_7_4, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_9_4_8, "answer": "us-001", "start": 16, "end": 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( "impira/layoutlm-document-qa" , revision="3dc6de3" , add_prefix_space=_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="impira/layoutlm-document-qa" , tokenizer=_UpperCamelCase , revision="3dc6de3" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=_UpperCamelCase , question=_UpperCamelCase , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ [ {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23}, ] ] * 2 , ) SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(_UpperCamelCase ) , _UpperCamelCase , "" ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE = dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def __snake_case( self : Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( "impira/layoutlm-document-qa" , revision="3dc6de3" , add_prefix_space=_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="impira/layoutlm-document-qa" , tokenizer=_UpperCamelCase , revision="3dc6de3" , max_seq_len=50 , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=_UpperCamelCase , question=_UpperCamelCase , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ [ {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16}, ] ] * 2 , ) SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(_UpperCamelCase ) , _UpperCamelCase , "" ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE = dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(_UpperCamelCase , decimals=4 ) , [ {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16}, ] , ) @slow @require_torch def __snake_case( self : Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="naver-clova-ix/donut-base-finetuned-docvqa" , tokenizer=AutoTokenizer.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa" ) , feature_extractor="naver-clova-ix/donut-base-finetuned-docvqa" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=_UpperCamelCase , question=_UpperCamelCase , top_k=2 ) self.assertEqual(nested_simplify(_UpperCamelCase , decimals=4 ) , [{"answer": "us-001"}] ) @require_tf @unittest.skip("Document question answering not implemented in TF" ) def __snake_case( self : List[str] ) -> Dict: '''simple docstring''' pass
647
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
1
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_gpta import GPTaTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _lowerCamelCase : Any = logging.get_logger(__name__) _lowerCamelCase : str = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCamelCase : Dict = { '''vocab_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/vocab.json''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/vocab.json''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/vocab.json''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/vocab.json''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/vocab.json''', }, '''merges_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/merges.txt''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/merges.txt''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/merges.txt''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/merges.txt''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/tokenizer.json''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/tokenizer.json''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/tokenizer.json''', }, } _lowerCamelCase : Dict = { '''gpt2''': 10_24, '''gpt2-medium''': 10_24, '''gpt2-large''': 10_24, '''gpt2-xl''': 10_24, '''distilgpt2''': 10_24, } class lowercase ( a ): lowercase__ : Dict = VOCAB_FILES_NAMES lowercase__ : int = PRETRAINED_VOCAB_FILES_MAP lowercase__ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Optional[Any] = ["""input_ids""", """attention_mask"""] lowercase__ : Tuple = GPTaTokenizer def __init__( self : str , _UpperCamelCase : int=None , _UpperCamelCase : Any=None , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Dict="<|endoftext|>" , _UpperCamelCase : Union[str, Any]="<|endoftext|>" , _UpperCamelCase : Any="<|endoftext|>" , _UpperCamelCase : List[Any]=False , **_UpperCamelCase : Tuple , ) -> Optional[Any]: '''simple docstring''' super().__init__( _UpperCamelCase , _UpperCamelCase , tokenizer_file=_UpperCamelCase , unk_token=_UpperCamelCase , bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , add_prefix_space=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = kwargs.pop("add_bos_token" , _UpperCamelCase ) SCREAMING_SNAKE_CASE = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , _UpperCamelCase ) != add_prefix_space: SCREAMING_SNAKE_CASE = getattr(_UpperCamelCase , pre_tok_state.pop("type" ) ) SCREAMING_SNAKE_CASE = add_prefix_space SCREAMING_SNAKE_CASE = pre_tok_class(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = add_prefix_space def __snake_case( self : Dict , *_UpperCamelCase : List[Any] , **_UpperCamelCase : int ) -> BatchEncoding: '''simple docstring''' SCREAMING_SNAKE_CASE = kwargs.get("is_split_into_words" , _UpperCamelCase ) assert self.add_prefix_space or not is_split_into_words, ( F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : List[Any] ) -> BatchEncoding: '''simple docstring''' SCREAMING_SNAKE_CASE = kwargs.get("is_split_into_words" , _UpperCamelCase ) assert self.add_prefix_space or not is_split_into_words, ( F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self._tokenizer.model.save(_UpperCamelCase , name=_UpperCamelCase ) return tuple(_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : "Conversation" ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(_UpperCamelCase , add_special_tokens=_UpperCamelCase ) + [self.eos_token_id] ) if len(_UpperCamelCase ) > self.model_max_length: SCREAMING_SNAKE_CASE = input_ids[-self.model_max_length :] return input_ids
647
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
1
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: _lowerCamelCase : Optional[int] = None _lowerCamelCase : str = logging.get_logger(__name__) _lowerCamelCase : Union[str, Any] = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCamelCase : Optional[int] = { '''vocab_file''': { '''facebook/mbart-large-en-ro''': ( '''https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model''' ), '''facebook/mbart-large-cc25''': ( '''https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model''' ), }, '''tokenizer_file''': { '''facebook/mbart-large-en-ro''': '''https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json''', '''facebook/mbart-large-cc25''': '''https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json''', }, } _lowerCamelCase : Union[str, Any] = { '''facebook/mbart-large-en-ro''': 10_24, '''facebook/mbart-large-cc25''': 10_24, } # fmt: off _lowerCamelCase : Any = ['''ar_AR''', '''cs_CZ''', '''de_DE''', '''en_XX''', '''es_XX''', '''et_EE''', '''fi_FI''', '''fr_XX''', '''gu_IN''', '''hi_IN''', '''it_IT''', '''ja_XX''', '''kk_KZ''', '''ko_KR''', '''lt_LT''', '''lv_LV''', '''my_MM''', '''ne_NP''', '''nl_XX''', '''ro_RO''', '''ru_RU''', '''si_LK''', '''tr_TR''', '''vi_VN''', '''zh_CN'''] class lowercase ( a ): lowercase__ : List[str] = VOCAB_FILES_NAMES lowercase__ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Optional[Any] = ["""input_ids""", """attention_mask"""] lowercase__ : Optional[Any] = MBartTokenizer lowercase__ : List[int] = [] lowercase__ : List[int] = [] def __init__( self : int , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Tuple="<s>" , _UpperCamelCase : Optional[Any]="</s>" , _UpperCamelCase : List[Any]="</s>" , _UpperCamelCase : str="<s>" , _UpperCamelCase : Optional[Any]="<unk>" , _UpperCamelCase : Tuple="<pad>" , _UpperCamelCase : Union[str, Any]="<mask>" , _UpperCamelCase : List[Any]=None , _UpperCamelCase : Dict=None , _UpperCamelCase : List[Any]=None , **_UpperCamelCase : List[Any] , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token super().__init__( vocab_file=_UpperCamelCase , tokenizer_file=_UpperCamelCase , bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , cls_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , mask_token=_UpperCamelCase , src_lang=_UpperCamelCase , tgt_lang=_UpperCamelCase , additional_special_tokens=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = vocab_file SCREAMING_SNAKE_CASE = False if not self.vocab_file else True SCREAMING_SNAKE_CASE = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({"additional_special_tokens": _additional_special_tokens} ) SCREAMING_SNAKE_CASE = { lang_code: self.convert_tokens_to_ids(_UpperCamelCase ) for lang_code in FAIRSEQ_LANGUAGE_CODES } SCREAMING_SNAKE_CASE = src_lang if src_lang is not None else "en_XX" SCREAMING_SNAKE_CASE = self.convert_tokens_to_ids(self._src_lang ) SCREAMING_SNAKE_CASE = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def __snake_case( self : str ) -> str: '''simple docstring''' return self._src_lang @src_lang.setter def __snake_case( self : Dict , _UpperCamelCase : str ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __snake_case( self : Dict , _UpperCamelCase : Optional[int] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] , _UpperCamelCase : Optional[str] , **_UpperCamelCase : int ) -> Union[str, Any]: '''simple docstring''' if src_lang is None or tgt_lang is None: raise ValueError("Translation requires a `src_lang` and a `tgt_lang` for this model" ) SCREAMING_SNAKE_CASE = src_lang SCREAMING_SNAKE_CASE = self(_UpperCamelCase , add_special_tokens=_UpperCamelCase , return_tensors=_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.convert_tokens_to_ids(_UpperCamelCase ) SCREAMING_SNAKE_CASE = tgt_lang_id return inputs def __snake_case( self : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : str = "en_XX" , _UpperCamelCase : Optional[List[str]] = None , _UpperCamelCase : str = "ro_RO" , **_UpperCamelCase : List[str] , ) -> BatchEncoding: '''simple docstring''' SCREAMING_SNAKE_CASE = src_lang SCREAMING_SNAKE_CASE = tgt_lang return super().prepare_seqaseq_batch(_UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' return self.set_src_lang_special_tokens(self.src_lang ) def __snake_case( self : Any ) -> int: '''simple docstring''' return self.set_tgt_lang_special_tokens(self.tgt_lang ) def __snake_case( self : int , _UpperCamelCase : List[Any] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = self.convert_tokens_to_ids(_UpperCamelCase ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [self.eos_token_id, self.cur_lang_code] SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(self.prefix_tokens ) SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(self.suffix_tokens ) SCREAMING_SNAKE_CASE = processors.TemplateProcessing( single=prefix_tokens_str + ["$A"] + suffix_tokens_str , pair=prefix_tokens_str + ["$A", "$B"] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def __snake_case( self : Optional[Any] , _UpperCamelCase : str ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = self.convert_tokens_to_ids(_UpperCamelCase ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [self.eos_token_id, self.cur_lang_code] SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(self.prefix_tokens ) SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(self.suffix_tokens ) SCREAMING_SNAKE_CASE = processors.TemplateProcessing( single=prefix_tokens_str + ["$A"] + suffix_tokens_str , pair=prefix_tokens_str + ["$A", "$B"] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def __snake_case( self : Any , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory." ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ): copyfile(self.vocab_file , _UpperCamelCase ) return (out_vocab_file,)
647
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
1
import unittest from transformers import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device if is_torch_available(): import torch from transformers import AutoModelForImageClassification if is_vision_available(): from transformers import AutoImageProcessor @require_torch @require_vision class lowercase ( unittest.TestCase ): @slow def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoImageProcessor.from_pretrained("microsoft/dit-base-finetuned-rvlcdip" ) SCREAMING_SNAKE_CASE = AutoModelForImageClassification.from_pretrained("microsoft/dit-base-finetuned-rvlcdip" ) model.to(_UpperCamelCase ) from datasets import load_dataset SCREAMING_SNAKE_CASE = load_dataset("nielsr/rvlcdip-demo" ) SCREAMING_SNAKE_CASE = dataset["train"][0]["image"].convert("RGB" ) SCREAMING_SNAKE_CASE = image_processor(_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits SCREAMING_SNAKE_CASE = torch.Size((1, 16) ) self.assertEqual(logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor( [-0.4_1_5_8, -0.4_0_9_2, -0.4_3_4_7] , device=_UpperCamelCase , dtype=torch.float , ) self.assertTrue(torch.allclose(logits[0, :3] , _UpperCamelCase , atol=1e-4 ) )
647
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
1
import warnings from typing import Dict import numpy as np from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING def __lowerCamelCase (UpperCAmelCase__ : Dict ): return 1.0 / (1.0 + np.exp(-_outputs )) def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = np.max(_outputs , axis=-1 , keepdims=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.exp(_outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=UpperCAmelCase__ ) class lowercase ( a ): lowercase__ : List[str] = """sigmoid""" lowercase__ : str = """softmax""" lowercase__ : List[Any] = """none""" @add_end_docstrings( a , R""" return_all_scores (`bool`, *optional*, defaults to `False`): Whether to return all prediction scores or just the one of the predicted class. function_to_apply (`str`, *optional*, defaults to `\"default\"`): The function to apply to the model outputs in order to retrieve the scores. Accepts four different values: - `\"default\"`: if the model has a single label, will apply the sigmoid function on the output. If the model has several labels, will apply the softmax function on the output. - `\"sigmoid\"`: Applies the sigmoid function on the output. - `\"softmax\"`: Applies the softmax function on the output. - `\"none\"`: Does not apply any function on the output. """ , ) class lowercase ( a ): lowercase__ : Optional[int] = False lowercase__ : int = ClassificationFunction.NONE def __init__( self : Optional[Any] , **_UpperCamelCase : List[str] ) -> List[str]: '''simple docstring''' super().__init__(**_UpperCamelCase ) self.check_model_type( TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if self.framework == "tf" else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING ) def __snake_case( self : int , _UpperCamelCase : List[str]=None , _UpperCamelCase : Tuple=None , _UpperCamelCase : str="" , **_UpperCamelCase : Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = tokenizer_kwargs SCREAMING_SNAKE_CASE = {} if hasattr(self.model.config , "return_all_scores" ) and return_all_scores is None: SCREAMING_SNAKE_CASE = self.model.config.return_all_scores if isinstance(_UpperCamelCase , _UpperCamelCase ) or top_k is None: SCREAMING_SNAKE_CASE = top_k SCREAMING_SNAKE_CASE = False elif return_all_scores is not None: warnings.warn( "`return_all_scores` is now deprecated, if want a similar functionality use `top_k=None` instead of" " `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`." , _UpperCamelCase , ) if return_all_scores: SCREAMING_SNAKE_CASE = None else: SCREAMING_SNAKE_CASE = 1 if isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = ClassificationFunction[function_to_apply.upper()] if function_to_apply is not None: SCREAMING_SNAKE_CASE = function_to_apply return preprocess_params, {}, postprocess_params def __call__( self : Any , *_UpperCamelCase : List[str] , **_UpperCamelCase : List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = super().__call__(*_UpperCamelCase , **_UpperCamelCase ) # TODO try and retrieve it in a nicer way from _sanitize_parameters. SCREAMING_SNAKE_CASE = "top_k" not in kwargs if isinstance(args[0] , _UpperCamelCase ) and _legacy: # This pipeline is odd, and return a list when single item is run return [result] else: return result def __snake_case( self : Optional[Any] , _UpperCamelCase : str , **_UpperCamelCase : List[Any] ) -> Dict[str, GenericTensor]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.framework if isinstance(_UpperCamelCase , _UpperCamelCase ): return self.tokenizer(**_UpperCamelCase , return_tensors=_UpperCamelCase , **_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ) and len(_UpperCamelCase ) == 1 and isinstance(inputs[0] , _UpperCamelCase ) and len(inputs[0] ) == 2: # It used to be valid to use a list of list of list for text pairs, keeping this path for BC return self.tokenizer( text=inputs[0][0] , text_pair=inputs[0][1] , return_tensors=_UpperCamelCase , **_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): # This is likely an invalid usage of the pipeline attempting to pass text pairs. raise ValueError( "The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a" " dictionary `{\"text\": \"My text\", \"text_pair\": \"My pair\"}` in order to send a text pair." ) return self.tokenizer(_UpperCamelCase , return_tensors=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : List[Any] ) -> Optional[Any]: '''simple docstring''' return self.model(**_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : Tuple , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[int]=1 , _UpperCamelCase : Any=True ) -> Tuple: '''simple docstring''' if function_to_apply is None: if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1: SCREAMING_SNAKE_CASE = ClassificationFunction.SIGMOID elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1: SCREAMING_SNAKE_CASE = ClassificationFunction.SOFTMAX elif hasattr(self.model.config , "function_to_apply" ) and function_to_apply is None: SCREAMING_SNAKE_CASE = self.model.config.function_to_apply else: SCREAMING_SNAKE_CASE = ClassificationFunction.NONE SCREAMING_SNAKE_CASE = model_outputs["logits"][0] SCREAMING_SNAKE_CASE = outputs.numpy() if function_to_apply == ClassificationFunction.SIGMOID: SCREAMING_SNAKE_CASE = sigmoid(_UpperCamelCase ) elif function_to_apply == ClassificationFunction.SOFTMAX: SCREAMING_SNAKE_CASE = softmax(_UpperCamelCase ) elif function_to_apply == ClassificationFunction.NONE: SCREAMING_SNAKE_CASE = outputs else: raise ValueError(F"Unrecognized `function_to_apply` argument: {function_to_apply}" ) if top_k == 1 and _legacy: return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()} SCREAMING_SNAKE_CASE = [ {"label": self.model.config.idalabel[i], "score": score.item()} for i, score in enumerate(_UpperCamelCase ) ] if not _legacy: dict_scores.sort(key=lambda _UpperCamelCase : x["score"] , reverse=_UpperCamelCase ) if top_k is not None: SCREAMING_SNAKE_CASE = dict_scores[:top_k] return dict_scores
647
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(F"The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
1
import itertools import random import unittest import numpy as np from transformers import ASTFeatureExtractor from transformers.testing_utils import require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin _lowerCamelCase : List[str] = random.Random() if is_torch_available(): import torch def __lowerCamelCase (UpperCAmelCase__ : Dict , UpperCAmelCase__ : str=1.0 , UpperCAmelCase__ : Optional[int]=None , UpperCAmelCase__ : Tuple=None ): if rng is None: SCREAMING_SNAKE_CASE = global_rng SCREAMING_SNAKE_CASE = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase ( unittest.TestCase ): def __init__( self : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : int=7 , _UpperCamelCase : Optional[Any]=400 , _UpperCamelCase : List[str]=2_000 , _UpperCamelCase : List[str]=1 , _UpperCamelCase : List[str]=0.0 , _UpperCamelCase : Optional[Any]=16_000 , _UpperCamelCase : str=True , _UpperCamelCase : int=True , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = min_seq_length SCREAMING_SNAKE_CASE = max_seq_length SCREAMING_SNAKE_CASE = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = return_attention_mask SCREAMING_SNAKE_CASE = do_normalize def __snake_case( self : Dict ) -> str: '''simple docstring''' return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def __snake_case( self : int , _UpperCamelCase : Union[str, Any]=False , _UpperCamelCase : Dict=False ) -> Tuple: '''simple docstring''' def _flatten(_UpperCamelCase : int ): return list(itertools.chain(*_UpperCamelCase ) ) if equal_length: SCREAMING_SNAKE_CASE = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( a , unittest.TestCase ): lowercase__ : List[Any] = ASTFeatureExtractor def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = ASTFeatureExtractionTester(self ) def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE = feat_extract(speech_inputs[0] , return_tensors="np" ).input_values SCREAMING_SNAKE_CASE = feat_extract(np_speech_inputs[0] , return_tensors="np" ).input_values self.assertTrue(np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) # Test batched SCREAMING_SNAKE_CASE = feat_extract(_UpperCamelCase , padding=_UpperCamelCase , return_tensors="np" ).input_values SCREAMING_SNAKE_CASE = feat_extract(_UpperCamelCase , padding=_UpperCamelCase , return_tensors="np" ).input_values for enc_seq_a, enc_seq_a in zip(_UpperCamelCase , _UpperCamelCase ): self.assertTrue(np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase ) SCREAMING_SNAKE_CASE = feat_extract(_UpperCamelCase , return_tensors="np" ).input_values SCREAMING_SNAKE_CASE = feat_extract(_UpperCamelCase , return_tensors="np" ).input_values for enc_seq_a, enc_seq_a in zip(_UpperCamelCase , _UpperCamelCase ): self.assertTrue(np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) @require_torch def __snake_case( self : str ) -> Dict: '''simple docstring''' import torch SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE = feature_extractor.pad([{"input_values": inputs}] , return_tensors="np" ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE = feature_extractor.pad([{"input_values": inputs}] , return_tensors="pt" ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def __snake_case( self : Optional[int] , _UpperCamelCase : Union[str, Any] ) -> List[Any]: '''simple docstring''' from datasets import load_dataset SCREAMING_SNAKE_CASE = load_dataset("hf-internal-testing/librispeech_asr_dummy" , "clean" , split="validation" ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE = ds.sort("id" ).select(range(_UpperCamelCase ) )[:num_samples]["audio"] return [x["array"] for x in speech_samples] @require_torch def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = torch.tensor( [-0.9_8_9_4, -1.2_7_7_6, -0.9_0_6_6, -1.2_7_7_6, -0.9_3_4_9, -1.2_6_0_9, -1.0_3_8_6, -1.2_7_7_6, -1.1_5_6_1, -1.2_7_7_6, -1.2_0_5_2, -1.2_7_2_3, -1.2_1_9_0, -1.2_1_3_2, -1.2_7_7_6, -1.1_1_3_3, -1.1_9_5_3, -1.1_3_4_3, -1.1_5_8_4, -1.2_2_0_3, -1.1_7_7_0, -1.2_4_7_4, -1.2_3_8_1, -1.1_9_3_6, -0.9_2_7_0, -0.8_3_1_7, -0.8_0_4_9, -0.7_7_0_6, -0.7_5_6_5, -0.7_8_6_9] ) # fmt: on SCREAMING_SNAKE_CASE = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE = ASTFeatureExtractor() SCREAMING_SNAKE_CASE = feature_extractor(_UpperCamelCase , return_tensors="pt" ).input_values self.assertEquals(input_values.shape , (1, 1_024, 128) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _UpperCamelCase , atol=1e-4 ) )
647
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow 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 DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
1
import argparse import os import re import packaging.version _lowerCamelCase : List[Any] = '''examples/''' _lowerCamelCase : str = { '''examples''': (re.compile(r'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''), '''init''': (re.compile(r'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''), '''setup''': (re.compile(r'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), r'''\1version="VERSION",'''), '''doc''': (re.compile(r'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''), } _lowerCamelCase : Optional[Any] = { '''init''': '''src/transformers/__init__.py''', '''setup''': '''setup.py''', } _lowerCamelCase : int = '''README.md''' def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Union[str, Any] ): with open(UpperCAmelCase__ , "r" , encoding="utf-8" , newline="\n" ) as f: SCREAMING_SNAKE_CASE = f.read() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = REPLACE_PATTERNS[pattern] SCREAMING_SNAKE_CASE = replace.replace("VERSION" , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = re_pattern.sub(UpperCAmelCase__ , UpperCAmelCase__ ) with open(UpperCAmelCase__ , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : List[str] ): for folder, directories, fnames in os.walk(UpperCAmelCase__ ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("research_projects" ) if "legacy" in directories: directories.remove("legacy" ) for fname in fnames: if fname.endswith(".py" ): update_version_in_file(os.path.join(UpperCAmelCase__ , UpperCAmelCase__ ) , UpperCAmelCase__ , pattern="examples" ) def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Any=False ): for pattern, fname in REPLACE_FILES.items(): update_version_in_file(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) if not patch: update_version_in_examples(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = "🤗 Transformers currently provides the following architectures" SCREAMING_SNAKE_CASE = "1. Want to contribute a new model?" with open(UpperCAmelCase__ , "r" , encoding="utf-8" , newline="\n" ) as f: SCREAMING_SNAKE_CASE = f.readlines() # Find the start of the list. SCREAMING_SNAKE_CASE = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 SCREAMING_SNAKE_CASE = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("1." ): SCREAMING_SNAKE_CASE = lines[index].replace( "https://huggingface.co/docs/transformers/main/model_doc" , "https://huggingface.co/docs/transformers/model_doc" , ) index += 1 with open(UpperCAmelCase__ , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(UpperCAmelCase__ ) def __lowerCamelCase (): with open(REPLACE_FILES["init"] , "r" ) as f: SCREAMING_SNAKE_CASE = f.read() SCREAMING_SNAKE_CASE = REPLACE_PATTERNS["init"][0].search(UpperCAmelCase__ ).groups()[0] return packaging.version.parse(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = get_version() if patch and default_version.is_devrelease: raise ValueError("Can't create a patch version from the dev branch, checkout a released version!" ) if default_version.is_devrelease: SCREAMING_SNAKE_CASE = default_version.base_version elif patch: SCREAMING_SNAKE_CASE = F"{default_version.major}.{default_version.minor}.{default_version.micro + 1}" else: SCREAMING_SNAKE_CASE = F"{default_version.major}.{default_version.minor + 1}.0" # Now let's ask nicely if that's the right one. SCREAMING_SNAKE_CASE = input(F"Which version are you releasing? [{default_version}]" ) if len(UpperCAmelCase__ ) == 0: SCREAMING_SNAKE_CASE = default_version print(F"Updating version to {version}." ) global_version_update(UpperCAmelCase__ , patch=UpperCAmelCase__ ) if not patch: print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() def __lowerCamelCase (): SCREAMING_SNAKE_CASE = get_version() SCREAMING_SNAKE_CASE = F"{current_version.major}.{current_version.minor + 1}.0.dev0" SCREAMING_SNAKE_CASE = current_version.base_version # Check with the user we got that right. SCREAMING_SNAKE_CASE = input(F"Which version are we developing now? [{dev_version}]" ) if len(UpperCAmelCase__ ) == 0: SCREAMING_SNAKE_CASE = dev_version print(F"Updating version to {version}." ) global_version_update(UpperCAmelCase__ ) print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() if __name__ == "__main__": _lowerCamelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''') parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''') _lowerCamelCase : str = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('''Nothing to do after a patch :-)''') else: post_release_work()
647
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
1
import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) _lowerCamelCase : str = logging.getLogger() def __lowerCamelCase (): SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("-f" ) SCREAMING_SNAKE_CASE = parser.parse_args() return args.f def __lowerCamelCase (UpperCAmelCase__ : Any ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = os.path.join(UpperCAmelCase__ , "all_results.json" ) if os.path.exists(UpperCAmelCase__ ): with open(UpperCAmelCase__ , "r" ) as f: SCREAMING_SNAKE_CASE = json.load(UpperCAmelCase__ ) else: raise ValueError(F"can't find {path}" ) return results def __lowerCamelCase (): SCREAMING_SNAKE_CASE = torch.cuda.is_available() and torch_device == "cuda" return is_using_cuda and is_apex_available() _lowerCamelCase : Dict = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowercase ( a ): @classmethod def __snake_case( cls : Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = os.path.join(cls.tmpdir , "default_config.yml" ) write_basic_config(save_location=cls.configPath ) SCREAMING_SNAKE_CASE = ["accelerate", "launch", "--config_file", cls.configPath] @classmethod def __snake_case( cls : List[str] ) -> Dict: '''simple docstring''' shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py\n --model_name_or_path distilbert-base-uncased\n --output_dir {tmp_dir}\n --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --learning_rate=1e-4\n --seed=42\n --checkpointing_steps epoch\n --with_tracking\n ".split() if is_cuda_and_apex_available(): testargs.append("--fp16" ) run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.7_5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "glue_no_trainer" ) ) ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --block_size 128\n --per_device_train_batch_size 5\n --per_device_eval_batch_size 5\n --num_train_epochs 2\n --output_dir {tmp_dir}\n --checkpointing_steps epoch\n --with_tracking\n ".split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) self.assertLess(result["perplexity"] , 100 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "clm_no_trainer" ) ) ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py\n --model_name_or_path distilroberta-base\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --output_dir {tmp_dir}\n --num_train_epochs=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) self.assertLess(result["perplexity"] , 42 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "mlm_no_trainer" ) ) ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = 7 if get_gpu_count() > 1 else 2 SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/conll/sample.json\n --validation_file tests/fixtures/tests_samples/conll/sample.json\n --output_dir {tmp_dir}\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.7_5 ) self.assertLess(result["train_loss"] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "ner_no_trainer" ) ) ) @unittest.skip(reason="Fix me @muellerzr" ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py\n --model_name_or_path bert-base-uncased\n --version_2_with_negative\n --train_file tests/fixtures/tests_samples/SQUAD/sample.json\n --validation_file tests/fixtures/tests_samples/SQUAD/sample.json\n --output_dir {tmp_dir}\n --seed=42\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result["eval_f1"] , 28 ) self.assertGreaterEqual(result["eval_exact"] , 28 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "qa_no_trainer" ) ) ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/swag/sample.json\n --validation_file tests/fixtures/tests_samples/swag/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=20\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "swag_no_trainer" ) ) ) @slow @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py\n --model_name_or_path t5-small\n --train_file tests/fixtures/tests_samples/xsum/sample.json\n --validation_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["eval_rouge1"] , 10 ) self.assertGreaterEqual(result["eval_rouge2"] , 2 ) self.assertGreaterEqual(result["eval_rougeL"] , 7 ) self.assertGreaterEqual(result["eval_rougeLsum"] , 7 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "summarization_no_trainer" ) ) ) @slow @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py\n --model_name_or_path sshleifer/student_marian_en_ro_6_1\n --source_lang en\n --target_lang ro\n --train_file tests/fixtures/tests_samples/wmt16/sample.json\n --validation_file tests/fixtures/tests_samples/wmt16/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --num_beams=6\n --learning_rate=3e-3\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --source_lang en_XX\n --target_lang ro_RO\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["eval_bleu"] , 30 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "translation_no_trainer" ) ) ) @slow def __snake_case( self : int ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py\n --dataset_name huggingface/semantic-segmentation-test-sample\n --output_dir {tmp_dir}\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) self.assertGreaterEqual(result["eval_overall_accuracy"] , 0.1_0 ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"\n {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py\n --model_name_or_path google/vit-base-patch16-224-in21k\n --dataset_name hf-internal-testing/cats_vs_dogs_sample\n --learning_rate 1e-4\n --per_device_train_batch_size 2\n --per_device_eval_batch_size 1\n --max_train_steps 2\n --train_val_split 0.1\n --seed 42\n --output_dir {tmp_dir}\n --with_tracking\n --checkpointing_steps 1\n ".split() if is_cuda_and_apex_available(): testargs.append("--fp16" ) run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE = get_results(_UpperCamelCase ) # The base model scores a 25% self.assertGreaterEqual(result["eval_accuracy"] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "step_1" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase , "image_classification_no_trainer" ) ) )
647
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # Validation if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(UpperCAmelCase__ ) != 3 or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(UpperCAmelCase__ ) == 0: return 0 if min(UpperCAmelCase__ ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(UpperCAmelCase__ ) >= 3_6_6: raise ValueError("All days elements should be less than 366" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> int: if index > 3_6_5: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 3_0 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
647
1
import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) _lowerCamelCase : Dict = {'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''} _lowerCamelCase : Any = { '''vocab_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''', }, '''emoji_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''', }, } _lowerCamelCase : Optional[Any] = { '''abeja/gpt-neox-japanese-2.7b''': 20_48, } def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Optional[Any] ): with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = collections.OrderedDict() SCREAMING_SNAKE_CASE = collections.OrderedDict() SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as f: SCREAMING_SNAKE_CASE = f.readlines() SCREAMING_SNAKE_CASE = [[t.rstrip("\n" )] if (t == "," or "," not in t) else t.rstrip("\n" ).split("," ) for t in token] for idx, b in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = b SCREAMING_SNAKE_CASE = idx for wd in b: SCREAMING_SNAKE_CASE = idx return vocab, raw_vocab, ids_to_tokens, emoji class lowercase ( a ): lowercase__ : List[Any] = VOCAB_FILES_NAMES lowercase__ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Optional[int] = ["""input_ids""", """attention_mask"""] def __init__( self : Dict , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Dict="<|endoftext|>" , _UpperCamelCase : Tuple="<|endoftext|>" , _UpperCamelCase : int="<|startoftext|>" , _UpperCamelCase : int="<|endoftext|>" , _UpperCamelCase : Optional[Any]=False , **_UpperCamelCase : Tuple , ) -> Tuple: '''simple docstring''' super().__init__( unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , do_clean_text=_UpperCamelCase , **_UpperCamelCase , ) if not os.path.isfile(_UpperCamelCase ): raise ValueError( F"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained" " model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`" ) if not os.path.isfile(_UpperCamelCase ): raise ValueError( F"Can't find a emoji file at path '{emoji_file}'. To load the emoji information from a Google" " pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`" ) SCREAMING_SNAKE_CASE = do_clean_text SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = load_vocab_and_emoji(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = SubWordJapaneseTokenizer( vocab=self.vocab , ids_to_tokens=self.ids_to_tokens , emoji=self.emoji ) @property def __snake_case( self : int ) -> int: '''simple docstring''' return len(self.raw_vocab ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' return dict(self.raw_vocab , **self.added_tokens_encoder ) def __snake_case( self : Dict , _UpperCamelCase : int ) -> Optional[Any]: '''simple docstring''' return self.subword_tokenizer.tokenize(_UpperCamelCase , clean=self.do_clean_text ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> Any: '''simple docstring''' return self.vocab.get(_UpperCamelCase , self.vocab.get(self.unk_token ) ) def __snake_case( self : Optional[Any] , _UpperCamelCase : Optional[int] ) -> Dict: '''simple docstring''' return self.subword_tokenizer.convert_id_to_token(_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).strip() return out_string def __snake_case( self : Tuple , _UpperCamelCase : "Conversation" ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(_UpperCamelCase , add_special_tokens=_UpperCamelCase ) + [self.eos_token_id] ) if len(_UpperCamelCase ) > self.model_max_length: SCREAMING_SNAKE_CASE = input_ids[-self.model_max_length :] return input_ids def __snake_case( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = 0 if os.path.isdir(_UpperCamelCase ): SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["emoji_file"] ) else: SCREAMING_SNAKE_CASE = ( (filename_prefix + "-" if filename_prefix else "") + save_directory + VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE = ( (filename_prefix + "-" if filename_prefix else "") + save_directory + VOCAB_FILES_NAMES["emoji_file"] ) with open(_UpperCamelCase , "w" , encoding="utf-8" ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( F"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive." " Please check that the vocabulary is not corrupted!" ) SCREAMING_SNAKE_CASE = token_index writer.write(",".join(_UpperCamelCase ) + "\n" ) index += 1 with open(_UpperCamelCase , "w" , encoding="utf-8" ) as writer: json.dump(self.emoji , _UpperCamelCase ) return vocab_file, emoji_file class lowercase ( a ): def __init__( self : Dict , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[int] , _UpperCamelCase : Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = vocab # same as swe SCREAMING_SNAKE_CASE = ids_to_tokens # same as bpe SCREAMING_SNAKE_CASE = emoji SCREAMING_SNAKE_CASE = np.max([len(_UpperCamelCase ) for w in self.vocab.keys()] ) SCREAMING_SNAKE_CASE = re.compile(R"(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)" ) SCREAMING_SNAKE_CASE = re.compile(R"[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*" ) SCREAMING_SNAKE_CASE = re.compile(R"[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}" ) SCREAMING_SNAKE_CASE = re.compile( R"([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*" ) SCREAMING_SNAKE_CASE = re.compile( R"(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*" ) SCREAMING_SNAKE_CASE = re.compile( R"((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*" ) SCREAMING_SNAKE_CASE = "─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿" SCREAMING_SNAKE_CASE = "▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟" SCREAMING_SNAKE_CASE = str.maketrans({k: "<BLOCK>" for k in keisen + blocks} ) def __len__( self : Tuple ) -> List[Any]: '''simple docstring''' return len(self.ids_to_tokens ) def __snake_case( self : Optional[int] , _UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.content_repattera.sub("<URL>" , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.content_repattera.sub("<EMAIL>" , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.content_repattera.sub("<TEL>" , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.content_repattera.sub("<DATE>" , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.content_repattera.sub("<DATE>" , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.content_repattera.sub("<PRICE>" , _UpperCamelCase ) SCREAMING_SNAKE_CASE = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: SCREAMING_SNAKE_CASE = content.replace("<BLOCK><BLOCK>" , "<BLOCK>" ) return content def __snake_case( self : List[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Union[str, Any]=False ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = text.replace(" " , "<SP>" ) SCREAMING_SNAKE_CASE = text.replace(" " , "<SP>" ) SCREAMING_SNAKE_CASE = text.replace("\r\n" , "<BR>" ) SCREAMING_SNAKE_CASE = text.replace("\n" , "<BR>" ) SCREAMING_SNAKE_CASE = text.replace("\r" , "<BR>" ) SCREAMING_SNAKE_CASE = text.replace("\t" , "<TAB>" ) SCREAMING_SNAKE_CASE = text.replace("—" , "ー" ) SCREAMING_SNAKE_CASE = text.replace("−" , "ー" ) for k, v in self.emoji["emoji"].items(): if k in text: SCREAMING_SNAKE_CASE = text.replace(_UpperCamelCase , _UpperCamelCase ) if clean: SCREAMING_SNAKE_CASE = self.clean_text(_UpperCamelCase ) def check_simbol(_UpperCamelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE = x.encode() if len(_UpperCamelCase ) == 1 and len(_UpperCamelCase ) == 2: SCREAMING_SNAKE_CASE = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0xC_2_A_1 and c <= 0xC_2_B_F) or (c >= 0xC_7_8_0 and c <= 0xC_7_8_3) or (c >= 0xC_A_B_9 and c <= 0xC_B_B_F) or (c >= 0xC_C_8_0 and c <= 0xC_D_A_2) ): return True return False def checkuae(_UpperCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = x.encode() if len(_UpperCamelCase ) == 1 and len(_UpperCamelCase ) == 3: SCREAMING_SNAKE_CASE = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0xE_2_8_0_8_0 and c <= 0xE_2_B_0_7_F: return True return False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = [] while pos < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = min(len(_UpperCamelCase ) , pos + self.maxlen + 1 ) if text[pos] == "<" else pos + 3 SCREAMING_SNAKE_CASE = [] # (token_id, token, pos) for e in range(_UpperCamelCase , _UpperCamelCase , -1 ): SCREAMING_SNAKE_CASE = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(_UpperCamelCase ) > 2: SCREAMING_SNAKE_CASE = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(_UpperCamelCase ) > 0: # the smallest token_id is adopted SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = sorted(_UpperCamelCase , key=lambda _UpperCamelCase : x[0] )[0] result.append(_UpperCamelCase ) SCREAMING_SNAKE_CASE = e else: SCREAMING_SNAKE_CASE = pos + 1 SCREAMING_SNAKE_CASE = text[pos:end] if check_simbol(_UpperCamelCase ): result.append("<KIGOU>" ) elif checkuae(_UpperCamelCase ): result.append("<U2000U2BFF>" ) else: for i in wd.encode("utf-8" ): result.append("<|byte%d|>" % i ) SCREAMING_SNAKE_CASE = end return result def __snake_case( self : Optional[int] , _UpperCamelCase : str , _UpperCamelCase : Any="\n" ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(_UpperCamelCase ) > 0: words.append(bytearray(_UpperCamelCase ).decode("utf-8" , errors="replace" ) ) SCREAMING_SNAKE_CASE = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji["emoji_inv"][word] ) elif word == "<SP>": words.append(" " ) elif word == "<BR>": words.append(_UpperCamelCase ) elif word == "<TAB>": words.append("\t" ) elif word == "<BLOCK>": words.append("▀" ) elif word == "<KIGOU>": words.append("ǀ" ) elif word == "<U2000U2BFF>": words.append("‖" ) else: words.append(_UpperCamelCase ) if len(_UpperCamelCase ) > 0: words.append(bytearray(_UpperCamelCase ).decode("utf-8" , errors="replace" ) ) SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ) return text
647
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : int ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
1
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging _lowerCamelCase : str = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class lowercase ( a ): def __init__( self : int , _UpperCamelCase : int = 101 ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = length def __len__( self : List[str] ) -> Dict: '''simple docstring''' return self.length def __getitem__( self : Dict , _UpperCamelCase : Union[str, Any] ) -> int: '''simple docstring''' return i class lowercase : def __call__( self : List[str] , _UpperCamelCase : Optional[Any] ) -> int: '''simple docstring''' return {"input_ids": torch.tensor(_UpperCamelCase ), "labels": torch.tensor(_UpperCamelCase )} class lowercase ( nn.Module ): def __init__( self : Dict ) -> List[Any]: '''simple docstring''' super().__init__() # Add some (unused) params otherwise DDP will complain. SCREAMING_SNAKE_CASE = nn.Linear(120 , 80 ) def __snake_case( self : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[Any]=None ) -> Tuple: '''simple docstring''' if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class lowercase ( a ): @require_torch_neuroncore def __snake_case( self : Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = F"--nproc_per_node=2\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n ".split() SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"--output_dir {output_dir}".split() SCREAMING_SNAKE_CASE = ["torchrun"] + distributed_args + args execute_subprocess_async(_UpperCamelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class lowercase ( a ): @require_torch_multi_gpu def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = F"--nproc_per_node={torch.cuda.device_count()}\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n ".split() SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = F"--output_dir {output_dir}".split() SCREAMING_SNAKE_CASE = ["torchrun"] + distributed_args + args execute_subprocess_async(_UpperCamelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py _lowerCamelCase : Optional[Any] = HfArgumentParser((TrainingArguments,)) _lowerCamelCase : Optional[int] = parser.parse_args_into_dataclasses()[0] logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, """ f"""distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}""" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [1_01, 40, 7]: _lowerCamelCase : List[str] = DummyDataset(dataset_length) def __lowerCamelCase (UpperCAmelCase__ : EvalPrediction ): SCREAMING_SNAKE_CASE = list(range(len(UpperCAmelCase__ ) ) ) SCREAMING_SNAKE_CASE = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( "Predictions and/or labels do not match expected results:\n - predictions: " F"{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}" ) return {"success": success} _lowerCamelCase : str = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) _lowerCamelCase : List[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) _lowerCamelCase : Dict = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) _lowerCamelCase : Union[str, Any] = 2 _lowerCamelCase : Any = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) _lowerCamelCase : Dict = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) _lowerCamelCase : int = None
647
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(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"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(UpperCAmelCase__ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
1
import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) _lowerCamelCase : Any = logging.getLogger(__name__) class lowercase ( a ): def __snake_case( self : Any , _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Any=None ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.layer[current_layer](_UpperCamelCase , _UpperCamelCase , head_mask[current_layer] ) SCREAMING_SNAKE_CASE = layer_outputs[0] return hidden_states @add_start_docstrings( """The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top.""" , a , ) class lowercase ( a ): def __init__( self : Dict , _UpperCamelCase : str ) -> int: '''simple docstring''' super().__init__(_UpperCamelCase ) SCREAMING_SNAKE_CASE = BertEncoderWithPabee(_UpperCamelCase ) self.init_weights() SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 def __snake_case( self : int , _UpperCamelCase : List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = threshold def __snake_case( self : Tuple , _UpperCamelCase : Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = patience def __snake_case( self : Tuple ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 def __snake_case( self : Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.inference_layers_num / self.inference_instances_num SCREAMING_SNAKE_CASE = ( F"*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =" F" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***" ) print(_UpperCamelCase ) @add_start_docstrings_to_model_forward(_UpperCamelCase ) def __snake_case( self : Dict , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=None , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Tuple=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Optional[int]=None , _UpperCamelCase : str=None , _UpperCamelCase : Tuple=None , _UpperCamelCase : Tuple=None , _UpperCamelCase : int=False , ) -> Union[str, Any]: '''simple docstring''' if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time" ) elif input_ids is not None: SCREAMING_SNAKE_CASE = input_ids.size() elif inputs_embeds is not None: SCREAMING_SNAKE_CASE = inputs_embeds.size()[:-1] else: raise ValueError("You have to specify either input_ids or inputs_embeds" ) SCREAMING_SNAKE_CASE = input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: SCREAMING_SNAKE_CASE = torch.ones(_UpperCamelCase , device=_UpperCamelCase ) if token_type_ids is None: SCREAMING_SNAKE_CASE = torch.zeros(_UpperCamelCase , dtype=torch.long , device=_UpperCamelCase ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. SCREAMING_SNAKE_CASE = self.get_extended_attention_mask(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = encoder_hidden_states.size() SCREAMING_SNAKE_CASE = (encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: SCREAMING_SNAKE_CASE = torch.ones(_UpperCamelCase , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.invert_attention_mask(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] SCREAMING_SNAKE_CASE = self.get_head_mask(_UpperCamelCase , self.config.num_hidden_layers ) SCREAMING_SNAKE_CASE = self.embeddings( input_ids=_UpperCamelCase , position_ids=_UpperCamelCase , token_type_ids=_UpperCamelCase , inputs_embeds=_UpperCamelCase ) SCREAMING_SNAKE_CASE = embedding_output if self.training: SCREAMING_SNAKE_CASE = [] for i in range(self.config.num_hidden_layers ): SCREAMING_SNAKE_CASE = self.encoder.adaptive_forward( _UpperCamelCase , current_layer=_UpperCamelCase , attention_mask=_UpperCamelCase , head_mask=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.pooler(_UpperCamelCase ) SCREAMING_SNAKE_CASE = output_layers[i](output_dropout(_UpperCamelCase ) ) res.append(_UpperCamelCase ) elif self.patience == 0: # Use all layers for inference SCREAMING_SNAKE_CASE = self.encoder( _UpperCamelCase , attention_mask=_UpperCamelCase , head_mask=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , encoder_attention_mask=_UpperCamelCase , ) SCREAMING_SNAKE_CASE = self.pooler(encoder_outputs[0] ) SCREAMING_SNAKE_CASE = [output_layers[self.config.num_hidden_layers - 1](_UpperCamelCase )] else: SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = 0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 SCREAMING_SNAKE_CASE = self.encoder.adaptive_forward( _UpperCamelCase , current_layer=_UpperCamelCase , attention_mask=_UpperCamelCase , head_mask=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.pooler(_UpperCamelCase ) SCREAMING_SNAKE_CASE = output_layers[i](_UpperCamelCase ) if regression: SCREAMING_SNAKE_CASE = logits.detach() if patient_result is not None: SCREAMING_SNAKE_CASE = patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: SCREAMING_SNAKE_CASE = 0 else: SCREAMING_SNAKE_CASE = logits.detach().argmax(dim=1 ) if patient_result is not None: SCREAMING_SNAKE_CASE = patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(_UpperCamelCase ) ): patient_counter += 1 else: SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = logits if patient_counter == self.patience: break SCREAMING_SNAKE_CASE = [patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( """Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. """ , a , ) class lowercase ( a ): def __init__( self : Any , _UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' super().__init__(_UpperCamelCase ) SCREAMING_SNAKE_CASE = config.num_labels SCREAMING_SNAKE_CASE = BertModelWithPabee(_UpperCamelCase ) SCREAMING_SNAKE_CASE = nn.Dropout(config.hidden_dropout_prob ) SCREAMING_SNAKE_CASE = nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(_UpperCamelCase ) def __snake_case( self : str , _UpperCamelCase : Optional[int]=None , _UpperCamelCase : Dict=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Dict=None , _UpperCamelCase : int=None , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.bert( input_ids=_UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase , position_ids=_UpperCamelCase , head_mask=_UpperCamelCase , inputs_embeds=_UpperCamelCase , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) SCREAMING_SNAKE_CASE = (logits[-1],) if labels is not None: SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = 0 for ix, logits_item in enumerate(_UpperCamelCase ): if self.num_labels == 1: # We are doing regression SCREAMING_SNAKE_CASE = MSELoss() SCREAMING_SNAKE_CASE = loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: SCREAMING_SNAKE_CASE = CrossEntropyLoss() SCREAMING_SNAKE_CASE = loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: SCREAMING_SNAKE_CASE = loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 SCREAMING_SNAKE_CASE = (total_loss / total_weights,) + outputs return outputs
647
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __snake_case( self : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
1
_lowerCamelCase : Optional[Any] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' _lowerCamelCase : List[Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] _lowerCamelCase : Optional[Any] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
647
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
1
import argparse import torch from transformers import ( WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForAudioFrameClassification, WavaVecaForSequenceClassification, WavaVecaForXVector, logging, ) logging.set_verbosity_info() _lowerCamelCase : List[str] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : Dict , UpperCAmelCase__ : int , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = WavaVecaForSequenceClassification.from_pretrained(UpperCAmelCase__ , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = downstream_dict["projector.weight"] SCREAMING_SNAKE_CASE = downstream_dict["projector.bias"] SCREAMING_SNAKE_CASE = downstream_dict["model.post_net.linear.weight"] SCREAMING_SNAKE_CASE = downstream_dict["model.post_net.linear.bias"] return model def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = WavaVecaForAudioFrameClassification.from_pretrained(UpperCAmelCase__ , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = downstream_dict["model.linear.weight"] SCREAMING_SNAKE_CASE = downstream_dict["model.linear.bias"] return model def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : str , UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = WavaVecaForXVector.from_pretrained(UpperCAmelCase__ , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = downstream_dict["connector.weight"] SCREAMING_SNAKE_CASE = downstream_dict["connector.bias"] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): SCREAMING_SNAKE_CASE = downstream_dict[ F"model.framelevel_feature_extractor.module.{i}.kernel.weight" ] SCREAMING_SNAKE_CASE = downstream_dict[F"model.framelevel_feature_extractor.module.{i}.kernel.bias"] SCREAMING_SNAKE_CASE = downstream_dict["model.utterancelevel_feature_extractor.linear1.weight"] SCREAMING_SNAKE_CASE = downstream_dict["model.utterancelevel_feature_extractor.linear1.bias"] SCREAMING_SNAKE_CASE = downstream_dict["model.utterancelevel_feature_extractor.linear2.weight"] SCREAMING_SNAKE_CASE = downstream_dict["model.utterancelevel_feature_extractor.linear2.bias"] SCREAMING_SNAKE_CASE = downstream_dict["objective.W"] return model @torch.no_grad() def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , map_location="cpu" ) SCREAMING_SNAKE_CASE = checkpoint["Downstream"] SCREAMING_SNAKE_CASE = WavaVecaConfig.from_pretrained(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = WavaVecaFeatureExtractor.from_pretrained( UpperCAmelCase__ , return_attention_mask=UpperCAmelCase__ , do_normalize=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = hf_config.architectures[0] if arch.endswith("ForSequenceClassification" ): SCREAMING_SNAKE_CASE = convert_classification(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) elif arch.endswith("ForAudioFrameClassification" ): SCREAMING_SNAKE_CASE = convert_diarization(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) elif arch.endswith("ForXVector" ): SCREAMING_SNAKE_CASE = convert_xvector(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) else: raise NotImplementedError(F"S3PRL weights conversion is not supported for {arch}" ) if hf_config.use_weighted_layer_sum: SCREAMING_SNAKE_CASE = checkpoint["Featurizer"]["weights"] hf_feature_extractor.save_pretrained(UpperCAmelCase__ ) hf_model.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : int = argparse.ArgumentParser() parser.add_argument( '''--base_model_name''', default=None, type=str, help='''Name of the huggingface pretrained base model.''' ) parser.add_argument('''--config_path''', default=None, type=str, help='''Path to the huggingface classifier config.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to the s3prl checkpoint.''') parser.add_argument('''--model_dump_path''', default=None, type=str, help='''Path to the final converted model.''') _lowerCamelCase : Optional[int] = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
647
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"Only mono-channel audio is supported for input to {self}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
1
_lowerCamelCase : Optional[Any] = 8.3_144_598 def __lowerCamelCase (UpperCAmelCase__ : float , UpperCAmelCase__ : float ): if temperature < 0: raise Exception("Temperature cannot be less than 0 K" ) if molar_mass <= 0: raise Exception("Molar mass cannot be less than or equal to 0 kg/mol" ) else: return (3 * UNIVERSAL_GAS_CONSTANT * temperature / molar_mass) ** 0.5 if __name__ == "__main__": import doctest # run doctest doctest.testmod() # example _lowerCamelCase : str = 3_00 _lowerCamelCase : Tuple = 28 _lowerCamelCase : Tuple = rms_speed_of_molecule(temperature, molar_mass) print(f"""Vrms of Nitrogen gas at 300 K is {vrms} m/s""")
647
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
1
import json import os import unittest from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES, BioGptTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class lowercase ( a , unittest.TestCase ): lowercase__ : List[Any] = BioGptTokenizer lowercase__ : Any = False def __snake_case( self : Optional[Any] ) -> int: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "w</w>", "r</w>", "t</w>", "lo", "low", "er</w>", "low</w>", "lowest</w>", "newer</w>", "wider</w>", "<unk>", ] SCREAMING_SNAKE_CASE = dict(zip(_UpperCamelCase , range(len(_UpperCamelCase ) ) ) ) SCREAMING_SNAKE_CASE = ["l o 123", "lo w 1456", "e r</w> 1789", ""] SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" ) as fp: fp.write(json.dumps(_UpperCamelCase ) ) with open(self.merges_file , "w" ) as fp: fp.write("\n".join(_UpperCamelCase ) ) def __snake_case( self : List[str] , _UpperCamelCase : Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = "lower newer" return input_text, output_text def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = BioGptTokenizer(self.vocab_file , self.merges_file ) SCREAMING_SNAKE_CASE = "lower" SCREAMING_SNAKE_CASE = ["low", "er</w>"] SCREAMING_SNAKE_CASE = tokenizer.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = tokens + ["<unk>"] SCREAMING_SNAKE_CASE = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , _UpperCamelCase ) @slow def __snake_case( self : Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = BioGptTokenizer.from_pretrained("microsoft/biogpt" ) SCREAMING_SNAKE_CASE = tokenizer.encode("sequence builders" , add_special_tokens=_UpperCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.encode("multi-sequence build" , add_special_tokens=_UpperCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(_UpperCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(_UpperCamelCase , _UpperCamelCase ) self.assertTrue(encoded_sentence == [2] + text ) self.assertTrue(encoded_pair == [2] + text + [2] + text_a )
647
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
1
from __future__ import annotations import requests def __lowerCamelCase (UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = F"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty" return requests.get(UpperCAmelCase__ ).json() def __lowerCamelCase (UpperCAmelCase__ : int = 1_0 ): SCREAMING_SNAKE_CASE = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" SCREAMING_SNAKE_CASE = requests.get(UpperCAmelCase__ ).json()[:max_stories] return [get_hackernews_story(UpperCAmelCase__ ) for story_id in story_ids] def __lowerCamelCase (UpperCAmelCase__ : int = 1_0 ): SCREAMING_SNAKE_CASE = hackernews_top_stories(UpperCAmelCase__ ) return "\n".join("* [{title}]({url})".format(**UpperCAmelCase__ ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
647
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
1
_lowerCamelCase : int = { '''a''': '''AAAAA''', '''b''': '''AAAAB''', '''c''': '''AAABA''', '''d''': '''AAABB''', '''e''': '''AABAA''', '''f''': '''AABAB''', '''g''': '''AABBA''', '''h''': '''AABBB''', '''i''': '''ABAAA''', '''j''': '''BBBAA''', '''k''': '''ABAAB''', '''l''': '''ABABA''', '''m''': '''ABABB''', '''n''': '''ABBAA''', '''o''': '''ABBAB''', '''p''': '''ABBBA''', '''q''': '''ABBBB''', '''r''': '''BAAAA''', '''s''': '''BAAAB''', '''t''': '''BAABA''', '''u''': '''BAABB''', '''v''': '''BBBAB''', '''w''': '''BABAA''', '''x''': '''BABAB''', '''y''': '''BABBA''', '''z''': '''BABBB''', ''' ''': ''' ''', } _lowerCamelCase : List[Any] = {value: key for key, value in encode_dict.items()} def __lowerCamelCase (UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = "" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("encode() accepts only letters of the alphabet and spaces" ) return encoded def __lowerCamelCase (UpperCAmelCase__ : str ): if set(UpperCAmelCase__ ) - {"A", "B", " "} != set(): raise Exception("decode() accepts only 'A', 'B' and spaces" ) SCREAMING_SNAKE_CASE = "" for word in coded.split(): while len(UpperCAmelCase__ ) != 0: decoded += decode_dict[word[:5]] SCREAMING_SNAKE_CASE = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
647
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
1
def __lowerCamelCase (UpperCAmelCase__ : int = 1 , UpperCAmelCase__ : int = 1_0_0_0 ): SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = 0 for divide_by_number in range(UpperCAmelCase__ , digit + 1 ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = len(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = divide_by_number else: has_been_divided.append(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = now_divide * 1_0 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
647
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) _lowerCamelCase : Dict = { '''sail/poolformer_s12''': '''https://huggingface.co/sail/poolformer_s12/resolve/main/config.json''', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class lowercase ( a ): lowercase__ : Tuple = """poolformer""" def __init__( self : Optional[Any] , _UpperCamelCase : Any=3 , _UpperCamelCase : Any=16 , _UpperCamelCase : str=16 , _UpperCamelCase : Dict=3 , _UpperCamelCase : Optional[Any]=4.0 , _UpperCamelCase : Tuple=[2, 2, 6, 2] , _UpperCamelCase : List[Any]=[64, 128, 320, 512] , _UpperCamelCase : List[str]=[7, 3, 3, 3] , _UpperCamelCase : str=[4, 2, 2, 2] , _UpperCamelCase : int=[2, 1, 1, 1] , _UpperCamelCase : Any=4 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : str="gelu" , _UpperCamelCase : str=True , _UpperCamelCase : Dict=1e-5 , _UpperCamelCase : Optional[int]=0.0_2 , **_UpperCamelCase : str , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = stride SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = pool_size SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = mlp_ratio SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = patch_sizes SCREAMING_SNAKE_CASE = strides SCREAMING_SNAKE_CASE = num_encoder_blocks SCREAMING_SNAKE_CASE = drop_path_rate SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = use_layer_scale SCREAMING_SNAKE_CASE = layer_scale_init_value SCREAMING_SNAKE_CASE = initializer_range super().__init__(**_UpperCamelCase ) class lowercase ( a ): lowercase__ : List[Any] = version.parse("""1.11""" ) @property def __snake_case( self : List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def __snake_case( self : str ) -> float: '''simple docstring''' return 2e-3
647
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
1
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
1
from .integrations import ( is_optuna_available, is_ray_available, is_sigopt_available, is_wandb_available, run_hp_search_optuna, run_hp_search_ray, run_hp_search_sigopt, run_hp_search_wandb, ) from .trainer_utils import ( HPSearchBackend, default_hp_space_optuna, default_hp_space_ray, default_hp_space_sigopt, default_hp_space_wandb, ) from .utils import logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase : lowercase__ : str lowercase__ : str = None @staticmethod def __snake_case( ) -> Union[str, Any]: '''simple docstring''' raise NotImplementedError def __snake_case( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : int , _UpperCamelCase : str , **_UpperCamelCase : Optional[int] ) -> List[str]: '''simple docstring''' raise NotImplementedError def __snake_case( self : str , _UpperCamelCase : List[Any] ) -> Any: '''simple docstring''' raise NotImplementedError def __snake_case( self : str ) -> Union[str, Any]: '''simple docstring''' if not self.is_available(): raise RuntimeError( F"You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}." ) @classmethod def __snake_case( cls : str ) -> Union[str, Any]: '''simple docstring''' return F"`pip install {cls.pip_package or cls.name}`" class lowercase ( a ): lowercase__ : Optional[Any] = """optuna""" @staticmethod def __snake_case( ) -> Optional[int]: '''simple docstring''' return is_optuna_available() def __snake_case( self : Union[str, Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : int , _UpperCamelCase : str , **_UpperCamelCase : List[Any] ) -> Union[str, Any]: '''simple docstring''' return run_hp_search_optuna(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : List[str] , _UpperCamelCase : Optional[int] ) -> Any: '''simple docstring''' return default_hp_space_optuna(_UpperCamelCase ) class lowercase ( a ): lowercase__ : Union[str, Any] = """ray""" lowercase__ : int = """'ray[tune]'""" @staticmethod def __snake_case( ) -> Union[str, Any]: '''simple docstring''' return is_ray_available() def __snake_case( self : List[str] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : str , **_UpperCamelCase : str ) -> Any: '''simple docstring''' return run_hp_search_ray(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Any , _UpperCamelCase : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' return default_hp_space_ray(_UpperCamelCase ) class lowercase ( a ): lowercase__ : Tuple = """sigopt""" @staticmethod def __snake_case( ) -> Optional[int]: '''simple docstring''' return is_sigopt_available() def __snake_case( self : Any , _UpperCamelCase : Any , _UpperCamelCase : int , _UpperCamelCase : str , **_UpperCamelCase : List[str] ) -> Tuple: '''simple docstring''' return run_hp_search_sigopt(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : Optional[Any] ) -> int: '''simple docstring''' return default_hp_space_sigopt(_UpperCamelCase ) class lowercase ( a ): lowercase__ : Tuple = """wandb""" @staticmethod def __snake_case( ) -> Union[str, Any]: '''simple docstring''' return is_wandb_available() def __snake_case( self : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : int , _UpperCamelCase : str , **_UpperCamelCase : Any ) -> Dict: '''simple docstring''' return run_hp_search_wandb(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : Dict ) -> str: '''simple docstring''' return default_hp_space_wandb(_UpperCamelCase ) _lowerCamelCase : List[Any] = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def __lowerCamelCase (): SCREAMING_SNAKE_CASE = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(UpperCAmelCase__ ) > 0: SCREAMING_SNAKE_CASE = available_backends[0].name if len(UpperCAmelCase__ ) > 1: logger.info( F"{len(UpperCAmelCase__ )} hyperparameter search backends available. Using {name} as the default." ) return name raise RuntimeError( "No hyperparameter search backend available.\n" + "\n".join( F" - To install {backend.name} run {backend.pip_install()}" for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() ) )
647
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
1
from ..utils import DummyObject, requires_backends class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : Tuple , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : List[str] ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : str , **_UpperCamelCase : List[Any] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Dict , *_UpperCamelCase : List[Any] , **_UpperCamelCase : List[Any] ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[str] = ["""torch"""] def __init__( self : int , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> List[str]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[int] , *_UpperCamelCase : str , **_UpperCamelCase : Dict ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : Any , **_UpperCamelCase : Optional[Any] ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : List[Any] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : int ) -> int: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : List[Any] ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : str , **_UpperCamelCase : List[Any] ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Tuple = ["""torch"""] def __init__( self : List[str] , *_UpperCamelCase : int , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[int] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Any ) -> List[str]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Optional[int] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Tuple ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : int , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Any ) -> List[str]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : str , **_UpperCamelCase : Union[str, Any] ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Optional[int] ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : int = ["""torch"""] def __init__( self : str , *_UpperCamelCase : List[str] , **_UpperCamelCase : Any ) -> Any: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Dict , *_UpperCamelCase : List[Any] , **_UpperCamelCase : str ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Optional[int] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : int ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : Dict , *_UpperCamelCase : List[str] , **_UpperCamelCase : Any ) -> str: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[int] , *_UpperCamelCase : str , **_UpperCamelCase : List[str] ) -> Dict: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : List[str] , **_UpperCamelCase : Optional[int] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Tuple = ["""torch"""] def __init__( self : Any , *_UpperCamelCase : str , **_UpperCamelCase : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Optional[Any] ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : Optional[Any] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Dict , *_UpperCamelCase : List[Any] , **_UpperCamelCase : str ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Optional[int] , *_UpperCamelCase : Dict , **_UpperCamelCase : Optional[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[Any] = ["""torch"""] def __init__( self : Tuple , *_UpperCamelCase : Any , **_UpperCamelCase : int ) -> Any: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : Dict , **_UpperCamelCase : Optional[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : int = ["""torch"""] def __init__( self : Any , *_UpperCamelCase : str , **_UpperCamelCase : Dict ) -> Any: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : str , **_UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : Any , **_UpperCamelCase : Dict ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) def __lowerCamelCase (*UpperCAmelCase__ : Optional[int] , **UpperCAmelCase__ : List[str] ): requires_backends(UpperCAmelCase__ , ["torch"] ) def __lowerCamelCase (*UpperCAmelCase__ : Optional[Any] , **UpperCAmelCase__ : Any ): requires_backends(UpperCAmelCase__ , ["torch"] ) def __lowerCamelCase (*UpperCAmelCase__ : str , **UpperCAmelCase__ : Union[str, Any] ): requires_backends(UpperCAmelCase__ , ["torch"] ) def __lowerCamelCase (*UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : str ): requires_backends(UpperCAmelCase__ , ["torch"] ) def __lowerCamelCase (*UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : str ): requires_backends(UpperCAmelCase__ , ["torch"] ) def __lowerCamelCase (*UpperCAmelCase__ : Optional[int] , **UpperCAmelCase__ : List[str] ): requires_backends(UpperCAmelCase__ , ["torch"] ) def __lowerCamelCase (*UpperCAmelCase__ : List[str] , **UpperCAmelCase__ : Optional[int] ): requires_backends(UpperCAmelCase__ , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[int] = ["""torch"""] def __init__( self : str , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : List[Any] ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : List[str] , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : List[str] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : List[Any] ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[Any] = ["""torch"""] def __init__( self : str , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Optional[Any] ) -> str: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Tuple ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : List[Any] , *_UpperCamelCase : Dict , **_UpperCamelCase : Any ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : int = ["""torch"""] def __init__( self : List[str] , *_UpperCamelCase : List[str] , **_UpperCamelCase : Dict ) -> int: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[int] , *_UpperCamelCase : Any , **_UpperCamelCase : Union[str, Any] ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : int , **_UpperCamelCase : List[Any] ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[str] = ["""torch"""] def __init__( self : int , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : int ) -> List[str]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : List[Any] , *_UpperCamelCase : Any , **_UpperCamelCase : Optional[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : List[str] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Tuple = ["""torch"""] def __init__( self : List[str] , *_UpperCamelCase : Tuple , **_UpperCamelCase : Tuple ) -> List[str]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : str , **_UpperCamelCase : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[Any] = ["""torch"""] def __init__( self : List[Any] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : str ) -> Any: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : Any , **_UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : str , **_UpperCamelCase : Dict ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[int] = ["""torch"""] def __init__( self : Tuple , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Optional[int] ) -> Tuple: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : List[str] , **_UpperCamelCase : Tuple ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : int ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[str] = ["""torch"""] def __init__( self : List[str] , *_UpperCamelCase : Dict , **_UpperCamelCase : Tuple ) -> Any: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : str , **_UpperCamelCase : int ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[int] = ["""torch"""] def __init__( self : Any , *_UpperCamelCase : Dict , **_UpperCamelCase : Optional[int] ) -> Dict: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : List[Any] , *_UpperCamelCase : Tuple , **_UpperCamelCase : Optional[Any] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Any ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : int , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : int , **_UpperCamelCase : Dict ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : int , **_UpperCamelCase : Any ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[Any] = ["""torch"""] def __init__( self : int , *_UpperCamelCase : int , **_UpperCamelCase : str ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : List[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : int , **_UpperCamelCase : Any ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Union[str, Any] = ["""torch"""] def __init__( self : str , *_UpperCamelCase : Dict , **_UpperCamelCase : Optional[Any] ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : Dict , **_UpperCamelCase : int ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : List[Any] , **_UpperCamelCase : str ) -> Dict: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Dict = ["""torch"""] def __init__( self : List[Any] , *_UpperCamelCase : str , **_UpperCamelCase : str ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : int , **_UpperCamelCase : Any ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Any , **_UpperCamelCase : Tuple ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Union[str, Any] = ["""torch"""] def __init__( self : str , *_UpperCamelCase : Dict , **_UpperCamelCase : List[str] ) -> str: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : str , **_UpperCamelCase : Dict ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Optional[int] ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Union[str, Any] = ["""torch"""] def __init__( self : Dict , *_UpperCamelCase : Tuple , **_UpperCamelCase : Union[str, Any] ) -> int: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Dict , *_UpperCamelCase : List[Any] , **_UpperCamelCase : int ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Union[str, Any] ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[Any] = ["""torch"""] def __init__( self : Optional[int] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : List[str] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : List[str] , **_UpperCamelCase : Dict ) -> Dict: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : Tuple , **_UpperCamelCase : Any ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[str] = ["""torch"""] def __init__( self : str , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : List[str] , **_UpperCamelCase : Any ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : Any , *_UpperCamelCase : List[Any] , **_UpperCamelCase : str ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Any ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : List[str] , **_UpperCamelCase : int ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[str] = ["""torch"""] def __init__( self : Tuple , *_UpperCamelCase : Tuple , **_UpperCamelCase : Optional[Any] ) -> Tuple: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Dict , *_UpperCamelCase : List[Any] , **_UpperCamelCase : int ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[int] = ["""torch"""] def __init__( self : str , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Any ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Dict ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : List[str] ) -> List[str]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[Any] = ["""torch"""] def __init__( self : Dict , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Optional[int] ) -> Dict: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : List[str] , **_UpperCamelCase : Tuple ) -> List[str]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Any , **_UpperCamelCase : List[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Union[str, Any] = ["""torch"""] def __init__( self : Union[str, Any] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Optional[int] ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : List[str] , **_UpperCamelCase : Optional[Any] ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Union[str, Any] = ["""torch"""] def __init__( self : str , *_UpperCamelCase : Any , **_UpperCamelCase : Optional[Any] ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : Dict , **_UpperCamelCase : str ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Dict , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Optional[Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[int] = ["""torch"""] def __init__( self : Any , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : int ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : str , **_UpperCamelCase : Dict ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : Any , **_UpperCamelCase : Union[str, Any] ) -> List[str]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Tuple = ["""torch"""] def __init__( self : Any , *_UpperCamelCase : int , **_UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : int , **_UpperCamelCase : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : Any , *_UpperCamelCase : Tuple , **_UpperCamelCase : Tuple ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Union[str, Any] ) -> List[str]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : List[str] , **_UpperCamelCase : str ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Dict = ["""torch"""] def __init__( self : Tuple , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : str ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Dict ) -> int: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : List[str] ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Union[str, Any] = ["""torch"""] def __init__( self : Tuple , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Optional[int] ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : Tuple , **_UpperCamelCase : Union[str, Any] ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Dict ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Tuple = ["""torch"""] def __init__( self : Optional[int] , *_UpperCamelCase : str , **_UpperCamelCase : int ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Any ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : int ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[str] = ["""torch"""] def __init__( self : Optional[Any] , *_UpperCamelCase : Any , **_UpperCamelCase : Tuple ) -> Any: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Tuple , *_UpperCamelCase : Dict , **_UpperCamelCase : Optional[Any] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : List[str] , *_UpperCamelCase : Dict , **_UpperCamelCase : str ) -> List[str]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[Any] = ["""torch"""] def __init__( self : Union[str, Any] , *_UpperCamelCase : Dict , **_UpperCamelCase : str ) -> str: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : List[str] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : List[Any] ) -> str: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Any = ["""torch"""] def __init__( self : str , *_UpperCamelCase : str , **_UpperCamelCase : int ) -> Tuple: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : List[str] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Dict , **_UpperCamelCase : List[str] ) -> Dict: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : List[Any] = ["""torch"""] def __init__( self : List[Any] , *_UpperCamelCase : Tuple , **_UpperCamelCase : Union[str, Any] ) -> Tuple: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Dict ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : Any , **_UpperCamelCase : Tuple ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Tuple = ["""torch"""] def __init__( self : List[Any] , *_UpperCamelCase : str , **_UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : int , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : str , **_UpperCamelCase : int ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Dict = ["""torch"""] def __init__( self : str , *_UpperCamelCase : Any , **_UpperCamelCase : Any ) -> Any: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : List[str] , *_UpperCamelCase : int , **_UpperCamelCase : Dict ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Dict , *_UpperCamelCase : Any , **_UpperCamelCase : Optional[int] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Tuple = ["""torch"""] def __init__( self : List[str] , *_UpperCamelCase : str , **_UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : List[str] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Any ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : int = ["""torch"""] def __init__( self : Optional[Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Optional[Any] , *_UpperCamelCase : int , **_UpperCamelCase : Union[str, Any] ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : int , **_UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : int = ["""torch"""] def __init__( self : int , *_UpperCamelCase : Any , **_UpperCamelCase : List[Any] ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : int , *_UpperCamelCase : Tuple , **_UpperCamelCase : Optional[int] ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : Union[str, Any] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Tuple ) -> List[str]: '''simple docstring''' requires_backends(cls , ["torch"] ) class lowercase ( metaclass=a ): lowercase__ : Optional[int] = ["""torch"""] def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) @classmethod def __snake_case( cls : Any , *_UpperCamelCase : List[Any] , **_UpperCamelCase : List[Any] ) -> Any: '''simple docstring''' requires_backends(cls , ["torch"] ) @classmethod def __snake_case( cls : str , *_UpperCamelCase : Tuple , **_UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["torch"] )
647
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
1
import datasets from .nmt_bleu import compute_bleu # From: https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py _lowerCamelCase : List[Any] = '''\ @INPROCEEDINGS{Papineni02bleu:a, author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu}, title = {BLEU: a Method for Automatic Evaluation of Machine Translation}, booktitle = {}, year = {2002}, pages = {311--318} } @inproceedings{lin-och-2004-orange, title = "{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation", author = "Lin, Chin-Yew and Och, Franz Josef", booktitle = "{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics", month = "aug 23{--}aug 27", year = "2004", address = "Geneva, Switzerland", publisher = "COLING", url = "https://www.aclweb.org/anthology/C04-1072", pages = "501--507", } ''' _lowerCamelCase : str = '''\ BLEU (bilingual evaluation understudy) is an algorithm for evaluating the quality of text which has been machine-translated from one natural language to another. Quality is considered to be the correspondence between a machine\'s output and that of a human: "the closer a machine translation is to a professional human translation, the better it is" – this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and remains one of the most popular automated and inexpensive metrics. Scores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations. Those scores are then averaged over the whole corpus to reach an estimate of the translation\'s overall quality. Intelligibility or grammatical correctness are not taken into account[citation needed]. BLEU\'s output is always a number between 0 and 1. This value indicates how similar the candidate text is to the reference texts, with values closer to 1 representing more similar texts. Few human translations will attain a score of 1, since this would indicate that the candidate is identical to one of the reference translations. For this reason, it is not necessary to attain a score of 1. Because there are more opportunities to match, adding additional reference translations will increase the BLEU score. ''' _lowerCamelCase : Dict = ''' Computes BLEU score of translated segments against one or more references. Args: predictions: list of translations to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. max_order: Maximum n-gram order to use when computing BLEU score. smooth: Whether or not to apply Lin et al. 2004 smoothing. Returns: \'bleu\': bleu score, \'precisions\': geometric mean of n-gram precisions, \'brevity_penalty\': brevity penalty, \'length_ratio\': ratio of lengths, \'translation_length\': translation_length, \'reference_length\': reference_length Examples: >>> predictions = [ ... ["hello", "there", "general", "kenobi"], # tokenized prediction of the first sample ... ["foo", "bar", "foobar"] # tokenized prediction of the second sample ... ] >>> references = [ ... [["hello", "there", "general", "kenobi"], ["hello", "there", "!"]], # tokenized references for the first sample (2 references) ... [["foo", "bar", "foobar"]] # tokenized references for the second sample (1 reference) ... ] >>> bleu = datasets.load_metric("bleu") >>> results = bleu.compute(predictions=predictions, references=references) >>> print(results["bleu"]) 1.0 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def __snake_case( self : int ) -> Tuple: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py"] , reference_urls=[ "https://en.wikipedia.org/wiki/BLEU", "https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213", ] , ) def __snake_case( self : List[str] , _UpperCamelCase : str , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any]=4 , _UpperCamelCase : Tuple=False ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = compute_bleu( reference_corpus=_UpperCamelCase , translation_corpus=_UpperCamelCase , max_order=_UpperCamelCase , smooth=_UpperCamelCase ) ((SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE) , (SCREAMING_SNAKE_CASE)) = score return { "bleu": bleu, "precisions": precisions, "brevity_penalty": bp, "length_ratio": ratio, "translation_length": translation_length, "reference_length": reference_length, }
647
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
1
import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets _lowerCamelCase : Any = datasets.logging.get_logger(__name__) _lowerCamelCase : Any = '''\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = "Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric", author = "Moosavi, Nafise Sadat and Strube, Michael", booktitle = "Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)", month = aug, year = "2016", address = "Berlin, Germany", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/P16-1060", doi = "10.18653/v1/P16-1060", pages = "632--642", } ''' _lowerCamelCase : List[Any] = '''\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the "([pos] [word])" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a "-" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section "*_conll File Format"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. ''' _lowerCamelCase : List[Any] = ''' Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting \'keep_singletons=False\', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the \'NP_only\' option, the scorer will only evaluate the resolution of NPs. min_span: By setting \'min_span\', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: \'mentions\': mentions \'muc\': MUC metric [Vilain et al, 1995] \'bcub\': B-cubed [Bagga and Baldwin, 1998] \'ceafe\': CEAFe [Luo et al., 2005] \'lea\': LEA [Moosavi and Strube, 2016] \'conll_score\': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric(\'coval\') >>> words = [\'bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -\', ... \'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)\', ... \'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)\', ... \'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -\', ... \'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -\', ... \'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -\'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {\'mentions/recall\': 1.0,[...] \'conll_score\': 100.0} ''' def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Union[str, Any]=False , UpperCAmelCase__ : Optional[int]=False , UpperCAmelCase__ : int=True , UpperCAmelCase__ : Optional[int]=False , UpperCAmelCase__ : Union[str, Any]="dummy_doc" ): SCREAMING_SNAKE_CASE = {doc: key_lines} SCREAMING_SNAKE_CASE = {doc: sys_lines} SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = reader.get_doc_mentions(UpperCAmelCase__ , key_doc_lines[doc] , UpperCAmelCase__ ) key_singletons_num += singletons_num if NP_only or min_span: SCREAMING_SNAKE_CASE = reader.set_annotated_parse_trees(UpperCAmelCase__ , key_doc_lines[doc] , UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = reader.get_doc_mentions(UpperCAmelCase__ , sys_doc_lines[doc] , UpperCAmelCase__ ) sys_singletons_num += singletons_num if NP_only or min_span: SCREAMING_SNAKE_CASE = reader.set_annotated_parse_trees(UpperCAmelCase__ , key_doc_lines[doc] , UpperCAmelCase__ , UpperCAmelCase__ ) if remove_nested: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = reader.remove_nested_coref_mentions(UpperCAmelCase__ , UpperCAmelCase__ ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = reader.remove_nested_coref_mentions(UpperCAmelCase__ , UpperCAmelCase__ ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters SCREAMING_SNAKE_CASE = reader.get_mention_assignments(UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = reader.get_mention_assignments(UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( "Number of removed nested coreferring mentions in the key " F"annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}" ) logger.info( "Number of resulting singleton clusters in the key " F"annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}" ) if not keep_singletons: logger.info( F"{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system " "files, respectively" ) return doc_coref_infos def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = get_coref_infos(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 for name, metric in metrics: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = evaluator.evaluate_documents(UpperCAmelCase__ , UpperCAmelCase__ , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F"{name}/recall": recall, F"{name}/precision": precision, F"{name}/f1": fa} ) logger.info( name.ljust(1_0 ) , F"Recall: {recall * 1_0_0:.2f}" , F" Precision: {precision * 1_0_0:.2f}" , F" F1: {fa * 1_0_0:.2f}" , ) if conll_subparts_num == 3: SCREAMING_SNAKE_CASE = (conll / 3) * 1_0_0 logger.info(F"CoNLL score: {conll:.2f}" ) output_scores.update({"conll_score": conll} ) return output_scores def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = False for line in key_lines: if not line.startswith("#" ): if len(line.split() ) > 6: SCREAMING_SNAKE_CASE = line.split()[5] if not parse_col == "-": SCREAMING_SNAKE_CASE = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ) ), "references": datasets.Sequence(datasets.Value("string" ) ), } ) , codebase_urls=["https://github.com/ns-moosavi/coval"] , reference_urls=[ "https://github.com/ns-moosavi/coval", "https://www.aclweb.org/anthology/P16-1060", "http://www.conll.cemantix.org/2012/data.html", ] , ) def __snake_case( self : Dict , _UpperCamelCase : Dict , _UpperCamelCase : Optional[int] , _UpperCamelCase : List[str]=True , _UpperCamelCase : Any=False , _UpperCamelCase : Tuple=False , _UpperCamelCase : str=False ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = [ ("mentions", evaluator.mentions), ("muc", evaluator.muc), ("bcub", evaluator.b_cubed), ("ceafe", evaluator.ceafe), ("lea", evaluator.lea), ] if min_span: SCREAMING_SNAKE_CASE = util.check_gold_parse_annotation(_UpperCamelCase ) if not has_gold_parse: raise NotImplementedError("References should have gold parse annotation to use 'min_span'." ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" SCREAMING_SNAKE_CASE = evaluate( key_lines=_UpperCamelCase , sys_lines=_UpperCamelCase , metrics=_UpperCamelCase , NP_only=_UpperCamelCase , remove_nested=_UpperCamelCase , keep_singletons=_UpperCamelCase , min_span=_UpperCamelCase , ) return score
647
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
1
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __snake_case( self : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
1
def __lowerCamelCase (UpperCAmelCase__ : int ): if num <= 0: raise ValueError("Input must be a positive integer" ) SCREAMING_SNAKE_CASE = [True] * (num + 1) SCREAMING_SNAKE_CASE = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() _lowerCamelCase : Optional[Any] = int(input('''Enter a positive integer: ''').strip()) print(prime_sieve_eratosthenes(user_num))
647
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(F"The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
1
from __future__ import annotations from typing import TypedDict class lowercase ( a ): lowercase__ : str lowercase__ : int def __lowerCamelCase (UpperCAmelCase__ : str ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise TypeError("The parameter s type must be str." ) return [s[i:] + s[:i] for i in range(len(UpperCAmelCase__ ) )] def __lowerCamelCase (UpperCAmelCase__ : str ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise TypeError("The parameter s type must be str." ) if not s: raise ValueError("The parameter s must not be empty." ) SCREAMING_SNAKE_CASE = all_rotations(UpperCAmelCase__ ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation SCREAMING_SNAKE_CASE = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(UpperCAmelCase__ ), } return response def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise TypeError("The parameter bwt_string type must be str." ) if not bwt_string: raise ValueError("The parameter bwt_string must not be empty." ) try: SCREAMING_SNAKE_CASE = int(UpperCAmelCase__ ) except ValueError: raise TypeError( "The parameter idx_original_string type must be int or passive" " of cast to int." ) if idx_original_string < 0: raise ValueError("The parameter idx_original_string must not be lower than 0." ) if idx_original_string >= len(UpperCAmelCase__ ): raise ValueError( "The parameter idx_original_string must be lower than" " len(bwt_string)." ) SCREAMING_SNAKE_CASE = [""] * len(UpperCAmelCase__ ) for _ in range(len(UpperCAmelCase__ ) ): for i in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": _lowerCamelCase : List[Any] = '''Provide a string that I will generate its BWT transform: ''' _lowerCamelCase : List[Any] = input(entry_msg).strip() _lowerCamelCase : List[str] = bwt_transform(s) print( f"""Burrows Wheeler transform for string '{s}' results """ f"""in '{result["bwt_string"]}'""" ) _lowerCamelCase : str = reverse_bwt(result['''bwt_string'''], result['''idx_original_string''']) print( f"""Reversing Burrows Wheeler transform for entry '{result["bwt_string"]}' """ f"""we get original string '{original_string}'""" )
647
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow 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 DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
1
import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class lowercase ( unittest.TestCase ): def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() # fmt: off SCREAMING_SNAKE_CASE = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest"] # fmt: on SCREAMING_SNAKE_CASE = 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] ) ) SCREAMING_SNAKE_CASE = { "do_resize": True, "size": {"height": 18, "width": 18}, "do_normalize": True, "image_mean": [0.5, 0.5, 0.5], "image_std": [0.5, 0.5, 0.5], } SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname , _UpperCamelCase ) with open(self.image_processor_file , "w" , encoding="utf-8" ) as fp: json.dump(_UpperCamelCase , _UpperCamelCase ) def __snake_case( self : Tuple , **_UpperCamelCase : Optional[Any] ) -> str: '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname , **_UpperCamelCase ) def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Dict: '''simple docstring''' return ViTImageProcessor.from_pretrained(self.tmpdirname , **_UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(_UpperCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __snake_case( self : str ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=_UpperCamelCase , image_processor=_UpperCamelCase ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE = self.get_image_processor(do_normalize=_UpperCamelCase , padding_value=1.0 ) SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=_UpperCamelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _UpperCamelCase ) def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=_UpperCamelCase , image_processor=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = image_processor(_UpperCamelCase , return_tensors="np" ) SCREAMING_SNAKE_CASE = processor(images=_UpperCamelCase , return_tensors="np" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=_UpperCamelCase , image_processor=_UpperCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = processor(text=_UpperCamelCase ) SCREAMING_SNAKE_CASE = tokenizer(_UpperCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __snake_case( self : Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=_UpperCamelCase , image_processor=_UpperCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = processor(text=_UpperCamelCase , images=_UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["input_ids", "token_type_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with self.assertRaises(_UpperCamelCase ): processor() def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=_UpperCamelCase , image_processor=_UpperCamelCase ) SCREAMING_SNAKE_CASE = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE = processor.batch_decode(_UpperCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.batch_decode(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = VisionTextDualEncoderProcessor(tokenizer=_UpperCamelCase , image_processor=_UpperCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = processor(text=_UpperCamelCase , images=_UpperCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
647
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
1
import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class lowercase ( a , a ): @register_to_config def __init__( self : List[Any] , *, _UpperCamelCase : int = 4 , _UpperCamelCase : int = 768 , _UpperCamelCase : int , _UpperCamelCase : Optional[int] , ) -> Optional[Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE = nn.Parameter(torch.zeros(_UpperCamelCase ) ) # parameters for additional clip time embeddings SCREAMING_SNAKE_CASE = nn.Linear(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = nn.Linear(_UpperCamelCase , _UpperCamelCase ) # parameters for encoder hidden states SCREAMING_SNAKE_CASE = clip_extra_context_tokens SCREAMING_SNAKE_CASE = nn.Linear( _UpperCamelCase , self.clip_extra_context_tokens * cross_attention_dim ) SCREAMING_SNAKE_CASE = nn.Linear(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = nn.LayerNorm(_UpperCamelCase ) def __snake_case( self : Optional[int] , *, _UpperCamelCase : Optional[int] , _UpperCamelCase : str , _UpperCamelCase : int , _UpperCamelCase : str ) -> List[str]: '''simple docstring''' if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings SCREAMING_SNAKE_CASE = image_embeddings.shape[0] SCREAMING_SNAKE_CASE = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) SCREAMING_SNAKE_CASE = classifier_free_guidance_embeddings.expand( _UpperCamelCase , -1 ) SCREAMING_SNAKE_CASE = torch.cat([classifier_free_guidance_embeddings, image_embeddings] , dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] SCREAMING_SNAKE_CASE = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... SCREAMING_SNAKE_CASE = self.embedding_proj(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.clip_image_embeddings_project_to_time_embeddings(_UpperCamelCase ) SCREAMING_SNAKE_CASE = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" SCREAMING_SNAKE_CASE = self.clip_extra_context_tokens_proj(_UpperCamelCase ) SCREAMING_SNAKE_CASE = clip_extra_context_tokens.reshape(_UpperCamelCase , -1 , self.clip_extra_context_tokens ) SCREAMING_SNAKE_CASE = clip_extra_context_tokens.permute(0 , 2 , 1 ) SCREAMING_SNAKE_CASE = self.encoder_hidden_states_proj(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.text_encoder_hidden_states_norm(_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] , dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
647
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # Validation if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(UpperCAmelCase__ ) != 3 or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(UpperCAmelCase__ ) == 0: return 0 if min(UpperCAmelCase__ ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(UpperCAmelCase__ ) >= 3_6_6: raise ValueError("All days elements should be less than 366" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> int: if index > 3_6_5: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 3_0 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
647
1
import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import ( DiffusionPipeline, UnCLIPImageVariationPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel, ) from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, load_image, require_torch_gpu, skip_mps from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[Any] = UnCLIPImageVariationPipeline lowercase__ : Dict = IMAGE_VARIATION_PARAMS - {"""height""", """width""", """guidance_scale"""} lowercase__ : int = IMAGE_VARIATION_BATCH_PARAMS lowercase__ : Tuple = [ """generator""", """return_dict""", """decoder_num_inference_steps""", """super_res_num_inference_steps""", ] lowercase__ : int = False @property def __snake_case( self : Tuple ) -> List[str]: '''simple docstring''' return 32 @property def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' return 32 @property def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' return self.time_input_dim @property def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' return self.time_input_dim * 4 @property def __snake_case( self : List[Any] ) -> str: '''simple docstring''' return 100 @property def __snake_case( self : Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) return tokenizer @property def __snake_case( self : str ) -> str: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) return CLIPTextModelWithProjection(_UpperCamelCase ) @property def __snake_case( self : Dict ) -> Tuple: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) return CLIPVisionModelWithProjection(_UpperCamelCase ) @property def __snake_case( self : str ) -> int: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = { "clip_embeddings_dim": self.text_embedder_hidden_size, "time_embed_dim": self.time_embed_dim, "cross_attention_dim": self.cross_attention_dim, } SCREAMING_SNAKE_CASE = UnCLIPTextProjModel(**_UpperCamelCase ) return model @property def __snake_case( self : List[Any] ) -> int: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = { "sample_size": 32, # RGB in channels "in_channels": 3, # Out channels is double in channels because predicts mean and variance "out_channels": 6, "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": "identity", } SCREAMING_SNAKE_CASE = UNetaDConditionModel(**_UpperCamelCase ) return model @property def __snake_case( self : List[Any] ) -> Tuple: '''simple docstring''' return { "sample_size": 64, "layers_per_block": 1, "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"), "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"), "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "in_channels": 6, "out_channels": 3, } @property def __snake_case( self : str ) -> Any: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = UNetaDModel(**self.dummy_super_res_kwargs ) return model @property def __snake_case( self : Optional[Any] ) -> Dict: '''simple docstring''' torch.manual_seed(1 ) SCREAMING_SNAKE_CASE = UNetaDModel(**self.dummy_super_res_kwargs ) return model def __snake_case( self : Union[str, Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.dummy_decoder SCREAMING_SNAKE_CASE = self.dummy_text_proj SCREAMING_SNAKE_CASE = self.dummy_text_encoder SCREAMING_SNAKE_CASE = self.dummy_tokenizer SCREAMING_SNAKE_CASE = self.dummy_super_res_first SCREAMING_SNAKE_CASE = self.dummy_super_res_last SCREAMING_SNAKE_CASE = UnCLIPScheduler( variance_type="learned_range" , prediction_type="epsilon" , num_train_timesteps=1_000 , ) SCREAMING_SNAKE_CASE = UnCLIPScheduler( variance_type="fixed_small_log" , prediction_type="epsilon" , num_train_timesteps=1_000 , ) SCREAMING_SNAKE_CASE = CLIPImageProcessor(crop_size=32 , size=32 ) SCREAMING_SNAKE_CASE = self.dummy_image_encoder return { "decoder": decoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "text_proj": text_proj, "feature_extractor": feature_extractor, "image_encoder": image_encoder, "super_res_first": super_res_first, "super_res_last": super_res_last, "decoder_scheduler": decoder_scheduler, "super_res_scheduler": super_res_scheduler, } def __snake_case( self : Optional[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[int]=0 , _UpperCamelCase : int=True ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 32, 32) , rng=random.Random(_UpperCamelCase ) ).to(_UpperCamelCase ) if str(_UpperCamelCase ).startswith("mps" ): SCREAMING_SNAKE_CASE = torch.manual_seed(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) if pil_image: SCREAMING_SNAKE_CASE = input_image * 0.5 + 0.5 SCREAMING_SNAKE_CASE = input_image.clamp(0 , 1 ) SCREAMING_SNAKE_CASE = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() SCREAMING_SNAKE_CASE = DiffusionPipeline.numpy_to_pil(_UpperCamelCase )[0] return { "image": input_image, "generator": generator, "decoder_num_inference_steps": 2, "super_res_num_inference_steps": 2, "output_type": "np", } def __snake_case( self : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = "cpu" SCREAMING_SNAKE_CASE = self.get_dummy_components() SCREAMING_SNAKE_CASE = self.pipeline_class(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_UpperCamelCase , pil_image=_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = output.images SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_UpperCamelCase , pil_image=_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe( **_UpperCamelCase , return_dict=_UpperCamelCase , )[0] SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE = np.array( [ 0.9_9_9_7, 0.0_0_0_2, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_6_9, 0.0_0_2_3, 0.9_9_9_7, 0.9_9_6_9, 0.9_9_7_0, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 def __snake_case( self : Optional[int] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = "cpu" SCREAMING_SNAKE_CASE = self.get_dummy_components() SCREAMING_SNAKE_CASE = self.pipeline_class(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_UpperCamelCase , pil_image=_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = output.images SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_UpperCamelCase , pil_image=_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe( **_UpperCamelCase , return_dict=_UpperCamelCase , )[0] SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE = np.array([0.9_9_9_7, 0.0_0_0_3, 0.9_9_9_7, 0.9_9_9_7, 0.9_9_7_0, 0.0_0_2_4, 0.9_9_9_7, 0.9_9_7_1, 0.9_9_7_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 def __snake_case( self : Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = "cpu" SCREAMING_SNAKE_CASE = self.get_dummy_components() SCREAMING_SNAKE_CASE = self.pipeline_class(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_UpperCamelCase , pil_image=_UpperCamelCase ) SCREAMING_SNAKE_CASE = [ pipeline_inputs["image"], pipeline_inputs["image"], ] SCREAMING_SNAKE_CASE = pipe(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = output.images SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_UpperCamelCase , pil_image=_UpperCamelCase ) SCREAMING_SNAKE_CASE = [ tuple_pipeline_inputs["image"], tuple_pipeline_inputs["image"], ] SCREAMING_SNAKE_CASE = pipe( **_UpperCamelCase , return_dict=_UpperCamelCase , )[0] SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (2, 64, 64, 3) SCREAMING_SNAKE_CASE = np.array( [ 0.9_9_9_7, 0.9_9_8_9, 0.0_0_0_8, 0.0_0_2_1, 0.9_9_6_0, 0.0_0_1_8, 0.0_0_1_4, 0.0_0_0_2, 0.9_9_3_3, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = torch.device("cpu" ) class lowercase : lowercase__ : Optional[Any] = 1 SCREAMING_SNAKE_CASE = self.get_dummy_components() SCREAMING_SNAKE_CASE = self.pipeline_class(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.Generator(device=_UpperCamelCase ).manual_seed(0 ) SCREAMING_SNAKE_CASE = pipe.decoder.dtype SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = ( batch_size, pipe.decoder.config.in_channels, pipe.decoder.config.sample_size, pipe.decoder.config.sample_size, ) SCREAMING_SNAKE_CASE = pipe.prepare_latents( _UpperCamelCase , dtype=_UpperCamelCase , device=_UpperCamelCase , generator=_UpperCamelCase , latents=_UpperCamelCase , scheduler=DummyScheduler() ) SCREAMING_SNAKE_CASE = ( batch_size, pipe.super_res_first.config.in_channels // 2, pipe.super_res_first.config.sample_size, pipe.super_res_first.config.sample_size, ) SCREAMING_SNAKE_CASE = pipe.prepare_latents( _UpperCamelCase , dtype=_UpperCamelCase , device=_UpperCamelCase , generator=_UpperCamelCase , latents=_UpperCamelCase , scheduler=DummyScheduler() ) SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_UpperCamelCase , pil_image=_UpperCamelCase ) SCREAMING_SNAKE_CASE = pipe( **_UpperCamelCase , decoder_latents=_UpperCamelCase , super_res_latents=_UpperCamelCase ).images SCREAMING_SNAKE_CASE = self.get_dummy_inputs(_UpperCamelCase , pil_image=_UpperCamelCase ) # Don't pass image, instead pass embedding SCREAMING_SNAKE_CASE = pipeline_inputs.pop("image" ) SCREAMING_SNAKE_CASE = pipe.image_encoder(_UpperCamelCase ).image_embeds SCREAMING_SNAKE_CASE = pipe( **_UpperCamelCase , decoder_latents=_UpperCamelCase , super_res_latents=_UpperCamelCase , image_embeddings=_UpperCamelCase , ).images # make sure passing text embeddings manually is identical assert np.abs(img_out_a - img_out_a ).max() < 1e-4 @skip_mps def __snake_case( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = torch_device == "cpu" # Check is relaxed because there is not a torch 2.0 sliced attention added kv processor SCREAMING_SNAKE_CASE = 1e-2 self._test_attention_slicing_forward_pass( test_max_difference=_UpperCamelCase , expected_max_diff=_UpperCamelCase ) @skip_mps def __snake_case( self : List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = torch_device == "cpu" SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = [ "decoder_num_inference_steps", "super_res_num_inference_steps", ] self._test_inference_batch_single_identical( test_max_difference=_UpperCamelCase , relax_max_difference=_UpperCamelCase , additional_params_copy_to_batched_inputs=_UpperCamelCase , ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = [ "decoder_num_inference_steps", "super_res_num_inference_steps", ] if torch_device == "mps": # TODO: MPS errors with larger batch sizes SCREAMING_SNAKE_CASE = [2, 3] self._test_inference_batch_consistent( batch_sizes=_UpperCamelCase , additional_params_copy_to_batched_inputs=_UpperCamelCase , ) else: self._test_inference_batch_consistent( additional_params_copy_to_batched_inputs=_UpperCamelCase ) @skip_mps def __snake_case( self : str ) -> List[str]: '''simple docstring''' return super().test_dict_tuple_outputs_equivalent() @skip_mps def __snake_case( self : Tuple ) -> str: '''simple docstring''' return super().test_save_load_local() @skip_mps def __snake_case( self : List[str] ) -> List[Any]: '''simple docstring''' return super().test_save_load_optional_components() @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def __snake_case( self : List[Any] ) -> int: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __snake_case( self : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unclip/cat.png" ) SCREAMING_SNAKE_CASE = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/unclip/karlo_v1_alpha_cat_variation_fp16.npy" ) SCREAMING_SNAKE_CASE = UnCLIPImageVariationPipeline.from_pretrained( "kakaobrain/karlo-v1-alpha-image-variations" , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE = pipeline.to(_UpperCamelCase ) pipeline.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.Generator(device="cpu" ).manual_seed(0 ) SCREAMING_SNAKE_CASE = pipeline( _UpperCamelCase , generator=_UpperCamelCase , output_type="np" , ) SCREAMING_SNAKE_CASE = output.images[0] assert image.shape == (256, 256, 3) assert_mean_pixel_difference(_UpperCamelCase , _UpperCamelCase , 15 )
647
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : int ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging _lowerCamelCase : List[str] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : List[str] , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[Dict[str, int]] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BICUBIC , _UpperCamelCase : bool = True , _UpperCamelCase : bool = True , _UpperCamelCase : Union[int, float] = 1 / 255 , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[Union[float, List[float]]] = None , _UpperCamelCase : Optional[Union[float, List[float]]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase , default_to_square=_UpperCamelCase , param_name="crop_size" ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = do_center_crop SCREAMING_SNAKE_CASE = crop_size SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN SCREAMING_SNAKE_CASE = image_std if image_std is not None else IMAGENET_DEFAULT_STD def __snake_case( self : Any , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : str , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) if "shortest_edge" in size: SCREAMING_SNAKE_CASE = get_resize_output_image_size(_UpperCamelCase , size=size["shortest_edge"] , default_to_square=_UpperCamelCase ) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: SCREAMING_SNAKE_CASE = (size["height"], size["width"]) else: raise ValueError(F"Size must contain 'height' and 'width' keys or 'shortest_edge' key. Got {size.keys()}" ) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : str , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(F"The `size` parameter must contain the keys (height, width). Got {size.keys()}" ) return center_crop(_UpperCamelCase , size=(size["height"], size["width"]) , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , _UpperCamelCase : np.ndarray , _UpperCamelCase : float , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Dict ) -> np.ndarray: '''simple docstring''' return rescale(_UpperCamelCase , scale=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Union[float, List[float]] , _UpperCamelCase : Union[float, List[float]] , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Optional[Any] , ) -> np.ndarray: '''simple docstring''' return normalize(_UpperCamelCase , mean=_UpperCamelCase , std=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : ImageInput , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : int = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[float] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[float, List[float]]] = None , _UpperCamelCase : Optional[Union[float, List[float]]] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : Union[str, ChannelDimension] = ChannelDimension.FIRST , **_UpperCamelCase : Any , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE = do_center_crop if do_center_crop is not None else self.do_center_crop SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else self.crop_size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase , param_name="crop_size" , default_to_square=_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) if not is_batched(_UpperCamelCase ): SCREAMING_SNAKE_CASE = [images] if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] if do_center_crop: SCREAMING_SNAKE_CASE = [self.center_crop(image=_UpperCamelCase , size=_UpperCamelCase ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE = [self.rescale(image=_UpperCamelCase , scale=_UpperCamelCase ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE = [self.normalize(image=_UpperCamelCase , mean=_UpperCamelCase , std=_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = {"pixel_values": images} return BatchFeature(data=_UpperCamelCase , tensor_type=_UpperCamelCase )
647
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
1
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # Validation if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(UpperCAmelCase__ ) != 3 or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(UpperCAmelCase__ ) == 0: return 0 if min(UpperCAmelCase__ ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(UpperCAmelCase__ ) >= 3_6_6: raise ValueError("All days elements should be less than 366" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> int: if index > 3_6_5: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 3_0 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
647
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(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"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(UpperCAmelCase__ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
1
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"Only mono-channel audio is supported for input to {self}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __snake_case( self : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
1
from jiwer import compute_measures import datasets _lowerCamelCase : str = '''\ @inproceedings{inproceedings, author = {Morris, Andrew and Maier, Viktoria and Green, Phil}, year = {2004}, month = {01}, pages = {}, title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.} } ''' _lowerCamelCase : int = '''\ Word error rate (WER) is a common metric of the performance of an automatic speech recognition system. The general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort. This problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate. Word error rate can then be computed as: WER = (S + D + I) / N = (S + D + I) / (S + D + C) where S is the number of substitutions, D is the number of deletions, I is the number of insertions, C is the number of correct words, N is the number of words in the reference (N=S+D+C). This value indicates the average number of errors per reference word. The lower the value, the better the performance of the ASR system with a WER of 0 being a perfect score. ''' _lowerCamelCase : int = ''' Compute WER score of transcribed segments against references. Args: references: List of references for each speech input. predictions: List of transcriptions to score. concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively. Returns: (float): the word error rate Examples: >>> predictions = ["this is the prediction", "there is an other sample"] >>> references = ["this is the reference", "there is another one"] >>> wer = datasets.load_metric("wer") >>> wer_score = wer.compute(predictions=predictions, references=references) >>> print(wer_score) 0.5 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def __snake_case( self : int ) -> List[Any]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/jitsi/jiwer/"] , reference_urls=[ "https://en.wikipedia.org/wiki/Word_error_rate", ] , ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : List[Any]=None , _UpperCamelCase : List[str]=None , _UpperCamelCase : int=False ) -> Dict: '''simple docstring''' if concatenate_texts: return compute_measures(_UpperCamelCase , _UpperCamelCase )["wer"] else: SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 for prediction, reference in zip(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = compute_measures(_UpperCamelCase , _UpperCamelCase ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
647
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
1
import os import unittest from transformers.models.bartpho.tokenization_bartpho import VOCAB_FILES_NAMES, BartphoTokenizer from transformers.testing_utils import get_tests_dir from ...test_tokenization_common import TokenizerTesterMixin _lowerCamelCase : int = get_tests_dir('''fixtures/test_sentencepiece_bpe.model''') class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = BartphoTokenizer lowercase__ : Dict = False lowercase__ : Union[str, Any] = True def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE = ["▁This", "▁is", "▁a", "▁t", "est"] SCREAMING_SNAKE_CASE = dict(zip(_UpperCamelCase , range(len(_UpperCamelCase ) ) ) ) SCREAMING_SNAKE_CASE = {"unk_token": "<unk>"} SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["monolingual_vocab_file"] ) with open(self.monolingual_vocab_file , "w" , encoding="utf-8" ) as fp: for token in vocab_tokens: fp.write(F"{token} {vocab_tokens[token]}\n" ) SCREAMING_SNAKE_CASE = BartphoTokenizer(_UpperCamelCase , self.monolingual_vocab_file , **self.special_tokens_map ) tokenizer.save_pretrained(self.tmpdirname ) def __snake_case( self : str , **_UpperCamelCase : Dict ) -> Dict: '''simple docstring''' kwargs.update(self.special_tokens_map ) return BartphoTokenizer.from_pretrained(self.tmpdirname , **_UpperCamelCase ) def __snake_case( self : Optional[int] , _UpperCamelCase : Tuple ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = "This is a là test" SCREAMING_SNAKE_CASE = "This is a<unk><unk> test" return input_text, output_text def __snake_case( self : Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = BartphoTokenizer(_UpperCamelCase , self.monolingual_vocab_file , **self.special_tokens_map ) SCREAMING_SNAKE_CASE = "This is a là test" SCREAMING_SNAKE_CASE = "▁This ▁is ▁a ▁l à ▁t est".split() SCREAMING_SNAKE_CASE = tokenizer.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE = [4, 5, 6, 3, 3, 7, 8, 3] self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , _UpperCamelCase )
647
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"Only mono-channel audio is supported for input to {self}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
1
import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) _lowerCamelCase : Union[str, Any] = logging.getLogger(__name__) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = argparse.ArgumentParser( description="Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids)." ) parser.add_argument("--file_path" , type=UpperCAmelCase__ , default="data/dump.txt" , help="The path to the data." ) parser.add_argument("--tokenizer_type" , type=UpperCAmelCase__ , default="bert" , choices=["bert", "roberta", "gpt2"] ) parser.add_argument("--tokenizer_name" , type=UpperCAmelCase__ , default="bert-base-uncased" , help="The tokenizer to use." ) parser.add_argument("--dump_file" , type=UpperCAmelCase__ , default="data/dump" , help="The dump file prefix." ) SCREAMING_SNAKE_CASE = parser.parse_args() logger.info(F"Loading Tokenizer ({args.tokenizer_name})" ) if args.tokenizer_type == "bert": SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained(args.tokenizer_name ) SCREAMING_SNAKE_CASE = tokenizer.special_tokens_map["cls_token"] # `[CLS]` SCREAMING_SNAKE_CASE = tokenizer.special_tokens_map["sep_token"] # `[SEP]` elif args.tokenizer_type == "roberta": SCREAMING_SNAKE_CASE = RobertaTokenizer.from_pretrained(args.tokenizer_name ) SCREAMING_SNAKE_CASE = tokenizer.special_tokens_map["cls_token"] # `<s>` SCREAMING_SNAKE_CASE = tokenizer.special_tokens_map["sep_token"] # `</s>` elif args.tokenizer_type == "gpt2": SCREAMING_SNAKE_CASE = GPTaTokenizer.from_pretrained(args.tokenizer_name ) SCREAMING_SNAKE_CASE = tokenizer.special_tokens_map["bos_token"] # `<|endoftext|>` SCREAMING_SNAKE_CASE = tokenizer.special_tokens_map["eos_token"] # `<|endoftext|>` logger.info(F"Loading text from {args.file_path}" ) with open(args.file_path , "r" , encoding="utf8" ) as fp: SCREAMING_SNAKE_CASE = fp.readlines() logger.info("Start encoding" ) logger.info(F"{len(UpperCAmelCase__ )} examples to process." ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1_0_0_0_0 SCREAMING_SNAKE_CASE = time.time() for text in data: SCREAMING_SNAKE_CASE = F"{bos} {text.strip()} {sep}" SCREAMING_SNAKE_CASE = tokenizer.encode(UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ ) rslt.append(UpperCAmelCase__ ) iter += 1 if iter % interval == 0: SCREAMING_SNAKE_CASE = time.time() logger.info(F"{iter} examples processed. - {(end-start):.2f}s/{interval}expl" ) SCREAMING_SNAKE_CASE = time.time() logger.info("Finished binarization" ) logger.info(F"{len(UpperCAmelCase__ )} examples processed." ) SCREAMING_SNAKE_CASE = F"{args.dump_file}.{args.tokenizer_name}.pickle" SCREAMING_SNAKE_CASE = tokenizer.vocab_size if vocab_size < (1 << 1_6): SCREAMING_SNAKE_CASE = [np.uintaa(UpperCAmelCase__ ) for d in rslt] else: SCREAMING_SNAKE_CASE = [np.intaa(UpperCAmelCase__ ) for d in rslt] random.shuffle(rslt_ ) logger.info(F"Dump to {dp_file}" ) with open(UpperCAmelCase__ , "wb" ) as handle: pickle.dump(rslt_ , UpperCAmelCase__ , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
647
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
1
import inspect import unittest from transformers import ConvNextConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase : def __init__( self : Optional[Any] , _UpperCamelCase : int , _UpperCamelCase : Any=13 , _UpperCamelCase : str=32 , _UpperCamelCase : List[str]=3 , _UpperCamelCase : int=4 , _UpperCamelCase : Dict=[10, 20, 30, 40] , _UpperCamelCase : int=[2, 2, 3, 2] , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : str=True , _UpperCamelCase : List[Any]=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : List[str]=0.0_2 , _UpperCamelCase : Tuple=["stage2", "stage3", "stage4"] , _UpperCamelCase : Union[str, Any]=[2, 3, 4] , _UpperCamelCase : int=None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = num_stages SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = out_features SCREAMING_SNAKE_CASE = out_indices SCREAMING_SNAKE_CASE = scope def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_labels ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' return ConvNextConfig( num_channels=self.num_channels , hidden_sizes=self.hidden_sizes , depths=self.depths , num_stages=self.num_stages , hidden_act=self.hidden_act , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , out_features=self.out_features , out_indices=self.out_indices , num_labels=self.num_labels , ) def __snake_case( self : Optional[int] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = ConvNextModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def __snake_case( self : Tuple , _UpperCamelCase : Dict , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = ConvNextForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[int] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ConvNextBackbone(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) # verify hidden states self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = ConvNextBackbone(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def __snake_case( self : List[str] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Union[str, Any] = ( ( ConvNextModel, ConvNextForImageClassification, ConvNextBackbone, ) if is_torch_available() else () ) lowercase__ : List[Any] = ( {"""feature-extraction""": ConvNextModel, """image-classification""": ConvNextForImageClassification} if is_torch_available() else {} ) lowercase__ : Any = True lowercase__ : str = False lowercase__ : List[Any] = False lowercase__ : List[str] = False lowercase__ : List[str] = False def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ConvNextModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __snake_case( self : Tuple ) -> Optional[Any]: '''simple docstring''' return @unittest.skip(reason="ConvNext does not use inputs_embeds" ) def __snake_case( self : int ) -> str: '''simple docstring''' pass @unittest.skip(reason="ConvNext does not support input and output embeddings" ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason="ConvNext does not use feedforward chunking" ) def __snake_case( self : List[Any] ) -> Dict: '''simple docstring''' pass def __snake_case( self : Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_UpperCamelCase ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' def check_hidden_states_output(_UpperCamelCase : Union[str, Any] , _UpperCamelCase : Dict , _UpperCamelCase : Dict ): SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states SCREAMING_SNAKE_CASE = self.model_tester.num_stages self.assertEqual(len(_UpperCamelCase ) , expected_num_stages + 1 ) # ConvNext's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) @slow def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ConvNextModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : Dict ) -> Any: '''simple docstring''' return AutoImageProcessor.from_pretrained("facebook/convnext-tiny-224" ) if is_vision_available() else None @slow def __snake_case( self : int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = ConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @require_torch class lowercase ( unittest.TestCase , a ): lowercase__ : Any = (ConvNextBackbone,) if is_torch_available() else () lowercase__ : Optional[int] = ConvNextConfig lowercase__ : Union[str, Any] = False def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = ConvNextModelTester(self )
647
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
1
from __future__ import annotations def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not nums: return 0 SCREAMING_SNAKE_CASE = nums[0] SCREAMING_SNAKE_CASE = 0 for num in nums[1:]: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ( max_excluding + num, max(UpperCAmelCase__ , UpperCAmelCase__ ), ) return max(UpperCAmelCase__ , UpperCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
647
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
1
import importlib.metadata import operator import re import sys from typing import Optional from packaging import version _lowerCamelCase : Tuple = { '''<''': operator.lt, '''<=''': operator.le, '''==''': operator.eq, '''!=''': operator.ne, '''>=''': operator.ge, '''>''': operator.gt, } def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[int] ): if got_ver is None or want_ver is None: raise ValueError( F"Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider" F" reinstalling {pkg}." ) if not ops[op](version.parse(UpperCAmelCase__ ) , version.parse(UpperCAmelCase__ ) ): raise ImportError( F"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}" ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = F"\n{hint}" if hint is not None else "" # non-versioned check if re.match(r"^[\w_\-\d]+$" , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = requirement, None, None else: SCREAMING_SNAKE_CASE = re.findall(r"^([^!=<>\s]+)([\s!=<>]{1,2}.+)" , UpperCAmelCase__ ) if not match: raise ValueError( "requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but" F" got {requirement}" ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = match[0] SCREAMING_SNAKE_CASE = want_full.split("," ) # there could be multiple requirements SCREAMING_SNAKE_CASE = {} for w in want_range: SCREAMING_SNAKE_CASE = re.findall(r"^([\s!=<>]{1,2})(.+)" , UpperCAmelCase__ ) if not match: raise ValueError( "requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23," F" but got {requirement}" ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = match[0] SCREAMING_SNAKE_CASE = want_ver if op not in ops: raise ValueError(F"{requirement}: need one of {list(ops.keys() )}, but got {op}" ) # special case if pkg == "python": SCREAMING_SNAKE_CASE = ".".join([str(UpperCAmelCase__ ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) return # check if any version is installed try: SCREAMING_SNAKE_CASE = importlib.metadata.version(UpperCAmelCase__ ) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( F"The '{requirement}' distribution was not found and is required by this application. {hint}" ) # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = "Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main" return require_version(UpperCAmelCase__ , UpperCAmelCase__ )
647
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
1
import argparse from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird from transformers.utils import logging logging.set_verbosity_info() def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Dict ): # Initialise PyTorch model SCREAMING_SNAKE_CASE = BigBirdConfig.from_json_file(UpperCAmelCase__ ) print(F"Building PyTorch model from configuration: {config}" ) if is_trivia_qa: SCREAMING_SNAKE_CASE = BigBirdForQuestionAnswering(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = BigBirdForPreTraining(UpperCAmelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_big_bird(UpperCAmelCase__ , UpperCAmelCase__ , is_trivia_qa=UpperCAmelCase__ ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--big_bird_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained BERT model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--is_trivia_qa''', action='''store_true''', help='''Whether to convert a model with a trivia_qa head.''' ) _lowerCamelCase : Dict = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa )
647
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) _lowerCamelCase : Tuple = { '''configuration_gpt_bigcode''': ['''GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTBigCodeConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Union[str, Any] = [ '''GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTBigCodeForSequenceClassification''', '''GPTBigCodeForTokenClassification''', '''GPTBigCodeForCausalLM''', '''GPTBigCodeModel''', '''GPTBigCodePreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys _lowerCamelCase : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
1
from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : Any = { '''snap-research/efficientformer-l1-300''': ( '''https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json''' ), } class lowercase ( a ): lowercase__ : Tuple = """efficientformer""" def __init__( self : Union[str, Any] , _UpperCamelCase : List[int] = [3, 2, 6, 4] , _UpperCamelCase : List[int] = [48, 96, 224, 448] , _UpperCamelCase : List[bool] = [True, True, True, True] , _UpperCamelCase : int = 448 , _UpperCamelCase : int = 32 , _UpperCamelCase : int = 4 , _UpperCamelCase : int = 7 , _UpperCamelCase : int = 5 , _UpperCamelCase : int = 8 , _UpperCamelCase : int = 4 , _UpperCamelCase : float = 0.0 , _UpperCamelCase : int = 16 , _UpperCamelCase : int = 3 , _UpperCamelCase : int = 3 , _UpperCamelCase : int = 3 , _UpperCamelCase : int = 2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 0.0 , _UpperCamelCase : int = 1 , _UpperCamelCase : bool = True , _UpperCamelCase : bool = True , _UpperCamelCase : float = 1e-5 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : float = 1e-12 , _UpperCamelCase : int = 224 , _UpperCamelCase : float = 1e-05 , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = mlp_expansion_ratio SCREAMING_SNAKE_CASE = downsamples SCREAMING_SNAKE_CASE = dim SCREAMING_SNAKE_CASE = key_dim SCREAMING_SNAKE_CASE = attention_ratio SCREAMING_SNAKE_CASE = resolution SCREAMING_SNAKE_CASE = pool_size SCREAMING_SNAKE_CASE = downsample_patch_size SCREAMING_SNAKE_CASE = downsample_stride SCREAMING_SNAKE_CASE = downsample_pad SCREAMING_SNAKE_CASE = drop_path_rate SCREAMING_SNAKE_CASE = num_metaad_blocks SCREAMING_SNAKE_CASE = distillation SCREAMING_SNAKE_CASE = use_layer_scale SCREAMING_SNAKE_CASE = layer_scale_init_value SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = batch_norm_eps
647
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
1
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device _lowerCamelCase : Union[str, Any] = False class lowercase ( unittest.TestCase ): pass @nightly @require_torch_gpu class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __snake_case( self : str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion" , torch_dtype=torch.floataa ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg" ) SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = pipe.dual_guided( prompt="first prompt" , image=_UpperCamelCase , text_to_image_strength=0.7_5 , generator=_UpperCamelCase , guidance_scale=7.5 , num_inference_steps=2 , output_type="numpy" , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(_UpperCamelCase ) SCREAMING_SNAKE_CASE = VersatileDiffusionPipeline.from_pretrained(_UpperCamelCase , torch_dtype=torch.floataa ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = generator.manual_seed(0 ) SCREAMING_SNAKE_CASE = pipe.dual_guided( prompt="first prompt" , image=_UpperCamelCase , text_to_image_strength=0.7_5 , generator=_UpperCamelCase , guidance_scale=7.5 , num_inference_steps=2 , output_type="numpy" , ).images assert np.abs(image - new_image ).sum() < 1e-5, "Models don't have the same forward pass" def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion" , torch_dtype=torch.floataa ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) SCREAMING_SNAKE_CASE = "cyberpunk 2077" SCREAMING_SNAKE_CASE = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg" ) SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = pipe.dual_guided( prompt=_UpperCamelCase , image=_UpperCamelCase , text_to_image_strength=0.7_5 , generator=_UpperCamelCase , guidance_scale=7.5 , num_inference_steps=50 , output_type="numpy" , ).images SCREAMING_SNAKE_CASE = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE = np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 SCREAMING_SNAKE_CASE = "A painting of a squirrel eating a burger " SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = pipe.text_to_image( prompt=_UpperCamelCase , generator=_UpperCamelCase , guidance_scale=7.5 , num_inference_steps=50 , output_type="numpy" ).images SCREAMING_SNAKE_CASE = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE = np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 SCREAMING_SNAKE_CASE = pipe.image_variation(_UpperCamelCase , generator=_UpperCamelCase , output_type="numpy" ).images SCREAMING_SNAKE_CASE = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE = np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1
647
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
1
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
1
import argparse import os import re import torch from flax.traverse_util import flatten_dict from tax import checkpoints from transformers import ( AutoTokenizer, PixaStructConfig, PixaStructForConditionalGeneration, PixaStructImageProcessor, PixaStructProcessor, PixaStructTextConfig, PixaStructVisionConfig, ) def __lowerCamelCase (UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = checkpoints.load_tax_checkpoint(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = flatten_dict(UpperCAmelCase__ ) return flax_params def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = { "token_embedder": "embeddings", "encoder_norm": "layernorm", "kernel": "weight", ".out": ".output", "scale": "weight", "embedders_0.pos_embedding": "row_embedder.weight", "embedders_1.pos_embedding": "column_embedder.weight", } SCREAMING_SNAKE_CASE = { "query": "attention.query", "key": "attention.key", "value": "attention.value", "output.dense": "output", "encoder_decoder_attention.o": "encoder_decoder_attention.attention.o", "pre_self_attention_layer_norm": "self_attention.layer_norm", "pre_cross_attention_layer_norm": "encoder_decoder_attention.layer_norm", "mlp.": "mlp.DenseReluDense.", "pre_mlp_layer_norm": "mlp.layer_norm", "self_attention.o": "self_attention.attention.o", "decoder.embeddings.embedding": "decoder.embed_tokens.weight", "decoder.relpos_bias.rel_embedding": "decoder.layer.0.self_attention.attention.relative_attention_bias.weight", "decoder.decoder_norm.weight": "decoder.final_layer_norm.weight", "decoder.logits_dense.weight": "decoder.lm_head.weight", } for key in flax_dict.keys(): if "target" in key: # remove the first prefix from the key SCREAMING_SNAKE_CASE = ".".join(key[1:] ) # rename the key for old, new in CONVERSION_MAPPING.items(): SCREAMING_SNAKE_CASE = new_key.replace(UpperCAmelCase__ , UpperCAmelCase__ ) if "decoder" in new_key: for old, new in DECODER_CONVERSION_MAPPING.items(): SCREAMING_SNAKE_CASE = new_key.replace(UpperCAmelCase__ , UpperCAmelCase__ ) if "layers" in new_key and "decoder" not in new_key: # use regex to replace the layer number SCREAMING_SNAKE_CASE = re.sub(r"layers_(\d+)" , r"layer.\1" , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_key.replace("encoder" , "encoder.encoder" ) elif "layers" in new_key and "decoder" in new_key: # use regex to replace the layer number SCREAMING_SNAKE_CASE = re.sub(r"layers_(\d+)" , r"layer.\1" , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = flax_dict[key] SCREAMING_SNAKE_CASE = {} # convert converted_dict into torch format for key in converted_dict.keys(): if ("embed_tokens" not in key) and ("embedder" not in key): SCREAMING_SNAKE_CASE = torch.from_numpy(converted_dict[key].T ) else: SCREAMING_SNAKE_CASE = torch.from_numpy(converted_dict[key] ) return converted_torch_dict def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : str , UpperCAmelCase__ : Dict=False , UpperCAmelCase__ : Any=False ): SCREAMING_SNAKE_CASE = get_flax_param(UpperCAmelCase__ ) if not use_large: SCREAMING_SNAKE_CASE = PixaStructVisionConfig() SCREAMING_SNAKE_CASE = PixaStructTextConfig() else: SCREAMING_SNAKE_CASE = PixaStructVisionConfig( hidden_size=1_5_3_6 , d_ff=3_9_6_8 , num_attention_heads=2_4 , num_hidden_layers=1_8 ) SCREAMING_SNAKE_CASE = PixaStructTextConfig(hidden_size=1_5_3_6 , d_ff=3_9_6_8 , num_heads=2_4 , num_layers=1_8 ) SCREAMING_SNAKE_CASE = PixaStructConfig( vision_config=encoder_config.to_dict() , text_config=decoder_config.to_dict() , is_vqa=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = PixaStructForConditionalGeneration(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = rename_and_convert_flax_params(UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("ybelkada/test-pix2struct-tokenizer" ) SCREAMING_SNAKE_CASE = PixaStructImageProcessor() SCREAMING_SNAKE_CASE = PixaStructProcessor(image_processor=UpperCAmelCase__ , tokenizer=UpperCAmelCase__ ) if use_large: SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = True # mkdir if needed os.makedirs(UpperCAmelCase__ , exist_ok=UpperCAmelCase__ ) model.save_pretrained(UpperCAmelCase__ ) processor.save_pretrained(UpperCAmelCase__ ) print("Model saved in {}".format(UpperCAmelCase__ ) ) if __name__ == "__main__": _lowerCamelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument('''--t5x_checkpoint_path''', default=None, type=str, help='''Path to the original T5x checkpoint.''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--use_large''', action='''store_true''', help='''Use large model.''') parser.add_argument('''--is_vqa''', action='''store_true''', help='''Use large model.''') _lowerCamelCase : int = parser.parse_args() convert_pixastruct_original_pytorch_checkpoint_to_hf( args.tax_checkpoint_path, args.pytorch_dump_folder_path, args.use_large )
647
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
1
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 _lowerCamelCase : List[str] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[str] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[Any]=False ): 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: SCREAMING_SNAKE_CASE = os.path.abspath(UpperCAmelCase__ ) logger.info(F"Loading PyTorch weights from {pt_path}" ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , map_location="cpu" ) logger.info(F"PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters." ) SCREAMING_SNAKE_CASE = convert_pytorch_state_dict_to_flax(UpperCAmelCase__ , UpperCAmelCase__ ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files SCREAMING_SNAKE_CASE = convert_pytorch_sharded_state_dict_to_flax(UpperCAmelCase__ , UpperCAmelCase__ ) return flax_state_dict def __lowerCamelCase (UpperCAmelCase__ : Tuple[str] , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Dict[str, jnp.ndarray] , UpperCAmelCase__ : str , ): def is_key_or_prefix_key_in_dict(UpperCAmelCase__ : Tuple[str] ) -> bool: return len(set(UpperCAmelCase__ ) & {key, (model_prefix,) + key} ) > 0 # layer norm SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = 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__ ): SCREAMING_SNAKE_CASE = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer SCREAMING_SNAKE_CASE = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight SCREAMING_SNAKE_CASE = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): SCREAMING_SNAKE_CASE = pt_tuple_key[-2] + "_g" elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): SCREAMING_SNAKE_CASE = pt_tuple_key[-2] + "_v" if name is not None: SCREAMING_SNAKE_CASE = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Any ): # convert pytorch tensor to numpy SCREAMING_SNAKE_CASE = {k: v.numpy() for k, v in pt_state_dict.items()} SCREAMING_SNAKE_CASE = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: SCREAMING_SNAKE_CASE = flax_model.params["params"] else: SCREAMING_SNAKE_CASE = flax_model.params SCREAMING_SNAKE_CASE = flatten_dict(UpperCAmelCase__ ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: SCREAMING_SNAKE_CASE = flatten_dict(flax_model.params["batch_stats"] ) random_flax_state_dict.update(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = (model_prefix not in flax_model_params) and ( model_prefix in {k.split("." )[0] for k in pt_state_dict.keys()} ) SCREAMING_SNAKE_CASE = (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(): SCREAMING_SNAKE_CASE = tuple(pt_key.split("." ) ) # remove base model prefix if necessary SCREAMING_SNAKE_CASE = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: SCREAMING_SNAKE_CASE = pt_tuple_key[1:] # Correctly rename weight parameters SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = rename_key_and_reshape_tensor( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) # add model prefix if necessary SCREAMING_SNAKE_CASE = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: SCREAMING_SNAKE_CASE = (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]: SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = jnp.asarray(UpperCAmelCase__ ) else: # also add unexpected weight so that warning is thrown SCREAMING_SNAKE_CASE = jnp.asarray(UpperCAmelCase__ ) return unflatten_dict(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Optional[Any] ): import torch # Load the index SCREAMING_SNAKE_CASE = {} for shard_file in shard_filenames: # load using msgpack utils SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = {k: v.numpy() for k, v in pt_state_dict.items()} SCREAMING_SNAKE_CASE = 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: SCREAMING_SNAKE_CASE = flax_model.params["params"] SCREAMING_SNAKE_CASE = flatten_dict(UpperCAmelCase__ ) random_flax_state_dict.update(flatten_dict(flax_model.params["batch_stats"] ) ) else: SCREAMING_SNAKE_CASE = flax_model.params SCREAMING_SNAKE_CASE = flatten_dict(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = (model_prefix not in flax_model_params) and ( model_prefix in {k.split("." )[0] for k in pt_state_dict.keys()} ) SCREAMING_SNAKE_CASE = (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(): SCREAMING_SNAKE_CASE = tuple(pt_key.split("." ) ) # remove base model prefix if necessary SCREAMING_SNAKE_CASE = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: SCREAMING_SNAKE_CASE = pt_tuple_key[1:] # Correctly rename weight parameters SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = rename_key_and_reshape_tensor( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) # add model prefix if necessary SCREAMING_SNAKE_CASE = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: SCREAMING_SNAKE_CASE = (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]: SCREAMING_SNAKE_CASE = jnp.asarray(UpperCAmelCase__ ) continue if "var" in flax_key[-1]: SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = jnp.asarray(UpperCAmelCase__ ) else: # also add unexpected weight so that warning is thrown SCREAMING_SNAKE_CASE = jnp.asarray(UpperCAmelCase__ ) return unflatten_dict(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = os.path.abspath(UpperCAmelCase__ ) logger.info(F"Loading Flax weights from {flax_checkpoint_path}" ) # import correct flax class SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase__ , "Flax" + model.__class__.__name__ ) # load flax weight dict with open(UpperCAmelCase__ , "rb" ) as state_f: try: SCREAMING_SNAKE_CASE = 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 __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Dict ): 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 SCREAMING_SNAKE_CASE = 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." ) SCREAMING_SNAKE_CASE = jax.tree_util.tree_map( lambda UpperCAmelCase__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = flatten_dict(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = pt_model.state_dict() SCREAMING_SNAKE_CASE = (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()} ) SCREAMING_SNAKE_CASE = (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 SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): SCREAMING_SNAKE_CASE = flax_key_tuple[0] == pt_model.base_model_prefix SCREAMING_SNAKE_CASE = ".".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: SCREAMING_SNAKE_CASE = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: SCREAMING_SNAKE_CASE = (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 SCREAMING_SNAKE_CASE = flax_key_tuple[:-1] + ("weight",) SCREAMING_SNAKE_CASE = jnp.transpose(UpperCAmelCase__ , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(UpperCAmelCase__ ) not in pt_model_dict: # linear layer SCREAMING_SNAKE_CASE = flax_key_tuple[:-1] + ("weight",) SCREAMING_SNAKE_CASE = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: SCREAMING_SNAKE_CASE = flax_key_tuple[:-1] + ("weight",) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: SCREAMING_SNAKE_CASE = flax_key_tuple[:-1] + ("running_mean",) elif "var" in flax_key_tuple[-1]: SCREAMING_SNAKE_CASE = flax_key_tuple[:-1] + ("running_var",) if "batch_stats" in flax_state: SCREAMING_SNAKE_CASE = ".".join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: SCREAMING_SNAKE_CASE = ".".join(UpperCAmelCase__ ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. SCREAMING_SNAKE_CASE = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: SCREAMING_SNAKE_CASE = key.split("." ) SCREAMING_SNAKE_CASE = None if key_components[-3::2] == ["parametrizations", "original0"]: SCREAMING_SNAKE_CASE = key_components[-2] + "_g" elif key_components[-3::2] == ["parametrizations", "original1"]: SCREAMING_SNAKE_CASE = key_components[-2] + "_v" if name is not None: SCREAMING_SNAKE_CASE = key_components[:-3] + [name] SCREAMING_SNAKE_CASE = ".".join(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = key if flax_key in special_pt_names: SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = np.asarray(UpperCAmelCase__ ) if not isinstance(UpperCAmelCase__ , np.ndarray ) else flax_tensor SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = 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
647
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
1
# Algorithm for the pigeonhole sorting def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ ) # min() finds the minimum value SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ ) # max() finds the maximum value SCREAMING_SNAKE_CASE = max_val - min_val + 1 # size is difference of max and min values plus one # list of pigeonholes of size equal to the variable size SCREAMING_SNAKE_CASE = [0] * size # Populate the pigeonholes. for x in a: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), "integers only please" holes[x - min_val] += 1 # Putting the elements back into the array in an order. SCREAMING_SNAKE_CASE = 0 for count in range(UpperCAmelCase__ ): while holes[count] > 0: holes[count] -= 1 SCREAMING_SNAKE_CASE = count + min_val i += 1 def __lowerCamelCase (): SCREAMING_SNAKE_CASE = [8, 3, 2, 7, 4, 6, 8] pigeonhole_sort(UpperCAmelCase__ ) print("Sorted order is:" , " ".join(UpperCAmelCase__ ) ) if __name__ == "__main__": main()
647
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
1
def __lowerCamelCase (UpperCAmelCase__ : list ): if len(UpperCAmelCase__ ) <= 1: return lst SCREAMING_SNAKE_CASE = 1 while i < len(UpperCAmelCase__ ): if lst[i - 1] <= lst[i]: i += 1 else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = lst[i], lst[i - 1] i -= 1 if i == 0: SCREAMING_SNAKE_CASE = 1 return lst if __name__ == "__main__": _lowerCamelCase : Tuple = input('''Enter numbers separated by a comma:\n''').strip() _lowerCamelCase : Tuple = [int(item) for item in user_input.split(''',''')] print(gnome_sort(unsorted))
647
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(F"The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
1
import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any]=() , UpperCAmelCase__ : Optional[Any]=None , UpperCAmelCase__ : Any="no" , UpperCAmelCase__ : Optional[Any]="29500" ): SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False if any(key.startswith("KAGGLE" ) for key in os.environ.keys() ): SCREAMING_SNAKE_CASE = True elif "IPython" in sys.modules: SCREAMING_SNAKE_CASE = "google.colab" in str(sys.modules["IPython"].get_ipython() ) try: SCREAMING_SNAKE_CASE = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( F"Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}." ) if (in_colab or in_kaggle) and (os.environ.get("TPU_NAME" , UpperCAmelCase__ ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( "To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside " "your training function. Restart your notebook and make sure no cells initializes an " "`Accelerator`." ) if num_processes is None: SCREAMING_SNAKE_CASE = 8 SCREAMING_SNAKE_CASE = PrepareForLaunch(UpperCAmelCase__ , distributed_type="TPU" ) print(F"Launching a training on {num_processes} TPU cores." ) xmp.spawn(UpperCAmelCase__ , args=UpperCAmelCase__ , nprocs=UpperCAmelCase__ , start_method="fork" ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print("Launching training on one GPU." ) else: print("Launching training on one CPU." ) function(*UpperCAmelCase__ ) else: if num_processes is None: raise ValueError( "You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call." ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( "To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized " "inside your training function. Restart your notebook and make sure no cells initializes an " "`Accelerator`." ) if torch.cuda.is_initialized(): raise ValueError( "To launch a multi-GPU training from your notebook, you need to avoid running any instruction " "using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA " "function." ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=UpperCAmelCase__ , master_addr="127.0.01" , master_port=UpperCAmelCase__ , mixed_precision=UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = PrepareForLaunch(UpperCAmelCase__ , distributed_type="MULTI_GPU" ) print(F"Launching training on {num_processes} GPUs." ) try: start_processes(UpperCAmelCase__ , args=UpperCAmelCase__ , nprocs=UpperCAmelCase__ , start_method="fork" ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( "CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. " "This likely stems from an outside import causing issues once the `notebook_launcher()` is called. " "Please review your imports and test them when running the `notebook_launcher()` to identify " "which one is problematic." ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): SCREAMING_SNAKE_CASE = "1" print("Launching training on MPS." ) elif torch.cuda.is_available(): print("Launching training on one GPU." ) else: print("Launching training on CPU." ) function(*UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Union[str, Any]=() , UpperCAmelCase__ : int=2 ): from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=UpperCAmelCase__ , master_addr="127.0.01" , master_port="29500" , accelerate_mixed_precision="no" , accelerate_debug_rdv_file=tmp_file.name , accelerate_use_cpu="yes" , ): SCREAMING_SNAKE_CASE = PrepareForLaunch(UpperCAmelCase__ , debug=UpperCAmelCase__ ) start_processes(UpperCAmelCase__ , args=UpperCAmelCase__ , nprocs=UpperCAmelCase__ , start_method="fork" )
647
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow 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 DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
1
import jax.numpy as jnp from ...utils import logging from ..ta.modeling_flax_ta import FlaxTaEncoderModel, FlaxTaForConditionalGeneration, FlaxTaModel from .configuration_mta import MTaConfig _lowerCamelCase : Tuple = logging.get_logger(__name__) _lowerCamelCase : Tuple = '''T5Config''' def __lowerCamelCase (UpperCAmelCase__ : jnp.array , UpperCAmelCase__ : int , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = jnp.zeros_like(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = shifted_input_ids.at[:, 1:].set(input_ids[:, :-1] ) SCREAMING_SNAKE_CASE = shifted_input_ids.at[:, 0].set(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = jnp.where(shifted_input_ids == -1_0_0 , UpperCAmelCase__ , UpperCAmelCase__ ) return shifted_input_ids class lowercase ( a ): lowercase__ : int = """mt5""" lowercase__ : Dict = MTaConfig class lowercase ( a ): lowercase__ : str = """mt5""" lowercase__ : List[str] = MTaConfig class lowercase ( a ): lowercase__ : Optional[int] = """mt5""" lowercase__ : Any = MTaConfig
647
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
1
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( "split_dict" , [ SplitDict(), SplitDict({"train": SplitInfo(name="train" , num_bytes=1_3_3_7 , num_examples=4_2 , dataset_name="my_dataset" )} ), SplitDict({"train": SplitInfo(name="train" , num_bytes=1_3_3_7 , num_examples=4_2 )} ), SplitDict({"train": SplitInfo()} ), ] , ) def __lowerCamelCase (UpperCAmelCase__ : SplitDict ): SCREAMING_SNAKE_CASE = split_dict._to_yaml_list() assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = SplitDict._from_yaml_list(UpperCAmelCase__ ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump SCREAMING_SNAKE_CASE = None # the split name of split_dict takes over the name of the split info object SCREAMING_SNAKE_CASE = split_name assert split_dict == reloaded @pytest.mark.parametrize( "split_info" , [SplitInfo(), SplitInfo(dataset_name=UpperCAmelCase__ ), SplitInfo(dataset_name="my_dataset" )] ) def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] ): # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files SCREAMING_SNAKE_CASE = asdict(SplitDict({"train": split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
647
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # Validation if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(UpperCAmelCase__ ) != 3 or not all(isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(UpperCAmelCase__ ) == 0: return 0 if min(UpperCAmelCase__ ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(UpperCAmelCase__ ) >= 3_6_6: raise ValueError("All days elements should be less than 366" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> int: if index > 3_6_5: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 3_0 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
647
1
import unittest import numpy as np import requests from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: _lowerCamelCase : List[str] = False if is_vision_available(): from PIL import Image from transformers import PixaStructImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Tuple , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Tuple=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : Union[str, Any]=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Tuple=400 , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Tuple=None , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 20, "width": 20} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = do_convert_rgb SCREAMING_SNAKE_CASE = [512, 1_024, 2_048, 4_096] SCREAMING_SNAKE_CASE = patch_size if patch_size is not None else {"height": 16, "width": 16} def __snake_case( self : List[Any] ) -> List[Any]: '''simple docstring''' return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb} def __snake_case( self : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(_UpperCamelCase , stream=_UpperCamelCase ).raw ).convert("RGB" ) return raw_image @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`.""" , ) @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : str = PixaStructImageProcessor if is_vision_available() else None def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = PixaStructImageProcessingTester(self ) @property def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_convert_rgb" ) ) def __snake_case( self : Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processor_tester.prepare_dummy_image() SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) SCREAMING_SNAKE_CASE = 2_048 SCREAMING_SNAKE_CASE = image_processor(_UpperCamelCase , return_tensors="pt" , max_patches=_UpperCamelCase ) self.assertTrue(torch.allclose(inputs.flattened_patches.mean() , torch.tensor(0.0_6_0_6 ) , atol=1e-3 , rtol=1e-3 ) ) def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input SCREAMING_SNAKE_CASE = image_processor( image_inputs[0] , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE = image_processor( _UpperCamelCase , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __snake_case( self : int ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * self.image_processor_tester.num_channels ) + 2 SCREAMING_SNAKE_CASE = True for max_patch in self.image_processor_tester.max_patches: # Test not batched input with self.assertRaises(_UpperCamelCase ): SCREAMING_SNAKE_CASE = image_processor( image_inputs[0] , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches SCREAMING_SNAKE_CASE = "Hello" SCREAMING_SNAKE_CASE = image_processor( image_inputs[0] , return_tensors="pt" , max_patches=_UpperCamelCase , header_text=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE = image_processor( _UpperCamelCase , return_tensors="pt" , max_patches=_UpperCamelCase , header_text=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) SCREAMING_SNAKE_CASE = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input SCREAMING_SNAKE_CASE = image_processor( image_inputs[0] , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE = image_processor( _UpperCamelCase , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def __snake_case( self : Optional[int] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input SCREAMING_SNAKE_CASE = image_processor( image_inputs[0] , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE = image_processor( _UpperCamelCase , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`.""" , ) @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[Any] = PixaStructImageProcessor if is_vision_available() else None def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = PixaStructImageProcessingTester(self , num_channels=4 ) SCREAMING_SNAKE_CASE = 3 @property def __snake_case( self : str ) -> int: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_convert_rgb" ) ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = ( (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"]) * (self.image_processor_tester.num_channels - 1) ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input SCREAMING_SNAKE_CASE = image_processor( image_inputs[0] , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched SCREAMING_SNAKE_CASE = image_processor( _UpperCamelCase , return_tensors="pt" , max_patches=_UpperCamelCase ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , )
647
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : int ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
1
import re def __lowerCamelCase (UpperCAmelCase__ : str ): return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]" , str_ )] def __lowerCamelCase (UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = split_input(str_ ) return "".join( ["".join([char.capitalize() for char in sub_str] ) for sub_str in string_split] ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : bool , UpperCAmelCase__ : str ): try: SCREAMING_SNAKE_CASE = split_input(UpperCAmelCase__ ) if upper: SCREAMING_SNAKE_CASE = "".join( [ separator.join([char.upper() for char in sub_str] ) for sub_str in string_split ] ) else: SCREAMING_SNAKE_CASE = "".join( [ separator.join([char.lower() for char in sub_str] ) for sub_str in string_split ] ) return res_str except IndexError: return "not valid string" def __lowerCamelCase (UpperCAmelCase__ : str ): return to_simple_case(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : str ): try: SCREAMING_SNAKE_CASE = to_simple_case(UpperCAmelCase__ ) return res_str[0].lower() + res_str[1:] except IndexError: return "not valid string" def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : bool ): return to_complex_case(UpperCAmelCase__ , UpperCAmelCase__ , "_" ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : bool ): return to_complex_case(UpperCAmelCase__ , UpperCAmelCase__ , "-" ) if __name__ == "__main__": __import__('''doctest''').testmod()
647
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
1
# Function to print upper half of diamond (pyramid) def __lowerCamelCase (UpperCAmelCase__ : List[str] ): for i in range(0 , UpperCAmelCase__ ): for _ in range(0 , n - i - 1 ): # printing spaces print(" " , end="" ) for _ in range(0 , i + 1 ): # printing stars print("* " , end="" ) print() def __lowerCamelCase (UpperCAmelCase__ : Optional[int] ): for i in range(UpperCAmelCase__ , 0 , -1 ): for _ in range(UpperCAmelCase__ , 0 , -1 ): # printing stars print("* " , end="" ) print() for _ in range(n - i + 1 , 0 , -1 ): # printing spaces print(" " , end="" ) def __lowerCamelCase (UpperCAmelCase__ : int ): if n <= 0: print(" ... .... nothing printing :(" ) return floyd(UpperCAmelCase__ ) # upper half reverse_floyd(UpperCAmelCase__ ) # lower half if __name__ == "__main__": print(r'''| /\ | |- | |- |--| |\ /| |-''') print(r'''|/ \| |- |_ |_ |__| | \/ | |_''') _lowerCamelCase : Any = 1 while K: _lowerCamelCase : Optional[int] = int(input('''enter the number and , and see the magic : ''')) print() pretty_print(user_number) _lowerCamelCase : List[Any] = int(input('''press 0 to exit... and 1 to continue...''')) print('''Good Bye...''')
647
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(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"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(UpperCAmelCase__ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
1
import tempfile import unittest from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from transformers.testing_utils import ( is_torch_available, require_optimum, require_torch, slow, ) if is_torch_available(): import torch @require_torch @require_optimum @slow class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = "hf-internal-testing/tiny-random-t5" SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(_UpperCamelCase ) SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained(_UpperCamelCase ) SCREAMING_SNAKE_CASE = tokenizer("This is me" , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model.to_bettertransformer() self.assertTrue(any("BetterTransformer" in mod.__class__.__name__ for _, mod in model.named_modules() ) ) SCREAMING_SNAKE_CASE = model.generate(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = model.reverse_bettertransformer() self.assertFalse(any("BetterTransformer" in mod.__class__.__name__ for _, mod in model.named_modules() ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_UpperCamelCase ) SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained(_UpperCamelCase ) self.assertFalse( any("BetterTransformer" in mod.__class__.__name__ for _, mod in model_reloaded.named_modules() ) ) SCREAMING_SNAKE_CASE = model_reloaded.generate(**_UpperCamelCase ) self.assertTrue(torch.allclose(_UpperCamelCase , _UpperCamelCase ) ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = "hf-internal-testing/tiny-random-t5" SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained(_UpperCamelCase ) SCREAMING_SNAKE_CASE = model.to_bettertransformer() with tempfile.TemporaryDirectory() as tmpdirname: with self.assertRaises(_UpperCamelCase ): model.save_pretrained(_UpperCamelCase ) SCREAMING_SNAKE_CASE = model.reverse_bettertransformer() model.save_pretrained(_UpperCamelCase )
647
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __snake_case( self : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
1
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, AutoConfig, AutoFeatureExtractor, WavaVecaConfig, WavaVecaFeatureExtractor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 _lowerCamelCase : Union[str, Any] = get_tests_dir('''fixtures''') _lowerCamelCase : Dict = get_tests_dir('''fixtures/dummy_feature_extractor_config.json''') _lowerCamelCase : int = get_tests_dir('''fixtures/dummy-config.json''') class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = 0 def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h" ) self.assertIsInstance(A__ , A__ ) def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(A__ ) self.assertIsInstance(A__ , A__ ) def __snake_case( self : List[str] ) -> List[str]: '''simple docstring''' with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(A__ ).to_dict() config_dict.pop("feature_extractor_type" ) SCREAMING_SNAKE_CASE = WavaVecaFeatureExtractor(**A__ ) # save in new folder model_config.save_pretrained(A__ ) config.save_pretrained(A__ ) SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(A__ ) # make sure private variable is not incorrectly saved SCREAMING_SNAKE_CASE = json.loads(config.to_json_string() ) self.assertTrue("_processor_class" not in dict_as_saved ) self.assertIsInstance(A__ , A__ ) def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(A__ ) self.assertIsInstance(A__ , A__ ) def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' with self.assertRaisesRegex( A__ , "bert-base is not a local folder and is not a valid model identifier" ): SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained("bert-base" ) def __snake_case( self : Tuple ) -> Union[str, Any]: '''simple docstring''' with self.assertRaisesRegex( A__ , R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(A__ , revision="aaaaaa" ) def __snake_case( self : List[Any] ) -> int: '''simple docstring''' with self.assertRaisesRegex( A__ , "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json." , ): SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model" ) def __snake_case( self : Any ) -> int: '''simple docstring''' with self.assertRaises(A__ ): SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained( "hf-internal-testing/test_dynamic_feature_extractor" ) # If remote code is disabled, we can't load this config. with self.assertRaises(A__ ): SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained( "hf-internal-testing/test_dynamic_feature_extractor" , trust_remote_code=A__ ) SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained( "hf-internal-testing/test_dynamic_feature_extractor" , trust_remote_code=A__ ) self.assertEqual(feature_extractor.__class__.__name__ , "NewFeatureExtractor" ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(A__ ) SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(A__ , trust_remote_code=A__ ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , "NewFeatureExtractor" ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' try: AutoConfig.register("custom" , A__ ) AutoFeatureExtractor.register(A__ , A__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(A__ ): AutoFeatureExtractor.register(A__ , A__ ) # Now that the config is registered, it can be used as any other config with the auto-API SCREAMING_SNAKE_CASE = CustomFeatureExtractor.from_pretrained(A__ ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(A__ ) SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(A__ ) self.assertIsInstance(A__ , A__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] def __snake_case( self : List[str] ) -> str: '''simple docstring''' class lowercase ( _lowerCamelCase ): lowercase__ : int = True try: AutoConfig.register("custom" , A__ ) AutoFeatureExtractor.register(A__ , A__ ) # If remote code is not set, the default is to use local SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained( "hf-internal-testing/test_dynamic_feature_extractor" ) self.assertEqual(feature_extractor.__class__.__name__ , "NewFeatureExtractor" ) self.assertTrue(feature_extractor.is_local ) # If remote code is disabled, we load the local one. SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained( "hf-internal-testing/test_dynamic_feature_extractor" , trust_remote_code=A__ ) self.assertEqual(feature_extractor.__class__.__name__ , "NewFeatureExtractor" ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained( "hf-internal-testing/test_dynamic_feature_extractor" , trust_remote_code=A__ ) self.assertEqual(feature_extractor.__class__.__name__ , "NewFeatureExtractor" ) self.assertTrue(not hasattr(A__ , "is_local" ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
700
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
0
# # This a `torch.distributed` diagnostics script that checks that all GPUs in the cluster (one or # many nodes) can talk to each other via nccl and allocate gpu memory. # # To run first adjust the number of processes and nodes: # # python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # You may need to add --master_addr $MASTER_ADDR --master_port $MASTER_PORT if using a custom addr:port # # You can also use the rdzv API: --rdzv_endpoint $MASTER_ADDR:$MASTER_PORT --rdzv_backend c10d # # use torch.distributed.launch instead of torch.distributed.run for torch < 1.9 # # If you get a hanging in `barrier` calls you have some network issues, you may try to debug this with: # # NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # which should tell you what's going on behind the scenes. # # # This script can be run via `srun` in the SLURM environment as well. Here is a SLURM script that # runs on 2 nodes of 4 gpus per node: # # #SBATCH --job-name=test-nodes # name # #SBATCH --nodes=2 # nodes # #SBATCH --ntasks-per-node=1 # crucial - only 1 task per dist per node! # #SBATCH --cpus-per-task=10 # number of cores per tasks # #SBATCH --gres=gpu:4 # number of gpus # #SBATCH --time 0:05:00 # maximum execution time (HH:MM:SS) # #SBATCH --output=%x-%j.out # output file name # # GPUS_PER_NODE=4 # MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) # MASTER_PORT=6000 # # srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ # --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ # --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ # torch-distributed-gpu-test.py' # import fcntl import os import socket import torch import torch.distributed as dist def __lowerCamelCase (*UpperCAmelCase__ : Union[str, Any] ): with open(__A , "r" ) as fh: fcntl.flock(__A , fcntl.LOCK_EX ) try: print(*__A ) finally: fcntl.flock(__A , fcntl.LOCK_UN ) _lowerCamelCase : Optional[Any] = int(os.environ['''LOCAL_RANK''']) torch.cuda.set_device(local_rank) _lowerCamelCase : Any = torch.device('''cuda''', local_rank) _lowerCamelCase : Tuple = socket.gethostname() _lowerCamelCase : Dict = f"""[{hostname}-{local_rank}]""" try: # test distributed dist.init_process_group('''nccl''') dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM) dist.barrier() # test cuda is available and can allocate memory torch.cuda.is_available() torch.ones(1).cuda(local_rank) # global rank _lowerCamelCase : List[str] = dist.get_rank() _lowerCamelCase : Dict = dist.get_world_size() printflock(f"""{gpu} is OK (global rank: {rank}/{world_size})""") dist.barrier() if rank == 0: printflock(f"""pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}""") except Exception: printflock(f"""{gpu} is broken""") raise
701
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the `sampling_rate` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"Only mono-channel audio is supported for input to {self}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
0
import copy import importlib.metadata import json import os from dataclasses import dataclass from typing import Any, Dict, Union from packaging import version from ..utils import is_torch_available, logging if is_torch_available(): import torch _lowerCamelCase : Tuple = logging.get_logger(__name__) @dataclass class lowercase : def __init__( self : Optional[int] , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : Tuple=False , _UpperCamelCase : List[str]=6.0 , _UpperCamelCase : List[str]=None , _UpperCamelCase : int=False , _UpperCamelCase : List[str]=False , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any="fp4" , _UpperCamelCase : Union[str, Any]=False , **_UpperCamelCase : Union[str, Any] , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = load_in_abit SCREAMING_SNAKE_CASE = load_in_abit SCREAMING_SNAKE_CASE = llm_inta_threshold SCREAMING_SNAKE_CASE = llm_inta_skip_modules SCREAMING_SNAKE_CASE = llm_inta_enable_fpaa_cpu_offload SCREAMING_SNAKE_CASE = llm_inta_has_fpaa_weight SCREAMING_SNAKE_CASE = bnb_abit_quant_type SCREAMING_SNAKE_CASE = bnb_abit_use_double_quant if bnb_abit_compute_dtype is None: SCREAMING_SNAKE_CASE = torch.floataa elif isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): SCREAMING_SNAKE_CASE = getattr(UpperCAmelCase_ , UpperCAmelCase_ ) elif isinstance(UpperCAmelCase_ , torch.dtype ): SCREAMING_SNAKE_CASE = bnb_abit_compute_dtype else: raise ValueError("bnb_4bit_compute_dtype must be a string or a torch.dtype" ) self.post_init() def __snake_case( self : Tuple ) -> List[str]: '''simple docstring''' if not isinstance(self.llm_inta_threshold , UpperCAmelCase_ ): raise ValueError("llm_int8_threshold must be a float" ) if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules , UpperCAmelCase_ ): raise ValueError("llm_int8_skip_modules must be a list of strings" ) if not isinstance(self.llm_inta_enable_fpaa_cpu_offload , UpperCAmelCase_ ): raise ValueError("llm_int8_enable_fp32_cpu_offload must be a boolean" ) if not isinstance(self.llm_inta_has_fpaa_weight , UpperCAmelCase_ ): raise ValueError("llm_int8_has_fp16_weight must be a boolean" ) if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype , torch.dtype ): raise ValueError("bnb_4bit_compute_dtype must be torch.dtype" ) if not isinstance(self.bnb_abit_quant_type , UpperCAmelCase_ ): raise ValueError("bnb_4bit_quant_type must be a string" ) if not isinstance(self.bnb_abit_use_double_quant , UpperCAmelCase_ ): raise ValueError("bnb_4bit_use_double_quant must be a boolean" ) if self.load_in_abit and not version.parse(importlib.metadata.version("bitsandbytes" ) ) >= version.parse( "0.39.0" ): raise ValueError( "4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version" ) def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.load_in_abit or self.load_in_abit def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' if self.load_in_abit: return "llm_int8" elif self.load_in_abit and self.bnb_abit_quant_type == "fp4": return "fp4" elif self.load_in_abit and self.bnb_abit_quant_type == "nf4": return "nf4" else: return None @classmethod def __snake_case( cls : Any , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[int] , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = cls(**UpperCAmelCase_ ) SCREAMING_SNAKE_CASE = [] for key, value in kwargs.items(): if hasattr(UpperCAmelCase_ , UpperCAmelCase_ ): setattr(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) to_remove.append(UpperCAmelCase_ ) for key in to_remove: kwargs.pop(UpperCAmelCase_ , UpperCAmelCase_ ) if return_unused_kwargs: return config, kwargs else: return config def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[str, os.PathLike] ) -> Any: '''simple docstring''' with open(UpperCAmelCase_ , "w" , encoding="utf-8" ) as writer: SCREAMING_SNAKE_CASE = self.to_dict() SCREAMING_SNAKE_CASE = json.dumps(UpperCAmelCase_ , indent=2 , sort_keys=UpperCAmelCase_ ) + "\n" writer.write(UpperCAmelCase_ ) def __snake_case( self : Any ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = str(output["bnb_4bit_compute_dtype"] ).split("." )[1] return output def __repr__( self : Optional[Any] ) -> int: '''simple docstring''' return F"{self.__class__.__name__} {self.to_json_string()}" def __snake_case( self : List[str] , _UpperCamelCase : bool = True ) -> str: '''simple docstring''' if use_diff is True: SCREAMING_SNAKE_CASE = self.to_diff_dict() else: SCREAMING_SNAKE_CASE = self.to_dict() return json.dumps(UpperCAmelCase_ , indent=2 , sort_keys=UpperCAmelCase_ ) + "\n" def __snake_case( self : Any ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.to_dict() # get the default config dict SCREAMING_SNAKE_CASE = BitsAndBytesConfig().to_dict() SCREAMING_SNAKE_CASE = {} # only serialize values that differ from the default config for key, value in config_dict.items(): if value != default_config_dict[key]: SCREAMING_SNAKE_CASE = value return serializable_config_dict
702
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
0
def __lowerCamelCase (UpperCAmelCase__ : Optional[int] = 4_0_0_0_0_0_0 ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = b, a + b return sum(UpperCAmelCase__ ) if __name__ == "__main__": print(f"""{solution() = }""")
703
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
0
import qiskit def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = qiskit.Aer.get_backend("aer_simulator" ) SCREAMING_SNAKE_CASE = qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator SCREAMING_SNAKE_CASE = qiskit.execute(_lowercase , _lowercase , shots=1_0_0_0 ) # Return the histogram data of the results of the experiment return job.result().get_counts(_lowercase ) if __name__ == "__main__": _lowerCamelCase : Any = half_adder(1, 1) print(f"""Half Adder Output Qubit Counts: {counts}""")
704
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
0
from collections import defaultdict from math import ceil, sqrt def __lowerCamelCase (UpperCAmelCase__ : int = 1_0_0_0_0_0_0 , UpperCAmelCase__ : int = 1_0 ): SCREAMING_SNAKE_CASE = defaultdict(_A ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: SCREAMING_SNAKE_CASE = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: SCREAMING_SNAKE_CASE = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(_A , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 1_0 ) if __name__ == "__main__": print(f"""{solution() = }""")
705
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
0
import operator def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Dict = False , UpperCAmelCase__ : Optional[int] = None ): SCREAMING_SNAKE_CASE = operator.lt if reverse else operator.gt SCREAMING_SNAKE_CASE = solution or [] if not arr: return solution SCREAMING_SNAKE_CASE = [arr.pop(0 )] for i, item in enumerate(lowerCamelCase_ ): if _operator(lowerCamelCase_ , sublist[-1] ): sublist.append(lowerCamelCase_ ) arr.pop(lowerCamelCase_ ) # merging sublist into solution list if not solution: solution.extend(lowerCamelCase_ ) else: while sublist: SCREAMING_SNAKE_CASE = sublist.pop(0 ) for i, xx in enumerate(lowerCamelCase_ ): if not _operator(lowerCamelCase_ , lowerCamelCase_ ): solution.insert(lowerCamelCase_ , lowerCamelCase_ ) break else: solution.append(lowerCamelCase_ ) strand_sort(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
706
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
0
def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : List[str]=False ): if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = len(set_a.intersection(UpperCAmelCase__ ) ) if alternative_union: SCREAMING_SNAKE_CASE = len(UpperCAmelCase__ ) + len(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = len(set_a.union(UpperCAmelCase__ ) ) return intersection / union if isinstance(UpperCAmelCase__ , (list, tuple) ) and isinstance(UpperCAmelCase__ , (list, tuple) ): SCREAMING_SNAKE_CASE = [element for element in set_a if element in set_b] if alternative_union: SCREAMING_SNAKE_CASE = len(UpperCAmelCase__ ) + len(UpperCAmelCase__ ) return len(UpperCAmelCase__ ) / union else: SCREAMING_SNAKE_CASE = set_a + [element for element in set_b if element not in set_a] return len(UpperCAmelCase__ ) / len(UpperCAmelCase__ ) return len(UpperCAmelCase__ ) / len(UpperCAmelCase__ ) return None if __name__ == "__main__": _lowerCamelCase : List[Any] = {"a", "b", "c", "d", "e"} _lowerCamelCase : Optional[Any] = {"c", "d", "e", "f", "h", "i"} print(jaccard_similarity(set_a, set_b))
707
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCamelCase : Union[str, Any] = { '''configuration_blip_2''': [ '''BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Blip2Config''', '''Blip2QFormerConfig''', '''Blip2VisionConfig''', ], '''processing_blip_2''': ['''Blip2Processor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = [ '''BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Blip2Model''', '''Blip2QFormerModel''', '''Blip2PreTrainedModel''', '''Blip2ForConditionalGeneration''', '''Blip2VisionModel''', ] if TYPE_CHECKING: from .configuration_blip_a import ( BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipaConfig, BlipaQFormerConfig, BlipaVisionConfig, ) from .processing_blip_a import BlipaProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip_a import ( BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST, BlipaForConditionalGeneration, BlipaModel, BlipaPreTrainedModel, BlipaQFormerModel, BlipaVisionModel, ) else: import sys _lowerCamelCase : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
708
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
0
import json import os import torch from diffusers import UNetaDModel os.makedirs('''hub/hopper-medium-v2/unet/hor32''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/unet/hor128''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/value_function''', exist_ok=True) def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] ): if hor == 1_2_8: SCREAMING_SNAKE_CASE = ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D") SCREAMING_SNAKE_CASE = (3_2, 1_2_8, 2_5_6) SCREAMING_SNAKE_CASE = ("UpResnetBlock1D", "UpResnetBlock1D") elif hor == 3_2: SCREAMING_SNAKE_CASE = ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D") SCREAMING_SNAKE_CASE = (3_2, 6_4, 1_2_8, 2_5_6) SCREAMING_SNAKE_CASE = ("UpResnetBlock1D", "UpResnetBlock1D", "UpResnetBlock1D") SCREAMING_SNAKE_CASE = torch.load(F"/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch" ) SCREAMING_SNAKE_CASE = model.state_dict() SCREAMING_SNAKE_CASE = { "down_block_types": down_block_types, "block_out_channels": block_out_channels, "up_block_types": up_block_types, "layers_per_block": 1, "use_timestep_embedding": True, "out_block_type": "OutConv1DBlock", "norm_num_groups": 8, "downsample_each_block": False, "in_channels": 1_4, "out_channels": 1_4, "extra_in_channels": 0, "time_embedding_type": "positional", "flip_sin_to_cos": False, "freq_shift": 1, "sample_size": 6_5_5_3_6, "mid_block_type": "MidResTemporalBlock1D", "act_fn": "mish", } SCREAMING_SNAKE_CASE = UNetaDModel(**__lowerCAmelCase ) print(F"length of state dict: {len(state_dict.keys() )}" ) print(F"length of value function dict: {len(hf_value_function.state_dict().keys() )}" ) SCREAMING_SNAKE_CASE = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): SCREAMING_SNAKE_CASE = state_dict.pop(__lowerCAmelCase ) hf_value_function.load_state_dict(__lowerCAmelCase ) torch.save(hf_value_function.state_dict() , F"hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin" ) with open(F"hub/hopper-medium-v2/unet/hor{hor}/config.json" , "w" ) as f: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = { "in_channels": 1_4, "down_block_types": ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D"), "up_block_types": (), "out_block_type": "ValueFunction", "mid_block_type": "ValueFunctionMidBlock1D", "block_out_channels": (3_2, 6_4, 1_2_8, 2_5_6), "layers_per_block": 1, "downsample_each_block": True, "sample_size": 6_5_5_3_6, "out_channels": 1_4, "extra_in_channels": 0, "time_embedding_type": "positional", "use_timestep_embedding": True, "flip_sin_to_cos": False, "freq_shift": 1, "norm_num_groups": 8, "act_fn": "mish", } SCREAMING_SNAKE_CASE = torch.load("/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch" ) SCREAMING_SNAKE_CASE = model SCREAMING_SNAKE_CASE = UNetaDModel(**__lowerCAmelCase ) print(F"length of state dict: {len(state_dict.keys() )}" ) print(F"length of value function dict: {len(hf_value_function.state_dict().keys() )}" ) SCREAMING_SNAKE_CASE = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): SCREAMING_SNAKE_CASE = state_dict.pop(__lowerCAmelCase ) hf_value_function.load_state_dict(__lowerCAmelCase ) torch.save(hf_value_function.state_dict() , "hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin" ) with open("hub/hopper-medium-v2/value_function/config.json" , "w" ) as f: json.dump(__lowerCAmelCase , __lowerCAmelCase ) if __name__ == "__main__": unet(32) # unet(128) value_function()
709
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
0
from ...utils import deprecate from ..controlnet.pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline # noqa: F401 deprecate( 'stable diffusion controlnet', '0.22.0', 'Importing `FlaxStableDiffusionControlNetPipeline` from diffusers.pipelines.stable_diffusion.flax_pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import FlaxStableDiffusionControlNetPipeline` instead.', standard_warn=False, stacklevel=3, )
710
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
0
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 SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): def __snake_case( self : List[Any] , _UpperCamelCase : int , _UpperCamelCase : int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = jnp.ones((batch_size, length) ) / length return scores def __snake_case( self : int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = 20 SCREAMING_SNAKE_CASE = self._get_uniform_logits(batch_size=2 , length=__A ) # tweak scores to not be uniform anymore SCREAMING_SNAKE_CASE = scores.at[1, 5].set((1 / length) + 0.1 ) # peak, 1st batch SCREAMING_SNAKE_CASE = scores.at[1, 10].set((1 / length) - 0.4 ) # valley, 1st batch # compute softmax SCREAMING_SNAKE_CASE = jax.nn.softmax(__A , axis=-1 ) SCREAMING_SNAKE_CASE = FlaxTemperatureLogitsWarper(temperature=0.5 ) SCREAMING_SNAKE_CASE = FlaxTemperatureLogitsWarper(temperature=1.3 ) SCREAMING_SNAKE_CASE = jax.nn.softmax(temp_dist_warper_sharper(__A , scores.copy() , cur_len=__A ) , axis=-1 ) SCREAMING_SNAKE_CASE = jax.nn.softmax(temp_dist_warper_smoother(__A , scores.copy() , cur_len=__A ) , 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 __snake_case( self : List[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = 10 SCREAMING_SNAKE_CASE = 2 # create ramp distribution SCREAMING_SNAKE_CASE = np.broadcast_to(np.arange(__A )[None, :] , (batch_size, vocab_size) ).copy() SCREAMING_SNAKE_CASE = ramp_logits[1:, : vocab_size // 2] + vocab_size SCREAMING_SNAKE_CASE = FlaxTopKLogitsWarper(3 ) SCREAMING_SNAKE_CASE = top_k_warp(__A , __A , cur_len=__A ) # 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 SCREAMING_SNAKE_CASE = 5 SCREAMING_SNAKE_CASE = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3 ) SCREAMING_SNAKE_CASE = np.broadcast_to(np.arange(__A )[None, :] , (batch_size, length) ).copy() SCREAMING_SNAKE_CASE = top_k_warp_safety_check(__A , __A , cur_len=__A ) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1 ).tolist() , [2, 2] ) def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = 10 SCREAMING_SNAKE_CASE = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) SCREAMING_SNAKE_CASE = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.1_5, 0.3, 0.3, 0.2_5]] ) ) SCREAMING_SNAKE_CASE = FlaxTopPLogitsWarper(0.8 ) SCREAMING_SNAKE_CASE = np.exp(top_p_warp(__A , __A , cur_len=__A ) ) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 SCREAMING_SNAKE_CASE = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.2_5]] ) self.assertTrue(np.allclose(__A , __A , atol=1e-3 ) ) # check edge cases with negative and extreme logits SCREAMING_SNAKE_CASE = np.broadcast_to(np.arange(__A )[None, :] , (batch_size, vocab_size) ).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme SCREAMING_SNAKE_CASE = ramp_logits[1] * 1_0_0.0 # make sure at least 2 tokens are kept SCREAMING_SNAKE_CASE = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0 ) SCREAMING_SNAKE_CASE = top_p_warp(__A , __A , cur_len=__A ) # 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 __snake_case( self : Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = 20 SCREAMING_SNAKE_CASE = 4 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=__A ) # check that min length is applied at length 5 SCREAMING_SNAKE_CASE = ids_tensor((batch_size, 20) , vocab_size=20 ) SCREAMING_SNAKE_CASE = 5 SCREAMING_SNAKE_CASE = self._get_uniform_logits(__A , __A ) SCREAMING_SNAKE_CASE = min_dist_processor(__A , __A , cur_len=__A ) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("inf" )] ) # check that min length is not applied anymore at length 15 SCREAMING_SNAKE_CASE = self._get_uniform_logits(__A , __A ) SCREAMING_SNAKE_CASE = 15 SCREAMING_SNAKE_CASE = min_dist_processor(__A , __A , cur_len=__A ) self.assertFalse(jnp.isinf(__A ).any() ) def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = 20 SCREAMING_SNAKE_CASE = 4 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__A ) # check that all scores are -inf except the bos_token_id score SCREAMING_SNAKE_CASE = ids_tensor((batch_size, 1) , vocab_size=20 ) SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = self._get_uniform_logits(__A , __A ) SCREAMING_SNAKE_CASE = logits_processor(__A , __A , cur_len=__A ) 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 SCREAMING_SNAKE_CASE = 3 SCREAMING_SNAKE_CASE = self._get_uniform_logits(__A , __A ) SCREAMING_SNAKE_CASE = logits_processor(__A , __A , cur_len=__A ) self.assertFalse(jnp.isinf(__A ).any() ) def __snake_case( self : Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = 20 SCREAMING_SNAKE_CASE = 4 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 5 SCREAMING_SNAKE_CASE = FlaxForcedEOSTokenLogitsProcessor(max_length=__A , eos_token_id=__A ) # check that all scores are -inf except the eos_token_id when max_length is reached SCREAMING_SNAKE_CASE = ids_tensor((batch_size, 4) , vocab_size=20 ) SCREAMING_SNAKE_CASE = 4 SCREAMING_SNAKE_CASE = self._get_uniform_logits(__A , __A ) SCREAMING_SNAKE_CASE = logits_processor(__A , __A , cur_len=__A ) 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 SCREAMING_SNAKE_CASE = 3 SCREAMING_SNAKE_CASE = self._get_uniform_logits(__A , __A ) SCREAMING_SNAKE_CASE = logits_processor(__A , __A , cur_len=__A ) self.assertFalse(jnp.isinf(__A ).any() ) def __snake_case( self : int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = 4 SCREAMING_SNAKE_CASE = 10 SCREAMING_SNAKE_CASE = 15 SCREAMING_SNAKE_CASE = 2 SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE = ids_tensor((batch_size, sequence_length) , __A ) SCREAMING_SNAKE_CASE = input_ids.copy() SCREAMING_SNAKE_CASE = self._get_uniform_logits(__A , __A ) SCREAMING_SNAKE_CASE = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE = FlaxTemperatureLogitsWarper(temperature=0.5 ) SCREAMING_SNAKE_CASE = FlaxTopKLogitsWarper(3 ) SCREAMING_SNAKE_CASE = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors SCREAMING_SNAKE_CASE = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=__A ) SCREAMING_SNAKE_CASE = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__A ) SCREAMING_SNAKE_CASE = FlaxForcedEOSTokenLogitsProcessor(max_length=__A , eos_token_id=__A ) SCREAMING_SNAKE_CASE = 10 # no processor list SCREAMING_SNAKE_CASE = temp_dist_warp(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = top_k_warp(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = top_p_warp(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = min_dist_proc(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = bos_dist_proc(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = eos_dist_proc(__A , __A , cur_len=__A ) # with processor list SCREAMING_SNAKE_CASE = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) SCREAMING_SNAKE_CASE = processor(__A , __A , cur_len=__A ) # scores should be equal self.assertTrue(jnp.allclose(__A , __A , atol=1e-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = 4 SCREAMING_SNAKE_CASE = 10 SCREAMING_SNAKE_CASE = 15 SCREAMING_SNAKE_CASE = 2 SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE = ids_tensor((batch_size, sequence_length) , __A ) SCREAMING_SNAKE_CASE = input_ids.copy() SCREAMING_SNAKE_CASE = self._get_uniform_logits(__A , __A ) SCREAMING_SNAKE_CASE = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE = FlaxTemperatureLogitsWarper(temperature=0.5 ) SCREAMING_SNAKE_CASE = FlaxTopKLogitsWarper(3 ) SCREAMING_SNAKE_CASE = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors SCREAMING_SNAKE_CASE = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=__A ) SCREAMING_SNAKE_CASE = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__A ) SCREAMING_SNAKE_CASE = FlaxForcedEOSTokenLogitsProcessor(max_length=__A , eos_token_id=__A ) SCREAMING_SNAKE_CASE = 10 # no processor list def run_no_processor_list(_UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[Any] ): SCREAMING_SNAKE_CASE = temp_dist_warp(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = top_k_warp(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = top_p_warp(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = min_dist_proc(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = bos_dist_proc(__A , __A , cur_len=__A ) SCREAMING_SNAKE_CASE = eos_dist_proc(__A , __A , cur_len=__A ) return scores # with processor list def run_processor_list(_UpperCamelCase : str , _UpperCamelCase : int , _UpperCamelCase : int ): SCREAMING_SNAKE_CASE = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) SCREAMING_SNAKE_CASE = processor(__A , __A , cur_len=__A ) return scores SCREAMING_SNAKE_CASE = jax.jit(__A ) SCREAMING_SNAKE_CASE = jax.jit(__A ) SCREAMING_SNAKE_CASE = jitted_run_no_processor_list(__A , __A , __A ) SCREAMING_SNAKE_CASE = jitted_run_processor_list(__A , __A , __A ) # scores should be equal self.assertTrue(jnp.allclose(__A , __A , atol=1e-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() )
711
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
0