code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" from math import pi, sqrt def lowercase ( a__ : float ) -> float: if num <= 0: raise ValueError('''math domain error''' ) if num > 171.5: raise OverflowError('''math range error''' ) elif num - int(a__ ) not in (0, 0.5): raise NotImplementedError('''num must be an integer or a half-integer''' ) elif num == 0.5: return sqrt(a__ ) else: return 1.0 if num == 1 else (num - 1) * gamma(num - 1 ) def lowercase ( ) -> None: assert gamma(0.5 ) == sqrt(a__ ) assert gamma(1 ) == 1.0 assert gamma(2 ) == 1.0 if __name__ == "__main__": from doctest import testmod testmod() UpperCAmelCase = 1.0 while num: UpperCAmelCase = float(input("""Gamma of: """)) print(F'''gamma({num}) = {gamma(num)}''') print("""\nEnter 0 to exit...""")
256
import math import random from typing import Any from .hill_climbing import SearchProblem def lowerCAmelCase__( lowercase : Dict , lowercase : bool = True , lowercase : float = math.inf , lowercase : float = -math.inf , lowercase : float = math.inf , lowercase : float = -math.inf , lowercase : bool = False , lowercase : float = 100 , lowercase : float = 0.0_1 , lowercase : float = 1 , ) -> Any: __snake_case : Optional[Any] = False __snake_case : Optional[Any] = search_prob __snake_case : str = start_temperate __snake_case : List[Any] = [] __snake_case : str = 0 __snake_case : Dict = None while not search_end: __snake_case : List[Any] = current_state.score() if best_state is None or current_score > best_state.score(): __snake_case : List[Any] = current_state scores.append(lowercase ) iterations += 1 __snake_case : Dict = None __snake_case : str = current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to __snake_case : Any = random.randint(0 , len(lowercase ) - 1 ) # picking a random neighbor __snake_case : int = neighbors.pop(lowercase ) __snake_case : Optional[Any] = picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: __snake_case : Any = change * -1 # in case we are finding minimum if change > 0: # improves the solution __snake_case : List[str] = picked_neighbor else: __snake_case : Optional[Any] = (math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability __snake_case : str = picked_neighbor __snake_case : Optional[Any] = current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor __snake_case : Optional[Any] = True else: __snake_case : str = next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(lowercase ) , lowercase ) plt.xlabel("Iterations" ) plt.ylabel("Function values" ) plt.show() return best_state if __name__ == "__main__": def lowerCAmelCase__( lowercase : List[str] , lowercase : Tuple ) -> str: return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) _UpperCamelCase = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing( prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) # starting the problem with initial coordinates (12, 47) _UpperCamelCase = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing( prob, find_max=True, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) def lowerCAmelCase__( lowercase : Any , lowercase : Union[str, Any] ) -> Any: return (3 * x**2) - (6 * y) _UpperCamelCase = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing(prob, find_max=False, visualization=True) print( '''The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' F'''{local_min.score()}''' ) _UpperCamelCase = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing(prob, find_max=True, visualization=True) print( '''The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' F'''{local_min.score()}''' )
326
0
import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) lowerCAmelCase : Union[str, Any] = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation="""relu""") ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation="""relu""")) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation="""relu""")) classifier.add(layers.Dense(units=1, activation="""sigmoid""")) # Compiling the CNN classifier.compile( optimizer="""adam""", loss="""binary_crossentropy""", metrics=["""accuracy"""] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') lowerCAmelCase : Tuple = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) lowerCAmelCase : Tuple = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) lowerCAmelCase : Optional[int] = train_datagen.flow_from_directory( """dataset/training_set""", target_size=(64, 64), batch_size=32, class_mode="""binary""" ) lowerCAmelCase : Any = test_datagen.flow_from_directory( """dataset/test_set""", target_size=(64, 64), batch_size=32, class_mode="""binary""" ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save("""cnn.h5""") # Part 3 - Making new predictions lowerCAmelCase : Tuple = tf.keras.preprocessing.image.load_img( """dataset/single_prediction/image.png""", target_size=(64, 64) ) lowerCAmelCase : Optional[Any] = tf.keras.preprocessing.image.img_to_array(test_image) lowerCAmelCase : Optional[int] = np.expand_dims(test_image, axis=0) lowerCAmelCase : Optional[Any] = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: lowerCAmelCase : Tuple = """Normal""" if result[0][0] == 1: lowerCAmelCase : List[str] = """Abnormality detected"""
13
import warnings from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =["image_processor", "tokenizer"] UpperCAmelCase_ : Tuple ="FlavaImageProcessor" UpperCAmelCase_ : List[Any] =("BertTokenizer", "BertTokenizerFast") def __init__( self , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase ) -> int: '''simple docstring''' __snake_case : List[Any] = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) __snake_case : List[Any] = kwargs.pop("feature_extractor" ) __snake_case : Any = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = self.image_processor def __call__( self , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = True , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = 0 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = True , UpperCAmelCase = None , **UpperCAmelCase , ) -> List[Any]: '''simple docstring''' if text is None and images is None: raise ValueError("You have to specify either text or images. Both cannot be none." ) if text is not None: __snake_case : Union[str, Any] = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if images is not None: __snake_case : Union[str, Any] = self.image_processor( UpperCAmelCase , return_image_mask=UpperCAmelCase , return_codebook_pixels=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if text is not None and images is not None: encoding.update(UpperCAmelCase ) return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def UpperCAmelCase ( self , *UpperCAmelCase , **UpperCAmelCase ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , *UpperCAmelCase , **UpperCAmelCase ) -> Tuple: '''simple docstring''' return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case : List[Any] = self.tokenizer.model_input_names __snake_case : Union[str, Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
326
0
import os _A = {'''I''': 1, '''V''': 5, '''X''': 10, '''L''': 50, '''C''': 100, '''D''': 500, '''M''': 1_000} def __UpperCamelCase ( _A ): lowerCAmelCase_ = 0 lowerCAmelCase_ = 0 while index < len(_A ) - 1: lowerCAmelCase_ = SYMBOLS[numerals[index]] lowerCAmelCase_ = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def __UpperCamelCase ( _A ): lowerCAmelCase_ = "" lowerCAmelCase_ = num // 1000 numerals += m_count * "M" num %= 1000 lowerCAmelCase_ = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 lowerCAmelCase_ = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def __UpperCamelCase ( _A = "/p089_roman.txt" ): lowerCAmelCase_ = 0 with open(os.path.dirname(_A ) + roman_numerals_filename ) as filea: lowerCAmelCase_ = filea.readlines() for line in lines: lowerCAmelCase_ = line.strip() lowerCAmelCase_ = parse_roman_numerals(_A ) lowerCAmelCase_ = generate_roman_numerals(_A ) savings += len(_A ) - len(_A ) return savings if __name__ == "__main__": print(f"{solution() = }")
278
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model'''} _UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', } } _UpperCamelCase = { '''camembert-base''': 512, } _UpperCamelCase = '''▁''' class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Optional[Any] =VOCAB_FILES_NAMES UpperCAmelCase_ : str =PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase_ : str =["input_ids", "attention_mask"] def __init__( self , UpperCAmelCase , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<s>" , UpperCAmelCase="<unk>" , UpperCAmelCase="<pad>" , UpperCAmelCase="<mask>" , UpperCAmelCase=["<s>NOTUSED", "</s>NOTUSED"] , UpperCAmelCase = None , **UpperCAmelCase , ) -> None: '''simple docstring''' __snake_case : Dict = AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token __snake_case : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , additional_special_tokens=UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **UpperCAmelCase , ) __snake_case : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(UpperCAmelCase ) ) __snake_case : Dict = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __snake_case : str = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __snake_case : Optional[int] = len(self.fairseq_tokens_to_ids ) __snake_case : Any = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __snake_case : List[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __snake_case : Dict = [self.cls_token_id] __snake_case : Any = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = 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 [1] + ([0] * len(UpperCAmelCase )) + [1] return [1] + ([0] * len(UpperCAmelCase )) + [1, 1] + ([0] * len(UpperCAmelCase )) + [1] def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: '''simple docstring''' __snake_case : int = [self.sep_token_id] __snake_case : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCAmelCase ( self ) -> int: '''simple docstring''' return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case : Optional[int] = {self.convert_ids_to_tokens(UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCAmelCase ( self , UpperCAmelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(UpperCAmelCase , out_type=UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> List[Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(UpperCAmelCase ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> Tuple: '''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 UpperCAmelCase ( self , UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __snake_case : Tuple = [] __snake_case : Union[str, Any] = "" __snake_case : Optional[int] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(UpperCAmelCase ) + token __snake_case : List[Any] = True __snake_case : Union[str, Any] = [] else: current_sub_tokens.append(UpperCAmelCase ) __snake_case : int = False out_string += self.sp_model.decode(UpperCAmelCase ) return out_string.strip() def __getstate__( self ) -> List[Any]: '''simple docstring''' __snake_case : str = self.__dict__.copy() __snake_case : Optional[Any] = None return state def __setstate__( self , UpperCAmelCase ) -> str: '''simple docstring''' __snake_case : Optional[Any] = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): __snake_case : List[str] = {} __snake_case : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return __snake_case : Optional[Any] = os.path.join( UpperCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase , "wb" ) as fi: __snake_case : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase ) return (out_vocab_file,)
326
0
'''simple docstring''' from argparse import ArgumentParser from datasets.commands.convert import ConvertCommand from datasets.commands.dummy_data import DummyDataCommand from datasets.commands.env import EnvironmentCommand from datasets.commands.run_beam import RunBeamCommand from datasets.commands.test import TestCommand from datasets.utils.logging import set_verbosity_info def _a( UpperCamelCase__ : Dict ): '''simple docstring''' return {key.lstrip('''-''' ): value for key, value in zip(unknown_args[::2], unknown_args[1::2] )} def _a( ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] =ArgumentParser( '''HuggingFace Datasets CLI tool''', usage='''datasets-cli <command> [<args>]''', allow_abbrev=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ : Tuple =parser.add_subparsers(help='''datasets-cli command helpers''' ) set_verbosity_info() # Register commands ConvertCommand.register_subcommand(UpperCamelCase__ ) EnvironmentCommand.register_subcommand(UpperCamelCase__ ) TestCommand.register_subcommand(UpperCamelCase__ ) RunBeamCommand.register_subcommand(UpperCamelCase__ ) DummyDataCommand.register_subcommand(UpperCamelCase__ ) # Parse args SCREAMING_SNAKE_CASE__ : Dict =parser.parse_known_args() if not hasattr(UpperCamelCase__, '''func''' ): parser.print_help() exit(1 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] =parse_unknown_args(UpperCamelCase__ ) # Run SCREAMING_SNAKE_CASE__ : List[str] =args.func(UpperCamelCase__, **UpperCamelCase__ ) service.run() if __name__ == "__main__": main()
152
def lowerCAmelCase__( lowercase : list[int] , lowercase : int ) -> bool: __snake_case : List[str] = len(lowercase ) __snake_case : int = [[False] * (required_sum + 1) for _ in range(arr_len + 1 )] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 for i in range(arr_len + 1 ): __snake_case : Optional[Any] = True # sum is not zero and set is empty then false for i in range(1 , required_sum + 1 ): __snake_case : Union[str, Any] = False for i in range(1 , arr_len + 1 ): for j in range(1 , required_sum + 1 ): if arr[i - 1] > j: __snake_case : List[str] = subset[i - 1][j] if arr[i - 1] <= j: __snake_case : Union[str, Any] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] return subset[arr_len][required_sum] if __name__ == "__main__": import doctest doctest.testmod()
326
0
"""simple docstring""" def a_ ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple ): '''simple docstring''' lowercase__ : Any = (boundary[1] - boundary[0]) / steps lowercase__ : Any = boundary[0] lowercase__ : Optional[Any] = boundary[1] lowercase__ : Union[str, Any] = make_points(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) lowercase__ : str = 0.0 y += (h / 2.0) * f(_lowerCAmelCase ) for i in x_i: # print(i) y += h * f(_lowerCAmelCase ) y += (h / 2.0) * f(_lowerCAmelCase ) return y def a_ ( _lowerCAmelCase : str , _lowerCAmelCase : Any , _lowerCAmelCase : Any ): '''simple docstring''' lowercase__ : Dict = a + h while x < (b - h): yield x lowercase__ : Dict = x + h def a_ ( _lowerCAmelCase : Optional[Any] ): # enter your function here '''simple docstring''' lowercase__ : Any = (x - 0) * (x - 0) return y def a_ ( ): '''simple docstring''' lowercase__ : Optional[Any] = 0.0 # Lower bound of integration lowercase__ : Union[str, Any] = 1.0 # Upper bound of integration lowercase__ : Tuple = 10.0 # define number of steps or resolution lowercase__ : Optional[Any] = [a, b] # define boundary of integration lowercase__ : List[Any] = method_a(_lowerCAmelCase , _lowerCAmelCase ) print(f"""y = {y}""" ) if __name__ == "__main__": main()
77
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node _UpperCamelCase = 4 _UpperCamelCase = 3 class _lowerCamelCase ( a ): """simple docstring""" pass def lowerCAmelCase__( lowercase : List[str] ) -> Any: for shard in shards: for i in range(lowercase ): yield {"i": i, "shard": shard} def lowerCAmelCase__( ) -> Optional[int]: __snake_case : List[Any] = int(os.environ["RANK"] ) __snake_case : Optional[int] = int(os.environ["WORLD_SIZE"] ) __snake_case : List[str] = ArgumentParser() parser.add_argument("--streaming" , type=lowercase ) parser.add_argument("--local_rank" , type=lowercase ) parser.add_argument("--num_workers" , type=lowercase , default=0 ) __snake_case : Any = parser.parse_args() __snake_case : Dict = args.streaming __snake_case : Union[str, Any] = args.num_workers __snake_case : Any = {"shards": [f"""shard_{shard_idx}""" for shard_idx in range(lowercase )]} __snake_case : Optional[int] = IterableDataset.from_generator(lowercase , gen_kwargs=lowercase ) if not streaming: __snake_case : Any = Dataset.from_list(list(lowercase ) ) __snake_case : Dict = split_dataset_by_node(lowercase , rank=lowercase , world_size=lowercase ) __snake_case : Union[str, Any] = torch.utils.data.DataLoader(lowercase , num_workers=lowercase ) __snake_case : Optional[int] = NUM_SHARDS * NUM_ITEMS_PER_SHARD __snake_case : List[str] = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) __snake_case : Dict = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f"""local_size {local_size} != expected_local_size {expected_local_size}""" ) if __name__ == "__main__": main()
326
0
def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' return price * (1 + tax_rate) if __name__ == "__main__": print(F'{price_plus_tax(100, 0.2_5) = }') print(F'{price_plus_tax(125.50, 0.0_5) = }')
43
def lowerCAmelCase__( lowercase : int = 100_0000 ) -> int: __snake_case : List[Any] = limit + 1 __snake_case : List[str] = [0] * limit for first_term in range(1 , lowercase ): for n in range(lowercase , lowercase , lowercase ): __snake_case : Union[str, Any] = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a __snake_case : Tuple = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
326
0
'''simple docstring''' from __future__ import annotations import numpy as np def UpperCamelCase_( snake_case : np.ndarray ): '''simple docstring''' snake_case_ = np.shape(snake_case ) if rows != columns: snake_case_ = ( "'table' has to be of square shaped array but got a " f'{rows}x{columns} array:\n{table}' ) raise ValueError(snake_case ) snake_case_ = np.zeros((rows, columns) ) snake_case_ = np.zeros((rows, columns) ) for i in range(snake_case ): for j in range(snake_case ): snake_case_ = sum(lower[i][k] * upper[k][j] for k in range(snake_case ) ) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists" ) snake_case_ = (table[i][j] - total) / upper[j][j] snake_case_ = 1 for j in range(snake_case , snake_case ): snake_case_ = sum(lower[i][k] * upper[k][j] for k in range(snake_case ) ) snake_case_ = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
85
from __future__ import annotations def lowerCAmelCase__( lowercase : str , lowercase : list[str] | None = None ) -> list[list[str]]: __snake_case : List[str] = word_bank or [] # create a table __snake_case : int = len(lowercase ) + 1 __snake_case : list[list[list[str]]] = [] for _ in range(lowercase ): table.append([] ) # seed value __snake_case : Optional[int] = [[]] # because empty string has empty combination # iterate through the indices for i in range(lowercase ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(lowercase )] == word: __snake_case : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(lowercase )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(lowercase )]: combination.reverse() return table[len(lowercase )] if __name__ == "__main__": print(all_construct('''jwajalapa''', ['''jwa''', '''j''', '''w''', '''a''', '''la''', '''lapa'''])) print(all_construct('''rajamati''', ['''s''', '''raj''', '''amat''', '''raja''', '''ma''', '''i''', '''t'''])) print( all_construct( '''hexagonosaurus''', ['''h''', '''ex''', '''hex''', '''ag''', '''ago''', '''ru''', '''auru''', '''rus''', '''go''', '''no''', '''o''', '''s'''], ) )
326
0
"""simple docstring""" 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 _SCREAMING_SNAKE_CASE : Optional[int] = logging.get_logger(__name__) class a : SCREAMING_SNAKE_CASE : str SCREAMING_SNAKE_CASE : str = None @staticmethod def UpperCamelCase ( ) -> Optional[int]: raise NotImplementedError def UpperCamelCase ( self : int , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : List[Any] , **__SCREAMING_SNAKE_CASE : Any ) -> List[str]: raise NotImplementedError def UpperCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Optional[int]: raise NotImplementedError def UpperCamelCase ( self : Tuple ) -> Dict: 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 UpperCamelCase ( cls : List[str] ) -> Tuple: return F'''`pip install {cls.pip_package or cls.name}`''' class a ( __snake_case ): SCREAMING_SNAKE_CASE : Optional[int] = "optuna" @staticmethod def UpperCamelCase ( ) -> Union[str, Any]: return is_optuna_available() def UpperCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Dict , **__SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: return run_hp_search_optuna(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> int: return default_hp_space_optuna(__SCREAMING_SNAKE_CASE ) class a ( __snake_case ): SCREAMING_SNAKE_CASE : List[str] = "ray" SCREAMING_SNAKE_CASE : Dict = "'ray[tune]'" @staticmethod def UpperCamelCase ( ) -> str: return is_ray_available() def UpperCamelCase ( self : Any , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , **__SCREAMING_SNAKE_CASE : Tuple ) -> List[Any]: return run_hp_search_ray(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : Any , __SCREAMING_SNAKE_CASE : List[str] ) -> str: return default_hp_space_ray(__SCREAMING_SNAKE_CASE ) class a ( __snake_case ): SCREAMING_SNAKE_CASE : Tuple = "sigopt" @staticmethod def UpperCamelCase ( ) -> int: return is_sigopt_available() def UpperCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[str] , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: return run_hp_search_sigopt(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: return default_hp_space_sigopt(__SCREAMING_SNAKE_CASE ) class a ( __snake_case ): SCREAMING_SNAKE_CASE : str = "wandb" @staticmethod def UpperCamelCase ( ) -> Optional[Any]: return is_wandb_available() def UpperCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[Any] , **__SCREAMING_SNAKE_CASE : Tuple ) -> Union[str, Any]: return run_hp_search_wandb(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Tuple ) -> List[str]: return default_hp_space_wandb(__SCREAMING_SNAKE_CASE ) _SCREAMING_SNAKE_CASE : List[Any] = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def lowerCamelCase__ ( ) -> str: lowerCamelCase_ = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(_lowerCamelCase ) > 0: lowerCamelCase_ = available_backends[0].name if len(_lowerCamelCase ) > 1: logger.info( F'''{len(_lowerCamelCase )} 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() ) )
183
import unittest from transformers import BigBirdConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=2 , UpperCAmelCase=56 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=99 , UpperCAmelCase=32 , UpperCAmelCase=2 , UpperCAmelCase=2 , UpperCAmelCase=7 , UpperCAmelCase="gelu_new" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=512 , UpperCAmelCase=16 , UpperCAmelCase=2 , UpperCAmelCase=0.02 , UpperCAmelCase=4 , UpperCAmelCase="block_sparse" , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=2 , UpperCAmelCase=3 , ) -> Tuple: '''simple docstring''' __snake_case : Optional[int] = parent __snake_case : Tuple = batch_size __snake_case : List[str] = seq_length __snake_case : Optional[int] = is_training __snake_case : int = use_attention_mask __snake_case : Union[str, Any] = use_token_type_ids __snake_case : Any = use_labels __snake_case : List[str] = vocab_size __snake_case : int = hidden_size __snake_case : List[str] = num_hidden_layers __snake_case : List[Any] = num_attention_heads __snake_case : Optional[int] = intermediate_size __snake_case : Union[str, Any] = hidden_act __snake_case : Optional[int] = hidden_dropout_prob __snake_case : Optional[Any] = attention_probs_dropout_prob __snake_case : str = max_position_embeddings __snake_case : List[Any] = type_vocab_size __snake_case : int = type_sequence_label_size __snake_case : Dict = initializer_range __snake_case : List[Any] = num_choices __snake_case : Union[str, Any] = rescale_embeddings __snake_case : List[Any] = attention_type __snake_case : str = use_bias __snake_case : Dict = block_size __snake_case : Optional[Any] = num_random_blocks def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case : Any = None if self.use_attention_mask: __snake_case : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case : Union[str, Any] = None if self.use_token_type_ids: __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case : Optional[int] = BigBirdConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , attention_type=self.attention_type , block_size=self.block_size , num_random_blocks=self.num_random_blocks , use_bias=self.use_bias , rescale_embeddings=self.rescale_embeddings , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case : Optional[int] = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case : Dict = config_and_inputs __snake_case : int = { "input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask, } return config, inputs_dict @require_flax class _lowerCamelCase ( a , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) UpperCAmelCase_ : Dict =False UpperCAmelCase_ : str =False def UpperCAmelCase ( self ) -> str: '''simple docstring''' __snake_case : Dict = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Any: '''simple docstring''' super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_hidden_states_output() @slow def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case : Any = model_class_name.from_pretrained("google/bigbird-roberta-base" ) self.assertIsNotNone(UpperCAmelCase ) def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case , __snake_case : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __snake_case : Optional[Any] = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = model_class(UpperCAmelCase ) @jax.jit def model_jitted(UpperCAmelCase , UpperCAmelCase=None , **UpperCAmelCase ): return model(input_ids=UpperCAmelCase , attention_mask=UpperCAmelCase , **UpperCAmelCase ) with self.subTest("JIT Enabled" ): __snake_case : int = model_jitted(**UpperCAmelCase ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __snake_case : List[Any] = model_jitted(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) ) for jitted_output, output in zip(UpperCAmelCase , UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=1E-5 , UpperCAmelCase="outputs" , UpperCAmelCase=None ) -> int: '''simple docstring''' if name.startswith("outputs.attentions" ): return else: super().check_pt_flax_outputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase )
326
0
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class UpperCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , UpperCamelCase__ : Optional[Any] , ) -> str: """simple docstring""" __magic_name__ = parent __magic_name__ = 13 __magic_name__ = 7 __magic_name__ = True __magic_name__ = True __magic_name__ = True __magic_name__ = True __magic_name__ = True __magic_name__ = False __magic_name__ = False __magic_name__ = False __magic_name__ = 2 __magic_name__ = 99 __magic_name__ = 0 __magic_name__ = 32 __magic_name__ = 2 __magic_name__ = 4 __magic_name__ = 0.1 __magic_name__ = 0.1 __magic_name__ = 512 __magic_name__ = 16 __magic_name__ = 2 __magic_name__ = 0.02 __magic_name__ = 3 __magic_name__ = 4 __magic_name__ = "last" __magic_name__ = True __magic_name__ = None __magic_name__ = 0 def _lowercase ( self : Optional[Any] ) -> List[str]: """simple docstring""" __magic_name__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __magic_name__ = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) __magic_name__ = None if self.use_input_lengths: __magic_name__ = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length __magic_name__ = None if self.use_token_type_ids: __magic_name__ = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) __magic_name__ = None __magic_name__ = None __magic_name__ = None if self.use_labels: __magic_name__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __magic_name__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __magic_name__ = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) __magic_name__ = ids_tensor([self.batch_size] , self.num_choices ) __magic_name__ = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _lowercase ( self : str , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Tuple , UpperCamelCase__ : Any , UpperCamelCase__ : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : List[Any] , ) -> Union[str, Any]: """simple docstring""" __magic_name__ = TFFlaubertModel(config=UpperCamelCase__ ) __magic_name__ = {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids} __magic_name__ = model(UpperCamelCase__ ) __magic_name__ = [input_ids, input_mask] __magic_name__ = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase ( self : Tuple , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : int , UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : List[str] , UpperCamelCase__ : int , UpperCamelCase__ : int , ) -> int: """simple docstring""" __magic_name__ = TFFlaubertWithLMHeadModel(UpperCamelCase__ ) __magic_name__ = {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids} __magic_name__ = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowercase ( self : List[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Any , UpperCamelCase__ : List[str] , UpperCamelCase__ : int , UpperCamelCase__ : str , ) -> List[Any]: """simple docstring""" __magic_name__ = TFFlaubertForQuestionAnsweringSimple(UpperCamelCase__ ) __magic_name__ = {"input_ids": input_ids, "lengths": input_lengths} __magic_name__ = model(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 _lowercase ( self : Optional[int] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : int , UpperCamelCase__ : Any , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] , ) -> List[str]: """simple docstring""" __magic_name__ = TFFlaubertForSequenceClassification(UpperCamelCase__ ) __magic_name__ = {"input_ids": input_ids, "lengths": input_lengths} __magic_name__ = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowercase ( self : int , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , ) -> Union[str, Any]: """simple docstring""" __magic_name__ = self.num_labels __magic_name__ = TFFlaubertForTokenClassification(config=UpperCamelCase__ ) __magic_name__ = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} __magic_name__ = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _lowercase ( self : Tuple , UpperCamelCase__ : Tuple , UpperCamelCase__ : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : int , ) -> List[Any]: """simple docstring""" __magic_name__ = self.num_choices __magic_name__ = TFFlaubertForMultipleChoice(config=UpperCamelCase__ ) __magic_name__ = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) __magic_name__ = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) __magic_name__ = tf.tile(tf.expand_dims(UpperCamelCase__ , 1 ) , (1, self.num_choices, 1) ) __magic_name__ = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } __magic_name__ = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _lowercase ( self : List[Any] ) -> int: """simple docstring""" __magic_name__ = self.prepare_config_and_inputs() ( __magic_name__ ) = config_and_inputs __magic_name__ = { "input_ids": input_ids, "token_type_ids": token_type_ids, "langs": token_type_ids, "lengths": input_lengths, } return config, inputs_dict @require_tf class UpperCAmelCase_ ( _A , _A , unittest.TestCase ): '''simple docstring''' a__ = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) a__ = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable a__ = ( { "feature-extraction": TFFlaubertModel, "fill-mask": TFFlaubertWithLMHeadModel, "question-answering": TFFlaubertForQuestionAnsweringSimple, "text-classification": TFFlaubertForSequenceClassification, "token-classification": TFFlaubertForTokenClassification, "zero-shot": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) a__ = False a__ = False def _lowercase ( self : Union[str, Any] , UpperCamelCase__ : int , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : List[str] , UpperCamelCase__ : List[str] ) -> List[Any]: """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _lowercase ( self : Tuple ) -> str: """simple docstring""" __magic_name__ = TFFlaubertModelTester(self ) __magic_name__ = ConfigTester(self , config_class=UpperCamelCase__ , emb_dim=37 ) def _lowercase ( self : str ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def _lowercase ( self : List[str] ) -> Any: """simple docstring""" __magic_name__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*UpperCamelCase__ ) def _lowercase ( self : Any ) -> str: """simple docstring""" __magic_name__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase__ ) def _lowercase ( self : str ) -> Optional[int]: """simple docstring""" __magic_name__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase__ ) def _lowercase ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" __magic_name__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase__ ) def _lowercase ( self : Optional[int] ) -> Optional[int]: """simple docstring""" __magic_name__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*UpperCamelCase__ ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" __magic_name__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*UpperCamelCase__ ) @slow def _lowercase ( self : Dict ) -> Union[str, Any]: """simple docstring""" for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __magic_name__ = TFFlaubertModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @require_tf @require_sentencepiece @require_tokenizers class UpperCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def _lowercase ( self : Any ) -> int: """simple docstring""" __magic_name__ = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) __magic_name__ = tf.convert_to_tensor( [[0, 158, 735, 2592, 1424, 6727, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" __magic_name__ = model(UpperCamelCase__ )[0] __magic_name__ = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape , UpperCamelCase__ ) # compare the actual values for a slice. __magic_name__ = tf.convert_to_tensor( [ [ [-1.8768773, -1.566555, 0.27072418], [-1.6920038, -0.5873505, 1.9329599], [-2.9563985, -1.6993835, 1.7972052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
88
import argparse import datetime def lowerCAmelCase__( lowercase : str ) -> str: __snake_case : int = { "0": "Sunday", "1": "Monday", "2": "Tuesday", "3": "Wednesday", "4": "Thursday", "5": "Friday", "6": "Saturday", } __snake_case : int = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(lowercase ) < 11: raise ValueError("Must be 10 characters long" ) # Get month __snake_case : int = int(date_input[0] + date_input[1] ) # Validate if not 0 < m < 13: raise ValueError("Month must be between 1 - 12" ) __snake_case : str = date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'" ) # Get day __snake_case : int = int(date_input[3] + date_input[4] ) # Validate if not 0 < d < 32: raise ValueError("Date must be between 1 - 31" ) # Get second separator __snake_case : str = date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'" ) # Get year __snake_case : int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9] ) # Arbitrary year range if not 45 < y < 8500: raise ValueError( "Year out of range. There has to be some sort of limit...right?" ) # Get datetime obj for validation __snake_case : str = datetime.date(int(lowercase ) , int(lowercase ) , int(lowercase ) ) # Start math if m <= 2: __snake_case : Optional[Any] = y - 1 __snake_case : Tuple = m + 12 # maths var __snake_case : int = int(str(lowercase )[:2] ) __snake_case : int = int(str(lowercase )[2:] ) __snake_case : int = int(2.6 * m - 5.3_9 ) __snake_case : int = int(c / 4 ) __snake_case : int = int(k / 4 ) __snake_case : int = int(d + k ) __snake_case : int = int(t + u + v + x ) __snake_case : int = int(z - (2 * c) ) __snake_case : int = round(w % 7 ) # End math # Validate math if f != convert_datetime_days[dt_ck.weekday()]: raise AssertionError("The date was evaluated incorrectly. Contact developer." ) # Response __snake_case : str = f"""Your date {date_input}, is a {days[str(lowercase )]}!""" return response if __name__ == "__main__": import doctest doctest.testmod() _UpperCamelCase = argparse.ArgumentParser( description=( '''Find out what day of the week nearly any date is or was. Enter ''' '''date as a string in the mm-dd-yyyy or mm/dd/yyyy format''' ) ) parser.add_argument( '''date_input''', type=str, help='''Date as a string (mm-dd-yyyy or mm/dd/yyyy)''' ) _UpperCamelCase = parser.parse_args() zeller(args.date_input)
326
0
def A ( lowercase , lowercase , lowercase , lowercase , lowercase , lowercase ) -> int: '''simple docstring''' if index == r: for j in range(lowercase ): print(data[j] , end=' ' ) print(' ' ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location UpperCamelCase = arr[i] combination_util(lowercase , lowercase , lowercase , index + 1 , lowercase , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(lowercase , lowercase , lowercase , lowercase , lowercase , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def A ( lowercase , lowercase , lowercase ) -> Optional[Any]: '''simple docstring''' UpperCamelCase = [0] * r # Print all combination using temporary array 'data[]' combination_util(lowercase , lowercase , lowercase , 0 , lowercase , 0 ) if __name__ == "__main__": # Driver code to check the function above _UpperCAmelCase : Union[str, Any] = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
222
def lowerCAmelCase__( lowercase : List[Any] , lowercase : Optional[Any] , lowercase : Optional[int] , lowercase : str , lowercase : List[Any] , lowercase : List[str] ) -> int: if index == r: for j in range(lowercase ): print(data[j] , end=" " ) print(" " ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location __snake_case : Union[str, Any] = arr[i] combination_util(lowercase , lowercase , lowercase , index + 1 , lowercase , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(lowercase , lowercase , lowercase , lowercase , lowercase , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def lowerCAmelCase__( lowercase : Any , lowercase : Tuple , lowercase : Union[str, Any] ) -> Optional[Any]: # A temporary array to store all combination one by one __snake_case : Tuple = [0] * r # Print all combination using temporary array 'data[]' combination_util(lowercase , lowercase , lowercase , 0 , lowercase , 0 ) if __name__ == "__main__": # Driver code to check the function above _UpperCamelCase = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
326
0
from ....configuration_utils import PretrainedConfig from ....utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { """CarlCochet/trajectory-transformer-halfcheetah-medium-v2""": ( """https://huggingface.co/CarlCochet/trajectory-transformer-halfcheetah-medium-v2/resolve/main/config.json""" ), # See all TrajectoryTransformer models at https://huggingface.co/models?filter=trajectory_transformer } class lowercase__ ( _UpperCAmelCase ): A__ : List[Any] ="trajectory_transformer" A__ : str =["past_key_values"] A__ : Optional[Any] ={ "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Any , UpperCAmelCase_ : Union[str, Any]=100 , UpperCAmelCase_ : Any=5 , UpperCAmelCase_ : Union[str, Any]=1 , UpperCAmelCase_ : List[str]=1 , UpperCAmelCase_ : Optional[int]=249 , UpperCAmelCase_ : Any=6 , UpperCAmelCase_ : str=17 , UpperCAmelCase_ : List[str]=25 , UpperCAmelCase_ : str=4 , UpperCAmelCase_ : Dict=4 , UpperCAmelCase_ : Tuple=128 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : str=0.1 , UpperCAmelCase_ : Union[str, Any]=0.0_006 , UpperCAmelCase_ : List[str]=512 , UpperCAmelCase_ : Optional[int]=0.02 , UpperCAmelCase_ : Tuple=1e-1_2 , UpperCAmelCase_ : Dict=1 , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : List[Any]=1 , UpperCAmelCase_ : str=50256 , UpperCAmelCase_ : Optional[int]=50256 , **UpperCAmelCase_ : int , ): SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = action_weight SCREAMING_SNAKE_CASE__ = reward_weight SCREAMING_SNAKE_CASE__ = value_weight SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = block_size SCREAMING_SNAKE_CASE__ = action_dim SCREAMING_SNAKE_CASE__ = observation_dim SCREAMING_SNAKE_CASE__ = transition_dim SCREAMING_SNAKE_CASE__ = learning_rate SCREAMING_SNAKE_CASE__ = n_layer SCREAMING_SNAKE_CASE__ = n_head SCREAMING_SNAKE_CASE__ = n_embd SCREAMING_SNAKE_CASE__ = embd_pdrop SCREAMING_SNAKE_CASE__ = attn_pdrop SCREAMING_SNAKE_CASE__ = resid_pdrop SCREAMING_SNAKE_CASE__ = initializer_range SCREAMING_SNAKE_CASE__ = layer_norm_eps SCREAMING_SNAKE_CASE__ = kaiming_initializer_range SCREAMING_SNAKE_CASE__ = use_cache super().__init__(pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_ , **UpperCAmelCase_ )
176
import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = [ ('''bert.bert''', '''visual_bert'''), ('''bert.cls''', '''cls'''), ('''bert.classifier''', '''cls'''), ('''token_type_embeddings_visual''', '''visual_token_type_embeddings'''), ('''position_embeddings_visual''', '''visual_position_embeddings'''), ('''projection''', '''visual_projection'''), ] _UpperCamelCase = [ '''nlvr2_coco_pre_trained.th''', '''nlvr2_fine_tuned.th''', '''nlvr2_pre_trained.th''', '''vcr_coco_pre_train.th''', '''vcr_fine_tune.th''', '''vcr_pre_train.th''', '''vqa_coco_pre_trained.th''', '''vqa_fine_tuned.th''', '''vqa_pre_trained.th''', ] def lowerCAmelCase__( lowercase : str ) -> Optional[Any]: __snake_case : Optional[int] = torch.load(lowercase , map_location="cpu" ) return sd def lowerCAmelCase__( lowercase : List[Any] , lowercase : List[Any] , lowercase : List[Any]=rename_keys_prefix ) -> Dict: __snake_case : Tuple = OrderedDict() __snake_case : str = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue __snake_case : Optional[Any] = key for name_pair in rename_keys_prefix: __snake_case : List[str] = new_key.replace(name_pair[0] , name_pair[1] ) __snake_case : List[str] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately __snake_case : List[Any] = new_d["cls.predictions.bias"] return new_d @torch.no_grad() def lowerCAmelCase__( lowercase : Optional[Any] , lowercase : Any ) -> List[Any]: assert ( checkpoint_path.split("/" )[-1] in ACCEPTABLE_CHECKPOINTS ), f"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: __snake_case : Any = "pretraining" if "vcr" in checkpoint_path: __snake_case : Optional[Any] = {"visual_embedding_dim": 512} elif "vqa_advanced" in checkpoint_path: __snake_case : Tuple = {"visual_embedding_dim": 2048} elif "vqa" in checkpoint_path: __snake_case : Dict = {"visual_embedding_dim": 2048} elif "nlvr" in checkpoint_path: __snake_case : Any = {"visual_embedding_dim": 1024} else: raise NotImplementedError(f"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: __snake_case : Dict = {"visual_embedding_dim": 512} __snake_case : Any = "multichoice" elif "vqa_advanced" in checkpoint_path: __snake_case : List[Any] = {"visual_embedding_dim": 2048} __snake_case : Optional[Any] = "vqa_advanced" elif "vqa" in checkpoint_path: __snake_case : Union[str, Any] = {"visual_embedding_dim": 2048, "num_labels": 3129} __snake_case : Union[str, Any] = "vqa" elif "nlvr" in checkpoint_path: __snake_case : Tuple = { "visual_embedding_dim": 1024, "num_labels": 2, } __snake_case : List[Any] = "nlvr" __snake_case : Union[str, Any] = VisualBertConfig(**lowercase ) # Load State Dict __snake_case : Any = load_state_dict(lowercase ) __snake_case : Dict = get_new_dict(lowercase , lowercase ) if model_type == "pretraining": __snake_case : Optional[Any] = VisualBertForPreTraining(lowercase ) elif model_type == "vqa": __snake_case : Tuple = VisualBertForQuestionAnswering(lowercase ) elif model_type == "nlvr": __snake_case : Tuple = VisualBertForVisualReasoning(lowercase ) elif model_type == "multichoice": __snake_case : List[Any] = VisualBertForMultipleChoice(lowercase ) model.load_state_dict(lowercase ) # Save Checkpoints Path(lowercase ).mkdir(exist_ok=lowercase ) model.save_pretrained(lowercase ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''orig_checkpoint_path''', type=str, help='''A path to .th on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', type=str, help='''Path to the output PyTorch model.''') _UpperCamelCase = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
326
0
"""simple docstring""" from __future__ import annotations def lowercase ( a__ : str ) -> list[int]: return [ord(a__ ) - 96 for elem in plain] def lowercase ( a__ : list[int] ) -> str: return "".join(chr(elem + 96 ) for elem in encoded ) def lowercase ( ) -> None: _UpperCamelCase = encode(input('''-> ''' ).strip().lower() ) print('''Encoded: ''' , a__ ) print('''Decoded:''' , decode(a__ ) ) if __name__ == "__main__": main()
256
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowerCAmelCase__( lowercase : Optional[int] , lowercase : Any , lowercase : Dict , lowercase : List[str] , lowercase : List[Any] ) -> Tuple: # Load configuration defined in the metadata file with open(lowercase ) as metadata_file: __snake_case : int = json.load(lowercase ) __snake_case : Optional[int] = LukeConfig(use_entity_aware_attention=lowercase , **metadata["model_config"] ) # Load in the weights from the checkpoint_path __snake_case : List[Any] = torch.load(lowercase , map_location="cpu" )["module"] # Load the entity vocab file __snake_case : Tuple = load_original_entity_vocab(lowercase ) # add an entry for [MASK2] __snake_case : Optional[int] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 __snake_case : Union[str, Any] = XLMRobertaTokenizer.from_pretrained(metadata["model_config"]["bert_model_name"] ) # Add special tokens to the token vocabulary for downstream tasks __snake_case : Optional[int] = AddedToken("<ent>" , lstrip=lowercase , rstrip=lowercase ) __snake_case : Any = AddedToken("<ent2>" , lstrip=lowercase , rstrip=lowercase ) tokenizer.add_special_tokens({"additional_special_tokens": [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(f"""Saving tokenizer to {pytorch_dump_folder_path}""" ) tokenizer.save_pretrained(lowercase ) with open(os.path.join(lowercase , "tokenizer_config.json" ) , "r" ) as f: __snake_case : Tuple = json.load(lowercase ) __snake_case : List[Any] = "MLukeTokenizer" with open(os.path.join(lowercase , "tokenizer_config.json" ) , "w" ) as f: json.dump(lowercase , lowercase ) with open(os.path.join(lowercase , MLukeTokenizer.vocab_files_names["entity_vocab_file"] ) , "w" ) as f: json.dump(lowercase , lowercase ) __snake_case : Any = MLukeTokenizer.from_pretrained(lowercase ) # Initialize the embeddings of the special tokens __snake_case : str = tokenizer.convert_tokens_to_ids(["@"] )[0] __snake_case : List[str] = tokenizer.convert_tokens_to_ids(["#"] )[0] __snake_case : List[Any] = state_dict["embeddings.word_embeddings.weight"] __snake_case : Union[str, Any] = word_emb[ent_init_index].unsqueeze(0 ) __snake_case : Union[str, Any] = word_emb[enta_init_index].unsqueeze(0 ) __snake_case : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: __snake_case : List[Any] = state_dict[bias_name] __snake_case : Optional[int] = decoder_bias[ent_init_index].unsqueeze(0 ) __snake_case : int = decoder_bias[enta_init_index].unsqueeze(0 ) __snake_case : Any = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: __snake_case : Dict = f"""encoder.layer.{layer_index}.attention.self.""" __snake_case : Union[str, Any] = state_dict[prefix + matrix_name] __snake_case : str = state_dict[prefix + matrix_name] __snake_case : Union[str, Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks __snake_case : Any = state_dict["entity_embeddings.entity_embeddings.weight"] __snake_case : List[str] = entity_emb[entity_vocab["[MASK]"]].unsqueeze(0 ) __snake_case : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' __snake_case : List[Any] = state_dict["entity_predictions.bias"] __snake_case : List[Any] = entity_prediction_bias[entity_vocab["[MASK]"]].unsqueeze(0 ) __snake_case : Union[str, Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) __snake_case : Any = LukeForMaskedLM(config=lowercase ).eval() state_dict.pop("entity_predictions.decoder.weight" ) state_dict.pop("lm_head.decoder.weight" ) state_dict.pop("lm_head.decoder.bias" ) __snake_case : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith("lm_head" ) or key.startswith("entity_predictions" )): __snake_case : str = state_dict[key] else: __snake_case : str = state_dict[key] __snake_case , __snake_case : Union[str, Any] = model.load_state_dict(lowercase , strict=lowercase ) if set(lowercase ) != {"luke.embeddings.position_ids"}: raise ValueError(f"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(lowercase ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(f"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs __snake_case : int = MLukeTokenizer.from_pretrained(lowercase , task="entity_classification" ) __snake_case : Tuple = "ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan)." __snake_case : Union[str, Any] = (0, 9) __snake_case : Optional[int] = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) __snake_case : Any = model(**lowercase ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base __snake_case : Optional[Any] = torch.Size((1, 33, 768) ) __snake_case : Optional[int] = torch.tensor([[0.0_8_9_2, 0.0_5_9_6, -0.2_8_1_9], [0.0_1_3_4, 0.1_1_9_9, 0.0_5_7_3], [-0.0_1_6_9, 0.0_9_2_7, 0.0_6_4_4]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowercase , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base __snake_case : str = torch.Size((1, 1, 768) ) __snake_case : int = torch.tensor([[-0.1_4_8_2, 0.0_6_0_9, 0.0_3_2_2]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" f""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , lowercase , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction __snake_case : str = MLukeTokenizer.from_pretrained(lowercase ) __snake_case : Dict = "Tokyo is the capital of <mask>." __snake_case : Union[str, Any] = (24, 30) __snake_case : int = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) __snake_case : int = model(**lowercase ) __snake_case : Dict = encoding["input_ids"][0].tolist() __snake_case : Dict = input_ids.index(tokenizer.convert_tokens_to_ids("<mask>" ) ) __snake_case : Optional[int] = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(lowercase ) __snake_case : Optional[Any] = outputs.entity_logits[0][0].argmax().item() __snake_case : Optional[int] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith("en:" )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print("Saving PyTorch model to {}".format(lowercase ) ) model.save_pretrained(lowercase ) def lowerCAmelCase__( lowercase : Optional[int] ) -> List[Any]: __snake_case : Any = ["[MASK]", "[PAD]", "[UNK]"] __snake_case : Any = [json.loads(lowercase ) for line in open(lowercase )] __snake_case : Any = {} for entry in data: __snake_case : Any = entry["id"] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: __snake_case : Optional[int] = entity_id break __snake_case : Union[str, Any] = f"""{language}:{entity_name}""" __snake_case : Any = entity_id return new_mapping if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) _UpperCamelCase = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
326
0
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConfig, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaForPreTraining, WavaVecaProcessor, logging, ) from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification logging.set_verbosity_info() lowerCAmelCase : List[str] = logging.get_logger(__name__) lowerCAmelCase : str = { """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_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""", """adapter_layer""": """encoder.layers.*.adapter_layer""", """w2v_model.layer_norm""": """feature_projection.layer_norm""", """quantizer.weight_proj""": """quantizer.weight_proj""", """quantizer.vars""": """quantizer.codevectors""", """project_q""": """project_q""", """final_proj""": """project_hid""", """w2v_encoder.proj""": """lm_head""", """mask_emb""": """masked_spec_embed""", """pooling_layer.linear""": """projector""", """pooling_layer.projection""": """classifier""", } lowerCAmelCase : Any = [ """lm_head""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", """projector""", """classifier""", ] def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = {} with open(_UpperCAmelCase , "r" ) as file: for line_number, line in enumerate(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = line.strip() if line: SCREAMING_SNAKE_CASE_: Dict = line.split() SCREAMING_SNAKE_CASE_: Tuple = line_number SCREAMING_SNAKE_CASE_: Optional[int] = words[0] SCREAMING_SNAKE_CASE_: int = value return result def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): for attribute in key.split("." ): SCREAMING_SNAKE_CASE_: Tuple = getattr(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Dict = PARAM_MAPPING[full_name.split("." )[-1]] SCREAMING_SNAKE_CASE_: Any = "param" if weight_type is not None and weight_type != "param": SCREAMING_SNAKE_CASE_: Any = getattr(_UpperCAmelCase , _UpperCAmelCase ).shape elif weight_type is not None and weight_type == "param": SCREAMING_SNAKE_CASE_: List[str] = hf_pointer for attribute in hf_param_name.split("." ): SCREAMING_SNAKE_CASE_: List[Any] = getattr(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = shape_pointer.shape # let's reduce dimension SCREAMING_SNAKE_CASE_: Union[str, Any] = value[0] else: SCREAMING_SNAKE_CASE_: Dict = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" f" {value.shape} for {full_name}" ) if weight_type == "weight": SCREAMING_SNAKE_CASE_: int = value elif weight_type == "weight_g": SCREAMING_SNAKE_CASE_: Tuple = value elif weight_type == "weight_v": SCREAMING_SNAKE_CASE_: List[str] = value elif weight_type == "bias": SCREAMING_SNAKE_CASE_: str = value elif weight_type == "param": for attribute in hf_param_name.split("." ): SCREAMING_SNAKE_CASE_: Any = getattr(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = value else: SCREAMING_SNAKE_CASE_: Tuple = value logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Dict = PARAM_MAPPING[full_name.split("." )[-1]] SCREAMING_SNAKE_CASE_: Any = "param" if weight_type is not None and weight_type != "param": SCREAMING_SNAKE_CASE_: Union[str, Any] = ".".join([key, weight_type] ) elif weight_type is not None and weight_type == "param": SCREAMING_SNAKE_CASE_: List[str] = ".".join([key, hf_param_name] ) else: SCREAMING_SNAKE_CASE_: Tuple = key SCREAMING_SNAKE_CASE_: int = value if "lm_head" in full_key else value[0] lowerCAmelCase : Tuple = { """W_a""": """linear_1.weight""", """W_b""": """linear_2.weight""", """b_a""": """linear_1.bias""", """b_b""": """linear_2.bias""", """ln_W""": """norm.weight""", """ln_b""": """norm.bias""", } def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: Optional[Any] = False for key, mapped_key in MAPPING.items(): SCREAMING_SNAKE_CASE_: Optional[Any] = "wav2vec2." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: SCREAMING_SNAKE_CASE_: Dict = True if "*" in mapped_key: SCREAMING_SNAKE_CASE_: Optional[int] = name.split(_UpperCAmelCase )[0].split("." )[-2] SCREAMING_SNAKE_CASE_: Union[str, Any] = mapped_key.replace("*" , _UpperCAmelCase ) if "weight_g" in name: SCREAMING_SNAKE_CASE_: Dict = "weight_g" elif "weight_v" in name: SCREAMING_SNAKE_CASE_: Dict = "weight_v" elif "bias" in name: SCREAMING_SNAKE_CASE_: Dict = "bias" elif "weight" in name: # TODO: don't match quantizer.weight_proj SCREAMING_SNAKE_CASE_: str = "weight" else: SCREAMING_SNAKE_CASE_: Any = None if hf_dict is not None: rename_dict(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) else: set_recursively(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return is_used return is_used def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[Any] = [] SCREAMING_SNAKE_CASE_: Dict = fairseq_model.state_dict() SCREAMING_SNAKE_CASE_: Dict = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): SCREAMING_SNAKE_CASE_: Tuple = False if "conv_layers" in name: load_conv_layer( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , hf_model.config.feat_extract_norm == "group" , ) SCREAMING_SNAKE_CASE_: int = True else: SCREAMING_SNAKE_CASE_: List[str] = load_wavaveca_layer(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) if not is_used: unused_weights.append(_UpperCAmelCase ) logger.warning(f"Unused weights: {unused_weights}" ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = full_name.split("conv_layers." )[-1] SCREAMING_SNAKE_CASE_: Dict = name.split("." ) SCREAMING_SNAKE_CASE_: str = int(items[0] ) SCREAMING_SNAKE_CASE_: Dict = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE_: int = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE_: Optional[int] = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE_: Any = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE_: Optional[int] = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_UpperCAmelCase ) @torch.no_grad() def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=True , _UpperCAmelCase=False ): if config_path is not None: SCREAMING_SNAKE_CASE_: Dict = WavaVecaConfig.from_pretrained(_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_: List[Any] = WavaVecaConfig() if is_seq_class: SCREAMING_SNAKE_CASE_: Optional[Any] = read_txt_into_dict(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = idalabel SCREAMING_SNAKE_CASE_: List[str] = WavaVecaForSequenceClassification(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=_UpperCAmelCase , return_attention_mask=_UpperCAmelCase , ) feature_extractor.save_pretrained(_UpperCAmelCase ) elif is_finetuned: if dict_path: SCREAMING_SNAKE_CASE_: int = Dictionary.load(_UpperCAmelCase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq SCREAMING_SNAKE_CASE_: Tuple = target_dict.pad_index SCREAMING_SNAKE_CASE_: Optional[int] = target_dict.bos_index SCREAMING_SNAKE_CASE_: int = target_dict.eos_index SCREAMING_SNAKE_CASE_: Optional[int] = len(target_dict.symbols ) SCREAMING_SNAKE_CASE_: str = os.path.join(_UpperCAmelCase , "vocab.json" ) if not os.path.isdir(_UpperCAmelCase ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(_UpperCAmelCase ) ) return os.makedirs(_UpperCAmelCase , exist_ok=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = target_dict.indices # fairseq has the <pad> and <s> switched SCREAMING_SNAKE_CASE_: Tuple = 0 SCREAMING_SNAKE_CASE_: str = 1 with open(_UpperCAmelCase , "w" , encoding="utf-8" ) as vocab_handle: json.dump(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = WavaVecaCTCTokenizer( _UpperCAmelCase , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: Union[str, Any] = True if config.feat_extract_norm == "layer" else False SCREAMING_SNAKE_CASE_: List[Any] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=_UpperCAmelCase , return_attention_mask=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: List[Any] = WavaVecaProcessor(feature_extractor=_UpperCAmelCase , tokenizer=_UpperCAmelCase ) processor.save_pretrained(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = WavaVecaForCTC(_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_: int = WavaVecaForPreTraining(_UpperCAmelCase ) if is_finetuned or is_seq_class: SCREAMING_SNAKE_CASE_: List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: SCREAMING_SNAKE_CASE_: Union[str, Any] = argparse.Namespace(task="audio_pretraining" ) SCREAMING_SNAKE_CASE_: Union[str, Any] = fairseq.tasks.setup_task(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = model[0].eval() recursively_load_weights(_UpperCAmelCase , _UpperCAmelCase , not is_finetuned ) hf_wavavec.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("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument( """--not_finetuned""", action="""store_true""", help="""Whether the model to convert is a fine-tuned model or not""" ) parser.add_argument( """--is_seq_class""", action="""store_true""", help="""Whether the model to convert is a fine-tuned sequence classification model or not""", ) lowerCAmelCase : Optional[int] = parser.parse_args() lowerCAmelCase : str = not args.not_finetuned and not args.is_seq_class convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, is_finetuned, args.is_seq_class, )
13
from maths.prime_factors import prime_factors def lowerCAmelCase__( lowercase : int ) -> int: if not isinstance(lowercase , lowercase ): __snake_case : Optional[int] = f"""Input value of [number={number}] must be an integer""" raise TypeError(lowercase ) if number < 1: raise ValueError("Input must be a positive integer" ) return -1 if len(prime_factors(lowercase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
326
0
import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class A ( __UpperCAmelCase ): def __init__( self, UpperCamelCase__, UpperCamelCase__=13, UpperCamelCase__=7, UpperCamelCase__=True, UpperCamelCase__=True, UpperCamelCase__=True, UpperCamelCase__=True, UpperCamelCase__=True, UpperCamelCase__=False, UpperCamelCase__=False, UpperCamelCase__=False, UpperCamelCase__=2, UpperCamelCase__=99, UpperCamelCase__=0, UpperCamelCase__=32, UpperCamelCase__=5, UpperCamelCase__=4, UpperCamelCase__=0.1, UpperCamelCase__=0.1, UpperCamelCase__=512, UpperCamelCase__=12, UpperCamelCase__=2, UpperCamelCase__=0.02, UpperCamelCase__=3, UpperCamelCase__=4, UpperCamelCase__="last", UpperCamelCase__=None, UpperCamelCase__=None, ): """simple docstring""" lowerCAmelCase_ = parent lowerCAmelCase_ = batch_size lowerCAmelCase_ = seq_length lowerCAmelCase_ = is_training lowerCAmelCase_ = use_input_lengths lowerCAmelCase_ = use_token_type_ids lowerCAmelCase_ = use_labels lowerCAmelCase_ = gelu_activation lowerCAmelCase_ = sinusoidal_embeddings lowerCAmelCase_ = causal lowerCAmelCase_ = asm lowerCAmelCase_ = n_langs lowerCAmelCase_ = vocab_size lowerCAmelCase_ = n_special lowerCAmelCase_ = hidden_size lowerCAmelCase_ = num_hidden_layers lowerCAmelCase_ = num_attention_heads lowerCAmelCase_ = hidden_dropout_prob lowerCAmelCase_ = attention_probs_dropout_prob lowerCAmelCase_ = max_position_embeddings lowerCAmelCase_ = type_vocab_size lowerCAmelCase_ = type_sequence_label_size lowerCAmelCase_ = initializer_range lowerCAmelCase_ = num_labels lowerCAmelCase_ = num_choices lowerCAmelCase_ = summary_type lowerCAmelCase_ = use_proj lowerCAmelCase_ = scope def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) lowerCAmelCase_ = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase_ = None if self.use_input_lengths: lowerCAmelCase_ = ( ids_tensor([self.batch_size], vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowerCAmelCase_ = None if self.use_token_type_ids: lowerCAmelCase_ = ids_tensor([self.batch_size, self.seq_length], self.n_langs ) lowerCAmelCase_ = None lowerCAmelCase_ = None lowerCAmelCase_ = None if self.use_labels: lowerCAmelCase_ = ids_tensor([self.batch_size], self.type_sequence_label_size ) lowerCAmelCase_ = ids_tensor([self.batch_size, self.seq_length], self.num_labels ) lowerCAmelCase_ = ids_tensor([self.batch_size], 2 ).float() lowerCAmelCase_ = ids_tensor([self.batch_size], self.num_choices ) lowerCAmelCase_ = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" return FlaubertConfig( vocab_size=self.vocab_size, n_special=self.n_special, emb_dim=self.hidden_size, n_layers=self.num_hidden_layers, n_heads=self.num_attention_heads, dropout=self.hidden_dropout_prob, attention_dropout=self.attention_probs_dropout_prob, gelu_activation=self.gelu_activation, sinusoidal_embeddings=self.sinusoidal_embeddings, asm=self.asm, causal=self.causal, n_langs=self.n_langs, max_position_embeddings=self.max_position_embeddings, initializer_range=self.initializer_range, summary_type=self.summary_type, use_proj=self.use_proj, ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = FlaubertModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() lowerCAmelCase_ = model(UpperCamelCase__, lengths=UpperCamelCase__, langs=UpperCamelCase__ ) lowerCAmelCase_ = model(UpperCamelCase__, langs=UpperCamelCase__ ) lowerCAmelCase_ = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = FlaubertWithLMHeadModel(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() lowerCAmelCase_ = model(UpperCamelCase__, token_type_ids=UpperCamelCase__, labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape, () ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = FlaubertForQuestionAnsweringSimple(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() lowerCAmelCase_ = model(UpperCamelCase__ ) lowerCAmelCase_ = model(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 SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = FlaubertForQuestionAnswering(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() lowerCAmelCase_ = model(UpperCamelCase__ ) lowerCAmelCase_ = model( UpperCamelCase__, start_positions=UpperCamelCase__, end_positions=UpperCamelCase__, cls_index=UpperCamelCase__, is_impossible=UpperCamelCase__, p_mask=UpperCamelCase__, ) lowerCAmelCase_ = model( UpperCamelCase__, start_positions=UpperCamelCase__, end_positions=UpperCamelCase__, cls_index=UpperCamelCase__, is_impossible=UpperCamelCase__, ) (lowerCAmelCase_ ) = result_with_labels.to_tuple() lowerCAmelCase_ = model(UpperCamelCase__, start_positions=UpperCamelCase__, end_positions=UpperCamelCase__ ) (lowerCAmelCase_ ) = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape, () ) self.parent.assertEqual(result.start_top_log_probs.shape, (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape, (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape, (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape, (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape, (self.batch_size,) ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = FlaubertForSequenceClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() lowerCAmelCase_ = model(UpperCamelCase__ ) lowerCAmelCase_ = model(UpperCamelCase__, labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape, () ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = self.num_labels lowerCAmelCase_ = FlaubertForTokenClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() lowerCAmelCase_ = model(UpperCamelCase__, attention_mask=UpperCamelCase__, labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = self.num_choices lowerCAmelCase_ = FlaubertForMultipleChoice(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() lowerCAmelCase_ = input_ids.unsqueeze(1 ).expand(-1, self.num_choices, -1 ).contiguous() lowerCAmelCase_ = token_type_ids.unsqueeze(1 ).expand(-1, self.num_choices, -1 ).contiguous() lowerCAmelCase_ = input_mask.unsqueeze(1 ).expand(-1, self.num_choices, -1 ).contiguous() lowerCAmelCase_ = model( UpperCamelCase__, attention_mask=UpperCamelCase__, token_type_ids=UpperCamelCase__, labels=UpperCamelCase__, ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_choices) ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.prepare_config_and_inputs() ( lowerCAmelCase_ ) = config_and_inputs lowerCAmelCase_ = { "input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths, "attention_mask": input_mask, } return config, inputs_dict @require_torch class A ( __UpperCAmelCase , __UpperCAmelCase , unittest.TestCase ): __snake_case = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) __snake_case = ( { "feature-extraction": FlaubertModel, "fill-mask": FlaubertWithLMHeadModel, "question-answering": FlaubertForQuestionAnsweringSimple, "text-classification": FlaubertForSequenceClassification, "token-classification": FlaubertForTokenClassification, "zero-shot": FlaubertForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__ ): """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__=False ): """simple docstring""" lowerCAmelCase_ = super()._prepare_for_class(UpperCamelCase__, UpperCamelCase__, return_labels=UpperCamelCase__ ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": lowerCAmelCase_ = torch.zeros( self.model_tester.batch_size, dtype=torch.long, device=UpperCamelCase__ ) lowerCAmelCase_ = torch.zeros( self.model_tester.batch_size, dtype=torch.long, device=UpperCamelCase__ ) return inputs_dict def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = FlaubertModelTester(self ) lowerCAmelCase_ = ConfigTester(self, config_class=UpperCamelCase__, emb_dim=37 ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*UpperCamelCase__ ) @slow def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase_ = FlaubertModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @slow @require_torch_gpu def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return lowerCAmelCase_ = True lowerCAmelCase_ = model_class(config=UpperCamelCase__ ) lowerCAmelCase_ = self._prepare_for_class(UpperCamelCase__, UpperCamelCase__ ) lowerCAmelCase_ = 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__, '''traced_model.pt''' ) ) lowerCAmelCase_ = torch.jit.load(os.path.join(UpperCamelCase__, '''traced_model.pt''' ), map_location=UpperCamelCase__ ) loaded(inputs_dict['''input_ids'''].to(UpperCamelCase__ ), inputs_dict['''attention_mask'''].to(UpperCamelCase__ ) ) @require_torch class A ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased''' ) lowerCAmelCase_ = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) with torch.no_grad(): lowerCAmelCase_ = model(UpperCamelCase__ )[0] lowerCAmelCase_ = torch.Size((1, 11, 768) ) self.assertEqual(output.shape, UpperCamelCase__ ) lowerCAmelCase_ = torch.tensor( [[[-2.6_251, -1.4_298, -0.0_227], [-2.8_510, -1.6_387, 0.2_258], [-2.8_114, -1.1_832, -0.3_066]]] ) self.assertTrue(torch.allclose(output[:, :3, :3], UpperCamelCase__, atol=1E-4 ) )
278
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" @slow def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' __snake_case : Tuple = FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small" ) __snake_case : str = AutoTokenizer.from_pretrained("google/mt5-small" ) __snake_case : List[Any] = tokenizer("Hello there" , return_tensors="np" ).input_ids __snake_case : int = tokenizer("Hi I am" , return_tensors="np" ).input_ids __snake_case : Tuple = shift_tokens_right(UpperCAmelCase , model.config.pad_token_id , model.config.decoder_start_token_id ) __snake_case : Tuple = model(UpperCAmelCase , decoder_input_ids=UpperCAmelCase ).logits __snake_case : str = optax.softmax_cross_entropy(UpperCAmelCase , onehot(UpperCAmelCase , logits.shape[-1] ) ).mean() __snake_case : Any = -(labels.shape[-1] * loss.item()) __snake_case : List[str] = -84.9_127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
326
0
'''simple docstring''' def _a( UpperCamelCase__ : str, UpperCamelCase__ : list[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict ="" for word_or_phrase in separated: if not isinstance(UpperCamelCase__, UpperCamelCase__ ): raise Exception('''join() accepts only strings to be joined''' ) joined += word_or_phrase + separator return joined.strip(UpperCamelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
152
import torch from torch import nn from transformers import CLIPPreTrainedModel, CLIPVisionModel from ...models.attention import BasicTransformerBlock from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name class _lowerCamelCase ( a ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=768 ) -> List[str]: '''simple docstring''' super().__init__(UpperCAmelCase ) __snake_case : Optional[int] = proj_size __snake_case : str = CLIPVisionModel(UpperCAmelCase ) __snake_case : Tuple = PaintByExampleMapper(UpperCAmelCase ) __snake_case : Union[str, Any] = nn.LayerNorm(config.hidden_size ) __snake_case : Optional[Any] = nn.Linear(config.hidden_size , self.proj_size ) # uncondition for scaling __snake_case : Optional[int] = nn.Parameter(torch.randn((1, 1, self.proj_size) ) ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase=False ) -> List[str]: '''simple docstring''' __snake_case : int = self.model(pixel_values=UpperCAmelCase ) __snake_case : Optional[int] = clip_output.pooler_output __snake_case : Any = self.mapper(latent_states[:, None] ) __snake_case : Any = self.final_layer_norm(UpperCAmelCase ) __snake_case : str = self.proj_out(UpperCAmelCase ) if return_uncond_vector: return latent_states, self.uncond_vector return latent_states class _lowerCamelCase ( nn.Module ): """simple docstring""" def __init__( self , UpperCAmelCase ) -> List[Any]: '''simple docstring''' super().__init__() __snake_case : List[Any] = (config.num_hidden_layers + 1) // 5 __snake_case : Dict = config.hidden_size __snake_case : str = 1 __snake_case : List[Any] = nn.ModuleList( [ BasicTransformerBlock(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , activation_fn="gelu" , attention_bias=UpperCAmelCase ) for _ in range(UpperCAmelCase ) ] ) def UpperCAmelCase ( self , UpperCAmelCase ) -> str: '''simple docstring''' for block in self.blocks: __snake_case : int = block(UpperCAmelCase ) return hidden_states
326
0
"""simple docstring""" from __future__ import annotations def a_ ( _lowerCAmelCase : str , _lowerCAmelCase : list[str] | None = None ): '''simple docstring''' lowercase__ : List[str] = word_bank or [] # create a table lowercase__ : int = len(_lowerCAmelCase ) + 1 lowercase__ : list[list[list[str]]] = [] for _ in range(_lowerCAmelCase ): table.append([] ) # seed value lowercase__ : Optional[int] = [[]] # because empty string has empty combination # iterate through the indices for i in range(_lowerCAmelCase ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(_lowerCAmelCase )] == word: lowercase__ : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(_lowerCAmelCase )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(_lowerCAmelCase )]: combination.reverse() return table[len(_lowerCAmelCase )] if __name__ == "__main__": print(all_construct("jwajalapa", ["jwa", "j", "w", "a", "la", "lapa"])) print(all_construct("rajamati", ["s", "raj", "amat", "raja", "ma", "i", "t"])) print( all_construct( "hexagonosaurus", ["h", "ex", "hex", "ag", "ago", "ru", "auru", "rus", "go", "no", "o", "s"], ) )
77
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
326
0
def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' return "\n".join( f"""{number} * {i} = {number * i}""" for i in range(1 , number_of_terms + 1 ) ) if __name__ == "__main__": print(multiplication_table(number=5, number_of_terms=10))
43
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = torch.device('''cpu''') def lowerCAmelCase__( ) -> Any: __snake_case : List[Any] = "http://images.cocodataset.org/val2017/000000039769.jpg" __snake_case : Optional[int] = Image.open(requests.get(lowercase , stream=lowercase ).raw ) return im def lowerCAmelCase__( lowercase : Dict ) -> List[Any]: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1_703E00, 2.1_107E00, -2.0_811E00, 8.8_685E-01, 2.4_360E-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9_636E-01, 2.3_478E-01, -1.6_963E00, -1.7_381E00, -8.6_337E-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2_768E-01, -4.7_429E-01, -1.0_897E00, -1.0_248E00, 3.5_523E-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5_330E-01, 2.4_211E-01, -6.0_185E-01, -8.2_789E-01, -6.0_446E-02] ) def lowerCAmelCase__( lowercase : Tuple , lowercase : Union[str, Any] , lowercase : Union[str, Any] ) -> List[Any]: __snake_case : List[Any] = dct.pop(lowercase ) __snake_case : List[Any] = val def lowerCAmelCase__( lowercase : Union[str, Any] ) -> Tuple: __snake_case : Optional[Any] = [] for k in state_dict.keys(): __snake_case : Union[str, Any] = k if ".pwconv" in k: __snake_case : Any = k_new.replace(".pwconv" , ".point_wise_conv" ) if ".dwconv" in k: __snake_case : List[Any] = k_new.replace(".dwconv" , ".depth_wise_conv" ) if ".Proj." in k: __snake_case : Optional[int] = k_new.replace(".Proj." , ".proj." ) if "patch_embed" in k_new: __snake_case : int = k_new.replace("patch_embed" , "swiftformer.patch_embed.patch_embedding" ) if "network" in k_new: __snake_case : int = k_new.split("." ) if ls[2].isdigit(): __snake_case : List[Any] = "swiftformer.encoder.network." + ls[1] + ".blocks." + ls[2] + "." + ".".join(ls[3:] ) else: __snake_case : Optional[int] = k_new.replace("network" , "swiftformer.encoder.network" ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def lowerCAmelCase__( lowercase : List[Any] , lowercase : Optional[Any] , lowercase : List[str] ) -> Union[str, Any]: __snake_case : List[str] = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size __snake_case : Tuple = 1000 __snake_case : Any = "huggingface/label-files" __snake_case : int = "imagenet-1k-id2label.json" __snake_case : Dict = json.load(open(hf_hub_download(lowercase , lowercase , repo_type="dataset" ) , "r" ) ) __snake_case : str = {int(lowercase ): v for k, v in idalabel.items()} __snake_case : int = idalabel __snake_case : Optional[int] = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": __snake_case : Optional[Any] = [3, 3, 6, 4] __snake_case : Optional[int] = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": __snake_case : List[str] = [3, 3, 9, 6] __snake_case : Optional[Any] = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": __snake_case : Optional[int] = [4, 3, 10, 5] __snake_case : Dict = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": __snake_case : str = [4, 4, 12, 6] __snake_case : Optional[Any] = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith("https" ): __snake_case : Optional[Any] = torch.hub.load_state_dict_from_url(lowercase , map_location="cpu" , check_hash=lowercase ) else: __snake_case : Tuple = torch.load(lowercase , map_location="cpu" ) __snake_case : Optional[int] = checkpoint __snake_case : Any = create_rename_keys(lowercase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(lowercase , lowercase , lowercase ) # load HuggingFace model __snake_case : Tuple = SwiftFormerForImageClassification(lowercase ).eval() hf_model.load_state_dict(lowercase ) # prepare test inputs __snake_case : Optional[Any] = prepare_img() __snake_case : str = ViTImageProcessor.from_pretrained("preprocessor_config" ) __snake_case : Optional[int] = processor(images=lowercase , return_tensors="pt" ) # compare outputs from both models __snake_case : str = get_expected_output(lowercase ) __snake_case : Optional[int] = hf_model(inputs["pixel_values"] ).logits assert hf_logits.shape == torch.Size([1, 1000] ) assert torch.allclose(hf_logits[0, 0:5] , lowercase , atol=1E-3 ) Path(lowercase ).mkdir(exist_ok=lowercase ) print(f"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(lowercase ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--swiftformer_name''', default='''swiftformer_xs''', choices=['''swiftformer_xs''', '''swiftformer_s''', '''swiftformer_l1''', '''swiftformer_l3'''], type=str, help='''Name of the SwiftFormer model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''./converted_outputs/''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--original_ckpt''', default=None, type=str, help='''Path to the original model checkpoint.''') _UpperCamelCase = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
326
0
'''simple docstring''' import math from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : List[str] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Dict = { "facebook/data2vec-base-960h": "https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json", # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class _snake_case ( lowercase_ ): lowerCAmelCase_ : str = "data2vec-audio" def __init__( self , a__=32 , a__=768 , a__=12 , a__=12 , a__=3_072 , a__="gelu" , a__=0.1 , a__=0.1 , a__=0.1 , a__=0.0 , a__=0.1 , a__=0.1 , a__=0.0_2 , a__=1e-5 , a__="gelu" , a__=(512, 512, 512, 512, 512, 512, 512) , a__=(5, 2, 2, 2, 2, 2, 2) , a__=(10, 3, 3, 3, 3, 2, 2) , a__=False , a__=16 , a__=19 , a__=5 , a__=0.0_5 , a__=10 , a__=2 , a__=0.0 , a__=10 , a__=0 , a__="sum" , a__=False , a__=False , a__=256 , a__=(512, 512, 512, 512, 1_500) , a__=(5, 3, 3, 1, 1) , a__=(1, 2, 3, 1, 1) , a__=512 , a__=0 , a__=1 , a__=2 , a__=False , a__=3 , a__=2 , a__=3 , a__=None , **a__ , ) -> str: '''simple docstring''' super().__init__(**a__ , pad_token_id=a__ , bos_token_id=a__ , eos_token_id=a__ ) snake_case_ = hidden_size snake_case_ = feat_extract_activation snake_case_ = list(a__ ) snake_case_ = list(a__ ) snake_case_ = list(a__ ) snake_case_ = conv_bias snake_case_ = num_conv_pos_embeddings snake_case_ = num_conv_pos_embedding_groups snake_case_ = conv_pos_kernel_size snake_case_ = len(self.conv_dim ) snake_case_ = num_hidden_layers snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = num_attention_heads snake_case_ = hidden_dropout snake_case_ = attention_dropout snake_case_ = activation_dropout snake_case_ = feat_proj_dropout snake_case_ = final_dropout snake_case_ = layerdrop snake_case_ = layer_norm_eps snake_case_ = initializer_range snake_case_ = vocab_size snake_case_ = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==" " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =" F' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,' F' `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 snake_case_ = mask_time_prob snake_case_ = mask_time_length snake_case_ = mask_time_min_masks snake_case_ = mask_feature_prob snake_case_ = mask_feature_length snake_case_ = mask_feature_min_masks # ctc loss snake_case_ = ctc_loss_reduction snake_case_ = ctc_zero_infinity # adapter snake_case_ = add_adapter snake_case_ = adapter_kernel_size snake_case_ = adapter_stride snake_case_ = num_adapter_layers snake_case_ = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. snake_case_ = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. snake_case_ = list(a__ ) snake_case_ = list(a__ ) snake_case_ = list(a__ ) snake_case_ = xvector_output_dim @property def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' return math.prod(self.conv_stride )
85
import json import logging import os import socket import git import numpy as np import torch logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO, ) _UpperCamelCase = logging.getLogger(__name__) def lowerCAmelCase__( lowercase : str ) -> List[str]: __snake_case : int = git.Repo(search_parent_directories=lowercase ) __snake_case : Union[str, Any] = { "repo_id": str(lowercase ), "repo_sha": str(repo.head.object.hexsha ), "repo_branch": str(repo.active_branch ), } with open(os.path.join(lowercase , "git_log.json" ) , "w" ) as f: json.dump(lowercase , lowercase , indent=4 ) def lowerCAmelCase__( lowercase : Optional[Any] ) -> Optional[Any]: if params.n_gpu <= 0: __snake_case : Union[str, Any] = 0 __snake_case : Optional[int] = -1 __snake_case : Union[str, Any] = True __snake_case : Tuple = False return assert torch.cuda.is_available() logger.info("Initializing GPUs" ) if params.n_gpu > 1: assert params.local_rank != -1 __snake_case : Optional[int] = int(os.environ["WORLD_SIZE"] ) __snake_case : int = int(os.environ["N_GPU_NODE"] ) __snake_case : Union[str, Any] = int(os.environ["RANK"] ) # number of nodes / node ID __snake_case : Optional[Any] = params.world_size // params.n_gpu_per_node __snake_case : Optional[Any] = params.global_rank // params.n_gpu_per_node __snake_case : Union[str, Any] = True assert params.n_nodes == int(os.environ["N_NODES"] ) assert params.node_id == int(os.environ["NODE_RANK"] ) # local job (single GPU) else: assert params.local_rank == -1 __snake_case : Any = 1 __snake_case : str = 0 __snake_case : Optional[Any] = 0 __snake_case : Dict = 0 __snake_case : int = 1 __snake_case : Optional[Any] = 1 __snake_case : Tuple = False # sanity checks assert params.n_nodes >= 1 assert 0 <= params.node_id < params.n_nodes assert 0 <= params.local_rank <= params.global_rank < params.world_size assert params.world_size == params.n_nodes * params.n_gpu_per_node # define whether this is the master process / if we are in multi-node distributed mode __snake_case : List[Any] = params.node_id == 0 and params.local_rank == 0 __snake_case : List[Any] = params.n_nodes > 1 # summary __snake_case : List[Any] = f"""--- Global rank: {params.global_rank} - """ logger.info(PREFIX + "Number of nodes: %i" % params.n_nodes ) logger.info(PREFIX + "Node ID : %i" % params.node_id ) logger.info(PREFIX + "Local rank : %i" % params.local_rank ) logger.info(PREFIX + "World size : %i" % params.world_size ) logger.info(PREFIX + "GPUs per node : %i" % params.n_gpu_per_node ) logger.info(PREFIX + "Master : %s" % str(params.is_master ) ) logger.info(PREFIX + "Multi-node : %s" % str(params.multi_node ) ) logger.info(PREFIX + "Multi-GPU : %s" % str(params.multi_gpu ) ) logger.info(PREFIX + "Hostname : %s" % socket.gethostname() ) # set GPU device torch.cuda.set_device(params.local_rank ) # initialize multi-GPU if params.multi_gpu: logger.info("Initializing PyTorch distributed" ) torch.distributed.init_process_group( init_method="env://" , backend="nccl" , ) def lowerCAmelCase__( lowercase : Dict ) -> Union[str, Any]: np.random.seed(args.seed ) torch.manual_seed(args.seed ) if args.n_gpu > 0: torch.cuda.manual_seed_all(args.seed )
326
0
"""simple docstring""" import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class a ( __snake_case ): def UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: lowerCamelCase_ = tempfile.mkdtemp() lowerCamelCase_ = 8 # DPR tok lowerCamelCase_ = [ "[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] lowerCamelCase_ = os.path.join(self.tmpdirname , 'dpr_tokenizer' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) # BART tok lowerCamelCase_ = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] lowerCamelCase_ = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) lowerCamelCase_ = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] lowerCamelCase_ = {"unk_token": "<unk>"} lowerCamelCase_ = os.path.join(self.tmpdirname , 'bart_tokenizer' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['vocab_file'] ) lowerCamelCase_ = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(__SCREAMING_SNAKE_CASE ) ) def UpperCamelCase ( self : str ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def UpperCamelCase ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer' ) ) def UpperCamelCase ( self : Tuple ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) @require_tokenizers def UpperCamelCase ( self : Optional[Any] ) -> List[Any]: lowerCamelCase_ = os.path.join(self.tmpdirname , 'rag_tokenizer' ) lowerCamelCase_ = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) lowerCamelCase_ = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(__SCREAMING_SNAKE_CASE ) rag_tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = RagTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , config=__SCREAMING_SNAKE_CASE ) self.assertIsInstance(new_rag_tokenizer.question_encoder , __SCREAMING_SNAKE_CASE ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , __SCREAMING_SNAKE_CASE ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def UpperCamelCase ( self : Dict ) -> List[str]: lowerCamelCase_ = RagTokenizer.from_pretrained('facebook/rag-token-nq' ) lowerCamelCase_ = [ "who got the first nobel prize in physics", "when is the next deadpool movie being released", "which mode is used for short wave broadcast service", "who is the owner of reading football club", "when is the next scandal episode coming out", "when is the last time the philadelphia won the superbowl", "what is the most current adobe flash player version", "how many episodes are there in dragon ball z", "what is the first step in the evolution of the eye", "where is gall bladder situated in human body", "what is the main mineral in lithium batteries", "who is the president of usa right now", "where do the greasers live in the outsiders", "panda is a national animal of which country", "what is the name of manchester united stadium", ] lowerCamelCase_ = tokenizer(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @slow def UpperCamelCase ( self : List[Any] ) -> List[Any]: lowerCamelCase_ = RagTokenizer.from_pretrained('facebook/rag-sequence-nq' ) lowerCamelCase_ = [ "who got the first nobel prize in physics", "when is the next deadpool movie being released", "which mode is used for short wave broadcast service", "who is the owner of reading football club", "when is the next scandal episode coming out", "when is the last time the philadelphia won the superbowl", "what is the most current adobe flash player version", "how many episodes are there in dragon ball z", "what is the first step in the evolution of the eye", "where is gall bladder situated in human body", "what is the main mineral in lithium batteries", "who is the president of usa right now", "where do the greasers live in the outsiders", "panda is a national animal of which country", "what is the name of manchester united stadium", ] lowerCamelCase_ = tokenizer(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE )
183
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : str =JukeboxTokenizer UpperCAmelCase_ : Tuple ={ "artist": "Zac Brown Band", "genres": "Country", "lyrics": "I met a traveller from an antique land,\n Who said \"Two vast and trunkless legs of stone\n Stand in the desert. . . . Near them, on the sand,\n Half sunk a shattered visage lies, whose frown,\n And wrinkled lip, and sneer of cold command,\n Tell that its sculptor well those passions read\n Which yet survive, stamped on these lifeless things,\n The hand that mocked them, and the heart that fed;\n And on the pedestal, these words appear:\n My name is Ozymandias, King of Kings;\n Look on my Works, ye Mighty, and despair!\n Nothing beside remains. Round the decay\n Of that colossal Wreck, boundless and bare\n The lone and level sands stretch far away\n ", } @require_torch def UpperCAmelCase ( self ) -> str: '''simple docstring''' import torch __snake_case : List[str] = JukeboxTokenizer.from_pretrained("openai/jukebox-1b-lyrics" ) __snake_case : Union[str, Any] = tokenizer(**self.metas )["input_ids"] # fmt: off __snake_case : Optional[Any] = [ torch.tensor([[ 0, 0, 0, 7169, 507, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) ) @require_torch def UpperCAmelCase ( self ) -> str: '''simple docstring''' import torch __snake_case : Optional[Any] = JukeboxTokenizer.from_pretrained("openai/jukebox-5b-lyrics" ) __snake_case : Tuple = tokenizer(**self.metas )["input_ids"] # fmt: off __snake_case : int = [ torch.tensor([[ 0, 0, 0, 1069, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) )
326
0
import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase : Tuple = logging.get_logger(__name__) __lowerCAmelCase : List[Any] = [ ['attention', 'attn'], ['encoder_attention', 'encoder_attn'], ['q_lin', 'q_proj'], ['k_lin', 'k_proj'], ['v_lin', 'v_proj'], ['out_lin', 'out_proj'], ['norm_embeddings', 'layernorm_embedding'], ['position_embeddings', 'embed_positions'], ['embeddings', 'embed_tokens'], ['ffn.lin', 'fc'], ] def a__ ( A_ ): '''simple docstring''' if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: __magic_name__ = k.replace(A_, A_ ) if k.startswith("""encoder""" ): __magic_name__ = k.replace(""".attn""", """.self_attn""" ) __magic_name__ = k.replace("""norm1""", """self_attn_layer_norm""" ) __magic_name__ = k.replace("""norm2""", """final_layer_norm""" ) elif k.startswith("""decoder""" ): __magic_name__ = k.replace("""norm1""", """self_attn_layer_norm""" ) __magic_name__ = k.replace("""norm2""", """encoder_attn_layer_norm""" ) __magic_name__ = k.replace("""norm3""", """final_layer_norm""" ) return k def a__ ( A_ ): '''simple docstring''' __magic_name__ = [ "model.encoder.layernorm_embedding.weight", "model.encoder.layernorm_embedding.bias", "model.decoder.layernorm_embedding.weight", "model.decoder.layernorm_embedding.bias", ] for k in keys: __magic_name__ = sd.pop(A_ ) __magic_name__ = k.replace("""layernorm_embedding""", """layer_norm""" ) assert new_k not in sd __magic_name__ = v __lowerCAmelCase : Union[str, Any] = ['START'] @torch.no_grad() def a__ ( A_, A_, A_ ): '''simple docstring''' __magic_name__ = torch.load(A_, map_location="""cpu""" ) __magic_name__ = model["model"] __magic_name__ = BlenderbotConfig.from_json_file(A_ ) __magic_name__ = BlenderbotForConditionalGeneration(A_ ) __magic_name__ = m.model.state_dict().keys() __magic_name__ = [] __magic_name__ = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue __magic_name__ = rename_state_dict_key(A_ ) if new_k not in valid_keys: failures.append([k, new_k] ) else: __magic_name__ = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(A_ ) m.model.load_state_dict(A_, strict=A_ ) m.half() m.save_pretrained(A_ ) if __name__ == "__main__": __lowerCAmelCase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('--src_path', type=str, help='like blenderbot-model.bin') parser.add_argument('--save_dir', default='hf_blenderbot', type=str, help='Where to save converted model.') parser.add_argument( '--hf_config_json', default='blenderbot-3b-config.json', type=str, help='Path to config to use' ) __lowerCAmelCase : Tuple = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
88
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 _UpperCamelCase = logging.get_logger(__name__) class _lowerCamelCase : """simple docstring""" UpperCAmelCase_ : str UpperCAmelCase_ : str =None @staticmethod def UpperCAmelCase ( ) -> Optional[int]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> List[str]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self , UpperCAmelCase ) -> Optional[int]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self ) -> Dict: '''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 UpperCAmelCase ( cls ) -> Tuple: '''simple docstring''' return F"""`pip install {cls.pip_package or cls.name}`""" class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Optional[int] ="optuna" @staticmethod def UpperCAmelCase ( ) -> Union[str, Any]: '''simple docstring''' return is_optuna_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: '''simple docstring''' return run_hp_search_optuna(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> int: '''simple docstring''' return default_hp_space_optuna(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : List[str] ="ray" UpperCAmelCase_ : Dict ="'ray[tune]'" @staticmethod def UpperCAmelCase ( ) -> str: '''simple docstring''' return is_ray_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> List[Any]: '''simple docstring''' return run_hp_search_ray(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> str: '''simple docstring''' return default_hp_space_ray(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Tuple ="sigopt" @staticmethod def UpperCAmelCase ( ) -> int: '''simple docstring''' return is_sigopt_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return run_hp_search_sigopt(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> Dict: '''simple docstring''' return default_hp_space_sigopt(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : str ="wandb" @staticmethod def UpperCAmelCase ( ) -> Optional[Any]: '''simple docstring''' return is_wandb_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' return run_hp_search_wandb(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> List[str]: '''simple docstring''' return default_hp_space_wandb(UpperCAmelCase ) _UpperCamelCase = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def lowerCAmelCase__( ) -> str: __snake_case : Optional[int] = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(lowercase ) > 0: __snake_case : Dict = available_backends[0].name if len(lowercase ) > 1: logger.info( f"""{len(lowercase )} 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() ) )
326
0
import random from typing import Any def A ( lowercase ) -> list[Any]: '''simple docstring''' for _ in range(len(lowercase ) ): UpperCamelCase = random.randint(0 , len(lowercase ) - 1 ) UpperCamelCase = random.randint(0 , len(lowercase ) - 1 ) UpperCamelCase = data[b], data[a] return data if __name__ == "__main__": _UpperCAmelCase : Optional[int] = [0, 1, 2, 3, 4, 5, 6, 7] _UpperCAmelCase : int = ["python", "says", "hello", "!"] print("Fisher-Yates Shuffle:") print("List", integers, strings) print("FY Shuffle", fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))
222
import math def lowerCAmelCase__( lowercase : list , lowercase : int = 0 , lowercase : int = 0 ) -> list: __snake_case : Any = end or len(lowercase ) for i in range(lowercase , lowercase ): __snake_case : List[str] = i __snake_case : Union[str, Any] = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __snake_case : Optional[Any] = array[temp_index - 1] temp_index -= 1 __snake_case : Any = temp_index_value return array def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int ) -> None: # Max Heap __snake_case : Any = index __snake_case : Optional[Any] = 2 * index + 1 # Left Node __snake_case : str = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __snake_case : Optional[int] = left_index if right_index < heap_size and array[largest] < array[right_index]: __snake_case : Tuple = right_index if largest != index: __snake_case , __snake_case : int = array[largest], array[index] heapify(lowercase , lowercase , lowercase ) def lowerCAmelCase__( lowercase : list ) -> list: __snake_case : List[str] = len(lowercase ) for i in range(n // 2 , -1 , -1 ): heapify(lowercase , lowercase , lowercase ) for i in range(n - 1 , 0 , -1 ): __snake_case , __snake_case : Optional[Any] = array[0], array[i] heapify(lowercase , 0 , lowercase ) return array def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int ) -> int: if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int ) -> int: __snake_case : Union[str, Any] = low __snake_case : Union[str, Any] = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __snake_case , __snake_case : str = array[j], array[i] i += 1 def lowerCAmelCase__( lowercase : list ) -> list: if len(lowercase ) == 0: return array __snake_case : Union[str, Any] = 2 * math.ceil(math.loga(len(lowercase ) ) ) __snake_case : Dict = 16 return intro_sort(lowercase , 0 , len(lowercase ) , lowercase , lowercase ) def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int , lowercase : int ) -> list: while end - start > size_threshold: if max_depth == 0: return heap_sort(lowercase ) max_depth -= 1 __snake_case : List[str] = median_of_a(lowercase , lowercase , start + ((end - start) // 2) + 1 , end - 1 ) __snake_case : Optional[Any] = partition(lowercase , lowercase , lowercase , lowercase ) intro_sort(lowercase , lowercase , lowercase , lowercase , lowercase ) __snake_case : List[str] = p return insertion_sort(lowercase , lowercase , lowercase ) if __name__ == "__main__": import doctest doctest.testmod() _UpperCamelCase = input('''Enter numbers separated by a comma : ''').strip() _UpperCamelCase = [float(item) for item in user_input.split(''',''')] print(sort(unsorted))
326
0
from datetime import datetime import requests def _lowercase ( UpperCamelCase_ ) -> bytes: '''simple docstring''' SCREAMING_SNAKE_CASE__ = "https://downloadgram.net/wp-json/wppress/video-downloader/video?url=" SCREAMING_SNAKE_CASE__ = requests.get(base_url + url ).json()[0]["urls"][0]["src"] return requests.get(UpperCamelCase_ ).content if __name__ == "__main__": __snake_case = input("""Enter Video/IGTV url: """).strip() __snake_case = F"""{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4""" with open(file_name, """wb""") as fp: fp.write(download_video(url)) print(F"""Done. Video saved to disk as {file_name}.""")
176
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def lowerCAmelCase__( lowercase : Dict ) -> str: # picklable for multiprocessing return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def lowerCAmelCase__( ) -> List[Any]: with parallel_backend("spark" ): assert ParallelBackendConfig.backend_name == "spark" __snake_case : Any = [1, 2, 3] with pytest.raises(lowercase ): with parallel_backend("unsupported backend" ): map_nested(lowercase , lowercase , num_proc=2 ) with pytest.raises(lowercase ): with parallel_backend("unsupported backend" ): map_nested(lowercase , lowercase , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("num_proc" , [2, -1] ) def lowerCAmelCase__( lowercase : Dict ) -> Dict: __snake_case : Any = [1, 2] __snake_case : Dict = {"a": 1, "b": 2} __snake_case : Optional[int] = {"a": [1, 2], "b": [3, 4]} __snake_case : int = {"a": {"1": 1}, "b": 2} __snake_case : str = {"a": 1, "b": 2, "c": 3, "d": 4} __snake_case : Dict = [2, 3] __snake_case : Tuple = {"a": 2, "b": 3} __snake_case : int = {"a": [2, 3], "b": [4, 5]} __snake_case : Dict = {"a": {"1": 2}, "b": 3} __snake_case : str = {"a": 2, "b": 3, "c": 4, "d": 5} with parallel_backend("spark" ): assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa
326
0
"""simple docstring""" import argparse import hashlib # hashlib is only used inside the Test class import struct class UpperCAmelCase_ : def __init__( self : List[Any] , __UpperCamelCase : Optional[Any] ) -> Any: _UpperCamelCase = data _UpperCamelCase = [0x6745_2301, 0xefcd_ab89, 0x98ba_dcfe, 0x1032_5476, 0xc3d2_e1f0] @staticmethod def _UpperCamelCase ( __UpperCamelCase : Any , __UpperCamelCase : int ) -> str: return ((n << b) | (n >> (32 - b))) & 0xffff_ffff def _UpperCamelCase ( self : Any ) -> Optional[int]: _UpperCamelCase = B"\x80" + B"\x00" * (63 - (len(self.data ) + 8) % 64) _UpperCamelCase = self.data + padding + struct.pack('''>Q''' , 8 * len(self.data ) ) return padded_data def _UpperCamelCase ( self : List[Any] ) -> int: return [ self.padded_data[i : i + 64] for i in range(0 , len(self.padded_data ) , 64 ) ] def _UpperCamelCase ( self : Any , __UpperCamelCase : int ) -> List[str]: _UpperCamelCase = list(struct.unpack('''>16L''' , __UpperCamelCase ) ) + [0] * 64 for i in range(16 , 80 ): _UpperCamelCase = self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]) , 1 ) return w def _UpperCamelCase ( self : Union[str, Any] ) -> List[str]: _UpperCamelCase = self.padding() _UpperCamelCase = self.split_blocks() for block in self.blocks: _UpperCamelCase = self.expand_block(__UpperCamelCase ) _UpperCamelCase = self.h for i in range(0 , 80 ): if 0 <= i < 20: _UpperCamelCase = (b & c) | ((~b) & d) _UpperCamelCase = 0x5a82_7999 elif 20 <= i < 40: _UpperCamelCase = b ^ c ^ d _UpperCamelCase = 0x6ed9_eba1 elif 40 <= i < 60: _UpperCamelCase = (b & c) | (b & d) | (c & d) _UpperCamelCase = 0x8f1b_bcdc elif 60 <= i < 80: _UpperCamelCase = b ^ c ^ d _UpperCamelCase = 0xca62_c1d6 _UpperCamelCase = ( self.rotate(__UpperCamelCase , 5 ) + f + e + k + expanded_block[i] & 0xffff_ffff, a, self.rotate(__UpperCamelCase , 30 ), c, d, ) _UpperCamelCase = ( self.h[0] + a & 0xffff_ffff, self.h[1] + b & 0xffff_ffff, self.h[2] + c & 0xffff_ffff, self.h[3] + d & 0xffff_ffff, self.h[4] + e & 0xffff_ffff, ) return ("{:08x}" * 5).format(*self.h ) def lowercase ( ) -> str: _UpperCamelCase = B"Test String" assert SHAaHash(a__ ).final_hash() == hashlib.shaa(a__ ).hexdigest() # noqa: S324 def lowercase ( ) -> str: _UpperCamelCase = argparse.ArgumentParser(description='''Process some strings or files''' ) parser.add_argument( '''--string''' , dest='''input_string''' , default='''Hello World!! Welcome to Cryptography''' , help='''Hash the string''' , ) parser.add_argument('''--file''' , dest='''input_file''' , help='''Hash contents of a file''' ) _UpperCamelCase = parser.parse_args() _UpperCamelCase = args.input_string # In any case hash input should be a bytestring if args.input_file: with open(args.input_file , '''rb''' ) as f: _UpperCamelCase = f.read() else: _UpperCamelCase = bytes(a__ , '''utf-8''' ) print(SHAaHash(a__ ).final_hash() ) if __name__ == "__main__": main() import doctest doctest.testmod()
256
import math import random from typing import Any from .hill_climbing import SearchProblem def lowerCAmelCase__( lowercase : Dict , lowercase : bool = True , lowercase : float = math.inf , lowercase : float = -math.inf , lowercase : float = math.inf , lowercase : float = -math.inf , lowercase : bool = False , lowercase : float = 100 , lowercase : float = 0.0_1 , lowercase : float = 1 , ) -> Any: __snake_case : Optional[Any] = False __snake_case : Optional[Any] = search_prob __snake_case : str = start_temperate __snake_case : List[Any] = [] __snake_case : str = 0 __snake_case : Dict = None while not search_end: __snake_case : List[Any] = current_state.score() if best_state is None or current_score > best_state.score(): __snake_case : List[Any] = current_state scores.append(lowercase ) iterations += 1 __snake_case : Dict = None __snake_case : str = current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to __snake_case : Any = random.randint(0 , len(lowercase ) - 1 ) # picking a random neighbor __snake_case : int = neighbors.pop(lowercase ) __snake_case : Optional[Any] = picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: __snake_case : Any = change * -1 # in case we are finding minimum if change > 0: # improves the solution __snake_case : List[str] = picked_neighbor else: __snake_case : Optional[Any] = (math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability __snake_case : str = picked_neighbor __snake_case : Optional[Any] = current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor __snake_case : Optional[Any] = True else: __snake_case : str = next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(lowercase ) , lowercase ) plt.xlabel("Iterations" ) plt.ylabel("Function values" ) plt.show() return best_state if __name__ == "__main__": def lowerCAmelCase__( lowercase : List[str] , lowercase : Tuple ) -> str: return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) _UpperCamelCase = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing( prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) # starting the problem with initial coordinates (12, 47) _UpperCamelCase = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing( prob, find_max=True, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) def lowerCAmelCase__( lowercase : Any , lowercase : Union[str, Any] ) -> Any: return (3 * x**2) - (6 * y) _UpperCamelCase = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing(prob, find_max=False, visualization=True) print( '''The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' F'''{local_min.score()}''' ) _UpperCamelCase = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing(prob, find_max=True, visualization=True) print( '''The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' F'''{local_min.score()}''' )
326
0
import mpmath # for roots of unity import numpy as np class __lowercase : """simple docstring""" def __init__( self : List[str] , lowerCAmelCase__ : str=None , lowerCAmelCase__ : Optional[Any]=None): SCREAMING_SNAKE_CASE_: Tuple = list(poly_a or [0])[:] SCREAMING_SNAKE_CASE_: Dict = list(poly_b or [0])[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() SCREAMING_SNAKE_CASE_: str = len(self.polyA) while self.polyB[-1] == 0: self.polyB.pop() SCREAMING_SNAKE_CASE_: int = len(self.polyB) # Add 0 to make lengths equal a power of 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = int( 2 ** np.ceil(np.loga(len(self.polyA) + len(self.polyB) - 1))) while len(self.polyA) < self.c_max_length: self.polyA.append(0) while len(self.polyB) < self.c_max_length: self.polyB.append(0) # A complex root used for the fourier transform SCREAMING_SNAKE_CASE_: Tuple = complex(mpmath.root(x=1 , n=self.c_max_length , k=1)) # The product SCREAMING_SNAKE_CASE_: str = self.__multiply() def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_: Dict = [[x] for x in self.polyA] if which == "A" else [[x] for x in self.polyB] # Corner case if len(lowerCAmelCase__) <= 1: return dft[0] # SCREAMING_SNAKE_CASE_: str = self.c_max_length // 2 while next_ncol > 0: SCREAMING_SNAKE_CASE_: str = [[] for i in range(lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: Optional[int] = self.root**next_ncol # First half of next step SCREAMING_SNAKE_CASE_: Tuple = 1 for j in range(self.c_max_length // (next_ncol * 2)): for i in range(lowerCAmelCase__): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j]) current_root *= root # Second half of next step SCREAMING_SNAKE_CASE_: str = 1 for j in range(self.c_max_length // (next_ncol * 2)): for i in range(lowerCAmelCase__): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j]) current_root *= root # Update SCREAMING_SNAKE_CASE_: Union[str, Any] = new_dft SCREAMING_SNAKE_CASE_: Any = next_ncol // 2 return dft[0] def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: str = self.__dft("A") SCREAMING_SNAKE_CASE_: Dict = self.__dft("B") SCREAMING_SNAKE_CASE_: List[str] = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length)]] del dft_a del dft_b # Corner Case if len(inverce_c[0]) <= 1: return inverce_c[0] # Inverse DFT SCREAMING_SNAKE_CASE_: Optional[Any] = 2 while next_ncol <= self.c_max_length: SCREAMING_SNAKE_CASE_: List[str] = [[] for i in range(lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: Tuple = self.root ** (next_ncol // 2) SCREAMING_SNAKE_CASE_: int = 1 # First half of next step for j in range(self.c_max_length // next_ncol): for i in range(next_ncol // 2): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root)) current_root *= root # Update SCREAMING_SNAKE_CASE_: Union[str, Any] = new_inverse_c next_ncol *= 2 # Unpack SCREAMING_SNAKE_CASE_: List[Any] = [round(x[0].real , 8) + round(x[0].imag , 8) * 1J for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self : List[str]): SCREAMING_SNAKE_CASE_: Dict = "A = " + " + ".join( F"{coef}*x^{i}" for coef, i in enumerate(self.polyA[: self.len_A])) SCREAMING_SNAKE_CASE_: int = "B = " + " + ".join( F"{coef}*x^{i}" for coef, i in enumerate(self.polyB[: self.len_B])) SCREAMING_SNAKE_CASE_: Any = "A*B = " + " + ".join( F"{coef}*x^{i}" for coef, i in enumerate(self.product)) return F"{a}\n{b}\n{c}" # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
13
import warnings from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =["image_processor", "tokenizer"] UpperCAmelCase_ : Tuple ="FlavaImageProcessor" UpperCAmelCase_ : List[Any] =("BertTokenizer", "BertTokenizerFast") def __init__( self , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase ) -> int: '''simple docstring''' __snake_case : List[Any] = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) __snake_case : List[Any] = kwargs.pop("feature_extractor" ) __snake_case : Any = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = self.image_processor def __call__( self , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = True , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = 0 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = True , UpperCAmelCase = None , **UpperCAmelCase , ) -> List[Any]: '''simple docstring''' if text is None and images is None: raise ValueError("You have to specify either text or images. Both cannot be none." ) if text is not None: __snake_case : Union[str, Any] = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if images is not None: __snake_case : Union[str, Any] = self.image_processor( UpperCAmelCase , return_image_mask=UpperCAmelCase , return_codebook_pixels=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if text is not None and images is not None: encoding.update(UpperCAmelCase ) return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def UpperCAmelCase ( self , *UpperCAmelCase , **UpperCAmelCase ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , *UpperCAmelCase , **UpperCAmelCase ) -> Tuple: '''simple docstring''' return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case : List[Any] = self.tokenizer.model_input_names __snake_case : Union[str, Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
326
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _A = logging.get_logger(__name__) _A = { '''RWKV/rwkv-4-169m-pile''': '''https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-430m-pile''': '''https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-1b5-pile''': '''https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json''', '''RWKV/rwkv-4-3b-pile''': '''https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-7b-pile''': '''https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-14b-pile''': '''https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json''', '''RWKV/rwkv-raven-1b5''': '''https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json''', '''RWKV/rwkv-raven-3b''': '''https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json''', '''RWKV/rwkv-raven-7b''': '''https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json''', '''RWKV/rwkv-raven-14b''': '''https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json''', } class A ( __UpperCAmelCase ): __snake_case = "rwkv" __snake_case = {"max_position_embeddings": "context_length"} def __init__( self, UpperCamelCase__=5_0277, UpperCamelCase__=1024, UpperCamelCase__=4096, UpperCamelCase__=32, UpperCamelCase__=None, UpperCamelCase__=None, UpperCamelCase__=1E-5, UpperCamelCase__=0, UpperCamelCase__=0, UpperCamelCase__=6, UpperCamelCase__=False, UpperCamelCase__=True, **UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ = vocab_size lowerCAmelCase_ = context_length lowerCAmelCase_ = hidden_size lowerCAmelCase_ = num_hidden_layers lowerCAmelCase_ = attention_hidden_size if attention_hidden_size is not None else hidden_size lowerCAmelCase_ = intermediate_size if intermediate_size is not None else 4 * hidden_size lowerCAmelCase_ = layer_norm_epsilon lowerCAmelCase_ = rescale_every lowerCAmelCase_ = use_cache lowerCAmelCase_ = bos_token_id lowerCAmelCase_ = eos_token_id super().__init__( tie_word_embeddings=UpperCamelCase__, bos_token_id=UpperCamelCase__, eos_token_id=UpperCamelCase__, **UpperCamelCase__ )
278
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model'''} _UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', } } _UpperCamelCase = { '''camembert-base''': 512, } _UpperCamelCase = '''▁''' class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Optional[Any] =VOCAB_FILES_NAMES UpperCAmelCase_ : str =PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase_ : str =["input_ids", "attention_mask"] def __init__( self , UpperCAmelCase , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<s>" , UpperCAmelCase="<unk>" , UpperCAmelCase="<pad>" , UpperCAmelCase="<mask>" , UpperCAmelCase=["<s>NOTUSED", "</s>NOTUSED"] , UpperCAmelCase = None , **UpperCAmelCase , ) -> None: '''simple docstring''' __snake_case : Dict = AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token __snake_case : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , additional_special_tokens=UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **UpperCAmelCase , ) __snake_case : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(UpperCAmelCase ) ) __snake_case : Dict = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __snake_case : str = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __snake_case : Optional[int] = len(self.fairseq_tokens_to_ids ) __snake_case : Any = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __snake_case : List[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __snake_case : Dict = [self.cls_token_id] __snake_case : Any = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = 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 [1] + ([0] * len(UpperCAmelCase )) + [1] return [1] + ([0] * len(UpperCAmelCase )) + [1, 1] + ([0] * len(UpperCAmelCase )) + [1] def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: '''simple docstring''' __snake_case : int = [self.sep_token_id] __snake_case : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCAmelCase ( self ) -> int: '''simple docstring''' return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case : Optional[int] = {self.convert_ids_to_tokens(UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCAmelCase ( self , UpperCAmelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(UpperCAmelCase , out_type=UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> List[Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(UpperCAmelCase ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> Tuple: '''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 UpperCAmelCase ( self , UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __snake_case : Tuple = [] __snake_case : Union[str, Any] = "" __snake_case : Optional[int] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(UpperCAmelCase ) + token __snake_case : List[Any] = True __snake_case : Union[str, Any] = [] else: current_sub_tokens.append(UpperCAmelCase ) __snake_case : int = False out_string += self.sp_model.decode(UpperCAmelCase ) return out_string.strip() def __getstate__( self ) -> List[Any]: '''simple docstring''' __snake_case : str = self.__dict__.copy() __snake_case : Optional[Any] = None return state def __setstate__( self , UpperCAmelCase ) -> str: '''simple docstring''' __snake_case : Optional[Any] = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): __snake_case : List[str] = {} __snake_case : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return __snake_case : Optional[Any] = os.path.join( UpperCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase , "wb" ) as fi: __snake_case : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase ) return (out_vocab_file,)
326
0
'''simple docstring''' def _a( UpperCamelCase__ : int = 5_0 ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] =[1] * (length + 1) for row_length in range(length + 1 ): for tile_length in range(2, 5 ): for tile_start in range(row_length - tile_length + 1 ): ways_number[row_length] += ways_number[ row_length - tile_start - tile_length ] return ways_number[length] if __name__ == "__main__": print(F'''{solution() = }''')
152
def lowerCAmelCase__( lowercase : list[int] , lowercase : int ) -> bool: __snake_case : List[str] = len(lowercase ) __snake_case : int = [[False] * (required_sum + 1) for _ in range(arr_len + 1 )] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 for i in range(arr_len + 1 ): __snake_case : Optional[Any] = True # sum is not zero and set is empty then false for i in range(1 , required_sum + 1 ): __snake_case : Union[str, Any] = False for i in range(1 , arr_len + 1 ): for j in range(1 , required_sum + 1 ): if arr[i - 1] > j: __snake_case : List[str] = subset[i - 1][j] if arr[i - 1] <= j: __snake_case : Union[str, Any] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] return subset[arr_len][required_sum] if __name__ == "__main__": import doctest doctest.testmod()
326
0
"""simple docstring""" import argparse import math import traceback import dateutil.parser as date_parser import requests def a_ ( _lowerCAmelCase : Optional[int] ): '''simple docstring''' lowercase__ : Tuple = {} lowercase__ : List[Any] = job["started_at"] lowercase__ : List[str] = job["completed_at"] lowercase__ : List[Any] = date_parser.parse(_lowerCAmelCase ) lowercase__ : str = date_parser.parse(_lowerCAmelCase ) lowercase__ : List[Any] = round((end_datetime - start_datetime).total_seconds() / 60.0 ) lowercase__ : Union[str, Any] = start lowercase__ : List[str] = end lowercase__ : Union[str, Any] = duration_in_min return job_info def a_ ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any]=None ): '''simple docstring''' lowercase__ : Any = None if token is not None: lowercase__ : Any = {"Accept": "application/vnd.github+json", "Authorization": f"""Bearer {token}"""} lowercase__ : List[Any] = f"""https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100""" lowercase__ : Optional[int] = requests.get(_lowerCAmelCase , headers=_lowerCAmelCase ).json() lowercase__ : Optional[int] = {} try: job_time.update({job['name']: extract_time_from_single_job(_lowerCAmelCase ) for job in result['jobs']} ) lowercase__ : Optional[int] = math.ceil((result['total_count'] - 100) / 100 ) for i in range(_lowerCAmelCase ): lowercase__ : str = requests.get(url + f"""&page={i + 2}""" , headers=_lowerCAmelCase ).json() job_time.update({job['name']: extract_time_from_single_job(_lowerCAmelCase ) for job in result['jobs']} ) return job_time except Exception: print(f"""Unknown error, could not fetch links:\n{traceback.format_exc()}""" ) return {} if __name__ == "__main__": _UpperCamelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.") _UpperCamelCase : Union[str, Any] = parser.parse_args() _UpperCamelCase : List[Any] = get_job_time(args.workflow_run_id) _UpperCamelCase : Tuple = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f'''{k}: {v["duration"]}''')
77
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node _UpperCamelCase = 4 _UpperCamelCase = 3 class _lowerCamelCase ( a ): """simple docstring""" pass def lowerCAmelCase__( lowercase : List[str] ) -> Any: for shard in shards: for i in range(lowercase ): yield {"i": i, "shard": shard} def lowerCAmelCase__( ) -> Optional[int]: __snake_case : List[Any] = int(os.environ["RANK"] ) __snake_case : Optional[int] = int(os.environ["WORLD_SIZE"] ) __snake_case : List[str] = ArgumentParser() parser.add_argument("--streaming" , type=lowercase ) parser.add_argument("--local_rank" , type=lowercase ) parser.add_argument("--num_workers" , type=lowercase , default=0 ) __snake_case : Any = parser.parse_args() __snake_case : Dict = args.streaming __snake_case : Union[str, Any] = args.num_workers __snake_case : Any = {"shards": [f"""shard_{shard_idx}""" for shard_idx in range(lowercase )]} __snake_case : Optional[int] = IterableDataset.from_generator(lowercase , gen_kwargs=lowercase ) if not streaming: __snake_case : Any = Dataset.from_list(list(lowercase ) ) __snake_case : Dict = split_dataset_by_node(lowercase , rank=lowercase , world_size=lowercase ) __snake_case : Union[str, Any] = torch.utils.data.DataLoader(lowercase , num_workers=lowercase ) __snake_case : Optional[int] = NUM_SHARDS * NUM_ITEMS_PER_SHARD __snake_case : List[str] = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) __snake_case : Dict = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f"""local_size {local_size} != expected_local_size {expected_local_size}""" ) if __name__ == "__main__": main()
326
0
import numpy as np def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = 1e-12 , SCREAMING_SNAKE_CASE = 100 , ): '''simple docstring''' assert np.shape(SCREAMING_SNAKE_CASE )[0] == np.shape(SCREAMING_SNAKE_CASE )[1] # Ensure proper dimensionality. assert np.shape(SCREAMING_SNAKE_CASE )[0] == np.shape(SCREAMING_SNAKE_CASE )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(SCREAMING_SNAKE_CASE ) == np.iscomplexobj(SCREAMING_SNAKE_CASE ) __UpperCamelCase :str = np.iscomplexobj(SCREAMING_SNAKE_CASE ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(SCREAMING_SNAKE_CASE , 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. __UpperCamelCase :Tuple = False __UpperCamelCase :List[Any] = 0 __UpperCamelCase :Union[str, Any] = 0 __UpperCamelCase :Any = 1e12 while not convergence: # Multiple matrix by the vector. __UpperCamelCase :Dict = np.dot(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Normalize the resulting output vector. __UpperCamelCase :List[Any] = w / np.linalg.norm(SCREAMING_SNAKE_CASE ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) __UpperCamelCase :Optional[int] = vector.conj().T if is_complex else vector.T __UpperCamelCase :Union[str, Any] = np.dot(SCREAMING_SNAKE_CASE , np.dot(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) # Check convergence. __UpperCamelCase :int = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: __UpperCamelCase :List[Any] = True __UpperCamelCase :str = lambda_ if is_complex: __UpperCamelCase :Any = np.real(lambda_ ) return lambda_, vector def lowerCamelCase ( ): '''simple docstring''' __UpperCamelCase :List[Any] = np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]] ) __UpperCamelCase :Dict = np.array([41, 4, 20] ) __UpperCamelCase :Tuple = real_input_matrix.astype(np.complexaaa ) __UpperCamelCase :str = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T __UpperCamelCase :Optional[int] = np.array([41, 4, 20] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": __UpperCamelCase :Optional[int] = real_input_matrix __UpperCamelCase :List[str] = real_vector elif problem_type == "complex": __UpperCamelCase :Any = complex_input_matrix __UpperCamelCase :int = complex_vector # Our implementation. __UpperCamelCase :Any = power_iteration(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). __UpperCamelCase :Dict = np.linalg.eigh(SCREAMING_SNAKE_CASE ) # Last eigenvalue is the maximum one. __UpperCamelCase :List[str] = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. __UpperCamelCase :str = 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(SCREAMING_SNAKE_CASE ) - np.abs(SCREAMING_SNAKE_CASE ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
43
def lowerCAmelCase__( lowercase : int = 100_0000 ) -> int: __snake_case : List[Any] = limit + 1 __snake_case : List[str] = [0] * limit for first_term in range(1 , lowercase ): for n in range(lowercase , lowercase , lowercase ): __snake_case : Union[str, Any] = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a __snake_case : Tuple = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
326
0
'''simple docstring''' import inspect import unittest from transformers import BitConfig 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 torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class _snake_case : def __init__( self , a__ , a__=3 , a__=32 , a__=3 , a__=10 , a__=[8, 16, 32, 64] , a__=[1, 1, 2, 1] , a__=True , a__=True , a__="relu" , a__=3 , a__=None , a__=["stage2", "stage3", "stage4"] , a__=[2, 3, 4] , a__=1 , ) -> Optional[int]: '''simple docstring''' snake_case_ = parent snake_case_ = batch_size snake_case_ = image_size snake_case_ = num_channels snake_case_ = embeddings_size snake_case_ = hidden_sizes snake_case_ = depths snake_case_ = is_training snake_case_ = use_labels snake_case_ = hidden_act snake_case_ = num_labels snake_case_ = scope snake_case_ = len(a__ ) snake_case_ = out_features snake_case_ = out_indices snake_case_ = num_groups def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.num_labels ) snake_case_ = self.get_config() return config, pixel_values, labels def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' return BitConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , out_features=self.out_features , out_indices=self.out_indices , num_groups=self.num_groups , ) def lowerCAmelCase__ ( self , a__ , a__ , a__ ) -> str: '''simple docstring''' snake_case_ = BitModel(config=a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowerCAmelCase__ ( self , a__ , a__ , a__ ) -> Union[str, Any]: '''simple docstring''' snake_case_ = self.num_labels snake_case_ = BitForImageClassification(a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ , labels=a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase__ ( self , a__ , a__ , a__ ) -> Optional[int]: '''simple docstring''' snake_case_ = BitBackbone(config=a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ ) # verify feature maps 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 snake_case_ = None snake_case_ = BitBackbone(config=a__ ) model.to(a__ ) model.eval() snake_case_ = model(a__ ) # 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 lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' snake_case_ = self.prepare_config_and_inputs() snake_case_ = config_and_inputs snake_case_ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class _snake_case ( lowercase_ , lowercase_ , unittest.TestCase ): lowerCAmelCase_ : Any = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () lowerCAmelCase_ : Tuple = ( {"feature-extraction": BitModel, "image-classification": BitForImageClassification} if is_torch_available() else {} ) lowerCAmelCase_ : List[Any] = False lowerCAmelCase_ : str = False lowerCAmelCase_ : List[Any] = False lowerCAmelCase_ : Union[str, Any] = False lowerCAmelCase_ : Dict = False def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' snake_case_ = BitModelTester(self ) snake_case_ = ConfigTester(self , config_class=a__ , has_text_modality=a__ ) def lowerCAmelCase__ ( self ) -> List[str]: '''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 lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' return @unittest.skip(reason="Bit does not output attentions" ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' pass @unittest.skip(reason="Bit does not use inputs_embeds" ) def lowerCAmelCase__ ( self ) -> Any: '''simple docstring''' pass @unittest.skip(reason="Bit does not support input and output embeddings" ) def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' pass def lowerCAmelCase__ ( self ) -> Dict: '''simple docstring''' snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = model_class(a__ ) snake_case_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ = [*signature.parameters.keys()] snake_case_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , a__ ) def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a__ ) def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*a__ ) def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = model_class(config=a__ ) for name, module in model.named_modules(): if isinstance(a__ , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=F'Parameter {name} of model {model_class} seems not properly initialized' , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=F'Parameter {name} of model {model_class} seems not properly initialized' , ) def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' def check_hidden_states_output(a__ , a__ , a__ ): snake_case_ = model_class(a__ ) model.to(a__ ) model.eval() with torch.no_grad(): snake_case_ = model(**self._prepare_for_class(a__ , a__ ) ) snake_case_ = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states snake_case_ = self.model_tester.num_stages self.assertEqual(len(a__ ) , expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = ["preactivation", "bottleneck"] for model_class in self.all_model_classes: for layer_type in layers_type: snake_case_ = layer_type snake_case_ = True check_hidden_states_output(a__ , a__ , a__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ = True check_hidden_states_output(a__ , a__ , a__ ) @unittest.skip(reason="Bit does not use feedforward chunking" ) def lowerCAmelCase__ ( self ) -> int: '''simple docstring''' pass def lowerCAmelCase__ ( self ) -> Tuple: '''simple docstring''' snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a__ ) @slow def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = BitModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) def UpperCamelCase_( ): '''simple docstring''' snake_case_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class _snake_case ( unittest.TestCase ): @cached_property def lowerCAmelCase__ ( self ) -> str: '''simple docstring''' return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowerCAmelCase__ ( self ) -> Tuple: '''simple docstring''' snake_case_ = BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(a__ ) snake_case_ = self.default_image_processor snake_case_ = prepare_img() snake_case_ = image_processor(images=a__ , return_tensors="pt" ).to(a__ ) # forward pass with torch.no_grad(): snake_case_ = model(**a__ ) # verify the logits snake_case_ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , a__ ) snake_case_ = torch.tensor([[-0.6_5_2_6, -0.5_2_6_3, -1.4_3_9_8]] ).to(a__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a__ , atol=1e-4 ) ) @require_torch class _snake_case ( lowercase_ , unittest.TestCase ): lowerCAmelCase_ : Optional[int] = (BitBackbone,) if is_torch_available() else () lowerCAmelCase_ : Union[str, Any] = BitConfig lowerCAmelCase_ : Union[str, Any] = False def lowerCAmelCase__ ( self ) -> Dict: '''simple docstring''' snake_case_ = BitModelTester(self )
85
from __future__ import annotations def lowerCAmelCase__( lowercase : str , lowercase : list[str] | None = None ) -> list[list[str]]: __snake_case : List[str] = word_bank or [] # create a table __snake_case : int = len(lowercase ) + 1 __snake_case : list[list[list[str]]] = [] for _ in range(lowercase ): table.append([] ) # seed value __snake_case : Optional[int] = [[]] # because empty string has empty combination # iterate through the indices for i in range(lowercase ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(lowercase )] == word: __snake_case : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(lowercase )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(lowercase )]: combination.reverse() return table[len(lowercase )] if __name__ == "__main__": print(all_construct('''jwajalapa''', ['''jwa''', '''j''', '''w''', '''a''', '''la''', '''lapa'''])) print(all_construct('''rajamati''', ['''s''', '''raj''', '''amat''', '''raja''', '''ma''', '''i''', '''t'''])) print( all_construct( '''hexagonosaurus''', ['''h''', '''ex''', '''hex''', '''ag''', '''ago''', '''ru''', '''auru''', '''rus''', '''go''', '''no''', '''o''', '''s'''], ) )
326
0
"""simple docstring""" import unittest from transformers import BigBirdConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class a ( unittest.TestCase ): def __init__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : List[str]=2 , __SCREAMING_SNAKE_CASE : Any=56 , __SCREAMING_SNAKE_CASE : Tuple=True , __SCREAMING_SNAKE_CASE : int=True , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : str=99 , __SCREAMING_SNAKE_CASE : str=32 , __SCREAMING_SNAKE_CASE : str=2 , __SCREAMING_SNAKE_CASE : str=2 , __SCREAMING_SNAKE_CASE : Any=7 , __SCREAMING_SNAKE_CASE : List[Any]="gelu_new" , __SCREAMING_SNAKE_CASE : Tuple=0.1 , __SCREAMING_SNAKE_CASE : Dict=0.1 , __SCREAMING_SNAKE_CASE : Union[str, Any]=512 , __SCREAMING_SNAKE_CASE : Any=16 , __SCREAMING_SNAKE_CASE : Optional[Any]=2 , __SCREAMING_SNAKE_CASE : List[str]=0.02 , __SCREAMING_SNAKE_CASE : Optional[Any]=4 , __SCREAMING_SNAKE_CASE : Any="block_sparse" , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : List[str]=3 , ) -> Tuple: lowerCamelCase_ = parent lowerCamelCase_ = batch_size lowerCamelCase_ = seq_length lowerCamelCase_ = is_training lowerCamelCase_ = use_attention_mask lowerCamelCase_ = use_token_type_ids lowerCamelCase_ = use_labels lowerCamelCase_ = vocab_size lowerCamelCase_ = hidden_size lowerCamelCase_ = num_hidden_layers lowerCamelCase_ = num_attention_heads lowerCamelCase_ = intermediate_size lowerCamelCase_ = hidden_act lowerCamelCase_ = hidden_dropout_prob lowerCamelCase_ = attention_probs_dropout_prob lowerCamelCase_ = max_position_embeddings lowerCamelCase_ = type_vocab_size lowerCamelCase_ = type_sequence_label_size lowerCamelCase_ = initializer_range lowerCamelCase_ = num_choices lowerCamelCase_ = rescale_embeddings lowerCamelCase_ = attention_type lowerCamelCase_ = use_bias lowerCamelCase_ = block_size lowerCamelCase_ = num_random_blocks def UpperCamelCase ( self : List[Any] ) -> int: lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase_ = None if self.use_attention_mask: lowerCamelCase_ = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase_ = None if self.use_token_type_ids: lowerCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase_ = BigBirdConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , attention_type=self.attention_type , block_size=self.block_size , num_random_blocks=self.num_random_blocks , use_bias=self.use_bias , rescale_embeddings=self.rescale_embeddings , ) return config, input_ids, token_type_ids, attention_mask def UpperCamelCase ( self : List[Any] ) -> Any: lowerCamelCase_ = self.prepare_config_and_inputs() lowerCamelCase_ = config_and_inputs lowerCamelCase_ = { "input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask, } return config, inputs_dict @require_flax class a ( __snake_case , unittest.TestCase ): SCREAMING_SNAKE_CASE : Union[str, Any] = ( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) SCREAMING_SNAKE_CASE : Dict = False SCREAMING_SNAKE_CASE : str = False def UpperCamelCase ( self : List[str] ) -> str: lowerCamelCase_ = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCamelCase ( self : str ) -> Any: super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCamelCase ( self : int ) -> Optional[int]: super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCamelCase ( self : List[str] ) -> Tuple: super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCamelCase ( self : Union[str, Any] ) -> Tuple: super().test_hidden_states_output() @slow def UpperCamelCase ( self : Any ) -> Dict: for model_class_name in self.all_model_classes: lowerCamelCase_ = model_class_name.from_pretrained('google/bigbird-roberta-base' ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : Tuple ) -> Optional[int]: if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCamelCase ( self : Dict ) -> int: lowerCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): lowerCamelCase_ = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCamelCase_ = model_class(__SCREAMING_SNAKE_CASE ) @jax.jit def model_jitted(__SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int=None , **__SCREAMING_SNAKE_CASE : Union[str, Any] ): return model(input_ids=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) with self.subTest('JIT Enabled' ): lowerCamelCase_ = model_jitted(**__SCREAMING_SNAKE_CASE ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): lowerCamelCase_ = model_jitted(**__SCREAMING_SNAKE_CASE ).to_tuple() self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , len(__SCREAMING_SNAKE_CASE ) ) for jitted_output, output in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(jitted_output.shape , output.shape ) def UpperCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str=1e-5 , __SCREAMING_SNAKE_CASE : Optional[Any]="outputs" , __SCREAMING_SNAKE_CASE : Union[str, Any]=None ) -> int: if name.startswith('outputs.attentions' ): return else: super().check_pt_flax_outputs(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
183
import unittest from transformers import BigBirdConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=2 , UpperCAmelCase=56 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=99 , UpperCAmelCase=32 , UpperCAmelCase=2 , UpperCAmelCase=2 , UpperCAmelCase=7 , UpperCAmelCase="gelu_new" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=512 , UpperCAmelCase=16 , UpperCAmelCase=2 , UpperCAmelCase=0.02 , UpperCAmelCase=4 , UpperCAmelCase="block_sparse" , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=2 , UpperCAmelCase=3 , ) -> Tuple: '''simple docstring''' __snake_case : Optional[int] = parent __snake_case : Tuple = batch_size __snake_case : List[str] = seq_length __snake_case : Optional[int] = is_training __snake_case : int = use_attention_mask __snake_case : Union[str, Any] = use_token_type_ids __snake_case : Any = use_labels __snake_case : List[str] = vocab_size __snake_case : int = hidden_size __snake_case : List[str] = num_hidden_layers __snake_case : List[Any] = num_attention_heads __snake_case : Optional[int] = intermediate_size __snake_case : Union[str, Any] = hidden_act __snake_case : Optional[int] = hidden_dropout_prob __snake_case : Optional[Any] = attention_probs_dropout_prob __snake_case : str = max_position_embeddings __snake_case : List[Any] = type_vocab_size __snake_case : int = type_sequence_label_size __snake_case : Dict = initializer_range __snake_case : List[Any] = num_choices __snake_case : Union[str, Any] = rescale_embeddings __snake_case : List[Any] = attention_type __snake_case : str = use_bias __snake_case : Dict = block_size __snake_case : Optional[Any] = num_random_blocks def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case : Any = None if self.use_attention_mask: __snake_case : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case : Union[str, Any] = None if self.use_token_type_ids: __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case : Optional[int] = BigBirdConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , attention_type=self.attention_type , block_size=self.block_size , num_random_blocks=self.num_random_blocks , use_bias=self.use_bias , rescale_embeddings=self.rescale_embeddings , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case : Optional[int] = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case : Dict = config_and_inputs __snake_case : int = { "input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask, } return config, inputs_dict @require_flax class _lowerCamelCase ( a , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) UpperCAmelCase_ : Dict =False UpperCAmelCase_ : str =False def UpperCAmelCase ( self ) -> str: '''simple docstring''' __snake_case : Dict = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Any: '''simple docstring''' super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_hidden_states_output() @slow def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case : Any = model_class_name.from_pretrained("google/bigbird-roberta-base" ) self.assertIsNotNone(UpperCAmelCase ) def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case , __snake_case : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __snake_case : Optional[Any] = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = model_class(UpperCAmelCase ) @jax.jit def model_jitted(UpperCAmelCase , UpperCAmelCase=None , **UpperCAmelCase ): return model(input_ids=UpperCAmelCase , attention_mask=UpperCAmelCase , **UpperCAmelCase ) with self.subTest("JIT Enabled" ): __snake_case : int = model_jitted(**UpperCAmelCase ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __snake_case : List[Any] = model_jitted(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) ) for jitted_output, output in zip(UpperCAmelCase , UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=1E-5 , UpperCAmelCase="outputs" , UpperCAmelCase=None ) -> int: '''simple docstring''' if name.startswith("outputs.attentions" ): return else: super().check_pt_flax_outputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase )
326
0
from maths.prime_factors import prime_factors def a__ ( A_ ): '''simple docstring''' if not isinstance(A_, A_ ): __magic_name__ = f'''Input value of [number={number}] must be an integer''' raise TypeError(A_ ) if number < 1: raise ValueError("""Input must be a positive integer""" ) return -1 if len(prime_factors(A_ ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
88
import argparse import datetime def lowerCAmelCase__( lowercase : str ) -> str: __snake_case : int = { "0": "Sunday", "1": "Monday", "2": "Tuesday", "3": "Wednesday", "4": "Thursday", "5": "Friday", "6": "Saturday", } __snake_case : int = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(lowercase ) < 11: raise ValueError("Must be 10 characters long" ) # Get month __snake_case : int = int(date_input[0] + date_input[1] ) # Validate if not 0 < m < 13: raise ValueError("Month must be between 1 - 12" ) __snake_case : str = date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'" ) # Get day __snake_case : int = int(date_input[3] + date_input[4] ) # Validate if not 0 < d < 32: raise ValueError("Date must be between 1 - 31" ) # Get second separator __snake_case : str = date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'" ) # Get year __snake_case : int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9] ) # Arbitrary year range if not 45 < y < 8500: raise ValueError( "Year out of range. There has to be some sort of limit...right?" ) # Get datetime obj for validation __snake_case : str = datetime.date(int(lowercase ) , int(lowercase ) , int(lowercase ) ) # Start math if m <= 2: __snake_case : Optional[Any] = y - 1 __snake_case : Tuple = m + 12 # maths var __snake_case : int = int(str(lowercase )[:2] ) __snake_case : int = int(str(lowercase )[2:] ) __snake_case : int = int(2.6 * m - 5.3_9 ) __snake_case : int = int(c / 4 ) __snake_case : int = int(k / 4 ) __snake_case : int = int(d + k ) __snake_case : int = int(t + u + v + x ) __snake_case : int = int(z - (2 * c) ) __snake_case : int = round(w % 7 ) # End math # Validate math if f != convert_datetime_days[dt_ck.weekday()]: raise AssertionError("The date was evaluated incorrectly. Contact developer." ) # Response __snake_case : str = f"""Your date {date_input}, is a {days[str(lowercase )]}!""" return response if __name__ == "__main__": import doctest doctest.testmod() _UpperCamelCase = argparse.ArgumentParser( description=( '''Find out what day of the week nearly any date is or was. Enter ''' '''date as a string in the mm-dd-yyyy or mm/dd/yyyy format''' ) ) parser.add_argument( '''date_input''', type=str, help='''Date as a string (mm-dd-yyyy or mm/dd/yyyy)''' ) _UpperCamelCase = parser.parse_args() zeller(args.date_input)
326
0
import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def A ( lowercase , lowercase , lowercase , lowercase=5 ) -> str: '''simple docstring''' assert masked_input.count('<mask>' ) == 1 UpperCamelCase = torch.tensor(tokenizer.encode(lowercase , add_special_tokens=lowercase ) ).unsqueeze(0 ) # Batch size 1 UpperCamelCase = model(lowercase )[0] # The last hidden-state is the first element of the output tuple UpperCamelCase = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() UpperCamelCase = logits[0, masked_index, :] UpperCamelCase = logits.softmax(dim=0 ) UpperCamelCase = prob.topk(k=lowercase , dim=0 ) UpperCamelCase = " ".join( [tokenizer.convert_ids_to_tokens(indices[i].item() ) for i in range(len(lowercase ) )] ) UpperCamelCase = tokenizer.mask_token UpperCamelCase = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(' ' ) ): UpperCamelCase = predicted_token_bpe.replace('\u2581' , ' ' ) if " {0}".format(lowercase ) in masked_input: topk_filled_outputs.append( ( masked_input.replace(' {0}'.format(lowercase ) , lowercase ), values[index].item(), predicted_token, ) ) else: topk_filled_outputs.append( ( masked_input.replace(lowercase , lowercase ), values[index].item(), predicted_token, ) ) return topk_filled_outputs _UpperCAmelCase : Union[str, Any] = CamembertTokenizer.from_pretrained("camembert-base") _UpperCAmelCase : List[Any] = CamembertForMaskedLM.from_pretrained("camembert-base") model.eval() _UpperCAmelCase : List[str] = "Le camembert est <mask> :)" print(fill_mask(masked_input, model, tokenizer, topk=3))
222
def lowerCAmelCase__( lowercase : List[Any] , lowercase : Optional[Any] , lowercase : Optional[int] , lowercase : str , lowercase : List[Any] , lowercase : List[str] ) -> int: if index == r: for j in range(lowercase ): print(data[j] , end=" " ) print(" " ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location __snake_case : Union[str, Any] = arr[i] combination_util(lowercase , lowercase , lowercase , index + 1 , lowercase , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(lowercase , lowercase , lowercase , lowercase , lowercase , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def lowerCAmelCase__( lowercase : Any , lowercase : Tuple , lowercase : Union[str, Any] ) -> Optional[Any]: # A temporary array to store all combination one by one __snake_case : Tuple = [0] * r # Print all combination using temporary array 'data[]' combination_util(lowercase , lowercase , lowercase , 0 , lowercase , 0 ) if __name__ == "__main__": # Driver code to check the function above _UpperCamelCase = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
326
0
__snake_case = 2_56 # Modulus to hash a string __snake_case = 1_00_00_03 def _lowercase ( UpperCamelCase_ , UpperCamelCase_ ) -> bool: '''simple docstring''' SCREAMING_SNAKE_CASE__ = len(UpperCamelCase_ ) SCREAMING_SNAKE_CASE__ = len(UpperCamelCase_ ) if p_len > t_len: return False SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 1 # Calculating the hash of pattern and substring of text for i in range(UpperCamelCase_ ): SCREAMING_SNAKE_CASE__ = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus SCREAMING_SNAKE_CASE__ = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue SCREAMING_SNAKE_CASE__ = (modulus_power * alphabet_size) % modulus for i in range(0 , t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash SCREAMING_SNAKE_CASE__ = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def _lowercase ( ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE__ = "abc1abc12" SCREAMING_SNAKE_CASE__ = "alskfjaldsabc1abc1abc12k23adsfabcabc" SCREAMING_SNAKE_CASE__ = "alskfjaldsk23adsfabcabc" assert rabin_karp(UpperCamelCase_ , UpperCamelCase_ ) and not rabin_karp(UpperCamelCase_ , UpperCamelCase_ ) # Test 2) SCREAMING_SNAKE_CASE__ = "ABABX" SCREAMING_SNAKE_CASE__ = "ABABZABABYABABX" assert rabin_karp(UpperCamelCase_ , UpperCamelCase_ ) # Test 3) SCREAMING_SNAKE_CASE__ = "AAAB" SCREAMING_SNAKE_CASE__ = "ABAAAAAB" assert rabin_karp(UpperCamelCase_ , UpperCamelCase_ ) # Test 4) SCREAMING_SNAKE_CASE__ = "abcdabcy" SCREAMING_SNAKE_CASE__ = "abcxabcdabxabcdabcdabcy" assert rabin_karp(UpperCamelCase_ , UpperCamelCase_ ) # Test 5) SCREAMING_SNAKE_CASE__ = "Lü" SCREAMING_SNAKE_CASE__ = "Lüsai" assert rabin_karp(UpperCamelCase_ , UpperCamelCase_ ) SCREAMING_SNAKE_CASE__ = "Lue" assert not rabin_karp(UpperCamelCase_ , UpperCamelCase_ ) print('Success.' ) if __name__ == "__main__": test_rabin_karp()
176
import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = [ ('''bert.bert''', '''visual_bert'''), ('''bert.cls''', '''cls'''), ('''bert.classifier''', '''cls'''), ('''token_type_embeddings_visual''', '''visual_token_type_embeddings'''), ('''position_embeddings_visual''', '''visual_position_embeddings'''), ('''projection''', '''visual_projection'''), ] _UpperCamelCase = [ '''nlvr2_coco_pre_trained.th''', '''nlvr2_fine_tuned.th''', '''nlvr2_pre_trained.th''', '''vcr_coco_pre_train.th''', '''vcr_fine_tune.th''', '''vcr_pre_train.th''', '''vqa_coco_pre_trained.th''', '''vqa_fine_tuned.th''', '''vqa_pre_trained.th''', ] def lowerCAmelCase__( lowercase : str ) -> Optional[Any]: __snake_case : Optional[int] = torch.load(lowercase , map_location="cpu" ) return sd def lowerCAmelCase__( lowercase : List[Any] , lowercase : List[Any] , lowercase : List[Any]=rename_keys_prefix ) -> Dict: __snake_case : Tuple = OrderedDict() __snake_case : str = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue __snake_case : Optional[Any] = key for name_pair in rename_keys_prefix: __snake_case : List[str] = new_key.replace(name_pair[0] , name_pair[1] ) __snake_case : List[str] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately __snake_case : List[Any] = new_d["cls.predictions.bias"] return new_d @torch.no_grad() def lowerCAmelCase__( lowercase : Optional[Any] , lowercase : Any ) -> List[Any]: assert ( checkpoint_path.split("/" )[-1] in ACCEPTABLE_CHECKPOINTS ), f"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: __snake_case : Any = "pretraining" if "vcr" in checkpoint_path: __snake_case : Optional[Any] = {"visual_embedding_dim": 512} elif "vqa_advanced" in checkpoint_path: __snake_case : Tuple = {"visual_embedding_dim": 2048} elif "vqa" in checkpoint_path: __snake_case : Dict = {"visual_embedding_dim": 2048} elif "nlvr" in checkpoint_path: __snake_case : Any = {"visual_embedding_dim": 1024} else: raise NotImplementedError(f"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: __snake_case : Dict = {"visual_embedding_dim": 512} __snake_case : Any = "multichoice" elif "vqa_advanced" in checkpoint_path: __snake_case : List[Any] = {"visual_embedding_dim": 2048} __snake_case : Optional[Any] = "vqa_advanced" elif "vqa" in checkpoint_path: __snake_case : Union[str, Any] = {"visual_embedding_dim": 2048, "num_labels": 3129} __snake_case : Union[str, Any] = "vqa" elif "nlvr" in checkpoint_path: __snake_case : Tuple = { "visual_embedding_dim": 1024, "num_labels": 2, } __snake_case : List[Any] = "nlvr" __snake_case : Union[str, Any] = VisualBertConfig(**lowercase ) # Load State Dict __snake_case : Any = load_state_dict(lowercase ) __snake_case : Dict = get_new_dict(lowercase , lowercase ) if model_type == "pretraining": __snake_case : Optional[Any] = VisualBertForPreTraining(lowercase ) elif model_type == "vqa": __snake_case : Tuple = VisualBertForQuestionAnswering(lowercase ) elif model_type == "nlvr": __snake_case : Tuple = VisualBertForVisualReasoning(lowercase ) elif model_type == "multichoice": __snake_case : List[Any] = VisualBertForMultipleChoice(lowercase ) model.load_state_dict(lowercase ) # Save Checkpoints Path(lowercase ).mkdir(exist_ok=lowercase ) model.save_pretrained(lowercase ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''orig_checkpoint_path''', type=str, help='''A path to .th on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', type=str, help='''Path to the output PyTorch model.''') _UpperCamelCase = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
326
0
"""simple docstring""" import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCAmelCase = logging.get_logger(__name__) UpperCAmelCase = """▁""" UpperCAmelCase = {"""vocab_file""": """sentencepiece.bpe.model"""} UpperCAmelCase = { """vocab_file""": { """facebook/xglm-564M""": """https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model""", } } UpperCAmelCase = { """facebook/xglm-564M""": 2_048, } class UpperCAmelCase_ ( _lowercase): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Dict , __UpperCamelCase : int , __UpperCamelCase : Dict="<s>" , __UpperCamelCase : Union[str, Any]="</s>" , __UpperCamelCase : Dict="</s>" , __UpperCamelCase : Optional[Any]="<s>" , __UpperCamelCase : str="<unk>" , __UpperCamelCase : Union[str, Any]="<pad>" , __UpperCamelCase : Any = None , **__UpperCamelCase : Dict , ) -> None: _UpperCamelCase = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer _UpperCamelCase = 7 _UpperCamelCase = [F'''<madeupword{i}>''' for i in range(self.num_madeup_words )] _UpperCamelCase = kwargs.get('''additional_special_tokens''' , [] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , cls_token=__UpperCamelCase , pad_token=__UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCamelCase , ) _UpperCamelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__UpperCamelCase ) ) _UpperCamelCase = 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' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab _UpperCamelCase = 1 # Mimic fairseq token-to-id alignment for the first 4 token _UpperCamelCase = {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3} _UpperCamelCase = len(self.sp_model ) _UpperCamelCase = {F'''<madeupword{i}>''': sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(__UpperCamelCase ) _UpperCamelCase = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self : Tuple ) -> List[str]: _UpperCamelCase = self.__dict__.copy() _UpperCamelCase = None _UpperCamelCase = self.sp_model.serialized_model_proto() return state def __setstate__( self : Union[str, Any] , __UpperCamelCase : Union[str, Any] ) -> Tuple: _UpperCamelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _UpperCamelCase = {} _UpperCamelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def _UpperCamelCase ( self : str , __UpperCamelCase : Dict , __UpperCamelCase : Tuple = None ) -> List[int]: if token_ids_a is None: return [self.sep_token_id] + token_ids_a _UpperCamelCase = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def _UpperCamelCase ( self : Optional[Any] , __UpperCamelCase : List[Any] , __UpperCamelCase : Any = None , __UpperCamelCase : List[Any] = False ) -> List[int]: 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 [1] + ([0] * len(__UpperCamelCase )) return [1] + ([0] * len(__UpperCamelCase )) + [1, 1] + ([0] * len(__UpperCamelCase )) def _UpperCamelCase ( self : int , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : List[str] = None ) -> List[int]: _UpperCamelCase = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def _UpperCamelCase ( self : Optional[int] ) -> Any: return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def _UpperCamelCase ( self : Union[str, Any] ) -> Tuple: _UpperCamelCase = {self.convert_ids_to_tokens(__UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def _UpperCamelCase ( self : List[Any] , __UpperCamelCase : int ) -> List[str]: return self.sp_model.encode(__UpperCamelCase , out_type=__UpperCamelCase ) def _UpperCamelCase ( self : Any , __UpperCamelCase : Tuple ) -> Union[str, Any]: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] _UpperCamelCase = 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 _UpperCamelCase ( self : int , __UpperCamelCase : str ) -> Any: 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 _UpperCamelCase ( self : int , __UpperCamelCase : Tuple ) -> str: _UpperCamelCase = "".join(__UpperCamelCase ).replace(__UpperCamelCase , ''' ''' ).strip() return out_string def _UpperCamelCase ( self : List[Any] , __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] = None ) -> Tuple[str]: if not os.path.isdir(__UpperCamelCase ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _UpperCamelCase = 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: _UpperCamelCase = self.sp_model.serialized_model_proto() fi.write(__UpperCamelCase ) return (out_vocab_file,)
256
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowerCAmelCase__( lowercase : Optional[int] , lowercase : Any , lowercase : Dict , lowercase : List[str] , lowercase : List[Any] ) -> Tuple: # Load configuration defined in the metadata file with open(lowercase ) as metadata_file: __snake_case : int = json.load(lowercase ) __snake_case : Optional[int] = LukeConfig(use_entity_aware_attention=lowercase , **metadata["model_config"] ) # Load in the weights from the checkpoint_path __snake_case : List[Any] = torch.load(lowercase , map_location="cpu" )["module"] # Load the entity vocab file __snake_case : Tuple = load_original_entity_vocab(lowercase ) # add an entry for [MASK2] __snake_case : Optional[int] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 __snake_case : Union[str, Any] = XLMRobertaTokenizer.from_pretrained(metadata["model_config"]["bert_model_name"] ) # Add special tokens to the token vocabulary for downstream tasks __snake_case : Optional[int] = AddedToken("<ent>" , lstrip=lowercase , rstrip=lowercase ) __snake_case : Any = AddedToken("<ent2>" , lstrip=lowercase , rstrip=lowercase ) tokenizer.add_special_tokens({"additional_special_tokens": [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(f"""Saving tokenizer to {pytorch_dump_folder_path}""" ) tokenizer.save_pretrained(lowercase ) with open(os.path.join(lowercase , "tokenizer_config.json" ) , "r" ) as f: __snake_case : Tuple = json.load(lowercase ) __snake_case : List[Any] = "MLukeTokenizer" with open(os.path.join(lowercase , "tokenizer_config.json" ) , "w" ) as f: json.dump(lowercase , lowercase ) with open(os.path.join(lowercase , MLukeTokenizer.vocab_files_names["entity_vocab_file"] ) , "w" ) as f: json.dump(lowercase , lowercase ) __snake_case : Any = MLukeTokenizer.from_pretrained(lowercase ) # Initialize the embeddings of the special tokens __snake_case : str = tokenizer.convert_tokens_to_ids(["@"] )[0] __snake_case : List[str] = tokenizer.convert_tokens_to_ids(["#"] )[0] __snake_case : List[Any] = state_dict["embeddings.word_embeddings.weight"] __snake_case : Union[str, Any] = word_emb[ent_init_index].unsqueeze(0 ) __snake_case : Union[str, Any] = word_emb[enta_init_index].unsqueeze(0 ) __snake_case : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: __snake_case : List[Any] = state_dict[bias_name] __snake_case : Optional[int] = decoder_bias[ent_init_index].unsqueeze(0 ) __snake_case : int = decoder_bias[enta_init_index].unsqueeze(0 ) __snake_case : Any = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: __snake_case : Dict = f"""encoder.layer.{layer_index}.attention.self.""" __snake_case : Union[str, Any] = state_dict[prefix + matrix_name] __snake_case : str = state_dict[prefix + matrix_name] __snake_case : Union[str, Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks __snake_case : Any = state_dict["entity_embeddings.entity_embeddings.weight"] __snake_case : List[str] = entity_emb[entity_vocab["[MASK]"]].unsqueeze(0 ) __snake_case : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' __snake_case : List[Any] = state_dict["entity_predictions.bias"] __snake_case : List[Any] = entity_prediction_bias[entity_vocab["[MASK]"]].unsqueeze(0 ) __snake_case : Union[str, Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) __snake_case : Any = LukeForMaskedLM(config=lowercase ).eval() state_dict.pop("entity_predictions.decoder.weight" ) state_dict.pop("lm_head.decoder.weight" ) state_dict.pop("lm_head.decoder.bias" ) __snake_case : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith("lm_head" ) or key.startswith("entity_predictions" )): __snake_case : str = state_dict[key] else: __snake_case : str = state_dict[key] __snake_case , __snake_case : Union[str, Any] = model.load_state_dict(lowercase , strict=lowercase ) if set(lowercase ) != {"luke.embeddings.position_ids"}: raise ValueError(f"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(lowercase ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(f"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs __snake_case : int = MLukeTokenizer.from_pretrained(lowercase , task="entity_classification" ) __snake_case : Tuple = "ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan)." __snake_case : Union[str, Any] = (0, 9) __snake_case : Optional[int] = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) __snake_case : Any = model(**lowercase ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base __snake_case : Optional[Any] = torch.Size((1, 33, 768) ) __snake_case : Optional[int] = torch.tensor([[0.0_8_9_2, 0.0_5_9_6, -0.2_8_1_9], [0.0_1_3_4, 0.1_1_9_9, 0.0_5_7_3], [-0.0_1_6_9, 0.0_9_2_7, 0.0_6_4_4]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowercase , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base __snake_case : str = torch.Size((1, 1, 768) ) __snake_case : int = torch.tensor([[-0.1_4_8_2, 0.0_6_0_9, 0.0_3_2_2]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" f""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , lowercase , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction __snake_case : str = MLukeTokenizer.from_pretrained(lowercase ) __snake_case : Dict = "Tokyo is the capital of <mask>." __snake_case : Union[str, Any] = (24, 30) __snake_case : int = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) __snake_case : int = model(**lowercase ) __snake_case : Dict = encoding["input_ids"][0].tolist() __snake_case : Dict = input_ids.index(tokenizer.convert_tokens_to_ids("<mask>" ) ) __snake_case : Optional[int] = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(lowercase ) __snake_case : Optional[Any] = outputs.entity_logits[0][0].argmax().item() __snake_case : Optional[int] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith("en:" )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print("Saving PyTorch model to {}".format(lowercase ) ) model.save_pretrained(lowercase ) def lowerCAmelCase__( lowercase : Optional[int] ) -> List[Any]: __snake_case : Any = ["[MASK]", "[PAD]", "[UNK]"] __snake_case : Any = [json.loads(lowercase ) for line in open(lowercase )] __snake_case : Any = {} for entry in data: __snake_case : Any = entry["id"] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: __snake_case : Optional[int] = entity_id break __snake_case : Union[str, Any] = f"""{language}:{entity_name}""" __snake_case : Any = entity_id return new_mapping if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) _UpperCamelCase = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
326
0
import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, PerceiverTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): lowerCAmelCase : int = """pt""" elif is_tf_available(): lowerCAmelCase : Tuple = """tf""" else: lowerCAmelCase : List[str] = """jax""" class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Union[str, Any] = PerceiverTokenizer _UpperCAmelCase : Union[str, Any] = False def _SCREAMING_SNAKE_CASE ( self : List[Any]): super().setUp() SCREAMING_SNAKE_CASE_: Any = PerceiverTokenizer() tokenizer.save_pretrained(self.tmpdirname) @cached_property def _SCREAMING_SNAKE_CASE ( self : List[str]): return PerceiverTokenizer.from_pretrained("deepmind/language-perceiver") def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , **lowerCAmelCase__ : Optional[Any]): return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Any=False , lowerCAmelCase__ : str=20 , lowerCAmelCase__ : Optional[Any]=5): SCREAMING_SNAKE_CASE_: int = [] for i in range(len(lowerCAmelCase__)): try: SCREAMING_SNAKE_CASE_: List[str] = tokenizer.decode([i] , clean_up_tokenization_spaces=lowerCAmelCase__) except UnicodeDecodeError: pass toks.append((i, tok)) SCREAMING_SNAKE_CASE_: Union[str, Any] = list(filter(lambda lowerCAmelCase__: re.match(R"^[ a-zA-Z]+$" , t[1]) , lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Tuple = list(filter(lambda lowerCAmelCase__: [t[0]] == tokenizer.encode(t[1] , add_special_tokens=lowerCAmelCase__) , lowerCAmelCase__)) if max_length is not None and len(lowerCAmelCase__) > max_length: SCREAMING_SNAKE_CASE_: Tuple = toks[:max_length] if min_length is not None and len(lowerCAmelCase__) < min_length and len(lowerCAmelCase__) > 0: while len(lowerCAmelCase__) < min_length: SCREAMING_SNAKE_CASE_: int = toks + toks # toks_str = [t[1] for t in toks] SCREAMING_SNAKE_CASE_: int = [t[0] for t in toks] # Ensure consistency SCREAMING_SNAKE_CASE_: Optional[int] = tokenizer.decode(lowerCAmelCase__ , clean_up_tokenization_spaces=lowerCAmelCase__) if " " not in output_txt and len(lowerCAmelCase__) > 1: SCREAMING_SNAKE_CASE_: List[str] = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=lowerCAmelCase__) + " " + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=lowerCAmelCase__) ) if with_prefix_space: SCREAMING_SNAKE_CASE_: Optional[int] = " " + output_txt SCREAMING_SNAKE_CASE_: int = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) return output_txt, output_ids def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[int] = self.perceiver_tokenizer SCREAMING_SNAKE_CASE_: List[str] = "Unicode €." SCREAMING_SNAKE_CASE_: str = tokenizer(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = [4, 91, 116, 111, 105, 117, 106, 107, 38, 232, 136, 178, 52, 5] self.assertEqual(encoded["input_ids"] , lowerCAmelCase__) # decoding SCREAMING_SNAKE_CASE_: Any = tokenizer.decode(lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , "[CLS]Unicode €.[SEP]") SCREAMING_SNAKE_CASE_: Tuple = tokenizer("e è é ê ë") SCREAMING_SNAKE_CASE_: Any = [4, 107, 38, 201, 174, 38, 201, 175, 38, 201, 176, 38, 201, 177, 5] self.assertEqual(encoded["input_ids"] , lowerCAmelCase__) # decoding SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.decode(lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , "[CLS]e è é ê ë[SEP]") # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("e è é ê ë")) , "[CLS]e è é ê ë[SEP]") def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Optional[int] = self.perceiver_tokenizer SCREAMING_SNAKE_CASE_: Union[str, Any] = ["A long paragraph for summarization.", "Another paragraph for summarization."] # fmt: off SCREAMING_SNAKE_CASE_: Dict = [4, 71, 38, 114, 117, 116, 109, 38, 118, 103, 120, 103, 109, 120, 103, 118, 110, 38, 108, 117, 120, 38, 121, 123, 115, 115, 103, 120, 111, 128, 103, 122, 111, 117, 116, 52, 5, 0] # fmt: on SCREAMING_SNAKE_CASE_: Dict = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors=lowerCAmelCase__) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__) if FRAMEWORK != "jax": SCREAMING_SNAKE_CASE_: Any = list(batch.input_ids.numpy()[0]) else: SCREAMING_SNAKE_CASE_: Dict = list(batch.input_ids.tolist()[0]) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) self.assertEqual((2, 38) , batch.input_ids.shape) self.assertEqual((2, 38) , batch.attention_mask.shape) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: str = self.perceiver_tokenizer SCREAMING_SNAKE_CASE_: Tuple = ["A long paragraph for summarization.", "Another paragraph for summarization."] SCREAMING_SNAKE_CASE_: Tuple = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors=lowerCAmelCase__) # check if input_ids are returned and no decoder_input_ids self.assertIn("input_ids" , lowerCAmelCase__) self.assertIn("attention_mask" , lowerCAmelCase__) self.assertNotIn("decoder_input_ids" , lowerCAmelCase__) self.assertNotIn("decoder_attention_mask" , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.perceiver_tokenizer SCREAMING_SNAKE_CASE_: Any = [ "Summary of the text.", "Another summary.", ] SCREAMING_SNAKE_CASE_: Any = tokenizer( text_target=lowerCAmelCase__ , max_length=32 , padding="max_length" , truncation=lowerCAmelCase__ , return_tensors=lowerCAmelCase__) self.assertEqual(32 , targets["input_ids"].shape[1]) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"{tokenizer.__class__.__name__}"): self.assertNotEqual(tokenizer.model_max_length , 42) # Now let's start the test SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"{tokenizer.__class__.__name__}"): # Isolate this from the other tests because we save additional tokens/etc SCREAMING_SNAKE_CASE_: Tuple = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_: Union[str, Any] = " He is very happy, UNwant\u00E9d,running" SCREAMING_SNAKE_CASE_: str = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) tokenizer.save_pretrained(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = tokenizer.__class__.from_pretrained(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = after_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) shutil.rmtree(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = self.get_tokenizers(model_max_length=42) for tokenizer in tokenizers: with self.subTest(F"{tokenizer.__class__.__name__}"): # Isolate this from the other tests because we save additional tokens/etc SCREAMING_SNAKE_CASE_: Any = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_: List[Any] = " He is very happy, UNwant\u00E9d,running" tokenizer.add_tokens(["bim", "bambam"]) SCREAMING_SNAKE_CASE_: str = tokenizer.additional_special_tokens additional_special_tokens.append("new_additional_special_token") tokenizer.add_special_tokens({"additional_special_tokens": additional_special_tokens}) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) tokenizer.save_pretrained(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = tokenizer.__class__.from_pretrained(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = after_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) self.assertIn("new_additional_special_token" , after_tokenizer.additional_special_tokens) self.assertEqual(after_tokenizer.model_max_length , 42) SCREAMING_SNAKE_CASE_: Optional[Any] = tokenizer.__class__.from_pretrained(lowerCAmelCase__ , model_max_length=43) self.assertEqual(tokenizer.model_max_length , 43) shutil.rmtree(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Union[str, Any] = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer())) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer())) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(lowerCAmelCase__) with open(os.path.join(lowerCAmelCase__ , "special_tokens_map.json") , encoding="utf-8") as json_file: SCREAMING_SNAKE_CASE_: List[Any] = json.load(lowerCAmelCase__) with open(os.path.join(lowerCAmelCase__ , "tokenizer_config.json") , encoding="utf-8") as json_file: SCREAMING_SNAKE_CASE_: str = json.load(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = [F"<extra_id_{i}>" for i in range(125)] SCREAMING_SNAKE_CASE_: Any = added_tokens_extra_ids + [ "an_additional_special_token" ] SCREAMING_SNAKE_CASE_: Union[str, Any] = added_tokens_extra_ids + [ "an_additional_special_token" ] with open(os.path.join(lowerCAmelCase__ , "special_tokens_map.json") , "w" , encoding="utf-8") as outfile: json.dump(lowerCAmelCase__ , lowerCAmelCase__) with open(os.path.join(lowerCAmelCase__ , "tokenizer_config.json") , "w" , encoding="utf-8") as outfile: json.dump(lowerCAmelCase__ , lowerCAmelCase__) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files SCREAMING_SNAKE_CASE_: List[Any] = tokenizer_class.from_pretrained( lowerCAmelCase__ , ) self.assertIn( "an_additional_special_token" , tokenizer_without_change_in_init.additional_special_tokens) self.assertEqual( ["an_additional_special_token"] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["an_additional_special_token"])) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained SCREAMING_SNAKE_CASE_: List[str] = added_tokens_extra_ids + [AddedToken("a_new_additional_special_token" , lstrip=lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: List[str] = tokenizer_class.from_pretrained( lowerCAmelCase__ , additional_special_tokens=lowerCAmelCase__ , ) self.assertIn("a_new_additional_special_token" , tokenizer.additional_special_tokens) self.assertEqual( ["a_new_additional_special_token"] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(["a_new_additional_special_token"])) , ) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Dict = self.perceiver_tokenizer self.assertEqual(tokenizer.decode([178]) , "�") def _SCREAMING_SNAKE_CASE ( self : List[str]): pass def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): pass def _SCREAMING_SNAKE_CASE ( self : int): pass def _SCREAMING_SNAKE_CASE ( self : str): pass def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Tuple = self.get_tokenizers(fast=lowerCAmelCase__ , do_lower_case=lowerCAmelCase__) for tokenizer in tokenizers: with self.subTest(F"{tokenizer.__class__.__name__}"): SCREAMING_SNAKE_CASE_: List[Any] = ["[CLS]", "t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "s", "t", "[SEP]"] SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.convert_tokens_to_string(lowerCAmelCase__) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__)
13
from maths.prime_factors import prime_factors def lowerCAmelCase__( lowercase : int ) -> int: if not isinstance(lowercase , lowercase ): __snake_case : Optional[int] = f"""Input value of [number={number}] must be an integer""" raise TypeError(lowercase ) if number < 1: raise ValueError("Input must be a positive integer" ) return -1 if len(prime_factors(lowercase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
326
0
import datasets _A = '''\ @InProceedings{conneau2018xnli, author = "Conneau, Alexis and Rinott, Ruty and Lample, Guillaume and Williams, Adina and Bowman, Samuel R. and Schwenk, Holger and Stoyanov, Veselin", title = "XNLI: Evaluating Cross-lingual Sentence Representations", booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing", year = "2018", publisher = "Association for Computational Linguistics", location = "Brussels, Belgium", } ''' _A = '''\ XNLI is a subset of a few thousand examples from MNLI which has been translated into a 14 different languages (some low-ish resource). As with MNLI, the goal is to predict textual entailment (does sentence A imply/contradict/neither sentence B) and is a classification task (given two sentences, predict one of three labels). ''' _A = ''' Computes XNLI score which is just simple accuracy. Args: predictions: Predicted labels. references: Ground truth labels. Returns: \'accuracy\': accuracy Examples: >>> predictions = [0, 1] >>> references = [0, 1] >>> xnli_metric = datasets.load_metric("xnli") >>> results = xnli_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} ''' def __UpperCamelCase ( _A , _A ): return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A ( datasets.Metric ): def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32''' ), '''references''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32''' ), } ), codebase_urls=[], reference_urls=[], format='''numpy''', ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__ ): """simple docstring""" return {"accuracy": simple_accuracy(UpperCamelCase__, UpperCamelCase__ )}
278
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" @slow def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' __snake_case : Tuple = FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small" ) __snake_case : str = AutoTokenizer.from_pretrained("google/mt5-small" ) __snake_case : List[Any] = tokenizer("Hello there" , return_tensors="np" ).input_ids __snake_case : int = tokenizer("Hi I am" , return_tensors="np" ).input_ids __snake_case : Tuple = shift_tokens_right(UpperCAmelCase , model.config.pad_token_id , model.config.decoder_start_token_id ) __snake_case : Tuple = model(UpperCAmelCase , decoder_input_ids=UpperCAmelCase ).logits __snake_case : str = optax.softmax_cross_entropy(UpperCAmelCase , onehot(UpperCAmelCase , logits.shape[-1] ) ).mean() __snake_case : Any = -(labels.shape[-1] * loss.item()) __snake_case : List[str] = -84.9_127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
326
0
'''simple docstring''' def _a( UpperCamelCase__ : int = 1_0_0_0 ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] =1, 1 SCREAMING_SNAKE_CASE__ : List[str] =2 while True: SCREAMING_SNAKE_CASE__ : Optional[Any] =0 SCREAMING_SNAKE_CASE__ : Any =fa + fa SCREAMING_SNAKE_CASE__ : Optional[int] =fa, f index += 1 for _ in str(UpperCamelCase__ ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
152
import torch from torch import nn from transformers import CLIPPreTrainedModel, CLIPVisionModel from ...models.attention import BasicTransformerBlock from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name class _lowerCamelCase ( a ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=768 ) -> List[str]: '''simple docstring''' super().__init__(UpperCAmelCase ) __snake_case : Optional[int] = proj_size __snake_case : str = CLIPVisionModel(UpperCAmelCase ) __snake_case : Tuple = PaintByExampleMapper(UpperCAmelCase ) __snake_case : Union[str, Any] = nn.LayerNorm(config.hidden_size ) __snake_case : Optional[Any] = nn.Linear(config.hidden_size , self.proj_size ) # uncondition for scaling __snake_case : Optional[int] = nn.Parameter(torch.randn((1, 1, self.proj_size) ) ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase=False ) -> List[str]: '''simple docstring''' __snake_case : int = self.model(pixel_values=UpperCAmelCase ) __snake_case : Optional[int] = clip_output.pooler_output __snake_case : Any = self.mapper(latent_states[:, None] ) __snake_case : Any = self.final_layer_norm(UpperCAmelCase ) __snake_case : str = self.proj_out(UpperCAmelCase ) if return_uncond_vector: return latent_states, self.uncond_vector return latent_states class _lowerCamelCase ( nn.Module ): """simple docstring""" def __init__( self , UpperCAmelCase ) -> List[Any]: '''simple docstring''' super().__init__() __snake_case : List[Any] = (config.num_hidden_layers + 1) // 5 __snake_case : Dict = config.hidden_size __snake_case : str = 1 __snake_case : List[Any] = nn.ModuleList( [ BasicTransformerBlock(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , activation_fn="gelu" , attention_bias=UpperCAmelCase ) for _ in range(UpperCAmelCase ) ] ) def UpperCAmelCase ( self , UpperCAmelCase ) -> str: '''simple docstring''' for block in self.blocks: __snake_case : int = block(UpperCAmelCase ) return hidden_states
326
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_dpt import DPTImageProcessor _UpperCamelCase : Tuple = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): def __init__( self , *a , **a ) -> None: warnings.warn( 'The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use DPTImageProcessor instead.' , a , ) super().__init__(*a , **a )
77
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
326
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() __lowercase = logging.get_logger(__name__) __lowercase = torch.device('''cpu''') def lowerCamelCase ( ): '''simple docstring''' __UpperCamelCase :List[Any] = "http://images.cocodataset.org/val2017/000000039769.jpg" __UpperCamelCase :Optional[int] = Image.open(requests.get(SCREAMING_SNAKE_CASE , stream=SCREAMING_SNAKE_CASE ).raw ) return im def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1_703e00, 2.1_107e00, -2.0_811e00, 8.8_685e-01, 2.4_360e-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9_636e-01, 2.3_478e-01, -1.6_963e00, -1.7_381e00, -8.6_337e-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2_768e-01, -4.7_429e-01, -1.0_897e00, -1.0_248e00, 3.5_523e-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5_330e-01, 2.4_211e-01, -6.0_185e-01, -8.2_789e-01, -6.0_446e-02] ) def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :List[Any] = dct.pop(SCREAMING_SNAKE_CASE ) __UpperCamelCase :List[Any] = val def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :Optional[Any] = [] for k in state_dict.keys(): __UpperCamelCase :Union[str, Any] = k if ".pwconv" in k: __UpperCamelCase :Any = k_new.replace('''.pwconv''' , '''.point_wise_conv''' ) if ".dwconv" in k: __UpperCamelCase :List[Any] = k_new.replace('''.dwconv''' , '''.depth_wise_conv''' ) if ".Proj." in k: __UpperCamelCase :Optional[int] = k_new.replace('''.Proj.''' , '''.proj.''' ) if "patch_embed" in k_new: __UpperCamelCase :int = k_new.replace('''patch_embed''' , '''swiftformer.patch_embed.patch_embedding''' ) if "network" in k_new: __UpperCamelCase :int = k_new.split('''.''' ) if ls[2].isdigit(): __UpperCamelCase :List[Any] = "swiftformer.encoder.network." + ls[1] + ".blocks." + ls[2] + "." + ".".join(ls[3:] ) else: __UpperCamelCase :Optional[int] = k_new.replace('''network''' , '''swiftformer.encoder.network''' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :List[str] = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size __UpperCamelCase :Tuple = 1_000 __UpperCamelCase :Any = "huggingface/label-files" __UpperCamelCase :int = "imagenet-1k-id2label.json" __UpperCamelCase :Dict = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) , '''r''' ) ) __UpperCamelCase :str = {int(SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} __UpperCamelCase :int = idalabel __UpperCamelCase :Optional[int] = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": __UpperCamelCase :Optional[Any] = [3, 3, 6, 4] __UpperCamelCase :Optional[int] = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": __UpperCamelCase :List[str] = [3, 3, 9, 6] __UpperCamelCase :Optional[Any] = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": __UpperCamelCase :Optional[int] = [4, 3, 10, 5] __UpperCamelCase :Dict = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": __UpperCamelCase :str = [4, 4, 12, 6] __UpperCamelCase :Optional[Any] = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('''https''' ): __UpperCamelCase :Optional[Any] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE , map_location='''cpu''' , check_hash=SCREAMING_SNAKE_CASE ) else: __UpperCamelCase :Tuple = torch.load(SCREAMING_SNAKE_CASE , map_location='''cpu''' ) __UpperCamelCase :Optional[int] = checkpoint __UpperCamelCase :Any = create_rename_keys(SCREAMING_SNAKE_CASE ) for rename_key_src, rename_key_dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # load HuggingFace model __UpperCamelCase :Tuple = SwiftFormerForImageClassification(SCREAMING_SNAKE_CASE ).eval() hf_model.load_state_dict(SCREAMING_SNAKE_CASE ) # prepare test inputs __UpperCamelCase :Optional[Any] = prepare_img() __UpperCamelCase :str = ViTImageProcessor.from_pretrained('''preprocessor_config''' ) __UpperCamelCase :Optional[int] = processor(images=SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) # compare outputs from both models __UpperCamelCase :str = get_expected_output(SCREAMING_SNAKE_CASE ) __UpperCamelCase :Optional[int] = hf_model(inputs['''pixel_values'''] ).logits assert hf_logits.shape == torch.Size([1, 1_000] ) assert torch.allclose(hf_logits[0, 0:5] , SCREAMING_SNAKE_CASE , atol=1e-3 ) Path(SCREAMING_SNAKE_CASE ).mkdir(exist_ok=SCREAMING_SNAKE_CASE ) print(f"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": __lowercase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--swiftformer_name''', default='''swiftformer_xs''', choices=['''swiftformer_xs''', '''swiftformer_s''', '''swiftformer_l1''', '''swiftformer_l3'''], type=str, help='''Name of the SwiftFormer model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''./converted_outputs/''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--original_ckpt''', default=None, type=str, help='''Path to the original model checkpoint.''') __lowercase = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
43
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = torch.device('''cpu''') def lowerCAmelCase__( ) -> Any: __snake_case : List[Any] = "http://images.cocodataset.org/val2017/000000039769.jpg" __snake_case : Optional[int] = Image.open(requests.get(lowercase , stream=lowercase ).raw ) return im def lowerCAmelCase__( lowercase : Dict ) -> List[Any]: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1_703E00, 2.1_107E00, -2.0_811E00, 8.8_685E-01, 2.4_360E-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9_636E-01, 2.3_478E-01, -1.6_963E00, -1.7_381E00, -8.6_337E-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2_768E-01, -4.7_429E-01, -1.0_897E00, -1.0_248E00, 3.5_523E-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5_330E-01, 2.4_211E-01, -6.0_185E-01, -8.2_789E-01, -6.0_446E-02] ) def lowerCAmelCase__( lowercase : Tuple , lowercase : Union[str, Any] , lowercase : Union[str, Any] ) -> List[Any]: __snake_case : List[Any] = dct.pop(lowercase ) __snake_case : List[Any] = val def lowerCAmelCase__( lowercase : Union[str, Any] ) -> Tuple: __snake_case : Optional[Any] = [] for k in state_dict.keys(): __snake_case : Union[str, Any] = k if ".pwconv" in k: __snake_case : Any = k_new.replace(".pwconv" , ".point_wise_conv" ) if ".dwconv" in k: __snake_case : List[Any] = k_new.replace(".dwconv" , ".depth_wise_conv" ) if ".Proj." in k: __snake_case : Optional[int] = k_new.replace(".Proj." , ".proj." ) if "patch_embed" in k_new: __snake_case : int = k_new.replace("patch_embed" , "swiftformer.patch_embed.patch_embedding" ) if "network" in k_new: __snake_case : int = k_new.split("." ) if ls[2].isdigit(): __snake_case : List[Any] = "swiftformer.encoder.network." + ls[1] + ".blocks." + ls[2] + "." + ".".join(ls[3:] ) else: __snake_case : Optional[int] = k_new.replace("network" , "swiftformer.encoder.network" ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def lowerCAmelCase__( lowercase : List[Any] , lowercase : Optional[Any] , lowercase : List[str] ) -> Union[str, Any]: __snake_case : List[str] = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size __snake_case : Tuple = 1000 __snake_case : Any = "huggingface/label-files" __snake_case : int = "imagenet-1k-id2label.json" __snake_case : Dict = json.load(open(hf_hub_download(lowercase , lowercase , repo_type="dataset" ) , "r" ) ) __snake_case : str = {int(lowercase ): v for k, v in idalabel.items()} __snake_case : int = idalabel __snake_case : Optional[int] = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": __snake_case : Optional[Any] = [3, 3, 6, 4] __snake_case : Optional[int] = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": __snake_case : List[str] = [3, 3, 9, 6] __snake_case : Optional[Any] = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": __snake_case : Optional[int] = [4, 3, 10, 5] __snake_case : Dict = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": __snake_case : str = [4, 4, 12, 6] __snake_case : Optional[Any] = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith("https" ): __snake_case : Optional[Any] = torch.hub.load_state_dict_from_url(lowercase , map_location="cpu" , check_hash=lowercase ) else: __snake_case : Tuple = torch.load(lowercase , map_location="cpu" ) __snake_case : Optional[int] = checkpoint __snake_case : Any = create_rename_keys(lowercase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(lowercase , lowercase , lowercase ) # load HuggingFace model __snake_case : Tuple = SwiftFormerForImageClassification(lowercase ).eval() hf_model.load_state_dict(lowercase ) # prepare test inputs __snake_case : Optional[Any] = prepare_img() __snake_case : str = ViTImageProcessor.from_pretrained("preprocessor_config" ) __snake_case : Optional[int] = processor(images=lowercase , return_tensors="pt" ) # compare outputs from both models __snake_case : str = get_expected_output(lowercase ) __snake_case : Optional[int] = hf_model(inputs["pixel_values"] ).logits assert hf_logits.shape == torch.Size([1, 1000] ) assert torch.allclose(hf_logits[0, 0:5] , lowercase , atol=1E-3 ) Path(lowercase ).mkdir(exist_ok=lowercase ) print(f"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(lowercase ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--swiftformer_name''', default='''swiftformer_xs''', choices=['''swiftformer_xs''', '''swiftformer_s''', '''swiftformer_l1''', '''swiftformer_l3'''], type=str, help='''Name of the SwiftFormer model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''./converted_outputs/''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--original_ckpt''', default=None, type=str, help='''Path to the original model checkpoint.''') _UpperCamelCase = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
326
0
'''simple docstring''' import os from typing import Dict, List, Union import tensorflow as tf from keras_nlp.tokenizers import BytePairTokenizer from tensorflow_text import pad_model_inputs from .tokenization_gpta import GPTaTokenizer class _snake_case ( tf.keras.layers.Layer ): def __init__( self , a__ , a__ , a__ = None , a__ = None ) -> Tuple: '''simple docstring''' super().__init__() snake_case_ = pad_token_id snake_case_ = max_length snake_case_ = vocab snake_case_ = merges snake_case_ = BytePairTokenizer(a__ , a__ , sequence_length=a__ ) @classmethod def lowerCAmelCase__ ( cls , a__ , *a__ , **a__ ) -> List[str]: '''simple docstring''' snake_case_ = [" ".join(a__ ) for m in tokenizer.bpe_ranks.keys()] snake_case_ = tokenizer.get_vocab() return cls(a__ , a__ , *a__ , **a__ ) @classmethod def lowerCAmelCase__ ( cls , a__ , *a__ , **a__ ) -> Union[str, Any]: '''simple docstring''' snake_case_ = GPTaTokenizer.from_pretrained(a__ , *a__ , **a__ ) return cls.from_tokenizer(a__ , *a__ , **a__ ) @classmethod def lowerCAmelCase__ ( cls , a__ ) -> str: '''simple docstring''' return cls(**a__ ) def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def lowerCAmelCase__ ( self , a__ , a__ = None ) -> List[str]: '''simple docstring''' snake_case_ = self.tf_tokenizer(a__ ) snake_case_ = tf.ones_like(a__ ) if self.pad_token_id is not None: # pad the tokens up to max length snake_case_ = max_length if max_length is not None else self.max_length if max_length is not None: snake_case_ = pad_model_inputs( a__ , max_seq_length=a__ , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
85
import json import logging import os import socket import git import numpy as np import torch logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO, ) _UpperCamelCase = logging.getLogger(__name__) def lowerCAmelCase__( lowercase : str ) -> List[str]: __snake_case : int = git.Repo(search_parent_directories=lowercase ) __snake_case : Union[str, Any] = { "repo_id": str(lowercase ), "repo_sha": str(repo.head.object.hexsha ), "repo_branch": str(repo.active_branch ), } with open(os.path.join(lowercase , "git_log.json" ) , "w" ) as f: json.dump(lowercase , lowercase , indent=4 ) def lowerCAmelCase__( lowercase : Optional[Any] ) -> Optional[Any]: if params.n_gpu <= 0: __snake_case : Union[str, Any] = 0 __snake_case : Optional[int] = -1 __snake_case : Union[str, Any] = True __snake_case : Tuple = False return assert torch.cuda.is_available() logger.info("Initializing GPUs" ) if params.n_gpu > 1: assert params.local_rank != -1 __snake_case : Optional[int] = int(os.environ["WORLD_SIZE"] ) __snake_case : int = int(os.environ["N_GPU_NODE"] ) __snake_case : Union[str, Any] = int(os.environ["RANK"] ) # number of nodes / node ID __snake_case : Optional[Any] = params.world_size // params.n_gpu_per_node __snake_case : Optional[Any] = params.global_rank // params.n_gpu_per_node __snake_case : Union[str, Any] = True assert params.n_nodes == int(os.environ["N_NODES"] ) assert params.node_id == int(os.environ["NODE_RANK"] ) # local job (single GPU) else: assert params.local_rank == -1 __snake_case : Any = 1 __snake_case : str = 0 __snake_case : Optional[Any] = 0 __snake_case : Dict = 0 __snake_case : int = 1 __snake_case : Optional[Any] = 1 __snake_case : Tuple = False # sanity checks assert params.n_nodes >= 1 assert 0 <= params.node_id < params.n_nodes assert 0 <= params.local_rank <= params.global_rank < params.world_size assert params.world_size == params.n_nodes * params.n_gpu_per_node # define whether this is the master process / if we are in multi-node distributed mode __snake_case : List[Any] = params.node_id == 0 and params.local_rank == 0 __snake_case : List[Any] = params.n_nodes > 1 # summary __snake_case : List[Any] = f"""--- Global rank: {params.global_rank} - """ logger.info(PREFIX + "Number of nodes: %i" % params.n_nodes ) logger.info(PREFIX + "Node ID : %i" % params.node_id ) logger.info(PREFIX + "Local rank : %i" % params.local_rank ) logger.info(PREFIX + "World size : %i" % params.world_size ) logger.info(PREFIX + "GPUs per node : %i" % params.n_gpu_per_node ) logger.info(PREFIX + "Master : %s" % str(params.is_master ) ) logger.info(PREFIX + "Multi-node : %s" % str(params.multi_node ) ) logger.info(PREFIX + "Multi-GPU : %s" % str(params.multi_gpu ) ) logger.info(PREFIX + "Hostname : %s" % socket.gethostname() ) # set GPU device torch.cuda.set_device(params.local_rank ) # initialize multi-GPU if params.multi_gpu: logger.info("Initializing PyTorch distributed" ) torch.distributed.init_process_group( init_method="env://" , backend="nccl" , ) def lowerCAmelCase__( lowercase : Dict ) -> Union[str, Any]: np.random.seed(args.seed ) torch.manual_seed(args.seed ) if args.n_gpu > 0: torch.cuda.manual_seed_all(args.seed )
326
0
"""simple docstring""" import copy from collections import OrderedDict from typing import Dict, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Tuple = { '''facebook/detr-resnet-50''': '''https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json''', # See all DETR models at https://huggingface.co/models?filter=detr } class a ( __snake_case ): SCREAMING_SNAKE_CASE : Union[str, Any] = "detr" SCREAMING_SNAKE_CASE : Optional[int] = ["past_key_values"] SCREAMING_SNAKE_CASE : Optional[int] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[int]=True , __SCREAMING_SNAKE_CASE : int=None , __SCREAMING_SNAKE_CASE : Union[str, Any]=3 , __SCREAMING_SNAKE_CASE : Union[str, Any]=100 , __SCREAMING_SNAKE_CASE : List[Any]=6 , __SCREAMING_SNAKE_CASE : Any=2048 , __SCREAMING_SNAKE_CASE : Optional[Any]=8 , __SCREAMING_SNAKE_CASE : Optional[Any]=6 , __SCREAMING_SNAKE_CASE : Union[str, Any]=2048 , __SCREAMING_SNAKE_CASE : str=8 , __SCREAMING_SNAKE_CASE : List[str]=0.0 , __SCREAMING_SNAKE_CASE : Tuple=0.0 , __SCREAMING_SNAKE_CASE : Optional[int]=True , __SCREAMING_SNAKE_CASE : List[Any]="relu" , __SCREAMING_SNAKE_CASE : Optional[int]=256 , __SCREAMING_SNAKE_CASE : Dict=0.1 , __SCREAMING_SNAKE_CASE : str=0.0 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.0 , __SCREAMING_SNAKE_CASE : Tuple=0.02 , __SCREAMING_SNAKE_CASE : int=1.0 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : str="sine" , __SCREAMING_SNAKE_CASE : Tuple="resnet50" , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : Dict=False , __SCREAMING_SNAKE_CASE : Any=1 , __SCREAMING_SNAKE_CASE : Optional[Any]=5 , __SCREAMING_SNAKE_CASE : Optional[Any]=2 , __SCREAMING_SNAKE_CASE : List[str]=1 , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : Optional[int]=5 , __SCREAMING_SNAKE_CASE : str=2 , __SCREAMING_SNAKE_CASE : Dict=0.1 , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> List[Any]: if backbone_config is not None and use_timm_backbone: raise ValueError('You can\'t specify both `backbone_config` and `use_timm_backbone`.' ) if not use_timm_backbone: if backbone_config is None: logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.' ) lowerCamelCase_ = CONFIG_MAPPING["resnet"](out_features=['stage4'] ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCamelCase_ = backbone_config.get('model_type' ) lowerCamelCase_ = CONFIG_MAPPING[backbone_model_type] lowerCamelCase_ = config_class.from_dict(__SCREAMING_SNAKE_CASE ) # set timm attributes to None lowerCamelCase_ = None, None, None lowerCamelCase_ = use_timm_backbone lowerCamelCase_ = backbone_config lowerCamelCase_ = num_channels lowerCamelCase_ = num_queries lowerCamelCase_ = d_model lowerCamelCase_ = encoder_ffn_dim lowerCamelCase_ = encoder_layers lowerCamelCase_ = encoder_attention_heads lowerCamelCase_ = decoder_ffn_dim lowerCamelCase_ = decoder_layers lowerCamelCase_ = decoder_attention_heads lowerCamelCase_ = dropout lowerCamelCase_ = attention_dropout lowerCamelCase_ = activation_dropout lowerCamelCase_ = activation_function lowerCamelCase_ = init_std lowerCamelCase_ = init_xavier_std lowerCamelCase_ = encoder_layerdrop lowerCamelCase_ = decoder_layerdrop lowerCamelCase_ = encoder_layers lowerCamelCase_ = auxiliary_loss lowerCamelCase_ = position_embedding_type lowerCamelCase_ = backbone lowerCamelCase_ = use_pretrained_backbone lowerCamelCase_ = dilation # Hungarian matcher lowerCamelCase_ = class_cost lowerCamelCase_ = bbox_cost lowerCamelCase_ = giou_cost # Loss coefficients lowerCamelCase_ = mask_loss_coefficient lowerCamelCase_ = dice_loss_coefficient lowerCamelCase_ = bbox_loss_coefficient lowerCamelCase_ = giou_loss_coefficient lowerCamelCase_ = eos_coefficient super().__init__(is_encoder_decoder=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def UpperCamelCase ( self : Optional[Any] ) -> int: return self.encoder_attention_heads @property def UpperCamelCase ( self : List[Any] ) -> int: return self.d_model @classmethod def UpperCamelCase ( cls : int , __SCREAMING_SNAKE_CASE : int , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: return cls(backbone_config=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : str ) -> Dict[str, any]: lowerCamelCase_ = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: lowerCamelCase_ = self.backbone_config.to_dict() lowerCamelCase_ = self.__class__.model_type return output class a ( __snake_case ): SCREAMING_SNAKE_CASE : Dict = version.parse("""1.11""" ) @property def UpperCamelCase ( self : Any ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ('pixel_mask', {0: 'batch'}), ] ) @property def UpperCamelCase ( self : Optional[int] ) -> float: return 1e-5 @property def UpperCamelCase ( self : Union[str, Any] ) -> int: return 12
183
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : str =JukeboxTokenizer UpperCAmelCase_ : Tuple ={ "artist": "Zac Brown Band", "genres": "Country", "lyrics": "I met a traveller from an antique land,\n Who said \"Two vast and trunkless legs of stone\n Stand in the desert. . . . Near them, on the sand,\n Half sunk a shattered visage lies, whose frown,\n And wrinkled lip, and sneer of cold command,\n Tell that its sculptor well those passions read\n Which yet survive, stamped on these lifeless things,\n The hand that mocked them, and the heart that fed;\n And on the pedestal, these words appear:\n My name is Ozymandias, King of Kings;\n Look on my Works, ye Mighty, and despair!\n Nothing beside remains. Round the decay\n Of that colossal Wreck, boundless and bare\n The lone and level sands stretch far away\n ", } @require_torch def UpperCAmelCase ( self ) -> str: '''simple docstring''' import torch __snake_case : List[str] = JukeboxTokenizer.from_pretrained("openai/jukebox-1b-lyrics" ) __snake_case : Union[str, Any] = tokenizer(**self.metas )["input_ids"] # fmt: off __snake_case : Optional[Any] = [ torch.tensor([[ 0, 0, 0, 7169, 507, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) ) @require_torch def UpperCAmelCase ( self ) -> str: '''simple docstring''' import torch __snake_case : Optional[Any] = JukeboxTokenizer.from_pretrained("openai/jukebox-5b-lyrics" ) __snake_case : Tuple = tokenizer(**self.metas )["input_ids"] # fmt: off __snake_case : int = [ torch.tensor([[ 0, 0, 0, 1069, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) )
326
0
__lowerCAmelCase : List[Any] = 'Alexander Joslin' import operator as op from .stack import Stack def a__ ( A_ ): '''simple docstring''' __magic_name__ = {"*": op.mul, "/": op.truediv, "+": op.add, "-": op.sub} __magic_name__ = Stack() __magic_name__ = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(A_ ) ) elif i in operators: # RULE 2 operator_stack.push(A_ ) elif i == ")": # RULE 4 __magic_name__ = operator_stack.peek() operator_stack.pop() __magic_name__ = operand_stack.peek() operand_stack.pop() __magic_name__ = operand_stack.peek() operand_stack.pop() __magic_name__ = operators[opr](A_, A_ ) operand_stack.push(A_ ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": __lowerCAmelCase : Optional[int] = '(5 + ((4 * 2) * (2 + 3)))' # answer = 45 print(F'''{equation} = {dijkstras_two_stack_algorithm(equation)}''')
88
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 _UpperCamelCase = logging.get_logger(__name__) class _lowerCamelCase : """simple docstring""" UpperCAmelCase_ : str UpperCAmelCase_ : str =None @staticmethod def UpperCAmelCase ( ) -> Optional[int]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> List[str]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self , UpperCAmelCase ) -> Optional[int]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self ) -> Dict: '''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 UpperCAmelCase ( cls ) -> Tuple: '''simple docstring''' return F"""`pip install {cls.pip_package or cls.name}`""" class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Optional[int] ="optuna" @staticmethod def UpperCAmelCase ( ) -> Union[str, Any]: '''simple docstring''' return is_optuna_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: '''simple docstring''' return run_hp_search_optuna(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> int: '''simple docstring''' return default_hp_space_optuna(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : List[str] ="ray" UpperCAmelCase_ : Dict ="'ray[tune]'" @staticmethod def UpperCAmelCase ( ) -> str: '''simple docstring''' return is_ray_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> List[Any]: '''simple docstring''' return run_hp_search_ray(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> str: '''simple docstring''' return default_hp_space_ray(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Tuple ="sigopt" @staticmethod def UpperCAmelCase ( ) -> int: '''simple docstring''' return is_sigopt_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return run_hp_search_sigopt(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> Dict: '''simple docstring''' return default_hp_space_sigopt(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : str ="wandb" @staticmethod def UpperCAmelCase ( ) -> Optional[Any]: '''simple docstring''' return is_wandb_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' return run_hp_search_wandb(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> List[str]: '''simple docstring''' return default_hp_space_wandb(UpperCAmelCase ) _UpperCamelCase = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def lowerCAmelCase__( ) -> str: __snake_case : Optional[int] = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(lowercase ) > 0: __snake_case : Dict = available_backends[0].name if len(lowercase ) > 1: logger.info( f"""{len(lowercase )} 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() ) )
326
0
import unittest from transformers import is_tf_available from transformers.testing_utils import require_tf if is_tf_available(): import tensorflow as tf from tensorflow.python.eager import context from tensorflow.python.framework import ops from transformers import GradientAccumulator, create_optimizer @require_tf class lowercase ( unittest.TestCase ): def __UpperCamelCase ( self , A_ , A_ , A_ ) -> Optional[Any]: """simple docstring""" self.assertEqual(len(A_ ) , len(A_ ) ) for a, b in zip(A_ , A_ ): self.assertAlmostEqual(A_ , A_ , delta=A_ ) def __UpperCamelCase ( self ) -> Union[str, Any]: """simple docstring""" UpperCamelCase = GradientAccumulator() accumulator([tf.constant([1.0, 2.0] )] ) accumulator([tf.constant([-2.0, 1.0] )] ) accumulator([tf.constant([-1.0, 2.0] )] ) with self.assertRaises(A_ ): accumulator([tf.constant([1.0, 1.0] ), tf.constant([2.0, 2.0] )] ) self.assertEqual(accumulator.step , 3 ) self.assertEqual(len(accumulator.gradients ) , 1 ) self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist() , [-2.0, 5.0] , tol=1e-2 ) accumulator.reset() self.assertEqual(accumulator.step , 0 ) self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist() , [0.0, 0.0] , tol=1e-2 ) def __UpperCamelCase ( self ) -> List[str]: """simple docstring""" UpperCamelCase = None ops.enable_eager_execution_internal() UpperCamelCase = tf.config.list_physical_devices('CPU' ) if len(A_ ) == 1: tf.config.set_logical_device_configuration( physical_devices[0] , [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()] ) UpperCamelCase = tf.config.list_logical_devices(device_type='CPU' ) UpperCamelCase = tf.distribute.MirroredStrategy(devices=devices[:2] ) with strategy.scope(): UpperCamelCase = GradientAccumulator() UpperCamelCase = tf.Variable([4.0, 3.0] ) UpperCamelCase = create_optimizer(5e-5 , 10 , 5 ) UpperCamelCase = tf.Variable([0.0, 0.0] , trainable=A_ ) def accumulate_on_replica(A_ ): accumulator([gradient] ) def apply_on_replica(): optimizer.apply_gradients(list(zip(accumulator.gradients , [variable] ) ) ) @tf.function def accumulate(A_ , A_ ): with strategy.scope(): UpperCamelCase = strategy.experimental_local_results(A_ ) local_variables[0].assign(A_ ) local_variables[1].assign(A_ ) strategy.run(A_ , args=(gradient_placeholder,) ) @tf.function def apply_grad(): with strategy.scope(): strategy.run(A_ ) def _check_local_values(A_ , A_ ): UpperCamelCase = strategy.experimental_local_results(accumulator._gradients[0] ) self.assertListAlmostEqual(values[0].value() , A_ , tol=1e-2 ) self.assertListAlmostEqual(values[1].value() , A_ , tol=1e-2 ) accumulate([1.0, 2.0] , [-1.0, 1.0] ) accumulate([3.0, -1.0] , [-1.0, -1.0] ) accumulate([-2.0, 2.0] , [3.0, -2.0] ) self.assertEqual(accumulator.step , 3 ) _check_local_values([2.0, 3.0] , [1.0, -2.0] ) apply_grad() self.assertListAlmostEqual(variable.value() , [4.0, 3.0] , tol=1e-2 ) accumulator.reset() self.assertEqual(accumulator.step , 0 ) _check_local_values([0.0, 0.0] , [0.0, 0.0] )
222
import math def lowerCAmelCase__( lowercase : list , lowercase : int = 0 , lowercase : int = 0 ) -> list: __snake_case : Any = end or len(lowercase ) for i in range(lowercase , lowercase ): __snake_case : List[str] = i __snake_case : Union[str, Any] = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __snake_case : Optional[Any] = array[temp_index - 1] temp_index -= 1 __snake_case : Any = temp_index_value return array def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int ) -> None: # Max Heap __snake_case : Any = index __snake_case : Optional[Any] = 2 * index + 1 # Left Node __snake_case : str = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __snake_case : Optional[int] = left_index if right_index < heap_size and array[largest] < array[right_index]: __snake_case : Tuple = right_index if largest != index: __snake_case , __snake_case : int = array[largest], array[index] heapify(lowercase , lowercase , lowercase ) def lowerCAmelCase__( lowercase : list ) -> list: __snake_case : List[str] = len(lowercase ) for i in range(n // 2 , -1 , -1 ): heapify(lowercase , lowercase , lowercase ) for i in range(n - 1 , 0 , -1 ): __snake_case , __snake_case : Optional[Any] = array[0], array[i] heapify(lowercase , 0 , lowercase ) return array def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int ) -> int: if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int ) -> int: __snake_case : Union[str, Any] = low __snake_case : Union[str, Any] = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __snake_case , __snake_case : str = array[j], array[i] i += 1 def lowerCAmelCase__( lowercase : list ) -> list: if len(lowercase ) == 0: return array __snake_case : Union[str, Any] = 2 * math.ceil(math.loga(len(lowercase ) ) ) __snake_case : Dict = 16 return intro_sort(lowercase , 0 , len(lowercase ) , lowercase , lowercase ) def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int , lowercase : int ) -> list: while end - start > size_threshold: if max_depth == 0: return heap_sort(lowercase ) max_depth -= 1 __snake_case : List[str] = median_of_a(lowercase , lowercase , start + ((end - start) // 2) + 1 , end - 1 ) __snake_case : Optional[Any] = partition(lowercase , lowercase , lowercase , lowercase ) intro_sort(lowercase , lowercase , lowercase , lowercase , lowercase ) __snake_case : List[str] = p return insertion_sort(lowercase , lowercase , lowercase ) if __name__ == "__main__": import doctest doctest.testmod() _UpperCamelCase = input('''Enter numbers separated by a comma : ''').strip() _UpperCamelCase = [float(item) for item in user_input.split(''',''')] print(sort(unsorted))
326
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __snake_case = logging.get_logger(__name__) class lowercase__ ( _UpperCAmelCase ): A__ : Optional[Any] =["pixel_values"] def __init__( self : Optional[int] , UpperCAmelCase_ : Dict = True , UpperCAmelCase_ : Dict = None , UpperCAmelCase_ : int = 0.9 , UpperCAmelCase_ : str = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Any] = True , UpperCAmelCase_ : int = None , UpperCAmelCase_ : Optional[Any] = 1 / 255 , UpperCAmelCase_ : Dict = True , UpperCAmelCase_ : Dict = True , UpperCAmelCase_ : Union[str, Any] = None , UpperCAmelCase_ : Any = None , **UpperCAmelCase_ : Optional[int] , ): super().__init__(**UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = size if size is not None else {"shortest_edge": 224} SCREAMING_SNAKE_CASE__ = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = crop_size if crop_size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE__ = get_size_dict(UpperCAmelCase_ , param_name='crop_size' ) SCREAMING_SNAKE_CASE__ = do_resize SCREAMING_SNAKE_CASE__ = size SCREAMING_SNAKE_CASE__ = crop_pct SCREAMING_SNAKE_CASE__ = resample SCREAMING_SNAKE_CASE__ = do_center_crop SCREAMING_SNAKE_CASE__ = crop_size SCREAMING_SNAKE_CASE__ = do_rescale SCREAMING_SNAKE_CASE__ = rescale_factor SCREAMING_SNAKE_CASE__ = do_normalize SCREAMING_SNAKE_CASE__ = image_mean 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 A_ ( self : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any = None , UpperCAmelCase_ : List[Any] = PILImageResampling.BICUBIC , UpperCAmelCase_ : Union[str, Any] = None , **UpperCAmelCase_ : Dict , ): SCREAMING_SNAKE_CASE__ = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_ ) if "shortest_edge" not in size and ("height" not in size or "width" not in size): raise ValueError(F'size must contain \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}' ) if crop_pct is not None: if "shortest_edge" in size: SCREAMING_SNAKE_CASE__ = int(size['shortest_edge'] / crop_pct ) elif "height" in size and "width" in size: if size["height"] == size["width"]: SCREAMING_SNAKE_CASE__ = int(size['height'] / crop_pct ) else: SCREAMING_SNAKE_CASE__ = (int(size['height'] / crop_pct ), int(size['width'] / crop_pct )) else: raise ValueError('Invalid size for resize: {}'.format(UpperCAmelCase_ ) ) SCREAMING_SNAKE_CASE__ = get_resize_output_image_size(UpperCAmelCase_ , size=UpperCAmelCase_ , default_to_square=UpperCAmelCase_ ) else: if "shortest_edge" in size: SCREAMING_SNAKE_CASE__ = get_resize_output_image_size(UpperCAmelCase_ , size=size['shortest_edge'] , default_to_square=UpperCAmelCase_ ) elif "height" in size and "width" in size: SCREAMING_SNAKE_CASE__ = (size["height"], size["width"]) else: raise ValueError('Invalid size for resize: {}'.format(UpperCAmelCase_ ) ) return resize(UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : str , UpperCAmelCase_ : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[str] = None , **UpperCAmelCase_ : int , ): SCREAMING_SNAKE_CASE__ = get_size_dict(UpperCAmelCase_ ) if "height" not in size or "width" not in size: raise ValueError(F'size must contain \'height\' and \'width\' as keys. Got {size.keys()}' ) return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[Any] = None , **UpperCAmelCase_ : Tuple , ): return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] = None , **UpperCAmelCase_ : Any , ): return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any] = None , UpperCAmelCase_ : Union[str, Any] = None , UpperCAmelCase_ : Union[str, Any] = None , UpperCAmelCase_ : Union[str, Any] = None , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Dict = None , UpperCAmelCase_ : Union[str, Any] = None , UpperCAmelCase_ : List[Any] = None , UpperCAmelCase_ : Tuple = None , UpperCAmelCase_ : List[Any] = None , UpperCAmelCase_ : Any = None , UpperCAmelCase_ : Tuple = None , UpperCAmelCase_ : Tuple = ChannelDimension.FIRST , **UpperCAmelCase_ : Optional[int] , ): SCREAMING_SNAKE_CASE__ = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE__ = crop_pct if crop_pct is not None else self.crop_pct SCREAMING_SNAKE_CASE__ = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE__ = do_center_crop if do_center_crop is not None else self.do_center_crop SCREAMING_SNAKE_CASE__ = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE__ = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE__ = do_normalize if do_normalize is not None else self.do_normalize 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_ , default_to_square=UpperCAmelCase_ ) 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' ) 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 or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_center_crop and crop_pct is None: raise ValueError('Crop_pct must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE__ = [to_numpy_array(UpperCAmelCase_ ) for image in images] if do_resize: SCREAMING_SNAKE_CASE__ = [self.resize(image=UpperCAmelCase_ , size=UpperCAmelCase_ , crop_pct=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_ )
176
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def lowerCAmelCase__( lowercase : Dict ) -> str: # picklable for multiprocessing return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def lowerCAmelCase__( ) -> List[Any]: with parallel_backend("spark" ): assert ParallelBackendConfig.backend_name == "spark" __snake_case : Any = [1, 2, 3] with pytest.raises(lowercase ): with parallel_backend("unsupported backend" ): map_nested(lowercase , lowercase , num_proc=2 ) with pytest.raises(lowercase ): with parallel_backend("unsupported backend" ): map_nested(lowercase , lowercase , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("num_proc" , [2, -1] ) def lowerCAmelCase__( lowercase : Dict ) -> Dict: __snake_case : Any = [1, 2] __snake_case : Dict = {"a": 1, "b": 2} __snake_case : Optional[int] = {"a": [1, 2], "b": [3, 4]} __snake_case : int = {"a": {"1": 1}, "b": 2} __snake_case : str = {"a": 1, "b": 2, "c": 3, "d": 4} __snake_case : Dict = [2, 3] __snake_case : Tuple = {"a": 2, "b": 3} __snake_case : int = {"a": [2, 3], "b": [4, 5]} __snake_case : Dict = {"a": {"1": 2}, "b": 3} __snake_case : str = {"a": 2, "b": 3, "c": 4, "d": 5} with parallel_backend("spark" ): assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa
326
0
"""simple docstring""" from math import ceil, sqrt def lowercase ( a__ : int = 1000000 ) -> int: _UpperCamelCase = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: _UpperCamelCase = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: _UpperCamelCase = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(F'''{solution() = }''')
256
import math import random from typing import Any from .hill_climbing import SearchProblem def lowerCAmelCase__( lowercase : Dict , lowercase : bool = True , lowercase : float = math.inf , lowercase : float = -math.inf , lowercase : float = math.inf , lowercase : float = -math.inf , lowercase : bool = False , lowercase : float = 100 , lowercase : float = 0.0_1 , lowercase : float = 1 , ) -> Any: __snake_case : Optional[Any] = False __snake_case : Optional[Any] = search_prob __snake_case : str = start_temperate __snake_case : List[Any] = [] __snake_case : str = 0 __snake_case : Dict = None while not search_end: __snake_case : List[Any] = current_state.score() if best_state is None or current_score > best_state.score(): __snake_case : List[Any] = current_state scores.append(lowercase ) iterations += 1 __snake_case : Dict = None __snake_case : str = current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to __snake_case : Any = random.randint(0 , len(lowercase ) - 1 ) # picking a random neighbor __snake_case : int = neighbors.pop(lowercase ) __snake_case : Optional[Any] = picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: __snake_case : Any = change * -1 # in case we are finding minimum if change > 0: # improves the solution __snake_case : List[str] = picked_neighbor else: __snake_case : Optional[Any] = (math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability __snake_case : str = picked_neighbor __snake_case : Optional[Any] = current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor __snake_case : Optional[Any] = True else: __snake_case : str = next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(lowercase ) , lowercase ) plt.xlabel("Iterations" ) plt.ylabel("Function values" ) plt.show() return best_state if __name__ == "__main__": def lowerCAmelCase__( lowercase : List[str] , lowercase : Tuple ) -> str: return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) _UpperCamelCase = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing( prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) # starting the problem with initial coordinates (12, 47) _UpperCamelCase = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing( prob, find_max=True, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) def lowerCAmelCase__( lowercase : Any , lowercase : Union[str, Any] ) -> Any: return (3 * x**2) - (6 * y) _UpperCamelCase = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing(prob, find_max=False, visualization=True) print( '''The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' F'''{local_min.score()}''' ) _UpperCamelCase = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing(prob, find_max=True, visualization=True) print( '''The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' F'''{local_min.score()}''' )
326
0
import math def A_ ( ): SCREAMING_SNAKE_CASE_: str = input("Enter message: " ) SCREAMING_SNAKE_CASE_: List[Any] = int(input(f"Enter key [2-{len(_UpperCAmelCase ) - 1}]: " ) ) SCREAMING_SNAKE_CASE_: Tuple = input("Encryption/Decryption [e/d]: " ) if mode.lower().startswith("e" ): SCREAMING_SNAKE_CASE_: int = encrypt_message(_UpperCAmelCase , _UpperCAmelCase ) elif mode.lower().startswith("d" ): SCREAMING_SNAKE_CASE_: Optional[int] = decrypt_message(_UpperCAmelCase , _UpperCAmelCase ) # Append pipe symbol (vertical bar) to identify spaces at the end. print(f"Output:\n{text + '|'}" ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = [""] * key for col in range(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = col while pointer < len(_UpperCAmelCase ): cipher_text[col] += message[pointer] pointer += key return "".join(_UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = math.ceil(len(_UpperCAmelCase ) / key ) SCREAMING_SNAKE_CASE_: Tuple = key SCREAMING_SNAKE_CASE_: Optional[int] = (num_cols * num_rows) - len(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = [""] * num_cols SCREAMING_SNAKE_CASE_: Dict = 0 SCREAMING_SNAKE_CASE_: Any = 0 for symbol in message: plain_text[col] += symbol col += 1 if ( (col == num_cols) or (col == num_cols - 1) and (row >= num_rows - num_shaded_boxes) ): SCREAMING_SNAKE_CASE_: int = 0 row += 1 return "".join(_UpperCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod() main()
13
import warnings from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =["image_processor", "tokenizer"] UpperCAmelCase_ : Tuple ="FlavaImageProcessor" UpperCAmelCase_ : List[Any] =("BertTokenizer", "BertTokenizerFast") def __init__( self , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase ) -> int: '''simple docstring''' __snake_case : List[Any] = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) __snake_case : List[Any] = kwargs.pop("feature_extractor" ) __snake_case : Any = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = self.image_processor def __call__( self , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = True , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = 0 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = True , UpperCAmelCase = None , **UpperCAmelCase , ) -> List[Any]: '''simple docstring''' if text is None and images is None: raise ValueError("You have to specify either text or images. Both cannot be none." ) if text is not None: __snake_case : Union[str, Any] = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if images is not None: __snake_case : Union[str, Any] = self.image_processor( UpperCAmelCase , return_image_mask=UpperCAmelCase , return_codebook_pixels=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if text is not None and images is not None: encoding.update(UpperCAmelCase ) return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def UpperCAmelCase ( self , *UpperCAmelCase , **UpperCAmelCase ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , *UpperCAmelCase , **UpperCAmelCase ) -> Tuple: '''simple docstring''' return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case : List[Any] = self.tokenizer.model_input_names __snake_case : Union[str, Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
326
0
class A : def __init__( self ): """simple docstring""" lowerCAmelCase_ = {} def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" print(self.vertex ) for i in self.vertex: print(UpperCamelCase__, ''' -> ''', ''' -> '''.join([str(UpperCamelCase__ ) for j in self.vertex[i]] ) ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__ ): """simple docstring""" if from_vertex in self.vertex: self.vertex[from_vertex].append(UpperCamelCase__ ) else: # else make a new vertex lowerCAmelCase_ = [to_vertex] def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(UpperCamelCase__, UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = True print(UpperCamelCase__, end=''' ''' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(UpperCamelCase__, UpperCamelCase__ ) if __name__ == "__main__": _A = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('''DFS:''') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
278
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model'''} _UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', } } _UpperCamelCase = { '''camembert-base''': 512, } _UpperCamelCase = '''▁''' class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Optional[Any] =VOCAB_FILES_NAMES UpperCAmelCase_ : str =PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase_ : str =["input_ids", "attention_mask"] def __init__( self , UpperCAmelCase , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<s>" , UpperCAmelCase="<unk>" , UpperCAmelCase="<pad>" , UpperCAmelCase="<mask>" , UpperCAmelCase=["<s>NOTUSED", "</s>NOTUSED"] , UpperCAmelCase = None , **UpperCAmelCase , ) -> None: '''simple docstring''' __snake_case : Dict = AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token __snake_case : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , additional_special_tokens=UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **UpperCAmelCase , ) __snake_case : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(UpperCAmelCase ) ) __snake_case : Dict = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __snake_case : str = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __snake_case : Optional[int] = len(self.fairseq_tokens_to_ids ) __snake_case : Any = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __snake_case : List[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __snake_case : Dict = [self.cls_token_id] __snake_case : Any = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = 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 [1] + ([0] * len(UpperCAmelCase )) + [1] return [1] + ([0] * len(UpperCAmelCase )) + [1, 1] + ([0] * len(UpperCAmelCase )) + [1] def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: '''simple docstring''' __snake_case : int = [self.sep_token_id] __snake_case : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCAmelCase ( self ) -> int: '''simple docstring''' return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case : Optional[int] = {self.convert_ids_to_tokens(UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCAmelCase ( self , UpperCAmelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(UpperCAmelCase , out_type=UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> List[Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(UpperCAmelCase ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> Tuple: '''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 UpperCAmelCase ( self , UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __snake_case : Tuple = [] __snake_case : Union[str, Any] = "" __snake_case : Optional[int] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(UpperCAmelCase ) + token __snake_case : List[Any] = True __snake_case : Union[str, Any] = [] else: current_sub_tokens.append(UpperCAmelCase ) __snake_case : int = False out_string += self.sp_model.decode(UpperCAmelCase ) return out_string.strip() def __getstate__( self ) -> List[Any]: '''simple docstring''' __snake_case : str = self.__dict__.copy() __snake_case : Optional[Any] = None return state def __setstate__( self , UpperCAmelCase ) -> str: '''simple docstring''' __snake_case : Optional[Any] = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): __snake_case : List[str] = {} __snake_case : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return __snake_case : Optional[Any] = os.path.join( UpperCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase , "wb" ) as fi: __snake_case : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase ) return (out_vocab_file,)
326
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/config.json'} class __SCREAMING_SNAKE_CASE ( lowerCamelCase ): snake_case_ = "openai-gpt" snake_case_ = { "max_position_embeddings": "n_positions", "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Any , __lowercase : List[str]=4_04_78 , __lowercase : int=5_12 , __lowercase : Dict=7_68 , __lowercase : Any=12 , __lowercase : Optional[int]=12 , __lowercase : Optional[int]="gelu" , __lowercase : List[str]=0.1 , __lowercase : Any=0.1 , __lowercase : Union[str, Any]=0.1 , __lowercase : Union[str, Any]=1e-5 , __lowercase : Optional[Any]=0.02 , __lowercase : Any="cls_index" , __lowercase : Union[str, Any]=True , __lowercase : List[Any]=None , __lowercase : Optional[Any]=True , __lowercase : List[str]=0.1 , **__lowercase : Dict , ) -> str: SCREAMING_SNAKE_CASE__ : Optional[int] =vocab_size SCREAMING_SNAKE_CASE__ : Dict =n_positions SCREAMING_SNAKE_CASE__ : Union[str, Any] =n_embd SCREAMING_SNAKE_CASE__ : int =n_layer SCREAMING_SNAKE_CASE__ : Optional[int] =n_head SCREAMING_SNAKE_CASE__ : Dict =afn SCREAMING_SNAKE_CASE__ : Dict =resid_pdrop SCREAMING_SNAKE_CASE__ : Optional[Any] =embd_pdrop SCREAMING_SNAKE_CASE__ : List[Any] =attn_pdrop SCREAMING_SNAKE_CASE__ : Union[str, Any] =layer_norm_epsilon SCREAMING_SNAKE_CASE__ : Union[str, Any] =initializer_range SCREAMING_SNAKE_CASE__ : Optional[int] =summary_type SCREAMING_SNAKE_CASE__ : List[str] =summary_use_proj SCREAMING_SNAKE_CASE__ : Any =summary_activation SCREAMING_SNAKE_CASE__ : List[Any] =summary_first_dropout SCREAMING_SNAKE_CASE__ : Union[str, Any] =summary_proj_to_labels super().__init__(**__lowercase )
152
def lowerCAmelCase__( lowercase : list[int] , lowercase : int ) -> bool: __snake_case : List[str] = len(lowercase ) __snake_case : int = [[False] * (required_sum + 1) for _ in range(arr_len + 1 )] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 for i in range(arr_len + 1 ): __snake_case : Optional[Any] = True # sum is not zero and set is empty then false for i in range(1 , required_sum + 1 ): __snake_case : Union[str, Any] = False for i in range(1 , arr_len + 1 ): for j in range(1 , required_sum + 1 ): if arr[i - 1] > j: __snake_case : List[str] = subset[i - 1][j] if arr[i - 1] <= j: __snake_case : Union[str, Any] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] return subset[arr_len][required_sum] if __name__ == "__main__": import doctest doctest.testmod()
326
0
"""simple docstring""" import argparse import os from io import BytesIO from pathlib import Path import requests from clip_retrieval.clip_client import ClipClient from PIL import Image from tqdm import tqdm def a_ ( _lowerCAmelCase : str , _lowerCAmelCase : Any , _lowerCAmelCase : Tuple ): '''simple docstring''' lowercase__ : Any = 1.5 lowercase__ : List[str] = int(factor * num_class_images ) lowercase__ : List[str] = ClipClient( url='https://knn.laion.ai/knn-service' , indice_name='laion_400m' , num_images=_lowerCAmelCase , aesthetic_weight=0.1 ) os.makedirs(f"""{class_data_dir}/images""" , exist_ok=_lowerCAmelCase ) if len(list(Path(f"""{class_data_dir}/images""" ).iterdir() ) ) >= num_class_images: return while True: lowercase__ : str = client.query(text=_lowerCAmelCase ) if len(_lowerCAmelCase ) >= factor * num_class_images or num_images > 1E4: break else: lowercase__ : int = int(factor * num_images ) lowercase__ : List[str] = ClipClient( url='https://knn.laion.ai/knn-service' , indice_name='laion_400m' , num_images=_lowerCAmelCase , aesthetic_weight=0.1 , ) lowercase__ : Optional[Any] = 0 lowercase__ : List[str] = 0 lowercase__ : Dict = tqdm(desc='downloading real regularization images' , total=_lowerCAmelCase ) with open(f"""{class_data_dir}/caption.txt""" , 'w' ) as fa, open(f"""{class_data_dir}/urls.txt""" , 'w' ) as fa, open( f"""{class_data_dir}/images.txt""" , 'w' ) as fa: while total < num_class_images: lowercase__ : Union[str, Any] = class_images[count] count += 1 try: lowercase__ : Optional[Any] = requests.get(images['url'] ) if img.status_code == 200: lowercase__ : Any = Image.open(BytesIO(img.content ) ) with open(f"""{class_data_dir}/images/{total}.jpg""" , 'wb' ) as f: f.write(img.content ) fa.write(images['caption'] + '\n' ) fa.write(images['url'] + '\n' ) fa.write(f"""{class_data_dir}/images/{total}.jpg""" + '\n' ) total += 1 pbar.update(1 ) else: continue except Exception: continue return def a_ ( ): '''simple docstring''' lowercase__ : Optional[Any] = argparse.ArgumentParser('' , add_help=_lowerCAmelCase ) parser.add_argument('--class_prompt' , help='text prompt to retrieve images' , required=_lowerCAmelCase , type=_lowerCAmelCase ) parser.add_argument('--class_data_dir' , help='path to save images' , required=_lowerCAmelCase , type=_lowerCAmelCase ) parser.add_argument('--num_class_images' , help='number of images to download' , default=200 , type=_lowerCAmelCase ) return parser.parse_args() if __name__ == "__main__": _UpperCamelCase : Optional[Any] = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
77
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node _UpperCamelCase = 4 _UpperCamelCase = 3 class _lowerCamelCase ( a ): """simple docstring""" pass def lowerCAmelCase__( lowercase : List[str] ) -> Any: for shard in shards: for i in range(lowercase ): yield {"i": i, "shard": shard} def lowerCAmelCase__( ) -> Optional[int]: __snake_case : List[Any] = int(os.environ["RANK"] ) __snake_case : Optional[int] = int(os.environ["WORLD_SIZE"] ) __snake_case : List[str] = ArgumentParser() parser.add_argument("--streaming" , type=lowercase ) parser.add_argument("--local_rank" , type=lowercase ) parser.add_argument("--num_workers" , type=lowercase , default=0 ) __snake_case : Any = parser.parse_args() __snake_case : Dict = args.streaming __snake_case : Union[str, Any] = args.num_workers __snake_case : Any = {"shards": [f"""shard_{shard_idx}""" for shard_idx in range(lowercase )]} __snake_case : Optional[int] = IterableDataset.from_generator(lowercase , gen_kwargs=lowercase ) if not streaming: __snake_case : Any = Dataset.from_list(list(lowercase ) ) __snake_case : Dict = split_dataset_by_node(lowercase , rank=lowercase , world_size=lowercase ) __snake_case : Union[str, Any] = torch.utils.data.DataLoader(lowercase , num_workers=lowercase ) __snake_case : Optional[int] = NUM_SHARDS * NUM_ITEMS_PER_SHARD __snake_case : List[str] = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) __snake_case : Dict = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f"""local_size {local_size} != expected_local_size {expected_local_size}""" ) if __name__ == "__main__": main()
326
0
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class lowerCamelCase_ : '''simple docstring''' @staticmethod def UpperCamelCase__ ( *__lowercase , **__lowercase) -> Optional[int]: pass @is_pipeline_test @require_vision class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' @require_torch def UpperCamelCase__ ( self) -> Any: __UpperCamelCase :str = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , ) __UpperCamelCase :Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') __UpperCamelCase :Dict = image_classifier(__lowercase , candidate_labels=['''a''', '''b''', '''c''']) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(__lowercase) , [ [{'''score''': 0.3_33, '''label''': '''a'''}, {'''score''': 0.3_33, '''label''': '''b'''}, {'''score''': 0.3_33, '''label''': '''c'''}], [{'''score''': 0.3_33, '''label''': '''a'''}, {'''score''': 0.3_33, '''label''': '''c'''}, {'''score''': 0.3_33, '''label''': '''b'''}], ] , ) __UpperCamelCase :str = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2) self.assertEqual( nested_simplify(__lowercase) , [ [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], ] , ) @require_tf def UpperCamelCase__ ( self) -> List[str]: __UpperCamelCase :List[str] = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , framework='''tf''') __UpperCamelCase :Any = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') __UpperCamelCase :Dict = image_classifier(__lowercase , candidate_labels=['''a''', '''b''', '''c''']) self.assertEqual( nested_simplify(__lowercase) , [{'''score''': 0.3_33, '''label''': '''a'''}, {'''score''': 0.3_33, '''label''': '''b'''}, {'''score''': 0.3_33, '''label''': '''c'''}] , ) __UpperCamelCase :Optional[Any] = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2) self.assertEqual( nested_simplify(__lowercase) , [ [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], [ {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, {'''score''': 0.3_33, '''label''': ANY(__lowercase)}, ], ] , ) @slow @require_torch def UpperCamelCase__ ( self) -> Tuple: __UpperCamelCase :Optional[int] = pipeline( task='''zero-shot-image-classification''' , model='''openai/clip-vit-base-patch32''' , ) # This is an image of 2 cats with remotes and no planes __UpperCamelCase :int = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') __UpperCamelCase :str = image_classifier(__lowercase , candidate_labels=['''cat''', '''plane''', '''remote''']) self.assertEqual( nested_simplify(__lowercase) , [ {'''score''': 0.5_11, '''label''': '''remote'''}, {'''score''': 0.4_85, '''label''': '''cat'''}, {'''score''': 0.0_04, '''label''': '''plane'''}, ] , ) __UpperCamelCase :Union[str, Any] = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2) self.assertEqual( nested_simplify(__lowercase) , [ [ {'''score''': 0.5_11, '''label''': '''remote'''}, {'''score''': 0.4_85, '''label''': '''cat'''}, {'''score''': 0.0_04, '''label''': '''plane'''}, ], ] * 5 , ) @slow @require_tf def UpperCamelCase__ ( self) -> int: __UpperCamelCase :int = pipeline( task='''zero-shot-image-classification''' , model='''openai/clip-vit-base-patch32''' , framework='''tf''') # This is an image of 2 cats with remotes and no planes __UpperCamelCase :Any = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') __UpperCamelCase :Tuple = image_classifier(__lowercase , candidate_labels=['''cat''', '''plane''', '''remote''']) self.assertEqual( nested_simplify(__lowercase) , [ {'''score''': 0.5_11, '''label''': '''remote'''}, {'''score''': 0.4_85, '''label''': '''cat'''}, {'''score''': 0.0_04, '''label''': '''plane'''}, ] , ) __UpperCamelCase :Optional[int] = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2) self.assertEqual( nested_simplify(__lowercase) , [ [ {'''score''': 0.5_11, '''label''': '''remote'''}, {'''score''': 0.4_85, '''label''': '''cat'''}, {'''score''': 0.0_04, '''label''': '''plane'''}, ], ] * 5 , )
43
def lowerCAmelCase__( lowercase : int = 100_0000 ) -> int: __snake_case : List[Any] = limit + 1 __snake_case : List[str] = [0] * limit for first_term in range(1 , lowercase ): for n in range(lowercase , lowercase , lowercase ): __snake_case : Union[str, Any] = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a __snake_case : Tuple = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
326
0
'''simple docstring''' import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def UpperCamelCase_( ): '''simple docstring''' snake_case_ = argparse.ArgumentParser() parser.add_argument("--model_ckpt" , type=snake_case , default="microsoft/unixcoder-base-nine" ) parser.add_argument("--num_epochs" , type=snake_case , default=5 ) parser.add_argument("--batch_size" , type=snake_case , default=6 ) parser.add_argument("--gradient_accumulation_steps" , type=snake_case , default=1 ) parser.add_argument("--freeze" , type=snake_case , default=snake_case ) parser.add_argument("--learning_rate" , type=snake_case , default=5e-4 ) parser.add_argument("--seed" , type=snake_case , default=0 ) parser.add_argument("--lr_scheduler_type" , type=snake_case , default="cosine" ) parser.add_argument("--num_warmup_steps" , type=snake_case , default=1_0 ) parser.add_argument("--weight_decay" , type=snake_case , default=0.01 ) parser.add_argument("--output_dir" , type=snake_case , default="./results" ) return parser.parse_args() _SCREAMING_SNAKE_CASE : Optional[Any] = load("accuracy") def UpperCamelCase_( snake_case : int ): '''simple docstring''' snake_case_ = eval_pred snake_case_ = np.argmax(snake_case , axis=1 ) return metric.compute(predictions=snake_case , references=snake_case ) class _snake_case ( lowercase_ ): def __init__( self , a__ ) -> None: '''simple docstring''' super().__init__() snake_case_ = trainer def lowerCAmelCase__ ( self , a__ , a__ , a__ , **a__ ) -> Optional[int]: '''simple docstring''' if control.should_evaluate: snake_case_ = deepcopy(a__ ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix="train" ) return control_copy def UpperCamelCase_( ): '''simple docstring''' snake_case_ = get_args() set_seed(args.seed ) snake_case_ = load_dataset("codeparrot/codecomplex" , split="train" ) snake_case_ = dataset.train_test_split(test_size=0.2 ) snake_case_ = train_test["test"].train_test_split(test_size=0.5 ) snake_case_ = DatasetDict( { "train": train_test["train"], "test": test_validation["train"], "valid": test_validation["test"], } ) print("Loading tokenizer and model" ) snake_case_ = AutoTokenizer.from_pretrained(args.model_ckpt ) snake_case_ = tokenizer.eos_token snake_case_ = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) snake_case_ = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): snake_case_ = False snake_case_ = ClassLabel(num_classes=7 , names=list(set(train_test_validation["train"]["complexity"] ) ) ) def tokenize(snake_case : Union[str, Any] ): snake_case_ = tokenizer(example["src"] , truncation=snake_case , max_length=1_0_2_4 ) snake_case_ = labels.straint(example["complexity"] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } snake_case_ = train_test_validation.map( snake_case , batched=snake_case , remove_columns=train_test_validation["train"].column_names , ) snake_case_ = DataCollatorWithPadding(tokenizer=snake_case ) snake_case_ = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy="epoch" , save_strategy="epoch" , logging_strategy="epoch" , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.01 , metric_for_best_model="accuracy" , run_name="complexity-java" , report_to="wandb" , ) snake_case_ = Trainer( model=snake_case , args=snake_case , train_dataset=tokenized_datasets["train"] , eval_dataset=tokenized_datasets["valid"] , tokenizer=snake_case , data_collator=snake_case , compute_metrics=snake_case , ) print("Training..." ) trainer.add_callback(CustomCallback(snake_case ) ) trainer.train() if __name__ == "__main__": main()
85
from __future__ import annotations def lowerCAmelCase__( lowercase : str , lowercase : list[str] | None = None ) -> list[list[str]]: __snake_case : List[str] = word_bank or [] # create a table __snake_case : int = len(lowercase ) + 1 __snake_case : list[list[list[str]]] = [] for _ in range(lowercase ): table.append([] ) # seed value __snake_case : Optional[int] = [[]] # because empty string has empty combination # iterate through the indices for i in range(lowercase ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(lowercase )] == word: __snake_case : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(lowercase )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(lowercase )]: combination.reverse() return table[len(lowercase )] if __name__ == "__main__": print(all_construct('''jwajalapa''', ['''jwa''', '''j''', '''w''', '''a''', '''la''', '''lapa'''])) print(all_construct('''rajamati''', ['''s''', '''raj''', '''amat''', '''raja''', '''ma''', '''i''', '''t'''])) print( all_construct( '''hexagonosaurus''', ['''h''', '''ex''', '''hex''', '''ag''', '''ago''', '''ru''', '''auru''', '''rus''', '''go''', '''no''', '''o''', '''s'''], ) )
326
0
"""simple docstring""" import re from filelock import FileLock try: import nltk _SCREAMING_SNAKE_CASE : Dict = True except (ImportError, ModuleNotFoundError): _SCREAMING_SNAKE_CASE : Union[str, Any] = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def lowerCamelCase__ ( _lowerCamelCase : str ) -> str: re.sub('<n>' , '' , _lowerCamelCase ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(_lowerCamelCase ) )
183
import unittest from transformers import BigBirdConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=2 , UpperCAmelCase=56 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=99 , UpperCAmelCase=32 , UpperCAmelCase=2 , UpperCAmelCase=2 , UpperCAmelCase=7 , UpperCAmelCase="gelu_new" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=512 , UpperCAmelCase=16 , UpperCAmelCase=2 , UpperCAmelCase=0.02 , UpperCAmelCase=4 , UpperCAmelCase="block_sparse" , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=2 , UpperCAmelCase=3 , ) -> Tuple: '''simple docstring''' __snake_case : Optional[int] = parent __snake_case : Tuple = batch_size __snake_case : List[str] = seq_length __snake_case : Optional[int] = is_training __snake_case : int = use_attention_mask __snake_case : Union[str, Any] = use_token_type_ids __snake_case : Any = use_labels __snake_case : List[str] = vocab_size __snake_case : int = hidden_size __snake_case : List[str] = num_hidden_layers __snake_case : List[Any] = num_attention_heads __snake_case : Optional[int] = intermediate_size __snake_case : Union[str, Any] = hidden_act __snake_case : Optional[int] = hidden_dropout_prob __snake_case : Optional[Any] = attention_probs_dropout_prob __snake_case : str = max_position_embeddings __snake_case : List[Any] = type_vocab_size __snake_case : int = type_sequence_label_size __snake_case : Dict = initializer_range __snake_case : List[Any] = num_choices __snake_case : Union[str, Any] = rescale_embeddings __snake_case : List[Any] = attention_type __snake_case : str = use_bias __snake_case : Dict = block_size __snake_case : Optional[Any] = num_random_blocks def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case : Any = None if self.use_attention_mask: __snake_case : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case : Union[str, Any] = None if self.use_token_type_ids: __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case : Optional[int] = BigBirdConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , attention_type=self.attention_type , block_size=self.block_size , num_random_blocks=self.num_random_blocks , use_bias=self.use_bias , rescale_embeddings=self.rescale_embeddings , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case : Optional[int] = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case : Dict = config_and_inputs __snake_case : int = { "input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask, } return config, inputs_dict @require_flax class _lowerCamelCase ( a , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) UpperCAmelCase_ : Dict =False UpperCAmelCase_ : str =False def UpperCAmelCase ( self ) -> str: '''simple docstring''' __snake_case : Dict = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Any: '''simple docstring''' super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_hidden_states_output() @slow def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case : Any = model_class_name.from_pretrained("google/bigbird-roberta-base" ) self.assertIsNotNone(UpperCAmelCase ) def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case , __snake_case : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __snake_case : Optional[Any] = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = model_class(UpperCAmelCase ) @jax.jit def model_jitted(UpperCAmelCase , UpperCAmelCase=None , **UpperCAmelCase ): return model(input_ids=UpperCAmelCase , attention_mask=UpperCAmelCase , **UpperCAmelCase ) with self.subTest("JIT Enabled" ): __snake_case : int = model_jitted(**UpperCAmelCase ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __snake_case : List[Any] = model_jitted(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) ) for jitted_output, output in zip(UpperCAmelCase , UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=1E-5 , UpperCAmelCase="outputs" , UpperCAmelCase=None ) -> int: '''simple docstring''' if name.startswith("outputs.attentions" ): return else: super().check_pt_flax_outputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase )
326
0
def a__ ( A_ ): '''simple docstring''' if num <= 0: raise ValueError("""Input must be a positive integer""" ) __magic_name__ = [True] * (num + 1) __magic_name__ = 2 while p * p <= num: if primes[p]: for i in range(p * p, num + 1, A_ ): __magic_name__ = False p += 1 return [prime for prime in range(2, num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase : Optional[int] = int(input('Enter a positive integer: ').strip()) print(prime_sieve_eratosthenes(user_num))
88
import argparse import datetime def lowerCAmelCase__( lowercase : str ) -> str: __snake_case : int = { "0": "Sunday", "1": "Monday", "2": "Tuesday", "3": "Wednesday", "4": "Thursday", "5": "Friday", "6": "Saturday", } __snake_case : int = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(lowercase ) < 11: raise ValueError("Must be 10 characters long" ) # Get month __snake_case : int = int(date_input[0] + date_input[1] ) # Validate if not 0 < m < 13: raise ValueError("Month must be between 1 - 12" ) __snake_case : str = date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'" ) # Get day __snake_case : int = int(date_input[3] + date_input[4] ) # Validate if not 0 < d < 32: raise ValueError("Date must be between 1 - 31" ) # Get second separator __snake_case : str = date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'" ) # Get year __snake_case : int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9] ) # Arbitrary year range if not 45 < y < 8500: raise ValueError( "Year out of range. There has to be some sort of limit...right?" ) # Get datetime obj for validation __snake_case : str = datetime.date(int(lowercase ) , int(lowercase ) , int(lowercase ) ) # Start math if m <= 2: __snake_case : Optional[Any] = y - 1 __snake_case : Tuple = m + 12 # maths var __snake_case : int = int(str(lowercase )[:2] ) __snake_case : int = int(str(lowercase )[2:] ) __snake_case : int = int(2.6 * m - 5.3_9 ) __snake_case : int = int(c / 4 ) __snake_case : int = int(k / 4 ) __snake_case : int = int(d + k ) __snake_case : int = int(t + u + v + x ) __snake_case : int = int(z - (2 * c) ) __snake_case : int = round(w % 7 ) # End math # Validate math if f != convert_datetime_days[dt_ck.weekday()]: raise AssertionError("The date was evaluated incorrectly. Contact developer." ) # Response __snake_case : str = f"""Your date {date_input}, is a {days[str(lowercase )]}!""" return response if __name__ == "__main__": import doctest doctest.testmod() _UpperCamelCase = argparse.ArgumentParser( description=( '''Find out what day of the week nearly any date is or was. Enter ''' '''date as a string in the mm-dd-yyyy or mm/dd/yyyy format''' ) ) parser.add_argument( '''date_input''', type=str, help='''Date as a string (mm-dd-yyyy or mm/dd/yyyy)''' ) _UpperCamelCase = parser.parse_args() zeller(args.date_input)
326
0
import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() _UpperCAmelCase : Tuple = logging.get_logger(__name__) _UpperCAmelCase : Optional[int] = [ ("bert.bert", "visual_bert"), ("bert.cls", "cls"), ("bert.classifier", "cls"), ("token_type_embeddings_visual", "visual_token_type_embeddings"), ("position_embeddings_visual", "visual_position_embeddings"), ("projection", "visual_projection"), ] _UpperCAmelCase : List[Any] = [ "nlvr2_coco_pre_trained.th", "nlvr2_fine_tuned.th", "nlvr2_pre_trained.th", "vcr_coco_pre_train.th", "vcr_fine_tune.th", "vcr_pre_train.th", "vqa_coco_pre_trained.th", "vqa_fine_tuned.th", "vqa_pre_trained.th", ] def A ( lowercase ) -> Optional[Any]: '''simple docstring''' UpperCamelCase = torch.load(lowercase , map_location='cpu' ) return sd def A ( lowercase , lowercase , lowercase=rename_keys_prefix ) -> Dict: '''simple docstring''' UpperCamelCase = OrderedDict() UpperCamelCase = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue UpperCamelCase = key for name_pair in rename_keys_prefix: UpperCamelCase = new_key.replace(name_pair[0] , name_pair[1] ) UpperCamelCase = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately UpperCamelCase = new_d["cls.predictions.bias"] return new_d @torch.no_grad() def A ( lowercase , lowercase ) -> List[Any]: '''simple docstring''' assert ( checkpoint_path.split('/' )[-1] in ACCEPTABLE_CHECKPOINTS ), f'''The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.''' # Get Config if "pre" in checkpoint_path: UpperCamelCase = "pretraining" if "vcr" in checkpoint_path: UpperCamelCase = {"visual_embedding_dim": 512} elif "vqa_advanced" in checkpoint_path: UpperCamelCase = {"visual_embedding_dim": 2_048} elif "vqa" in checkpoint_path: UpperCamelCase = {"visual_embedding_dim": 2_048} elif "nlvr" in checkpoint_path: UpperCamelCase = {"visual_embedding_dim": 1_024} else: raise NotImplementedError(f'''No implementation found for `{checkpoint_path}`.''' ) else: if "vcr" in checkpoint_path: UpperCamelCase = {"visual_embedding_dim": 512} UpperCamelCase = "multichoice" elif "vqa_advanced" in checkpoint_path: UpperCamelCase = {"visual_embedding_dim": 2_048} UpperCamelCase = "vqa_advanced" elif "vqa" in checkpoint_path: UpperCamelCase = {"visual_embedding_dim": 2_048, "num_labels": 3_129} UpperCamelCase = "vqa" elif "nlvr" in checkpoint_path: UpperCamelCase = { "visual_embedding_dim": 1_024, "num_labels": 2, } UpperCamelCase = "nlvr" UpperCamelCase = VisualBertConfig(**lowercase ) # Load State Dict UpperCamelCase = load_state_dict(lowercase ) UpperCamelCase = get_new_dict(lowercase , lowercase ) if model_type == "pretraining": UpperCamelCase = VisualBertForPreTraining(lowercase ) elif model_type == "vqa": UpperCamelCase = VisualBertForQuestionAnswering(lowercase ) elif model_type == "nlvr": UpperCamelCase = VisualBertForVisualReasoning(lowercase ) elif model_type == "multichoice": UpperCamelCase = VisualBertForMultipleChoice(lowercase ) model.load_state_dict(lowercase ) # Save Checkpoints Path(lowercase ).mkdir(exist_ok=lowercase ) model.save_pretrained(lowercase ) if __name__ == "__main__": _UpperCAmelCase : str = argparse.ArgumentParser() # Required parameters parser.add_argument("orig_checkpoint_path", type=str, help="A path to .th on local filesystem.") parser.add_argument("pytorch_dump_folder_path", type=str, help="Path to the output PyTorch model.") _UpperCAmelCase : Tuple = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
222
def lowerCAmelCase__( lowercase : List[Any] , lowercase : Optional[Any] , lowercase : Optional[int] , lowercase : str , lowercase : List[Any] , lowercase : List[str] ) -> int: if index == r: for j in range(lowercase ): print(data[j] , end=" " ) print(" " ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location __snake_case : Union[str, Any] = arr[i] combination_util(lowercase , lowercase , lowercase , index + 1 , lowercase , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(lowercase , lowercase , lowercase , lowercase , lowercase , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def lowerCAmelCase__( lowercase : Any , lowercase : Tuple , lowercase : Union[str, Any] ) -> Optional[Any]: # A temporary array to store all combination one by one __snake_case : Tuple = [0] * r # Print all combination using temporary array 'data[]' combination_util(lowercase , lowercase , lowercase , 0 , lowercase , 0 ) if __name__ == "__main__": # Driver code to check the function above _UpperCamelCase = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
326
0
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { """microsoft/unispeech-sat-base-100h-libri-ft""": ( """https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json""" ), # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat } class lowercase__ ( _UpperCAmelCase ): A__ : Tuple ="unispeech-sat" def __init__( self : int , UpperCAmelCase_ : Optional[Any]=32 , UpperCAmelCase_ : Optional[int]=768 , UpperCAmelCase_ : Any=12 , UpperCAmelCase_ : int=12 , UpperCAmelCase_ : List[str]=3072 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Any=0.1 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : str=0.0 , UpperCAmelCase_ : Any=0.0 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : List[str]=0.02 , UpperCAmelCase_ : List[str]=1e-5 , UpperCAmelCase_ : int="group" , UpperCAmelCase_ : Dict="gelu" , UpperCAmelCase_ : int=(512, 512, 512, 512, 512, 512, 512) , UpperCAmelCase_ : Optional[Any]=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase_ : Tuple=(10, 3, 3, 3, 3, 2, 2) , UpperCAmelCase_ : Tuple=False , UpperCAmelCase_ : List[Any]=128 , UpperCAmelCase_ : str=16 , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : List[str]=0.05 , UpperCAmelCase_ : Any=10 , UpperCAmelCase_ : Any=2 , UpperCAmelCase_ : List[Any]=0.0 , UpperCAmelCase_ : Optional[Any]=10 , UpperCAmelCase_ : Any=0 , UpperCAmelCase_ : Union[str, Any]=320 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : List[str]=100 , UpperCAmelCase_ : Tuple=256 , UpperCAmelCase_ : Union[str, Any]=256 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : Dict="mean" , UpperCAmelCase_ : int=False , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : int=256 , UpperCAmelCase_ : Any=(512, 512, 512, 512, 1500) , UpperCAmelCase_ : Union[str, Any]=(5, 3, 3, 1, 1) , UpperCAmelCase_ : List[str]=(1, 2, 3, 1, 1) , UpperCAmelCase_ : Tuple=512 , UpperCAmelCase_ : Optional[Any]=0 , UpperCAmelCase_ : Dict=1 , UpperCAmelCase_ : List[Any]=2 , UpperCAmelCase_ : int=504 , **UpperCAmelCase_ : int , ): super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = hidden_size SCREAMING_SNAKE_CASE__ = feat_extract_norm SCREAMING_SNAKE_CASE__ = feat_extract_activation SCREAMING_SNAKE_CASE__ = list(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = list(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = list(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = conv_bias SCREAMING_SNAKE_CASE__ = num_conv_pos_embeddings SCREAMING_SNAKE_CASE__ = num_conv_pos_embedding_groups SCREAMING_SNAKE_CASE__ = len(self.conv_dim ) SCREAMING_SNAKE_CASE__ = num_hidden_layers SCREAMING_SNAKE_CASE__ = intermediate_size SCREAMING_SNAKE_CASE__ = hidden_act SCREAMING_SNAKE_CASE__ = num_attention_heads SCREAMING_SNAKE_CASE__ = hidden_dropout SCREAMING_SNAKE_CASE__ = attention_dropout SCREAMING_SNAKE_CASE__ = activation_dropout SCREAMING_SNAKE_CASE__ = feat_proj_dropout SCREAMING_SNAKE_CASE__ = final_dropout SCREAMING_SNAKE_CASE__ = layerdrop SCREAMING_SNAKE_CASE__ = layer_norm_eps SCREAMING_SNAKE_CASE__ = initializer_range SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = num_clusters SCREAMING_SNAKE_CASE__ = do_stable_layer_norm SCREAMING_SNAKE_CASE__ = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' F' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,' F' `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 SCREAMING_SNAKE_CASE__ = apply_spec_augment SCREAMING_SNAKE_CASE__ = mask_time_prob SCREAMING_SNAKE_CASE__ = mask_time_length SCREAMING_SNAKE_CASE__ = mask_time_min_masks SCREAMING_SNAKE_CASE__ = mask_feature_prob SCREAMING_SNAKE_CASE__ = mask_feature_length SCREAMING_SNAKE_CASE__ = mask_feature_min_masks # parameters for pretraining with codevector quantized representations SCREAMING_SNAKE_CASE__ = num_codevectors_per_group SCREAMING_SNAKE_CASE__ = num_codevector_groups SCREAMING_SNAKE_CASE__ = contrastive_logits_temperature SCREAMING_SNAKE_CASE__ = feat_quantizer_dropout SCREAMING_SNAKE_CASE__ = num_negatives SCREAMING_SNAKE_CASE__ = codevector_dim SCREAMING_SNAKE_CASE__ = proj_codevector_dim SCREAMING_SNAKE_CASE__ = diversity_loss_weight # ctc loss SCREAMING_SNAKE_CASE__ = ctc_loss_reduction SCREAMING_SNAKE_CASE__ = ctc_zero_infinity # SequenceClassification-specific parameter. Feel free to ignore for other classes. SCREAMING_SNAKE_CASE__ = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. SCREAMING_SNAKE_CASE__ = list(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = list(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = list(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = xvector_output_dim @property def A_ ( self : Optional[int] ): return functools.reduce(operator.mul , self.conv_stride , 1 )
176
import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = [ ('''bert.bert''', '''visual_bert'''), ('''bert.cls''', '''cls'''), ('''bert.classifier''', '''cls'''), ('''token_type_embeddings_visual''', '''visual_token_type_embeddings'''), ('''position_embeddings_visual''', '''visual_position_embeddings'''), ('''projection''', '''visual_projection'''), ] _UpperCamelCase = [ '''nlvr2_coco_pre_trained.th''', '''nlvr2_fine_tuned.th''', '''nlvr2_pre_trained.th''', '''vcr_coco_pre_train.th''', '''vcr_fine_tune.th''', '''vcr_pre_train.th''', '''vqa_coco_pre_trained.th''', '''vqa_fine_tuned.th''', '''vqa_pre_trained.th''', ] def lowerCAmelCase__( lowercase : str ) -> Optional[Any]: __snake_case : Optional[int] = torch.load(lowercase , map_location="cpu" ) return sd def lowerCAmelCase__( lowercase : List[Any] , lowercase : List[Any] , lowercase : List[Any]=rename_keys_prefix ) -> Dict: __snake_case : Tuple = OrderedDict() __snake_case : str = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue __snake_case : Optional[Any] = key for name_pair in rename_keys_prefix: __snake_case : List[str] = new_key.replace(name_pair[0] , name_pair[1] ) __snake_case : List[str] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately __snake_case : List[Any] = new_d["cls.predictions.bias"] return new_d @torch.no_grad() def lowerCAmelCase__( lowercase : Optional[Any] , lowercase : Any ) -> List[Any]: assert ( checkpoint_path.split("/" )[-1] in ACCEPTABLE_CHECKPOINTS ), f"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: __snake_case : Any = "pretraining" if "vcr" in checkpoint_path: __snake_case : Optional[Any] = {"visual_embedding_dim": 512} elif "vqa_advanced" in checkpoint_path: __snake_case : Tuple = {"visual_embedding_dim": 2048} elif "vqa" in checkpoint_path: __snake_case : Dict = {"visual_embedding_dim": 2048} elif "nlvr" in checkpoint_path: __snake_case : Any = {"visual_embedding_dim": 1024} else: raise NotImplementedError(f"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: __snake_case : Dict = {"visual_embedding_dim": 512} __snake_case : Any = "multichoice" elif "vqa_advanced" in checkpoint_path: __snake_case : List[Any] = {"visual_embedding_dim": 2048} __snake_case : Optional[Any] = "vqa_advanced" elif "vqa" in checkpoint_path: __snake_case : Union[str, Any] = {"visual_embedding_dim": 2048, "num_labels": 3129} __snake_case : Union[str, Any] = "vqa" elif "nlvr" in checkpoint_path: __snake_case : Tuple = { "visual_embedding_dim": 1024, "num_labels": 2, } __snake_case : List[Any] = "nlvr" __snake_case : Union[str, Any] = VisualBertConfig(**lowercase ) # Load State Dict __snake_case : Any = load_state_dict(lowercase ) __snake_case : Dict = get_new_dict(lowercase , lowercase ) if model_type == "pretraining": __snake_case : Optional[Any] = VisualBertForPreTraining(lowercase ) elif model_type == "vqa": __snake_case : Tuple = VisualBertForQuestionAnswering(lowercase ) elif model_type == "nlvr": __snake_case : Tuple = VisualBertForVisualReasoning(lowercase ) elif model_type == "multichoice": __snake_case : List[Any] = VisualBertForMultipleChoice(lowercase ) model.load_state_dict(lowercase ) # Save Checkpoints Path(lowercase ).mkdir(exist_ok=lowercase ) model.save_pretrained(lowercase ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''orig_checkpoint_path''', type=str, help='''A path to .th on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', type=str, help='''Path to the output PyTorch model.''') _UpperCamelCase = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
326
0
"""simple docstring""" from __future__ import annotations def lowercase ( a__ : int | str ) -> bool: _UpperCamelCase = str(a__ ) return n == n[::-1] def lowercase ( a__ : int = 1000000 ) -> List[str]: _UpperCamelCase = 0 for i in range(1 , a__ ): if is_palindrome(a__ ) and is_palindrome(bin(a__ ).split('''b''' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
256
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowerCAmelCase__( lowercase : Optional[int] , lowercase : Any , lowercase : Dict , lowercase : List[str] , lowercase : List[Any] ) -> Tuple: # Load configuration defined in the metadata file with open(lowercase ) as metadata_file: __snake_case : int = json.load(lowercase ) __snake_case : Optional[int] = LukeConfig(use_entity_aware_attention=lowercase , **metadata["model_config"] ) # Load in the weights from the checkpoint_path __snake_case : List[Any] = torch.load(lowercase , map_location="cpu" )["module"] # Load the entity vocab file __snake_case : Tuple = load_original_entity_vocab(lowercase ) # add an entry for [MASK2] __snake_case : Optional[int] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 __snake_case : Union[str, Any] = XLMRobertaTokenizer.from_pretrained(metadata["model_config"]["bert_model_name"] ) # Add special tokens to the token vocabulary for downstream tasks __snake_case : Optional[int] = AddedToken("<ent>" , lstrip=lowercase , rstrip=lowercase ) __snake_case : Any = AddedToken("<ent2>" , lstrip=lowercase , rstrip=lowercase ) tokenizer.add_special_tokens({"additional_special_tokens": [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(f"""Saving tokenizer to {pytorch_dump_folder_path}""" ) tokenizer.save_pretrained(lowercase ) with open(os.path.join(lowercase , "tokenizer_config.json" ) , "r" ) as f: __snake_case : Tuple = json.load(lowercase ) __snake_case : List[Any] = "MLukeTokenizer" with open(os.path.join(lowercase , "tokenizer_config.json" ) , "w" ) as f: json.dump(lowercase , lowercase ) with open(os.path.join(lowercase , MLukeTokenizer.vocab_files_names["entity_vocab_file"] ) , "w" ) as f: json.dump(lowercase , lowercase ) __snake_case : Any = MLukeTokenizer.from_pretrained(lowercase ) # Initialize the embeddings of the special tokens __snake_case : str = tokenizer.convert_tokens_to_ids(["@"] )[0] __snake_case : List[str] = tokenizer.convert_tokens_to_ids(["#"] )[0] __snake_case : List[Any] = state_dict["embeddings.word_embeddings.weight"] __snake_case : Union[str, Any] = word_emb[ent_init_index].unsqueeze(0 ) __snake_case : Union[str, Any] = word_emb[enta_init_index].unsqueeze(0 ) __snake_case : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: __snake_case : List[Any] = state_dict[bias_name] __snake_case : Optional[int] = decoder_bias[ent_init_index].unsqueeze(0 ) __snake_case : int = decoder_bias[enta_init_index].unsqueeze(0 ) __snake_case : Any = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: __snake_case : Dict = f"""encoder.layer.{layer_index}.attention.self.""" __snake_case : Union[str, Any] = state_dict[prefix + matrix_name] __snake_case : str = state_dict[prefix + matrix_name] __snake_case : Union[str, Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks __snake_case : Any = state_dict["entity_embeddings.entity_embeddings.weight"] __snake_case : List[str] = entity_emb[entity_vocab["[MASK]"]].unsqueeze(0 ) __snake_case : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' __snake_case : List[Any] = state_dict["entity_predictions.bias"] __snake_case : List[Any] = entity_prediction_bias[entity_vocab["[MASK]"]].unsqueeze(0 ) __snake_case : Union[str, Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) __snake_case : Any = LukeForMaskedLM(config=lowercase ).eval() state_dict.pop("entity_predictions.decoder.weight" ) state_dict.pop("lm_head.decoder.weight" ) state_dict.pop("lm_head.decoder.bias" ) __snake_case : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith("lm_head" ) or key.startswith("entity_predictions" )): __snake_case : str = state_dict[key] else: __snake_case : str = state_dict[key] __snake_case , __snake_case : Union[str, Any] = model.load_state_dict(lowercase , strict=lowercase ) if set(lowercase ) != {"luke.embeddings.position_ids"}: raise ValueError(f"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(lowercase ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(f"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs __snake_case : int = MLukeTokenizer.from_pretrained(lowercase , task="entity_classification" ) __snake_case : Tuple = "ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan)." __snake_case : Union[str, Any] = (0, 9) __snake_case : Optional[int] = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) __snake_case : Any = model(**lowercase ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base __snake_case : Optional[Any] = torch.Size((1, 33, 768) ) __snake_case : Optional[int] = torch.tensor([[0.0_8_9_2, 0.0_5_9_6, -0.2_8_1_9], [0.0_1_3_4, 0.1_1_9_9, 0.0_5_7_3], [-0.0_1_6_9, 0.0_9_2_7, 0.0_6_4_4]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowercase , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base __snake_case : str = torch.Size((1, 1, 768) ) __snake_case : int = torch.tensor([[-0.1_4_8_2, 0.0_6_0_9, 0.0_3_2_2]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" f""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , lowercase , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction __snake_case : str = MLukeTokenizer.from_pretrained(lowercase ) __snake_case : Dict = "Tokyo is the capital of <mask>." __snake_case : Union[str, Any] = (24, 30) __snake_case : int = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) __snake_case : int = model(**lowercase ) __snake_case : Dict = encoding["input_ids"][0].tolist() __snake_case : Dict = input_ids.index(tokenizer.convert_tokens_to_ids("<mask>" ) ) __snake_case : Optional[int] = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(lowercase ) __snake_case : Optional[Any] = outputs.entity_logits[0][0].argmax().item() __snake_case : Optional[int] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith("en:" )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print("Saving PyTorch model to {}".format(lowercase ) ) model.save_pretrained(lowercase ) def lowerCAmelCase__( lowercase : Optional[int] ) -> List[Any]: __snake_case : Any = ["[MASK]", "[PAD]", "[UNK]"] __snake_case : Any = [json.loads(lowercase ) for line in open(lowercase )] __snake_case : Any = {} for entry in data: __snake_case : Any = entry["id"] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: __snake_case : Optional[int] = entity_id break __snake_case : Union[str, Any] = f"""{language}:{entity_name}""" __snake_case : Any = entity_id return new_mapping if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) _UpperCamelCase = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
326
0
import argparse import os import torch from transformers.utils import WEIGHTS_NAME lowerCAmelCase : int = ["""small""", """medium""", """large"""] lowerCAmelCase : Optional[Any] = """lm_head.decoder.weight""" lowerCAmelCase : int = """lm_head.weight""" def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = torch.load(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: str = d.pop(_UpperCAmelCase ) os.makedirs(_UpperCAmelCase , exist_ok=_UpperCAmelCase ) torch.save(_UpperCAmelCase , os.path.join(_UpperCAmelCase , _UpperCAmelCase ) ) if __name__ == "__main__": lowerCAmelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument("""--dialogpt_path""", default=""".""", type=str) lowerCAmelCase : List[str] = parser.parse_args() for MODEL in DIALOGPT_MODELS: lowerCAmelCase : List[Any] = os.path.join(args.dialogpt_path, f'''{MODEL}_ft.pkl''') lowerCAmelCase : Any = f'''./DialoGPT-{MODEL}''' convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
13
from maths.prime_factors import prime_factors def lowerCAmelCase__( lowercase : int ) -> int: if not isinstance(lowercase , lowercase ): __snake_case : Optional[int] = f"""Input value of [number={number}] must be an integer""" raise TypeError(lowercase ) if number < 1: raise ValueError("Input must be a positive integer" ) return -1 if len(prime_factors(lowercase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
326
0
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image _A = ['''text''', '''image''', '''audio'''] def __UpperCamelCase ( _A ): lowerCAmelCase_ = [] for input_type in input_types: if input_type == "text": inputs.append('''Text input''' ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO''' ) ) / '''000000039769.png''' ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(_A , _A ): inputs.append(create_inputs(_A ) ) else: raise ValueError(f"Invalid type requested: {input_type}" ) return inputs def __UpperCamelCase ( _A ): lowerCAmelCase_ = [] for output in outputs: if isinstance(_A , (str, AgentText) ): output_types.append('''text''' ) elif isinstance(_A , (Image.Image, AgentImage) ): output_types.append('''image''' ) elif isinstance(_A , (torch.Tensor, AgentAudio) ): output_types.append('''audio''' ) else: raise ValueError(f"Invalid output: {output}" ) return output_types @is_tool_test class A : def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" self.assertTrue(hasattr(self.tool, '''inputs''' ) ) self.assertTrue(hasattr(self.tool, '''outputs''' ) ) lowerCAmelCase_ = self.tool.inputs for _input in inputs: if isinstance(_input, UpperCamelCase__ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) lowerCAmelCase_ = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = create_inputs(self.tool.inputs ) lowerCAmelCase_ = self.tool(*UpperCamelCase__ ) # There is a single output if len(self.tool.outputs ) == 1: lowerCAmelCase_ = [outputs] self.assertListEqual(output_types(UpperCamelCase__ ), self.tool.outputs ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" self.assertTrue(hasattr(self.tool, '''description''' ) ) self.assertTrue(hasattr(self.tool, '''default_checkpoint''' ) ) self.assertTrue(self.tool.description.startswith('''This is a tool that''' ) ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = create_inputs(self.tool.inputs ) lowerCAmelCase_ = self.tool(*UpperCamelCase__ ) if not isinstance(UpperCamelCase__, UpperCamelCase__ ): lowerCAmelCase_ = [outputs] self.assertEqual(len(UpperCamelCase__ ), len(self.tool.outputs ) ) for output, output_type in zip(UpperCamelCase__, self.tool.outputs ): lowerCAmelCase_ = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(UpperCamelCase__, UpperCamelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = create_inputs(self.tool.inputs ) lowerCAmelCase_ = [] for _input, input_type in zip(UpperCamelCase__, self.tool.inputs ): if isinstance(UpperCamelCase__, UpperCamelCase__ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error lowerCAmelCase_ = self.tool(*UpperCamelCase__ ) if not isinstance(UpperCamelCase__, UpperCamelCase__ ): lowerCAmelCase_ = [outputs] self.assertEqual(len(UpperCamelCase__ ), len(self.tool.outputs ) )
278
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" @slow def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' __snake_case : Tuple = FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small" ) __snake_case : str = AutoTokenizer.from_pretrained("google/mt5-small" ) __snake_case : List[Any] = tokenizer("Hello there" , return_tensors="np" ).input_ids __snake_case : int = tokenizer("Hi I am" , return_tensors="np" ).input_ids __snake_case : Tuple = shift_tokens_right(UpperCAmelCase , model.config.pad_token_id , model.config.decoder_start_token_id ) __snake_case : Tuple = model(UpperCAmelCase , decoder_input_ids=UpperCAmelCase ).logits __snake_case : str = optax.softmax_cross_entropy(UpperCAmelCase , onehot(UpperCAmelCase , logits.shape[-1] ) ).mean() __snake_case : Any = -(labels.shape[-1] * loss.item()) __snake_case : List[str] = -84.9_127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
326
0
'''simple docstring''' from __future__ import annotations def _a( UpperCamelCase__ : list[float], UpperCamelCase__ : Any ): '''simple docstring''' print(f"Vertex\tShortest Distance from vertex {src}" ) for i, d in enumerate(UpperCamelCase__ ): print(f"{i}\t\t{d}" ) def _a( UpperCamelCase__ : list[dict[str, int]], UpperCamelCase__ : list[float], UpperCamelCase__ : int ): '''simple docstring''' for j in range(UpperCamelCase__ ): SCREAMING_SNAKE_CASE__ : List[Any] =(graph[j][k] for k in ["src", "dst", "weight"]) if distance[u] != float('''inf''' ) and distance[u] + w < distance[v]: return True return False def _a( UpperCamelCase__ : list[dict[str, int]], UpperCamelCase__ : int, UpperCamelCase__ : int, UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[Any] =[float('''inf''' )] * vertex_count SCREAMING_SNAKE_CASE__ : Optional[int] =0.0 for _ in range(vertex_count - 1 ): for j in range(UpperCamelCase__ ): SCREAMING_SNAKE_CASE__ : List[Any] =(graph[j][k] for k in ["src", "dst", "weight"]) if distance[u] != float('''inf''' ) and distance[u] + w < distance[v]: SCREAMING_SNAKE_CASE__ : Optional[Any] =distance[u] + w SCREAMING_SNAKE_CASE__ : Union[str, Any] =check_negative_cycle(UpperCamelCase__, UpperCamelCase__, UpperCamelCase__ ) if negative_cycle_exists: raise Exception('''Negative cycle found''' ) return distance if __name__ == "__main__": import doctest doctest.testmod() a_ = int(input('Enter number of vertices: ').strip()) a_ = int(input('Enter number of edges: ').strip()) a_ = [{} for _ in range(E)] for i in range(E): print('Edge ', i + 1) a_ , a_ , a_ = ( int(x) for x in input('Enter source, destination, weight: ').strip().split(' ') ) a_ = {'src': src, 'dst': dest, 'weight': weight} a_ = int(input('\nEnter shortest path source:').strip()) a_ = bellman_ford(graph, V, E, source) print_distance(shortest_distance, 0)
152
import torch from torch import nn from transformers import CLIPPreTrainedModel, CLIPVisionModel from ...models.attention import BasicTransformerBlock from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name class _lowerCamelCase ( a ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=768 ) -> List[str]: '''simple docstring''' super().__init__(UpperCAmelCase ) __snake_case : Optional[int] = proj_size __snake_case : str = CLIPVisionModel(UpperCAmelCase ) __snake_case : Tuple = PaintByExampleMapper(UpperCAmelCase ) __snake_case : Union[str, Any] = nn.LayerNorm(config.hidden_size ) __snake_case : Optional[Any] = nn.Linear(config.hidden_size , self.proj_size ) # uncondition for scaling __snake_case : Optional[int] = nn.Parameter(torch.randn((1, 1, self.proj_size) ) ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase=False ) -> List[str]: '''simple docstring''' __snake_case : int = self.model(pixel_values=UpperCAmelCase ) __snake_case : Optional[int] = clip_output.pooler_output __snake_case : Any = self.mapper(latent_states[:, None] ) __snake_case : Any = self.final_layer_norm(UpperCAmelCase ) __snake_case : str = self.proj_out(UpperCAmelCase ) if return_uncond_vector: return latent_states, self.uncond_vector return latent_states class _lowerCamelCase ( nn.Module ): """simple docstring""" def __init__( self , UpperCAmelCase ) -> List[Any]: '''simple docstring''' super().__init__() __snake_case : List[Any] = (config.num_hidden_layers + 1) // 5 __snake_case : Dict = config.hidden_size __snake_case : str = 1 __snake_case : List[Any] = nn.ModuleList( [ BasicTransformerBlock(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , activation_fn="gelu" , attention_bias=UpperCAmelCase ) for _ in range(UpperCAmelCase ) ] ) def UpperCAmelCase ( self , UpperCAmelCase ) -> str: '''simple docstring''' for block in self.blocks: __snake_case : int = block(UpperCAmelCase ) return hidden_states
326
0
"""simple docstring""" from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCamelCase : Tuple = logging.get_logger(__name__) _UpperCamelCase : Optional[int] = { "microsoft/xprophetnet-large-wiki100-cased": ( "https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): lowerCamelCase__ : Dict = "xlm-prophetnet" lowerCamelCase__ : int = ["past_key_values"] lowerCamelCase__ : str = { "num_attention_heads": "num_encoder_attention_heads", } def __init__( self , a = 0.1 , a = "gelu" , a = 3_0_5_2_2 , a = 1_0_2_4 , a = 4_0_9_6 , a = 1_2 , a = 1_6 , a = 4_0_9_6 , a = 1_2 , a = 1_6 , a = 0.1 , a = 0.1 , a = 5_1_2 , a = 0.02 , a = True , a = True , a = 0 , a = 2 , a = 3_2 , a = 1_2_8 , a = False , a = 0.0 , a = True , a = 0 , a = 1 , a = 2 , **a , ) -> str: lowercase__ : List[Any] = vocab_size lowercase__ : Optional[Any] = hidden_size lowercase__ : Union[str, Any] = encoder_ffn_dim lowercase__ : Optional[int] = num_encoder_layers lowercase__ : Optional[Any] = num_encoder_attention_heads lowercase__ : Union[str, Any] = decoder_ffn_dim lowercase__ : List[str] = num_decoder_layers lowercase__ : List[str] = num_decoder_attention_heads lowercase__ : str = max_position_embeddings lowercase__ : str = init_std # Normal(0, this parameter) lowercase__ : Optional[int] = activation_function # parameters for xlmprophetnet lowercase__ : Tuple = ngram lowercase__ : Optional[int] = num_buckets lowercase__ : Optional[int] = relative_max_distance lowercase__ : Any = disable_ngram_loss lowercase__ : Optional[Any] = eps # 3 Types of Dropout lowercase__ : List[Any] = attention_dropout lowercase__ : List[str] = activation_dropout lowercase__ : Optional[Any] = dropout lowercase__ : Union[str, Any] = use_cache super().__init__( pad_token_id=a , bos_token_id=a , eos_token_id=a , is_encoder_decoder=a , add_cross_attention=a , decoder_start_token_id=a , **a , ) @property def _UpperCAmelCase ( self ) -> int: return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def _UpperCAmelCase ( self , a ) -> List[str]: raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and' ' `num_decoder_layers`.' )
77
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
326
0
import json import logging import os import socket import git import numpy as np import torch logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO, ) __lowercase = logging.getLogger(__name__) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :int = git.Repo(search_parent_directories=SCREAMING_SNAKE_CASE ) __UpperCamelCase :Union[str, Any] = { "repo_id": str(SCREAMING_SNAKE_CASE ), "repo_sha": str(repo.head.object.hexsha ), "repo_branch": str(repo.active_branch ), } with open(os.path.join(SCREAMING_SNAKE_CASE , '''git_log.json''' ) , '''w''' ) as f: json.dump(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , indent=4 ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' if params.n_gpu <= 0: __UpperCamelCase :Union[str, Any] = 0 __UpperCamelCase :Optional[int] = -1 __UpperCamelCase :Union[str, Any] = True __UpperCamelCase :Tuple = False return assert torch.cuda.is_available() logger.info('''Initializing GPUs''' ) if params.n_gpu > 1: assert params.local_rank != -1 __UpperCamelCase :Optional[int] = int(os.environ['''WORLD_SIZE'''] ) __UpperCamelCase :int = int(os.environ['''N_GPU_NODE'''] ) __UpperCamelCase :Union[str, Any] = int(os.environ['''RANK'''] ) # number of nodes / node ID __UpperCamelCase :Optional[Any] = params.world_size // params.n_gpu_per_node __UpperCamelCase :Optional[Any] = params.global_rank // params.n_gpu_per_node __UpperCamelCase :Union[str, Any] = True assert params.n_nodes == int(os.environ['''N_NODES'''] ) assert params.node_id == int(os.environ['''NODE_RANK'''] ) # local job (single GPU) else: assert params.local_rank == -1 __UpperCamelCase :Any = 1 __UpperCamelCase :str = 0 __UpperCamelCase :Optional[Any] = 0 __UpperCamelCase :Dict = 0 __UpperCamelCase :int = 1 __UpperCamelCase :Optional[Any] = 1 __UpperCamelCase :Tuple = False # sanity checks assert params.n_nodes >= 1 assert 0 <= params.node_id < params.n_nodes assert 0 <= params.local_rank <= params.global_rank < params.world_size assert params.world_size == params.n_nodes * params.n_gpu_per_node # define whether this is the master process / if we are in multi-node distributed mode __UpperCamelCase :List[Any] = params.node_id == 0 and params.local_rank == 0 __UpperCamelCase :List[Any] = params.n_nodes > 1 # summary __UpperCamelCase :List[Any] = f"""--- Global rank: {params.global_rank} - """ logger.info(PREFIX + '''Number of nodes: %i''' % params.n_nodes ) logger.info(PREFIX + '''Node ID : %i''' % params.node_id ) logger.info(PREFIX + '''Local rank : %i''' % params.local_rank ) logger.info(PREFIX + '''World size : %i''' % params.world_size ) logger.info(PREFIX + '''GPUs per node : %i''' % params.n_gpu_per_node ) logger.info(PREFIX + '''Master : %s''' % str(params.is_master ) ) logger.info(PREFIX + '''Multi-node : %s''' % str(params.multi_node ) ) logger.info(PREFIX + '''Multi-GPU : %s''' % str(params.multi_gpu ) ) logger.info(PREFIX + '''Hostname : %s''' % socket.gethostname() ) # set GPU device torch.cuda.set_device(params.local_rank ) # initialize multi-GPU if params.multi_gpu: logger.info('''Initializing PyTorch distributed''' ) torch.distributed.init_process_group( init_method='''env://''' , backend='''nccl''' , ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' np.random.seed(args.seed ) torch.manual_seed(args.seed ) if args.n_gpu > 0: torch.cuda.manual_seed_all(args.seed )
43
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = torch.device('''cpu''') def lowerCAmelCase__( ) -> Any: __snake_case : List[Any] = "http://images.cocodataset.org/val2017/000000039769.jpg" __snake_case : Optional[int] = Image.open(requests.get(lowercase , stream=lowercase ).raw ) return im def lowerCAmelCase__( lowercase : Dict ) -> List[Any]: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1_703E00, 2.1_107E00, -2.0_811E00, 8.8_685E-01, 2.4_360E-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9_636E-01, 2.3_478E-01, -1.6_963E00, -1.7_381E00, -8.6_337E-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2_768E-01, -4.7_429E-01, -1.0_897E00, -1.0_248E00, 3.5_523E-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5_330E-01, 2.4_211E-01, -6.0_185E-01, -8.2_789E-01, -6.0_446E-02] ) def lowerCAmelCase__( lowercase : Tuple , lowercase : Union[str, Any] , lowercase : Union[str, Any] ) -> List[Any]: __snake_case : List[Any] = dct.pop(lowercase ) __snake_case : List[Any] = val def lowerCAmelCase__( lowercase : Union[str, Any] ) -> Tuple: __snake_case : Optional[Any] = [] for k in state_dict.keys(): __snake_case : Union[str, Any] = k if ".pwconv" in k: __snake_case : Any = k_new.replace(".pwconv" , ".point_wise_conv" ) if ".dwconv" in k: __snake_case : List[Any] = k_new.replace(".dwconv" , ".depth_wise_conv" ) if ".Proj." in k: __snake_case : Optional[int] = k_new.replace(".Proj." , ".proj." ) if "patch_embed" in k_new: __snake_case : int = k_new.replace("patch_embed" , "swiftformer.patch_embed.patch_embedding" ) if "network" in k_new: __snake_case : int = k_new.split("." ) if ls[2].isdigit(): __snake_case : List[Any] = "swiftformer.encoder.network." + ls[1] + ".blocks." + ls[2] + "." + ".".join(ls[3:] ) else: __snake_case : Optional[int] = k_new.replace("network" , "swiftformer.encoder.network" ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def lowerCAmelCase__( lowercase : List[Any] , lowercase : Optional[Any] , lowercase : List[str] ) -> Union[str, Any]: __snake_case : List[str] = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size __snake_case : Tuple = 1000 __snake_case : Any = "huggingface/label-files" __snake_case : int = "imagenet-1k-id2label.json" __snake_case : Dict = json.load(open(hf_hub_download(lowercase , lowercase , repo_type="dataset" ) , "r" ) ) __snake_case : str = {int(lowercase ): v for k, v in idalabel.items()} __snake_case : int = idalabel __snake_case : Optional[int] = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": __snake_case : Optional[Any] = [3, 3, 6, 4] __snake_case : Optional[int] = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": __snake_case : List[str] = [3, 3, 9, 6] __snake_case : Optional[Any] = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": __snake_case : Optional[int] = [4, 3, 10, 5] __snake_case : Dict = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": __snake_case : str = [4, 4, 12, 6] __snake_case : Optional[Any] = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith("https" ): __snake_case : Optional[Any] = torch.hub.load_state_dict_from_url(lowercase , map_location="cpu" , check_hash=lowercase ) else: __snake_case : Tuple = torch.load(lowercase , map_location="cpu" ) __snake_case : Optional[int] = checkpoint __snake_case : Any = create_rename_keys(lowercase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(lowercase , lowercase , lowercase ) # load HuggingFace model __snake_case : Tuple = SwiftFormerForImageClassification(lowercase ).eval() hf_model.load_state_dict(lowercase ) # prepare test inputs __snake_case : Optional[Any] = prepare_img() __snake_case : str = ViTImageProcessor.from_pretrained("preprocessor_config" ) __snake_case : Optional[int] = processor(images=lowercase , return_tensors="pt" ) # compare outputs from both models __snake_case : str = get_expected_output(lowercase ) __snake_case : Optional[int] = hf_model(inputs["pixel_values"] ).logits assert hf_logits.shape == torch.Size([1, 1000] ) assert torch.allclose(hf_logits[0, 0:5] , lowercase , atol=1E-3 ) Path(lowercase ).mkdir(exist_ok=lowercase ) print(f"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(lowercase ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--swiftformer_name''', default='''swiftformer_xs''', choices=['''swiftformer_xs''', '''swiftformer_s''', '''swiftformer_l1''', '''swiftformer_l3'''], type=str, help='''Name of the SwiftFormer model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''./converted_outputs/''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--original_ckpt''', default=None, type=str, help='''Path to the original model checkpoint.''') _UpperCamelCase = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
326
0
'''simple docstring''' from __future__ import annotations _SCREAMING_SNAKE_CASE : Tuple = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] _SCREAMING_SNAKE_CASE : Any = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def UpperCamelCase_( snake_case : list[float] ): '''simple docstring''' snake_case_ = [] snake_case_ = len(snake_case ) for i in range(snake_case ): snake_case_ = -1 for j in range(i + 1 , snake_case ): if arr[i] < arr[j]: snake_case_ = arr[j] break result.append(snake_case ) return result def UpperCamelCase_( snake_case : list[float] ): '''simple docstring''' snake_case_ = [] for i, outer in enumerate(snake_case ): snake_case_ = -1 for inner in arr[i + 1 :]: if outer < inner: snake_case_ = inner break result.append(snake_case ) return result def UpperCamelCase_( snake_case : list[float] ): '''simple docstring''' snake_case_ = len(snake_case ) snake_case_ = [] snake_case_ = [-1] * arr_size for index in reversed(range(snake_case ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: snake_case_ = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) _SCREAMING_SNAKE_CASE : Any = ( "from __main__ import arr, next_greatest_element_slow, " "next_greatest_element_fast, next_greatest_element" ) print( "next_greatest_element_slow():", timeit("next_greatest_element_slow(arr)", setup=setup), ) print( "next_greatest_element_fast():", timeit("next_greatest_element_fast(arr)", setup=setup), ) print( " next_greatest_element():", timeit("next_greatest_element(arr)", setup=setup), )
85
import json import logging import os import socket import git import numpy as np import torch logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO, ) _UpperCamelCase = logging.getLogger(__name__) def lowerCAmelCase__( lowercase : str ) -> List[str]: __snake_case : int = git.Repo(search_parent_directories=lowercase ) __snake_case : Union[str, Any] = { "repo_id": str(lowercase ), "repo_sha": str(repo.head.object.hexsha ), "repo_branch": str(repo.active_branch ), } with open(os.path.join(lowercase , "git_log.json" ) , "w" ) as f: json.dump(lowercase , lowercase , indent=4 ) def lowerCAmelCase__( lowercase : Optional[Any] ) -> Optional[Any]: if params.n_gpu <= 0: __snake_case : Union[str, Any] = 0 __snake_case : Optional[int] = -1 __snake_case : Union[str, Any] = True __snake_case : Tuple = False return assert torch.cuda.is_available() logger.info("Initializing GPUs" ) if params.n_gpu > 1: assert params.local_rank != -1 __snake_case : Optional[int] = int(os.environ["WORLD_SIZE"] ) __snake_case : int = int(os.environ["N_GPU_NODE"] ) __snake_case : Union[str, Any] = int(os.environ["RANK"] ) # number of nodes / node ID __snake_case : Optional[Any] = params.world_size // params.n_gpu_per_node __snake_case : Optional[Any] = params.global_rank // params.n_gpu_per_node __snake_case : Union[str, Any] = True assert params.n_nodes == int(os.environ["N_NODES"] ) assert params.node_id == int(os.environ["NODE_RANK"] ) # local job (single GPU) else: assert params.local_rank == -1 __snake_case : Any = 1 __snake_case : str = 0 __snake_case : Optional[Any] = 0 __snake_case : Dict = 0 __snake_case : int = 1 __snake_case : Optional[Any] = 1 __snake_case : Tuple = False # sanity checks assert params.n_nodes >= 1 assert 0 <= params.node_id < params.n_nodes assert 0 <= params.local_rank <= params.global_rank < params.world_size assert params.world_size == params.n_nodes * params.n_gpu_per_node # define whether this is the master process / if we are in multi-node distributed mode __snake_case : List[Any] = params.node_id == 0 and params.local_rank == 0 __snake_case : List[Any] = params.n_nodes > 1 # summary __snake_case : List[Any] = f"""--- Global rank: {params.global_rank} - """ logger.info(PREFIX + "Number of nodes: %i" % params.n_nodes ) logger.info(PREFIX + "Node ID : %i" % params.node_id ) logger.info(PREFIX + "Local rank : %i" % params.local_rank ) logger.info(PREFIX + "World size : %i" % params.world_size ) logger.info(PREFIX + "GPUs per node : %i" % params.n_gpu_per_node ) logger.info(PREFIX + "Master : %s" % str(params.is_master ) ) logger.info(PREFIX + "Multi-node : %s" % str(params.multi_node ) ) logger.info(PREFIX + "Multi-GPU : %s" % str(params.multi_gpu ) ) logger.info(PREFIX + "Hostname : %s" % socket.gethostname() ) # set GPU device torch.cuda.set_device(params.local_rank ) # initialize multi-GPU if params.multi_gpu: logger.info("Initializing PyTorch distributed" ) torch.distributed.init_process_group( init_method="env://" , backend="nccl" , ) def lowerCAmelCase__( lowercase : Dict ) -> Union[str, Any]: np.random.seed(args.seed ) torch.manual_seed(args.seed ) if args.n_gpu > 0: torch.cuda.manual_seed_all(args.seed )
326
0
"""simple docstring""" from __future__ import annotations def lowerCamelCase__ ( _lowerCamelCase : int ) -> bool: lowerCamelCase_ = str(_lowerCamelCase ) return len(_lowerCamelCase ) == 9 and set(_lowerCamelCase ) == set('123456789' ) def lowerCamelCase__ ( ) -> int | None: for base_num in range(9999 , 4999 , -1 ): lowerCamelCase_ = 100002 * base_num if is_9_pandigital(_lowerCamelCase ): return candidate for base_num in range(333 , 99 , -1 ): lowerCamelCase_ = 1002003 * base_num if is_9_pandigital(_lowerCamelCase ): return candidate return None if __name__ == "__main__": print(F'''{solution() = }''')
183
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : str =JukeboxTokenizer UpperCAmelCase_ : Tuple ={ "artist": "Zac Brown Band", "genres": "Country", "lyrics": "I met a traveller from an antique land,\n Who said \"Two vast and trunkless legs of stone\n Stand in the desert. . . . Near them, on the sand,\n Half sunk a shattered visage lies, whose frown,\n And wrinkled lip, and sneer of cold command,\n Tell that its sculptor well those passions read\n Which yet survive, stamped on these lifeless things,\n The hand that mocked them, and the heart that fed;\n And on the pedestal, these words appear:\n My name is Ozymandias, King of Kings;\n Look on my Works, ye Mighty, and despair!\n Nothing beside remains. Round the decay\n Of that colossal Wreck, boundless and bare\n The lone and level sands stretch far away\n ", } @require_torch def UpperCAmelCase ( self ) -> str: '''simple docstring''' import torch __snake_case : List[str] = JukeboxTokenizer.from_pretrained("openai/jukebox-1b-lyrics" ) __snake_case : Union[str, Any] = tokenizer(**self.metas )["input_ids"] # fmt: off __snake_case : Optional[Any] = [ torch.tensor([[ 0, 0, 0, 7169, 507, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) ) @require_torch def UpperCAmelCase ( self ) -> str: '''simple docstring''' import torch __snake_case : Optional[Any] = JukeboxTokenizer.from_pretrained("openai/jukebox-5b-lyrics" ) __snake_case : Tuple = tokenizer(**self.metas )["input_ids"] # fmt: off __snake_case : int = [ torch.tensor([[ 0, 0, 0, 1069, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) )
326
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __lowerCAmelCase : int = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Dict = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys __lowerCAmelCase : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
88
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 _UpperCamelCase = logging.get_logger(__name__) class _lowerCamelCase : """simple docstring""" UpperCAmelCase_ : str UpperCAmelCase_ : str =None @staticmethod def UpperCAmelCase ( ) -> Optional[int]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> List[str]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self , UpperCAmelCase ) -> Optional[int]: '''simple docstring''' raise NotImplementedError def UpperCAmelCase ( self ) -> Dict: '''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 UpperCAmelCase ( cls ) -> Tuple: '''simple docstring''' return F"""`pip install {cls.pip_package or cls.name}`""" class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Optional[int] ="optuna" @staticmethod def UpperCAmelCase ( ) -> Union[str, Any]: '''simple docstring''' return is_optuna_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: '''simple docstring''' return run_hp_search_optuna(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> int: '''simple docstring''' return default_hp_space_optuna(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : List[str] ="ray" UpperCAmelCase_ : Dict ="'ray[tune]'" @staticmethod def UpperCAmelCase ( ) -> str: '''simple docstring''' return is_ray_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> List[Any]: '''simple docstring''' return run_hp_search_ray(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> str: '''simple docstring''' return default_hp_space_ray(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Tuple ="sigopt" @staticmethod def UpperCAmelCase ( ) -> int: '''simple docstring''' return is_sigopt_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return run_hp_search_sigopt(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> Dict: '''simple docstring''' return default_hp_space_sigopt(UpperCAmelCase ) class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : str ="wandb" @staticmethod def UpperCAmelCase ( ) -> Optional[Any]: '''simple docstring''' return is_wandb_available() def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' return run_hp_search_wandb(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> List[str]: '''simple docstring''' return default_hp_space_wandb(UpperCAmelCase ) _UpperCamelCase = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def lowerCAmelCase__( ) -> str: __snake_case : Optional[int] = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(lowercase ) > 0: __snake_case : Dict = available_backends[0].name if len(lowercase ) > 1: logger.info( f"""{len(lowercase )} 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() ) )
326
0
def A ( lowercase ) -> str: '''simple docstring''' if not all(char in '01' for char in bin_string ): raise ValueError('Non-binary value was passed to the function' ) if not bin_string: raise ValueError('Empty string was passed to the function' ) UpperCamelCase = "" while len(lowercase ) % 3 != 0: UpperCamelCase = "0" + bin_string UpperCamelCase = [ bin_string[index : index + 3] for index in range(len(lowercase ) ) if index % 3 == 0 ] for bin_group in bin_string_in_3_list: UpperCamelCase = 0 for index, val in enumerate(lowercase ): oct_val += int(2 ** (2 - index) * int(lowercase ) ) oct_string += str(lowercase ) return oct_string if __name__ == "__main__": from doctest import testmod testmod()
222
import math def lowerCAmelCase__( lowercase : list , lowercase : int = 0 , lowercase : int = 0 ) -> list: __snake_case : Any = end or len(lowercase ) for i in range(lowercase , lowercase ): __snake_case : List[str] = i __snake_case : Union[str, Any] = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __snake_case : Optional[Any] = array[temp_index - 1] temp_index -= 1 __snake_case : Any = temp_index_value return array def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int ) -> None: # Max Heap __snake_case : Any = index __snake_case : Optional[Any] = 2 * index + 1 # Left Node __snake_case : str = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __snake_case : Optional[int] = left_index if right_index < heap_size and array[largest] < array[right_index]: __snake_case : Tuple = right_index if largest != index: __snake_case , __snake_case : int = array[largest], array[index] heapify(lowercase , lowercase , lowercase ) def lowerCAmelCase__( lowercase : list ) -> list: __snake_case : List[str] = len(lowercase ) for i in range(n // 2 , -1 , -1 ): heapify(lowercase , lowercase , lowercase ) for i in range(n - 1 , 0 , -1 ): __snake_case , __snake_case : Optional[Any] = array[0], array[i] heapify(lowercase , 0 , lowercase ) return array def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int ) -> int: if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int ) -> int: __snake_case : Union[str, Any] = low __snake_case : Union[str, Any] = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __snake_case , __snake_case : str = array[j], array[i] i += 1 def lowerCAmelCase__( lowercase : list ) -> list: if len(lowercase ) == 0: return array __snake_case : Union[str, Any] = 2 * math.ceil(math.loga(len(lowercase ) ) ) __snake_case : Dict = 16 return intro_sort(lowercase , 0 , len(lowercase ) , lowercase , lowercase ) def lowerCAmelCase__( lowercase : list , lowercase : int , lowercase : int , lowercase : int , lowercase : int ) -> list: while end - start > size_threshold: if max_depth == 0: return heap_sort(lowercase ) max_depth -= 1 __snake_case : List[str] = median_of_a(lowercase , lowercase , start + ((end - start) // 2) + 1 , end - 1 ) __snake_case : Optional[Any] = partition(lowercase , lowercase , lowercase , lowercase ) intro_sort(lowercase , lowercase , lowercase , lowercase , lowercase ) __snake_case : List[str] = p return insertion_sort(lowercase , lowercase , lowercase ) if __name__ == "__main__": import doctest doctest.testmod() _UpperCamelCase = input('''Enter numbers separated by a comma : ''').strip() _UpperCamelCase = [float(item) for item in user_input.split(''',''')] print(sort(unsorted))
326
0
import itertools import math def _lowercase ( UpperCamelCase_ ) -> bool: '''simple docstring''' 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 def _lowercase ( ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE__ = 2 while True: if is_prime(UpperCamelCase_ ): yield num num += 1 def _lowercase ( UpperCamelCase_ = 10001 ) -> int: '''simple docstring''' return next(itertools.islice(prime_generator() , nth - 1 , UpperCamelCase_ ) ) if __name__ == "__main__": print(F"""{solution() = }""")
176
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def lowerCAmelCase__( lowercase : Dict ) -> str: # picklable for multiprocessing return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def lowerCAmelCase__( ) -> List[Any]: with parallel_backend("spark" ): assert ParallelBackendConfig.backend_name == "spark" __snake_case : Any = [1, 2, 3] with pytest.raises(lowercase ): with parallel_backend("unsupported backend" ): map_nested(lowercase , lowercase , num_proc=2 ) with pytest.raises(lowercase ): with parallel_backend("unsupported backend" ): map_nested(lowercase , lowercase , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("num_proc" , [2, -1] ) def lowerCAmelCase__( lowercase : Dict ) -> Dict: __snake_case : Any = [1, 2] __snake_case : Dict = {"a": 1, "b": 2} __snake_case : Optional[int] = {"a": [1, 2], "b": [3, 4]} __snake_case : int = {"a": {"1": 1}, "b": 2} __snake_case : str = {"a": 1, "b": 2, "c": 3, "d": 4} __snake_case : Dict = [2, 3] __snake_case : Tuple = {"a": 2, "b": 3} __snake_case : int = {"a": [2, 3], "b": [4, 5]} __snake_case : Dict = {"a": {"1": 2}, "b": 3} __snake_case : str = {"a": 2, "b": 3, "c": 4, "d": 5} with parallel_backend("spark" ): assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa assert map_nested(lowercase , lowercase , num_proc=lowercase ) == expected_map_nested_sa
326
0
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
256
import math import random from typing import Any from .hill_climbing import SearchProblem def lowerCAmelCase__( lowercase : Dict , lowercase : bool = True , lowercase : float = math.inf , lowercase : float = -math.inf , lowercase : float = math.inf , lowercase : float = -math.inf , lowercase : bool = False , lowercase : float = 100 , lowercase : float = 0.0_1 , lowercase : float = 1 , ) -> Any: __snake_case : Optional[Any] = False __snake_case : Optional[Any] = search_prob __snake_case : str = start_temperate __snake_case : List[Any] = [] __snake_case : str = 0 __snake_case : Dict = None while not search_end: __snake_case : List[Any] = current_state.score() if best_state is None or current_score > best_state.score(): __snake_case : List[Any] = current_state scores.append(lowercase ) iterations += 1 __snake_case : Dict = None __snake_case : str = current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to __snake_case : Any = random.randint(0 , len(lowercase ) - 1 ) # picking a random neighbor __snake_case : int = neighbors.pop(lowercase ) __snake_case : Optional[Any] = picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: __snake_case : Any = change * -1 # in case we are finding minimum if change > 0: # improves the solution __snake_case : List[str] = picked_neighbor else: __snake_case : Optional[Any] = (math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability __snake_case : str = picked_neighbor __snake_case : Optional[Any] = current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor __snake_case : Optional[Any] = True else: __snake_case : str = next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(lowercase ) , lowercase ) plt.xlabel("Iterations" ) plt.ylabel("Function values" ) plt.show() return best_state if __name__ == "__main__": def lowerCAmelCase__( lowercase : List[str] , lowercase : Tuple ) -> str: return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) _UpperCamelCase = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing( prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) # starting the problem with initial coordinates (12, 47) _UpperCamelCase = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing( prob, find_max=True, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) def lowerCAmelCase__( lowercase : Any , lowercase : Union[str, Any] ) -> Any: return (3 * x**2) - (6 * y) _UpperCamelCase = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing(prob, find_max=False, visualization=True) print( '''The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' F'''{local_min.score()}''' ) _UpperCamelCase = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) _UpperCamelCase = simulated_annealing(prob, find_max=True, visualization=True) print( '''The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' F'''{local_min.score()}''' )
326
0
import requests lowerCAmelCase : List[str] = """YOUR API KEY""" def A_ ( _UpperCAmelCase , _UpperCAmelCase = giphy_api_key ): SCREAMING_SNAKE_CASE_: List[str] = "+".join(query.split() ) SCREAMING_SNAKE_CASE_: Optional[int] = f"https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}" SCREAMING_SNAKE_CASE_: str = requests.get(_UpperCAmelCase ).json()["data"] return [gif["url"] for gif in gifs] if __name__ == "__main__": print("""\n""".join(get_gifs("""space ship""")))
13
import warnings from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =["image_processor", "tokenizer"] UpperCAmelCase_ : Tuple ="FlavaImageProcessor" UpperCAmelCase_ : List[Any] =("BertTokenizer", "BertTokenizerFast") def __init__( self , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase ) -> int: '''simple docstring''' __snake_case : List[Any] = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) __snake_case : List[Any] = kwargs.pop("feature_extractor" ) __snake_case : Any = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = self.image_processor def __call__( self , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = True , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = 0 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = True , UpperCAmelCase = None , **UpperCAmelCase , ) -> List[Any]: '''simple docstring''' if text is None and images is None: raise ValueError("You have to specify either text or images. Both cannot be none." ) if text is not None: __snake_case : Union[str, Any] = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if images is not None: __snake_case : Union[str, Any] = self.image_processor( UpperCAmelCase , return_image_mask=UpperCAmelCase , return_codebook_pixels=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if text is not None and images is not None: encoding.update(UpperCAmelCase ) return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def UpperCAmelCase ( self , *UpperCAmelCase , **UpperCAmelCase ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def UpperCAmelCase ( self , *UpperCAmelCase , **UpperCAmelCase ) -> Tuple: '''simple docstring''' return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case : List[Any] = self.tokenizer.model_input_names __snake_case : Union[str, Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
326
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available _A = { '''configuration_gpt_neo''': ['''GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTNeoConfig''', '''GPTNeoOnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ '''GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTNeoForCausalLM''', '''GPTNeoForQuestionAnswering''', '''GPTNeoForSequenceClassification''', '''GPTNeoForTokenClassification''', '''GPTNeoModel''', '''GPTNeoPreTrainedModel''', '''load_tf_weights_in_gpt_neo''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ '''FlaxGPTNeoForCausalLM''', '''FlaxGPTNeoModel''', '''FlaxGPTNeoPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_neo import GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoConfig, GPTNeoOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neo import ( GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoForCausalLM, GPTNeoForQuestionAnswering, GPTNeoForSequenceClassification, GPTNeoForTokenClassification, GPTNeoModel, GPTNeoPreTrainedModel, load_tf_weights_in_gpt_neo, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_gpt_neo import FlaxGPTNeoForCausalLM, FlaxGPTNeoModel, FlaxGPTNeoPreTrainedModel else: import sys _A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
278
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model'''} _UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', } } _UpperCamelCase = { '''camembert-base''': 512, } _UpperCamelCase = '''▁''' class _lowerCamelCase ( a ): """simple docstring""" UpperCAmelCase_ : Optional[Any] =VOCAB_FILES_NAMES UpperCAmelCase_ : str =PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase_ : str =["input_ids", "attention_mask"] def __init__( self , UpperCAmelCase , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<s>" , UpperCAmelCase="<unk>" , UpperCAmelCase="<pad>" , UpperCAmelCase="<mask>" , UpperCAmelCase=["<s>NOTUSED", "</s>NOTUSED"] , UpperCAmelCase = None , **UpperCAmelCase , ) -> None: '''simple docstring''' __snake_case : Dict = AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token __snake_case : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , additional_special_tokens=UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **UpperCAmelCase , ) __snake_case : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(UpperCAmelCase ) ) __snake_case : Dict = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> __snake_case : str = {"<s>NOTUSED": 0, "<pad>": 1, "</s>NOTUSED": 2, "<unk>": 3} __snake_case : Optional[int] = len(self.fairseq_tokens_to_ids ) __snake_case : Any = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) __snake_case : List[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __snake_case : Dict = [self.cls_token_id] __snake_case : Any = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = 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 [1] + ([0] * len(UpperCAmelCase )) + [1] return [1] + ([0] * len(UpperCAmelCase )) + [1, 1] + ([0] * len(UpperCAmelCase )) + [1] def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: '''simple docstring''' __snake_case : int = [self.sep_token_id] __snake_case : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCAmelCase ( self ) -> int: '''simple docstring''' return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case : Optional[int] = {self.convert_ids_to_tokens(UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCAmelCase ( self , UpperCAmelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(UpperCAmelCase , out_type=UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> List[Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(UpperCAmelCase ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(UpperCAmelCase ) def UpperCAmelCase ( self , UpperCAmelCase ) -> Tuple: '''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 UpperCAmelCase ( self , UpperCAmelCase ) -> Optional[int]: '''simple docstring''' __snake_case : Tuple = [] __snake_case : Union[str, Any] = "" __snake_case : Optional[int] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(UpperCAmelCase ) + token __snake_case : List[Any] = True __snake_case : Union[str, Any] = [] else: current_sub_tokens.append(UpperCAmelCase ) __snake_case : int = False out_string += self.sp_model.decode(UpperCAmelCase ) return out_string.strip() def __getstate__( self ) -> List[Any]: '''simple docstring''' __snake_case : str = self.__dict__.copy() __snake_case : Optional[Any] = None return state def __setstate__( self , UpperCAmelCase ) -> str: '''simple docstring''' __snake_case : Optional[Any] = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): __snake_case : List[str] = {} __snake_case : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return __snake_case : Optional[Any] = os.path.join( UpperCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase , "wb" ) as fi: __snake_case : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase ) return (out_vocab_file,)
326
0
'''simple docstring''' class __SCREAMING_SNAKE_CASE : def __init__( self : Optional[int] , __lowercase : Dict ) -> None: SCREAMING_SNAKE_CASE__ : Optional[int] =len(__lowercase ) SCREAMING_SNAKE_CASE__ : Any =[0] * len_array if len_array > 0: SCREAMING_SNAKE_CASE__ : Tuple =array[0] for i in range(1 , __lowercase ): SCREAMING_SNAKE_CASE__ : Optional[Any] =self.prefix_sum[i - 1] + array[i] def __magic_name__ ( self : List[str] , __lowercase : Union[str, Any] , __lowercase : Optional[Any] ) -> int: if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def __magic_name__ ( self : Dict , __lowercase : Optional[int] ) -> bool: SCREAMING_SNAKE_CASE__ : Optional[Any] ={0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(__lowercase ) return False if __name__ == "__main__": import doctest doctest.testmod()
152
def lowerCAmelCase__( lowercase : list[int] , lowercase : int ) -> bool: __snake_case : List[str] = len(lowercase ) __snake_case : int = [[False] * (required_sum + 1) for _ in range(arr_len + 1 )] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 for i in range(arr_len + 1 ): __snake_case : Optional[Any] = True # sum is not zero and set is empty then false for i in range(1 , required_sum + 1 ): __snake_case : Union[str, Any] = False for i in range(1 , arr_len + 1 ): for j in range(1 , required_sum + 1 ): if arr[i - 1] > j: __snake_case : List[str] = subset[i - 1][j] if arr[i - 1] <= j: __snake_case : Union[str, Any] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] return subset[arr_len][required_sum] if __name__ == "__main__": import doctest doctest.testmod()
326
0
"""simple docstring""" import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.activations import gelu_new, gelu_python, get_activation @require_torch class UpperCAmelCase_ ( unittest.TestCase): def _UpperCAmelCase ( self ) -> Dict: lowercase__ : Optional[int] = torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] ) lowercase__ : List[Any] = get_activation('gelu' ) self.assertTrue(torch.allclose(gelu_python(a ) , torch_builtin(a ) ) ) self.assertFalse(torch.allclose(gelu_python(a ) , gelu_new(a ) ) ) def _UpperCAmelCase ( self ) -> Optional[Any]: lowercase__ : List[Any] = torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] ) lowercase__ : Any = get_activation('gelu' ) lowercase__ : Any = get_activation('gelu_10' ) lowercase__ : Dict = torch_builtin(a ) lowercase__ : int = geluaa(a ) lowercase__ : List[Any] = torch.where(y_gelu_aa < 10.0 , 1 , 0 ) self.assertTrue(torch.max(a ).item() == 10.0 ) self.assertTrue(torch.allclose(y_gelu * clipped_mask , y_gelu_aa * clipped_mask ) ) def _UpperCAmelCase ( self ) -> Any: get_activation('gelu' ) get_activation('gelu_10' ) get_activation('gelu_fast' ) get_activation('gelu_new' ) get_activation('gelu_python' ) get_activation('gelu_pytorch_tanh' ) get_activation('linear' ) get_activation('mish' ) get_activation('quick_gelu' ) get_activation('relu' ) get_activation('sigmoid' ) get_activation('silu' ) get_activation('swish' ) get_activation('tanh' ) with self.assertRaises(a ): get_activation('bogus' ) with self.assertRaises(a ): get_activation(a ) def _UpperCAmelCase ( self ) -> List[Any]: lowercase__ : Union[str, Any] = get_activation('gelu' ) lowercase__ : List[str] = 1 lowercase__ : Optional[Any] = get_activation('gelu' ) self.assertEqual(acta.a , 1 ) with self.assertRaises(a ): lowercase__ : Dict = acta.a
77
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node _UpperCamelCase = 4 _UpperCamelCase = 3 class _lowerCamelCase ( a ): """simple docstring""" pass def lowerCAmelCase__( lowercase : List[str] ) -> Any: for shard in shards: for i in range(lowercase ): yield {"i": i, "shard": shard} def lowerCAmelCase__( ) -> Optional[int]: __snake_case : List[Any] = int(os.environ["RANK"] ) __snake_case : Optional[int] = int(os.environ["WORLD_SIZE"] ) __snake_case : List[str] = ArgumentParser() parser.add_argument("--streaming" , type=lowercase ) parser.add_argument("--local_rank" , type=lowercase ) parser.add_argument("--num_workers" , type=lowercase , default=0 ) __snake_case : Any = parser.parse_args() __snake_case : Dict = args.streaming __snake_case : Union[str, Any] = args.num_workers __snake_case : Any = {"shards": [f"""shard_{shard_idx}""" for shard_idx in range(lowercase )]} __snake_case : Optional[int] = IterableDataset.from_generator(lowercase , gen_kwargs=lowercase ) if not streaming: __snake_case : Any = Dataset.from_list(list(lowercase ) ) __snake_case : Dict = split_dataset_by_node(lowercase , rank=lowercase , world_size=lowercase ) __snake_case : Union[str, Any] = torch.utils.data.DataLoader(lowercase , num_workers=lowercase ) __snake_case : Optional[int] = NUM_SHARDS * NUM_ITEMS_PER_SHARD __snake_case : List[str] = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) __snake_case : Dict = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f"""local_size {local_size} != expected_local_size {expected_local_size}""" ) if __name__ == "__main__": main()
326
0
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowerCamelCase_ ( UpperCAmelCase_ ): '''simple docstring''' a__ : Dict = ["image_processor", "tokenizer"] a__ : Optional[Any] = "ChineseCLIPImageProcessor" a__ : Tuple = ("BertTokenizer", "BertTokenizerFast") def __init__( self , __lowercase=None , __lowercase=None , **__lowercase) -> Optional[Any]: __UpperCamelCase :Dict = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __lowercase , ) __UpperCamelCase :Dict = kwargs.pop('''feature_extractor''') __UpperCamelCase :Optional[int] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''') if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''') super().__init__(__lowercase , __lowercase) __UpperCamelCase :List[str] = self.image_processor def __call__( self , __lowercase=None , __lowercase=None , __lowercase=None , **__lowercase) -> Union[str, Any]: if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''') if text is not None: __UpperCamelCase :Tuple = self.tokenizer(__lowercase , return_tensors=__lowercase , **__lowercase) if images is not None: __UpperCamelCase :Tuple = self.image_processor(__lowercase , return_tensors=__lowercase , **__lowercase) if text is not None and images is not None: __UpperCamelCase :str = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__lowercase) , tensor_type=__lowercase) def UpperCamelCase__ ( self , *__lowercase , **__lowercase) -> Union[str, Any]: return self.tokenizer.batch_decode(*__lowercase , **__lowercase) def UpperCamelCase__ ( self , *__lowercase , **__lowercase) -> Dict: return self.tokenizer.decode(*__lowercase , **__lowercase) @property def UpperCamelCase__ ( self) -> Optional[int]: __UpperCamelCase :Optional[int] = self.tokenizer.model_input_names __UpperCamelCase :Optional[Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names)) @property def UpperCamelCase__ ( self) -> str: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __lowercase , ) return self.image_processor_class
43
def lowerCAmelCase__( lowercase : int = 100_0000 ) -> int: __snake_case : List[Any] = limit + 1 __snake_case : List[str] = [0] * limit for first_term in range(1 , lowercase ): for n in range(lowercase , lowercase , lowercase ): __snake_case : Union[str, Any] = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a __snake_case : Tuple = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
326
0
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from .config import config_command_parser from .config_args import default_config_file, load_config_from_file # noqa: F401 from .default import default_command_parser from .update import update_command_parser def UpperCamelCase_( snake_case : List[str]=None ): '''simple docstring''' snake_case_ = argparse.ArgumentParser(add_help=snake_case , allow_abbrev=snake_case ) # The main config parser snake_case_ = config_command_parser(snake_case ) # The subparser to add commands to snake_case_ = config_parser.add_subparsers(title="subcommands" , dest="subcommand" ) # Then add other parsers with the parent parser default_command_parser(snake_case , parents=[parent_parser] ) update_command_parser(snake_case , parents=[parent_parser] ) return config_parser def UpperCamelCase_( ): '''simple docstring''' snake_case_ = get_config_parser() snake_case_ = config_parser.parse_args() if not hasattr(snake_case , "func" ): config_parser.print_help() exit(1 ) # Run args.func(snake_case ) if __name__ == "__main__": main()
85
from __future__ import annotations def lowerCAmelCase__( lowercase : str , lowercase : list[str] | None = None ) -> list[list[str]]: __snake_case : List[str] = word_bank or [] # create a table __snake_case : int = len(lowercase ) + 1 __snake_case : list[list[list[str]]] = [] for _ in range(lowercase ): table.append([] ) # seed value __snake_case : Optional[int] = [[]] # because empty string has empty combination # iterate through the indices for i in range(lowercase ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(lowercase )] == word: __snake_case : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(lowercase )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(lowercase )]: combination.reverse() return table[len(lowercase )] if __name__ == "__main__": print(all_construct('''jwajalapa''', ['''jwa''', '''j''', '''w''', '''a''', '''la''', '''lapa'''])) print(all_construct('''rajamati''', ['''s''', '''raj''', '''amat''', '''raja''', '''ma''', '''i''', '''t'''])) print( all_construct( '''hexagonosaurus''', ['''h''', '''ex''', '''hex''', '''ag''', '''ago''', '''ru''', '''auru''', '''rus''', '''go''', '''no''', '''o''', '''s'''], ) )
326
0
"""simple docstring""" import math def lowerCamelCase__ ( _lowerCamelCase : int ) -> str: lowerCamelCase_ = 0 lowerCamelCase_ = 0 while num > 0: lowerCamelCase_ = num % 8 lowerCamelCase_ = octal + (remainder * math.floor(math.pow(10 , _lowerCamelCase ) )) counter += 1 lowerCamelCase_ = math.floor(num / 8 ) # basically /= 8 without remainder if any # This formatting removes trailing '.0' from `octal`. return F'''0o{int(_lowerCamelCase )}''' def lowerCamelCase__ ( ) -> None: print('\n2 in octal is:' ) print(decimal_to_octal(2 ) ) # = 2 print('\n8 in octal is:' ) print(decimal_to_octal(8 ) ) # = 10 print('\n65 in octal is:' ) print(decimal_to_octal(65 ) ) # = 101 print('\n216 in octal is:' ) print(decimal_to_octal(216 ) ) # = 330 print('\n512 in octal is:' ) print(decimal_to_octal(512 ) ) # = 1000 print('\n' ) if __name__ == "__main__": main()
183
import unittest from transformers import BigBirdConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=2 , UpperCAmelCase=56 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=99 , UpperCAmelCase=32 , UpperCAmelCase=2 , UpperCAmelCase=2 , UpperCAmelCase=7 , UpperCAmelCase="gelu_new" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=512 , UpperCAmelCase=16 , UpperCAmelCase=2 , UpperCAmelCase=0.02 , UpperCAmelCase=4 , UpperCAmelCase="block_sparse" , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=2 , UpperCAmelCase=3 , ) -> Tuple: '''simple docstring''' __snake_case : Optional[int] = parent __snake_case : Tuple = batch_size __snake_case : List[str] = seq_length __snake_case : Optional[int] = is_training __snake_case : int = use_attention_mask __snake_case : Union[str, Any] = use_token_type_ids __snake_case : Any = use_labels __snake_case : List[str] = vocab_size __snake_case : int = hidden_size __snake_case : List[str] = num_hidden_layers __snake_case : List[Any] = num_attention_heads __snake_case : Optional[int] = intermediate_size __snake_case : Union[str, Any] = hidden_act __snake_case : Optional[int] = hidden_dropout_prob __snake_case : Optional[Any] = attention_probs_dropout_prob __snake_case : str = max_position_embeddings __snake_case : List[Any] = type_vocab_size __snake_case : int = type_sequence_label_size __snake_case : Dict = initializer_range __snake_case : List[Any] = num_choices __snake_case : Union[str, Any] = rescale_embeddings __snake_case : List[Any] = attention_type __snake_case : str = use_bias __snake_case : Dict = block_size __snake_case : Optional[Any] = num_random_blocks def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case : Any = None if self.use_attention_mask: __snake_case : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case : Union[str, Any] = None if self.use_token_type_ids: __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case : Optional[int] = BigBirdConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , attention_type=self.attention_type , block_size=self.block_size , num_random_blocks=self.num_random_blocks , use_bias=self.use_bias , rescale_embeddings=self.rescale_embeddings , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case : Optional[int] = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case : Dict = config_and_inputs __snake_case : int = { "input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask, } return config, inputs_dict @require_flax class _lowerCamelCase ( a , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) UpperCAmelCase_ : Dict =False UpperCAmelCase_ : str =False def UpperCAmelCase ( self ) -> str: '''simple docstring''' __snake_case : Dict = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Any: '''simple docstring''' super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_hidden_states_output() @slow def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case : Any = model_class_name.from_pretrained("google/bigbird-roberta-base" ) self.assertIsNotNone(UpperCAmelCase ) def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case , __snake_case : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __snake_case : Optional[Any] = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = model_class(UpperCAmelCase ) @jax.jit def model_jitted(UpperCAmelCase , UpperCAmelCase=None , **UpperCAmelCase ): return model(input_ids=UpperCAmelCase , attention_mask=UpperCAmelCase , **UpperCAmelCase ) with self.subTest("JIT Enabled" ): __snake_case : int = model_jitted(**UpperCAmelCase ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __snake_case : List[Any] = model_jitted(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) ) for jitted_output, output in zip(UpperCAmelCase , UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=1E-5 , UpperCAmelCase="outputs" , UpperCAmelCase=None ) -> int: '''simple docstring''' if name.startswith("outputs.attentions" ): return else: super().check_pt_flax_outputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase )
326
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase : Dict = { 'configuration_x_clip': [ 'XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XCLIPConfig', 'XCLIPTextConfig', 'XCLIPVisionConfig', ], 'processing_x_clip': ['XCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Tuple = [ 'XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'XCLIPModel', 'XCLIPPreTrainedModel', 'XCLIPTextModel', 'XCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys __lowerCAmelCase : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
88
import argparse import datetime def lowerCAmelCase__( lowercase : str ) -> str: __snake_case : int = { "0": "Sunday", "1": "Monday", "2": "Tuesday", "3": "Wednesday", "4": "Thursday", "5": "Friday", "6": "Saturday", } __snake_case : int = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(lowercase ) < 11: raise ValueError("Must be 10 characters long" ) # Get month __snake_case : int = int(date_input[0] + date_input[1] ) # Validate if not 0 < m < 13: raise ValueError("Month must be between 1 - 12" ) __snake_case : str = date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'" ) # Get day __snake_case : int = int(date_input[3] + date_input[4] ) # Validate if not 0 < d < 32: raise ValueError("Date must be between 1 - 31" ) # Get second separator __snake_case : str = date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError("Date separator must be '-' or '/'" ) # Get year __snake_case : int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9] ) # Arbitrary year range if not 45 < y < 8500: raise ValueError( "Year out of range. There has to be some sort of limit...right?" ) # Get datetime obj for validation __snake_case : str = datetime.date(int(lowercase ) , int(lowercase ) , int(lowercase ) ) # Start math if m <= 2: __snake_case : Optional[Any] = y - 1 __snake_case : Tuple = m + 12 # maths var __snake_case : int = int(str(lowercase )[:2] ) __snake_case : int = int(str(lowercase )[2:] ) __snake_case : int = int(2.6 * m - 5.3_9 ) __snake_case : int = int(c / 4 ) __snake_case : int = int(k / 4 ) __snake_case : int = int(d + k ) __snake_case : int = int(t + u + v + x ) __snake_case : int = int(z - (2 * c) ) __snake_case : int = round(w % 7 ) # End math # Validate math if f != convert_datetime_days[dt_ck.weekday()]: raise AssertionError("The date was evaluated incorrectly. Contact developer." ) # Response __snake_case : str = f"""Your date {date_input}, is a {days[str(lowercase )]}!""" return response if __name__ == "__main__": import doctest doctest.testmod() _UpperCamelCase = argparse.ArgumentParser( description=( '''Find out what day of the week nearly any date is or was. Enter ''' '''date as a string in the mm-dd-yyyy or mm/dd/yyyy format''' ) ) parser.add_argument( '''date_input''', type=str, help='''Date as a string (mm-dd-yyyy or mm/dd/yyyy)''' ) _UpperCamelCase = parser.parse_args() zeller(args.date_input)
326
0
_UpperCAmelCase : Optional[int] = "\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" _UpperCAmelCase : Any = [{"type": "code", "content": INSTALL_CONTENT}] _UpperCAmelCase : List[Any] = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
222
def lowerCAmelCase__( lowercase : List[Any] , lowercase : Optional[Any] , lowercase : Optional[int] , lowercase : str , lowercase : List[Any] , lowercase : List[str] ) -> int: if index == r: for j in range(lowercase ): print(data[j] , end=" " ) print(" " ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location __snake_case : Union[str, Any] = arr[i] combination_util(lowercase , lowercase , lowercase , index + 1 , lowercase , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(lowercase , lowercase , lowercase , lowercase , lowercase , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def lowerCAmelCase__( lowercase : Any , lowercase : Tuple , lowercase : Union[str, Any] ) -> Optional[Any]: # A temporary array to store all combination one by one __snake_case : Tuple = [0] * r # Print all combination using temporary array 'data[]' combination_util(lowercase , lowercase , lowercase , 0 , lowercase , 0 ) if __name__ == "__main__": # Driver code to check the function above _UpperCamelCase = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
326
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __snake_case = logging.get_logger(__name__) class lowercase__ ( _UpperCAmelCase ): A__ : Any =["pixel_values"] def __init__( self : Dict , UpperCAmelCase_ : Union[str, Any] = True , UpperCAmelCase_ : int = None , UpperCAmelCase_ : List[Any] = PILImageResampling.BILINEAR , UpperCAmelCase_ : Dict = True , UpperCAmelCase_ : List[Any] = None , UpperCAmelCase_ : Dict = True , UpperCAmelCase_ : int = 1 / 255 , UpperCAmelCase_ : Tuple = True , UpperCAmelCase_ : Union[str, Any] = None , UpperCAmelCase_ : Dict = None , **UpperCAmelCase_ : int , ): super().__init__(**UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = size if size is not None else {"shortest_edge": 256} SCREAMING_SNAKE_CASE__ = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = crop_size if crop_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__ = do_center_crop SCREAMING_SNAKE_CASE__ = crop_size SCREAMING_SNAKE_CASE__ = do_rescale SCREAMING_SNAKE_CASE__ = rescale_factor SCREAMING_SNAKE_CASE__ = do_normalize SCREAMING_SNAKE_CASE__ = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE__ = image_std if image_std is not None else IMAGENET_STANDARD_STD def A_ ( self : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Any] = None , **UpperCAmelCase_ : Union[str, Any] , ): SCREAMING_SNAKE_CASE__ = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_ ) if "shortest_edge" not in size: raise ValueError(F'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) SCREAMING_SNAKE_CASE__ = get_resize_output_image_size(UpperCAmelCase_ , size=size['shortest_edge'] , default_to_square=UpperCAmelCase_ ) return resize(UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[Any] = None , **UpperCAmelCase_ : str , ): SCREAMING_SNAKE_CASE__ = get_size_dict(UpperCAmelCase_ ) return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : List[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Any , UpperCAmelCase_ : str = None , **UpperCAmelCase_ : Optional[Any] ): return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : Optional[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict = None , **UpperCAmelCase_ : Any , ): return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str = None , UpperCAmelCase_ : int = None , UpperCAmelCase_ : List[str] = None , UpperCAmelCase_ : Tuple = None , UpperCAmelCase_ : Dict = None , UpperCAmelCase_ : Any = None , UpperCAmelCase_ : int = None , UpperCAmelCase_ : str = None , UpperCAmelCase_ : Optional[Any] = None , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Dict = None , UpperCAmelCase_ : Optional[Any] = ChannelDimension.FIRST , **UpperCAmelCase_ : int , ): 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_ , default_to_square=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = resample if resample is not None else self.resample 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_ ) SCREAMING_SNAKE_CASE__ = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE__ = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE__ = do_normalize if do_normalize is not None else self.do_normalize 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__ = 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.' ) if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # All transformations expect numpy arrays. 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_ )
176
import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = [ ('''bert.bert''', '''visual_bert'''), ('''bert.cls''', '''cls'''), ('''bert.classifier''', '''cls'''), ('''token_type_embeddings_visual''', '''visual_token_type_embeddings'''), ('''position_embeddings_visual''', '''visual_position_embeddings'''), ('''projection''', '''visual_projection'''), ] _UpperCamelCase = [ '''nlvr2_coco_pre_trained.th''', '''nlvr2_fine_tuned.th''', '''nlvr2_pre_trained.th''', '''vcr_coco_pre_train.th''', '''vcr_fine_tune.th''', '''vcr_pre_train.th''', '''vqa_coco_pre_trained.th''', '''vqa_fine_tuned.th''', '''vqa_pre_trained.th''', ] def lowerCAmelCase__( lowercase : str ) -> Optional[Any]: __snake_case : Optional[int] = torch.load(lowercase , map_location="cpu" ) return sd def lowerCAmelCase__( lowercase : List[Any] , lowercase : List[Any] , lowercase : List[Any]=rename_keys_prefix ) -> Dict: __snake_case : Tuple = OrderedDict() __snake_case : str = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue __snake_case : Optional[Any] = key for name_pair in rename_keys_prefix: __snake_case : List[str] = new_key.replace(name_pair[0] , name_pair[1] ) __snake_case : List[str] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately __snake_case : List[Any] = new_d["cls.predictions.bias"] return new_d @torch.no_grad() def lowerCAmelCase__( lowercase : Optional[Any] , lowercase : Any ) -> List[Any]: assert ( checkpoint_path.split("/" )[-1] in ACCEPTABLE_CHECKPOINTS ), f"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: __snake_case : Any = "pretraining" if "vcr" in checkpoint_path: __snake_case : Optional[Any] = {"visual_embedding_dim": 512} elif "vqa_advanced" in checkpoint_path: __snake_case : Tuple = {"visual_embedding_dim": 2048} elif "vqa" in checkpoint_path: __snake_case : Dict = {"visual_embedding_dim": 2048} elif "nlvr" in checkpoint_path: __snake_case : Any = {"visual_embedding_dim": 1024} else: raise NotImplementedError(f"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: __snake_case : Dict = {"visual_embedding_dim": 512} __snake_case : Any = "multichoice" elif "vqa_advanced" in checkpoint_path: __snake_case : List[Any] = {"visual_embedding_dim": 2048} __snake_case : Optional[Any] = "vqa_advanced" elif "vqa" in checkpoint_path: __snake_case : Union[str, Any] = {"visual_embedding_dim": 2048, "num_labels": 3129} __snake_case : Union[str, Any] = "vqa" elif "nlvr" in checkpoint_path: __snake_case : Tuple = { "visual_embedding_dim": 1024, "num_labels": 2, } __snake_case : List[Any] = "nlvr" __snake_case : Union[str, Any] = VisualBertConfig(**lowercase ) # Load State Dict __snake_case : Any = load_state_dict(lowercase ) __snake_case : Dict = get_new_dict(lowercase , lowercase ) if model_type == "pretraining": __snake_case : Optional[Any] = VisualBertForPreTraining(lowercase ) elif model_type == "vqa": __snake_case : Tuple = VisualBertForQuestionAnswering(lowercase ) elif model_type == "nlvr": __snake_case : Tuple = VisualBertForVisualReasoning(lowercase ) elif model_type == "multichoice": __snake_case : List[Any] = VisualBertForMultipleChoice(lowercase ) model.load_state_dict(lowercase ) # Save Checkpoints Path(lowercase ).mkdir(exist_ok=lowercase ) model.save_pretrained(lowercase ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''orig_checkpoint_path''', type=str, help='''A path to .th on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', type=str, help='''Path to the output PyTorch model.''') _UpperCamelCase = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
326
0
"""simple docstring""" from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class UpperCAmelCase_ ( _lowercase , _lowercase , unittest.TestCase): snake_case__ = ( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) snake_case__ = ( { "feature-extraction": TFMobileBertModel, "fill-mask": TFMobileBertForMaskedLM, "question-answering": TFMobileBertForQuestionAnswering, "text-classification": TFMobileBertForSequenceClassification, "token-classification": TFMobileBertForTokenClassification, "zero-shot": TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) snake_case__ = False snake_case__ = False def _UpperCamelCase ( self : int , __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] , __UpperCamelCase : List[Any]=False ) -> Any: _UpperCamelCase = super()._prepare_for_class(__UpperCamelCase , __UpperCamelCase , return_labels=__UpperCamelCase ) if return_labels: if model_class in get_values(__UpperCamelCase ): _UpperCamelCase = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) return inputs_dict class UpperCAmelCase_ ( _lowercase): def __init__( self : int , __UpperCamelCase : int , __UpperCamelCase : int=13 , __UpperCamelCase : Optional[Any]=7 , __UpperCamelCase : str=True , __UpperCamelCase : Optional[int]=True , __UpperCamelCase : Any=True , __UpperCamelCase : Tuple=True , __UpperCamelCase : Optional[int]=99 , __UpperCamelCase : Optional[int]=32 , __UpperCamelCase : Dict=32 , __UpperCamelCase : Any=2 , __UpperCamelCase : Optional[int]=4 , __UpperCamelCase : Optional[Any]=37 , __UpperCamelCase : Optional[Any]="gelu" , __UpperCamelCase : str=0.1 , __UpperCamelCase : str=0.1 , __UpperCamelCase : List[str]=512 , __UpperCamelCase : str=16 , __UpperCamelCase : str=2 , __UpperCamelCase : int=0.0_2 , __UpperCamelCase : Any=3 , __UpperCamelCase : List[str]=4 , __UpperCamelCase : List[Any]=None , ) -> Optional[Any]: _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = seq_length _UpperCamelCase = is_training _UpperCamelCase = use_input_mask _UpperCamelCase = use_token_type_ids _UpperCamelCase = use_labels _UpperCamelCase = vocab_size _UpperCamelCase = hidden_size _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = intermediate_size _UpperCamelCase = hidden_act _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = max_position_embeddings _UpperCamelCase = type_vocab_size _UpperCamelCase = type_sequence_label_size _UpperCamelCase = initializer_range _UpperCamelCase = num_labels _UpperCamelCase = num_choices _UpperCamelCase = scope _UpperCamelCase = embedding_size def _UpperCamelCase ( self : Union[str, Any] ) -> Any: _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCamelCase = None if self.use_input_mask: _UpperCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCamelCase = None if self.use_token_type_ids: _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None if self.use_labels: _UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCamelCase = ids_tensor([self.batch_size] , self.num_choices ) _UpperCamelCase = MobileBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , embedding_size=self.embedding_size , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _UpperCamelCase ( self : Dict , __UpperCamelCase : List[str] , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : str , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Optional[int] , __UpperCamelCase : int , __UpperCamelCase : int ) -> List[Any]: _UpperCamelCase = TFMobileBertModel(config=__UpperCamelCase ) _UpperCamelCase = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _UpperCamelCase = model(__UpperCamelCase ) _UpperCamelCase = [input_ids, input_mask] _UpperCamelCase = model(__UpperCamelCase ) _UpperCamelCase = 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 _UpperCamelCase ( self : List[str] , __UpperCamelCase : Tuple , __UpperCamelCase : List[Any] , __UpperCamelCase : Optional[int] , __UpperCamelCase : str , __UpperCamelCase : Optional[int] , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: _UpperCamelCase = TFMobileBertForMaskedLM(config=__UpperCamelCase ) _UpperCamelCase = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _UpperCamelCase = model(__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCamelCase ( self : Tuple , __UpperCamelCase : Dict , __UpperCamelCase : Optional[int] , __UpperCamelCase : str , __UpperCamelCase : Any , __UpperCamelCase : Optional[int] , __UpperCamelCase : Optional[Any] , __UpperCamelCase : Dict ) -> Union[str, Any]: _UpperCamelCase = TFMobileBertForNextSentencePrediction(config=__UpperCamelCase ) _UpperCamelCase = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _UpperCamelCase = model(__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def _UpperCamelCase ( self : str , __UpperCamelCase : List[Any] , __UpperCamelCase : Optional[Any] , __UpperCamelCase : List[Any] , __UpperCamelCase : List[Any] , __UpperCamelCase : List[Any] , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : str ) -> Any: _UpperCamelCase = TFMobileBertForPreTraining(config=__UpperCamelCase ) _UpperCamelCase = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _UpperCamelCase = model(__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 _UpperCamelCase ( self : str , __UpperCamelCase : List[str] , __UpperCamelCase : Any , __UpperCamelCase : Tuple , __UpperCamelCase : Dict , __UpperCamelCase : Tuple , __UpperCamelCase : List[Any] , __UpperCamelCase : Dict ) -> Union[str, Any]: _UpperCamelCase = self.num_labels _UpperCamelCase = TFMobileBertForSequenceClassification(config=__UpperCamelCase ) _UpperCamelCase = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _UpperCamelCase = model(__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCamelCase ( self : Optional[int] , __UpperCamelCase : Optional[Any] , __UpperCamelCase : Any , __UpperCamelCase : str , __UpperCamelCase : int , __UpperCamelCase : str , __UpperCamelCase : List[Any] , __UpperCamelCase : Union[str, Any] ) -> Tuple: _UpperCamelCase = self.num_choices _UpperCamelCase = TFMobileBertForMultipleChoice(config=__UpperCamelCase ) _UpperCamelCase = tf.tile(tf.expand_dims(__UpperCamelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCamelCase = tf.tile(tf.expand_dims(__UpperCamelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCamelCase = tf.tile(tf.expand_dims(__UpperCamelCase , 1 ) , (1, self.num_choices, 1) ) _UpperCamelCase = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } _UpperCamelCase = model(__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _UpperCamelCase ( self : Dict , __UpperCamelCase : List[str] , __UpperCamelCase : Dict , __UpperCamelCase : Optional[Any] , __UpperCamelCase : int , __UpperCamelCase : Dict , __UpperCamelCase : Tuple , __UpperCamelCase : Optional[int] ) -> Dict: _UpperCamelCase = self.num_labels _UpperCamelCase = TFMobileBertForTokenClassification(config=__UpperCamelCase ) _UpperCamelCase = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _UpperCamelCase = model(__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _UpperCamelCase ( self : Tuple , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : List[str] , __UpperCamelCase : int , __UpperCamelCase : Any , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Dict , __UpperCamelCase : Union[str, Any] ) -> List[Any]: _UpperCamelCase = TFMobileBertForQuestionAnswering(config=__UpperCamelCase ) _UpperCamelCase = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} _UpperCamelCase = model(__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 _UpperCamelCase ( self : int ) -> Any: _UpperCamelCase = self.prepare_config_and_inputs() ( _UpperCamelCase ) = config_and_inputs _UpperCamelCase = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict def _UpperCamelCase ( self : Tuple ) -> Any: _UpperCamelCase = TFMobileBertModelTest.TFMobileBertModelTester(self ) _UpperCamelCase = ConfigTester(self , config_class=__UpperCamelCase , hidden_size=37 ) def _UpperCamelCase ( self : str ) -> Optional[int]: self.config_tester.run_common_tests() def _UpperCamelCase ( self : List[Any] ) -> Union[str, Any]: _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*__UpperCamelCase ) def _UpperCamelCase ( self : Any ) -> Optional[Any]: _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*__UpperCamelCase ) def _UpperCamelCase ( self : Optional[int] ) -> int: _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__UpperCamelCase ) def _UpperCamelCase ( self : Dict ) -> Any: _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__UpperCamelCase ) def _UpperCamelCase ( self : int ) -> Optional[Any]: _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*__UpperCamelCase ) def _UpperCamelCase ( self : Dict ) -> List[str]: _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*__UpperCamelCase ) def _UpperCamelCase ( self : Dict ) -> int: _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__UpperCamelCase ) def _UpperCamelCase ( self : str ) -> Optional[Any]: _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*__UpperCamelCase ) @slow def _UpperCamelCase ( self : Any ) -> Optional[int]: for model_name in ["google/mobilebert-uncased"]: _UpperCamelCase = TFMobileBertModel.from_pretrained(__UpperCamelCase ) self.assertIsNotNone(__UpperCamelCase ) @require_tf class UpperCAmelCase_ ( unittest.TestCase): @slow def _UpperCamelCase ( self : int ) -> List[Any]: _UpperCamelCase = TFMobileBertForPreTraining.from_pretrained('''google/mobilebert-uncased''' ) _UpperCamelCase = tf.constant([[0, 1, 2, 3, 4, 5]] ) _UpperCamelCase = model(__UpperCamelCase )[0] _UpperCamelCase = [1, 6, 3_0522] self.assertEqual(output.shape , __UpperCamelCase ) _UpperCamelCase = tf.constant( [ [ [-4.5_9_1_9_5_4_7, -9.2_4_8_2_9_5, -9.6_4_5_2_5_6], [-6.7_3_0_6_1_7_5, -6.4_4_0_2_8_4, -6.6_0_5_2_8_3_7], [-7.2_7_4_3_5_0_6, -6.7_8_4_7_9_1_5, -6.0_2_4_6_7_3], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __UpperCamelCase , atol=1E-4 )
256
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowerCAmelCase__( lowercase : Optional[int] , lowercase : Any , lowercase : Dict , lowercase : List[str] , lowercase : List[Any] ) -> Tuple: # Load configuration defined in the metadata file with open(lowercase ) as metadata_file: __snake_case : int = json.load(lowercase ) __snake_case : Optional[int] = LukeConfig(use_entity_aware_attention=lowercase , **metadata["model_config"] ) # Load in the weights from the checkpoint_path __snake_case : List[Any] = torch.load(lowercase , map_location="cpu" )["module"] # Load the entity vocab file __snake_case : Tuple = load_original_entity_vocab(lowercase ) # add an entry for [MASK2] __snake_case : Optional[int] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 __snake_case : Union[str, Any] = XLMRobertaTokenizer.from_pretrained(metadata["model_config"]["bert_model_name"] ) # Add special tokens to the token vocabulary for downstream tasks __snake_case : Optional[int] = AddedToken("<ent>" , lstrip=lowercase , rstrip=lowercase ) __snake_case : Any = AddedToken("<ent2>" , lstrip=lowercase , rstrip=lowercase ) tokenizer.add_special_tokens({"additional_special_tokens": [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(f"""Saving tokenizer to {pytorch_dump_folder_path}""" ) tokenizer.save_pretrained(lowercase ) with open(os.path.join(lowercase , "tokenizer_config.json" ) , "r" ) as f: __snake_case : Tuple = json.load(lowercase ) __snake_case : List[Any] = "MLukeTokenizer" with open(os.path.join(lowercase , "tokenizer_config.json" ) , "w" ) as f: json.dump(lowercase , lowercase ) with open(os.path.join(lowercase , MLukeTokenizer.vocab_files_names["entity_vocab_file"] ) , "w" ) as f: json.dump(lowercase , lowercase ) __snake_case : Any = MLukeTokenizer.from_pretrained(lowercase ) # Initialize the embeddings of the special tokens __snake_case : str = tokenizer.convert_tokens_to_ids(["@"] )[0] __snake_case : List[str] = tokenizer.convert_tokens_to_ids(["#"] )[0] __snake_case : List[Any] = state_dict["embeddings.word_embeddings.weight"] __snake_case : Union[str, Any] = word_emb[ent_init_index].unsqueeze(0 ) __snake_case : Union[str, Any] = word_emb[enta_init_index].unsqueeze(0 ) __snake_case : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: __snake_case : List[Any] = state_dict[bias_name] __snake_case : Optional[int] = decoder_bias[ent_init_index].unsqueeze(0 ) __snake_case : int = decoder_bias[enta_init_index].unsqueeze(0 ) __snake_case : Any = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: __snake_case : Dict = f"""encoder.layer.{layer_index}.attention.self.""" __snake_case : Union[str, Any] = state_dict[prefix + matrix_name] __snake_case : str = state_dict[prefix + matrix_name] __snake_case : Union[str, Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks __snake_case : Any = state_dict["entity_embeddings.entity_embeddings.weight"] __snake_case : List[str] = entity_emb[entity_vocab["[MASK]"]].unsqueeze(0 ) __snake_case : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' __snake_case : List[Any] = state_dict["entity_predictions.bias"] __snake_case : List[Any] = entity_prediction_bias[entity_vocab["[MASK]"]].unsqueeze(0 ) __snake_case : Union[str, Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) __snake_case : Any = LukeForMaskedLM(config=lowercase ).eval() state_dict.pop("entity_predictions.decoder.weight" ) state_dict.pop("lm_head.decoder.weight" ) state_dict.pop("lm_head.decoder.bias" ) __snake_case : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith("lm_head" ) or key.startswith("entity_predictions" )): __snake_case : str = state_dict[key] else: __snake_case : str = state_dict[key] __snake_case , __snake_case : Union[str, Any] = model.load_state_dict(lowercase , strict=lowercase ) if set(lowercase ) != {"luke.embeddings.position_ids"}: raise ValueError(f"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(lowercase ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(f"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs __snake_case : int = MLukeTokenizer.from_pretrained(lowercase , task="entity_classification" ) __snake_case : Tuple = "ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan)." __snake_case : Union[str, Any] = (0, 9) __snake_case : Optional[int] = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) __snake_case : Any = model(**lowercase ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base __snake_case : Optional[Any] = torch.Size((1, 33, 768) ) __snake_case : Optional[int] = torch.tensor([[0.0_8_9_2, 0.0_5_9_6, -0.2_8_1_9], [0.0_1_3_4, 0.1_1_9_9, 0.0_5_7_3], [-0.0_1_6_9, 0.0_9_2_7, 0.0_6_4_4]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowercase , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base __snake_case : str = torch.Size((1, 1, 768) ) __snake_case : int = torch.tensor([[-0.1_4_8_2, 0.0_6_0_9, 0.0_3_2_2]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" f""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , lowercase , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction __snake_case : str = MLukeTokenizer.from_pretrained(lowercase ) __snake_case : Dict = "Tokyo is the capital of <mask>." __snake_case : Union[str, Any] = (24, 30) __snake_case : int = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) __snake_case : int = model(**lowercase ) __snake_case : Dict = encoding["input_ids"][0].tolist() __snake_case : Dict = input_ids.index(tokenizer.convert_tokens_to_ids("<mask>" ) ) __snake_case : Optional[int] = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(lowercase ) __snake_case : Optional[Any] = outputs.entity_logits[0][0].argmax().item() __snake_case : Optional[int] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith("en:" )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print("Saving PyTorch model to {}".format(lowercase ) ) model.save_pretrained(lowercase ) def lowerCAmelCase__( lowercase : Optional[int] ) -> List[Any]: __snake_case : Any = ["[MASK]", "[PAD]", "[UNK]"] __snake_case : Any = [json.loads(lowercase ) for line in open(lowercase )] __snake_case : Any = {} for entry in data: __snake_case : Any = entry["id"] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: __snake_case : Optional[int] = entity_id break __snake_case : Union[str, Any] = f"""{language}:{entity_name}""" __snake_case : Any = entity_id return new_mapping if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) _UpperCamelCase = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
326
0
import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Dict = VideoToVideoSDPipeline _UpperCAmelCase : Dict = TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({'''video'''} ) - {"image", "width", "height"} _UpperCAmelCase : Dict = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'''video'''} ) - {"image"} _UpperCAmelCase : Tuple = PipelineTesterMixin.required_optional_params - {"latents"} _UpperCAmelCase : List[Any] = False # No `output_type`. _UpperCAmelCase : Optional[int] = frozenset( [ '''num_inference_steps''', '''generator''', '''latents''', '''return_dict''', '''callback''', '''callback_steps''', ] ) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: List[Any] = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D") , up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D") , cross_attention_dim=32 , attention_head_dim=4 , ) SCREAMING_SNAKE_CASE_: List[Any] = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule="scaled_linear" , clip_sample=lowerCAmelCase__ , set_alpha_to_one=lowerCAmelCase__ , ) torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Tuple = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Any = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="gelu" , projection_dim=512 , ) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTextModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") SCREAMING_SNAKE_CASE_: Optional[Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any]=0): SCREAMING_SNAKE_CASE_: List[Any] = floats_tensor((1, 3, 3, 32, 32) , rng=random.Random(lowerCAmelCase__)).to(lowerCAmelCase__) if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: int = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: List[Any] = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = { "prompt": "A painting of a squirrel eating a burger", "video": video, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "pt", } return inputs def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: int = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: Optional[int] = self.get_dummy_components() SCREAMING_SNAKE_CASE_: List[str] = VideoToVideoSDPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = "np" SCREAMING_SNAKE_CASE_: Dict = sd_pipe(**lowerCAmelCase__).frames SCREAMING_SNAKE_CASE_: Optional[int] = frames[0][-3:, -3:, -1] assert frames[0].shape == (32, 32, 3) SCREAMING_SNAKE_CASE_: Tuple = np.array([106, 117, 113, 174, 137, 112, 148, 151, 131]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def _SCREAMING_SNAKE_CASE ( self : int): self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowerCAmelCase__ , expected_max_diff=5E-3) @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline.") def _SCREAMING_SNAKE_CASE ( self : int): pass @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline.") def _SCREAMING_SNAKE_CASE ( self : List[Any]): pass @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline.") def _SCREAMING_SNAKE_CASE ( self : List[Any]): pass def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return super().test_progress_bar() @slow @skip_mps class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: List[Any] = VideoToVideoSDPipeline.from_pretrained("cerspense/zeroscope_v2_XL" , torch_dtype=torch.floataa) pipe.enable_model_cpu_offload() # 10 frames SCREAMING_SNAKE_CASE_: str = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: str = torch.randn((1, 10, 3, 1024, 576) , generator=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = video.to("cuda") SCREAMING_SNAKE_CASE_: Optional[Any] = "Spiderman is surfing" SCREAMING_SNAKE_CASE_: Union[str, Any] = pipe(lowerCAmelCase__ , video=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=3 , output_type="pt").frames SCREAMING_SNAKE_CASE_: str = np.array([-1.045_8984, -1.127_9297, -0.966_3086, -0.9150_3906, -0.7509_7656]) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array).sum() < 1E-2
13
from maths.prime_factors import prime_factors def lowerCAmelCase__( lowercase : int ) -> int: if not isinstance(lowercase , lowercase ): __snake_case : Optional[int] = f"""Input value of [number={number}] must be an integer""" raise TypeError(lowercase ) if number < 1: raise ValueError("Input must be a positive integer" ) return -1 if len(prime_factors(lowercase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
326
0
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 ViTImageProcessor class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): def __init__( self : str , _A : List[Any] , _A : int=13 , _A : str=3 , _A : Optional[Any]=224 , _A : str=30 , _A : int=400 , _A : str=True , _A : List[str]=None , _A : Tuple=True , _A : List[str]=[0.5, 0.5, 0.5] , _A : Any=[0.5, 0.5, 0.5] , ) -> Any: """simple docstring""" snake_case_ : int = size if size is not None else {'height': 18, 'width': 18} snake_case_ : Tuple = parent snake_case_ : int = batch_size snake_case_ : Any = num_channels snake_case_ : Any = image_size snake_case_ : List[str] = min_resolution snake_case_ : List[str] = max_resolution snake_case_ : int = do_resize snake_case_ : Dict = size snake_case_ : Dict = do_normalize snake_case_ : Optional[int] = image_mean snake_case_ : Dict = image_std def UpperCAmelCase_ ( self : List[str] ) -> List[str]: """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 SCREAMING_SNAKE_CASE_ ( snake_case_ , unittest.TestCase ): __magic_name__: Any = ViTImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self : List[Any] ) -> List[str]: """simple docstring""" snake_case_ : Optional[int] = EfficientFormerImageProcessorTester(self ) @property def UpperCAmelCase_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" return self.image_proc_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self : int ) -> Any: """simple docstring""" snake_case_ : Dict = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_A , 'image_mean' ) ) self.assertTrue(hasattr(_A , 'image_std' ) ) self.assertTrue(hasattr(_A , 'do_normalize' ) ) self.assertTrue(hasattr(_A , 'do_resize' ) ) self.assertTrue(hasattr(_A , 'size' ) ) def UpperCAmelCase_ ( self : str ) -> int: """simple docstring""" pass def UpperCAmelCase_ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" snake_case_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ : Dict = prepare_image_inputs(self.image_proc_tester , equal_resolution=_A ) for image in image_inputs: self.assertIsInstance(_A , Image.Image ) # Test not batched input snake_case_ : Dict = image_processor(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) # Test batched snake_case_ : Optional[int] = image_processor(_A , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) def UpperCAmelCase_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" snake_case_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ : Optional[Any] = prepare_image_inputs(self.image_proc_tester , equal_resolution=_A , numpify=_A ) for image in image_inputs: self.assertIsInstance(_A , np.ndarray ) # Test not batched input snake_case_ : List[Any] = image_processor(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) # Test batched snake_case_ : str = image_processor(_A , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) def UpperCAmelCase_ ( self : Tuple ) -> Union[str, Any]: """simple docstring""" snake_case_ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ : Dict = prepare_image_inputs(self.image_proc_tester , equal_resolution=_A , torchify=_A ) for image in image_inputs: self.assertIsInstance(_A , torch.Tensor ) # Test not batched input snake_case_ : str = image_processor(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , ) # Test batched snake_case_ : List[str] = image_processor(_A , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['height'], self.image_proc_tester.size['width'], ) , )
327
from itertools import permutations def SCREAMING_SNAKE_CASE__ ( __a ): if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False snake_case_ : Any = [7, 11, 13, 17] for i, test in enumerate(__a ): if (num[i + 4] * 1_00 + num[i + 5] * 10 + num[i + 6]) % test != 0: return False return True def SCREAMING_SNAKE_CASE__ ( __a = 10 ): return sum( int(''.join(map(__a , __a ) ) ) for num in permutations(range(__a ) ) if is_substring_divisible(__a ) ) if __name__ == "__main__": print(F'''{solution() = }''')
327
1
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} _SCREAMING_SNAKE_CASE = { """vocab_file""": { """facebook/dpr-ctx_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-ctx_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-ctx_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-ctx_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json""" ), }, } _SCREAMING_SNAKE_CASE = { """vocab_file""": { """facebook/dpr-question_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-question_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-question_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-question_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json""" ), }, } _SCREAMING_SNAKE_CASE = { """vocab_file""": { """facebook/dpr-reader-single-nq-base""": ( """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-reader-multiset-base""": ( """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-reader-single-nq-base""": ( """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-reader-multiset-base""": ( """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json""" ), }, } _SCREAMING_SNAKE_CASE = { """facebook/dpr-ctx_encoder-single-nq-base""": 5_12, """facebook/dpr-ctx_encoder-multiset-base""": 5_12, } _SCREAMING_SNAKE_CASE = { """facebook/dpr-question_encoder-single-nq-base""": 5_12, """facebook/dpr-question_encoder-multiset-base""": 5_12, } _SCREAMING_SNAKE_CASE = { """facebook/dpr-reader-single-nq-base""": 5_12, """facebook/dpr-reader-multiset-base""": 5_12, } _SCREAMING_SNAKE_CASE = { """facebook/dpr-ctx_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-ctx_encoder-multiset-base""": {"""do_lower_case""": True}, } _SCREAMING_SNAKE_CASE = { """facebook/dpr-question_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-question_encoder-multiset-base""": {"""do_lower_case""": True}, } _SCREAMING_SNAKE_CASE = { """facebook/dpr-reader-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-reader-multiset-base""": {"""do_lower_case""": True}, } class SCREAMING_SNAKE_CASE_ ( snake_case_ ): __magic_name__: int = VOCAB_FILES_NAMES __magic_name__: int = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP __magic_name__: List[str] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __magic_name__: int = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class SCREAMING_SNAKE_CASE_ ( snake_case_ ): __magic_name__: Any = VOCAB_FILES_NAMES __magic_name__: List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP __magic_name__: Optional[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __magic_name__: Union[str, Any] = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION _SCREAMING_SNAKE_CASE = collections.namedtuple( """DPRSpanPrediction""", ["""span_score""", """relevance_score""", """doc_id""", """start_index""", """end_index""", """text"""] ) _SCREAMING_SNAKE_CASE = collections.namedtuple("""DPRReaderOutput""", ["""start_logits""", """end_logits""", """relevance_logits"""]) _SCREAMING_SNAKE_CASE = R""" Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: ``` [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> ``` Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `'tf'`: Return TensorFlow `tf.constant` objects. - `'pt'`: Return PyTorch `torch.Tensor` objects. - `'np'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer's default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Returns: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. """ @add_start_docstrings(snake_case_ ) class SCREAMING_SNAKE_CASE_ : def __call__( self : Union[str, Any] , _A : Optional[int] , _A : Optional[str] = None , _A : Optional[str] = None , _A : Union[bool, str] = False , _A : Union[bool, str] = False , _A : Optional[int] = None , _A : Optional[Union[str, TensorType]] = None , _A : Optional[bool] = None , **_A : Dict , ) -> BatchEncoding: """simple docstring""" if titles is None and texts is None: return super().__call__( _A , padding=_A , truncation=_A , max_length=_A , return_tensors=_A , return_attention_mask=_A , **_A , ) elif titles is None or texts is None: snake_case_ : Optional[Any] = titles if texts is None else texts return super().__call__( _A , _A , padding=_A , truncation=_A , max_length=_A , return_tensors=_A , return_attention_mask=_A , **_A , ) snake_case_ : Any = titles if not isinstance(_A , _A ) else [titles] snake_case_ : Tuple = texts if not isinstance(_A , _A ) else [texts] snake_case_ : Tuple = len(_A ) snake_case_ : Optional[Any] = questions if not isinstance(_A , _A ) else [questions] * n_passages if len(_A ) != len(_A ): raise ValueError( F"""There should be as many titles than texts but got {len(_A )} titles and {len(_A )} texts.""" ) snake_case_ : int = super().__call__(_A , _A , padding=_A , truncation=_A )['input_ids'] snake_case_ : Optional[int] = super().__call__(_A , add_special_tokens=_A , padding=_A , truncation=_A )['input_ids'] snake_case_ : Optional[int] = { 'input_ids': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(_A , _A ) ] } if return_attention_mask is not False: snake_case_ : Tuple = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) snake_case_ : int = attention_mask return self.pad(_A , padding=_A , max_length=_A , return_tensors=_A ) def UpperCAmelCase_ ( self : Optional[int] , _A : BatchEncoding , _A : DPRReaderOutput , _A : int = 16 , _A : int = 64 , _A : int = 4 , ) -> List[DPRSpanPrediction]: """simple docstring""" snake_case_ : Any = reader_input['input_ids'] snake_case_ ,snake_case_ ,snake_case_ : Optional[Any] = reader_output[:3] snake_case_ : Dict = len(_A ) snake_case_ : str = sorted(range(_A ) , reverse=_A , key=relevance_logits.__getitem__ ) snake_case_ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: snake_case_ : Union[str, Any] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence snake_case_ : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: snake_case_ : Tuple = sequence_ids.index(self.pad_token_id ) else: snake_case_ : Tuple = len(_A ) snake_case_ : Optional[int] = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=_A , top_spans=_A , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=_A , start_index=_A , end_index=_A , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(_A ) >= num_spans: break return nbest_spans_predictions[:num_spans] def UpperCAmelCase_ ( self : Dict , _A : List[int] , _A : List[int] , _A : int , _A : int , ) -> List[DPRSpanPrediction]: """simple docstring""" snake_case_ : Dict = [] for start_index, start_score in enumerate(_A ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) snake_case_ : List[str] = sorted(_A , key=lambda _A : x[1] , reverse=_A ) snake_case_ : List[Any] = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(F"""Wrong span indices: [{start_index}:{end_index}]""" ) snake_case_ : Tuple = end_index - start_index + 1 if length > max_answer_length: raise ValueError(F"""Span is too long: {length} > {max_answer_length}""" ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(_A ) == top_spans: break return chosen_span_intervals @add_end_docstrings(snake_case_ ) class SCREAMING_SNAKE_CASE_ ( snake_case_ , snake_case_ ): __magic_name__: Optional[int] = VOCAB_FILES_NAMES __magic_name__: List[Any] = READER_PRETRAINED_VOCAB_FILES_MAP __magic_name__: Optional[Any] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __magic_name__: Optional[Any] = READER_PRETRAINED_INIT_CONFIGURATION __magic_name__: List[str] = ["input_ids", "attention_mask"]
327
from __future__ import annotations from collections import namedtuple def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): snake_case_ : Any = namedtuple('result' , 'name value' ) if (voltage, current, power).count(0 ) != 1: raise ValueError('Only one argument must be 0' ) elif power < 0: raise ValueError( 'Power cannot be negative in any electrical/electronics system' ) elif voltage == 0: return result('voltage' , power / current ) elif current == 0: return result('current' , power / voltage ) elif power == 0: return result('power' , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError('Exactly one argument must be 0' ) if __name__ == "__main__": import doctest doctest.testmod()
327
1
import os import re import shutil import sys import tempfile import unittest import black _SCREAMING_SNAKE_CASE = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, """utils""")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. _SCREAMING_SNAKE_CASE = """ \"\"\" Output class for the scheduler's step function output. Args: prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the denoising loop. pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): The predicted denoised sample (x_{0}) based on the model output from the current timestep. `pred_original_sample` can be used to preview progress or for guidance. \"\"\" prev_sample: torch.FloatTensor pred_original_sample: Optional[torch.FloatTensor] = None """ class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): def UpperCAmelCase_ ( self : Any ) -> Any: """simple docstring""" snake_case_ : Any = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , 'schedulers/' ) ) snake_case_ : Any = self.diffusers_dir shutil.copy( os.path.join(_A , 'src/diffusers/schedulers/scheduling_ddpm.py' ) , os.path.join(self.diffusers_dir , 'schedulers/scheduling_ddpm.py' ) , ) def UpperCAmelCase_ ( self : List[str] ) -> List[str]: """simple docstring""" snake_case_ : List[str] = 'src/diffusers' shutil.rmtree(self.diffusers_dir ) def UpperCAmelCase_ ( self : Optional[int] , _A : Union[str, Any] , _A : Dict , _A : Dict , _A : List[str]=None ) -> Optional[int]: """simple docstring""" snake_case_ : List[Any] = comment + F"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: snake_case_ : int = comment + F"""\nclass {class_name}(nn.Module):\n""" + overwrite_result snake_case_ : Dict = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) snake_case_ : Tuple = black.format_str(_A , mode=_A ) snake_case_ : Union[str, Any] = os.path.join(self.diffusers_dir , 'new_code.py' ) with open(_A , 'w' , newline='\n' ) as f: f.write(_A ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_A ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_A ) with open(_A , 'r' ) as f: self.assertTrue(f.read() , _A ) def UpperCAmelCase_ ( self : Any ) -> str: """simple docstring""" snake_case_ : List[str] = check_copies.find_code_in_diffusers('schedulers.scheduling_ddpm.DDPMSchedulerOutput' ) self.assertEqual(_A , _A ) def UpperCAmelCase_ ( self : Dict ) -> str: """simple docstring""" self.check_copy_consistency( '# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput' , 'DDPMSchedulerOutput' , REFERENCE_CODE + '\n' , ) # With no empty line at the end self.check_copy_consistency( '# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput' , 'DDPMSchedulerOutput' , _A , ) # Copy consistency with rename self.check_copy_consistency( '# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test' , 'TestSchedulerOutput' , re.sub('DDPM' , 'Test' , _A ) , ) # Copy consistency with a really long name snake_case_ : int = 'TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason' self.check_copy_consistency( F"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , F"""{long_class_name}SchedulerOutput""" , re.sub('Bert' , _A , _A ) , ) # Copy consistency with overwrite self.check_copy_consistency( '# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test' , 'TestSchedulerOutput' , _A , overwrite_result=re.sub('DDPM' , 'Test' , _A ) , )
327
import re import string import numpy as np import datasets _SCREAMING_SNAKE_CASE = """ Returns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list. """ _SCREAMING_SNAKE_CASE = """ Args: predictions: List of predicted texts. references: List of reference texts. regexes_to_ignore: List, defaults to None. Regex expressions of characters to ignore when calculating the exact matches. Note: these regexes are removed from the input data before the changes based on the options below (e.g. ignore_case, ignore_punctuation, ignore_numbers) are applied. ignore_case: Boolean, defaults to False. If true, turns everything to lowercase so that capitalization differences are ignored. ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before comparing predictions and references. ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before comparing predictions and references. Returns: exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive. Examples: >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"] >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"] >>> results = exact_match.compute(references=refs, predictions=preds) >>> print(round(results[\"exact_match\"], 1)) 25.0 >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"] >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"] >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True) >>> print(round(results[\"exact_match\"], 1)) 50.0 >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"] >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"] >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True) >>> print(round(results[\"exact_match\"], 1)) 75.0 >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"] >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"] >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True) >>> print(round(results[\"exact_match\"], 1)) 100.0 >>> exact_match = datasets.load_metric(\"exact_match\") >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"] >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"] >>> results = exact_match.compute(references=refs, predictions=preds) >>> print(round(results[\"exact_match\"], 1)) 33.3 """ _SCREAMING_SNAKE_CASE = """ """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE_ ( datasets.Metric ): def UpperCAmelCase_ ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Value('string' , id='sequence' ), } ) , reference_urls=[] , ) def UpperCAmelCase_ ( self : int , _A : Tuple , _A : Tuple , _A : str=None , _A : Dict=False , _A : Tuple=False , _A : str=False , ) -> Tuple: """simple docstring""" if regexes_to_ignore is not None: for s in regexes_to_ignore: snake_case_ : List[Any] = np.array([re.sub(_A , '' , _A ) for x in predictions] ) snake_case_ : Optional[Any] = np.array([re.sub(_A , '' , _A ) for x in references] ) else: snake_case_ : Dict = np.asarray(_A ) snake_case_ : Tuple = np.asarray(_A ) if ignore_case: snake_case_ : List[str] = np.char.lower(_A ) snake_case_ : Any = np.char.lower(_A ) if ignore_punctuation: snake_case_ : int = string.punctuation.maketrans('' , '' , string.punctuation ) snake_case_ : Tuple = np.char.translate(_A , table=_A ) snake_case_ : str = np.char.translate(_A , table=_A ) if ignore_numbers: snake_case_ : Optional[int] = string.digits.maketrans('' , '' , string.digits ) snake_case_ : str = np.char.translate(_A , table=_A ) snake_case_ : Union[str, Any] = np.char.translate(_A , table=_A ) snake_case_ : int = predictions == references return {"exact_match": np.mean(_A ) * 100}
327
1
import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class SCREAMING_SNAKE_CASE_ ( snake_case_ , unittest.TestCase ): __magic_name__: int = MobileBertTokenizer __magic_name__: List[Any] = MobileBertTokenizerFast __magic_name__: List[str] = True __magic_name__: List[Any] = True __magic_name__: Union[str, Any] = filter_non_english __magic_name__: str = "google/mobilebert-uncased" def UpperCAmelCase_ ( self : List[str] ) -> Dict: """simple docstring""" super().setUp() snake_case_ : List[Any] = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] snake_case_ : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) snake_case_ : List[Any] = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def UpperCAmelCase_ ( self : Optional[int] , _A : List[str] ) -> int: """simple docstring""" snake_case_ : Tuple = 'UNwant\u00E9d,running' snake_case_ : Optional[int] = 'unwanted, running' return input_text, output_text def UpperCAmelCase_ ( self : Tuple ) -> Tuple: """simple docstring""" snake_case_ : Dict = self.tokenizer_class(self.vocab_file ) snake_case_ : Any = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(_A , ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_A ) , [9, 6, 7, 12, 10, 11] ) def UpperCAmelCase_ ( self : Tuple ) -> List[str]: """simple docstring""" if not self.test_rust_tokenizer: return snake_case_ : List[str] = self.get_tokenizer() snake_case_ : Any = self.get_rust_tokenizer() snake_case_ : str = 'UNwant\u00E9d,running' snake_case_ : str = tokenizer.tokenize(_A ) snake_case_ : Any = rust_tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) snake_case_ : Optional[Any] = tokenizer.encode(_A , add_special_tokens=_A ) snake_case_ : List[Any] = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) snake_case_ : Any = self.get_rust_tokenizer() snake_case_ : Union[str, Any] = tokenizer.encode(_A ) snake_case_ : Any = rust_tokenizer.encode(_A ) self.assertListEqual(_A , _A ) # With lower casing snake_case_ : str = self.get_tokenizer(do_lower_case=_A ) snake_case_ : Dict = self.get_rust_tokenizer(do_lower_case=_A ) snake_case_ : List[str] = 'UNwant\u00E9d,running' snake_case_ : Tuple = tokenizer.tokenize(_A ) snake_case_ : Dict = rust_tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) snake_case_ : Dict = tokenizer.encode(_A , add_special_tokens=_A ) snake_case_ : Tuple = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) snake_case_ : Tuple = self.get_rust_tokenizer() snake_case_ : Dict = tokenizer.encode(_A ) snake_case_ : Any = rust_tokenizer.encode(_A ) self.assertListEqual(_A , _A ) def UpperCAmelCase_ ( self : str ) -> Any: """simple docstring""" snake_case_ : Union[str, Any] = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ) , ['ah', '\u535A', '\u63A8', 'zz'] ) def UpperCAmelCase_ ( self : Tuple ) -> Any: """simple docstring""" snake_case_ : List[str] = BasicTokenizer(do_lower_case=_A ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['hello', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCAmelCase_ ( self : str ) -> Dict: """simple docstring""" snake_case_ : Tuple = BasicTokenizer(do_lower_case=_A , strip_accents=_A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hällo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['h\u00E9llo'] ) def UpperCAmelCase_ ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" snake_case_ : Any = BasicTokenizer(do_lower_case=_A , strip_accents=_A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCAmelCase_ ( self : Any ) -> List[str]: """simple docstring""" snake_case_ : Tuple = BasicTokenizer(do_lower_case=_A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCAmelCase_ ( self : Dict ) -> Dict: """simple docstring""" snake_case_ : Tuple = BasicTokenizer(do_lower_case=_A ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" snake_case_ : List[Any] = BasicTokenizer(do_lower_case=_A , strip_accents=_A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HäLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCAmelCase_ ( self : int ) -> Tuple: """simple docstring""" snake_case_ : str = BasicTokenizer(do_lower_case=_A , strip_accents=_A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HaLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCAmelCase_ ( self : Dict ) -> Any: """simple docstring""" snake_case_ : str = BasicTokenizer(do_lower_case=_A , never_split=['[UNK]'] ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]'] ) def UpperCAmelCase_ ( self : Optional[int] ) -> Tuple: """simple docstring""" snake_case_ : Union[str, Any] = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] snake_case_ : Any = {} for i, token in enumerate(_A ): snake_case_ : str = i snake_case_ : Any = WordpieceTokenizer(vocab=_A , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('unwanted running' ) , ['un', '##want', '##ed', 'runn', '##ing'] ) self.assertListEqual(tokenizer.tokenize('unwantedX running' ) , ['[UNK]', 'runn', '##ing'] ) def UpperCAmelCase_ ( self : Tuple ) -> Any: """simple docstring""" self.assertTrue(_is_whitespace(' ' ) ) self.assertTrue(_is_whitespace('\t' ) ) self.assertTrue(_is_whitespace('\r' ) ) self.assertTrue(_is_whitespace('\n' ) ) self.assertTrue(_is_whitespace('\u00A0' ) ) self.assertFalse(_is_whitespace('A' ) ) self.assertFalse(_is_whitespace('-' ) ) def UpperCAmelCase_ ( self : Dict ) -> Any: """simple docstring""" self.assertTrue(_is_control('\u0005' ) ) self.assertFalse(_is_control('A' ) ) self.assertFalse(_is_control(' ' ) ) self.assertFalse(_is_control('\t' ) ) self.assertFalse(_is_control('\r' ) ) def UpperCAmelCase_ ( self : Any ) -> List[Any]: """simple docstring""" self.assertTrue(_is_punctuation('-' ) ) self.assertTrue(_is_punctuation('$' ) ) self.assertTrue(_is_punctuation('`' ) ) self.assertTrue(_is_punctuation('.' ) ) self.assertFalse(_is_punctuation('A' ) ) self.assertFalse(_is_punctuation(' ' ) ) def UpperCAmelCase_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" snake_case_ : Tuple = self.get_tokenizer() snake_case_ : List[str] = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(_A ) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']] ) self.assertListEqual( [rust_tokenizer.tokenize(_A ) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']] ) @slow def UpperCAmelCase_ ( self : Dict ) -> int: """simple docstring""" snake_case_ : Tuple = self.tokenizer_class.from_pretrained('google/mobilebert-uncased' ) snake_case_ : Optional[Any] = tokenizer.encode('sequence builders' , add_special_tokens=_A ) snake_case_ : Any = tokenizer.encode('multi-sequence build' , add_special_tokens=_A ) snake_case_ : Tuple = tokenizer.build_inputs_with_special_tokens(_A ) snake_case_ : Optional[int] = tokenizer.build_inputs_with_special_tokens(_A , _A ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def UpperCAmelCase_ ( self : Tuple ) -> str: """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): snake_case_ : Any = self.rust_tokenizer_class.from_pretrained(_A , **_A ) snake_case_ : Optional[Any] = F"""A, naïve {tokenizer_r.mask_token} AllenNLP sentence.""" snake_case_ : List[str] = tokenizer_r.encode_plus( _A , return_attention_mask=_A , return_token_type_ids=_A , return_offsets_mapping=_A , add_special_tokens=_A , ) snake_case_ : Optional[Any] = tokenizer_r.do_lower_case if hasattr(_A , 'do_lower_case' ) else False snake_case_ : Tuple = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), 'A'), ((1, 2), ','), ((3, 5), 'na'), ((5, 6), '##ï'), ((6, 8), '##ve'), ((9, 15), tokenizer_r.mask_token), ((16, 21), 'Allen'), ((21, 23), '##NL'), ((23, 24), '##P'), ((25, 33), 'sentence'), ((33, 34), '.'), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), 'a'), ((1, 2), ','), ((3, 8), 'naive'), ((9, 15), tokenizer_r.mask_token), ((16, 21), 'allen'), ((21, 23), '##nl'), ((23, 24), '##p'), ((25, 33), 'sentence'), ((33, 34), '.'), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['input_ids'] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['offset_mapping'] ) def UpperCAmelCase_ ( self : List[str] ) -> Optional[Any]: """simple docstring""" snake_case_ : str = ['的', '人', '有'] snake_case_ : Any = ''.join(_A ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): snake_case_ : int = True snake_case_ : Optional[int] = self.tokenizer_class.from_pretrained(_A , **_A ) snake_case_ : List[str] = self.rust_tokenizer_class.from_pretrained(_A , **_A ) snake_case_ : List[Any] = tokenizer_p.encode(_A , add_special_tokens=_A ) snake_case_ : Union[str, Any] = tokenizer_r.encode(_A , add_special_tokens=_A ) snake_case_ : Optional[Any] = tokenizer_r.convert_ids_to_tokens(_A ) snake_case_ : Optional[int] = tokenizer_p.convert_ids_to_tokens(_A ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(_A , _A ) self.assertListEqual(_A , _A ) snake_case_ : Union[str, Any] = False snake_case_ : Any = self.rust_tokenizer_class.from_pretrained(_A , **_A ) snake_case_ : Any = self.tokenizer_class.from_pretrained(_A , **_A ) snake_case_ : List[Any] = tokenizer_r.encode(_A , add_special_tokens=_A ) snake_case_ : List[Any] = tokenizer_p.encode(_A , add_special_tokens=_A ) snake_case_ : Union[str, Any] = tokenizer_r.convert_ids_to_tokens(_A ) snake_case_ : Union[str, Any] = tokenizer_p.convert_ids_to_tokens(_A ) # it is expected that only the first Chinese character is not preceded by "##". snake_case_ : Optional[int] = [ F"""##{token}""" if idx != 0 else token for idx, token in enumerate(_A ) ] self.assertListEqual(_A , _A ) self.assertListEqual(_A , _A )
327
from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class SCREAMING_SNAKE_CASE_ : def __init__( self : List[Any] , _A : Optional[Any] , _A : Dict=13 , _A : Union[str, Any]=30 , _A : Tuple=2 , _A : Union[str, Any]=3 , _A : Optional[int]=True , _A : Optional[Any]=True , _A : str=32 , _A : int=2 , _A : List[str]=4 , _A : List[str]=37 , _A : Tuple="gelu" , _A : Dict=0.1 , _A : Optional[Any]=0.1 , _A : Optional[int]=10 , _A : Optional[int]=0.0_2 , _A : Optional[Any]=3 , _A : str=0.6 , _A : Union[str, Any]=None , ) -> Any: """simple docstring""" snake_case_ : Optional[int] = parent snake_case_ : Tuple = batch_size snake_case_ : List[Any] = image_size snake_case_ : List[str] = patch_size snake_case_ : List[str] = num_channels snake_case_ : Optional[Any] = is_training snake_case_ : Any = use_labels snake_case_ : Tuple = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : List[Any] = num_attention_heads snake_case_ : Optional[Any] = intermediate_size snake_case_ : List[Any] = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Any = attention_probs_dropout_prob snake_case_ : Tuple = type_sequence_label_size snake_case_ : List[str] = initializer_range snake_case_ : Optional[Any] = mask_ratio snake_case_ : Any = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) snake_case_ : Optional[int] = (image_size // patch_size) ** 2 snake_case_ : str = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def UpperCAmelCase_ ( self : List[str] ) -> Optional[Any]: """simple docstring""" snake_case_ : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ : Union[str, Any] = None if self.use_labels: snake_case_ : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ : Union[str, Any] = self.get_config() return config, pixel_values, labels def UpperCAmelCase_ ( self : int ) -> Optional[Any]: """simple docstring""" return ViTMAEConfig( 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 , decoder_hidden_size=self.hidden_size , decoder_num_hidden_layers=self.num_hidden_layers , decoder_num_attention_heads=self.num_attention_heads , decoder_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=_A , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def UpperCAmelCase_ ( self : List[Any] , _A : int , _A : Dict , _A : str ) -> Dict: """simple docstring""" snake_case_ : Union[str, Any] = TFViTMAEModel(config=_A ) snake_case_ : str = model(_A , training=_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self : Dict , _A : Dict , _A : Any , _A : List[Any] ) -> int: """simple docstring""" snake_case_ : Any = TFViTMAEForPreTraining(_A ) snake_case_ : Optional[Any] = model(_A , training=_A ) # expected sequence length = num_patches snake_case_ : List[str] = (self.image_size // self.patch_size) ** 2 snake_case_ : Optional[Any] = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images snake_case_ : str = 1 snake_case_ : Dict = TFViTMAEForPreTraining(_A ) snake_case_ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) snake_case_ : List[str] = model(_A , training=_A ) snake_case_ : Optional[Any] = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" snake_case_ : List[Any] = self.prepare_config_and_inputs() ((snake_case_) ,(snake_case_) ,(snake_case_)) : Any = config_and_inputs snake_case_ : Optional[Any] = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE_ ( snake_case_ , snake_case_ , unittest.TestCase ): __magic_name__: List[str] = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () __magic_name__: str = {"feature-extraction": TFViTMAEModel} if is_tf_available() else {} __magic_name__: Dict = False __magic_name__: Dict = False __magic_name__: List[Any] = False __magic_name__: Dict = False def UpperCAmelCase_ ( self : Any ) -> List[Any]: """simple docstring""" snake_case_ : List[Any] = TFViTMAEModelTester(self ) snake_case_ : Tuple = ConfigTester(self , config_class=_A , has_text_modality=_A , hidden_size=37 ) def UpperCAmelCase_ ( self : int ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViTMAE does not use inputs_embeds' ) def UpperCAmelCase_ ( self : Tuple ) -> Union[str, Any]: """simple docstring""" pass def UpperCAmelCase_ ( self : List[str] ) -> Union[str, Any]: """simple docstring""" snake_case_ ,snake_case_ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ : List[Any] = model_class(_A ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) snake_case_ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_A , tf.keras.layers.Layer ) ) def UpperCAmelCase_ ( self : List[str] ) -> Dict: """simple docstring""" snake_case_ ,snake_case_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ : List[str] = model_class(_A ) snake_case_ : Any = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ : Dict = [*signature.parameters.keys()] snake_case_ : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] , _A ) def UpperCAmelCase_ ( self : Dict ) -> List[str]: """simple docstring""" snake_case_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def UpperCAmelCase_ ( self : List[Any] ) -> List[str]: """simple docstring""" snake_case_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_A ) def UpperCAmelCase_ ( self : Tuple ) -> Dict: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : int = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : Optional[int] = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : Optional[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: snake_case_ : Optional[Any] = model_class(_A ) snake_case_ : Union[str, Any] = self._prepare_for_class(_A , _A ) snake_case_ : List[str] = model(_A , noise=_A ) snake_case_ : Tuple = copy.deepcopy(self._prepare_for_class(_A , _A ) ) snake_case_ : str = model(**_A , noise=_A ) snake_case_ : Union[str, Any] = outputs_dict[0].numpy() snake_case_ : Optional[Any] = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1E-6 ) def UpperCAmelCase_ ( self : List[Any] ) -> List[Any]: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : Tuple = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(_A : int ): snake_case_ : Any = {} for k, v in inputs_dict.items(): if tf.is_tensor(_A ): snake_case_ : str = v.numpy() else: snake_case_ : Optional[Any] = np.array(_A ) return inputs_np_dict for model_class in self.all_model_classes: snake_case_ : int = model_class(_A ) snake_case_ : List[Any] = self._prepare_for_class(_A , _A ) snake_case_ : Any = prepare_numpy_arrays(_A ) snake_case_ : List[Any] = model(_A , noise=_A ) snake_case_ : List[Any] = model(**_A , noise=_A ) self.assert_outputs_same(_A , _A ) def UpperCAmelCase_ ( self : Tuple , _A : Union[str, Any] , _A : Union[str, Any] , _A : List[Any] ) -> List[str]: """simple docstring""" np.random.seed(2 ) snake_case_ : Optional[int] = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) snake_case_ : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) snake_case_ : Optional[int] = tf.constant(_A ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument snake_case_ : Optional[Any] = tf_noise super().check_pt_tf_models(_A , _A , _A ) def UpperCAmelCase_ ( self : Optional[Any] ) -> Dict: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : int = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(_A ) if module_member_name.endswith('MainLayer' ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len('MainLayer' )] == model_class.__name__[: -len('Model' )] for module_member in (getattr(_A , _A ),) if isinstance(_A , _A ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(_A , '_keras_serializable' , _A ) } snake_case_ : List[Any] = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) snake_case_ : Optional[int] = tf.convert_to_tensor(_A ) inputs_dict.update({'noise': noise} ) for main_layer_class in tf_main_layer_classes: snake_case_ : Optional[Any] = main_layer_class(_A ) snake_case_ : List[str] = { name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } snake_case_ : Union[str, Any] = tf.keras.Model(_A , outputs=main_layer(_A ) ) snake_case_ : int = model(_A ) with tempfile.TemporaryDirectory() as tmpdirname: snake_case_ : List[Any] = os.path.join(_A , 'keras_model.h5' ) model.save(_A ) snake_case_ : str = tf.keras.models.load_model( _A , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(_A , tf.keras.Model ) snake_case_ : List[str] = model(_A ) self.assert_outputs_same(_A , _A ) @slow def UpperCAmelCase_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : int = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : int = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: snake_case_ : Optional[Any] = model_class(_A ) snake_case_ : Optional[Any] = self._prepare_for_class(_A , _A ) snake_case_ : int = model(_A , noise=_A ) if model_class.__name__ == "TFViTMAEModel": snake_case_ : Any = outputs.last_hidden_state.numpy() snake_case_ : Optional[int] = 0 else: snake_case_ : str = outputs.logits.numpy() snake_case_ : Optional[Any] = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_A , saved_model=_A ) snake_case_ : Any = model_class.from_pretrained(_A ) snake_case_ : Any = model(_A , noise=_A ) if model_class.__name__ == "TFViTMAEModel": snake_case_ : Dict = after_outputs['last_hidden_state'].numpy() snake_case_ : Dict = 0 else: snake_case_ : Any = after_outputs['logits'].numpy() snake_case_ : Optional[Any] = 0 snake_case_ : Any = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_A , 1E-5 ) def UpperCAmelCase_ ( self : Any ) -> str: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : Optional[int] = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : Union[str, Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: snake_case_ : str = model_class(_A ) snake_case_ : int = self._prepare_for_class(_A , _A ) snake_case_ : str = model(_A , noise=_A ) snake_case_ : Dict = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(_A ) snake_case_ : Any = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config snake_case_ : str = model_class.from_config(model.config ) snake_case_ : Union[str, Any] = new_model(_A ) # Build model new_model.set_weights(model.get_weights() ) snake_case_ : List[str] = new_model(_A , noise=_A ) self.assert_outputs_same(_A , _A ) @unittest.skip( reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results.' ) def UpperCAmelCase_ ( self : List[Any] ) -> Optional[int]: """simple docstring""" pass @unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load' ) def UpperCAmelCase_ ( self : Optional[int] ) -> Tuple: """simple docstring""" pass @slow def UpperCAmelCase_ ( self : Tuple ) -> Tuple: """simple docstring""" snake_case_ : Optional[Any] = TFViTMAEModel.from_pretrained('google/vit-base-patch16-224' ) self.assertIsNotNone(_A ) def SCREAMING_SNAKE_CASE__ ( ): snake_case_ : Optional[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): @cached_property def UpperCAmelCase_ ( self : str ) -> Dict: """simple docstring""" return ViTImageProcessor.from_pretrained('facebook/vit-mae-base' ) if is_vision_available() else None @slow def UpperCAmelCase_ ( self : str ) -> Dict: """simple docstring""" np.random.seed(2 ) snake_case_ : List[str] = TFViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base' ) snake_case_ : List[Any] = self.default_image_processor snake_case_ : Dict = prepare_img() snake_case_ : Optional[Any] = image_processor(images=_A , return_tensors='tf' ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) snake_case_ : int = ViTMAEConfig() snake_case_ : List[Any] = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) snake_case_ : List[Any] = np.random.uniform(size=(1, num_patches) ) # forward pass snake_case_ : Optional[Any] = model(**_A , noise=_A ) # verify the logits snake_case_ : Optional[int] = tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape , _A ) snake_case_ : Any = tf.convert_to_tensor( [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] , _A , atol=1E-4 )
327
1
import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def SCREAMING_SNAKE_CASE__ ( __a , __a ): snake_case_ : str = old_name if "patch_embed" in old_name: snake_case_ ,snake_case_ ,snake_case_ : str = old_name.split('.' ) if layer == "0": snake_case_ : Any = old_name.replace('0' , 'convolution1' ) elif layer == "1": snake_case_ : List[str] = old_name.replace('1' , 'batchnorm_before' ) elif layer == "3": snake_case_ : Union[str, Any] = old_name.replace('3' , 'convolution2' ) else: snake_case_ : Any = old_name.replace('4' , 'batchnorm_after' ) if "network" in old_name and re.search(r'\d\.\d' , __a ): snake_case_ : List[Any] = r'\b\d{2}\b' if bool(re.search(__a , __a ) ): snake_case_ : List[str] = re.search(r'\d\.\d\d.' , __a ).group() else: snake_case_ : Optional[Any] = re.search(r'\d\.\d.' , __a ).group() if int(match[0] ) < 6: snake_case_ : Any = old_name.replace(__a , '' ) snake_case_ : int = trimmed_name.replace('network' , match[0] + '.meta4D_layers.blocks.' + match[2:-1] ) snake_case_ : Dict = 'intermediate_stages.' + trimmed_name else: snake_case_ : Dict = old_name.replace(__a , '' ) if int(match[2] ) < num_meta4D_last_stage: snake_case_ : Optional[Any] = trimmed_name.replace('network' , 'meta4D_layers.blocks.' + match[2] ) else: snake_case_ : List[Any] = str(int(match[2] ) - num_meta4D_last_stage ) snake_case_ : List[Any] = trimmed_name.replace('network' , 'meta3D_layers.blocks.' + layer_index ) if "norm1" in old_name: snake_case_ : int = trimmed_name.replace('norm1' , 'layernorm1' ) elif "norm2" in old_name: snake_case_ : List[str] = trimmed_name.replace('norm2' , 'layernorm2' ) elif "fc1" in old_name: snake_case_ : int = trimmed_name.replace('fc1' , 'linear_in' ) elif "fc2" in old_name: snake_case_ : Dict = trimmed_name.replace('fc2' , 'linear_out' ) snake_case_ : int = 'last_stage.' + trimmed_name elif "network" in old_name and re.search(r'.\d.' , __a ): snake_case_ : int = old_name.replace('network' , 'intermediate_stages' ) if "fc" in new_name: snake_case_ : Union[str, Any] = new_name.replace('fc' , 'convolution' ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): snake_case_ : Union[str, Any] = new_name.replace('norm1' , 'batchnorm_before' ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): snake_case_ : Tuple = new_name.replace('norm2' , 'batchnorm_after' ) if "proj" in new_name: snake_case_ : Optional[Any] = new_name.replace('proj' , 'projection' ) if "dist_head" in new_name: snake_case_ : Union[str, Any] = new_name.replace('dist_head' , 'distillation_classifier' ) elif "head" in new_name: snake_case_ : Tuple = new_name.replace('head' , 'classifier' ) elif "patch_embed" in new_name: snake_case_ : str = 'efficientformer.' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": snake_case_ : Optional[Any] = new_name.replace('norm' , 'layernorm' ) snake_case_ : Optional[Any] = 'efficientformer.' + new_name else: snake_case_ : Dict = 'efficientformer.encoder.' + new_name return new_name def SCREAMING_SNAKE_CASE__ ( __a , __a ): for key in checkpoint.copy().keys(): snake_case_ : Union[str, Any] = checkpoint.pop(__a ) snake_case_ : Union[str, Any] = val return checkpoint def SCREAMING_SNAKE_CASE__ ( ): snake_case_ : Tuple = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case_ : Optional[Any] = Image.open(requests.get(__a , stream=__a ).raw ) return image def SCREAMING_SNAKE_CASE__ ( __a , __a , __a , __a ): snake_case_ : List[str] = torch.load(__a , map_location='cpu' )['model'] snake_case_ : str = EfficientFormerConfig.from_json_file(__a ) snake_case_ : str = EfficientFormerForImageClassificationWithTeacher(__a ) snake_case_ : Optional[Any] = '_'.join(checkpoint_path.split('/' )[-1].split('.' )[0].split('_' )[:-1] ) snake_case_ : Optional[int] = config.depths[-1] - config.num_metaad_blocks + 1 snake_case_ : Any = convert_torch_checkpoint(__a , __a ) model.load_state_dict(__a ) model.eval() snake_case_ : Dict = { 'bilinear': PILImageResampling.BILINEAR, 'bicubic': PILImageResampling.BICUBIC, 'nearest': PILImageResampling.NEAREST, } # prepare image snake_case_ : Dict = prepare_img() snake_case_ : Optional[int] = 2_56 snake_case_ : int = 2_24 snake_case_ : Optional[Any] = EfficientFormerImageProcessor( size={'shortest_edge': image_size} , crop_size={'height': crop_size, 'width': crop_size} , resample=pillow_resamplings['bicubic'] , ) snake_case_ : Optional[Any] = processor(images=__a , return_tensors='pt' ).pixel_values # original processing pipeline snake_case_ : str = Compose( [ Resize(__a , interpolation=pillow_resamplings['bicubic'] ), CenterCrop(__a ), ToTensor(), Normalize(__a , __a ), ] ) snake_case_ : Union[str, Any] = image_transforms(__a ).unsqueeze(0 ) assert torch.allclose(__a , __a ) snake_case_ : Optional[Any] = model(__a ) snake_case_ : int = outputs.logits snake_case_ : int = (1, 10_00) if "l1" in model_name: snake_case_ : int = torch.Tensor( [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328] ) assert torch.allclose(logits[0, :10] , __a , atol=1E-3 ) assert logits.shape == expected_shape elif "l3" in model_name: snake_case_ : int = torch.Tensor( [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127] ) assert torch.allclose(logits[0, :10] , __a , atol=1E-3 ) assert logits.shape == expected_shape elif "l7" in model_name: snake_case_ : List[Any] = torch.Tensor( [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878] ) assert logits.shape == expected_shape else: raise ValueError( f"""Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7""" ) # Save Checkpoints Path(__a ).mkdir(exist_ok=__a ) model.save_pretrained(__a ) print(f"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""" ) processor.save_pretrained(__a ) print(f"""Processor successfuly saved at {pytorch_dump_path}""" ) if push_to_hub: print('Pushing model to the hub...' ) model.push_to_hub( repo_id=f"""Bearnardd/{pytorch_dump_path}""" , commit_message='Add model' , use_temp_dir=__a , ) processor.push_to_hub( repo_id=f"""Bearnardd/{pytorch_dump_path}""" , commit_message='Add image processor' , use_temp_dir=__a , ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( """--pytorch_model_path""", default=None, type=str, required=True, help="""Path to EfficientFormer pytorch checkpoint.""", ) parser.add_argument( """--config_file""", default=None, type=str, required=True, help="""The json file for EfficientFormer model config.""", ) parser.add_argument( """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") parser.add_argument( """--no-push_to_hub""", dest="""push_to_hub""", action="""store_false""", help="""Do not push model and image processor to the hub""", ) parser.set_defaults(push_to_hub=True) _SCREAMING_SNAKE_CASE = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
327
from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( __a , __a ): snake_case_ : list[list[int]] = [] snake_case_ : list[int] = [] snake_case_ : List[Any] = 0 snake_case_ : Union[str, Any] = sum(__a ) create_state_space_tree(__a , __a , __a , __a , __a , __a ) return result def SCREAMING_SNAKE_CASE__ ( __a , __a , __a , __a , __a , __a , ): if sum(__a ) > max_sum or (remaining_nums_sum + sum(__a )) < max_sum: return if sum(__a ) == max_sum: result.append(__a ) return for index in range(__a , len(__a ) ): create_state_space_tree( __a , __a , index + 1 , [*path, nums[index]] , __a , remaining_nums_sum - nums[index] , ) _SCREAMING_SNAKE_CASE = [3, 34, 4, 12, 5, 2] _SCREAMING_SNAKE_CASE = 9 _SCREAMING_SNAKE_CASE = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
327
1
from __future__ import annotations import unittest from transformers import FunnelConfig, is_tf_available from transformers.testing_utils import require_tf from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, ) class SCREAMING_SNAKE_CASE_ : def __init__( self : Any , _A : Optional[Any] , _A : str=13 , _A : List[Any]=7 , _A : int=True , _A : int=True , _A : Optional[int]=True , _A : List[str]=True , _A : Union[str, Any]=99 , _A : Dict=[1, 1, 2] , _A : Dict=1 , _A : Any=32 , _A : str=4 , _A : Dict=8 , _A : Dict=37 , _A : Dict="gelu_new" , _A : str=0.1 , _A : Tuple=0.1 , _A : str=0.0 , _A : Optional[Any]=512 , _A : List[Any]=3 , _A : str=0.0_2 , _A : Optional[Any]=3 , _A : List[str]=4 , _A : Dict=None , _A : str=False , ) -> Tuple: """simple docstring""" snake_case_ : List[Any] = parent snake_case_ : Tuple = batch_size snake_case_ : Optional[Any] = seq_length snake_case_ : Dict = is_training snake_case_ : Optional[int] = use_input_mask snake_case_ : Any = use_token_type_ids snake_case_ : List[str] = use_labels snake_case_ : Tuple = vocab_size snake_case_ : List[str] = block_sizes snake_case_ : Optional[Any] = num_decoder_layers snake_case_ : Dict = d_model snake_case_ : int = n_head snake_case_ : str = d_head snake_case_ : Tuple = d_inner snake_case_ : Optional[int] = hidden_act snake_case_ : Optional[Any] = hidden_dropout snake_case_ : Dict = attention_dropout snake_case_ : List[str] = activation_dropout snake_case_ : Union[str, Any] = max_position_embeddings snake_case_ : Optional[int] = type_vocab_size snake_case_ : Optional[int] = 2 snake_case_ : Dict = num_labels snake_case_ : List[str] = num_choices snake_case_ : int = scope snake_case_ : List[Any] = initializer_std # Used in the tests to check the size of the first attention layer snake_case_ : Dict = n_head # Used in the tests to check the size of the first hidden state snake_case_ : str = self.d_model # Used in the tests to check the number of output hidden states/attentions snake_case_ : Any = sum(self.block_sizes ) + (0 if base else self.num_decoder_layers) # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with # the last hidden state of the first block (which is the first hidden state of the decoder). if not base: snake_case_ : int = self.num_hidden_layers + 2 def UpperCAmelCase_ ( self : int ) -> List[Any]: """simple docstring""" snake_case_ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ : Tuple = None if self.use_input_mask: snake_case_ : int = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ : Optional[Any] = None if self.use_token_type_ids: snake_case_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case_ : Dict = None snake_case_ : List[str] = None snake_case_ : str = None if self.use_labels: snake_case_ : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ : Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ : Tuple = ids_tensor([self.batch_size] , self.num_choices ) snake_case_ : Optional[Any] = FunnelConfig( vocab_size=self.vocab_size , block_sizes=self.block_sizes , num_decoder_layers=self.num_decoder_layers , d_model=self.d_model , n_head=self.n_head , d_head=self.d_head , d_inner=self.d_inner , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , activation_dropout=self.activation_dropout , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_std=self.initializer_std , ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, ) def UpperCAmelCase_ ( self : Optional[int] , _A : str , _A : int , _A : Dict , _A : Tuple , _A : Optional[int] , _A : Dict , _A : Tuple , ) -> int: """simple docstring""" snake_case_ : Tuple = TFFunnelModel(config=_A ) snake_case_ : List[str] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case_ : Optional[Any] = model(_A ) snake_case_ : List[Any] = [input_ids, input_mask] snake_case_ : List[str] = model(_A ) snake_case_ : Dict = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) snake_case_ : List[Any] = False snake_case_ : int = TFFunnelModel(config=_A ) snake_case_ : Optional[Any] = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) snake_case_ : Tuple = False snake_case_ : Optional[Any] = TFFunnelModel(config=_A ) snake_case_ : Tuple = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) def UpperCAmelCase_ ( self : Any , _A : Dict , _A : Dict , _A : Optional[Any] , _A : Dict , _A : List[str] , _A : Union[str, Any] , _A : List[str] , ) -> Tuple: """simple docstring""" snake_case_ : Optional[int] = TFFunnelBaseModel(config=_A ) snake_case_ : Tuple = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case_ : Optional[int] = model(_A ) snake_case_ : Optional[Any] = [input_ids, input_mask] snake_case_ : Tuple = model(_A ) snake_case_ : Optional[Any] = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 2, self.d_model) ) snake_case_ : int = False snake_case_ : List[Any] = TFFunnelBaseModel(config=_A ) snake_case_ : Tuple = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 3, self.d_model) ) snake_case_ : Optional[int] = False snake_case_ : Tuple = TFFunnelBaseModel(config=_A ) snake_case_ : Optional[Any] = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 2, self.d_model) ) def UpperCAmelCase_ ( self : Optional[int] , _A : int , _A : Optional[Any] , _A : List[Any] , _A : Tuple , _A : Union[str, Any] , _A : int , _A : Dict , ) -> Union[str, Any]: """simple docstring""" snake_case_ : int = TFFunnelForPreTraining(config=_A ) snake_case_ : List[Any] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case_ : Optional[Any] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length) ) def UpperCAmelCase_ ( self : str , _A : Dict , _A : Optional[Any] , _A : List[str] , _A : str , _A : Optional[Any] , _A : List[str] , _A : Tuple , ) -> List[str]: """simple docstring""" snake_case_ : str = TFFunnelForMaskedLM(config=_A ) snake_case_ : List[Any] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case_ : List[Any] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase_ ( self : Optional[Any] , _A : List[str] , _A : Dict , _A : Union[str, Any] , _A : Optional[int] , _A : Dict , _A : List[Any] , _A : List[Any] , ) -> Union[str, Any]: """simple docstring""" snake_case_ : Optional[Any] = self.num_labels snake_case_ : int = TFFunnelForSequenceClassification(config=_A ) snake_case_ : Any = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case_ : Dict = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase_ ( self : Optional[Any] , _A : Optional[int] , _A : Optional[Any] , _A : Any , _A : int , _A : Tuple , _A : Dict , _A : str , ) -> int: """simple docstring""" snake_case_ : List[str] = self.num_choices snake_case_ : Dict = TFFunnelForMultipleChoice(config=_A ) snake_case_ : List[Any] = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) snake_case_ : str = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) snake_case_ : str = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) snake_case_ : Tuple = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } snake_case_ : Dict = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCAmelCase_ ( self : Optional[int] , _A : Dict , _A : Tuple , _A : int , _A : Optional[int] , _A : Union[str, Any] , _A : str , _A : Optional[Any] , ) -> Any: """simple docstring""" snake_case_ : Tuple = self.num_labels snake_case_ : Union[str, Any] = TFFunnelForTokenClassification(config=_A ) snake_case_ : List[str] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case_ : int = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase_ ( self : Any , _A : Dict , _A : Optional[Any] , _A : Optional[int] , _A : str , _A : Optional[Any] , _A : List[str] , _A : List[str] , ) -> str: """simple docstring""" snake_case_ : Optional[Any] = TFFunnelForQuestionAnswering(config=_A ) snake_case_ : Dict = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case_ : Any = model(_A ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCAmelCase_ ( self : List[str] ) -> Any: """simple docstring""" snake_case_ : Tuple = self.prepare_config_and_inputs() ( ( snake_case_ ) ,( snake_case_ ) ,( snake_case_ ) ,( snake_case_ ) ,( snake_case_ ) ,( snake_case_ ) ,( snake_case_ ) , ) : Any = config_and_inputs snake_case_ : str = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE_ ( snake_case_ , snake_case_ , unittest.TestCase ): __magic_name__: List[Any] = ( ( TFFunnelModel, TFFunnelForMaskedLM, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForTokenClassification, ) if is_tf_available() else () ) __magic_name__: List[Any] = ( { "feature-extraction": (TFFunnelBaseModel, TFFunnelModel), "fill-mask": TFFunnelForMaskedLM, "question-answering": TFFunnelForQuestionAnswering, "text-classification": TFFunnelForSequenceClassification, "token-classification": TFFunnelForTokenClassification, "zero-shot": TFFunnelForSequenceClassification, } if is_tf_available() else {} ) __magic_name__: int = False __magic_name__: Union[str, Any] = False def UpperCAmelCase_ ( self : Tuple ) -> Tuple: """simple docstring""" snake_case_ : Any = TFFunnelModelTester(self ) snake_case_ : List[str] = ConfigTester(self , config_class=_A ) def UpperCAmelCase_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" self.config_tester.run_common_tests() def UpperCAmelCase_ ( self : Optional[Any] ) -> Tuple: """simple docstring""" snake_case_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Any: """simple docstring""" snake_case_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_A ) def UpperCAmelCase_ ( self : int ) -> Tuple: """simple docstring""" snake_case_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_A ) def UpperCAmelCase_ ( self : str ) -> Any: """simple docstring""" snake_case_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_A ) def UpperCAmelCase_ ( self : Tuple ) -> Optional[Any]: """simple docstring""" snake_case_ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_A ) @require_tf class SCREAMING_SNAKE_CASE_ ( snake_case_ , unittest.TestCase ): __magic_name__: Optional[int] = ( (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else () ) __magic_name__: Any = False __magic_name__: Any = False def UpperCAmelCase_ ( self : List[Any] ) -> List[Any]: """simple docstring""" snake_case_ : Any = TFFunnelModelTester(self , base=_A ) snake_case_ : Union[str, Any] = ConfigTester(self , config_class=_A ) def UpperCAmelCase_ ( self : int ) -> int: """simple docstring""" self.config_tester.run_common_tests() def UpperCAmelCase_ ( self : Dict ) -> List[Any]: """simple docstring""" snake_case_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_base_model(*_A ) def UpperCAmelCase_ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_A ) def UpperCAmelCase_ ( self : Tuple ) -> int: """simple docstring""" snake_case_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_A )
327
def SCREAMING_SNAKE_CASE__ ( __a , __a ): if density <= 0: raise ValueError('Impossible fluid density' ) if bulk_modulus <= 0: raise ValueError('Impossible bulk modulus' ) return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
327
1
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPSegProcessor, ViTImageProcessor @require_vision class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): def UpperCAmelCase_ ( self : str ) -> Dict: """simple docstring""" snake_case_ : Optional[int] = tempfile.mkdtemp() # fmt: off snake_case_ : Union[str, Any] = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on snake_case_ : Tuple = dict(zip(_A , range(len(_A ) ) ) ) snake_case_ : List[str] = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', ''] snake_case_ : str = {'unk_token': '<unk>'} snake_case_ : Dict = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) snake_case_ : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_A ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_A ) ) snake_case_ : Tuple = { 'do_resize': True, 'size': 20, 'do_center_crop': True, 'crop_size': 18, 'do_normalize': True, 'image_mean': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], 'image_std': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], } snake_case_ : Optional[Any] = os.path.join(self.tmpdirname , _A ) with open(self.image_processor_file , 'w' , encoding='utf-8' ) as fp: json.dump(_A , _A ) def UpperCAmelCase_ ( self : List[str] , **_A : str ) -> Dict: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **_A ) def UpperCAmelCase_ ( self : Union[str, Any] , **_A : List[str] ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_A ) def UpperCAmelCase_ ( self : Optional[int] , **_A : Tuple ) -> str: """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_A ) def UpperCAmelCase_ ( self : List[Any] ) -> List[str]: """simple docstring""" shutil.rmtree(self.tmpdirname ) def UpperCAmelCase_ ( self : Any ) -> int: """simple docstring""" snake_case_ : int = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] snake_case_ : List[Any] = [Image.fromarray(np.moveaxis(_A , 0 , -1 ) ) for x in image_inputs] return image_inputs def UpperCAmelCase_ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" snake_case_ : int = self.get_tokenizer() snake_case_ : Dict = self.get_rust_tokenizer() snake_case_ : List[Any] = self.get_image_processor() snake_case_ : int = CLIPSegProcessor(tokenizer=_A , image_processor=_A ) processor_slow.save_pretrained(self.tmpdirname ) snake_case_ : Optional[Any] = CLIPSegProcessor.from_pretrained(self.tmpdirname , use_fast=_A ) snake_case_ : int = CLIPSegProcessor(tokenizer=_A , image_processor=_A ) processor_fast.save_pretrained(self.tmpdirname ) snake_case_ : Tuple = CLIPSegProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _A ) self.assertIsInstance(processor_fast.tokenizer , _A ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _A ) self.assertIsInstance(processor_fast.image_processor , _A ) def UpperCAmelCase_ ( self : str ) -> List[Any]: """simple docstring""" snake_case_ : int = CLIPSegProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) snake_case_ : Dict = self.get_tokenizer(bos_token='(BOS)' , eos_token='(EOS)' ) snake_case_ : List[str] = self.get_image_processor(do_normalize=_A , padding_value=1.0 ) snake_case_ : List[str] = CLIPSegProcessor.from_pretrained( self.tmpdirname , bos_token='(BOS)' , eos_token='(EOS)' , do_normalize=_A , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _A ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _A ) def UpperCAmelCase_ ( self : Optional[Any] ) -> int: """simple docstring""" snake_case_ : Optional[int] = self.get_image_processor() snake_case_ : List[Any] = self.get_tokenizer() snake_case_ : List[str] = CLIPSegProcessor(tokenizer=_A , image_processor=_A ) snake_case_ : str = self.prepare_image_inputs() snake_case_ : Any = image_processor(_A , return_tensors='np' ) snake_case_ : int = processor(images=_A , 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 UpperCAmelCase_ ( self : List[Any] ) -> str: """simple docstring""" snake_case_ : Tuple = self.get_image_processor() snake_case_ : int = self.get_tokenizer() snake_case_ : Dict = CLIPSegProcessor(tokenizer=_A , image_processor=_A ) snake_case_ : Tuple = 'lower newer' snake_case_ : List[Any] = processor(text=_A ) snake_case_ : Optional[int] = tokenizer(_A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCAmelCase_ ( self : Optional[Any] ) -> Any: """simple docstring""" snake_case_ : List[Any] = self.get_image_processor() snake_case_ : Optional[Any] = self.get_tokenizer() snake_case_ : str = CLIPSegProcessor(tokenizer=_A , image_processor=_A ) snake_case_ : Optional[Any] = 'lower newer' snake_case_ : str = self.prepare_image_inputs() snake_case_ : Tuple = processor(text=_A , images=_A ) self.assertListEqual(list(inputs.keys() ) , ['input_ids', 'attention_mask', 'pixel_values'] ) # test if it raises when no input is passed with pytest.raises(_A ): processor() def UpperCAmelCase_ ( self : int ) -> int: """simple docstring""" snake_case_ : Union[str, Any] = self.get_image_processor() snake_case_ : List[str] = self.get_tokenizer() snake_case_ : int = CLIPSegProcessor(tokenizer=_A , image_processor=_A ) snake_case_ : List[Any] = self.prepare_image_inputs() snake_case_ : List[str] = self.prepare_image_inputs() snake_case_ : Tuple = processor(images=_A , visual_prompt=_A ) self.assertListEqual(list(inputs.keys() ) , ['pixel_values', 'conditional_pixel_values'] ) # test if it raises when no input is passed with pytest.raises(_A ): processor() def UpperCAmelCase_ ( self : Union[str, Any] ) -> int: """simple docstring""" snake_case_ : Tuple = self.get_image_processor() snake_case_ : Any = self.get_tokenizer() snake_case_ : int = CLIPSegProcessor(tokenizer=_A , image_processor=_A ) snake_case_ : Optional[int] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] snake_case_ : Union[str, Any] = processor.batch_decode(_A ) snake_case_ : str = tokenizer.batch_decode(_A ) self.assertListEqual(_A , _A )
327
from math import pi def SCREAMING_SNAKE_CASE__ ( __a , __a ): return 2 * pi * radius * (angle / 3_60) if __name__ == "__main__": print(arc_length(90, 10))
327
1
def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): snake_case_ : List[Any] = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def SCREAMING_SNAKE_CASE__ ( ): print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
327
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, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE_ ( snake_case_ ): __magic_name__: Optional[Any] = ["pixel_values"] def __init__( self : str , _A : bool = True , _A : Dict[str, int] = None , _A : PILImageResampling = PIL.Image.BICUBIC , _A : bool = True , _A : Dict[str, int] = None , _A : Union[int, float] = 1 / 255 , _A : bool = True , _A : bool = True , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , **_A : str , ) -> None: """simple docstring""" super().__init__(**_A ) snake_case_ : Dict = size if size is not None else {'height': 256, 'width': 256} snake_case_ : Tuple = get_size_dict(_A ) snake_case_ : str = crop_size if crop_size is not None else {'height': 224, 'width': 224} snake_case_ : int = get_size_dict(_A , param_name='crop_size' ) snake_case_ : Union[str, Any] = do_resize snake_case_ : str = size snake_case_ : List[str] = resample snake_case_ : List[Any] = do_center_crop snake_case_ : Dict = crop_size snake_case_ : Tuple = do_rescale snake_case_ : Optional[Any] = rescale_factor snake_case_ : Any = do_normalize snake_case_ : Any = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN snake_case_ : Optional[int] = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCAmelCase_ ( self : Optional[int] , _A : np.ndarray , _A : Dict[str, int] , _A : PILImageResampling = PIL.Image.BICUBIC , _A : Optional[Union[str, ChannelDimension]] = None , **_A : List[str] , ) -> np.ndarray: """simple docstring""" snake_case_ : Tuple = get_size_dict(_A ) if "height" not in size or "width" not in size: raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" ) return resize( _A , size=(size['height'], size['width']) , resample=_A , data_format=_A , **_A ) def UpperCAmelCase_ ( self : int , _A : np.ndarray , _A : Dict[str, int] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Optional[Any] , ) -> np.ndarray: """simple docstring""" snake_case_ : Optional[int] = get_size_dict(_A ) if "height" not in size or "width" not in size: raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" ) return center_crop(_A , size=(size['height'], size['width']) , data_format=_A , **_A ) def UpperCAmelCase_ ( self : Dict , _A : np.ndarray , _A : Union[int, float] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : str , ) -> str: """simple docstring""" return rescale(_A , scale=_A , data_format=_A , **_A ) def UpperCAmelCase_ ( self : Any , _A : np.ndarray , _A : Union[float, List[float]] , _A : Union[float, List[float]] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Tuple , ) -> np.ndarray: """simple docstring""" return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def UpperCAmelCase_ ( self : List[str] , _A : ImageInput , _A : bool = None , _A : Dict[str, int] = None , _A : Union[str, Any]=None , _A : bool = None , _A : Dict[str, int] = None , _A : bool = None , _A : float = None , _A : bool = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[str, TensorType]] = None , _A : ChannelDimension = ChannelDimension.FIRST , **_A : int , ) -> PIL.Image.Image: """simple docstring""" snake_case_ : int = do_resize if do_resize is not None else self.do_resize snake_case_ : str = resample if resample is not None else self.resample snake_case_ : Any = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case_ : List[str] = do_rescale if do_rescale is not None else self.do_rescale snake_case_ : Any = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case_ : List[str] = do_normalize if do_normalize is not None else self.do_normalize snake_case_ : Any = image_mean if image_mean is not None else self.image_mean snake_case_ : Dict = image_std if image_std is not None else self.image_std snake_case_ : int = size if size is not None else self.size snake_case_ : Optional[int] = get_size_dict(_A ) snake_case_ : int = crop_size if crop_size is not None else self.crop_size snake_case_ : Any = get_size_dict(_A , param_name='crop_size' ) snake_case_ : Optional[Any] = make_list_of_images(_A ) if not valid_images(_A ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # All transformations expect numpy arrays. snake_case_ : Optional[Any] = [to_numpy_array(_A ) for image in images] if do_resize: snake_case_ : Dict = [self.resize(image=_A , size=_A , resample=_A ) for image in images] if do_center_crop: snake_case_ : Optional[Any] = [self.center_crop(image=_A , size=_A ) for image in images] if do_rescale: snake_case_ : Optional[int] = [self.rescale(image=_A , scale=_A ) for image in images] if do_normalize: snake_case_ : str = [self.normalize(image=_A , mean=_A , std=_A ) for image in images] snake_case_ : Dict = [to_channel_dimension_format(_A , _A ) for image in images] snake_case_ : Tuple = {'pixel_values': images} return BatchFeature(data=_A , tensor_type=_A )
327
1
import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMInverseScheduler, DDIMScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, StableDiffusionDiffEditPipeline, UNetaDConditionModel, ) from diffusers.utils import load_image, slow from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE_ ( snake_case_ , snake_case_ , unittest.TestCase ): __magic_name__: List[str] = StableDiffusionDiffEditPipeline __magic_name__: Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"height", "width", "image"} | {"image_latents"} __magic_name__: int = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"image"} | {"image_latents"} __magic_name__: Optional[Any] = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __magic_name__: int = frozenset([] ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" torch.manual_seed(0 ) snake_case_ : str = 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 , attention_head_dim=(2, 4) , use_linear_projection=_A , ) snake_case_ : List[str] = DDIMScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' , clip_sample=_A , set_alpha_to_one=_A , ) snake_case_ : Any = DDIMInverseScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' , clip_sample=_A , set_alpha_to_zero=_A , ) torch.manual_seed(0 ) snake_case_ : Dict = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) snake_case_ : Dict = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='gelu' , projection_dim=512 , ) snake_case_ : List[str] = CLIPTextModel(_A ) snake_case_ : Optional[Any] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) snake_case_ : List[Any] = { 'unet': unet, 'scheduler': scheduler, 'inverse_scheduler': inverse_scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def UpperCAmelCase_ ( self : Optional[Any] , _A : Optional[int] , _A : List[Any]=0 ) -> Optional[int]: """simple docstring""" snake_case_ : Tuple = floats_tensor((1, 16, 16) , rng=random.Random(_A ) ).to(_A ) snake_case_ : Optional[Any] = floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(_A ) ).to(_A ) if str(_A ).startswith('mps' ): snake_case_ : Dict = torch.manual_seed(_A ) else: snake_case_ : int = torch.Generator(device=_A ).manual_seed(_A ) snake_case_ : str = { 'prompt': 'a dog and a newt', 'mask_image': mask, 'image_latents': latents, 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def UpperCAmelCase_ ( self : Tuple , _A : Any , _A : List[str]=0 ) -> int: """simple docstring""" snake_case_ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(_A ) ).to(_A ) snake_case_ : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case_ : Dict = Image.fromarray(np.uinta(_A ) ).convert('RGB' ) if str(_A ).startswith('mps' ): snake_case_ : List[Any] = torch.manual_seed(_A ) else: snake_case_ : Optional[Any] = torch.Generator(device=_A ).manual_seed(_A ) snake_case_ : Any = { 'image': image, 'source_prompt': 'a cat and a frog', 'target_prompt': 'a dog and a newt', 'generator': generator, 'num_inference_steps': 2, 'num_maps_per_mask': 2, 'mask_encode_strength': 1.0, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def UpperCAmelCase_ ( self : Optional[int] , _A : Any , _A : Dict=0 ) -> List[str]: """simple docstring""" snake_case_ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(_A ) ).to(_A ) snake_case_ : str = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case_ : List[str] = Image.fromarray(np.uinta(_A ) ).convert('RGB' ) if str(_A ).startswith('mps' ): snake_case_ : Tuple = torch.manual_seed(_A ) else: snake_case_ : Any = torch.Generator(device=_A ).manual_seed(_A ) snake_case_ : Dict = { 'image': image, 'prompt': 'a cat and a frog', 'generator': generator, 'num_inference_steps': 2, 'inpaint_strength': 1.0, 'guidance_scale': 6.0, 'decode_latents': True, 'output_type': 'numpy', } return inputs def UpperCAmelCase_ ( self : Tuple ) -> Tuple: """simple docstring""" if not hasattr(self.pipeline_class , '_optional_components' ): return snake_case_ : int = self.get_dummy_components() snake_case_ : Tuple = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(_A , _A , _A ) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components} ) snake_case_ : List[Any] = self.get_dummy_inputs(_A ) snake_case_ : Any = pipe(**_A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_A ) snake_case_ : int = self.pipeline_class.from_pretrained(_A ) pipe_loaded.to(_A ) pipe_loaded.set_progress_bar_config(disable=_A ) for optional_component in pipe._optional_components: self.assertTrue( getattr(_A , _A ) is None , F"""`{optional_component}` did not stay set to None after loading.""" , ) snake_case_ : Union[str, Any] = self.get_dummy_inputs(_A ) snake_case_ : str = pipe_loaded(**_A )[0] snake_case_ : int = np.abs(output - output_loaded ).max() self.assertLess(_A , 1E-4 ) def UpperCAmelCase_ ( self : Dict ) -> str: """simple docstring""" snake_case_ : Optional[Any] = 'cpu' snake_case_ : Tuple = self.get_dummy_components() snake_case_ : str = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) snake_case_ : str = self.get_dummy_mask_inputs(_A ) snake_case_ : str = pipe.generate_mask(**_A ) snake_case_ : Dict = mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16) ) snake_case_ : Tuple = np.array([0] * 9 ) snake_case_ : List[Any] = np.abs(mask_slice.flatten() - expected_slice ).max() self.assertLessEqual(_A , 1E-3 ) self.assertEqual(mask[0, -3, -4] , 0 ) def UpperCAmelCase_ ( self : Any ) -> Optional[int]: """simple docstring""" snake_case_ : List[Any] = 'cpu' snake_case_ : Union[str, Any] = self.get_dummy_components() snake_case_ : Optional[Any] = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) snake_case_ : Union[str, Any] = self.get_dummy_inversion_inputs(_A ) snake_case_ : List[Any] = pipe.invert(**_A ).images snake_case_ : Optional[int] = image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) snake_case_ : Dict = np.array( [0.5_1_5_0, 0.5_1_3_4, 0.5_0_4_3, 0.5_3_7_6, 0.4_6_9_4, 0.5_1_0_5_0, 0.5_0_1_5, 0.4_4_0_7, 0.4_7_9_9] , ) snake_case_ : Optional[int] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_A , 1E-3 ) def UpperCAmelCase_ ( self : List[Any] ) -> Dict: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=5E-3 ) def UpperCAmelCase_ ( self : List[str] ) -> List[Any]: """simple docstring""" snake_case_ : List[str] = 'cpu' snake_case_ : List[str] = self.get_dummy_components() snake_case_ : List[Any] = {'beta_start': 0.0_0_0_8_5, 'beta_end': 0.0_1_2, 'beta_schedule': 'scaled_linear'} snake_case_ : Optional[int] = DPMSolverMultistepScheduler(**_A ) snake_case_ : Optional[Any] = DPMSolverMultistepInverseScheduler(**_A ) snake_case_ : List[str] = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) snake_case_ : Dict = self.get_dummy_inversion_inputs(_A ) snake_case_ : str = pipe.invert(**_A ).images snake_case_ : Dict = image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) snake_case_ : Optional[int] = np.array( [0.5_1_5_0, 0.5_1_3_4, 0.5_0_4_3, 0.5_3_7_6, 0.4_6_9_4, 0.5_1_0_5_0, 0.5_0_1_5, 0.4_4_0_7, 0.4_7_9_9] , ) snake_case_ : List[str] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_A , 1E-3 ) @require_torch_gpu @slow class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): def UpperCAmelCase_ ( self : Dict ) -> Any: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def UpperCAmelCase_ ( cls : Optional[Any] ) -> int: """simple docstring""" snake_case_ : Optional[int] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png' ) snake_case_ : Tuple = raw_image.convert('RGB' ).resize((768, 768) ) snake_case_ : Dict = raw_image def UpperCAmelCase_ ( self : Any ) -> Optional[int]: """simple docstring""" snake_case_ : str = torch.manual_seed(0 ) snake_case_ : Any = StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=_A , torch_dtype=torch.floataa ) snake_case_ : Optional[Any] = DDIMScheduler.from_config(pipe.scheduler.config ) snake_case_ : Optional[int] = DDIMInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=_A ) snake_case_ : Dict = 'a bowl of fruit' snake_case_ : str = 'a bowl of pears' snake_case_ : str = pipe.generate_mask( image=self.raw_image , source_prompt=_A , target_prompt=_A , generator=_A , ) snake_case_ : Optional[Any] = pipe.invert( prompt=_A , image=self.raw_image , inpaint_strength=0.7 , generator=_A ).latents snake_case_ : Optional[Any] = pipe( prompt=_A , mask_image=_A , image_latents=_A , generator=_A , negative_prompt=_A , inpaint_strength=0.7 , output_type='numpy' , ).images[0] snake_case_ : Dict = ( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1 def UpperCAmelCase_ ( self : str ) -> Tuple: """simple docstring""" snake_case_ : List[Any] = torch.manual_seed(0 ) snake_case_ : Union[str, Any] = StableDiffusionDiffEditPipeline.from_pretrained( 'stabilityai/stable-diffusion-2-1' , safety_checker=_A , torch_dtype=torch.floataa ) snake_case_ : Optional[int] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) snake_case_ : Any = DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=_A ) snake_case_ : Dict = 'a bowl of fruit' snake_case_ : List[str] = 'a bowl of pears' snake_case_ : Optional[int] = pipe.generate_mask( image=self.raw_image , source_prompt=_A , target_prompt=_A , generator=_A , ) snake_case_ : List[Any] = pipe.invert( prompt=_A , image=self.raw_image , inpaint_strength=0.7 , generator=_A , num_inference_steps=25 , ).latents snake_case_ : str = pipe( prompt=_A , mask_image=_A , image_latents=_A , generator=_A , negative_prompt=_A , inpaint_strength=0.7 , num_inference_steps=25 , output_type='numpy' , ).images[0] snake_case_ : Optional[Any] = ( np.array( load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/diffedit/pears.png' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1
327
import sys _SCREAMING_SNAKE_CASE = ( """73167176531330624919225119674426574742355349194934""" """96983520312774506326239578318016984801869478851843""" """85861560789112949495459501737958331952853208805511""" """12540698747158523863050715693290963295227443043557""" """66896648950445244523161731856403098711121722383113""" """62229893423380308135336276614282806444486645238749""" """30358907296290491560440772390713810515859307960866""" """70172427121883998797908792274921901699720888093776""" """65727333001053367881220235421809751254540594752243""" """52584907711670556013604839586446706324415722155397""" """53697817977846174064955149290862569321978468622482""" """83972241375657056057490261407972968652414535100474""" """82166370484403199890008895243450658541227588666881""" """16427171479924442928230863465674813919123162824586""" """17866458359124566529476545682848912883142607690042""" """24219022671055626321111109370544217506941658960408""" """07198403850962455444362981230987879927244284909188""" """84580156166097919133875499200524063689912560717606""" """05886116467109405077541002256983155200055935729725""" """71636269561882670428252483600823257530420752963450""" ) def SCREAMING_SNAKE_CASE__ ( __a = N ): snake_case_ : Optional[Any] = -sys.maxsize - 1 for i in range(len(__a ) - 12 ): snake_case_ : Optional[Any] = 1 for j in range(13 ): product *= int(n[i + j] ) if product > largest_product: snake_case_ : int = product return largest_product if __name__ == "__main__": print(F'''{solution() = }''')
327
1
import re from filelock import FileLock try: import nltk _SCREAMING_SNAKE_CASE = True except (ImportError, ModuleNotFoundError): _SCREAMING_SNAKE_CASE = False if NLTK_AVAILABLE: with FileLock(""".lock""") as lock: nltk.download("""punkt""", quiet=True) def SCREAMING_SNAKE_CASE__ ( __a ): re.sub('<n>' , '' , __a ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(__a ) )
327
import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) _SCREAMING_SNAKE_CASE = logging.getLogger(__name__) @dataclass class SCREAMING_SNAKE_CASE_ : __magic_name__: str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) __magic_name__: Optional[str] = field( default=snake_case_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) __magic_name__: Optional[str] = field( default=snake_case_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) __magic_name__: Optional[str] = field( default=snake_case_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) __magic_name__: bool = field(default=snake_case_ , metadata={"help": "Whether tp freeze the encoder."} ) __magic_name__: bool = field(default=snake_case_ , metadata={"help": "Whether to freeze the embeddings."} ) @dataclass class SCREAMING_SNAKE_CASE_ : __magic_name__: str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) __magic_name__: Optional[str] = field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) __magic_name__: Optional[int] = field( default=1024 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __magic_name__: Optional[int] = field( default=128 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __magic_name__: Optional[int] = field( default=142 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) __magic_name__: Optional[int] = field( default=142 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __magic_name__: Optional[int] = field(default=-1 , metadata={"help": "# training examples. -1 means use all."} ) __magic_name__: Optional[int] = field(default=-1 , metadata={"help": "# validation examples. -1 means use all."} ) __magic_name__: Optional[int] = field(default=-1 , metadata={"help": "# test examples. -1 means use all."} ) __magic_name__: Optional[str] = field(default=snake_case_ , metadata={"help": "Source language id for translation."} ) __magic_name__: Optional[str] = field(default=snake_case_ , metadata={"help": "Target language id for translation."} ) __magic_name__: Optional[int] = field(default=snake_case_ , metadata={"help": "# num_beams to use for evaluation."} ) __magic_name__: bool = field( default=snake_case_ , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): logger.info(f"""***** {split} metrics *****""" ) for key in sorted(metrics.keys() ): logger.info(f""" {key} = {metrics[key]}""" ) save_json(__a , os.path.join(__a , f"""{split}_results.json""" ) ) def SCREAMING_SNAKE_CASE__ ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case_ : Any = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. snake_case_ ,snake_case_ ,snake_case_ : List[Any] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: snake_case_ ,snake_case_ ,snake_case_ : List[str] = parser.parse_args_into_dataclasses() check_output_dir(__a ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info('Training/evaluation parameters %s' , __a ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case_ : Tuple = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case_ : Any = ('encoder_layerdrop', 'decoder_layerdrop', 'dropout', 'attention_dropout') for p in extra_model_params: if getattr(__a , __a , __a ): assert hasattr(__a , __a ), f"""({config.__class__.__name__}) doesn't have a `{p}` attribute""" setattr(__a , __a , getattr(__a , __a ) ) snake_case_ : Tuple = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case_ : Any = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf='.ckpt' in model_args.model_name_or_path , config=__a , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(__a , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: snake_case_ : Any = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(__a , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(__a , __a ): snake_case_ : int = tokenizer.lang_code_to_id[data_args.tgt_lang] else: snake_case_ : int = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(__a ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) snake_case_ : List[Any] = SeqaSeqDataset # Get datasets snake_case_ : List[Any] = ( dataset_class( __a , type_path='train' , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_train else None ) snake_case_ : List[str] = ( dataset_class( __a , type_path='val' , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) snake_case_ : List[Any] = ( dataset_class( __a , type_path='test' , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_predict else None ) # Initialize our Trainer snake_case_ : Any = ( build_compute_metrics_fn(data_args.task , __a ) if training_args.predict_with_generate else None ) snake_case_ : List[str] = SeqaSeqTrainer( model=__a , args=__a , data_args=__a , train_dataset=__a , eval_dataset=__a , data_collator=SeqaSeqDataCollator( __a , __a , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=__a , tokenizer=__a , ) snake_case_ : Optional[int] = {} # Training if training_args.do_train: logger.info('*** Train ***' ) snake_case_ : Any = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) snake_case_ : Tuple = train_result.metrics snake_case_ : List[str] = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics('train' , __a , training_args.output_dir ) all_metrics.update(__a ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , 'trainer_state.json' ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case_ : List[Any] = trainer.evaluate(metric_key_prefix='val' ) snake_case_ : str = data_args.n_val snake_case_ : Union[str, Any] = round(metrics['val_loss'] , 4 ) if trainer.is_world_process_zero(): handle_metrics('val' , __a , training_args.output_dir ) all_metrics.update(__a ) if training_args.do_predict: logger.info('*** Predict ***' ) snake_case_ : Dict = trainer.predict(test_dataset=__a , metric_key_prefix='test' ) snake_case_ : Union[str, Any] = test_output.metrics snake_case_ : int = data_args.n_test if trainer.is_world_process_zero(): snake_case_ : List[str] = round(metrics['test_loss'] , 4 ) handle_metrics('test' , __a , training_args.output_dir ) all_metrics.update(__a ) if training_args.predict_with_generate: snake_case_ : Any = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=__a , clean_up_tokenization_spaces=__a ) snake_case_ : Any = lmap(str.strip , __a ) write_txt_file(__a , os.path.join(training_args.output_dir , 'test_generations.txt' ) ) if trainer.is_world_process_zero(): save_json(__a , os.path.join(training_args.output_dir , 'all_results.json' ) ) return all_metrics def SCREAMING_SNAKE_CASE__ ( __a ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
327
1
import warnings from .generation import TFGenerationMixin class SCREAMING_SNAKE_CASE_ ( snake_case_ ): # warning at import time warnings.warn( "Importing `TFGenerationMixin` from `src/transformers/generation_tf_utils.py` is deprecated and will " "be removed in Transformers v5. Import as `from transformers import TFGenerationMixin` instead." , snake_case_ , )
327
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available _SCREAMING_SNAKE_CASE = { """configuration_poolformer""": [ """POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PoolFormerConfig""", """PoolFormerOnnxConfig""", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = ["""PoolFormerFeatureExtractor"""] _SCREAMING_SNAKE_CASE = ["""PoolFormerImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ """POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """PoolFormerForImageClassification""", """PoolFormerModel""", """PoolFormerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_poolformer import ( POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, PoolFormerConfig, PoolFormerOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_poolformer import PoolFormerFeatureExtractor from .image_processing_poolformer import PoolFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_poolformer import ( POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, PoolFormerForImageClassification, PoolFormerModel, PoolFormerPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
327
1
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE = { """linear""": get_linear_schedule_with_warmup, """cosine""": get_cosine_schedule_with_warmup, """cosine_w_restarts""": get_cosine_with_hard_restarts_schedule_with_warmup, """polynomial""": get_polynomial_decay_schedule_with_warmup, """constant""": get_constant_schedule, """constant_w_warmup""": get_constant_schedule_with_warmup, } class SCREAMING_SNAKE_CASE_ ( snake_case_ ): def __init__( self : Optional[Any] , _A : Optional[int]=None , _A : Union[str, Any]=None , *_A : Tuple , **_A : Dict ) -> Optional[Any]: """simple docstring""" super().__init__(*_A , **_A ) if config is None: assert isinstance(self.model , _A ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F""" {self.model.__class__}""" ) snake_case_ : Tuple = self.model.config else: snake_case_ : Tuple = config snake_case_ : Optional[Any] = data_args snake_case_ : int = self.config.tgt_vocab_size if isinstance(self.config , _A ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F"""The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for""" ' padding..' ) if self.args.label_smoothing == 0: snake_case_ : Any = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss snake_case_ : Optional[Any] = label_smoothed_nll_loss def UpperCAmelCase_ ( self : Tuple , _A : int ) -> str: """simple docstring""" if self.optimizer is None: snake_case_ : List[Any] = ['bias', 'LayerNorm.weight'] snake_case_ : Tuple = [ { 'params': [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], 'weight_decay': self.args.weight_decay, }, { 'params': [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], 'weight_decay': 0.0, }, ] snake_case_ : Optional[int] = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: snake_case_ : int = Adafactor snake_case_ : List[str] = {'scale_parameter': False, 'relative_step': False} else: snake_case_ : Optional[Any] = AdamW snake_case_ : str = { 'betas': (self.args.adam_betaa, self.args.adam_betaa), 'eps': self.args.adam_epsilon, } snake_case_ : str = self.args.learning_rate if self.sharded_ddp: snake_case_ : Tuple = OSS( params=_A , optim=_A , **_A , ) else: snake_case_ : str = optimizer_cls(_A , **_A ) if self.lr_scheduler is None: snake_case_ : int = self._get_lr_scheduler(_A ) else: # ignoring --lr_scheduler logger.warning('scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.' ) def UpperCAmelCase_ ( self : Dict , _A : Optional[int] ) -> List[Any]: """simple docstring""" snake_case_ : Tuple = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": snake_case_ : Dict = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": snake_case_ : Tuple = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: snake_case_ : Tuple = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=_A ) return scheduler def UpperCAmelCase_ ( self : str ) -> Optional[torch.utils.data.Sampler]: """simple docstring""" if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def UpperCAmelCase_ ( self : Union[str, Any] , _A : List[str] , _A : Tuple , _A : Optional[Any] ) -> List[str]: """simple docstring""" if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token snake_case_ : List[Any] = model(**_A , use_cache=_A )[0] snake_case_ : Optional[Any] = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models snake_case_ ,snake_case_ : Dict = model(**_A , labels=_A , use_cache=_A )[:2] else: # compute label smoothed loss snake_case_ : List[str] = model(**_A , use_cache=_A )[0] snake_case_ : str = torch.nn.functional.log_softmax(_A , dim=-1 ) snake_case_ ,snake_case_ : List[Any] = self.loss_fn(_A , _A , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def UpperCAmelCase_ ( self : Optional[int] , _A : Optional[Any] , _A : List[Any] ) -> str: """simple docstring""" snake_case_ : Union[str, Any] = inputs.pop('labels' ) snake_case_ ,snake_case_ : int = self._compute_loss(_A , _A , _A ) return loss def UpperCAmelCase_ ( self : Optional[int] , _A : nn.Module , _A : Dict[str, Union[torch.Tensor, Any]] , _A : bool , _A : Optional[List[str]] = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: """simple docstring""" snake_case_ : Optional[int] = self._prepare_inputs(_A ) snake_case_ : Dict = { 'max_length': self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, 'num_beams': self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: snake_case_ : Optional[int] = self.model.generate( inputs['input_ids'] , attention_mask=inputs['attention_mask'] , **_A , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: snake_case_ : int = self._pad_tensors_to_max_len(_A , gen_kwargs['max_length'] ) snake_case_ : Tuple = inputs.pop('labels' ) with torch.no_grad(): # compute loss on predict data snake_case_ ,snake_case_ : List[Any] = self._compute_loss(_A , _A , _A ) snake_case_ : int = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) snake_case_ : int = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: snake_case_ : List[str] = self._pad_tensors_to_max_len(_A , gen_kwargs['max_length'] ) return (loss, logits, labels) def UpperCAmelCase_ ( self : Any , _A : List[str] , _A : Tuple ) -> List[Any]: """simple docstring""" snake_case_ : str = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( 'Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be' F""" padded to `max_length`={max_length}""" ) snake_case_ : Union[str, Any] = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) snake_case_ : int = tensor return padded_tensor
327
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 SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): def UpperCAmelCase_ ( self : Dict ) -> List[Any]: """simple docstring""" snake_case_ : Any = 'hf-internal-testing/tiny-random-t5' snake_case_ : Optional[Any] = AutoTokenizer.from_pretrained(_A ) snake_case_ : Optional[int] = AutoModelForSeqaSeqLM.from_pretrained(_A ) snake_case_ : List[Any] = tokenizer('This is me' , return_tensors='pt' ) snake_case_ : Any = model.to_bettertransformer() self.assertTrue(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules() ) ) snake_case_ : Optional[Any] = model.generate(**_A ) snake_case_ : int = 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(_A ) snake_case_ : Tuple = AutoModelForSeqaSeqLM.from_pretrained(_A ) self.assertFalse( any('BetterTransformer' in mod.__class__.__name__ for _, mod in model_reloaded.named_modules() ) ) snake_case_ : Optional[Any] = model_reloaded.generate(**_A ) self.assertTrue(torch.allclose(_A , _A ) ) def UpperCAmelCase_ ( self : Optional[Any] ) -> Tuple: """simple docstring""" snake_case_ : Any = 'hf-internal-testing/tiny-random-t5' snake_case_ : int = AutoModelForSeqaSeqLM.from_pretrained(_A ) snake_case_ : Dict = model.to_bettertransformer() with tempfile.TemporaryDirectory() as tmpdirname: with self.assertRaises(_A ): model.save_pretrained(_A ) snake_case_ : Union[str, Any] = model.reverse_bettertransformer() model.save_pretrained(_A )
327
1
from ..utils import DummyObject, requires_backends class SCREAMING_SNAKE_CASE_ ( metaclass=snake_case_ ): __magic_name__: List[str] = ["transformers", "torch", "note_seq"] def __init__( self : List[str] , *_A : List[Any] , **_A : int ) -> str: """simple docstring""" requires_backends(self , ['transformers', 'torch', 'note_seq'] ) @classmethod def UpperCAmelCase_ ( cls : List[str] , *_A : Optional[int] , **_A : int ) -> Optional[int]: """simple docstring""" requires_backends(cls , ['transformers', 'torch', 'note_seq'] ) @classmethod def UpperCAmelCase_ ( cls : Optional[int] , *_A : List[Any] , **_A : Optional[Any] ) -> List[Any]: """simple docstring""" requires_backends(cls , ['transformers', 'torch', 'note_seq'] )
327
import numpy as np import torch from torch.utils.data import Dataset from utils import logger class SCREAMING_SNAKE_CASE_ ( snake_case_ ): def __init__( self : Union[str, Any] , _A : Any , _A : Dict ) -> Union[str, Any]: """simple docstring""" snake_case_ : str = params snake_case_ : int = np.array(_A ) snake_case_ : Optional[int] = np.array([len(_A ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self : Tuple , _A : Optional[int] ) -> str: """simple docstring""" return (self.token_ids[index], self.lengths[index]) def __len__( self : List[str] ) -> str: """simple docstring""" return len(self.lengths ) def UpperCAmelCase_ ( self : Dict ) -> str: """simple docstring""" assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def UpperCAmelCase_ ( self : Any ) -> Optional[Any]: """simple docstring""" snake_case_ : Dict = self.params.max_model_input_size snake_case_ : Tuple = self.lengths > max_len logger.info(F"""Splitting {sum(_A )} too long sequences.""" ) def divide_chunks(_A : Union[str, Any] , _A : Dict ): return [l[i : i + n] for i in range(0 , len(_A ) , _A )] snake_case_ : Dict = [] snake_case_ : Union[str, Any] = [] if self.params.mlm: snake_case_ ,snake_case_ : Optional[int] = self.params.special_tok_ids['cls_token'], self.params.special_tok_ids['sep_token'] else: snake_case_ ,snake_case_ : Any = self.params.special_tok_ids['bos_token'], self.params.special_tok_ids['eos_token'] for seq_, len_ in zip(self.token_ids , self.lengths ): assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_ if len_ <= max_len: new_tok_ids.append(seq_ ) new_lengths.append(len_ ) else: snake_case_ : List[Any] = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: snake_case_ : Optional[int] = np.insert(_A , 0 , _A ) if sub_s[-1] != sep_id: snake_case_ : Optional[Any] = np.insert(_A , len(_A ) , _A ) assert len(_A ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(_A ) new_tok_ids.extend(_A ) new_lengths.extend([len(_A ) for l in sub_seqs] ) snake_case_ : Tuple = np.array(_A ) snake_case_ : int = np.array(_A ) def UpperCAmelCase_ ( self : List[str] ) -> List[str]: """simple docstring""" snake_case_ : Tuple = len(self ) snake_case_ : int = self.lengths > 11 snake_case_ : Dict = self.token_ids[indices] snake_case_ : int = self.lengths[indices] snake_case_ : List[Any] = len(self ) logger.info(F"""Remove {init_size - new_size} too short (<=11 tokens) sequences.""" ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> int: """simple docstring""" if "unk_token" not in self.params.special_tok_ids: return else: snake_case_ : Optional[Any] = self.params.special_tok_ids['unk_token'] snake_case_ : Dict = len(self ) snake_case_ : str = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) snake_case_ : Any = (unk_occs / self.lengths) < 0.5 snake_case_ : List[Any] = self.token_ids[indices] snake_case_ : int = self.lengths[indices] snake_case_ : Tuple = len(self ) logger.info(F"""Remove {init_size - new_size} sequences with a high level of unknown tokens (50%).""" ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Any: """simple docstring""" if not self.params.is_master: return logger.info(F"""{len(self )} sequences""" ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def UpperCAmelCase_ ( self : Optional[int] , _A : Union[str, Any] ) -> List[Any]: """simple docstring""" snake_case_ : Any = [t[0] for t in batch] snake_case_ : int = [t[1] for t in batch] assert len(_A ) == len(_A ) # Max for paddings snake_case_ : str = max(_A ) # Pad token ids if self.params.mlm: snake_case_ : int = self.params.special_tok_ids['pad_token'] else: snake_case_ : Dict = self.params.special_tok_ids['unk_token'] snake_case_ : Dict = [list(t.astype(_A ) ) + [pad_idx] * (max_seq_len_ - len(_A )) for t in token_ids] assert len(tk_ ) == len(_A ) assert all(len(_A ) == max_seq_len_ for t in tk_ ) snake_case_ : Any = torch.tensor(tk_ ) # (bs, max_seq_len_) snake_case_ : Optional[Any] = torch.tensor(_A ) # (bs) return tk_t, lg_t
327
1
import numpy as np import qiskit def SCREAMING_SNAKE_CASE__ ( __a = 8 , __a = None ): snake_case_ : str = np.random.default_rng(seed=__a ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. snake_case_ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. snake_case_ : Optional[Any] = rng.integers(2 , size=__a ) # The set of states Alice will prepare. snake_case_ : str = rng.integers(2 , size=__a ) # Measurement basis for Bob's qubits. snake_case_ : str = rng.integers(2 , size=__a ) # Quantum Circuit to simulate BB84 snake_case_ : str = qiskit.QuantumCircuit(__a , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(__a ): if alice_state[index] == 1: bbaa_circ.x(__a ) if alice_basis[index] == 1: bbaa_circ.h(__a ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(__a ): if bob_basis[index] == 1: bbaa_circ.h(__a ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. snake_case_ : Optional[Any] = qiskit.Aer.get_backend('aer_simulator' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. snake_case_ : List[str] = qiskit.execute(__a , __a , shots=1 , seed_simulator=__a ) # Returns the result of measurement. snake_case_ : Union[str, Any] = job.result().get_counts(__a ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. snake_case_ : int = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( __a , __a , __a ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. snake_case_ : Dict = gen_key[:key_len] if len(__a ) >= key_len else gen_key.ljust(__a , '0' ) return key if __name__ == "__main__": print(F'''The generated key is : {bbaa(8, seed=0)}''') from doctest import testmod testmod()
327
def SCREAMING_SNAKE_CASE__ ( __a , __a ): while b: snake_case_ ,snake_case_ : Any = b, a % b return a def SCREAMING_SNAKE_CASE__ ( __a , __a ): return a if b == 0 else euclidean_gcd_recursive(__a , a % b ) def SCREAMING_SNAKE_CASE__ ( ): print(f"""euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}""" ) print(f"""euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}""" ) print(f"""euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}""" ) print(f"""euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}""" ) print(f"""euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}""" ) print(f"""euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}""" ) print(f"""euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}""" ) print(f"""euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}""" ) print(f"""euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}""" ) print(f"""euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}""" ) if __name__ == "__main__": main()
327
1
import argparse import json import os import tensorstore as ts import torch from flax import serialization from flax.traverse_util import flatten_dict, unflatten_dict from tensorflow.io import gfile from transformers.modeling_utils import dtype_byte_size from transformers.models.switch_transformers.convert_switch_transformers_original_flax_checkpoint_to_pytorch import ( rename_keys, ) from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME from transformers.utils.hub import convert_file_size_to_int def SCREAMING_SNAKE_CASE__ ( __a , __a ): if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 3: # expert layer snake_case_ : List[Any] = flax_key_tuple[:-1] + ('weight',) snake_case_ : Tuple = torch.permute(__a , (0, 2, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(__a ): # linear layer snake_case_ : int = flax_key_tuple[:-1] + ('weight',) snake_case_ : Optional[Any] = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: snake_case_ : List[str] = flax_key_tuple[:-1] + ('weight',) return flax_key_tuple, flax_tensor def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): if "metadata" in layer: snake_case_ : str = layer.split('metadata' ) snake_case_ : Optional[int] = ''.join(split_layer[0] )[:-1] snake_case_ : Union[str, Any] = [tuple(('metadata' + split_layer[1]).split('/' ) )] elif "kvstore" in layer: snake_case_ : str = layer.split('kvstore' ) snake_case_ : List[str] = ''.join(split_layer[0] )[:-1] snake_case_ : Optional[Any] = [tuple(('kvstore' + split_layer[1]).split('/' ) )] else: snake_case_ : Any = layer.split('/' ) snake_case_ : List[str] = '/'.join(split_layer[:-1] ) snake_case_ : str = (split_layer[-1],) if "kvstore/path" in layer: snake_case_ : Union[str, Any] = f"""{switch_checkpoint_path}/{checkpoint_info[layer]}""" elif "kvstore/driver" in layer: snake_case_ : List[str] = 'file' else: snake_case_ : Dict = checkpoint_info[layer] return curr_real_layer_name, split_layer, content def SCREAMING_SNAKE_CASE__ ( __a , __a ): snake_case_ : Any = rename_keys(__a ) snake_case_ : Union[str, Any] = {} for k, v in current_block.items(): snake_case_ : Union[str, Any] = v snake_case_ : Any = new_current_block torch.save(__a , __a ) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a , __a , __a = WEIGHTS_NAME ): snake_case_ : str = convert_file_size_to_int(__a ) snake_case_ : Optional[Any] = [] snake_case_ : List[str] = {} snake_case_ : Tuple = 0 snake_case_ : Optional[Any] = 0 os.makedirs(__a , exist_ok=__a ) with gfile.GFile(switch_checkpoint_path + '/checkpoint' , 'rb' ) as fp: snake_case_ : Union[str, Any] = serialization.msgpack_restore(fp.read() )['optimizer']['target'] snake_case_ : Optional[Any] = flatten_dict(__a , sep='/' ) snake_case_ : Tuple = {} for layer in checkpoint_info.keys(): snake_case_ ,snake_case_ ,snake_case_ : str = get_key_and_tensorstore_dict( __a , __a , __a ) if curr_real_layer_name in all_layers: snake_case_ : str = content else: snake_case_ : Optional[int] = {split_layer[-1]: content} for key in all_layers.keys(): # open tensorstore file snake_case_ : Dict = ts.open(unflatten_dict(all_layers[key] ) ).result().read().result() snake_case_ : Tuple = torch.tensor(__a ) snake_case_ : Tuple = raw_weights.numel() * dtype_byte_size(raw_weights.dtype ) # use the renaming pattern from the small conversion scripts snake_case_ ,snake_case_ : Optional[Any] = rename_base_flax_keys(tuple(key.split('/' ) ) , __a ) snake_case_ : Optional[int] = '/'.join(__a ) # If this weight is going to tip up over the maximal size, we split. if current_block_size + weight_size > max_shard_size: snake_case_ : Optional[Any] = os.path.join( __a , weights_name.replace('.bin' , f"""-{len(__a )+1:05d}-of-???.bin""" ) ) rename_and_save_block(__a , __a ) sharded_state_dicts.append(current_block.keys() ) del current_block snake_case_ : Any = {} snake_case_ : Tuple = 0 snake_case_ : List[str] = raw_weights.to(getattr(__a , __a ) ) current_block_size += weight_size total_size += weight_size # Add the last block snake_case_ : List[str] = os.path.join(__a , weights_name.replace('.bin' , f"""-{len(__a )+1:05d}-of-???.bin""" ) ) rename_and_save_block(__a , __a ) sharded_state_dicts.append(current_block.keys() ) # If we only have one shard, we return it if len(__a ) == 1: return {weights_name: sharded_state_dicts[0]}, None # Otherwise, let's build the index snake_case_ : Optional[Any] = {} snake_case_ : List[Any] = {} for idx, shard in enumerate(__a ): snake_case_ : Dict = weights_name.replace( '.bin' , f"""-{idx+1:05d}-of-{len(__a ):05d}.bin""" ) # len(sharded_state_dicts):05d} snake_case_ : List[Any] = os.path.join(__a , weights_name.replace('.bin' , f"""-{idx+1:05d}-of-???.bin""" ) ) os.rename(__a , os.path.join(__a , __a ) ) snake_case_ : List[str] = shard for key in shard: snake_case_ : Optional[Any] = shard_file # Add the metadata snake_case_ : str = {'total_size': total_size} snake_case_ : Any = {'metadata': metadata, 'weight_map': weight_map} with open(os.path.join(__a , __a ) , 'w' , encoding='utf-8' ) as f: snake_case_ : Tuple = json.dumps(__a , indent=2 , sort_keys=__a ) + '\n' f.write(__a ) return metadata, index if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( """--switch_t5x_checkpoint_path""", default="""/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128/checkpoint_634600""", type=str, required=False, help="""Path to a directory containing a folder per layer. Follows the original Google format.""", ) parser.add_argument("""--max_shard_size""", default="""10GB""", required=False, help="""Max shard size""") parser.add_argument("""--dtype""", default="""bfloat16""", type=str, required=False, help="""dtype of the saved model""") parser.add_argument( """--pytorch_dump_folder_path""", default="""/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128-converted""", type=str, required=False, help="""Path to the output pytorch model.""", ) _SCREAMING_SNAKE_CASE = parser.parse_args() shard_on_the_fly( args.switch_tax_checkpoint_path, args.pytorch_dump_folder_path, args.max_shard_size, args.dtype, ) def SCREAMING_SNAKE_CASE__ ( ): from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration, TaTokenizer snake_case_ : int = SwitchTransformersConfig.from_pretrained('google/switch-base-8' ) config.save_pretrained('/home/arthur_huggingface_co/transformers/switch_converted' ) snake_case_ : Optional[int] = SwitchTransformersForConditionalGeneration.from_pretrained( '/home/arthur_huggingface_co/transformers/switch_converted' , device_map='auto' ) snake_case_ : List[str] = TaTokenizer.from_pretrained('t5-small' ) snake_case_ : List[Any] = 'A <extra_id_0> walks into a bar a orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.' snake_case_ : Union[str, Any] = tokenizer(__a , return_tensors='pt' ).input_ids snake_case_ : Dict = model.generate(__a , decoder_start_token_id=0 ) print(tokenizer.decode(out[0] ) )
327
import os import torch from ..logging import get_logger from .constants import FSDP_PYTORCH_VERSION, MODEL_NAME, OPTIMIZER_NAME from .versions import is_torch_version if is_torch_version(""">=""", FSDP_PYTORCH_VERSION): import torch.distributed.checkpoint as dist_cp from torch.distributed.checkpoint.default_planner import DefaultLoadPlanner, DefaultSavePlanner from torch.distributed.checkpoint.optimizer import load_sharded_optimizer_state_dict from torch.distributed.fsdp.fully_sharded_data_parallel import FullyShardedDataParallel as FSDP from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType _SCREAMING_SNAKE_CASE = get_logger(__name__) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a , __a , __a=0 ): os.makedirs(__a , exist_ok=__a ) with FSDP.state_dict_type( __a , fsdp_plugin.state_dict_type , fsdp_plugin.state_dict_config , fsdp_plugin.optim_state_dict_config ): snake_case_ : Dict = model.state_dict() if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT: snake_case_ : Optional[int] = f"""{MODEL_NAME}.bin""" if model_index == 0 else f"""{MODEL_NAME}_{model_index}.bin""" snake_case_ : Dict = os.path.join(__a , __a ) if accelerator.process_index == 0: logger.info(f"""Saving model to {output_model_file}""" ) torch.save(__a , __a ) logger.info(f"""Model saved to {output_model_file}""" ) elif fsdp_plugin.state_dict_type == StateDictType.LOCAL_STATE_DICT: snake_case_ : Dict = ( f"""{MODEL_NAME}_rank{accelerator.process_index}.bin""" if model_index == 0 else f"""{MODEL_NAME}_{model_index}_rank{accelerator.process_index}.bin""" ) snake_case_ : Dict = os.path.join(__a , __a ) logger.info(f"""Saving model to {output_model_file}""" ) torch.save(__a , __a ) logger.info(f"""Model saved to {output_model_file}""" ) elif fsdp_plugin.state_dict_type == StateDictType.SHARDED_STATE_DICT: snake_case_ : Optional[int] = os.path.join(__a , f"""{MODEL_NAME}_{model_index}""" ) os.makedirs(__a , exist_ok=__a ) logger.info(f"""Saving model to {ckpt_dir}""" ) snake_case_ : int = {'model': state_dict} dist_cp.save_state_dict( state_dict=__a , storage_writer=dist_cp.FileSystemWriter(__a ) , planner=DefaultSavePlanner() , ) logger.info(f"""Model saved to {ckpt_dir}""" ) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a , __a , __a=0 ): accelerator.wait_for_everyone() with FSDP.state_dict_type( __a , fsdp_plugin.state_dict_type , fsdp_plugin.state_dict_config , fsdp_plugin.optim_state_dict_config ): if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT: if type(__a ) != FSDP and accelerator.process_index != 0: if not fsdp_plugin.sync_module_states: raise ValueError( 'Set the `sync_module_states` flag to `True` so that model states are synced across processes when ' 'initializing FSDP object' ) return snake_case_ : Optional[int] = f"""{MODEL_NAME}.bin""" if model_index == 0 else f"""{MODEL_NAME}_{model_index}.bin""" snake_case_ : Optional[Any] = os.path.join(__a , __a ) logger.info(f"""Loading model from {input_model_file}""" ) snake_case_ : Optional[Any] = torch.load(__a ) logger.info(f"""Model loaded from {input_model_file}""" ) elif fsdp_plugin.state_dict_type == StateDictType.LOCAL_STATE_DICT: snake_case_ : Optional[Any] = ( f"""{MODEL_NAME}_rank{accelerator.process_index}.bin""" if model_index == 0 else f"""{MODEL_NAME}_{model_index}_rank{accelerator.process_index}.bin""" ) snake_case_ : Tuple = os.path.join(__a , __a ) logger.info(f"""Loading model from {input_model_file}""" ) snake_case_ : Optional[int] = torch.load(__a ) logger.info(f"""Model loaded from {input_model_file}""" ) elif fsdp_plugin.state_dict_type == StateDictType.SHARDED_STATE_DICT: snake_case_ : Tuple = ( os.path.join(__a , f"""{MODEL_NAME}_{model_index}""" ) if f"""{MODEL_NAME}""" not in input_dir else input_dir ) logger.info(f"""Loading model from {ckpt_dir}""" ) snake_case_ : List[Any] = {'model': model.state_dict()} dist_cp.load_state_dict( state_dict=__a , storage_reader=dist_cp.FileSystemReader(__a ) , planner=DefaultLoadPlanner() , ) snake_case_ : Any = state_dict['model'] logger.info(f"""Model loaded from {ckpt_dir}""" ) model.load_state_dict(__a ) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a , __a , __a , __a=0 ): os.makedirs(__a , exist_ok=__a ) with FSDP.state_dict_type( __a , fsdp_plugin.state_dict_type , fsdp_plugin.state_dict_config , fsdp_plugin.optim_state_dict_config ): snake_case_ : List[str] = FSDP.optim_state_dict(__a , __a ) if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT: if accelerator.process_index == 0: snake_case_ : str = ( f"""{OPTIMIZER_NAME}.bin""" if optimizer_index == 0 else f"""{OPTIMIZER_NAME}_{optimizer_index}.bin""" ) snake_case_ : Any = os.path.join(__a , __a ) logger.info(f"""Saving Optimizer state to {output_optimizer_file}""" ) torch.save(__a , __a ) logger.info(f"""Optimizer state saved in {output_optimizer_file}""" ) else: snake_case_ : Optional[int] = os.path.join(__a , f"""{OPTIMIZER_NAME}_{optimizer_index}""" ) os.makedirs(__a , exist_ok=__a ) logger.info(f"""Saving Optimizer state to {ckpt_dir}""" ) dist_cp.save_state_dict( state_dict={'optimizer': optim_state} , storage_writer=dist_cp.FileSystemWriter(__a ) , planner=DefaultSavePlanner() , ) logger.info(f"""Optimizer state saved in {ckpt_dir}""" ) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a , __a , __a , __a=0 ): accelerator.wait_for_everyone() with FSDP.state_dict_type( __a , fsdp_plugin.state_dict_type , fsdp_plugin.state_dict_config , fsdp_plugin.optim_state_dict_config ): if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT: snake_case_ : Optional[Any] = None # below check should work but currently it isn't working (mostly opytorch issue), # in the meantime disabling it at the cost of excess memory usage # if accelerator.process_index == 0 or not fsdp_plugin.optim_state_dict_config.rank0_only: snake_case_ : Union[str, Any] = ( f"""{OPTIMIZER_NAME}.bin""" if optimizer_index == 0 else f"""{OPTIMIZER_NAME}_{optimizer_index}.bin""" ) snake_case_ : List[Any] = os.path.join(__a , __a ) logger.info(f"""Loading Optimizer state from {input_optimizer_file}""" ) snake_case_ : Optional[int] = torch.load(__a ) logger.info(f"""Optimizer state loaded from {input_optimizer_file}""" ) else: snake_case_ : str = ( os.path.join(__a , f"""{OPTIMIZER_NAME}_{optimizer_index}""" ) if f"""{OPTIMIZER_NAME}""" not in input_dir else input_dir ) logger.info(f"""Loading Optimizer from {ckpt_dir}""" ) snake_case_ : Any = load_sharded_optimizer_state_dict( model_state_dict=model.state_dict() , optimizer_key='optimizer' , storage_reader=dist_cp.FileSystemReader(__a ) , ) snake_case_ : Optional[int] = optim_state['optimizer'] logger.info(f"""Optimizer loaded from {ckpt_dir}""" ) snake_case_ : Optional[Any] = FSDP.optim_state_dict_to_load(__a , __a , __a ) optimizer.load_state_dict(__a )
327
1
import os import pytest from transformers.dynamic_module_utils import get_imports _SCREAMING_SNAKE_CASE = """ import os """ _SCREAMING_SNAKE_CASE = """ def foo(): import os return False """ _SCREAMING_SNAKE_CASE = """ def foo(): def bar(): if True: import os return False return bar() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar except ImportError: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os def foo(): try: import bar except ImportError: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar except (ImportError, AttributeError): raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar except ImportError as e: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar except: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar import baz except ImportError: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar import baz except ImportError: x = 1 raise ValueError() """ _SCREAMING_SNAKE_CASE = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('case' , __a ) def SCREAMING_SNAKE_CASE__ ( __a , __a ): snake_case_ : List[str] = os.path.join(__a , 'test_file.py' ) with open(__a , 'w' ) as _tmp_file: _tmp_file.write(__a ) snake_case_ : Optional[Any] = get_imports(__a ) assert parsed_imports == ["os"]
327
import unittest from typing import Dict, List, Optional, Union 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 BridgeTowerImageProcessor class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): def __init__( self : List[str] , _A : List[Any] , _A : bool = True , _A : Dict[str, int] = None , _A : int = 32 , _A : bool = True , _A : Union[int, float] = 1 / 255 , _A : bool = True , _A : bool = True , _A : Optional[Union[float, List[float]]] = [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3] , _A : Optional[Union[float, List[float]]] = [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1] , _A : bool = True , _A : Tuple=7 , _A : Tuple=30 , _A : int=400 , _A : Tuple=3 , ) -> Optional[int]: """simple docstring""" snake_case_ : str = parent snake_case_ : str = do_resize snake_case_ : str = size if size is not None else {'shortest_edge': 288} snake_case_ : Any = size_divisor snake_case_ : Any = do_rescale snake_case_ : Union[str, Any] = rescale_factor snake_case_ : str = do_normalize snake_case_ : int = do_center_crop snake_case_ : str = image_mean snake_case_ : int = image_std snake_case_ : Any = do_pad snake_case_ : Optional[int] = batch_size snake_case_ : List[str] = num_channels snake_case_ : Any = min_resolution snake_case_ : str = max_resolution def UpperCAmelCase_ ( self : Tuple ) -> Optional[int]: """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, "size_divisor": self.size_divisor, } def UpperCAmelCase_ ( self : Dict , _A : str , _A : Union[str, Any]=False ) -> int: """simple docstring""" if not batched: snake_case_ : Optional[int] = self.size['shortest_edge'] snake_case_ : List[Any] = image_inputs[0] if isinstance(_A , Image.Image ): snake_case_ ,snake_case_ : Optional[Any] = image.size else: snake_case_ ,snake_case_ : str = image.shape[1], image.shape[2] snake_case_ : Dict = size / min(_A , _A ) if h < w: snake_case_ ,snake_case_ : str = size, scale * w else: snake_case_ ,snake_case_ : Tuple = scale * h, size snake_case_ : Dict = int((1333 / 800) * size ) if max(_A , _A ) > max_size: snake_case_ : Union[str, Any] = max_size / max(_A , _A ) snake_case_ : Any = newh * scale snake_case_ : Union[str, Any] = neww * scale snake_case_ ,snake_case_ : Any = int(newh + 0.5 ), int(neww + 0.5 ) snake_case_ ,snake_case_ : int = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: snake_case_ : Optional[int] = [] for image in image_inputs: snake_case_ ,snake_case_ : Optional[int] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) snake_case_ : str = max(_A , key=lambda _A : item[0] )[0] snake_case_ : List[str] = max(_A , key=lambda _A : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class SCREAMING_SNAKE_CASE_ ( snake_case_ , unittest.TestCase ): __magic_name__: List[Any] = BridgeTowerImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self : List[Any] ) -> Tuple: """simple docstring""" snake_case_ : int = BridgeTowerImageProcessingTester(self ) @property def UpperCAmelCase_ ( self : int ) -> Any: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" snake_case_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_A , 'image_mean' ) ) self.assertTrue(hasattr(_A , 'image_std' ) ) self.assertTrue(hasattr(_A , 'do_normalize' ) ) self.assertTrue(hasattr(_A , 'do_resize' ) ) self.assertTrue(hasattr(_A , 'size' ) ) self.assertTrue(hasattr(_A , 'size_divisor' ) ) def UpperCAmelCase_ ( self : Optional[int] ) -> List[str]: """simple docstring""" pass def UpperCAmelCase_ ( self : Tuple ) -> Tuple: """simple docstring""" snake_case_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_A ) for image in image_inputs: self.assertIsInstance(_A , Image.Image ) # Test not batched input snake_case_ : Dict = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Optional[Any] = self.image_processor_tester.get_expected_values(_A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case_ : List[str] = image_processing(_A , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Optional[Any] = self.image_processor_tester.get_expected_values(_A , batched=_A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Any: """simple docstring""" snake_case_ : int = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=_A , numpify=_A ) for image in image_inputs: self.assertIsInstance(_A , np.ndarray ) # Test not batched input snake_case_ : Union[str, Any] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Tuple = self.image_processor_tester.get_expected_values(_A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case_ : Any = image_processing(_A , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Any = self.image_processor_tester.get_expected_values(_A , batched=_A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCAmelCase_ ( self : Dict ) -> List[str]: """simple docstring""" snake_case_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_A , torchify=_A ) for image in image_inputs: self.assertIsInstance(_A , torch.Tensor ) # Test not batched input snake_case_ : Optional[int] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Optional[Any] = self.image_processor_tester.get_expected_values(_A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case_ : str = image_processing(_A , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Tuple = self.image_processor_tester.get_expected_values(_A , batched=_A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
327
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _SCREAMING_SNAKE_CASE = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = ["""MLukeTokenizer"""] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
327
import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration _SCREAMING_SNAKE_CASE = 50_00_00 _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = os.path.split(__file__) _SCREAMING_SNAKE_CASE = os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json""")) @get_duration def SCREAMING_SNAKE_CASE__ ( __a , **__a ): snake_case_ : int = dataset.map(**__a ) @get_duration def SCREAMING_SNAKE_CASE__ ( __a , **__a ): snake_case_ : Dict = dataset.filter(**__a ) def SCREAMING_SNAKE_CASE__ ( ): snake_case_ : Tuple = {'num examples': SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ : Dict = datasets.Features({'text': datasets.Value('string' ), 'numbers': datasets.Value('float32' )} ) snake_case_ : List[Any] = generate_example_dataset( os.path.join(__a , 'dataset.arrow' ) , __a , num_examples=__a ) snake_case_ : str = transformers.AutoTokenizer.from_pretrained('bert-base-cased' , use_fast=__a ) def tokenize(__a ): return tokenizer(examples['text'] ) snake_case_ : Any = map(__a ) snake_case_ : Tuple = map(__a , batched=__a ) snake_case_ : str = map(__a , function=lambda __a : None , batched=__a ) with dataset.formatted_as(type='numpy' ): snake_case_ : Optional[int] = map(__a , function=lambda __a : None , batched=__a ) with dataset.formatted_as(type='pandas' ): snake_case_ : str = map(__a , function=lambda __a : None , batched=__a ) with dataset.formatted_as(type='torch' , columns='numbers' ): snake_case_ : int = map(__a , function=lambda __a : None , batched=__a ) with dataset.formatted_as(type='tensorflow' , columns='numbers' ): snake_case_ : List[Any] = map(__a , function=lambda __a : None , batched=__a ) snake_case_ : int = map(__a , function=__a , batched=__a ) snake_case_ : Optional[Any] = filter(__a ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(__a , 'wb' ) as f: f.write(json.dumps(__a ).encode('utf-8' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
327
1
import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): snake_case_ : Tuple = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(__a ) == len(__a ), f"""{len(__a )} != {len(__a )}""" dest_layers.load_state_dict(layers_to_copy.state_dict() ) _SCREAMING_SNAKE_CASE = { # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 12: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 11], 4: [0, 4, 8, 11], 6: [0, 2, 4, 7, 9, 11], 9: [0, 1, 2, 4, 5, 7, 9, 10, 11], 12: list(range(12)), }, 16: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 15], 3: [0, 8, 15], 4: [0, 5, 10, 15], 6: [0, 3, 6, 9, 12, 15], 8: [0, 2, 4, 6, 8, 10, 12, 15], 9: [0, 1, 3, 5, 7, 9, 11, 13, 15], 12: [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 15], 16: list(range(16)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } _SCREAMING_SNAKE_CASE = { # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 12: {1: [11], 2: [5, 11], 3: [3, 7, 11], 6: [1, 3, 5, 8, 10, 11]}, 16: {1: [15], 4: [4, 9, 12, 15], 8: [1, 3, 5, 7, 9, 11, 13, 15]}, } def SCREAMING_SNAKE_CASE__ ( __a , __a ): try: snake_case_ : Any = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( f"""no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first""" f""" {n_student}""" ) return list(range(__a ) ) def SCREAMING_SNAKE_CASE__ ( __a , __a ): if n_student > n_teacher: raise ValueError(f"""Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}""" ) elif n_teacher == n_student: return list(range(__a ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def SCREAMING_SNAKE_CASE__ ( __a , __a = "student" , __a = None , __a = None , __a=False , __a=None , __a=None , **__a , ): snake_case_ : Tuple = 'encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.' assert (e is not None) or (d is not None), _msg if isinstance(__a , __a ): AutoTokenizer.from_pretrained(__a ).save_pretrained(__a ) # purely for convenience snake_case_ : str = AutoModelForSeqaSeqLM.from_pretrained(__a ).eval() else: assert isinstance(__a , __a ), f"""teacher must be a model or string got type {type(__a )}""" snake_case_ : Optional[int] = teacher.config.to_diff_dict() try: snake_case_ ,snake_case_ : Dict = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: snake_case_ : Dict = teacher_e if d is None: snake_case_ : Optional[int] = teacher_d init_kwargs.update({'encoder_layers': e, 'decoder_layers': d} ) except AttributeError: # T5 if hasattr(teacher.config , 'num_encoder_layers' ): snake_case_ ,snake_case_ : List[Any] = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: snake_case_ ,snake_case_ : Optional[int] = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: snake_case_ : Tuple = teacher_e if d is None: snake_case_ : List[str] = teacher_d if hasattr(teacher.config , 'num_encoder_layers' ): init_kwargs.update({'num_encoder_layers': e, 'num_decoder_layers': d} ) else: init_kwargs.update({'num_layers': e, 'num_decoder_layers': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(__a ) # Copy weights snake_case_ : List[str] = teacher.config_class(**__a ) snake_case_ : str = AutoModelForSeqaSeqLM.from_config(__a ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. snake_case_ : List[Any] = student.load_state_dict(teacher.state_dict() , strict=__a ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save snake_case_ ,snake_case_ : Union[str, Any] = list(range(__a ) ), list(range(__a ) ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to""" f""" {save_path}""" ) student.save_pretrained(__a ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: snake_case_ : List[int] = pick_layers_to_copy(__a , __a ) if d_layers_to_copy is None: snake_case_ : List[int] = pick_layers_to_copy(__a , __a ) try: if hasattr( __a , 'prophetnet' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , __a ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , __a ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , __a ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , __a ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , __a ) copy_layers(teacher.decoder.block , student.decoder.block , __a ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}""" ) snake_case_ : Any = { 'teacher_type': teacher.config.model_type, 'copied_encoder_layers': e_layers_to_copy, 'copied_decoder_layers': d_layers_to_copy, } student.save_pretrained(__a ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
327
from collections import namedtuple import requests from lxml import html # type: ignore _SCREAMING_SNAKE_CASE = namedtuple("""covid_data""", """cases deaths recovered""") def SCREAMING_SNAKE_CASE__ ( __a = "https://www.worldometers.info/coronavirus/" ): snake_case_ : Union[str, Any] = '//div[@class = "maincounter-number"]/span/text()' return covid_data(*html.fromstring(requests.get(__a ).content ).xpath(__a ) ) _SCREAMING_SNAKE_CASE = """Total COVID-19 cases in the world: {} Total deaths due to COVID-19 in the world: {} Total COVID-19 patients recovered in the world: {}""" print(fmt.format(*covid_stats()))
327
1
import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration _SCREAMING_SNAKE_CASE = 50_00_00 _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = os.path.split(__file__) _SCREAMING_SNAKE_CASE = os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json""")) @get_duration def SCREAMING_SNAKE_CASE__ ( __a , **__a ): snake_case_ : int = dataset.map(**__a ) @get_duration def SCREAMING_SNAKE_CASE__ ( __a , **__a ): snake_case_ : Dict = dataset.filter(**__a ) def SCREAMING_SNAKE_CASE__ ( ): snake_case_ : Tuple = {'num examples': SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ : Dict = datasets.Features({'text': datasets.Value('string' ), 'numbers': datasets.Value('float32' )} ) snake_case_ : List[Any] = generate_example_dataset( os.path.join(__a , 'dataset.arrow' ) , __a , num_examples=__a ) snake_case_ : str = transformers.AutoTokenizer.from_pretrained('bert-base-cased' , use_fast=__a ) def tokenize(__a ): return tokenizer(examples['text'] ) snake_case_ : Any = map(__a ) snake_case_ : Tuple = map(__a , batched=__a ) snake_case_ : str = map(__a , function=lambda __a : None , batched=__a ) with dataset.formatted_as(type='numpy' ): snake_case_ : Optional[int] = map(__a , function=lambda __a : None , batched=__a ) with dataset.formatted_as(type='pandas' ): snake_case_ : str = map(__a , function=lambda __a : None , batched=__a ) with dataset.formatted_as(type='torch' , columns='numbers' ): snake_case_ : int = map(__a , function=lambda __a : None , batched=__a ) with dataset.formatted_as(type='tensorflow' , columns='numbers' ): snake_case_ : List[Any] = map(__a , function=lambda __a : None , batched=__a ) snake_case_ : int = map(__a , function=__a , batched=__a ) snake_case_ : Optional[Any] = filter(__a ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(__a , 'wb' ) as f: f.write(json.dumps(__a ).encode('utf-8' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
327
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_lxmert import LxmertTokenizer _SCREAMING_SNAKE_CASE = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} _SCREAMING_SNAKE_CASE = { """vocab_file""": { """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt""", }, """tokenizer_file""": { """unc-nlp/lxmert-base-uncased""": ( """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json""" ), }, } _SCREAMING_SNAKE_CASE = { """unc-nlp/lxmert-base-uncased""": 5_12, } _SCREAMING_SNAKE_CASE = { """unc-nlp/lxmert-base-uncased""": {"""do_lower_case""": True}, } class SCREAMING_SNAKE_CASE_ ( snake_case_ ): __magic_name__: List[Any] = VOCAB_FILES_NAMES __magic_name__: List[str] = PRETRAINED_VOCAB_FILES_MAP __magic_name__: List[str] = PRETRAINED_INIT_CONFIGURATION __magic_name__: Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __magic_name__: Union[str, Any] = LxmertTokenizer def __init__( self : List[str] , _A : Union[str, Any]=None , _A : Optional[Any]=None , _A : Dict=True , _A : Dict="[UNK]" , _A : Optional[int]="[SEP]" , _A : Dict="[PAD]" , _A : Union[str, Any]="[CLS]" , _A : str="[MASK]" , _A : Tuple=True , _A : Dict=None , **_A : List[Any] , ) -> Optional[int]: """simple docstring""" super().__init__( _A , tokenizer_file=_A , do_lower_case=_A , unk_token=_A , sep_token=_A , pad_token=_A , cls_token=_A , mask_token=_A , tokenize_chinese_chars=_A , strip_accents=_A , **_A , ) snake_case_ : Optional[int] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , _A ) != do_lower_case or normalizer_state.get('strip_accents' , _A ) != strip_accents or normalizer_state.get('handle_chinese_chars' , _A ) != tokenize_chinese_chars ): snake_case_ : Tuple = getattr(_A , normalizer_state.pop('type' ) ) snake_case_ : Union[str, Any] = do_lower_case snake_case_ : int = strip_accents snake_case_ : Optional[Any] = tokenize_chinese_chars snake_case_ : List[Any] = normalizer_class(**_A ) snake_case_ : Tuple = do_lower_case def UpperCAmelCase_ ( self : Dict , _A : Any , _A : List[Any]=None ) -> Dict: """simple docstring""" snake_case_ : Optional[Any] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def UpperCAmelCase_ ( self : Optional[Any] , _A : List[int] , _A : Optional[List[int]] = None ) -> List[int]: """simple docstring""" snake_case_ : str = [self.sep_token_id] snake_case_ : Dict = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase_ ( self : Optional[int] , _A : str , _A : Optional[str] = None ) -> Tuple[str]: """simple docstring""" snake_case_ : Union[str, Any] = self._tokenizer.model.save(_A , name=_A ) return tuple(_A )
327
1
import json import logging import os import socket import git import numpy as np import torch logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO, ) _SCREAMING_SNAKE_CASE = logging.getLogger(__name__) def SCREAMING_SNAKE_CASE__ ( __a ): snake_case_ : str = git.Repo(search_parent_directories=__a ) snake_case_ : Union[str, Any] = { 'repo_id': str(__a ), 'repo_sha': str(repo.head.object.hexsha ), 'repo_branch': str(repo.active_branch ), } with open(os.path.join(__a , 'git_log.json' ) , 'w' ) as f: json.dump(__a , __a , indent=4 ) def SCREAMING_SNAKE_CASE__ ( __a ): if params.n_gpu <= 0: snake_case_ : Optional[int] = 0 snake_case_ : List[Any] = -1 snake_case_ : List[Any] = True snake_case_ : Dict = False return assert torch.cuda.is_available() logger.info('Initializing GPUs' ) if params.n_gpu > 1: assert params.local_rank != -1 snake_case_ : int = int(os.environ['WORLD_SIZE'] ) snake_case_ : List[Any] = int(os.environ['N_GPU_NODE'] ) snake_case_ : Dict = int(os.environ['RANK'] ) # number of nodes / node ID snake_case_ : List[str] = params.world_size // params.n_gpu_per_node snake_case_ : List[Any] = params.global_rank // params.n_gpu_per_node snake_case_ : Optional[Any] = True assert params.n_nodes == int(os.environ['N_NODES'] ) assert params.node_id == int(os.environ['NODE_RANK'] ) # local job (single GPU) else: assert params.local_rank == -1 snake_case_ : str = 1 snake_case_ : Dict = 0 snake_case_ : str = 0 snake_case_ : int = 0 snake_case_ : int = 1 snake_case_ : Union[str, Any] = 1 snake_case_ : Optional[Any] = False # sanity checks assert params.n_nodes >= 1 assert 0 <= params.node_id < params.n_nodes assert 0 <= params.local_rank <= params.global_rank < params.world_size assert params.world_size == params.n_nodes * params.n_gpu_per_node # define whether this is the master process / if we are in multi-node distributed mode snake_case_ : Tuple = params.node_id == 0 and params.local_rank == 0 snake_case_ : List[Any] = params.n_nodes > 1 # summary snake_case_ : Union[str, Any] = f"""--- Global rank: {params.global_rank} - """ logger.info(PREFIX + 'Number of nodes: %i' % params.n_nodes ) logger.info(PREFIX + 'Node ID : %i' % params.node_id ) logger.info(PREFIX + 'Local rank : %i' % params.local_rank ) logger.info(PREFIX + 'World size : %i' % params.world_size ) logger.info(PREFIX + 'GPUs per node : %i' % params.n_gpu_per_node ) logger.info(PREFIX + 'Master : %s' % str(params.is_master ) ) logger.info(PREFIX + 'Multi-node : %s' % str(params.multi_node ) ) logger.info(PREFIX + 'Multi-GPU : %s' % str(params.multi_gpu ) ) logger.info(PREFIX + 'Hostname : %s' % socket.gethostname() ) # set GPU device torch.cuda.set_device(params.local_rank ) # initialize multi-GPU if params.multi_gpu: logger.info('Initializing PyTorch distributed' ) torch.distributed.init_process_group( init_method='env://' , backend='nccl' , ) def SCREAMING_SNAKE_CASE__ ( __a ): np.random.seed(args.seed ) torch.manual_seed(args.seed ) if args.n_gpu > 0: torch.cuda.manual_seed_all(args.seed )
327
def SCREAMING_SNAKE_CASE__ ( __a ): if not isinstance(__a , __a ): snake_case_ : int = f"""Input value of [number={number}] must be an integer""" raise TypeError(__a ) if number < 0: return False snake_case_ : Dict = number * number while number > 0: if number % 10 != number_square % 10: return False number //= 10 number_square //= 10 return True if __name__ == "__main__": import doctest doctest.testmod()
327
1
from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) # General docstring _SCREAMING_SNAKE_CASE = """MobileNetV1Config""" # Base docstring _SCREAMING_SNAKE_CASE = """google/mobilenet_v1_1.0_224""" _SCREAMING_SNAKE_CASE = [1, 10_24, 7, 7] # Image classification docstring _SCREAMING_SNAKE_CASE = """google/mobilenet_v1_1.0_224""" _SCREAMING_SNAKE_CASE = """tabby, tabby cat""" _SCREAMING_SNAKE_CASE = [ """google/mobilenet_v1_1.0_224""", """google/mobilenet_v1_0.75_192""", # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def SCREAMING_SNAKE_CASE__ ( __a , __a , __a=None ): snake_case_ : Union[str, Any] = {} if isinstance(__a , __a ): snake_case_ : str = model.mobilenet_va else: snake_case_ : Dict = model snake_case_ : Union[str, Any] = 'MobilenetV1/Conv2d_0/' snake_case_ : Any = backbone.conv_stem.convolution.weight snake_case_ : str = backbone.conv_stem.normalization.bias snake_case_ : int = backbone.conv_stem.normalization.weight snake_case_ : Optional[Any] = backbone.conv_stem.normalization.running_mean snake_case_ : Tuple = backbone.conv_stem.normalization.running_var for i in range(13 ): snake_case_ : Any = i + 1 snake_case_ : Any = i * 2 snake_case_ : Union[str, Any] = backbone.layer[pt_index] snake_case_ : int = f"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" snake_case_ : Any = pointer.convolution.weight snake_case_ : Optional[int] = pointer.normalization.bias snake_case_ : Dict = pointer.normalization.weight snake_case_ : Optional[Any] = pointer.normalization.running_mean snake_case_ : Optional[Any] = pointer.normalization.running_var snake_case_ : List[Any] = backbone.layer[pt_index + 1] snake_case_ : Any = f"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" snake_case_ : Any = pointer.convolution.weight snake_case_ : List[str] = pointer.normalization.bias snake_case_ : List[str] = pointer.normalization.weight snake_case_ : Optional[Any] = pointer.normalization.running_mean snake_case_ : Dict = pointer.normalization.running_var if isinstance(__a , __a ): snake_case_ : int = 'MobilenetV1/Logits/Conv2d_1c_1x1/' snake_case_ : List[str] = model.classifier.weight snake_case_ : Tuple = model.classifier.bias return tf_to_pt_map def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( 'Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see ' 'https://www.tensorflow.org/install/ for installation instructions.' ) raise # Load weights from TF model snake_case_ : Dict = tf.train.list_variables(__a ) snake_case_ : List[str] = {} for name, shape in init_vars: logger.info(f"""Loading TF weight {name} with shape {shape}""" ) snake_case_ : Dict = tf.train.load_variable(__a , __a ) snake_case_ : Tuple = array # Build TF to PyTorch weights loading map snake_case_ : Dict = _build_tf_to_pytorch_map(__a , __a , __a ) for name, pointer in tf_to_pt_map.items(): logger.info(f"""Importing {name}""" ) if name not in tf_weights: logger.info(f"""{name} not in tf pre-trained weights, skipping""" ) continue snake_case_ : Optional[int] = tf_weights[name] if "depthwise_weights" in name: logger.info('Transposing depthwise' ) snake_case_ : Dict = np.transpose(__a , (2, 3, 0, 1) ) elif "weights" in name: logger.info('Transposing' ) if len(pointer.shape ) == 2: # copying into linear layer snake_case_ : List[Any] = array.squeeze().transpose() else: snake_case_ : int = np.transpose(__a , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(f"""Initialize PyTorch weight {name} {array.shape}""" ) snake_case_ : List[Any] = torch.from_numpy(__a ) tf_weights.pop(__a , __a ) tf_weights.pop(name + '/RMSProp' , __a ) tf_weights.pop(name + '/RMSProp_1' , __a ) tf_weights.pop(name + '/ExponentialMovingAverage' , __a ) logger.info(f"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}""" ) return model def SCREAMING_SNAKE_CASE__ ( __a , __a ): snake_case_ ,snake_case_ : Dict = features.shape[-2:] snake_case_ ,snake_case_ : Any = conv_layer.stride snake_case_ ,snake_case_ : List[Any] = conv_layer.kernel_size if in_height % stride_height == 0: snake_case_ : str = max(kernel_height - stride_height , 0 ) else: snake_case_ : List[str] = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: snake_case_ : Dict = max(kernel_width - stride_width , 0 ) else: snake_case_ : Optional[int] = max(kernel_width - (in_width % stride_width) , 0 ) snake_case_ : List[Any] = pad_along_width // 2 snake_case_ : int = pad_along_width - pad_left snake_case_ : Dict = pad_along_height // 2 snake_case_ : Optional[int] = pad_along_height - pad_top snake_case_ : int = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(__a , __a , 'constant' , 0.0 ) class SCREAMING_SNAKE_CASE_ ( nn.Module ): def __init__( self : Union[str, Any] , _A : MobileNetVaConfig , _A : int , _A : int , _A : int , _A : Optional[int] = 1 , _A : Optional[int] = 1 , _A : bool = False , _A : Optional[bool] = True , _A : Optional[bool or str] = True , ) -> None: """simple docstring""" super().__init__() snake_case_ : Union[str, Any] = config if in_channels % groups != 0: raise ValueError(F"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(F"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) snake_case_ : Optional[Any] = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) snake_case_ : Optional[Any] = nn.Convad( in_channels=_A , out_channels=_A , kernel_size=_A , stride=_A , padding=_A , groups=_A , bias=_A , padding_mode='zeros' , ) if use_normalization: snake_case_ : List[Any] = nn.BatchNormad( num_features=_A , eps=config.layer_norm_eps , momentum=0.9_9_9_7 , affine=_A , track_running_stats=_A , ) else: snake_case_ : Dict = None if use_activation: if isinstance(_A , _A ): snake_case_ : str = ACTaFN[use_activation] elif isinstance(config.hidden_act , _A ): snake_case_ : Union[str, Any] = ACTaFN[config.hidden_act] else: snake_case_ : Union[str, Any] = config.hidden_act else: snake_case_ : Union[str, Any] = None def UpperCAmelCase_ ( self : List[Any] , _A : torch.Tensor ) -> torch.Tensor: """simple docstring""" if self.config.tf_padding: snake_case_ : Union[str, Any] = apply_tf_padding(_A , self.convolution ) snake_case_ : int = self.convolution(_A ) if self.normalization is not None: snake_case_ : List[Any] = self.normalization(_A ) if self.activation is not None: snake_case_ : Any = self.activation(_A ) return features class SCREAMING_SNAKE_CASE_ ( snake_case_ ): __magic_name__: Tuple = MobileNetVaConfig __magic_name__: Union[str, Any] = load_tf_weights_in_mobilenet_va __magic_name__: int = "mobilenet_v1" __magic_name__: int = "pixel_values" __magic_name__: int = False def UpperCAmelCase_ ( self : str , _A : Union[nn.Linear, nn.Convad] ) -> None: """simple docstring""" if isinstance(_A , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(_A , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) _SCREAMING_SNAKE_CASE = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ _SCREAMING_SNAKE_CASE = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top." , snake_case_ , ) class SCREAMING_SNAKE_CASE_ ( snake_case_ ): def __init__( self : Union[str, Any] , _A : MobileNetVaConfig , _A : bool = True ) -> str: """simple docstring""" super().__init__(_A ) snake_case_ : Optional[int] = config snake_case_ : Optional[Any] = 32 snake_case_ : Optional[Any] = max(int(depth * config.depth_multiplier ) , config.min_depth ) snake_case_ : Union[str, Any] = MobileNetVaConvLayer( _A , in_channels=config.num_channels , out_channels=_A , kernel_size=3 , stride=2 , ) snake_case_ : Optional[int] = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] snake_case_ : Optional[int] = nn.ModuleList() for i in range(13 ): snake_case_ : Tuple = out_channels if strides[i] == 2 or i == 0: depth *= 2 snake_case_ : Tuple = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( _A , in_channels=_A , out_channels=_A , kernel_size=3 , stride=strides[i] , groups=_A , ) ) self.layer.append( MobileNetVaConvLayer( _A , in_channels=_A , out_channels=_A , kernel_size=1 , ) ) snake_case_ : List[Any] = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def UpperCAmelCase_ ( self : Dict , _A : List[Any] ) -> int: """simple docstring""" raise NotImplementedError @add_start_docstrings_to_model_forward(_A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=_A , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def UpperCAmelCase_ ( self : int , _A : Optional[torch.Tensor] = None , _A : Optional[bool] = None , _A : Optional[bool] = None , ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: """simple docstring""" snake_case_ : Tuple = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) snake_case_ : int = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError('You have to specify pixel_values' ) snake_case_ : int = self.conv_stem(_A ) snake_case_ : Tuple = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): snake_case_ : int = layer_module(_A ) if output_hidden_states: snake_case_ : Optional[int] = all_hidden_states + (hidden_states,) snake_case_ : Tuple = hidden_states if self.pooler is not None: snake_case_ : Union[str, Any] = torch.flatten(self.pooler(_A ) , start_dim=1 ) else: snake_case_ : Optional[int] = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=_A , pooler_output=_A , hidden_states=_A , ) @add_start_docstrings( "\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , snake_case_ , ) class SCREAMING_SNAKE_CASE_ ( snake_case_ ): def __init__( self : List[Any] , _A : MobileNetVaConfig ) -> None: """simple docstring""" super().__init__(_A ) snake_case_ : int = config.num_labels snake_case_ : Tuple = MobileNetVaModel(_A ) snake_case_ : Dict = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head snake_case_ : Tuple = nn.Dropout(config.classifier_dropout_prob , inplace=_A ) snake_case_ : Dict = nn.Linear(_A , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=_A , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCAmelCase_ ( self : Union[str, Any] , _A : Optional[torch.Tensor] = None , _A : Optional[bool] = None , _A : Optional[torch.Tensor] = None , _A : Optional[bool] = None , ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: """simple docstring""" snake_case_ : Union[str, Any] = return_dict if return_dict is not None else self.config.use_return_dict snake_case_ : int = self.mobilenet_va(_A , output_hidden_states=_A , return_dict=_A ) snake_case_ : str = outputs.pooler_output if return_dict else outputs[1] snake_case_ : Tuple = self.classifier(self.dropout(_A ) ) snake_case_ : Tuple = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: snake_case_ : Optional[Any] = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): snake_case_ : Optional[int] = 'single_label_classification' else: snake_case_ : str = 'multi_label_classification' if self.config.problem_type == "regression": snake_case_ : Tuple = MSELoss() if self.num_labels == 1: snake_case_ : List[str] = loss_fct(logits.squeeze() , labels.squeeze() ) else: snake_case_ : Optional[int] = loss_fct(_A , _A ) elif self.config.problem_type == "single_label_classification": snake_case_ : List[str] = CrossEntropyLoss() snake_case_ : str = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": snake_case_ : str = BCEWithLogitsLoss() snake_case_ : Union[str, Any] = loss_fct(_A , _A ) if not return_dict: snake_case_ : Union[str, Any] = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=_A , logits=_A , hidden_states=outputs.hidden_states , )
327
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _SCREAMING_SNAKE_CASE = { """configuration_autoformer""": [ """AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """AutoformerConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ """AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """AutoformerForPrediction""", """AutoformerModel""", """AutoformerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_autoformer import ( AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_autoformer import ( AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, AutoformerForPrediction, AutoformerModel, AutoformerPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
327
1