code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_=False ): """simple docstring""" if isinstance(UpperCamelCase_ ,UpperCamelCase_ ) and isinstance(UpperCamelCase_ ,UpperCamelCase_ ): snake_case = len(set_a.intersection(UpperCamelCase_ ) ) if alternative_union: snake_case = len(UpperCamelCase_ ) + len(UpperCamelCase_ ) else: snake_case = len(set_a.union(UpperCamelCase_ ) ) return intersection / union if isinstance(UpperCamelCase_ ,(list, tuple) ) and isinstance(UpperCamelCase_ ,(list, tuple) ): snake_case = [element for element in set_a if element in set_b] if alternative_union: snake_case = len(UpperCamelCase_ ) + len(UpperCamelCase_ ) return len(UpperCamelCase_ ) / union else: snake_case = set_a + [element for element in set_b if element not in set_a] return len(UpperCamelCase_ ) / len(UpperCamelCase_ ) return len(UpperCamelCase_ ) / len(UpperCamelCase_ ) return None if __name__ == "__main__": _SCREAMING_SNAKE_CASE : Optional[int] = {"a", "b", "c", "d", "e"} _SCREAMING_SNAKE_CASE : List[str] = {"c", "d", "e", "f", "h", "i"} print(jaccard_similarity(set_a, set_b))
127
from random import shuffle import tensorflow as tf from numpy import array def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" snake_case = int(UpperCamelCase_ ) assert noofclusters < len(UpperCamelCase_ ) # Find out the dimensionality snake_case = len(vectors[0] ) # Will help select random centroids from among the available vectors snake_case = list(range(len(UpperCamelCase_ ) ) ) shuffle(UpperCamelCase_ ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. snake_case = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION snake_case = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points snake_case = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(UpperCamelCase_ ) ] ##These nodes will assign the centroid Variables the appropriate ##values snake_case = tf.placeholder('''float64''' ,[dim] ) snake_case = [] for centroid in centroids: cent_assigns.append(tf.assign(UpperCamelCase_ ,UpperCamelCase_ ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) snake_case = [tf.Variable(0 ) for i in range(len(UpperCamelCase_ ) )] ##These nodes will assign an assignment Variable the appropriate ##value snake_case = tf.placeholder('''int32''' ) snake_case = [] for assignment in assignments: cluster_assigns.append(tf.assign(UpperCamelCase_ ,UpperCamelCase_ ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input snake_case = tf.placeholder('''float''' ,[None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors snake_case = tf.reduce_mean(UpperCamelCase_ ,0 ) ##Node for computing Euclidean distances # Placeholders for input snake_case = tf.placeholder('''float''' ,[dim] ) snake_case = tf.placeholder('''float''' ,[dim] ) snake_case = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(UpperCamelCase_ ,UpperCamelCase_ ) ,2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input snake_case = tf.placeholder('''float''' ,[noofclusters] ) snake_case = tf.argmin(UpperCamelCase_ ,0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. snake_case = tf.initialize_all_variables() # Initialize all variables sess.run(UpperCamelCase_ ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. snake_case = 1_00 for _ in range(UpperCamelCase_ ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(UpperCamelCase_ ) ): snake_case = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. snake_case = [ sess.run(UpperCamelCase_ ,feed_dict={va: vect, va: sess.run(UpperCamelCase_ )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input snake_case = sess.run( UpperCamelCase_ ,feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] ,feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(UpperCamelCase_ ): # Collect all the vectors assigned to this cluster snake_case = [ vectors[i] for i in range(len(UpperCamelCase_ ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location snake_case = sess.run( UpperCamelCase_ ,feed_dict={mean_input: array(UpperCamelCase_ )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] ,feed_dict={centroid_value: new_location} ) # Return centroids and assignments snake_case = sess.run(UpperCamelCase_ ) snake_case = sess.run(UpperCamelCase_ ) return centroids, assignments
127
1
'''simple docstring''' def _lowerCamelCase ( lowerCamelCase_ : Any ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = [] UpperCAmelCase_ : str = [] UpperCAmelCase_ : Union[str, Any] = { '^': 3, '*': 2, '/': 2, '%': 2, '+': 1, '-': 1, } # Priority of each operator UpperCAmelCase_ : int = len(lowerCamelCase_ ) if (len(lowerCamelCase_ ) > 7) else 7 # Print table header for output print( 'Symbol'.center(8 ) , 'Stack'.center(lowerCamelCase_ ) , 'Postfix'.center(lowerCamelCase_ ) , sep=' | ' , ) print('-' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(lowerCamelCase_ ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(lowerCamelCase_ ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(lowerCamelCase_ ) == 0: stack.append(lowerCamelCase_ ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(lowerCamelCase_ ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(lowerCamelCase_ ) # push x to stack print( x.center(8 ) , (''.join(lowerCamelCase_ )).ljust(lowerCamelCase_ ) , (''.join(lowerCamelCase_ )).ljust(lowerCamelCase_ ) , sep=' | ' , ) # Output in tabular format while len(lowerCamelCase_ ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ' '.center(8 ) , (''.join(lowerCamelCase_ )).ljust(lowerCamelCase_ ) , (''.join(lowerCamelCase_ )).ljust(lowerCamelCase_ ) , sep=' | ' , ) # Output in tabular format return "".join(lowerCamelCase_ ) # return Postfix as str def _lowerCamelCase ( lowerCamelCase_ : int ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = list(infix[::-1] ) # reverse the infix equation for i in range(len(lowerCamelCase_ ) ): if infix[i] == "(": UpperCAmelCase_ : Tuple = ')' # change "(" to ")" elif infix[i] == ")": UpperCAmelCase_ : Union[str, Any] = '(' # change ")" to "(" return (infix_2_postfix(''.join(lowerCamelCase_ ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": snake_case__ : str = input('''\nEnter an Infix Equation = ''') # Input an Infix equation snake_case__ : Any = ''''''.join(Infix.split()) # Remove spaces from the input print('''\n\t''', Infix, '''(Infix) -> ''', infix_2_prefix(Infix), '''(Prefix)''')
274
'''simple docstring''' snake_case__ : Optional[Any] = tuple[float, float, float] snake_case__ : Tuple = tuple[float, float, float] def _lowerCamelCase ( lowerCamelCase_ : Pointad , lowerCamelCase_ : Pointad ): """simple docstring""" UpperCAmelCase_ : Any = end_pointa[0] - end_pointa[0] UpperCAmelCase_ : Optional[Any] = end_pointa[1] - end_pointa[1] UpperCAmelCase_ : Any = end_pointa[2] - end_pointa[2] return (x, y, z) def _lowerCamelCase ( lowerCamelCase_ : Vectorad , lowerCamelCase_ : Vectorad ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = ab[1] * ac[2] - ab[2] * ac[1] # *i UpperCAmelCase_ : Optional[Any] = (ab[0] * ac[2] - ab[2] * ac[0]) * -1 # *j UpperCAmelCase_ : Dict = ab[0] * ac[1] - ab[1] * ac[0] # *k return (x, y, z) def _lowerCamelCase ( lowerCamelCase_ : Vectorad , lowerCamelCase_ : int ): """simple docstring""" return tuple(round(lowerCamelCase_ , lowerCamelCase_ ) for x in vector ) == (0, 0, 0) def _lowerCamelCase ( lowerCamelCase_ : Pointad , lowerCamelCase_ : Pointad , lowerCamelCase_ : Pointad , lowerCamelCase_ : int = 10 ): """simple docstring""" UpperCAmelCase_ : List[str] = create_vector(lowerCamelCase_ , lowerCamelCase_ ) UpperCAmelCase_ : List[Any] = create_vector(lowerCamelCase_ , lowerCamelCase_ ) return is_zero_vector(get_ad_vectors_cross(lowerCamelCase_ , lowerCamelCase_ ) , lowerCamelCase_ )
274
1
from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class A__ ( lowerCAmelCase__ ): lowerCAmelCase__ : Union[str, Any] = ["vqvae"] def __init__( self : int , _UpperCAmelCase : AutoencoderKL , _UpperCAmelCase : UNetaDConditionModel , _UpperCAmelCase : Mel , _UpperCAmelCase : Union[DDIMScheduler, DDPMScheduler] , ) -> str: """simple docstring""" super().__init__() self.register_modules(unet=_UpperCAmelCase , scheduler=_UpperCAmelCase , mel=_UpperCAmelCase , vqvae=_UpperCAmelCase ) def a__ ( self : Tuple ) -> int: """simple docstring""" return 50 if isinstance(self.scheduler , _UpperCAmelCase ) else 10_00 @torch.no_grad() def __call__( self : str , _UpperCAmelCase : int = 1 , _UpperCAmelCase : str = None , _UpperCAmelCase : np.ndarray = None , _UpperCAmelCase : int = 0 , _UpperCAmelCase : int = 0 , _UpperCAmelCase : int = None , _UpperCAmelCase : torch.Generator = None , _UpperCAmelCase : float = 0 , _UpperCAmelCase : float = 0 , _UpperCAmelCase : torch.Generator = None , _UpperCAmelCase : float = 0 , _UpperCAmelCase : torch.Tensor = None , _UpperCAmelCase : torch.Tensor = None , _UpperCAmelCase : str=True , ) -> Union[ Union[AudioPipelineOutput, ImagePipelineOutput], Tuple[List[Image.Image], Tuple[int, List[np.ndarray]]], ]: """simple docstring""" __lowercase = steps or self.get_default_steps() self.scheduler.set_timesteps(_UpperCAmelCase ) __lowercase = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: __lowercase = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: __lowercase = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=_UpperCAmelCase , device=self.device , ) __lowercase = noise __lowercase = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(_UpperCAmelCase , _UpperCAmelCase ) __lowercase = self.mel.audio_slice_to_image(_UpperCAmelCase ) __lowercase = np.frombuffer(input_image.tobytes() , dtype='uint8' ).reshape( (input_image.height, input_image.width) ) __lowercase = (input_image / 2_55) * 2 - 1 __lowercase = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: __lowercase = self.vqvae.encode(torch.unsqueeze(_UpperCAmelCase , 0 ) ).latent_dist.sample( generator=_UpperCAmelCase )[0] __lowercase = self.vqvae.config.scaling_factor * input_images if start_step > 0: __lowercase = self.scheduler.add_noise(_UpperCAmelCase , _UpperCAmelCase , self.scheduler.timesteps[start_step - 1] ) __lowercase = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) __lowercase = int(mask_start_secs * pixels_per_second ) __lowercase = int(mask_end_secs * pixels_per_second ) __lowercase = self.scheduler.add_noise(_UpperCAmelCase , _UpperCAmelCase , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , _UpperCAmelCase ): __lowercase = self.unet(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase )['sample'] else: __lowercase = self.unet(_UpperCAmelCase , _UpperCAmelCase )['sample'] if isinstance(self.scheduler , _UpperCAmelCase ): __lowercase = self.scheduler.step( model_output=_UpperCAmelCase , timestep=_UpperCAmelCase , sample=_UpperCAmelCase , eta=_UpperCAmelCase , generator=_UpperCAmelCase , )['prev_sample'] else: __lowercase = self.scheduler.step( model_output=_UpperCAmelCase , timestep=_UpperCAmelCase , sample=_UpperCAmelCase , generator=_UpperCAmelCase , )['prev_sample'] if mask is not None: if mask_start > 0: __lowercase = mask[:, step, :, :mask_start] if mask_end > 0: __lowercase = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance __lowercase = 1 / self.vqvae.config.scaling_factor * images __lowercase = self.vqvae.decode(_UpperCAmelCase )['sample'] __lowercase = (images / 2 + 0.5).clamp(0 , 1 ) __lowercase = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() __lowercase = (images * 2_55).round().astype('uint8' ) __lowercase = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(_UpperCAmelCase , mode='RGB' ).convert('L' ) for _ in images) ) __lowercase = [self.mel.image_to_audio(_UpperCAmelCase ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(_UpperCAmelCase )[:, np.newaxis, :] ) , **ImagePipelineOutput(_UpperCAmelCase ) ) @torch.no_grad() def a__ ( self : Any , _UpperCAmelCase : List[Image.Image] , _UpperCAmelCase : int = 50 ) -> np.ndarray: """simple docstring""" assert isinstance(self.scheduler , _UpperCAmelCase ) self.scheduler.set_timesteps(_UpperCAmelCase ) __lowercase = np.array( [np.frombuffer(image.tobytes() , dtype='uint8' ).reshape((1, image.height, image.width) ) for image in images] ) __lowercase = (sample / 2_55) * 2 - 1 __lowercase = torch.Tensor(_UpperCAmelCase ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): __lowercase = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps __lowercase = self.scheduler.alphas_cumprod[t] __lowercase = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) __lowercase = 1 - alpha_prod_t __lowercase = self.unet(_UpperCAmelCase , _UpperCAmelCase )['sample'] __lowercase = (1 - alpha_prod_t_prev) ** 0.5 * model_output __lowercase = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) __lowercase = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def a__ ( _UpperCAmelCase : torch.Tensor , _UpperCAmelCase : torch.Tensor , _UpperCAmelCase : float ) -> torch.Tensor: """simple docstring""" __lowercase = acos(torch.dot(torch.flatten(_UpperCAmelCase ) , torch.flatten(_UpperCAmelCase ) ) / torch.norm(_UpperCAmelCase ) / torch.norm(_UpperCAmelCase ) ) return sin((1 - alpha) * theta ) * xa / sin(_UpperCAmelCase ) + sin(alpha * theta ) * xa / sin(_UpperCAmelCase )
325
import unittest from pathlib import Path from shutil import copyfile from transformers import SPIECE_UNDERLINE, is_sentencepiece_available from transformers.models.speech_to_text import SpeechaTextTokenizer from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE__ = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_sentencepiece_available(): import sentencepiece as sp SCREAMING_SNAKE_CASE__ = 5 SCREAMING_SNAKE_CASE__ = 10 @require_sentencepiece @require_tokenizers class A__ ( lowerCAmelCase__ , unittest.TestCase ): lowerCAmelCase__ : Optional[Any] = SpeechaTextTokenizer lowerCAmelCase__ : Any = False lowerCAmelCase__ : List[Any] = True def a__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" super().setUp() __lowercase = sp.SentencePieceProcessor() spm_model.Load(_UpperCAmelCase ) __lowercase = ['<s>', '<pad>', '</s>', '<unk>'] vocab += [spm_model.IdToPiece(id_ ) for id_ in range(len(_UpperCAmelCase ) )] __lowercase = dict(zip(_UpperCAmelCase , range(len(_UpperCAmelCase ) ) ) ) __lowercase = Path(self.tmpdirname ) save_json(_UpperCAmelCase , save_dir / VOCAB_FILES_NAMES['vocab_file'] ) if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists(): copyfile(_UpperCAmelCase , save_dir / VOCAB_FILES_NAMES['spm_file'] ) __lowercase = SpeechaTextTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def a__ ( self : str ) -> int: """simple docstring""" __lowercase = '<pad>' __lowercase = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def a__ ( self : Optional[Any] ) -> str: """simple docstring""" __lowercase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<s>' ) self.assertEqual(vocab_keys[1] , '<pad>' ) self.assertEqual(vocab_keys[-1] , 'j' ) self.assertEqual(len(_UpperCAmelCase ) , 10_01 ) def a__ ( self : int ) -> Optional[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 10_01 ) def a__ ( self : Optional[Any] ) -> str: """simple docstring""" __lowercase = SpeechaTextTokenizer.from_pretrained(self.tmpdirname ) __lowercase = tokenizer.tokenize('This is a test' ) self.assertListEqual(_UpperCAmelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [2_89, 50, 14, 1_74, 3_86] , ) __lowercase = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( _UpperCAmelCase , [SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.'] , ) __lowercase = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , [12, 25, 88, 59, 28, 23, 11, 4, 6_06, 3_51, 3_51, 3_51, 7, 16, 70, 50, 76, 84, 10, 4, 8] ) __lowercase = tokenizer.convert_ids_to_tokens(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.'] , ) @slow def a__ ( self : Any ) -> Union[str, Any]: """simple docstring""" __lowercase = {'input_ids': [[37_91, 7_97, 31, 11, 64, 7_97, 31, 24_29, 4_33, 12, 11_76, 12, 20, 7_86, 9_15, 1_42, 24_13, 2_40, 37, 32_38, 7_97, 31, 11, 35, 93, 9_15, 1_42, 24_13, 2_40, 37, 55_40, 5_67, 12_76, 93, 37, 6_10, 40, 62, 4_55, 6_57, 10_42, 1_23, 7_80, 1_77, 37, 3_09, 2_41, 12_98, 5_14, 20, 2_92, 27_37, 1_14, 24_69, 2_41, 85, 64, 3_02, 5_48, 5_28, 4_23, 4, 5_09, 4_06, 4_23, 37, 6_01, 4, 7_77, 3_02, 5_48, 5_28, 4_23, 2_84, 4, 33_88, 5_11, 4_59, 4, 35_55, 40, 3_21, 3_02, 7_05, 4, 33_88, 5_11, 5_83, 3_26, 5, 5, 5, 62, 33_10, 5_60, 1_77, 26_80, 2_17, 15_08, 32, 31, 8_53, 4_18, 64, 5_83, 5_11, 16_05, 62, 35, 93, 5_60, 1_77, 26_80, 2_17, 15_08, 15_21, 64, 5_83, 5_11, 5_19, 62, 20, 15_15, 7_64, 20, 1_49, 2_61, 56_25, 79_72, 20, 55_40, 5_67, 12_76, 93, 39_25, 16_75, 11, 15, 8_02, 79_72, 5_76, 2_17, 15_08, 11, 35, 93, 12_53, 24_41, 15, 2_89, 6_52, 31, 4_16, 3_21, 38_42, 1_15, 40, 9_11, 8, 4_76, 6_19, 4, 3_80, 1_42, 4_23, 3_35, 2_40, 35, 93, 2_64, 8, 11, 3_35, 5_69, 4_20, 1_63, 5, 2], [2_60, 5_48, 5_28, 4_23, 20, 4_51, 20, 26_81, 11_53, 34_34, 20, 55_40, 37, 5_67, 1_26, 12_53, 24_41, 33_76, 4_49, 2_10, 4_31, 15_63, 1_77, 7_67, 55_40, 11, 12_03, 4_72, 11, 29_53, 6_85, 2_85, 3_64, 7_06, 11_53, 20, 67_99, 20, 28_69, 20, 44_64, 1_26, 40, 24_29, 20, 10_40, 8_66, 26_64, 4_18, 20, 3_18, 20, 17_26, 1_86, 20, 2_65, 5_22, 35, 93, 21_91, 46_34, 20, 10_40, 12, 67_99, 15, 2_28, 23_56, 1_42, 31, 11, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_75, 26_66, 6_84, 15_82, 11_76, 12, 6_27, 1_49, 6_19, 20, 49_02, 5_63, 11, 20, 1_49, 2_61, 34_20, 23_56, 1_74, 1_42, 47_14, 1_31, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name='facebook/s2t-small-mustc-en-de-st' , revision='a14f04cf0776c02f62a8cb800cf7909e15ea23ad' , ) @require_sentencepiece class A__ ( unittest.TestCase ): lowerCAmelCase__ : str = "valhalla/s2t_mustc_multilinguial_medium" lowerCAmelCase__ : Dict = "C'est trop cool" lowerCAmelCase__ : List[Any] = "Esto es genial" @classmethod def a__ ( cls : Any ) -> Optional[int]: """simple docstring""" __lowercase = SpeechaTextTokenizer.from_pretrained(cls.checkpoint_name ) return cls def a__ ( self : Tuple ) -> Tuple: """simple docstring""" self.assertEqual(self.tokenizer.lang_code_to_id['pt'] , 4 ) self.assertEqual(self.tokenizer.lang_code_to_id['ru'] , 6 ) self.assertEqual(self.tokenizer.lang_code_to_id['it'] , 9 ) self.assertEqual(self.tokenizer.lang_code_to_id['de'] , 11 ) def a__ ( self : Tuple ) -> List[str]: """simple docstring""" self.assertEqual(self.tokenizer.vocab_size , 1_00_00 ) def a__ ( self : str ) -> int: """simple docstring""" self.assertIn(_UpperCAmelCase , self.tokenizer.all_special_ids ) __lowercase = [ES_CODE, 4, 16_01, 47, 76_47, 2] __lowercase = self.tokenizer.decode(_UpperCAmelCase , skip_special_tokens=_UpperCAmelCase ) __lowercase = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=_UpperCAmelCase ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) self.assertNotIn(self.tokenizer.eos_token , _UpperCAmelCase ) def a__ ( self : Dict ) -> Union[str, Any]: """simple docstring""" __lowercase = 'fr' __lowercase = self.tokenizer(self.french_text ).input_ids self.assertEqual(encoded[0] , _UpperCAmelCase ) self.assertEqual(encoded[-1] , self.tokenizer.eos_token_id ) def a__ ( self : List[Any] ) -> Any: """simple docstring""" __lowercase = 'fr' self.assertListEqual(self.tokenizer.prefix_tokens , [FR_CODE] ) __lowercase = 'es' self.assertListEqual(self.tokenizer.prefix_tokens , [ES_CODE] )
325
1
"""simple docstring""" from __future__ import annotations def snake_case_ ( A_ : int ): '''simple docstring''' _lowerCamelCase : List[str] = [True] * limit _lowerCamelCase : Any = False _lowerCamelCase : Optional[Any] = False _lowerCamelCase : Optional[Any] = True for i in range(3, int(limit**0.5 + 1 ), 2 ): _lowerCamelCase : int = i * 2 while index < limit: _lowerCamelCase : Any = False _lowerCamelCase : Optional[Any] = index + i _lowerCamelCase : Optional[int] = [2] for i in range(3, _A, 2 ): if is_prime[i]: primes.append(_A ) return primes def snake_case_ ( A_ : int = 1_00_00_00 ): '''simple docstring''' _lowerCamelCase : List[str] = prime_sieve(_A ) _lowerCamelCase : Dict = 0 _lowerCamelCase : List[Any] = 0 for i in range(len(_A ) ): for j in range(i + length, len(_A ) ): _lowerCamelCase : int = sum(primes[i:j] ) if sol >= ceiling: break if sol in primes: _lowerCamelCase : Optional[int] = j - i _lowerCamelCase : Optional[Any] = sol return largest if __name__ == "__main__": print(F"""{solution() = }""")
355
"""simple docstring""" def snake_case_ ( A_ : int ): '''simple docstring''' return 1 if digit in (0, 1) else (digit * factorial(digit - 1 )) def snake_case_ ( A_ : int ): '''simple docstring''' _lowerCamelCase : str = 0 _lowerCamelCase : Any = number while duplicate > 0: _lowerCamelCase , _lowerCamelCase : Union[str, Any] = divmod(A_, 10 ) fact_sum += factorial(A_ ) return fact_sum == number if __name__ == "__main__": print('''Program to check whether a number is a Krisnamurthy Number or not.''') lowerCAmelCase__ = int(input('''Enter number: ''').strip()) print( F"""{number} is {"" if krishnamurthy(number) else "not "}a Krishnamurthy Number.""" )
175
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { 'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig'] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['RemBertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['RemBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RemBertForCausalLM', 'RemBertForMaskedLM', 'RemBertForMultipleChoice', 'RemBertForQuestionAnswering', 'RemBertForSequenceClassification', 'RemBertForTokenClassification', 'RemBertLayer', 'RemBertModel', 'RemBertPreTrainedModel', 'load_tf_weights_in_rembert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRemBertForCausalLM', 'TFRemBertForMaskedLM', 'TFRemBertForMultipleChoice', 'TFRemBertForQuestionAnswering', 'TFRemBertForSequenceClassification', 'TFRemBertForTokenClassification', 'TFRemBertLayer', 'TFRemBertModel', 'TFRemBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
321
'''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 lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> List[str]: UpperCamelCase = 1.5 UpperCamelCase = int(factor * num_class_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 ) os.makedirs(F"{class_data_dir}/images" , exist_ok=__UpperCamelCase ) if len(list(Path(F"{class_data_dir}/images" ).iterdir() ) ) >= num_class_images: return while True: UpperCamelCase = client.query(text=__UpperCamelCase ) if len(__UpperCamelCase ) >= factor * num_class_images or num_images > 1E4: break else: UpperCamelCase = int(factor * num_images ) UpperCamelCase = ClipClient( url="""https://knn.laion.ai/knn-service""" , indice_name="""laion_400m""" , num_images=__UpperCamelCase , aesthetic_weight=0.1 , ) UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = tqdm(desc="""downloading real regularization images""" , total=__UpperCamelCase ) 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: UpperCamelCase = class_images[count] count += 1 try: UpperCamelCase = requests.get(images["""url"""] ) if img.status_code == 200: UpperCamelCase = 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 lowercase__ ( )-> str: UpperCamelCase = argparse.ArgumentParser("""""" , add_help=__UpperCamelCase ) parser.add_argument("""--class_prompt""" , help="""text prompt to retrieve images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--class_data_dir""" , help="""path to save images""" , required=__UpperCamelCase , type=__UpperCamelCase ) parser.add_argument("""--num_class_images""" , help="""number of images to download""" , default=200 , type=__UpperCamelCase ) return parser.parse_args() if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
321
1
"""simple docstring""" import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging lowerCamelCase__ = logging.get_logger(__name__) def __lowerCAmelCase (_UpperCamelCase ): __lowerCAmelCase : List[Any] = r'\w+[.]\d+' __lowerCAmelCase : Any = re.findall(_UpperCamelCase , _UpperCamelCase ) for pat in pats: __lowerCAmelCase : Any = key.replace(_UpperCamelCase , '_'.join(pat.split('.' ) ) ) return key def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase : Optional[Any] = pt_tuple_key[:-1] + ('scale',) if ( any('norm' in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): __lowerCAmelCase : Tuple = pt_tuple_key[:-1] + ('scale',) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: __lowerCAmelCase : List[str] = pt_tuple_key[:-1] + ('scale',) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: __lowerCAmelCase : Union[str, Any] = pt_tuple_key[:-1] + ('embedding',) return renamed_pt_tuple_key, pt_tensor # conv layer __lowerCAmelCase : Dict = pt_tuple_key[:-1] + ('kernel',) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: __lowerCAmelCase : List[Any] = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer __lowerCAmelCase : List[str] = pt_tuple_key[:-1] + ('kernel',) if pt_tuple_key[-1] == "weight": __lowerCAmelCase : List[str] = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight __lowerCAmelCase : Any = pt_tuple_key[:-1] + ('weight',) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias __lowerCAmelCase : List[str] = pt_tuple_key[:-1] + ('bias',) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase=42 ): # Step 1: Convert pytorch tensor to numpy __lowerCAmelCase : str = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params __lowerCAmelCase : Optional[int] = flax_model.init_weights(PRNGKey(_UpperCamelCase ) ) __lowerCAmelCase : List[str] = flatten_dict(_UpperCamelCase ) __lowerCAmelCase : Tuple = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): __lowerCAmelCase : Union[str, Any] = rename_key(_UpperCamelCase ) __lowerCAmelCase : Dict = tuple(renamed_pt_key.split('.' ) ) # Correctly rename weight parameters __lowerCAmelCase , __lowerCAmelCase : Dict = rename_key_and_reshape_tensor(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape " F"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}." ) # also add unexpected weight so that warning is thrown __lowerCAmelCase : Tuple = jnp.asarray(_UpperCamelCase ) return unflatten_dict(_UpperCamelCase )
182
"""simple docstring""" import json import os import tempfile import unittest import unittest.mock as mock from pathlib import Path from requests.exceptions import HTTPError from transformers.utils import ( CONFIG_NAME, FLAX_WEIGHTS_NAME, TF2_WEIGHTS_NAME, TRANSFORMERS_CACHE, WEIGHTS_NAME, cached_file, get_file_from_repo, has_file, ) lowerCamelCase__ = """hf-internal-testing/tiny-random-bert""" lowerCamelCase__ = os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""") lowerCamelCase__ = """9b8c223d42b2188cb49d29af482996f9d0f3e5a6""" class A__ ( unittest.TestCase): def __lowerCamelCase ( self ): __lowerCAmelCase : Union[str, Any] = cached_file(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(_SCREAMING_SNAKE_CASE ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) ) with open(os.path.join(_SCREAMING_SNAKE_CASE , 'refs' , 'main' ) ) as f: __lowerCAmelCase : List[Any] = f.read() self.assertEqual(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , 'snapshots' , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) self.assertTrue(os.path.isfile(_SCREAMING_SNAKE_CASE ) ) # File is cached at the same place the second time. __lowerCAmelCase : Union[str, Any] = cached_file(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Using a specific revision to test the full commit hash. __lowerCAmelCase : Union[str, Any] = cached_file(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , revision='9b8c223' ) self.assertEqual(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , 'snapshots' , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __lowerCamelCase ( self ): with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , 'is not a valid model identifier' ): __lowerCAmelCase : Optional[Any] = cached_file('tiny-random-bert' , _SCREAMING_SNAKE_CASE ) with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , 'is not a valid git identifier' ): __lowerCAmelCase : str = cached_file(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , revision='aaaa' ) with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , 'does not appear to have a file named' ): __lowerCAmelCase : Optional[Any] = cached_file(_SCREAMING_SNAKE_CASE , 'conf' ) def __lowerCamelCase ( self ): with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , 'does not appear to have a file named' ): __lowerCAmelCase : Optional[int] = cached_file(_SCREAMING_SNAKE_CASE , 'conf' ) with open(os.path.join(_SCREAMING_SNAKE_CASE , 'refs' , 'main' ) ) as f: __lowerCAmelCase : Tuple = f.read() self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE , '.no_exist' , _SCREAMING_SNAKE_CASE , 'conf' ) ) ) __lowerCAmelCase : List[Any] = cached_file(_SCREAMING_SNAKE_CASE , 'conf' , _raise_exceptions_for_missing_entries=_SCREAMING_SNAKE_CASE ) self.assertIsNone(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[str] = cached_file(_SCREAMING_SNAKE_CASE , 'conf' , local_files_only=_SCREAMING_SNAKE_CASE , _raise_exceptions_for_missing_entries=_SCREAMING_SNAKE_CASE ) self.assertIsNone(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[Any] = mock.Mock() __lowerCAmelCase : Tuple = 5_00 __lowerCAmelCase : List[Any] = {} __lowerCAmelCase : Dict = HTTPError __lowerCAmelCase : str = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch('requests.Session.request' , return_value=_SCREAMING_SNAKE_CASE ) as mock_head: __lowerCAmelCase : Optional[Any] = cached_file(_SCREAMING_SNAKE_CASE , 'conf' , _raise_exceptions_for_connection_errors=_SCREAMING_SNAKE_CASE ) self.assertIsNone(_SCREAMING_SNAKE_CASE ) # This check we did call the fake head request mock_head.assert_called() def __lowerCamelCase ( self ): self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only' , _SCREAMING_SNAKE_CASE ) ) self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only' , _SCREAMING_SNAKE_CASE ) ) self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only' , _SCREAMING_SNAKE_CASE ) ) def __lowerCamelCase ( self ): # `get_file_from_repo` returns None if the file does not exist self.assertIsNone(get_file_from_repo('bert-base-cased' , 'ahah.txt' ) ) # The function raises if the repository does not exist. with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , 'is not a valid model identifier' ): get_file_from_repo('bert-base-case' , _SCREAMING_SNAKE_CASE ) # The function raises if the revision does not exist. with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , 'is not a valid git identifier' ): get_file_from_repo('bert-base-cased' , _SCREAMING_SNAKE_CASE , revision='ahaha' ) __lowerCAmelCase : Union[str, Any] = get_file_from_repo('bert-base-cased' , _SCREAMING_SNAKE_CASE ) # The name is the cached name which is not very easy to test, so instead we load the content. __lowerCAmelCase : List[Any] = json.loads(open(_SCREAMING_SNAKE_CASE , 'r' ).read() ) self.assertEqual(config['hidden_size'] , 7_68 ) def __lowerCamelCase ( self ): with tempfile.TemporaryDirectory() as tmp_dir: __lowerCAmelCase : str = Path(_SCREAMING_SNAKE_CASE ) / 'a.txt' filename.touch() self.assertEqual(get_file_from_repo(_SCREAMING_SNAKE_CASE , 'a.txt' ) , str(_SCREAMING_SNAKE_CASE ) ) self.assertIsNone(get_file_from_repo(_SCREAMING_SNAKE_CASE , 'b.txt' ) )
182
1
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = '''▁''' __snake_case = {'''vocab_file''': '''sentencepiece.bpe.model'''} __snake_case = { '''vocab_file''': { '''facebook/mbart-large-en-ro''': ( '''https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model''' ), '''facebook/mbart-large-cc25''': ( '''https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model''' ), } } __snake_case = { '''facebook/mbart-large-en-ro''': 1024, '''facebook/mbart-large-cc25''': 1024, } # fmt: off __snake_case = ['''ar_AR''', '''cs_CZ''', '''de_DE''', '''en_XX''', '''es_XX''', '''et_EE''', '''fi_FI''', '''fr_XX''', '''gu_IN''', '''hi_IN''', '''it_IT''', '''ja_XX''', '''kk_KZ''', '''ko_KR''', '''lt_LT''', '''lv_LV''', '''my_MM''', '''ne_NP''', '''nl_XX''', '''ro_RO''', '''ru_RU''', '''si_LK''', '''tr_TR''', '''vi_VN''', '''zh_CN'''] class lowercase ( A__ ): """simple docstring""" _a = VOCAB_FILES_NAMES _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = PRETRAINED_VOCAB_FILES_MAP _a = ['input_ids', 'attention_mask'] _a = [] _a = [] def __init__( self , UpperCamelCase_ , UpperCamelCase_="<s>" , UpperCamelCase_="</s>" , UpperCamelCase_="</s>" , UpperCamelCase_="<s>" , UpperCamelCase_="<unk>" , UpperCamelCase_="<pad>" , UpperCamelCase_="<mask>" , UpperCamelCase_=None , UpperCamelCase_=None , UpperCamelCase_=None , UpperCamelCase_ = None , UpperCamelCase_=None , **UpperCamelCase_ , ): '''simple docstring''' UpperCamelCase__ :Dict = AddedToken(UpperCamelCase_ , lstrip=UpperCamelCase_ , rstrip=UpperCamelCase_ ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ) else mask_token UpperCamelCase__ :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_ , tokenizer_file=UpperCamelCase_ , src_lang=UpperCamelCase_ , tgt_lang=UpperCamelCase_ , additional_special_tokens=UpperCamelCase_ , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase_ , ) UpperCamelCase__ :int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(UpperCamelCase_ ) ) UpperCamelCase__ :Optional[int] = 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' # Mimic fairseq token-to-id alignment for the first 4 token UpperCamelCase__ :Dict = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab UpperCamelCase__ :Tuple = 1 UpperCamelCase__ :int = len(self.sp_model ) UpperCamelCase__ :Dict = { code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(UpperCamelCase_ ) } UpperCamelCase__ :List[Any] = {v: k for k, v in self.lang_code_to_id.items()} UpperCamelCase__ :Any = len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset self.fairseq_tokens_to_ids.update(self.lang_code_to_id ) UpperCamelCase__ :Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} UpperCamelCase__ :Union[str, Any] = list(self.lang_code_to_id.keys() ) if additional_special_tokens is not None: # Only add those special tokens if they are not already there. self._additional_special_tokens.extend( [t for t in additional_special_tokens if t not in self._additional_special_tokens] ) UpperCamelCase__ :Any = src_lang if src_lang is not None else '''en_XX''' UpperCamelCase__ :Optional[Any] = self.lang_code_to_id[self._src_lang] UpperCamelCase__ :Union[str, Any] = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) def __getstate__( self ): '''simple docstring''' UpperCamelCase__ :Dict = self.__dict__.copy() UpperCamelCase__ :int = None UpperCamelCase__ :Dict = self.sp_model.serialized_model_proto() return state def __setstate__( self , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :Tuple = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): UpperCamelCase__ :Optional[int] = {} UpperCamelCase__ :Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) @property def lowerCAmelCase__ ( self ): '''simple docstring''' return len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset + 1 # Plus 1 for the mask token @property def lowerCAmelCase__ ( self ): '''simple docstring''' return self._src_lang @src_lang.setter def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :Optional[Any] = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ = None , UpperCamelCase_ = False ): '''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_ ) UpperCamelCase__ :List[str] = [1] * len(self.prefix_tokens ) UpperCamelCase__ :int = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(UpperCamelCase_ )) + suffix_ones return prefix_ones + ([0] * len(UpperCamelCase_ )) + ([0] * len(UpperCamelCase_ )) + suffix_ones def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ = None ): '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ = None ): '''simple docstring''' UpperCamelCase__ :Optional[int] = [self.sep_token_id] UpperCamelCase__ :List[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , **UpperCamelCase_ ): '''simple docstring''' if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) UpperCamelCase__ :Tuple = src_lang UpperCamelCase__ :Optional[Any] = self(UpperCamelCase_ , add_special_tokens=UpperCamelCase_ , return_tensors=UpperCamelCase_ , **UpperCamelCase_ ) UpperCamelCase__ :List[str] = self.convert_tokens_to_ids(UpperCamelCase_ ) UpperCamelCase__ :Dict = tgt_lang_id return inputs def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Tuple = {self.convert_ids_to_tokens(UpperCamelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' return self.sp_model.encode(UpperCamelCase_ , out_type=UpperCamelCase_ ) def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] UpperCamelCase__ :Any = 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 lowerCAmelCase__ ( self , UpperCamelCase_ ): '''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 lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :List[str] = ''''''.join(UpperCamelCase_ ).replace(UpperCamelCase_ , ''' ''' ).strip() return out_string def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ = None ): '''simple docstring''' if not os.path.isdir(UpperCamelCase_ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCamelCase__ :int = os.path.join( UpperCamelCase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCamelCase_ ) 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__ :Any = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase_ ) return (out_vocab_file,) def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ = "en_XX" , UpperCamelCase_ = None , UpperCamelCase_ = "ro_RO" , **UpperCamelCase_ , ): '''simple docstring''' UpperCamelCase__ :Optional[Any] = src_lang UpperCamelCase__ :Optional[Any] = tgt_lang return super().prepare_seqaseq_batch(UpperCamelCase_ , UpperCamelCase_ , **UpperCamelCase_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' return self.set_src_lang_special_tokens(self.src_lang ) def lowerCAmelCase__ ( self ): '''simple docstring''' return self.set_tgt_lang_special_tokens(self.tgt_lang ) def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :Any = self.lang_code_to_id[src_lang] UpperCamelCase__ :int = [] UpperCamelCase__ :Union[str, Any] = [self.eos_token_id, self.cur_lang_code] def lowerCAmelCase__ ( self , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :Dict = self.lang_code_to_id[lang] UpperCamelCase__ :Optional[Any] = [] UpperCamelCase__ :Tuple = [self.eos_token_id, self.cur_lang_code]
97
import logging import math import os from dataclasses import dataclass, field from glob import glob from typing import Optional from torch.utils.data import ConcatDataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_WITH_LM_HEAD_MAPPING, AutoConfig, AutoModelWithLMHead, AutoTokenizer, DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForWholeWordMask, HfArgumentParser, LineByLineTextDataset, LineByLineWithRefDataset, PreTrainedTokenizer, TextDataset, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process lowerCAmelCase = logging.getLogger(__name__) lowerCAmelCase = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) lowerCAmelCase = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class _a : _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={ '''help''': ( '''The model checkpoint for weights initialization. Leave None if you want to train a model from''' ''' scratch.''' ) } , ) _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(UpperCamelCase__ )} , ) _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) @dataclass class _a : _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={'''help''': '''The input training data file (a text file).'''} ) _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={ '''help''': ( '''The input training data files (multiple files in glob format). ''' '''Very often splitting large files to smaller files can prevent tokenizer going out of memory''' ) } , ) _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={'''help''': '''An optional input evaluation data file to evaluate the perplexity on (a text file).'''} , ) _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={'''help''': '''An optional input train ref data file for whole word mask in Chinese.'''} , ) _lowercase : Optional[str] = field( default=UpperCamelCase__ , metadata={'''help''': '''An optional input eval ref data file for whole word mask in Chinese.'''} , ) _lowercase : bool = field( default=UpperCamelCase__ , metadata={'''help''': '''Whether distinct lines of text in the dataset are to be handled as distinct sequences.'''} , ) _lowercase : bool = field( default=UpperCamelCase__ , metadata={'''help''': '''Train with masked-language modeling loss instead of language modeling.'''} ) _lowercase : bool = field(default=UpperCamelCase__ , metadata={'''help''': '''Whether ot not to use whole word mask.'''} ) _lowercase : float = field( default=0.15 , metadata={'''help''': '''Ratio of tokens to mask for masked language modeling loss'''} ) _lowercase : float = field( default=1 / 6 , metadata={ '''help''': ( '''Ratio of length of a span of masked tokens to surrounding context length for permutation language''' ''' modeling.''' ) } , ) _lowercase : int = field( default=5 , metadata={'''help''': '''Maximum length of a span of masked tokens for permutation language modeling.'''} ) _lowercase : int = field( default=-1 , metadata={ '''help''': ( '''Optional input sequence length after tokenization.''' '''The training dataset will be truncated in block of this size for training.''' '''Default to the model max input length for single sentence inputs (take into account special tokens).''' ) } , ) _lowercase : bool = field( default=UpperCamelCase__ , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = False , SCREAMING_SNAKE_CASE = None , ): """simple docstring""" def _dataset(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=None ): if args.line_by_line: if ref_path is not None: if not args.whole_word_mask or not args.mlm: raise ValueError('''You need to set world whole masking and mlm to True for Chinese Whole Word Mask''' ) return LineByLineWithRefDataset( tokenizer=SCREAMING_SNAKE_CASE , file_path=SCREAMING_SNAKE_CASE , block_size=args.block_size , ref_path=SCREAMING_SNAKE_CASE , ) return LineByLineTextDataset(tokenizer=SCREAMING_SNAKE_CASE , file_path=SCREAMING_SNAKE_CASE , block_size=args.block_size ) else: return TextDataset( tokenizer=SCREAMING_SNAKE_CASE , file_path=SCREAMING_SNAKE_CASE , block_size=args.block_size , overwrite_cache=args.overwrite_cache , cache_dir=SCREAMING_SNAKE_CASE , ) if evaluate: return _dataset(args.eval_data_file , args.eval_ref_file ) elif args.train_data_files: return ConcatDataset([_dataset(SCREAMING_SNAKE_CASE ) for f in glob(args.train_data_files )] ) else: return _dataset(args.train_data_file , args.train_ref_file ) def _a ( ): """simple docstring""" lowercase__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) lowercase__ , lowercase__ , lowercase__ = parser.parse_args_into_dataclasses() if data_args.eval_data_file is None and training_args.do_eval: raise ValueError( '''Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file ''' '''or remove the --do_eval argument.''' ) if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'Output directory ({training_args.output_dir}) already exists and is not empty. Use' ''' --overwrite_output_dir to overcome.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , SCREAMING_SNAKE_CASE ) # 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. if model_args.config_name: lowercase__ = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: lowercase__ = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: lowercase__ = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.tokenizer_name: lowercase__ = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: lowercase__ = AutoTokenizer.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: raise ValueError( '''You are instantiating a new tokenizer from scratch. This is not supported, but you can do it from another''' ''' script, save it,and load it from here, using --tokenizer_name''' ) if model_args.model_name_or_path: lowercase__ = AutoModelWithLMHead.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) else: logger.info('''Training new model from scratch''' ) lowercase__ = AutoModelWithLMHead.from_config(SCREAMING_SNAKE_CASE ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE ) ) if config.model_type in ["bert", "roberta", "distilbert", "camembert"] and not data_args.mlm: raise ValueError( '''BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the''' '''--mlm flag (masked language modeling).''' ) if data_args.block_size <= 0: lowercase__ = tokenizer.max_len # Our input block size will be the max possible for the model else: lowercase__ = min(data_args.block_size , tokenizer.max_len ) # Get datasets lowercase__ = ( get_dataset(SCREAMING_SNAKE_CASE , tokenizer=SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir ) if training_args.do_train else None ) lowercase__ = ( get_dataset(SCREAMING_SNAKE_CASE , tokenizer=SCREAMING_SNAKE_CASE , evaluate=SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir ) if training_args.do_eval else None ) if config.model_type == "xlnet": lowercase__ = DataCollatorForPermutationLanguageModeling( tokenizer=SCREAMING_SNAKE_CASE , plm_probability=data_args.plm_probability , max_span_length=data_args.max_span_length , ) else: if data_args.mlm and data_args.whole_word_mask: lowercase__ = DataCollatorForWholeWordMask( tokenizer=SCREAMING_SNAKE_CASE , mlm_probability=data_args.mlm_probability ) else: lowercase__ = DataCollatorForLanguageModeling( tokenizer=SCREAMING_SNAKE_CASE , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer lowercase__ = Trainer( model=SCREAMING_SNAKE_CASE , args=SCREAMING_SNAKE_CASE , data_collator=SCREAMING_SNAKE_CASE , train_dataset=SCREAMING_SNAKE_CASE , eval_dataset=SCREAMING_SNAKE_CASE , prediction_loss_only=SCREAMING_SNAKE_CASE , ) # Training if training_args.do_train: lowercase__ = ( model_args.model_name_or_path if model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ) else None ) trainer.train(model_path=SCREAMING_SNAKE_CASE ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation lowercase__ = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) lowercase__ = trainer.evaluate() lowercase__ = math.exp(eval_output['''eval_loss'''] ) lowercase__ = {'''perplexity''': perplexity} lowercase__ = os.path.join(training_args.output_dir , '''eval_results_lm.txt''' ) if trainer.is_world_master(): with open(SCREAMING_SNAKE_CASE , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key in sorted(result.keys() ): logger.info(''' %s = %s''' , SCREAMING_SNAKE_CASE , str(result[key] ) ) writer.write('''%s = %s\n''' % (key, str(result[key] )) ) results.update(SCREAMING_SNAKE_CASE ) return results def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" main() if __name__ == "__main__": main()
110
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _UpperCAmelCase : str = { """configuration_data2vec_audio""": ["""DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecAudioConfig"""], """configuration_data2vec_text""": [ """DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecTextConfig""", """Data2VecTextOnnxConfig""", ], """configuration_data2vec_vision""": [ """DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecVisionConfig""", """Data2VecVisionOnnxConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase : Any = [ """DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecAudioForAudioFrameClassification""", """Data2VecAudioForCTC""", """Data2VecAudioForSequenceClassification""", """Data2VecAudioForXVector""", """Data2VecAudioModel""", """Data2VecAudioPreTrainedModel""", ] _UpperCAmelCase : List[Any] = [ """DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecTextForCausalLM""", """Data2VecTextForMaskedLM""", """Data2VecTextForMultipleChoice""", """Data2VecTextForQuestionAnswering""", """Data2VecTextForSequenceClassification""", """Data2VecTextForTokenClassification""", """Data2VecTextModel""", """Data2VecTextPreTrainedModel""", ] _UpperCAmelCase : Optional[int] = [ """DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecVisionForImageClassification""", """Data2VecVisionForMaskedImageModeling""", """Data2VecVisionForSemanticSegmentation""", """Data2VecVisionModel""", """Data2VecVisionPreTrainedModel""", ] if is_tf_available(): _UpperCAmelCase : str = [ """TFData2VecVisionForImageClassification""", """TFData2VecVisionForSemanticSegmentation""", """TFData2VecVisionModel""", """TFData2VecVisionPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig from .configuration_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecTextConfig, DataaVecTextOnnxConfig, ) from .configuration_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecVisionConfig, DataaVecVisionOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dataavec_audio import ( DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecAudioForAudioFrameClassification, DataaVecAudioForCTC, DataaVecAudioForSequenceClassification, DataaVecAudioForXVector, DataaVecAudioModel, DataaVecAudioPreTrainedModel, ) from .modeling_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecTextForCausalLM, DataaVecTextForMaskedLM, DataaVecTextForMultipleChoice, DataaVecTextForQuestionAnswering, DataaVecTextForSequenceClassification, DataaVecTextForTokenClassification, DataaVecTextModel, DataaVecTextPreTrainedModel, ) from .modeling_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecVisionForImageClassification, DataaVecVisionForMaskedImageModeling, DataaVecVisionForSemanticSegmentation, DataaVecVisionModel, DataaVecVisionPreTrainedModel, ) if is_tf_available(): from .modeling_tf_dataavec_vision import ( TFDataaVecVisionForImageClassification, TFDataaVecVisionForSemanticSegmentation, TFDataaVecVisionModel, TFDataaVecVisionPreTrainedModel, ) else: import sys _UpperCAmelCase : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
9
'''simple docstring''' import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed _UpperCAmelCase : Dict = """true""" def __magic_name__( lowerCamelCase, lowerCamelCase=8_2, lowerCamelCase=1_6): set_seed(4_2) __lowerCAmelCase = RegressionModel() __lowerCAmelCase = deepcopy(lowerCamelCase) __lowerCAmelCase = RegressionDataset(length=lowerCamelCase) __lowerCAmelCase = DataLoader(lowerCamelCase, batch_size=lowerCamelCase) model.to(accelerator.device) __lowerCAmelCase , __lowerCAmelCase = accelerator.prepare(lowerCamelCase, lowerCamelCase) return model, ddp_model, dataloader def __magic_name__( lowerCamelCase, lowerCamelCase=False): __lowerCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/mrpc-bert-base-cased''') __lowerCAmelCase = load_dataset('''glue''', '''mrpc''', split='''validation''') def tokenize_function(lowerCamelCase): __lowerCAmelCase = tokenizer(examples['''sentence1'''], examples['''sentence2'''], truncation=lowerCamelCase, max_length=lowerCamelCase) return outputs with accelerator.main_process_first(): __lowerCAmelCase = dataset.map( lowerCamelCase, batched=lowerCamelCase, remove_columns=['''idx''', '''sentence1''', '''sentence2'''], ) __lowerCAmelCase = tokenized_datasets.rename_column('''label''', '''labels''') def collate_fn(lowerCamelCase): if use_longest: return tokenizer.pad(lowerCamelCase, padding='''longest''', return_tensors='''pt''') return tokenizer.pad(lowerCamelCase, padding='''max_length''', max_length=1_2_8, return_tensors='''pt''') return DataLoader(lowerCamelCase, shuffle=lowerCamelCase, collate_fn=lowerCamelCase, batch_size=1_6) def __magic_name__( lowerCamelCase, lowerCamelCase): __lowerCAmelCase = Accelerator(dispatch_batches=lowerCamelCase, split_batches=lowerCamelCase) __lowerCAmelCase = get_dataloader(lowerCamelCase, not dispatch_batches) __lowerCAmelCase = AutoModelForSequenceClassification.from_pretrained( '''hf-internal-testing/mrpc-bert-base-cased''', return_dict=lowerCamelCase) __lowerCAmelCase , __lowerCAmelCase = accelerator.prepare(lowerCamelCase, lowerCamelCase) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def __magic_name__( lowerCamelCase, lowerCamelCase, lowerCamelCase): __lowerCAmelCase = [] for batch in dataloader: __lowerCAmelCase , __lowerCAmelCase = batch.values() with torch.no_grad(): __lowerCAmelCase = model(lowerCamelCase) __lowerCAmelCase , __lowerCAmelCase = accelerator.gather_for_metrics((logit, target)) logits_and_targets.append((logit, target)) __lowerCAmelCase , __lowerCAmelCase = [], [] for logit, targ in logits_and_targets: logits.append(lowerCamelCase) targs.append(lowerCamelCase) __lowerCAmelCase , __lowerCAmelCase = torch.cat(lowerCamelCase), torch.cat(lowerCamelCase) return logits, targs def __magic_name__( lowerCamelCase, lowerCamelCase=8_2, lowerCamelCase=False, lowerCamelCase=False, lowerCamelCase=1_6): __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = get_basic_setup(lowerCamelCase, lowerCamelCase, lowerCamelCase) __lowerCAmelCase , __lowerCAmelCase = generate_predictions(lowerCamelCase, lowerCamelCase, lowerCamelCase) assert ( len(lowerCamelCase) == num_samples ), F"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(lowerCamelCase)}""" def __magic_name__( lowerCamelCase = False, lowerCamelCase = False): __lowerCAmelCase = evaluate.load('''glue''', '''mrpc''') __lowerCAmelCase , __lowerCAmelCase = get_mrpc_setup(lowerCamelCase, lowerCamelCase) # First do baseline __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = setup['''no'''] model.to(lowerCamelCase) model.eval() for batch in dataloader: batch.to(lowerCamelCase) with torch.inference_mode(): __lowerCAmelCase = model(**lowerCamelCase) __lowerCAmelCase = outputs.logits.argmax(dim=-1) metric.add_batch(predictions=lowerCamelCase, references=batch['''labels''']) __lowerCAmelCase = metric.compute() # Then do distributed __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = setup['''ddp'''] model.eval() for batch in dataloader: with torch.inference_mode(): __lowerCAmelCase = model(**lowerCamelCase) __lowerCAmelCase = outputs.logits.argmax(dim=-1) __lowerCAmelCase = batch['''labels'''] __lowerCAmelCase , __lowerCAmelCase = accelerator.gather_for_metrics((preds, references)) metric.add_batch(predictions=lowerCamelCase, references=lowerCamelCase) __lowerCAmelCase = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key], distributed[key]), F"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def __magic_name__( ): __lowerCAmelCase = Accelerator(split_batches=lowerCamelCase, dispatch_batches=lowerCamelCase) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('''**Testing gather_for_metrics**''') for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""") test_mrpc(lowerCamelCase, lowerCamelCase) accelerator.state._reset_state() if accelerator.is_local_main_process: print('''**Test torch metrics**''') for split_batches in [True, False]: for dispatch_batches in [True, False]: __lowerCAmelCase = Accelerator(split_batches=lowerCamelCase, dispatch_batches=lowerCamelCase) if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""") test_torch_metrics(lowerCamelCase, 9_9) accelerator.state._reset_state() if accelerator.is_local_main_process: print('''**Test last batch is not dropped when perfectly divisible**''') __lowerCAmelCase = Accelerator() test_torch_metrics(lowerCamelCase, 5_1_2) accelerator.state._reset_state() def __magic_name__( lowerCamelCase): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
9
1
import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor A : List[str] = logging.get_logger(__name__) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , *__lowerCAmelCase : Optional[int] , **__lowerCAmelCase : List[str] ) -> None: """simple docstring""" warnings.warn( """The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use FlavaImageProcessor instead.""" , __lowerCAmelCase , ) super().__init__(*__lowerCAmelCase , **__lowerCAmelCase )
274
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
1
from __future__ import annotations from collections.abc import Iterator class SCREAMING_SNAKE_CASE__ : def __init__(self : List[Any] , a__ : int ): """simple docstring""" __snake_case = value __snake_case = None __snake_case = None class SCREAMING_SNAKE_CASE__ : def __init__(self : Dict , a__ : Node ): """simple docstring""" __snake_case = tree def a (self : List[str] , a__ : Node | None ): """simple docstring""" if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__(self : List[Any] ): """simple docstring""" yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
238
# Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING import numpy as np import pyarrow as pa from .. import config from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import torch class SCREAMING_SNAKE_CASE__ ( TensorFormatter[Mapping, 'torch.Tensor', Mapping] ): def __init__(self : Optional[int] , a__ : Tuple=None , **a__ : Optional[int] ): """simple docstring""" super().__init__(features=a__ ) __snake_case = torch_tensor_kwargs import torch # noqa import torch at initialization def a (self : Union[str, Any] , a__ : Union[str, Any] ): """simple docstring""" import torch if isinstance(a__ , a__ ) and column: if all( isinstance(a__ , torch.Tensor ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return torch.stack(a__ ) return column def a (self : Optional[Any] , a__ : str ): """simple docstring""" import torch if isinstance(a__ , (str, bytes, type(a__ )) ): return value elif isinstance(a__ , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() __snake_case = {} if isinstance(a__ , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.integer ): __snake_case = {'''dtype''': torch.intaa} elif isinstance(a__ , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.floating ): __snake_case = {'''dtype''': torch.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(a__ , PIL.Image.Image ): __snake_case = np.asarray(a__ ) return torch.tensor(a__ , **{**default_dtype, **self.torch_tensor_kwargs} ) def a (self : Optional[int] , a__ : str ): """simple docstring""" import torch # support for torch, tf, jax etc. if hasattr(a__ , '''__array__''' ) and not isinstance(a__ , torch.Tensor ): __snake_case = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(a__ , np.ndarray ): if data_struct.dtype == object: # torch tensors cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(a__ ) for substruct in data_struct] ) elif isinstance(a__ , (list, tuple) ): return self._consolidate([self.recursive_tensorize(a__ ) for substruct in data_struct] ) return self._tensorize(a__ ) def a (self : Optional[Any] , a__ : dict ): """simple docstring""" return map_nested(self._recursive_tensorize , a__ , map_list=a__ ) def a (self : Optional[int] , a__ : pa.Table ): """simple docstring""" __snake_case = self.numpy_arrow_extractor().extract_row(a__ ) __snake_case = self.python_features_decoder.decode_row(a__ ) return self.recursive_tensorize(a__ ) def a (self : List[Any] , a__ : pa.Table ): """simple docstring""" __snake_case = self.numpy_arrow_extractor().extract_column(a__ ) __snake_case = self.python_features_decoder.decode_column(a__ , pa_table.column_names[0] ) __snake_case = self.recursive_tensorize(a__ ) __snake_case = self._consolidate(a__ ) return column def a (self : Optional[int] , a__ : pa.Table ): """simple docstring""" __snake_case = self.numpy_arrow_extractor().extract_batch(a__ ) __snake_case = self.python_features_decoder.decode_batch(a__ ) __snake_case = self.recursive_tensorize(a__ ) for column_name in batch: __snake_case = self._consolidate(batch[column_name] ) return batch
238
1
"""simple docstring""" class lowercase : def __init__( self , lowercase ) -> None: lowerCAmelCase = set_counts lowerCAmelCase = max(lowercase ) lowerCAmelCase = len(lowercase ) lowerCAmelCase = [1] * num_sets lowerCAmelCase = list(range(lowercase ) ) def _snake_case ( self , lowercase , lowercase ) -> bool: lowerCAmelCase = self.get_parent(lowercase ) lowerCAmelCase = self.get_parent(lowercase ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] lowerCAmelCase = 0 lowerCAmelCase = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 lowerCAmelCase = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] lowerCAmelCase = 0 lowerCAmelCase = src_parent lowerCAmelCase = self.set_counts[src_parent] lowerCAmelCase = max(self.max_set , lowercase ) return True def _snake_case ( self , lowercase ) -> int: if self.parents[disj_set] == disj_set: return disj_set lowerCAmelCase = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
46
from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert } class _lowercase ( snake_case_ ): lowercase = 'megatron-bert' def __init__( self : List[str] , snake_case : Tuple=2_9_0_5_6 , snake_case : Dict=1_0_2_4 , snake_case : Dict=2_4 , snake_case : Union[str, Any]=1_6 , snake_case : Optional[int]=4_0_9_6 , snake_case : Optional[int]="gelu" , snake_case : Any=0.1 , snake_case : Tuple=0.1 , snake_case : Optional[int]=5_1_2 , snake_case : List[Any]=2 , snake_case : Tuple=0.02 , snake_case : Optional[Any]=1e-12 , snake_case : str=0 , snake_case : Optional[int]="absolute" , snake_case : Union[str, Any]=True , **snake_case : Any , ) -> List[Any]: """simple docstring""" super().__init__(pad_token_id=snake_case , **snake_case ) UpperCamelCase_ : Optional[Any] = vocab_size UpperCamelCase_ : Any = hidden_size UpperCamelCase_ : Union[str, Any] = num_hidden_layers UpperCamelCase_ : List[Any] = num_attention_heads UpperCamelCase_ : str = hidden_act UpperCamelCase_ : List[str] = intermediate_size UpperCamelCase_ : List[Any] = hidden_dropout_prob UpperCamelCase_ : Any = attention_probs_dropout_prob UpperCamelCase_ : Tuple = max_position_embeddings UpperCamelCase_ : Dict = type_vocab_size UpperCamelCase_ : Optional[int] = initializer_range UpperCamelCase_ : Optional[Any] = layer_norm_eps UpperCamelCase_ : Dict = position_embedding_type UpperCamelCase_ : List[str] = use_cache
175
0
'''simple docstring''' import copy from dataclasses import dataclass from pathlib import Path from typing import Dict, Optional, Union @dataclass class __magic_name__ : UpperCamelCase__ = None UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = None UpperCamelCase__ = None UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = True UpperCamelCase__ = None UpperCamelCase__ = 1 UpperCamelCase__ = None UpperCamelCase__ = False UpperCamelCase__ = None UpperCamelCase__ = None def SCREAMING_SNAKE_CASE_ ( self : Tuple ): return self.__class__(**{k: copy.deepcopy(lowercase_ ) for k, v in self.__dict__.items()} )
21
'''simple docstring''' def lowerCamelCase ( ) -> Dict: lowercase_ : Union[str, Any] = [] lowercase_ : Tuple = 1 while len(UpperCAmelCase__ ) < 1e6: constant.append(str(UpperCAmelCase__ ) ) i += 1 lowercase_ : int = """""".join(UpperCAmelCase__ ) return ( int(constant[0] ) * int(constant[9] ) * int(constant[99] ) * int(constant[999] ) * int(constant[9999] ) * int(constant[99999] ) * int(constant[999999] ) ) if __name__ == "__main__": print(solution())
21
1
import random def A ( _lowercase , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE : Union[str, Any] = a[left_index] SCREAMING_SNAKE_CASE : Optional[int] = left_index + 1 for j in range(left_index + 1 , _lowercase ): if a[j] < pivot: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = a[i], a[j] i += 1 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = a[i - 1], a[left_index] return i - 1 def A ( _lowercase , _lowercase , _lowercase ): if left < right: SCREAMING_SNAKE_CASE : List[Any] = random.randint(_lowercase , right - 1 ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[Any] = ( a[left], a[pivot], ) # switches the pivot with the left most bound SCREAMING_SNAKE_CASE : str = partition(_lowercase , _lowercase , _lowercase ) quick_sort_random( _lowercase , _lowercase , _lowercase ) # recursive quicksort to the left of the pivot point quick_sort_random( _lowercase , pivot_index + 1 , _lowercase ) # recursive quicksort to the right of the pivot point def A ( ): SCREAMING_SNAKE_CASE : Tuple = input('''Enter numbers separated by a comma:\n''' ).strip() SCREAMING_SNAKE_CASE : int = [int(_lowercase ) for item in user_input.split(''',''' )] quick_sort_random(_lowercase , 0 , len(_lowercase ) ) print(_lowercase ) if __name__ == "__main__": main()
182
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( '''split_dict''' , [ SplitDict(), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_337 , num_examples=42 , dataset_name='''my_dataset''' )} ), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_337 , num_examples=42 )} ), SplitDict({'''train''': SplitInfo()} ), ] , ) def A ( _lowercase ): SCREAMING_SNAKE_CASE : Tuple = split_dict._to_yaml_list() assert len(_lowercase ) == len(_lowercase ) SCREAMING_SNAKE_CASE : Tuple = SplitDict._from_yaml_list(_lowercase ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump SCREAMING_SNAKE_CASE : Any = None # the split name of split_dict takes over the name of the split info object SCREAMING_SNAKE_CASE : Optional[Any] = split_name assert split_dict == reloaded @pytest.mark.parametrize( '''split_info''' , [SplitInfo(), SplitInfo(dataset_name=_lowercase ), SplitInfo(dataset_name='''my_dataset''' )] ) def A ( _lowercase ): # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files SCREAMING_SNAKE_CASE : List[Any] = asdict(SplitDict({'''train''': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
182
1
import argparse import os import jax as jnp import numpy as onp import torch import torch.nn as nn from music_spectrogram_diffusion import inference from tax import checkpoints from diffusers import DDPMScheduler, OnnxRuntimeModel, SpectrogramDiffusionPipeline from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder UpperCamelCase = """base_with_context""" def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A_ : List[str] = nn.Parameter(torch.FloatTensor(weights['''token_embedder''']['''embedding'''] ) ) A_ : Optional[int] = nn.Parameter( torch.FloatTensor(weights['''Embed_0''']['''embedding'''] ) , requires_grad=SCREAMING_SNAKE_CASE ) for lyr_num, lyr in enumerate(model.encoders ): A_ : List[Any] = weights[f'''layers_{lyr_num}'''] A_ : Any = nn.Parameter( torch.FloatTensor(ly_weight['''pre_attention_layer_norm''']['''scale'''] ) ) A_ : List[Any] = ly_weight['''attention'''] A_ : Optional[Any] = nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T ) ) A_ : Any = nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T ) ) A_ : Union[str, Any] = nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T ) ) A_ : Optional[Any] = nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T ) ) A_ : Tuple = nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale'''] ) ) A_ : Tuple = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T ) ) A_ : Any = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T ) ) A_ : Optional[int] = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T ) ) A_ : List[str] = nn.Parameter(torch.FloatTensor(weights['''encoder_norm''']['''scale'''] ) ) return model def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A_ : Any = nn.Parameter(torch.FloatTensor(weights['''input_proj''']['''kernel'''].T ) ) A_ : Dict = nn.Parameter( torch.FloatTensor(weights['''Embed_0''']['''embedding'''] ) , requires_grad=SCREAMING_SNAKE_CASE ) for lyr_num, lyr in enumerate(model.encoders ): A_ : List[str] = weights[f'''layers_{lyr_num}'''] A_ : Optional[int] = ly_weight['''attention'''] A_ : Tuple = nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T ) ) A_ : List[Any] = nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T ) ) A_ : Dict = nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T ) ) A_ : Any = nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T ) ) A_ : List[Any] = nn.Parameter( torch.FloatTensor(ly_weight['''pre_attention_layer_norm''']['''scale'''] ) ) A_ : List[str] = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T ) ) A_ : Tuple = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T ) ) A_ : List[str] = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T ) ) A_ : List[Any] = nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale'''] ) ) A_ : Optional[int] = nn.Parameter(torch.FloatTensor(weights['''encoder_norm''']['''scale'''] ) ) return model def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A_ : Any = nn.Parameter(torch.FloatTensor(weights['''time_emb_dense0''']['''kernel'''].T ) ) A_ : Tuple = nn.Parameter(torch.FloatTensor(weights['''time_emb_dense1''']['''kernel'''].T ) ) A_ : List[str] = nn.Parameter( torch.FloatTensor(weights['''Embed_0''']['''embedding'''] ) , requires_grad=SCREAMING_SNAKE_CASE ) A_ : Dict = nn.Parameter( torch.FloatTensor(weights['''continuous_inputs_projection''']['''kernel'''].T ) ) for lyr_num, lyr in enumerate(model.decoders ): A_ : Any = weights[f'''layers_{lyr_num}'''] A_ : str = nn.Parameter( torch.FloatTensor(ly_weight['''pre_self_attention_layer_norm''']['''scale'''] ) ) A_ : Dict = nn.Parameter( torch.FloatTensor(ly_weight['''FiLMLayer_0''']['''DenseGeneral_0''']['''kernel'''].T ) ) A_ : Tuple = ly_weight['''self_attention'''] A_ : Optional[Any] = nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T ) ) A_ : Union[str, Any] = nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T ) ) A_ : List[Any] = nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T ) ) A_ : Optional[int] = nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T ) ) A_ : Optional[Any] = ly_weight['''MultiHeadDotProductAttention_0'''] A_ : int = nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T ) ) A_ : Optional[Any] = nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T ) ) A_ : List[Any] = nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T ) ) A_ : Dict = nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T ) ) A_ : Any = nn.Parameter( torch.FloatTensor(ly_weight['''pre_cross_attention_layer_norm''']['''scale'''] ) ) A_ : str = nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale'''] ) ) A_ : List[str] = nn.Parameter( torch.FloatTensor(ly_weight['''FiLMLayer_1''']['''DenseGeneral_0''']['''kernel'''].T ) ) A_ : str = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T ) ) A_ : Optional[Any] = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T ) ) A_ : Any = nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T ) ) A_ : Optional[Any] = nn.Parameter(torch.FloatTensor(weights['''decoder_norm''']['''scale'''] ) ) A_ : Union[str, Any] = nn.Parameter(torch.FloatTensor(weights['''spec_out_dense''']['''kernel'''].T ) ) return model def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE ): A_ : Tuple = checkpoints.load_tax_checkpoint(args.checkpoint_path ) A_ : Any = jnp.tree_util.tree_map(onp.array , SCREAMING_SNAKE_CASE ) A_ : str = [ '''from __gin__ import dynamic_registration''', '''from music_spectrogram_diffusion.models.diffusion import diffusion_utils''', '''diffusion_utils.ClassifierFreeGuidanceConfig.eval_condition_weight = 2.0''', '''diffusion_utils.DiffusionConfig.classifier_free_guidance = @diffusion_utils.ClassifierFreeGuidanceConfig()''', ] A_ : str = os.path.join(args.checkpoint_path , '''..''' , '''config.gin''' ) A_ : Union[str, Any] = inference.parse_training_gin_file(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) A_ : Optional[int] = inference.InferenceModel(args.checkpoint_path , SCREAMING_SNAKE_CASE ) A_ : Tuple = DDPMScheduler(beta_schedule='''squaredcos_cap_v2''' , variance_type='''fixed_large''' ) A_ : str = SpectrogramNotesEncoder( max_length=synth_model.sequence_length['''inputs'''] , vocab_size=synth_model.model.module.config.vocab_size , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj='''gated-gelu''' , ) A_ : Optional[Any] = SpectrogramContEncoder( input_dims=synth_model.audio_codec.n_dims , targets_context_length=synth_model.sequence_length['''targets_context'''] , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj='''gated-gelu''' , ) A_ : List[Any] = TaFilmDecoder( input_dims=synth_model.audio_codec.n_dims , targets_length=synth_model.sequence_length['''targets_context'''] , max_decoder_noise_time=synth_model.model.module.config.max_decoder_noise_time , d_model=synth_model.model.module.config.emb_dim , num_layers=synth_model.model.module.config.num_decoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , dropout_rate=synth_model.model.module.config.dropout_rate , ) A_ : str = load_notes_encoder(ta_checkpoint['''target''']['''token_encoder'''] , SCREAMING_SNAKE_CASE ) A_ : Optional[int] = load_continuous_encoder(ta_checkpoint['''target''']['''continuous_encoder'''] , SCREAMING_SNAKE_CASE ) A_ : Tuple = load_decoder(ta_checkpoint['''target''']['''decoder'''] , SCREAMING_SNAKE_CASE ) A_ : Any = OnnxRuntimeModel.from_pretrained('''kashif/soundstream_mel_decoder''' ) A_ : Dict = SpectrogramDiffusionPipeline( notes_encoder=SCREAMING_SNAKE_CASE , continuous_encoder=SCREAMING_SNAKE_CASE , decoder=SCREAMING_SNAKE_CASE , scheduler=SCREAMING_SNAKE_CASE , melgan=SCREAMING_SNAKE_CASE , ) if args.save: pipe.save_pretrained(args.output_path ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument("""--output_path""", default=None, type=str, required=True, help="""Path to the converted model.""") parser.add_argument( """--save""", default=True, type=bool, required=False, help="""Whether to save the converted model or not.""" ) parser.add_argument( """--checkpoint_path""", default=F'''{MODEL}/checkpoint_500000''', type=str, required=False, help="""Path to the original jax model checkpoint.""", ) UpperCamelCase = parser.parse_args() main(args)
65
def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): # Check if the input is valid if not len(SCREAMING_SNAKE_CASE ) == len(SCREAMING_SNAKE_CASE ) == 3: raise ValueError('''Please enter a valid equation.''' ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError('''Both a & b of two equations can\'t be zero.''' ) # Extract the coefficients A_ , A_ , A_ : Any = equationa A_ , A_ , A_ : Union[str, Any] = equationa # Calculate the determinants of the matrices A_ : Optional[Any] = aa * ba - aa * ba A_ : Optional[int] = ca * ba - ca * ba A_ : List[Any] = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError('''Infinite solutions. (Consistent system)''' ) else: raise ValueError('''No solution. (Inconsistent system)''' ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: A_ : Optional[int] = determinant_x / determinant A_ : List[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
65
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __lowerCAmelCase : Union[str, Any] ={ 'configuration_data2vec_audio': ['DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Data2VecAudioConfig'], 'configuration_data2vec_text': [ 'DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Data2VecTextConfig', 'Data2VecTextOnnxConfig', ], 'configuration_data2vec_vision': [ 'DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Data2VecVisionConfig', 'Data2VecVisionOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Optional[int] =[ 'DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST', 'Data2VecAudioForAudioFrameClassification', 'Data2VecAudioForCTC', 'Data2VecAudioForSequenceClassification', 'Data2VecAudioForXVector', 'Data2VecAudioModel', 'Data2VecAudioPreTrainedModel', ] __lowerCAmelCase : List[str] =[ 'DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST', 'Data2VecTextForCausalLM', 'Data2VecTextForMaskedLM', 'Data2VecTextForMultipleChoice', 'Data2VecTextForQuestionAnswering', 'Data2VecTextForSequenceClassification', 'Data2VecTextForTokenClassification', 'Data2VecTextModel', 'Data2VecTextPreTrainedModel', ] __lowerCAmelCase : str =[ 'DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST', 'Data2VecVisionForImageClassification', 'Data2VecVisionForMaskedImageModeling', 'Data2VecVisionForSemanticSegmentation', 'Data2VecVisionModel', 'Data2VecVisionPreTrainedModel', ] if is_tf_available(): __lowerCAmelCase : str =[ 'TFData2VecVisionForImageClassification', 'TFData2VecVisionForSemanticSegmentation', 'TFData2VecVisionModel', 'TFData2VecVisionPreTrainedModel', ] if TYPE_CHECKING: from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig from .configuration_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecTextConfig, DataaVecTextOnnxConfig, ) from .configuration_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecVisionConfig, DataaVecVisionOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dataavec_audio import ( DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecAudioForAudioFrameClassification, DataaVecAudioForCTC, DataaVecAudioForSequenceClassification, DataaVecAudioForXVector, DataaVecAudioModel, DataaVecAudioPreTrainedModel, ) from .modeling_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecTextForCausalLM, DataaVecTextForMaskedLM, DataaVecTextForMultipleChoice, DataaVecTextForQuestionAnswering, DataaVecTextForSequenceClassification, DataaVecTextForTokenClassification, DataaVecTextModel, DataaVecTextPreTrainedModel, ) from .modeling_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecVisionForImageClassification, DataaVecVisionForMaskedImageModeling, DataaVecVisionForSemanticSegmentation, DataaVecVisionModel, DataaVecVisionPreTrainedModel, ) if is_tf_available(): from .modeling_tf_dataavec_vision import ( TFDataaVecVisionForImageClassification, TFDataaVecVisionForSemanticSegmentation, TFDataaVecVisionModel, TFDataaVecVisionPreTrainedModel, ) else: import sys __lowerCAmelCase : Optional[int] =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
from __future__ import annotations def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Dict = 0.00 __SCREAMING_SNAKE_CASE : List[str] = 0 for resistor in resistors: if resistor <= 0: __SCREAMING_SNAKE_CASE : Any = F'''Resistor at index {index} has a negative or zero value!''' raise ValueError(lowercase__ ) first_sum += 1 / float(lowercase__ ) index += 1 return 1 / first_sum def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Tuple = 0.00 __SCREAMING_SNAKE_CASE : int = 0 for resistor in resistors: sum_r += resistor if resistor < 0: __SCREAMING_SNAKE_CASE : Tuple = F'''Resistor at index {index} has a negative value!''' raise ValueError(lowercase__ ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
9
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A__ = {'''configuration_vit_msn''': ['''VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMSNConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A__ = [ '''VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ViTMSNModel''', '''ViTMSNForImageClassification''', '''ViTMSNPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys A__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
44
A__ = 0 # The first color of the flag. A__ = 1 # The second color of the flag. A__ = 2 # The third color of the flag. A__ = (red, white, blue) def _lowerCAmelCase ( __lowerCAmelCase ) -> list: """simple docstring""" if not sequence: return [] if len(__lowerCAmelCase ) == 1: return list(__lowerCAmelCase ) snake_case__ : List[Any] = 0 snake_case__ : str = len(__lowerCAmelCase ) - 1 snake_case__ : List[Any] = 0 while mid <= high: if sequence[mid] == colors[0]: snake_case__ , snake_case__ : List[Any] = sequence[mid], sequence[low] low += 1 mid += 1 elif sequence[mid] == colors[1]: mid += 1 elif sequence[mid] == colors[2]: snake_case__ , snake_case__ : int = sequence[high], sequence[mid] high -= 1 else: snake_case__ : List[Any] = f"""The elements inside the sequence must contains only {colors} values""" raise ValueError(__lowerCAmelCase ) return sequence if __name__ == "__main__": import doctest doctest.testmod() A__ = input('''Enter numbers separated by commas:\n''').strip() A__ = [int(item.strip()) for item in user_input.split(''',''')] print(f"""{dutch_national_flag_sort(unsorted)}""")
44
1
"""simple docstring""" import gc import unittest from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def snake_case ( self : int )-> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() def snake_case ( self : int )-> str: lowerCamelCase__ , lowerCamelCase__ : Optional[int] =FlaxControlNetModel.from_pretrained( '''lllyasviel/sd-controlnet-canny''', from_pt=lowerCamelCase, dtype=jnp.bfloataa ) lowerCamelCase__ , lowerCamelCase__ : Optional[int] =FlaxStableDiffusionControlNetPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''', controlnet=lowerCamelCase, from_pt=lowerCamelCase, dtype=jnp.bfloataa ) lowerCamelCase__ : Optional[Any] =controlnet_params lowerCamelCase__ : List[str] ='''bird''' lowerCamelCase__ : str =jax.device_count() lowerCamelCase__ : Any =pipe.prepare_text_inputs([prompts] * num_samples ) lowerCamelCase__ : Any =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png''' ) lowerCamelCase__ : Optional[int] =pipe.prepare_image_inputs([canny_image] * num_samples ) lowerCamelCase__ : Optional[Any] =jax.random.PRNGKey(0 ) lowerCamelCase__ : List[str] =jax.random.split(lowerCamelCase, jax.device_count() ) lowerCamelCase__ : Optional[Any] =replicate(lowerCamelCase ) lowerCamelCase__ : List[Any] =shard(lowerCamelCase ) lowerCamelCase__ : Union[str, Any] =shard(lowerCamelCase ) lowerCamelCase__ : Any =pipe( prompt_ids=lowerCamelCase, image=lowerCamelCase, params=lowerCamelCase, prng_seed=lowerCamelCase, num_inference_steps=50, jit=lowerCamelCase, ).images assert images.shape == (jax.device_count(), 1, 768, 512, 3) lowerCamelCase__ : Union[str, Any] =images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) lowerCamelCase__ : Optional[Any] =images[0, 253:256, 253:256, -1] lowerCamelCase__ : List[Any] =jnp.asarray(jax.device_get(image_slice.flatten() ) ) lowerCamelCase__ : List[str] =jnp.array( [0.167_969, 0.116_699, 0.081_543, 0.154_297, 0.132_812, 0.108_887, 0.169_922, 0.169_922, 0.205_078] ) print(F'''output_slice: {output_slice}''' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2 def snake_case ( self : str )-> List[Any]: lowerCamelCase__ , lowerCamelCase__ : Optional[int] =FlaxControlNetModel.from_pretrained( '''lllyasviel/sd-controlnet-openpose''', from_pt=lowerCamelCase, dtype=jnp.bfloataa ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =FlaxStableDiffusionControlNetPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''', controlnet=lowerCamelCase, from_pt=lowerCamelCase, dtype=jnp.bfloataa ) lowerCamelCase__ : Union[str, Any] =controlnet_params lowerCamelCase__ : List[str] ='''Chef in the kitchen''' lowerCamelCase__ : Any =jax.device_count() lowerCamelCase__ : List[Any] =pipe.prepare_text_inputs([prompts] * num_samples ) lowerCamelCase__ : str =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png''' ) lowerCamelCase__ : str =pipe.prepare_image_inputs([pose_image] * num_samples ) lowerCamelCase__ : Optional[Any] =jax.random.PRNGKey(0 ) lowerCamelCase__ : Dict =jax.random.split(lowerCamelCase, jax.device_count() ) lowerCamelCase__ : Any =replicate(lowerCamelCase ) lowerCamelCase__ : List[Any] =shard(lowerCamelCase ) lowerCamelCase__ : Tuple =shard(lowerCamelCase ) lowerCamelCase__ : Optional[Any] =pipe( prompt_ids=lowerCamelCase, image=lowerCamelCase, params=lowerCamelCase, prng_seed=lowerCamelCase, num_inference_steps=50, jit=lowerCamelCase, ).images assert images.shape == (jax.device_count(), 1, 768, 512, 3) lowerCamelCase__ : Dict =images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) lowerCamelCase__ : Union[str, Any] =images[0, 253:256, 253:256, -1] lowerCamelCase__ : Optional[int] =jnp.asarray(jax.device_get(image_slice.flatten() ) ) lowerCamelCase__ : Optional[Any] =jnp.array( [[0.271_484, 0.261_719, 0.275_391, 0.277_344, 0.279_297, 0.291_016, 0.294_922, 0.302_734, 0.302_734]] ) print(F'''output_slice: {output_slice}''' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
238
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType _lowercase : List[str] = logging.get_logger(__name__) _lowercase : int = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): '''simple docstring''' _a = 'deberta-v2' def __init__( self : Optional[Any], lowerCamelCase : Optional[int]=12_8100, lowerCamelCase : List[Any]=1536, lowerCamelCase : Dict=24, lowerCamelCase : Any=24, lowerCamelCase : Union[str, Any]=6144, lowerCamelCase : List[Any]="gelu", lowerCamelCase : int=0.1, lowerCamelCase : Union[str, Any]=0.1, lowerCamelCase : Union[str, Any]=512, lowerCamelCase : Optional[Any]=0, lowerCamelCase : Any=0.02, lowerCamelCase : int=1E-7, lowerCamelCase : Union[str, Any]=False, lowerCamelCase : Union[str, Any]=-1, lowerCamelCase : Tuple=0, lowerCamelCase : Union[str, Any]=True, lowerCamelCase : int=None, lowerCamelCase : Dict=0, lowerCamelCase : Tuple="gelu", **lowerCamelCase : Optional[int], )-> Union[str, Any]: super().__init__(**lowerCamelCase ) lowerCamelCase__ : str =hidden_size lowerCamelCase__ : Optional[int] =num_hidden_layers lowerCamelCase__ : Optional[Any] =num_attention_heads lowerCamelCase__ : List[Any] =intermediate_size lowerCamelCase__ : int =hidden_act lowerCamelCase__ : Tuple =hidden_dropout_prob lowerCamelCase__ : Union[str, Any] =attention_probs_dropout_prob lowerCamelCase__ : Optional[Any] =max_position_embeddings lowerCamelCase__ : int =type_vocab_size lowerCamelCase__ : Tuple =initializer_range lowerCamelCase__ : Tuple =relative_attention lowerCamelCase__ : Optional[Any] =max_relative_positions lowerCamelCase__ : List[Any] =pad_token_id lowerCamelCase__ : int =position_biased_input # Backwards compatibility if type(lowerCamelCase ) == str: lowerCamelCase__ : Union[str, Any] =[x.strip() for x in pos_att_type.lower().split('''|''' )] lowerCamelCase__ : Tuple =pos_att_type lowerCamelCase__ : Union[str, Any] =vocab_size lowerCamelCase__ : Optional[int] =layer_norm_eps lowerCamelCase__ : Dict =kwargs.get('''pooler_hidden_size''', lowerCamelCase ) lowerCamelCase__ : Tuple =pooler_dropout lowerCamelCase__ : List[Any] =pooler_hidden_act class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): '''simple docstring''' @property def snake_case ( self : List[str] )-> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": lowerCamelCase__ : Union[str, Any] ={0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: lowerCamelCase__ : Any ={0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def snake_case ( self : List[str] )-> int: return 12 def snake_case ( self : str, lowerCamelCase : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"], lowerCamelCase : int = -1, lowerCamelCase : int = -1, lowerCamelCase : int = -1, lowerCamelCase : bool = False, lowerCamelCase : Optional["TensorType"] = None, lowerCamelCase : int = 3, lowerCamelCase : int = 40, lowerCamelCase : int = 40, lowerCamelCase : "PreTrainedTokenizerBase" = None, )-> Mapping[str, Any]: lowerCamelCase__ : List[Any] =super().generate_dummy_inputs(preprocessor=lowerCamelCase, framework=lowerCamelCase ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
238
1
import os import pytest import yaml from datasets.features.features import Features, Value from datasets.info import DatasetInfo, DatasetInfosDict @pytest.mark.parametrize( '''files''' , [ ['''full:README.md''', '''dataset_infos.json'''], ['''empty:README.md''', '''dataset_infos.json'''], ['''dataset_infos.json'''], ['''full:README.md'''], ] , ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowercase__ = tmp_path_factory.mktemp('''dset_infos_dir''' ) if "full:README.md" in files: with open(dataset_infos_dir / '''README.md''' , '''w''' ) as f: f.write('''---\ndataset_info:\n dataset_size: 42\n---''' ) if "empty:README.md" in files: with open(dataset_infos_dir / '''README.md''' , '''w''' ) as f: f.write('''''' ) # we want to support dataset_infos.json for backward compatibility if "dataset_infos.json" in files: with open(dataset_infos_dir / '''dataset_infos.json''' , '''w''' ) as f: f.write('''{"default": {"dataset_size": 42}}''' ) lowercase__ = DatasetInfosDict.from_directory(SCREAMING_SNAKE_CASE ) assert dataset_infos assert dataset_infos["default"].dataset_size == 42 @pytest.mark.parametrize( '''dataset_info''' , [ DatasetInfo(), DatasetInfo( description='''foo''' , features=Features({'''a''': Value('''int32''' )} ) , builder_name='''builder''' , config_name='''config''' , version='''1.0.0''' , splits=[{'''name''': '''train'''}] , download_size=42 , ), ] , ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowercase__ = str(SCREAMING_SNAKE_CASE ) dataset_info.write_to_directory(SCREAMING_SNAKE_CASE ) lowercase__ = DatasetInfo.from_directory(SCREAMING_SNAKE_CASE ) assert dataset_info == reloaded assert os.path.exists(os.path.join(SCREAMING_SNAKE_CASE , '''dataset_info.json''' ) ) def _a ( ): lowercase__ = DatasetInfo( description='''foo''' , citation='''bar''' , homepage='''https://foo.bar''' , license='''CC0''' , features=Features({'''a''': Value('''int32''' )} ) , post_processed={} , supervised_keys=() , task_templates=[] , builder_name='''builder''' , config_name='''config''' , version='''1.0.0''' , splits=[{'''name''': '''train''', '''num_examples''': 42}] , download_checksums={} , download_size=13_37 , post_processing_size=4_42 , dataset_size=12_34 , size_in_bytes=13_37 + 4_42 + 12_34 , ) lowercase__ = dataset_info._to_yaml_dict() assert sorted(SCREAMING_SNAKE_CASE ) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML ) for key in DatasetInfo._INCLUDED_INFO_IN_YAML: assert key in dataset_info_yaml_dict assert isinstance(dataset_info_yaml_dict[key] , (list, dict, int, str) ) lowercase__ = yaml.safe_dump(SCREAMING_SNAKE_CASE ) lowercase__ = yaml.safe_load(SCREAMING_SNAKE_CASE ) assert dataset_info_yaml_dict == reloaded def _a ( ): lowercase__ = DatasetInfo() lowercase__ = dataset_info._to_yaml_dict() assert dataset_info_yaml_dict == {} @pytest.mark.parametrize( '''dataset_infos_dict''' , [ DatasetInfosDict(), DatasetInfosDict({'''default''': DatasetInfo()} ), DatasetInfosDict({'''my_config_name''': DatasetInfo()} ), DatasetInfosDict( { '''default''': DatasetInfo( description='''foo''' , features=Features({'''a''': Value('''int32''' )} ) , builder_name='''builder''' , config_name='''config''' , version='''1.0.0''' , splits=[{'''name''': '''train'''}] , download_size=42 , ) } ), DatasetInfosDict( { '''v1''': DatasetInfo(dataset_size=42 ), '''v2''': DatasetInfo(dataset_size=13_37 ), } ), ] , ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowercase__ = str(SCREAMING_SNAKE_CASE ) dataset_infos_dict.write_to_directory(SCREAMING_SNAKE_CASE ) lowercase__ = DatasetInfosDict.from_directory(SCREAMING_SNAKE_CASE ) # the config_name of the dataset_infos_dict take over the attribute for config_name, dataset_info in dataset_infos_dict.items(): lowercase__ = config_name # the yaml representation doesn't include fields like description or citation # so we just test that we can recover what we can from the yaml lowercase__ = DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict() ) assert dataset_infos_dict == reloaded if dataset_infos_dict: assert os.path.exists(os.path.join(SCREAMING_SNAKE_CASE , '''README.md''' ) )
357
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, ) lowerCAmelCase = logging.getLogger(__name__) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = git.Repo(search_parent_directories=SCREAMING_SNAKE_CASE ) lowercase__ = { '''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 _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" if params.n_gpu <= 0: lowercase__ = 0 lowercase__ = -1 lowercase__ = True lowercase__ = False return assert torch.cuda.is_available() logger.info('''Initializing GPUs''' ) if params.n_gpu > 1: assert params.local_rank != -1 lowercase__ = int(os.environ['''WORLD_SIZE'''] ) lowercase__ = int(os.environ['''N_GPU_NODE'''] ) lowercase__ = int(os.environ['''RANK'''] ) # number of nodes / node ID lowercase__ = params.world_size // params.n_gpu_per_node lowercase__ = params.global_rank // params.n_gpu_per_node lowercase__ = 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 lowercase__ = 1 lowercase__ = 0 lowercase__ = 0 lowercase__ = 0 lowercase__ = 1 lowercase__ = 1 lowercase__ = 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 lowercase__ = params.node_id == 0 and params.local_rank == 0 lowercase__ = params.n_nodes > 1 # summary lowercase__ = 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 _a ( 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 )
93
0
class _lowerCamelCase: def __init__( self) -> Dict: """simple docstring""" _lowercase : Union[str, Any] = {} def UpperCamelCase ( self) -> None: """simple docstring""" print(self.vertex) for i in self.vertex: print(lowerCamelCase, ' -> ', ' -> '.join([str(lowerCamelCase) for j in self.vertex[i]])) def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase) -> None: """simple docstring""" if from_vertex in self.vertex: self.vertex[from_vertex].append(lowerCamelCase) else: # else make a new vertex _lowercase : Optional[Any] = [to_vertex] def UpperCamelCase ( self) -> None: """simple docstring""" _lowercase : Dict = [False] * len(self.vertex) # call the recursive helper function for i in range(len(self.vertex)): if not visited[i]: self.dfs_recursive(lowerCamelCase, lowerCamelCase) def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase) -> None: """simple docstring""" _lowercase : List[Any] = True print(lowerCamelCase, end=' ') # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(lowerCamelCase, lowerCamelCase) if __name__ == "__main__": SCREAMING_SNAKE_CASE : Optional[Any] = 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
21
import gc import unittest import numpy as np import torch from torch.backends.cuda import sdp_kernel from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) from diffusers.utils import randn_tensor, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _lowerCamelCase( _a, unittest.TestCase ): lowercase_ : Union[str, Any] = ConsistencyModelPipeline lowercase_ : Tuple = UNCONDITIONAL_IMAGE_GENERATION_PARAMS lowercase_ : List[str] = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS # Override required_optional_params to remove num_images_per_prompt lowercase_ : List[str] = frozenset( [ """num_inference_steps""", """generator""", """latents""", """output_type""", """return_dict""", """callback""", """callback_steps""", ] ) @property def UpperCamelCase ( self) -> Tuple: """simple docstring""" _lowercase : Tuple = UNetaDModel.from_pretrained( 'diffusers/consistency-models-test', subfolder='test_unet', ) return unet @property def UpperCamelCase ( self) -> List[Any]: """simple docstring""" _lowercase : Tuple = UNetaDModel.from_pretrained( 'diffusers/consistency-models-test', subfolder='test_unet_class_cond', ) return unet def UpperCamelCase ( self, lowerCamelCase=False) -> Dict: """simple docstring""" if class_cond: _lowercase : Union[str, Any] = self.dummy_cond_unet else: _lowercase : Union[str, Any] = self.dummy_uncond_unet # Default to CM multistep sampler _lowercase : List[str] = CMStochasticIterativeScheduler( num_train_timesteps=40, sigma_min=0.0_0_2, sigma_max=8_0.0, ) _lowercase : Optional[Any] = { 'unet': unet, 'scheduler': scheduler, } return components def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase=0) -> Tuple: """simple docstring""" if str(lowerCamelCase).startswith('mps'): _lowercase : str = torch.manual_seed(lowerCamelCase) else: _lowercase : int = torch.Generator(device=lowerCamelCase).manual_seed(lowerCamelCase) _lowercase : Tuple = { 'batch_size': 1, 'num_inference_steps': None, 'timesteps': [22, 0], 'generator': generator, 'output_type': 'np', } return inputs def UpperCamelCase ( self) -> Any: """simple docstring""" _lowercase : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator _lowercase : Optional[int] = self.get_dummy_components() _lowercase : str = ConsistencyModelPipeline(**lowerCamelCase) _lowercase : Dict = pipe.to(lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : Tuple = self.get_dummy_inputs(lowerCamelCase) _lowercase : Optional[int] = pipe(**lowerCamelCase).images assert image.shape == (1, 32, 32, 3) _lowercase : int = image[0, -3:, -3:, -1] _lowercase : Dict = np.array([0.3_5_7_2, 0.6_2_7_3, 0.4_0_3_1, 0.3_9_6_1, 0.4_3_2_1, 0.5_7_3_0, 0.5_2_6_6, 0.4_7_8_0, 0.5_0_0_4]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3 def UpperCamelCase ( self) -> Any: """simple docstring""" _lowercase : Union[str, Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator _lowercase : Dict = self.get_dummy_components(class_cond=lowerCamelCase) _lowercase : Any = ConsistencyModelPipeline(**lowerCamelCase) _lowercase : str = pipe.to(lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : Any = self.get_dummy_inputs(lowerCamelCase) _lowercase : Any = 0 _lowercase : List[str] = pipe(**lowerCamelCase).images assert image.shape == (1, 32, 32, 3) _lowercase : Any = image[0, -3:, -3:, -1] _lowercase : Union[str, Any] = np.array([0.3_5_7_2, 0.6_2_7_3, 0.4_0_3_1, 0.3_9_6_1, 0.4_3_2_1, 0.5_7_3_0, 0.5_2_6_6, 0.4_7_8_0, 0.5_0_0_4]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3 def UpperCamelCase ( self) -> Tuple: """simple docstring""" _lowercase : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator _lowercase : Any = self.get_dummy_components() _lowercase : Optional[Any] = ConsistencyModelPipeline(**lowerCamelCase) _lowercase : List[str] = pipe.to(lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : Optional[Any] = self.get_dummy_inputs(lowerCamelCase) _lowercase : Union[str, Any] = 1 _lowercase : Tuple = None _lowercase : Tuple = pipe(**lowerCamelCase).images assert image.shape == (1, 32, 32, 3) _lowercase : str = image[0, -3:, -3:, -1] _lowercase : List[str] = np.array([0.5_0_0_4, 0.5_0_0_4, 0.4_9_9_4, 0.5_0_0_8, 0.4_9_7_6, 0.5_0_1_8, 0.4_9_9_0, 0.4_9_8_2, 0.4_9_8_7]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3 def UpperCamelCase ( self) -> str: """simple docstring""" _lowercase : Union[str, Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator _lowercase : Dict = self.get_dummy_components(class_cond=lowerCamelCase) _lowercase : Dict = ConsistencyModelPipeline(**lowerCamelCase) _lowercase : Optional[Any] = pipe.to(lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : Tuple = self.get_dummy_inputs(lowerCamelCase) _lowercase : Tuple = 1 _lowercase : int = None _lowercase : Tuple = 0 _lowercase : Dict = pipe(**lowerCamelCase).images assert image.shape == (1, 32, 32, 3) _lowercase : List[str] = image[0, -3:, -3:, -1] _lowercase : Any = np.array([0.5_0_0_4, 0.5_0_0_4, 0.4_9_9_4, 0.5_0_0_8, 0.4_9_7_6, 0.5_0_1_8, 0.4_9_9_0, 0.4_9_8_2, 0.4_9_8_7]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3 @slow @require_torch_gpu class _lowerCamelCase( unittest.TestCase ): def UpperCamelCase ( self) -> Union[str, Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self, lowerCamelCase=0, lowerCamelCase=False, lowerCamelCase="cpu", lowerCamelCase=torch.floataa, lowerCamelCase=(1, 3, 64, 64)) -> Optional[Any]: """simple docstring""" _lowercase : List[Any] = torch.manual_seed(lowerCamelCase) _lowercase : str = { 'num_inference_steps': None, 'timesteps': [22, 0], 'class_labels': 0, 'generator': generator, 'output_type': 'np', } if get_fixed_latents: _lowercase : Optional[Any] = self.get_fixed_latents(seed=lowerCamelCase, device=lowerCamelCase, dtype=lowerCamelCase, shape=lowerCamelCase) _lowercase : Tuple = latents return inputs def UpperCamelCase ( self, lowerCamelCase=0, lowerCamelCase="cpu", lowerCamelCase=torch.floataa, lowerCamelCase=(1, 3, 64, 64)) -> Any: """simple docstring""" if type(lowerCamelCase) == str: _lowercase : Union[str, Any] = torch.device(lowerCamelCase) _lowercase : int = torch.Generator(device=lowerCamelCase).manual_seed(lowerCamelCase) _lowercase : List[str] = randn_tensor(lowerCamelCase, generator=lowerCamelCase, device=lowerCamelCase, dtype=lowerCamelCase) return latents def UpperCamelCase ( self) -> str: """simple docstring""" _lowercase : Tuple = UNetaDModel.from_pretrained('diffusers/consistency_models', subfolder='diffusers_cd_imagenet64_l2') _lowercase : Optional[int] = CMStochasticIterativeScheduler( num_train_timesteps=40, sigma_min=0.0_0_2, sigma_max=8_0.0, ) _lowercase : Any = ConsistencyModelPipeline(unet=lowerCamelCase, scheduler=lowerCamelCase) pipe.to(torch_device=lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : str = self.get_inputs() _lowercase : Optional[int] = pipe(**lowerCamelCase).images assert image.shape == (1, 64, 64, 3) _lowercase : str = image[0, -3:, -3:, -1] _lowercase : Optional[Any] = np.array([0.0_8_8_8, 0.0_8_8_1, 0.0_6_6_6, 0.0_4_7_9, 0.0_2_9_2, 0.0_1_9_5, 0.0_2_0_1, 0.0_1_6_3, 0.0_2_5_4]) assert np.abs(image_slice.flatten() - expected_slice).max() < 2E-2 def UpperCamelCase ( self) -> str: """simple docstring""" _lowercase : List[str] = UNetaDModel.from_pretrained('diffusers/consistency_models', subfolder='diffusers_cd_imagenet64_l2') _lowercase : List[str] = CMStochasticIterativeScheduler( num_train_timesteps=40, sigma_min=0.0_0_2, sigma_max=8_0.0, ) _lowercase : Union[str, Any] = ConsistencyModelPipeline(unet=lowerCamelCase, scheduler=lowerCamelCase) pipe.to(torch_device=lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : List[Any] = self.get_inputs() _lowercase : int = 1 _lowercase : Optional[Any] = None _lowercase : str = pipe(**lowerCamelCase).images assert image.shape == (1, 64, 64, 3) _lowercase : List[Any] = image[0, -3:, -3:, -1] _lowercase : List[str] = np.array([0.0_3_4_0, 0.0_1_5_2, 0.0_0_6_3, 0.0_2_6_7, 0.0_2_2_1, 0.0_1_0_7, 0.0_4_1_6, 0.0_1_8_6, 0.0_2_1_7]) assert np.abs(image_slice.flatten() - expected_slice).max() < 2E-2 @require_torch_a def UpperCamelCase ( self) -> Union[str, Any]: """simple docstring""" _lowercase : str = UNetaDModel.from_pretrained('diffusers/consistency_models', subfolder='diffusers_cd_imagenet64_l2') _lowercase : Optional[int] = CMStochasticIterativeScheduler( num_train_timesteps=40, sigma_min=0.0_0_2, sigma_max=8_0.0, ) _lowercase : Optional[int] = ConsistencyModelPipeline(unet=lowerCamelCase, scheduler=lowerCamelCase) pipe.to(torch_device=lowerCamelCase, torch_dtype=torch.floataa) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : Any = self.get_inputs(get_fixed_latents=lowerCamelCase, device=lowerCamelCase) # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=lowerCamelCase, enable_math=lowerCamelCase, enable_mem_efficient=lowerCamelCase): _lowercase : Dict = pipe(**lowerCamelCase).images assert image.shape == (1, 64, 64, 3) _lowercase : Any = image[0, -3:, -3:, -1] _lowercase : Union[str, Any] = np.array([0.1_8_7_5, 0.1_4_2_8, 0.1_2_8_9, 0.2_1_5_1, 0.2_0_9_2, 0.1_4_7_7, 0.1_8_7_7, 0.1_6_4_1, 0.1_3_5_3]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3 @require_torch_a def UpperCamelCase ( self) -> Union[str, Any]: """simple docstring""" _lowercase : Dict = UNetaDModel.from_pretrained('diffusers/consistency_models', subfolder='diffusers_cd_imagenet64_l2') _lowercase : Optional[Any] = CMStochasticIterativeScheduler( num_train_timesteps=40, sigma_min=0.0_0_2, sigma_max=8_0.0, ) _lowercase : int = ConsistencyModelPipeline(unet=lowerCamelCase, scheduler=lowerCamelCase) pipe.to(torch_device=lowerCamelCase, torch_dtype=torch.floataa) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : List[Any] = self.get_inputs(get_fixed_latents=lowerCamelCase, device=lowerCamelCase) _lowercase : int = 1 _lowercase : str = None # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=lowerCamelCase, enable_math=lowerCamelCase, enable_mem_efficient=lowerCamelCase): _lowercase : Union[str, Any] = pipe(**lowerCamelCase).images assert image.shape == (1, 64, 64, 3) _lowercase : Any = image[0, -3:, -3:, -1] _lowercase : int = np.array([0.1_6_6_3, 0.1_9_4_8, 0.2_2_7_5, 0.1_6_8_0, 0.1_2_0_4, 0.1_2_4_5, 0.1_8_5_8, 0.1_3_3_8, 0.2_0_9_5]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3
21
1
def lowercase( UpperCamelCase_ ) -> Any: '''simple docstring''' UpperCamelCase = [] UpperCamelCase = [] UpperCamelCase = { """^""": 3, """*""": 2, """/""": 2, """%""": 2, """+""": 1, """-""": 1, } # Priority of each operator UpperCamelCase = len(UpperCamelCase_ ) if (len(UpperCamelCase_ ) > 7) else 7 # Print table header for output print( """Symbol""".center(8 ) , """Stack""".center(UpperCamelCase_ ) , """Postfix""".center(UpperCamelCase_ ) , sep=""" | """ , ) print("""-""" * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(UpperCamelCase_ ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(UpperCamelCase_ ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(UpperCamelCase_ ) == 0: stack.append(UpperCamelCase_ ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(UpperCamelCase_ ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(UpperCamelCase_ ) # push x to stack print( x.center(8 ) , ("""""".join(UpperCamelCase_ )).ljust(UpperCamelCase_ ) , ("""""".join(UpperCamelCase_ )).ljust(UpperCamelCase_ ) , sep=""" | """ , ) # Output in tabular format while len(UpperCamelCase_ ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( """ """.center(8 ) , ("""""".join(UpperCamelCase_ )).ljust(UpperCamelCase_ ) , ("""""".join(UpperCamelCase_ )).ljust(UpperCamelCase_ ) , sep=""" | """ , ) # Output in tabular format return "".join(UpperCamelCase_ ) # return Postfix as str def lowercase( UpperCamelCase_ ) -> List[str]: '''simple docstring''' UpperCamelCase = list(infix[::-1] ) # reverse the infix equation for i in range(len(UpperCamelCase_ ) ): if infix[i] == "(": UpperCamelCase = """)""" # change "(" to ")" elif infix[i] == ")": UpperCamelCase = """(""" # change ")" to "(" return (infix_2_postfix("""""".join(UpperCamelCase_ ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": _SCREAMING_SNAKE_CASE = input("""\nEnter an Infix Equation = """) # Input an Infix equation _SCREAMING_SNAKE_CASE = """""".join(Infix.split()) # Remove spaces from the input print("""\n\t""", Infix, """(Infix) -> """, infix_2_prefix(Infix), """(Prefix)""")
165
import argparse _SCREAMING_SNAKE_CASE = """docs/source/_static/js/custom.js""" def lowercase( UpperCamelCase_ ) -> Union[str, Any]: '''simple docstring''' with open(UpperCamelCase_ , encoding="""utf-8""" , newline="""\n""" ) as f: UpperCamelCase = f.readlines() UpperCamelCase = 0 # First let's put the right version while not lines[index].startswith("""const stableVersion =""" ): index += 1 UpperCamelCase = f"""const stableVersion = \"v{version}\"\n""" # Then update the dictionary while not lines[index].startswith("""const versionMapping = {""" ): index += 1 # We go until the end while not lines[index].startswith("""}""" ): index += 1 # We add the new version at the end lines[index - 1] += f""" \"v{version}\": \"v{version}\",\n""" with open(UpperCamelCase_ , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(UpperCamelCase_ ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("""--version""", help="""Release version.""") _SCREAMING_SNAKE_CASE = parser.parse_args() update_custom_js(args.version)
165
1
def lowerCAmelCase_ ( __A, __A ) -> int: '''simple docstring''' if len(__A ) != len(__A ): raise ValueError("String lengths must match!" ) UpperCAmelCase__ = 0 for chara, chara in zip(__A, __A ): if chara != chara: count += 1 return count if __name__ == "__main__": import doctest doctest.testmod()
65
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(UpperCAmelCase_ ) , 'Tatoeba directory does not exist.' ) class A ( unittest.TestCase ): @cached_property def lowercase_ (self : Optional[int] ) -> List[Any]: """simple docstring""" UpperCAmelCase__ = tempfile.mkdtemp() return TatoebaConverter(save_dir=__UpperCAmelCase ) @slow def lowercase_ (self : List[Any] ) -> Optional[int]: """simple docstring""" self.resolver.convert_models(["heb-eng"] ) @slow def lowercase_ (self : Dict ) -> List[str]: """simple docstring""" UpperCAmelCase__ , UpperCAmelCase__ = self.resolver.write_model_card("opus-mt-he-en" , dry_run=__UpperCAmelCase ) assert mmeta["long_pair"] == "heb-eng"
65
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''microsoft/trocr-base-handwritten''': ( '''https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json''' ), # See all TrOCR models at https://huggingface.co/models?filter=trocr } class __snake_case ( _lowercase): snake_case__ : Optional[Any] = "trocr" snake_case__ : Dict = ["past_key_values"] snake_case__ : List[str] = { "num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model", "num_hidden_layers": "decoder_layers", } def __init__( self : Optional[int] , __lowerCAmelCase : int=5_0_2_6_5 , __lowerCAmelCase : str=1_0_2_4 , __lowerCAmelCase : Optional[Any]=1_2 , __lowerCAmelCase : List[Any]=1_6 , __lowerCAmelCase : str=4_0_9_6 , __lowerCAmelCase : List[str]="gelu" , __lowerCAmelCase : List[Any]=5_1_2 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Tuple=0.0 , __lowerCAmelCase : Optional[int]=0.0 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Union[str, Any]=0.02 , __lowerCAmelCase : List[str]=0.0 , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : int=False , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : Optional[int]=1 , __lowerCAmelCase : Union[str, Any]=0 , __lowerCAmelCase : Any=2 , **__lowerCAmelCase : Optional[Any] , ): """simple docstring""" _lowerCamelCase : Optional[Any] = vocab_size _lowerCamelCase : Dict = d_model _lowerCamelCase : Tuple = decoder_layers _lowerCamelCase : Optional[int] = decoder_attention_heads _lowerCamelCase : Tuple = decoder_ffn_dim _lowerCamelCase : Tuple = activation_function _lowerCamelCase : Optional[int] = max_position_embeddings _lowerCamelCase : List[Any] = dropout _lowerCamelCase : Any = attention_dropout _lowerCamelCase : Optional[int] = activation_dropout _lowerCamelCase : List[Any] = init_std _lowerCamelCase : Dict = decoder_layerdrop _lowerCamelCase : List[Any] = use_cache _lowerCamelCase : str = scale_embedding _lowerCamelCase : Optional[int] = use_learned_position_embeddings _lowerCamelCase : int = layernorm_embedding super().__init__( pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , decoder_start_token_id=__lowerCAmelCase , **__lowerCAmelCase , )
363
"""simple docstring""" from dataclasses import dataclass from typing import Dict, Optional, Union import torch import torch.nn.functional as F from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .attention import BasicTransformerBlock from .attention_processor import AttentionProcessor, AttnProcessor from .embeddings import TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin @dataclass class __snake_case ( _lowercase): snake_case__ : torch.FloatTensor class __snake_case ( _lowercase , _lowercase): @register_to_config def __init__( self : int , __lowerCAmelCase : int = 3_2 , __lowerCAmelCase : int = 6_4 , __lowerCAmelCase : int = 2_0 , __lowerCAmelCase : int = 7_6_8 , __lowerCAmelCase : Tuple=7_7 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : float = 0.0 , __lowerCAmelCase : str = "silu" , __lowerCAmelCase : Optional[str] = None , __lowerCAmelCase : Optional[str] = None , __lowerCAmelCase : Optional[str] = "linear" , __lowerCAmelCase : Optional[str] = "prd" , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[int] = None , ): """simple docstring""" super().__init__() _lowerCamelCase : Any = num_attention_heads _lowerCamelCase : Optional[int] = attention_head_dim _lowerCamelCase : Dict = num_attention_heads * attention_head_dim _lowerCamelCase : Optional[Any] = additional_embeddings _lowerCamelCase : List[Any] = time_embed_dim or inner_dim _lowerCamelCase : Optional[Any] = embedding_proj_dim or embedding_dim _lowerCamelCase : Tuple = clip_embed_dim or embedding_dim _lowerCamelCase : int = Timesteps(__lowerCAmelCase , __lowerCAmelCase , 0 ) _lowerCamelCase : str = TimestepEmbedding(__lowerCAmelCase , __lowerCAmelCase , out_dim=__lowerCAmelCase , act_fn=__lowerCAmelCase ) _lowerCamelCase : Union[str, Any] = nn.Linear(__lowerCAmelCase , __lowerCAmelCase ) if embedding_proj_norm_type is None: _lowerCamelCase : Dict = None elif embedding_proj_norm_type == "layer": _lowerCamelCase : Any = nn.LayerNorm(__lowerCAmelCase ) else: raise ValueError(f'''unsupported embedding_proj_norm_type: {embedding_proj_norm_type}''' ) _lowerCamelCase : List[str] = nn.Linear(__lowerCAmelCase , __lowerCAmelCase ) if encoder_hid_proj_type is None: _lowerCamelCase : Any = None elif encoder_hid_proj_type == "linear": _lowerCamelCase : List[str] = nn.Linear(__lowerCAmelCase , __lowerCAmelCase ) else: raise ValueError(f'''unsupported encoder_hid_proj_type: {encoder_hid_proj_type}''' ) _lowerCamelCase : Optional[int] = nn.Parameter(torch.zeros(1 , num_embeddings + additional_embeddings , __lowerCAmelCase ) ) if added_emb_type == "prd": _lowerCamelCase : Optional[Any] = nn.Parameter(torch.zeros(1 , 1 , __lowerCAmelCase ) ) elif added_emb_type is None: _lowerCamelCase : Union[str, Any] = None else: raise ValueError( f'''`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `\'prd\'` or `None`.''' ) _lowerCamelCase : Tuple = nn.ModuleList( [ BasicTransformerBlock( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , dropout=__lowerCAmelCase , activation_fn='''gelu''' , attention_bias=__lowerCAmelCase , ) for d in range(__lowerCAmelCase ) ] ) if norm_in_type == "layer": _lowerCamelCase : Any = nn.LayerNorm(__lowerCAmelCase ) elif norm_in_type is None: _lowerCamelCase : Any = None else: raise ValueError(f'''Unsupported norm_in_type: {norm_in_type}.''' ) _lowerCamelCase : str = nn.LayerNorm(__lowerCAmelCase ) _lowerCamelCase : List[Any] = nn.Linear(__lowerCAmelCase , __lowerCAmelCase ) _lowerCamelCase : Optional[int] = torch.full( [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings] , -1_00_00.0 ) causal_attention_mask.triu_(1 ) _lowerCamelCase : Dict = causal_attention_mask[None, ...] self.register_buffer('''causal_attention_mask''' , __lowerCAmelCase , persistent=__lowerCAmelCase ) _lowerCamelCase : int = nn.Parameter(torch.zeros(1 , __lowerCAmelCase ) ) _lowerCamelCase : Tuple = nn.Parameter(torch.zeros(1 , __lowerCAmelCase ) ) @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" _lowerCamelCase : Any = {} def fn_recursive_add_processors(__lowerCAmelCase : str , __lowerCAmelCase : torch.nn.Module , __lowerCAmelCase : Dict[str, AttentionProcessor] ): if hasattr(__lowerCAmelCase , '''set_processor''' ): _lowerCamelCase : str = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(f'''{name}.{sub_name}''' , __lowerCAmelCase , __lowerCAmelCase ) return processors for name, module in self.named_children(): fn_recursive_add_processors(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) return processors def SCREAMING_SNAKE_CASE ( self : Any , __lowerCAmelCase : Union[AttentionProcessor, Dict[str, AttentionProcessor]] ): """simple docstring""" _lowerCamelCase : Optional[Any] = len(self.attn_processors.keys() ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) and len(__lowerCAmelCase ) != count: raise ValueError( f'''A dict of processors was passed, but the number of processors {len(__lowerCAmelCase )} does not match the''' f''' number of attention layers: {count}. Please make sure to pass {count} processor classes.''' ) def fn_recursive_attn_processor(__lowerCAmelCase : str , __lowerCAmelCase : torch.nn.Module , __lowerCAmelCase : Tuple ): if hasattr(__lowerCAmelCase , '''set_processor''' ): if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): module.set_processor(__lowerCAmelCase ) else: module.set_processor(processor.pop(f'''{name}.processor''' ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(f'''{name}.{sub_name}''' , __lowerCAmelCase , __lowerCAmelCase ) for name, module in self.named_children(): fn_recursive_attn_processor(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" self.set_attn_processor(AttnProcessor() ) def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[torch.Tensor, float, int] , __lowerCAmelCase : torch.FloatTensor , __lowerCAmelCase : Optional[torch.FloatTensor] = None , __lowerCAmelCase : Optional[torch.BoolTensor] = None , __lowerCAmelCase : bool = True , ): """simple docstring""" _lowerCamelCase : Optional[int] = hidden_states.shape[0] _lowerCamelCase : Union[str, Any] = timestep if not torch.is_tensor(__lowerCAmelCase ): _lowerCamelCase : Dict = torch.tensor([timesteps] , dtype=torch.long , device=hidden_states.device ) elif torch.is_tensor(__lowerCAmelCase ) and len(timesteps.shape ) == 0: _lowerCamelCase : str = timesteps[None].to(hidden_states.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML _lowerCamelCase : Tuple = timesteps * torch.ones(__lowerCAmelCase , dtype=timesteps.dtype , device=timesteps.device ) _lowerCamelCase : Tuple = self.time_proj(__lowerCAmelCase ) # timesteps does not contain any weights and will always return f32 tensors # but time_embedding might be fp16, so we need to cast here. _lowerCamelCase : Union[str, Any] = timesteps_projected.to(dtype=self.dtype ) _lowerCamelCase : Any = self.time_embedding(__lowerCAmelCase ) if self.embedding_proj_norm is not None: _lowerCamelCase : str = self.embedding_proj_norm(__lowerCAmelCase ) _lowerCamelCase : Optional[Any] = self.embedding_proj(__lowerCAmelCase ) if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None: _lowerCamelCase : Optional[Any] = self.encoder_hidden_states_proj(__lowerCAmelCase ) elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None: raise ValueError('''`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set''' ) _lowerCamelCase : str = self.proj_in(__lowerCAmelCase ) _lowerCamelCase : Tuple = self.positional_embedding.to(hidden_states.dtype ) _lowerCamelCase : Tuple = [] _lowerCamelCase : Dict = 0 if encoder_hidden_states is not None: additional_embeds.append(__lowerCAmelCase ) additional_embeddings_len += encoder_hidden_states.shape[1] if len(proj_embeddings.shape ) == 2: _lowerCamelCase : str = proj_embeddings[:, None, :] if len(hidden_states.shape ) == 2: _lowerCamelCase : Dict = hidden_states[:, None, :] _lowerCamelCase : int = additional_embeds + [ proj_embeddings, time_embeddings[:, None, :], hidden_states, ] if self.prd_embedding is not None: _lowerCamelCase : int = self.prd_embedding.to(hidden_states.dtype ).expand(__lowerCAmelCase , -1 , -1 ) additional_embeds.append(__lowerCAmelCase ) _lowerCamelCase : str = torch.cat( __lowerCAmelCase , dim=1 , ) # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens _lowerCamelCase : Dict = additional_embeddings_len + proj_embeddings.shape[1] + 1 if positional_embeddings.shape[1] < hidden_states.shape[1]: _lowerCamelCase : List[Any] = F.pad( __lowerCAmelCase , ( 0, 0, additional_embeddings_len, self.prd_embedding.shape[1] if self.prd_embedding is not None else 0, ) , value=0.0 , ) _lowerCamelCase : Any = hidden_states + positional_embeddings if attention_mask is not None: _lowerCamelCase : Union[str, Any] = (1 - attention_mask.to(hidden_states.dtype )) * -1_00_00.0 _lowerCamelCase : Tuple = F.pad(__lowerCAmelCase , (0, self.additional_embeddings) , value=0.0 ) _lowerCamelCase : Any = (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype ) _lowerCamelCase : int = attention_mask.repeat_interleave(self.config.num_attention_heads , dim=0 ) if self.norm_in is not None: _lowerCamelCase : List[str] = self.norm_in(__lowerCAmelCase ) for block in self.transformer_blocks: _lowerCamelCase : List[str] = block(__lowerCAmelCase , attention_mask=__lowerCAmelCase ) _lowerCamelCase : Optional[Any] = self.norm_out(__lowerCAmelCase ) if self.prd_embedding is not None: _lowerCamelCase : Any = hidden_states[:, -1] else: _lowerCamelCase : Dict = hidden_states[:, additional_embeddings_len:] _lowerCamelCase : str = self.proj_to_clip_embeddings(__lowerCAmelCase ) if not return_dict: return (predicted_image_embedding,) return PriorTransformerOutput(predicted_image_embedding=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __lowerCAmelCase : Tuple ): """simple docstring""" _lowerCamelCase : Dict = (prior_latents * self.clip_std) + self.clip_mean return prior_latents
175
0
"""simple docstring""" import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = ProphetNetTokenizer _UpperCamelCase : Any = False def __A ( self ): super().setUp() _lowerCAmelCase : Dict = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _lowerCAmelCase : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) def __A ( self , a__ ): _lowerCAmelCase : str = """UNwant\u00E9d,running""" _lowerCAmelCase : List[str] = """unwanted, running""" return input_text, output_text def __A ( self ): _lowerCAmelCase : Any = self.tokenizer_class(self.vocab_file ) _lowerCAmelCase : List[str] = 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 __A ( self ): _lowerCAmelCase : Union[str, Any] = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz""" ) , ["""ah""", """\u535A""", """\u63A8""", """zz"""] ) def __A ( self ): _lowerCAmelCase : Tuple = 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 __A ( self ): _lowerCAmelCase : 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 __A ( self ): _lowerCAmelCase : List[str] = 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 __A ( self ): _lowerCAmelCase : Union[str, Any] = 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 __A ( self ): _lowerCAmelCase : Optional[int] = BasicTokenizer(do_lower_case=a__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def __A ( self ): _lowerCAmelCase : int = BasicTokenizer(do_lower_case=a__ , strip_accents=a__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def __A ( self ): _lowerCAmelCase : str = BasicTokenizer(do_lower_case=a__ , strip_accents=a__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def __A ( self ): _lowerCAmelCase : Optional[Any] = BasicTokenizer(do_lower_case=a__ , never_split=["""[UNK]"""] ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? [UNK]""" ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""] ) def __A ( self ): _lowerCAmelCase : Any = ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""] _lowerCAmelCase : Optional[int] = {} for i, token in enumerate(a__ ): _lowerCAmelCase : Optional[Any] = i _lowerCAmelCase : str = 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"""] ) @require_torch def __A ( self ): _lowerCAmelCase : List[str] = self.tokenizer_class.from_pretrained("""microsoft/prophetnet-large-uncased""" ) _lowerCAmelCase : Dict = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] _lowerCAmelCase : Any = [1037, 2146, 20423, 2005, 7680, 7849, 3989, 1012, 102] _lowerCAmelCase : Any = tokenizer(a__ , padding=a__ , return_tensors="""pt""" ) self.assertIsInstance(a__ , a__ ) _lowerCAmelCase : Optional[Any] = list(batch.input_ids.numpy()[0] ) self.assertListEqual(a__ , a__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) def __A ( self ): 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 __A ( self ): 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 __A ( self ): self.assertTrue(_is_punctuation("""-""" ) ) self.assertTrue(_is_punctuation("""$""" ) ) self.assertTrue(_is_punctuation("""`""" ) ) self.assertTrue(_is_punctuation(""".""" ) ) self.assertFalse(_is_punctuation("""A""" ) ) self.assertFalse(_is_punctuation(""" """ ) ) @slow def __A ( self ): _lowerCAmelCase : List[Any] = self.tokenizer_class.from_pretrained("""microsoft/prophetnet-large-uncased""" ) _lowerCAmelCase : Dict = tokenizer.encode("""sequence builders""" , add_special_tokens=a__ ) _lowerCAmelCase : Union[str, Any] = tokenizer.encode("""multi-sequence build""" , add_special_tokens=a__ ) _lowerCAmelCase : Optional[int] = tokenizer.build_inputs_with_special_tokens(a__ ) _lowerCAmelCase : List[Any] = tokenizer.build_inputs_with_special_tokens(a__ , a__ ) assert encoded_sentence == text + [102] assert encoded_pair == text + [102] + text_a + [102]
44
"""simple docstring""" import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin _a : Optional[int] = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : List[Any] = XGLMTokenizer _UpperCamelCase : List[Any] = XGLMTokenizerFast _UpperCamelCase : Dict = True _UpperCamelCase : Tuple = True def __A ( self ): super().setUp() # We have a SentencePiece fixture for testing _lowerCAmelCase : List[Any] = XGLMTokenizer(a__ , keep_accents=a__ ) tokenizer.save_pretrained(self.tmpdirname ) def __A ( self ): _lowerCAmelCase : List[str] = """<pad>""" _lowerCAmelCase : List[Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(a__ ) , a__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(a__ ) , a__ ) def __A ( self ): _lowerCAmelCase : Optional[int] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<s>""" ) self.assertEqual(vocab_keys[1] , """<pad>""" ) self.assertEqual(len(a__ ) , 1008 ) def __A ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1008 ) def __A ( self ): _lowerCAmelCase : List[Any] = XGLMTokenizer(a__ , keep_accents=a__ ) _lowerCAmelCase : Dict = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(a__ , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(a__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) _lowerCAmelCase : Any = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( a__ , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) _lowerCAmelCase : List[str] = tokenizer.convert_tokens_to_ids(a__ ) self.assertListEqual( a__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) _lowerCAmelCase : Optional[int] = tokenizer.convert_ids_to_tokens(a__ ) self.assertListEqual( a__ , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) @cached_property def __A ( self ): return XGLMTokenizer.from_pretrained("""facebook/xglm-564M""" ) def __A ( self ): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(a__ , f.name ) _lowerCAmelCase : Union[str, Any] = XGLMTokenizer(f.name , keep_accents=a__ ) _lowerCAmelCase : List[str] = pickle.dumps(a__ ) pickle.loads(a__ ) def __A ( self ): if not self.test_rust_tokenizer: return _lowerCAmelCase : List[str] = self.get_tokenizer() _lowerCAmelCase : Optional[Any] = self.get_rust_tokenizer() _lowerCAmelCase : Tuple = """I was born in 92000, and this is falsé.""" _lowerCAmelCase : List[Any] = tokenizer.tokenize(a__ ) _lowerCAmelCase : Tuple = rust_tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Union[str, Any] = tokenizer.encode(a__ , add_special_tokens=a__ ) _lowerCAmelCase : str = rust_tokenizer.encode(a__ , add_special_tokens=a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : int = self.get_rust_tokenizer() _lowerCAmelCase : Dict = tokenizer.encode(a__ ) _lowerCAmelCase : List[Any] = rust_tokenizer.encode(a__ ) self.assertListEqual(a__ , a__ ) @slow def __A ( self ): _lowerCAmelCase : int = """Hello World!""" _lowerCAmelCase : Optional[int] = [2, 31227, 4447, 35] self.assertListEqual(a__ , self.big_tokenizer.encode(a__ ) ) @slow def __A ( self ): _lowerCAmelCase : Any = ( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth""" ) # fmt: off _lowerCAmelCase : List[str] = [2, 1018, 67, 11, 1988, 2617, 5631, 278, 11, 3407, 48, 71630, 28085, 4, 3234, 157, 13, 6, 5, 6, 4, 3526, 768, 15, 659, 57, 298, 3983, 864, 129, 21, 6, 5, 13675, 377, 652, 7580, 10341, 155, 2817, 422, 1666, 7, 1674, 53, 113, 202277, 17892, 33, 60, 87, 4, 3234, 157, 61, 2667, 52376, 19, 88, 23, 735] # fmt: on self.assertListEqual(a__ , self.big_tokenizer.encode(a__ ) ) @slow def __A ( self ): # fmt: off _lowerCAmelCase : List[str] = { """input_ids""": [[2, 108825, 1163, 15, 88010, 473, 15898, 157, 13672, 1857, 312, 8, 238021, 1163, 53, 13672, 1857, 312, 8, 53283, 182396, 8, 18566, 16, 36733, 4101, 8, 230, 244017, 122553, 7, 15, 132597, 4, 293, 12511, 7610, 4, 3414, 132597, 9, 4, 32361, 362, 4, 734, 28512, 32569, 18, 4, 32361, 26096, 14982, 73, 18715, 21433, 235261, 15, 492, 12427, 16, 53, 18715, 21433, 65454, 15, 23659, 563, 16, 278, 597, 2843, 595, 7931, 182396, 64186, 22, 886, 595, 132981, 53, 25540, 3449, 43982, 39901, 5951, 878, 330, 4, 27694, 80269, 312, 53, 6517, 11780, 611, 20408, 5], [2, 6, 132597, 67, 42897, 33, 592, 8, 163729, 25540, 361, 136997, 109514, 173230, 7, 501, 60, 102913, 196, 5631, 235, 63243, 473, 6, 231757, 74, 5277, 7905, 53, 3095, 37317, 22, 454, 183874, 5], [2, 268, 31298, 46530, 6, 132935, 43831, 7, 597, 32, 24, 3688, 9865, 5]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=a__ , model_name="""facebook/xglm-564M""" , padding=a__ , )
44
1
import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def snake_case ( snake_case__ :Dict) -> List[Any]: _A = SwinConfig() _A = swin_name.split("""_""") _A = name_split[1] _A = int(name_split[4]) _A = int(name_split[3][-1]) if model_size == "tiny": _A = 96 _A = (2, 2, 6, 2) _A = (3, 6, 12, 24) elif model_size == "small": _A = 96 _A = (2, 2, 18, 2) _A = (3, 6, 12, 24) elif model_size == "base": _A = 128 _A = (2, 2, 18, 2) _A = (4, 8, 16, 32) else: _A = 192 _A = (2, 2, 18, 2) _A = (6, 12, 24, 48) if "in22k" in swin_name: _A = 21_841 else: _A = 1_000 _A = """huggingface/label-files""" _A = """imagenet-1k-id2label.json""" _A = json.load(open(hf_hub_download(snake_case__ , snake_case__ , repo_type="""dataset""") , """r""")) _A = {int(snake_case__): v for k, v in idalabel.items()} _A = idalabel _A = {v: k for k, v in idalabel.items()} _A = img_size _A = num_classes _A = embed_dim _A = depths _A = num_heads _A = window_size return config def snake_case ( snake_case__ :List[Any]) -> Union[str, Any]: if "patch_embed.proj" in name: _A = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""") if "patch_embed.norm" in name: _A = name.replace("""patch_embed.norm""" , """embeddings.norm""") if "layers" in name: _A = """encoder.""" + name if "attn.proj" in name: _A = name.replace("""attn.proj""" , """attention.output.dense""") if "attn" in name: _A = name.replace("""attn""" , """attention.self""") if "norm1" in name: _A = name.replace("""norm1""" , """layernorm_before""") if "norm2" in name: _A = name.replace("""norm2""" , """layernorm_after""") if "mlp.fc1" in name: _A = name.replace("""mlp.fc1""" , """intermediate.dense""") if "mlp.fc2" in name: _A = name.replace("""mlp.fc2""" , """output.dense""") if name == "norm.weight": _A = """layernorm.weight""" if name == "norm.bias": _A = """layernorm.bias""" if "head" in name: _A = name.replace("""head""" , """classifier""") else: _A = """swin.""" + name return name def snake_case ( snake_case__ :Any , snake_case__ :Any) -> Dict: for key in orig_state_dict.copy().keys(): _A = orig_state_dict.pop(snake_case__) if "mask" in key: continue elif "qkv" in key: _A = key.split(""".""") _A = int(key_split[1]) _A = int(key_split[3]) _A = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: _A = val[:dim, :] _A = val[ dim : dim * 2, : ] _A = val[-dim:, :] else: _A = val[ :dim ] _A = val[ dim : dim * 2 ] _A = val[ -dim: ] else: _A = val return orig_state_dict def snake_case ( snake_case__ :Optional[Any] , snake_case__ :Tuple) -> Tuple: _A = timm.create_model(snake_case__ , pretrained=snake_case__) timm_model.eval() _A = get_swin_config(snake_case__) _A = SwinForImageClassification(snake_case__) model.eval() _A = convert_state_dict(timm_model.state_dict() , snake_case__) model.load_state_dict(snake_case__) _A = """http://images.cocodataset.org/val2017/000000039769.jpg""" _A = AutoImageProcessor.from_pretrained("""microsoft/{}""".format(swin_name.replace("""_""" , """-"""))) _A = Image.open(requests.get(snake_case__ , stream=snake_case__).raw) _A = image_processor(images=snake_case__ , return_tensors="""pt""") _A = timm_model(inputs["""pixel_values"""]) _A = model(**snake_case__).logits assert torch.allclose(snake_case__ , snake_case__ , atol=1E-3) print(F'''Saving model {swin_name} to {pytorch_dump_folder_path}''') model.save_pretrained(snake_case__) print(F'''Saving image processor to {pytorch_dump_folder_path}''') image_processor.save_pretrained(snake_case__) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( '--swin_name', default='swin_tiny_patch4_window7_224', type=str, help='Name of the Swin timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) _SCREAMING_SNAKE_CASE = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
81
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def snake_case ( ) -> List[Any]: _A = ArgumentParser( description=( """PyTorch TPU distributed training launch """ """helper utility that will spawn up """ """multiple distributed processes""" )) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=snake_case__ , default=1 , help="""Number of TPU cores to use (1 or 8).""") # positional parser.add_argument( """training_script""" , type=snake_case__ , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=snake_case__) return parser.parse_args() def snake_case ( ) -> List[str]: _A = parse_args() # Import training_script as a module. _A = Path(args.training_script) sys.path.append(str(script_fpath.parent.resolve())) _A = script_fpath.stem _A = importlib.import_module(snake_case__) # Patch sys.argv _A = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores)] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores) if __name__ == "__main__": main()
81
1
import functools def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): __a = len(_UpperCAmelCase ) __a = len(_UpperCAmelCase ) @functools.cache def min_distance(_UpperCAmelCase , _UpperCAmelCase ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa __a = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , _UpperCAmelCase ) , 1 + min_distance(_UpperCAmelCase , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
49
'''simple docstring''' def snake_case_ ( __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str ): """simple docstring""" lowercase_ : List[str] = len(__SCREAMING_SNAKE_CASE ) lowercase_ : Optional[int] = [] for i in range(len(__SCREAMING_SNAKE_CASE ) - pat_len + 1 ): lowercase_ : Tuple = True for j in range(__SCREAMING_SNAKE_CASE ): if s[i + j] != pattern[j]: lowercase_ : List[str] = False break if match_found: position.append(__SCREAMING_SNAKE_CASE ) return position if __name__ == "__main__": assert naive_pattern_search("ABCDEFG", "DE") == [3] print(naive_pattern_search("ABAAABCDBBABCDDEBCABC", "ABC"))
93
0
def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = 0 while len(UpperCamelCase__ ) > 1: snake_case_ = 0 # Consider two files with minimum cost to be merged for _ in range(2 ): snake_case_ = files.index(min(UpperCamelCase__ ) ) temp += files[min_index] files.pop(UpperCamelCase__ ) files.append(UpperCamelCase__ ) optimal_merge_cost += temp return optimal_merge_cost if __name__ == "__main__": import doctest doctest.testmod()
200
import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class lowercase ( lowercase_ , unittest.TestCase ): __SCREAMING_SNAKE_CASE : List[str] = BarthezTokenizer __SCREAMING_SNAKE_CASE : str = BarthezTokenizerFast __SCREAMING_SNAKE_CASE : Optional[Any] = True __SCREAMING_SNAKE_CASE : str = True def a ( self ): super().setUp() snake_case_ = BarthezTokenizerFast.from_pretrained('moussaKam/mbarthez' ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=snake_case ) snake_case_ = tokenizer def a ( self ): snake_case_ = '<pad>' snake_case_ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(snake_case ) , snake_case ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(snake_case ) , snake_case ) def a ( self ): snake_case_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<s>' ) self.assertEqual(vocab_keys[1] , '<pad>' ) self.assertEqual(vocab_keys[-1] , '<mask>' ) self.assertEqual(len(snake_case ) , 10_1122 ) def a ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 10_1122 ) @require_torch def a ( self ): snake_case_ = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] snake_case_ = [0, 57, 3018, 7_0307, 91, 2] snake_case_ = self.tokenizer( snake_case , max_length=len(snake_case ) , padding=snake_case , truncation=snake_case , return_tensors='pt' ) self.assertIsInstance(snake_case , snake_case ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) snake_case_ = batch.input_ids.tolist()[0] self.assertListEqual(snake_case , snake_case ) def a ( self ): if not self.test_rust_tokenizer: return snake_case_ = self.get_tokenizer() snake_case_ = self.get_rust_tokenizer() snake_case_ = 'I was born in 92000, and this is falsé.' snake_case_ = tokenizer.tokenize(snake_case ) snake_case_ = rust_tokenizer.tokenize(snake_case ) self.assertListEqual(snake_case , snake_case ) snake_case_ = tokenizer.encode(snake_case , add_special_tokens=snake_case ) snake_case_ = rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) self.assertListEqual(snake_case , snake_case ) snake_case_ = self.get_rust_tokenizer() snake_case_ = tokenizer.encode(snake_case ) snake_case_ = rust_tokenizer.encode(snake_case ) self.assertListEqual(snake_case , snake_case ) @slow def a ( self ): # fmt: off snake_case_ = {'input_ids': [[0, 490, 1_4328, 4507, 354, 47, 4_3669, 95, 25, 7_8117, 2_0215, 1_9779, 190, 22, 400, 4, 3_5343, 8_0310, 603, 86, 2_4937, 105, 3_3438, 9_4762, 196, 3_9642, 7, 15, 1_5933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0534, 87, 25, 66, 3358, 196, 5_5289, 8, 8_2961, 81, 2204, 7_5203, 7, 15, 763, 1_2956, 216, 178, 1_4328, 9595, 1377, 6_9693, 7, 448, 7_1021, 196, 1_8106, 1437, 1_3974, 108, 9083, 4, 4_9315, 7, 39, 86, 1326, 2793, 4_6333, 4, 448, 196, 7_4588, 7, 4_9315, 7, 39, 21, 822, 3_8470, 74, 21, 6_6723, 6_2480, 8, 2_2050, 5, 2]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. snake_case_ = [ 'Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ' 'utilisé principalement dans le domaine du traitement automatique des langues (TAL).', 'À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ' 'pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ' 'telles que la traduction et la synthèse de texte.', ] self.tokenizer_integration_test_util( expected_encoding=snake_case , model_name='moussaKam/mbarthez' , revision='c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6' , sequences=snake_case , )
200
1
"""simple docstring""" from collections import defaultdict class lowerCamelCase : def __init__( self : List[str] , __UpperCAmelCase : Dict , __UpperCAmelCase : Any ) -> Any: SCREAMING_SNAKE_CASE__ = total # total no of tasks (N) # DP table will have a dimension of (2^M)*N # initially all values are set to -1 SCREAMING_SNAKE_CASE__ = [ [-1 for i in range(total + 1 )] for j in range(2 ** len(__UpperCAmelCase ) ) ] SCREAMING_SNAKE_CASE__ = defaultdict(__UpperCAmelCase ) # stores the list of persons for each task # final_mask is used to check if all persons are included by setting all bits # to 1 SCREAMING_SNAKE_CASE__ = (1 << len(__UpperCAmelCase )) - 1 def SCREAMING_SNAKE_CASE ( self : str , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int] ) -> Optional[int]: # if mask == self.finalmask all persons are distributed tasks, return 1 if mask == self.final_mask: return 1 # if not everyone gets the task and no more tasks are available, return 0 if task_no > self.total_tasks: return 0 # if case already considered if self.dp[mask][task_no] != -1: return self.dp[mask][task_no] # Number of ways when we don't this task in the arrangement SCREAMING_SNAKE_CASE__ = self.count_ways_until(__UpperCAmelCase , task_no + 1 ) # now assign the tasks one by one to all possible persons and recursively # assign for the remaining tasks. if task_no in self.task: for p in self.task[task_no]: # if p is already given a task if mask & (1 << p): continue # assign this task to p and change the mask value. And recursively # assign tasks with the new mask value. total_ways_util += self.count_ways_until(mask | (1 << p) , task_no + 1 ) # save the value. SCREAMING_SNAKE_CASE__ = total_ways_util return self.dp[mask][task_no] def SCREAMING_SNAKE_CASE ( self : Optional[int] , __UpperCAmelCase : Union[str, Any] ) -> List[str]: # Store the list of persons for each task for i in range(len(__UpperCAmelCase ) ): for j in task_performed[i]: self.task[j].append(__UpperCAmelCase ) # call the function to fill the DP table, final answer is stored in dp[0][1] return self.count_ways_until(0 , 1 ) if __name__ == "__main__": A_ : Any = 5 # total no of tasks (the value of N) # the list of tasks that can be done by M persons. A_ : Any = [[1, 3, 4], [1, 2, 5], [3, 4]] print( AssignmentUsingBitmask(task_performed, total_tasks).count_no_of_ways( task_performed ) )
165
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType A_ : Dict = logging.get_logger(__name__) A_ : Any = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class lowerCamelCase (A__ ): lowerCamelCase__ : Tuple = 'deberta-v2' def __init__( self : Any , __UpperCAmelCase : Optional[Any]=1_2_8_1_0_0 , __UpperCAmelCase : Optional[Any]=1_5_3_6 , __UpperCAmelCase : List[Any]=2_4 , __UpperCAmelCase : str=2_4 , __UpperCAmelCase : Optional[int]=6_1_4_4 , __UpperCAmelCase : Any="gelu" , __UpperCAmelCase : Union[str, Any]=0.1 , __UpperCAmelCase : Dict=0.1 , __UpperCAmelCase : Optional[Any]=5_1_2 , __UpperCAmelCase : List[str]=0 , __UpperCAmelCase : int=0.02 , __UpperCAmelCase : Any=1e-7 , __UpperCAmelCase : Tuple=False , __UpperCAmelCase : Any=-1 , __UpperCAmelCase : Union[str, Any]=0 , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[Any]=None , __UpperCAmelCase : Optional[Any]=0 , __UpperCAmelCase : Union[str, Any]="gelu" , **__UpperCAmelCase : Any , ) -> Union[str, Any]: super().__init__(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = hidden_size SCREAMING_SNAKE_CASE__ = num_hidden_layers SCREAMING_SNAKE_CASE__ = num_attention_heads SCREAMING_SNAKE_CASE__ = intermediate_size SCREAMING_SNAKE_CASE__ = hidden_act SCREAMING_SNAKE_CASE__ = hidden_dropout_prob SCREAMING_SNAKE_CASE__ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = type_vocab_size SCREAMING_SNAKE_CASE__ = initializer_range SCREAMING_SNAKE_CASE__ = relative_attention SCREAMING_SNAKE_CASE__ = max_relative_positions SCREAMING_SNAKE_CASE__ = pad_token_id SCREAMING_SNAKE_CASE__ = position_biased_input # Backwards compatibility if type(__UpperCAmelCase ) == str: SCREAMING_SNAKE_CASE__ = [x.strip() for x in pos_att_type.lower().split("""|""" )] SCREAMING_SNAKE_CASE__ = pos_att_type SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = layer_norm_eps SCREAMING_SNAKE_CASE__ = kwargs.get("""pooler_hidden_size""" , __UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = pooler_dropout SCREAMING_SNAKE_CASE__ = pooler_hidden_act class lowerCamelCase (A__ ): @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": SCREAMING_SNAKE_CASE__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: SCREAMING_SNAKE_CASE__ = {0: """batch""", 1: """sequence"""} if self._config.type_vocab_size > 0: return OrderedDict( [("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis)] ) else: return OrderedDict([("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis)] ) @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: return 1_2 def SCREAMING_SNAKE_CASE ( self : Tuple , __UpperCAmelCase : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , __UpperCAmelCase : int = -1 , __UpperCAmelCase : int = -1 , __UpperCAmelCase : int = -1 , __UpperCAmelCase : bool = False , __UpperCAmelCase : Optional["TensorType"] = None , __UpperCAmelCase : int = 3 , __UpperCAmelCase : int = 4_0 , __UpperCAmelCase : int = 4_0 , __UpperCAmelCase : "PreTrainedTokenizerBase" = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE__ = super().generate_dummy_inputs(preprocessor=__UpperCAmelCase , framework=__UpperCAmelCase ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
165
1
import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class a__ ( snake_case__ , unittest.TestCase ): _a : int = DDIMPipeline _a : List[Any] = UNCONDITIONAL_IMAGE_GENERATION_PARAMS _a : List[Any] = PipelineTesterMixin.required_optional_params - { """num_images_per_prompt""", """latents""", """callback""", """callback_steps""", } _a : Optional[int] = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS _a : Optional[Any] = False def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" torch.manual_seed(0 ) __lowerCAmelCase = UNetaDModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=3 , out_channels=3 , down_block_types=("DownBlock2D", "AttnDownBlock2D") , up_block_types=("AttnUpBlock2D", "UpBlock2D") , ) __lowerCAmelCase = DDIMScheduler() __lowerCAmelCase = {"unet": unet, "scheduler": scheduler} return components def __SCREAMING_SNAKE_CASE( self , _A , _A=0 ): """simple docstring""" if str(_A ).startswith("mps" ): __lowerCAmelCase = torch.manual_seed(_A ) else: __lowerCAmelCase = torch.Generator(device=_A ).manual_seed(_A ) __lowerCAmelCase = { "batch_size": 1, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = "cpu" __lowerCAmelCase = self.get_dummy_components() __lowerCAmelCase = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) __lowerCAmelCase = self.get_dummy_inputs(_A ) __lowerCAmelCase = pipe(**_A ).images __lowerCAmelCase = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 3_2, 3_2, 3) ) __lowerCAmelCase = np.array( [1.0_0_0E0_0, 5.7_1_7E-0_1, 4.7_1_7E-0_1, 1.0_0_0E0_0, 0.0_0_0E0_0, 1.0_0_0E0_0, 3.0_0_0E-0_4, 0.0_0_0E0_0, 9.0_0_0E-0_4] ) __lowerCAmelCase = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_A , 1E-3 ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" super().test_save_load_local(expected_max_difference=3E-3 ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=3E-3 ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class a__ ( unittest.TestCase ): def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = "google/ddpm-cifar10-32" __lowerCAmelCase = UNetaDModel.from_pretrained(_A ) __lowerCAmelCase = DDIMScheduler() __lowerCAmelCase = DDIMPipeline(unet=_A , scheduler=_A ) ddim.to(_A ) ddim.set_progress_bar_config(disable=_A ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = ddim(generator=_A , eta=0.0 , output_type="numpy" ).images __lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) __lowerCAmelCase = np.array([0.17_23, 0.16_17, 0.16_00, 0.16_26, 0.14_97, 0.15_13, 0.15_05, 0.14_42, 0.14_53] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = "google/ddpm-ema-bedroom-256" __lowerCAmelCase = UNetaDModel.from_pretrained(_A ) __lowerCAmelCase = DDIMScheduler.from_pretrained(_A ) __lowerCAmelCase = DDIMPipeline(unet=_A , scheduler=_A ) ddpm.to(_A ) ddpm.set_progress_bar_config(disable=_A ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = ddpm(generator=_A , output_type="numpy" ).images __lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) __lowerCAmelCase = np.array([0.00_60, 0.02_01, 0.03_44, 0.00_24, 0.00_18, 0.00_02, 0.00_22, 0.00_00, 0.00_69] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
102
import unittest import numpy as np import torch from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class a__ ( unittest.TestCase ): @property def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" torch.manual_seed(0 ) __lowerCAmelCase = UNetaDModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=3 , out_channels=3 , down_block_types=("DownBlock2D", "AttnDownBlock2D") , up_block_types=("AttnUpBlock2D", "UpBlock2D") , ) return model def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = self.dummy_uncond_unet __lowerCAmelCase = ScoreSdeVeScheduler() __lowerCAmelCase = ScoreSdeVePipeline(unet=_A , scheduler=_A ) sde_ve.to(_A ) sde_ve.set_progress_bar_config(disable=_A ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = sde_ve(num_inference_steps=2 , output_type="numpy" , generator=_A ).images __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = sde_ve(num_inference_steps=2 , output_type="numpy" , generator=_A , return_dict=_A )[ 0 ] __lowerCAmelCase = image[0, -3:, -3:, -1] __lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) __lowerCAmelCase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch class a__ ( unittest.TestCase ): def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = "google/ncsnpp-church-256" __lowerCAmelCase = UNetaDModel.from_pretrained(_A ) __lowerCAmelCase = ScoreSdeVeScheduler.from_pretrained(_A ) __lowerCAmelCase = ScoreSdeVePipeline(unet=_A , scheduler=_A ) sde_ve.to(_A ) sde_ve.set_progress_bar_config(disable=_A ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = sde_ve(num_inference_steps=1_0 , output_type="numpy" , generator=_A ).images __lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) __lowerCAmelCase = np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
102
1
def __snake_case ( _lowerCAmelCase : str ) -> Optional[Any]: A_ : Dict = 0 for ch in input_str: A_ : Tuple = ord(_lowerCAmelCase ) A_ : str = pow(2 , _lowerCAmelCase ) # If we already turned on bit for current character's unicode if bitmap >> ch_unicode & 1 == 1: return False bitmap |= ch_bit_index_on return True if __name__ == "__main__": import doctest doctest.testmod()
300
import argparse import torch from transformers import ( UniSpeechSatConfig, UniSpeechSatForAudioFrameClassification, UniSpeechSatForSequenceClassification, UniSpeechSatForXVector, WavaVecaFeatureExtractor, logging, ) logging.set_verbosity_info() a_ = logging.get_logger(__name__) def __lowercase ( lowerCamelCase : Dict , lowerCamelCase : Union[str, Any] , lowerCamelCase : Any ): UpperCamelCase_ : List[Any] = UniSpeechSatForSequenceClassification.from_pretrained(lowerCamelCase , config=lowerCamelCase ) UpperCamelCase_ : Any = downstream_dict['projector.weight'] UpperCamelCase_ : List[str] = downstream_dict['projector.bias'] UpperCamelCase_ : Tuple = downstream_dict['model.post_net.linear.weight'] UpperCamelCase_ : int = downstream_dict['model.post_net.linear.bias'] return model def __lowercase ( lowerCamelCase : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : Union[str, Any] ): UpperCamelCase_ : Any = UniSpeechSatForAudioFrameClassification.from_pretrained(lowerCamelCase , config=lowerCamelCase ) UpperCamelCase_ : Tuple = downstream_dict['model.linear.weight'] UpperCamelCase_ : Any = downstream_dict['model.linear.bias'] return model def __lowercase ( lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int] , lowerCamelCase : List[Any] ): UpperCamelCase_ : str = UniSpeechSatForXVector.from_pretrained(lowerCamelCase , config=lowerCamelCase ) UpperCamelCase_ : Optional[Any] = downstream_dict['connector.weight'] UpperCamelCase_ : int = downstream_dict['connector.bias'] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): UpperCamelCase_ : Union[str, Any] = downstream_dict[ F"model.framelevel_feature_extractor.module.{i}.kernel.weight" ] UpperCamelCase_ : Tuple = downstream_dict[F"model.framelevel_feature_extractor.module.{i}.kernel.bias"] UpperCamelCase_ : int = downstream_dict['model.utterancelevel_feature_extractor.linear1.weight'] UpperCamelCase_ : Optional[int] = downstream_dict['model.utterancelevel_feature_extractor.linear1.bias'] UpperCamelCase_ : Dict = downstream_dict['model.utterancelevel_feature_extractor.linear2.weight'] UpperCamelCase_ : Optional[int] = downstream_dict['model.utterancelevel_feature_extractor.linear2.bias'] UpperCamelCase_ : List[Any] = downstream_dict['objective.W'] return model @torch.no_grad() def __lowercase ( lowerCamelCase : Optional[Any] , lowerCamelCase : int , lowerCamelCase : Any , lowerCamelCase : int ): UpperCamelCase_ : int = torch.load(lowerCamelCase , map_location='cpu' ) UpperCamelCase_ : Any = checkpoint['Downstream'] UpperCamelCase_ : Dict = UniSpeechSatConfig.from_pretrained(lowerCamelCase ) UpperCamelCase_ : str = WavaVecaFeatureExtractor.from_pretrained( lowerCamelCase , return_attention_mask=lowerCamelCase , do_normalize=lowerCamelCase ) UpperCamelCase_ : int = hf_config.architectures[0] if arch.endswith('ForSequenceClassification' ): UpperCamelCase_ : Tuple = convert_classification(lowerCamelCase , lowerCamelCase , lowerCamelCase ) elif arch.endswith('ForAudioFrameClassification' ): UpperCamelCase_ : List[str] = convert_diarization(lowerCamelCase , lowerCamelCase , lowerCamelCase ) elif arch.endswith('ForXVector' ): UpperCamelCase_ : Union[str, Any] = convert_xvector(lowerCamelCase , lowerCamelCase , lowerCamelCase ) else: raise NotImplementedError(F"S3PRL weights conversion is not supported for {arch}" ) if hf_config.use_weighted_layer_sum: UpperCamelCase_ : Tuple = checkpoint['Featurizer']['weights'] hf_feature_extractor.save_pretrained(lowerCamelCase ) hf_model.save_pretrained(lowerCamelCase ) if __name__ == "__main__": a_ = argparse.ArgumentParser() parser.add_argument( '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.' ) parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.') parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.') a_ = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
175
0
"""simple docstring""" 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 UpperCamelCase_ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' UpperCAmelCase__ = StableDiffusionDiffEditPipeline UpperCAmelCase__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'''height''', '''width''', '''image'''} | {'''image_latents'''} UpperCAmelCase__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {'''image'''} | {'''image_latents'''} UpperCAmelCase__ = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCAmelCase__ = frozenset([] ) def SCREAMING_SNAKE_CASE ( self : List[Any]) ->Tuple: '''simple docstring''' torch.manual_seed(0) A__ = 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=UpperCAmelCase__ , ) A__ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=UpperCAmelCase__ , set_alpha_to_one=UpperCAmelCase__ , ) A__ = DDIMInverseScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=UpperCAmelCase__ , set_alpha_to_zero=UpperCAmelCase__ , ) torch.manual_seed(0) A__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0) A__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) A__ = CLIPTextModel(UpperCAmelCase__) A__ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''') A__ = { '''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 SCREAMING_SNAKE_CASE ( self : Any , UpperCAmelCase__ : int , UpperCAmelCase__ : Any=0) ->str: '''simple docstring''' A__ = floats_tensor((1, 16, 16) , rng=random.Random(UpperCAmelCase__)).to(UpperCAmelCase__) A__ = floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(UpperCAmelCase__)).to(UpperCAmelCase__) if str(UpperCAmelCase__).startswith('''mps'''): A__ = torch.manual_seed(UpperCAmelCase__) else: A__ = torch.Generator(device=UpperCAmelCase__).manual_seed(UpperCAmelCase__) A__ = { '''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 SCREAMING_SNAKE_CASE ( self : Tuple , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Any=0) ->int: '''simple docstring''' A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(UpperCAmelCase__)).to(UpperCAmelCase__) A__ = image.cpu().permute(0 , 2 , 3 , 1)[0] A__ = Image.fromarray(np.uinta(UpperCAmelCase__)).convert('''RGB''') if str(UpperCAmelCase__).startswith('''mps'''): A__ = torch.manual_seed(UpperCAmelCase__) else: A__ = torch.Generator(device=UpperCAmelCase__).manual_seed(UpperCAmelCase__) A__ = { '''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 SCREAMING_SNAKE_CASE ( self : List[Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[int]=0) ->Union[str, Any]: '''simple docstring''' A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(UpperCAmelCase__)).to(UpperCAmelCase__) A__ = image.cpu().permute(0 , 2 , 3 , 1)[0] A__ = Image.fromarray(np.uinta(UpperCAmelCase__)).convert('''RGB''') if str(UpperCAmelCase__).startswith('''mps'''): A__ = torch.manual_seed(UpperCAmelCase__) else: A__ = torch.Generator(device=UpperCAmelCase__).manual_seed(UpperCAmelCase__) A__ = { '''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 SCREAMING_SNAKE_CASE ( self : List[Any]) ->int: '''simple docstring''' if not hasattr(self.pipeline_class , '''_optional_components'''): return A__ = self.get_dummy_components() A__ = self.pipeline_class(**UpperCAmelCase__) pipe.to(UpperCAmelCase__) pipe.set_progress_bar_config(disable=UpperCAmelCase__) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components}) A__ = self.get_dummy_inputs(UpperCAmelCase__) A__ = pipe(**UpperCAmelCase__)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(UpperCAmelCase__) A__ = self.pipeline_class.from_pretrained(UpperCAmelCase__) pipe_loaded.to(UpperCAmelCase__) pipe_loaded.set_progress_bar_config(disable=UpperCAmelCase__) for optional_component in pipe._optional_components: self.assertTrue( getattr(UpperCAmelCase__ , UpperCAmelCase__) is None , f"""`{optional_component}` did not stay set to None after loading.""" , ) A__ = self.get_dummy_inputs(UpperCAmelCase__) A__ = pipe_loaded(**UpperCAmelCase__)[0] A__ = np.abs(output - output_loaded).max() self.assertLess(UpperCAmelCase__ , 1e-4) def SCREAMING_SNAKE_CASE ( self : int) ->Optional[int]: '''simple docstring''' A__ = '''cpu''' A__ = self.get_dummy_components() A__ = self.pipeline_class(**UpperCAmelCase__) pipe.to(UpperCAmelCase__) pipe.set_progress_bar_config(disable=UpperCAmelCase__) A__ = self.get_dummy_mask_inputs(UpperCAmelCase__) A__ = pipe.generate_mask(**UpperCAmelCase__) A__ = mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16)) A__ = np.array([0] * 9) A__ = np.abs(mask_slice.flatten() - expected_slice).max() self.assertLessEqual(UpperCAmelCase__ , 1e-3) self.assertEqual(mask[0, -3, -4] , 0) def SCREAMING_SNAKE_CASE ( self : str) ->Any: '''simple docstring''' A__ = '''cpu''' A__ = self.get_dummy_components() A__ = self.pipeline_class(**UpperCAmelCase__) pipe.to(UpperCAmelCase__) pipe.set_progress_bar_config(disable=UpperCAmelCase__) A__ = self.get_dummy_inversion_inputs(UpperCAmelCase__) A__ = pipe.invert(**UpperCAmelCase__).images A__ = image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3)) A__ = np.array( [0.5150, 0.5134, 0.5043, 0.5376, 0.4694, 0.51050, 0.5015, 0.4407, 0.4799] , ) A__ = np.abs(image_slice.flatten() - expected_slice).max() self.assertLessEqual(UpperCAmelCase__ , 1e-3) def SCREAMING_SNAKE_CASE ( self : Dict) ->List[Any]: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=5e-3) def SCREAMING_SNAKE_CASE ( self : Optional[int]) ->Union[str, Any]: '''simple docstring''' A__ = '''cpu''' A__ = self.get_dummy_components() A__ = {'''beta_start''': 0.00085, '''beta_end''': 0.012, '''beta_schedule''': '''scaled_linear'''} A__ = DPMSolverMultistepScheduler(**UpperCAmelCase__) A__ = DPMSolverMultistepInverseScheduler(**UpperCAmelCase__) A__ = self.pipeline_class(**UpperCAmelCase__) pipe.to(UpperCAmelCase__) pipe.set_progress_bar_config(disable=UpperCAmelCase__) A__ = self.get_dummy_inversion_inputs(UpperCAmelCase__) A__ = pipe.invert(**UpperCAmelCase__).images A__ = image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3)) A__ = np.array( [0.5150, 0.5134, 0.5043, 0.5376, 0.4694, 0.51050, 0.5015, 0.4407, 0.4799] , ) A__ = np.abs(image_slice.flatten() - expected_slice).max() self.assertLessEqual(UpperCAmelCase__ , 1e-3) @require_torch_gpu @slow class UpperCamelCase_ ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE ( self : List[Any]) ->Dict: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def SCREAMING_SNAKE_CASE ( cls : List[str]) ->str: '''simple docstring''' A__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png''') A__ = raw_image.convert('''RGB''').resize((768, 768)) A__ = raw_image def SCREAMING_SNAKE_CASE ( self : List[Any]) ->Any: '''simple docstring''' A__ = torch.manual_seed(0) A__ = StableDiffusionDiffEditPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-1''' , safety_checker=UpperCAmelCase__ , torch_dtype=torch.floataa) A__ = DDIMScheduler.from_config(pipe.scheduler.config) A__ = DDIMInverseScheduler.from_config(pipe.scheduler.config) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=UpperCAmelCase__) A__ = '''a bowl of fruit''' A__ = '''a bowl of pears''' A__ = pipe.generate_mask( image=self.raw_image , source_prompt=UpperCAmelCase__ , target_prompt=UpperCAmelCase__ , generator=UpperCAmelCase__ , ) A__ = pipe.invert( prompt=UpperCAmelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=UpperCAmelCase__).latents A__ = pipe( prompt=UpperCAmelCase__ , mask_image=UpperCAmelCase__ , image_latents=UpperCAmelCase__ , generator=UpperCAmelCase__ , negative_prompt=UpperCAmelCase__ , inpaint_strength=0.7 , output_type='''numpy''' , ).images[0] A__ = ( 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 SCREAMING_SNAKE_CASE ( self : Optional[int]) ->str: '''simple docstring''' A__ = torch.manual_seed(0) A__ = StableDiffusionDiffEditPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-1''' , safety_checker=UpperCAmelCase__ , torch_dtype=torch.floataa) A__ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) A__ = DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=UpperCAmelCase__) A__ = '''a bowl of fruit''' A__ = '''a bowl of pears''' A__ = pipe.generate_mask( image=self.raw_image , source_prompt=UpperCAmelCase__ , target_prompt=UpperCAmelCase__ , generator=UpperCAmelCase__ , ) A__ = pipe.invert( prompt=UpperCAmelCase__ , image=self.raw_image , inpaint_strength=0.7 , generator=UpperCAmelCase__ , num_inference_steps=25 , ).latents A__ = pipe( prompt=UpperCAmelCase__ , mask_image=UpperCAmelCase__ , image_latents=UpperCAmelCase__ , generator=UpperCAmelCase__ , negative_prompt=UpperCAmelCase__ , inpaint_strength=0.7 , num_inference_steps=25 , output_type='''numpy''' , ).images[0] A__ = ( 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
370
from __future__ import annotations import queue class UpperCamelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , UpperCAmelCase__ : Dict) ->Any: '''simple docstring''' A__ = data A__ = None A__ = None def SCREAMING_SNAKE_CASE ( ) -> TreeNode: """simple docstring""" print('''\n********Press N to stop entering at any point of time********\n''' ) A__ = input('''Enter the value of the root node: ''' ).strip().lower() A__ = queue.Queue() A__ = TreeNode(int(lowercase_ ) ) q.put(lowercase_ ) while not q.empty(): A__ = q.get() A__ = f"""Enter the left node of {node_found.data}: """ A__ = input(lowercase_ ).strip().lower() or '''n''' if check == "n": return tree_node A__ = TreeNode(int(lowercase_ ) ) A__ = left_node q.put(lowercase_ ) A__ = f"""Enter the right node of {node_found.data}: """ A__ = input(lowercase_ ).strip().lower() or '''n''' if check == "n": return tree_node A__ = TreeNode(int(lowercase_ ) ) A__ = right_node q.put(lowercase_ ) raise def SCREAMING_SNAKE_CASE ( lowercase_ ) -> None: """simple docstring""" if not isinstance(lowercase_ , lowercase_ ) or not node: return print(node.data , end=''',''' ) pre_order(node.left ) pre_order(node.right ) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> None: """simple docstring""" if not isinstance(lowercase_ , lowercase_ ) or not node: return in_order(node.left ) print(node.data , end=''',''' ) in_order(node.right ) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> None: """simple docstring""" if not isinstance(lowercase_ , lowercase_ ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=''',''' ) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> None: """simple docstring""" if not isinstance(lowercase_ , lowercase_ ) or not node: return A__ = queue.Queue() q.put(lowercase_ ) while not q.empty(): A__ = q.get() print(node_dequeued.data , end=''',''' ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> None: """simple docstring""" if not isinstance(lowercase_ , lowercase_ ) or not node: return A__ = queue.Queue() q.put(lowercase_ ) while not q.empty(): A__ = [] while not q.empty(): A__ = q.get() print(node_dequeued.data , end=''',''' ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(lowercase_ ) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> None: """simple docstring""" if not isinstance(lowercase_ , lowercase_ ) or not node: return A__ = [] A__ = node while n or stack: while n: # start from root node, find its left child print(n.data , end=''',''' ) stack.append(lowercase_ ) A__ = n.left # end of while means current node doesn't have left child A__ = stack.pop() # start to traverse its right child A__ = n.right def SCREAMING_SNAKE_CASE ( lowercase_ ) -> None: """simple docstring""" if not isinstance(lowercase_ , lowercase_ ) or not node: return A__ = [] A__ = node while n or stack: while n: stack.append(lowercase_ ) A__ = n.left A__ = stack.pop() print(n.data , end=''',''' ) A__ = n.right def SCREAMING_SNAKE_CASE ( lowercase_ ) -> None: """simple docstring""" if not isinstance(lowercase_ , lowercase_ ) or not node: return A__ , A__ = [], [] A__ = node stacka.append(lowercase_ ) while stacka: # to find the reversed order of post order, store it in stack2 A__ = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(lowercase_ ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=''',''' ) def SCREAMING_SNAKE_CASE ( lowercase_ = "" , lowercase_=50 , lowercase_="*" ) -> str: """simple docstring""" if not s: return "\n" + width * char A__ , A__ = divmod(width - len(lowercase_ ) - 2 , 2 ) return f"""{left * char} {s} {(left + extra) * char}""" if __name__ == "__main__": import doctest doctest.testmod() print(prompt("""Binary Tree Traversals""")) _lowerCamelCase : TreeNode = build_tree() print(prompt("""Pre Order Traversal""")) pre_order(node) print(prompt() + """\n""") print(prompt("""In Order Traversal""")) in_order(node) print(prompt() + """\n""") print(prompt("""Post Order Traversal""")) post_order(node) print(prompt() + """\n""") print(prompt("""Level Order Traversal""")) level_order(node) print(prompt() + """\n""") print(prompt("""Actual Level Order Traversal""")) level_order_actual(node) print("""*""" * 50 + """\n""") print(prompt("""Pre Order Traversal - Iteration Version""")) pre_order_iter(node) print(prompt() + """\n""") print(prompt("""In Order Traversal - Iteration Version""")) in_order_iter(node) print(prompt() + """\n""") print(prompt("""Post Order Traversal - Iteration Version""")) post_order_iter(node) print(prompt())
231
0
"""simple docstring""" import unittest from transformers import SPIECE_UNDERLINE, XLNetTokenizer, XLNetTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase_ : str = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class __A ( _SCREAMING_SNAKE_CASE, unittest.TestCase ): """simple docstring""" __lowerCAmelCase = XLNetTokenizer __lowerCAmelCase = XLNetTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = True def SCREAMING_SNAKE_CASE ( self ) -> Tuple: super().setUp() # We have a SentencePiece fixture for testing a =XLNetTokenizer(__A , keep_accents=__A ) tokenizer.sanitize_special_tokens() tokenizer.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: a ='''<s>''' a =1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__A ) , __A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__A ) , __A ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: a =list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<eod>''' ) self.assertEqual(len(__A ) , 1006 ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: a =XLNetTokenizer(__A , keep_accents=__A ) a =tokenizer.tokenize('''This is a test''' ) self.assertListEqual(__A , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__A ) , [285, 46, 10, 170, 382] ) a =tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __A , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) a =tokenizer.convert_tokens_to_ids(__A ) self.assertListEqual(__A , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] ) a =tokenizer.convert_ids_to_tokens(__A ) self.assertListEqual( __A , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: a =XLNetTokenizer(__A , do_lower_case=__A ) a =tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __A , [ SPIECE_UNDERLINE + '''''', '''i''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''se''', '''.''', ] , ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''▁he''', '''ll''', '''o'''] ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: a =XLNetTokenizer(__A , do_lower_case=__A ) a =tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __A , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''se''', '''.''', ] , ) @slow def SCREAMING_SNAKE_CASE ( self ) -> str: a =XLNetTokenizer.from_pretrained('''xlnet-base-cased''' ) a =tokenizer.encode('''sequence builders''' , add_special_tokens=__A ) a =tokenizer.encode('''multi-sequence build''' , add_special_tokens=__A ) a =tokenizer.build_inputs_with_special_tokens(__A ) a =tokenizer.build_inputs_with_special_tokens(__A , __A ) assert encoded_sentence == text + [4, 3] assert encoded_pair == text + [4] + text_a + [4, 3] @slow def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: # fmt: off a ={'''input_ids''': [[17, 2_1442, 270, 17, 10, 1_4645, 318, 34, 17, 4546, 3145, 787, 13, 7752, 2_2018, 23, 21, 17, 4546, 3145, 787, 13, 3352, 1_4431, 13, 5500, 11, 1176, 580, 13, 1_6819, 4797, 23, 17, 10, 1_7135, 658, 19, 457, 7932, 13, 184, 19, 3154, 1_7135, 6468, 19, 1404, 1_2269, 19, 4229, 5356, 1_6264, 46, 19, 17, 2_0545, 1_0395, 9, 9, 9, 11, 28, 6421, 9531, 2_0729, 17, 10, 353, 1_7022, 11, 21, 6421, 9531, 1_6949, 17, 10, 1_1509, 753, 11, 33, 95, 2421, 7385, 956, 1_4431, 2626, 25, 842, 7385, 4836, 21, 1429, 2272, 9855, 3120, 161, 2_4738, 19, 1_3203, 658, 218, 787, 21, 430, 1_8482, 847, 2637, 9, 4, 3], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 322, 2_2178, 27, 1064, 22, 956, 13, 1_1101, 1429, 5854, 2_4313, 1_8953, 40, 422, 2_4366, 68, 1758, 37, 1_0483, 1_4257, 31, 207, 263, 21, 203, 3773, 25, 71, 9735, 9, 4, 3], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 32, 2049, 3442, 17, 1_3894, 3380, 23, 95, 18, 1_7634, 2288, 9, 4, 3]], '''token_type_ids''': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__A , model_name='''xlnet-base-cased''' , revision='''c841166438c31ec7ca9a106dee7bb312b73ae511''' , )
81
"""simple docstring""" 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 lowerCamelCase_ : Any = logging.get_logger(__name__) lowerCamelCase_ : Tuple = {"""vocab_file""": """sentencepiece.bpe.model"""} lowerCamelCase_ : str = { """vocab_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model""" ), }, } lowerCamelCase_ : Optional[int] = { """moussaKam/mbarthez""": 1_0_2_4, """moussaKam/barthez""": 1_0_2_4, """moussaKam/barthez-orangesum-title""": 1_0_2_4, } lowerCamelCase_ : Tuple = """▁""" class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = VOCAB_FILES_NAMES __lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCAmelCase = ["input_ids", "attention_mask"] def __init__( self , __A , __A="<s>" , __A="</s>" , __A="</s>" , __A="<s>" , __A="<unk>" , __A="<pad>" , __A="<mask>" , __A = None , **__A , ) -> None: # Mask token behave like a normal word, i.e. include the space before it a =AddedToken(__A , lstrip=__A , rstrip=__A ) if isinstance(__A , __A ) else mask_token a ={} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__A , eos_token=__A , unk_token=__A , sep_token=__A , cls_token=__A , pad_token=__A , mask_token=__A , sp_model_kwargs=self.sp_model_kwargs , **__A , ) a =vocab_file a =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__A ) ) a ={'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} a =len(self.sp_model ) - 1 a ={v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self , __A , __A = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a =[self.cls_token_id] a =[self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self , __A , __A = None , __A = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__A , token_ids_a=__A , already_has_special_tokens=__A ) if token_ids_a is None: return [1] + ([0] * len(__A )) + [1] return [1] + ([0] * len(__A )) + [1, 1] + ([0] * len(__A )) + [1] def SCREAMING_SNAKE_CASE ( self , __A , __A = None ) -> List[int]: a =[self.sep_token_id] a =[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 SCREAMING_SNAKE_CASE ( self ) -> Any: return len(self.sp_model ) def SCREAMING_SNAKE_CASE ( self ) -> int: a ={self.convert_ids_to_tokens(__A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def SCREAMING_SNAKE_CASE ( self , __A ) -> List[str]: return self.sp_model.encode(__A , out_type=__A ) def SCREAMING_SNAKE_CASE ( self , __A ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] a =self.sp_model.PieceToId(__A ) return spm_id if spm_id else self.unk_token_id def SCREAMING_SNAKE_CASE ( self , __A ) -> Optional[Any]: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(__A ) def SCREAMING_SNAKE_CASE ( self , __A ) -> Tuple: a =[] a ='''''' a =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(__A ) + token a =True a =[] else: current_sub_tokens.append(__A ) a =False out_string += self.sp_model.decode(__A ) return out_string.strip() def __getstate__( self ) -> Tuple: a =self.__dict__.copy() a =None return state def __setstate__( self , __A ) -> Tuple: a =d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a ={} a =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self , __A , __A = None ) -> Tuple[str]: if not os.path.isdir(__A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return a =os.path.join( __A , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__A ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __A ) elif not os.path.isfile(self.vocab_file ): with open(__A , '''wb''' ) as fi: a =self.sp_model.serialized_model_proto() fi.write(__A ) return (out_vocab_file,)
81
1
"""simple docstring""" import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class a : """simple docstring""" def __init__( self: Tuple , UpperCamelCase: str , UpperCamelCase: int=13 , UpperCamelCase: Optional[Any]=7 , UpperCamelCase: List[Any]=True , UpperCamelCase: int=True , UpperCamelCase: str=False , UpperCamelCase: Optional[int]=True , UpperCamelCase: List[Any]=99 , UpperCamelCase: Optional[int]=32 , UpperCamelCase: str=5 , UpperCamelCase: Tuple=4 , UpperCamelCase: Optional[Any]=37 , UpperCamelCase: Tuple="gelu" , UpperCamelCase: Dict=0.1 , UpperCamelCase: Dict=0.1 , UpperCamelCase: int=5_12 , UpperCamelCase: str=16 , UpperCamelCase: Optional[Any]=2 , UpperCamelCase: List[Any]=0.02 , UpperCamelCase: str=3 , UpperCamelCase: Union[str, Any]=4 , UpperCamelCase: Tuple=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_labels A__ = num_choices A__ = scope def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = None A__ = None A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A__ = ids_tensor([self.batch_size] , self.num_choices ) A__ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" return OpenLlamaConfig( 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 , use_stable_embedding=UpperCamelCase , ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: str , UpperCamelCase: Optional[Any] , UpperCamelCase: str , UpperCamelCase: str , UpperCamelCase: List[str] , UpperCamelCase: List[Any] , UpperCamelCase: Union[str, Any] ): """simple docstring""" A__ = OpenLlamaModel(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase , attention_mask=UpperCamelCase ) A__ = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self: Optional[Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: List[Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: List[Any] , UpperCamelCase: Union[str, Any] , ): """simple docstring""" A__ = True A__ = OpenLlamaModel(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model( UpperCamelCase , attention_mask=UpperCamelCase , encoder_hidden_states=UpperCamelCase , encoder_attention_mask=UpperCamelCase , ) A__ = model( UpperCamelCase , attention_mask=UpperCamelCase , encoder_hidden_states=UpperCamelCase , ) A__ = model(UpperCamelCase , attention_mask=UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self: str , UpperCamelCase: str , UpperCamelCase: List[Any] , UpperCamelCase: Dict , UpperCamelCase: List[Any] , UpperCamelCase: List[Any] , UpperCamelCase: str , UpperCamelCase: List[Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: str , ): """simple docstring""" A__ = OpenLlamaForCausalLM(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase , attention_mask=UpperCamelCase , labels=UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self: Dict , UpperCamelCase: Any , UpperCamelCase: Any , UpperCamelCase: Dict , UpperCamelCase: List[str] , UpperCamelCase: Any , UpperCamelCase: int , UpperCamelCase: Tuple , UpperCamelCase: Tuple , UpperCamelCase: Union[str, Any] , ): """simple docstring""" A__ = True A__ = True A__ = OpenLlamaForCausalLM(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() # first forward pass A__ = model( UpperCamelCase , attention_mask=UpperCamelCase , encoder_hidden_states=UpperCamelCase , encoder_attention_mask=UpperCamelCase , use_cache=UpperCamelCase , ) A__ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids A__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) A__ = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and A__ = torch.cat([input_ids, next_tokens] , dim=-1 ) A__ = torch.cat([input_mask, next_mask] , dim=-1 ) A__ = model( UpperCamelCase , attention_mask=UpperCamelCase , encoder_hidden_states=UpperCamelCase , encoder_attention_mask=UpperCamelCase , output_hidden_states=UpperCamelCase , )["""hidden_states"""][0] A__ = model( UpperCamelCase , attention_mask=UpperCamelCase , encoder_hidden_states=UpperCamelCase , encoder_attention_mask=UpperCamelCase , past_key_values=UpperCamelCase , output_hidden_states=UpperCamelCase , )["""hidden_states"""][0] # select random slice A__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() A__ = output_from_no_past[:, -3:, random_slice_idx].detach() A__ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(UpperCamelCase , UpperCamelCase , atol=1e-3 ) ) def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.prepare_config_and_inputs() ( ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ) = config_and_inputs A__ = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class a ( _lowerCamelCase, _lowerCamelCase, _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) UpperCAmelCase = (OpenLlamaForCausalLM,) if is_torch_available() else () UpperCAmelCase = ( { "feature-extraction": OpenLlamaModel, "text-classification": OpenLlamaForSequenceClassification, "text-generation": OpenLlamaForCausalLM, "zero-shot": OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = False def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = OpenLlamaModelTester(self ) A__ = ConfigTester(self , config_class=UpperCamelCase , hidden_size=37 ) def UpperCamelCase ( self: Tuple ): """simple docstring""" self.config_tester.run_common_tests() def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: A__ = type self.model_tester.create_and_check_model(*UpperCamelCase ) def UpperCamelCase ( self: Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = input_dict["""input_ids"""] A__ = input_ids.ne(1 ).to(UpperCamelCase ) A__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) A__ = OpenLlamaForSequenceClassification(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase , attention_mask=UpperCamelCase , labels=UpperCamelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = """single_label_classification""" A__ = input_dict["""input_ids"""] A__ = input_ids.ne(1 ).to(UpperCamelCase ) A__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) A__ = OpenLlamaForSequenceClassification(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase , attention_mask=UpperCamelCase , labels=UpperCamelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def UpperCamelCase ( self: Any ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = """multi_label_classification""" A__ = input_dict["""input_ids"""] A__ = input_ids.ne(1 ).to(UpperCamelCase ) A__ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) A__ = OpenLlamaForSequenceClassification(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase , attention_mask=UpperCamelCase , labels=UpperCamelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip("""Open-Llama buffers include complex numbers, which breaks this test""" ) def UpperCamelCase ( self: List[str] ): """simple docstring""" pass @parameterized.expand([("""linear""",), ("""dynamic""",)] ) def UpperCamelCase ( self: Optional[int] , UpperCamelCase: Optional[int] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = ids_tensor([1, 10] , config.vocab_size ) A__ = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights A__ = OpenLlamaModel(UpperCamelCase ) original_model.to(UpperCamelCase ) original_model.eval() A__ = original_model(UpperCamelCase ).last_hidden_state A__ = original_model(UpperCamelCase ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights A__ = {"""type""": scaling_type, """factor""": 10.0} A__ = OpenLlamaModel(UpperCamelCase ) scaled_model.to(UpperCamelCase ) scaled_model.eval() A__ = scaled_model(UpperCamelCase ).last_hidden_state A__ = scaled_model(UpperCamelCase ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(UpperCamelCase , UpperCamelCase , atol=1e-5 ) ) else: self.assertFalse(torch.allclose(UpperCamelCase , UpperCamelCase , atol=1e-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(UpperCamelCase , UpperCamelCase , atol=1e-5 ) )
69
"""simple docstring""" import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class a : """simple docstring""" def __init__( self: Optional[int] , UpperCamelCase: List[str] , UpperCamelCase: Dict=13 , UpperCamelCase: Optional[Any]=30 , UpperCamelCase: Optional[Any]=2 , UpperCamelCase: List[str]=3 , UpperCamelCase: Tuple=True , UpperCamelCase: Dict=True , UpperCamelCase: Optional[int]=32 , UpperCamelCase: Tuple=5 , UpperCamelCase: Optional[Any]=4 , UpperCamelCase: Optional[Any]=37 , UpperCamelCase: Optional[Any]="gelu" , UpperCamelCase: Dict=0.1 , UpperCamelCase: Any=0.1 , UpperCamelCase: str=10 , UpperCamelCase: Any=0.02 , UpperCamelCase: List[Any]=None , UpperCamelCase: int=2 , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = scope A__ = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A__ = (image_size // patch_size) ** 2 A__ = num_patches + 1 def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def UpperCamelCase ( self: int ): """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCamelCase , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def UpperCamelCase ( self: Tuple , UpperCamelCase: str , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[int] ): """simple docstring""" A__ = ViTModel(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self: Optional[int] , UpperCamelCase: Optional[Any] , UpperCamelCase: Optional[int] , UpperCamelCase: Union[str, Any] ): """simple docstring""" A__ = ViTForMaskedImageModeling(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images A__ = 1 A__ = ViTForMaskedImageModeling(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ = model(UpperCamelCase ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def UpperCamelCase ( self: str , UpperCamelCase: Dict , UpperCamelCase: List[Any] , UpperCamelCase: List[Any] ): """simple docstring""" A__ = self.type_sequence_label_size A__ = ViTForImageClassification(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = model(UpperCamelCase , labels=UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A__ = 1 A__ = ViTForImageClassification(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() A__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ = model(UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCamelCase ( self: List[str] ): """simple docstring""" A__ = self.prepare_config_and_inputs() ( ( A__ ) , ( A__ ) , ( A__ ) , ) = config_and_inputs A__ = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class a ( _lowerCamelCase, _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) UpperCAmelCase = ( {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification} if is_torch_available() else {} ) UpperCAmelCase = True UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def UpperCamelCase ( self: Any ): """simple docstring""" A__ = ViTModelTester(self ) A__ = ConfigTester(self , config_class=UpperCamelCase , has_text_modality=UpperCamelCase , hidden_size=37 ) def UpperCamelCase ( self: str ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""ViT does not use inputs_embeds""" ) def UpperCamelCase ( self: str ): """simple docstring""" pass def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase , nn.Linear ) ) def UpperCamelCase ( self: Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(UpperCamelCase ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , UpperCamelCase ) def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase ) def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCamelCase ) @slow def UpperCamelCase ( self: Dict ): """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = ViTModel.from_pretrained(UpperCamelCase ) self.assertIsNotNone(UpperCamelCase ) def _snake_case ( ): A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" @cached_property def UpperCamelCase ( self: List[Any] ): """simple docstring""" return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224""" ) if is_vision_available() else None @slow def UpperCamelCase ( self: Dict ): """simple docstring""" A__ = ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224""" ).to(UpperCamelCase ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=UpperCamelCase , return_tensors="""pt""" ).to(UpperCamelCase ) # forward pass with torch.no_grad(): A__ = model(**UpperCamelCase ) # verify the logits A__ = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , UpperCamelCase ) A__ = torch.tensor([-0.2_744, 0.8_215, -0.0_836] ).to(UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCamelCase , atol=1e-4 ) ) @slow def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = ViTModel.from_pretrained("""facebook/dino-vits8""" ).to(UpperCamelCase ) A__ = ViTImageProcessor.from_pretrained("""facebook/dino-vits8""" , size=4_80 ) A__ = prepare_img() A__ = image_processor(images=UpperCamelCase , return_tensors="""pt""" ) A__ = inputs.pixel_values.to(UpperCamelCase ) # forward pass with torch.no_grad(): A__ = model(UpperCamelCase , interpolate_pos_encoding=UpperCamelCase ) # verify the logits A__ = torch.Size((1, 36_01, 3_84) ) self.assertEqual(outputs.last_hidden_state.shape , UpperCamelCase ) A__ = torch.tensor( [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]] ).to(UpperCamelCase ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate @require_torch_gpu def UpperCamelCase ( self: List[Any] ): """simple docstring""" A__ = ViTModel.from_pretrained("""facebook/dino-vits8""" , torch_dtype=torch.floataa , device_map="""auto""" ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=UpperCamelCase , return_tensors="""pt""" ) A__ = inputs.pixel_values.to(UpperCamelCase ) # forward pass to make sure inference works in fp16 with torch.no_grad(): A__ = model(UpperCamelCase )
69
1
'''simple docstring''' def snake_case_ ( SCREAMING_SNAKE_CASE__ ): """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Union[str, Any] = [], [] while len(SCREAMING_SNAKE_CASE__ ) > 1: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : List[Any] = min(SCREAMING_SNAKE_CASE__ ), max(SCREAMING_SNAKE_CASE__ ) start.append(SCREAMING_SNAKE_CASE__ ) end.append(SCREAMING_SNAKE_CASE__ ) collection.remove(SCREAMING_SNAKE_CASE__ ) collection.remove(SCREAMING_SNAKE_CASE__ ) end.reverse() return start + collection + end if __name__ == "__main__": UpperCAmelCase_ : List[Any] = input('Enter numbers separated by a comma:\n').strip() UpperCAmelCase_ : Tuple = [int(item) for item in user_input.split(',')] print(*merge_sort(unsorted), sep=',')
200
'''simple docstring''' UpperCAmelCase_ : Dict = [ 999, 800, 799, 600, 599, 500, 400, 399, 377, 355, 333, 311, 288, 266, 244, 222, 200, 199, 177, 155, 133, 111, 88, 66, 44, 22, 0, ] UpperCAmelCase_ : Any = [ 999, 976, 952, 928, 905, 882, 858, 857, 810, 762, 715, 714, 572, 429, 428, 286, 285, 238, 190, 143, 142, 118, 95, 71, 47, 24, 0, ] UpperCAmelCase_ : Tuple = [ 999, 988, 977, 966, 955, 944, 933, 922, 911, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 350, 300, 299, 266, 233, 200, 199, 179, 159, 140, 120, 100, 99, 88, 77, 66, 55, 44, 33, 22, 11, 0, ] UpperCAmelCase_ : Dict = [ 999, 995, 992, 989, 985, 981, 978, 975, 971, 967, 964, 961, 957, 956, 951, 947, 942, 937, 933, 928, 923, 919, 914, 913, 908, 903, 897, 892, 887, 881, 876, 871, 870, 864, 858, 852, 846, 840, 834, 828, 827, 820, 813, 806, 799, 792, 785, 784, 777, 770, 763, 756, 749, 742, 741, 733, 724, 716, 707, 699, 698, 688, 677, 666, 656, 655, 645, 634, 623, 613, 612, 598, 584, 570, 569, 555, 541, 527, 526, 505, 484, 483, 462, 440, 439, 396, 395, 352, 351, 308, 307, 264, 263, 220, 219, 176, 132, 88, 44, 0, ] UpperCAmelCase_ : Tuple = [ 999, 997, 995, 992, 990, 988, 986, 984, 981, 979, 977, 975, 972, 970, 968, 966, 964, 961, 959, 957, 956, 954, 951, 949, 946, 944, 941, 939, 936, 934, 931, 929, 926, 924, 921, 919, 916, 914, 913, 910, 907, 905, 902, 899, 896, 893, 891, 888, 885, 882, 879, 877, 874, 871, 870, 867, 864, 861, 858, 855, 852, 849, 846, 843, 840, 837, 834, 831, 828, 827, 824, 821, 817, 814, 811, 808, 804, 801, 798, 795, 791, 788, 785, 784, 780, 777, 774, 770, 766, 763, 760, 756, 752, 749, 746, 742, 741, 737, 733, 730, 726, 722, 718, 714, 710, 707, 703, 699, 698, 694, 690, 685, 681, 677, 673, 669, 664, 660, 656, 655, 650, 646, 641, 636, 632, 627, 622, 618, 613, 612, 607, 602, 596, 591, 586, 580, 575, 570, 569, 563, 557, 551, 545, 539, 533, 527, 526, 519, 512, 505, 498, 491, 484, 483, 474, 466, 457, 449, 440, 439, 428, 418, 407, 396, 395, 381, 366, 352, 351, 330, 308, 307, 286, 264, 263, 242, 220, 219, 176, 175, 132, 131, 88, 44, 0, ] UpperCAmelCase_ : Union[str, Any] = [ 999, 991, 982, 974, 966, 958, 950, 941, 933, 925, 916, 908, 900, 899, 874, 850, 825, 800, 799, 700, 600, 500, 400, 300, 200, 100, 0, ] UpperCAmelCase_ : Tuple = [ 999, 992, 985, 978, 971, 964, 957, 949, 942, 935, 928, 921, 914, 907, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 300, 299, 200, 199, 100, 99, 0, ] UpperCAmelCase_ : int = [ 999, 996, 992, 989, 985, 982, 979, 975, 972, 968, 965, 961, 958, 955, 951, 948, 944, 941, 938, 934, 931, 927, 924, 920, 917, 914, 910, 907, 903, 900, 899, 891, 884, 876, 869, 861, 853, 846, 838, 830, 823, 815, 808, 800, 799, 788, 777, 766, 755, 744, 733, 722, 711, 700, 699, 688, 677, 666, 655, 644, 633, 622, 611, 600, 599, 585, 571, 557, 542, 528, 514, 500, 499, 485, 471, 457, 442, 428, 414, 400, 399, 379, 359, 340, 320, 300, 299, 279, 259, 240, 220, 200, 199, 166, 133, 100, 99, 66, 33, 0, ]
200
1
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 DetrConfig, DetrForObjectDetection, DetrForSegmentation, DetrImageProcessor, ResNetConfig from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Dict: # initialize config if "resnet-50" in model_name: UpperCAmelCase_ = ResNetConfig.from_pretrained('''microsoft/resnet-50''' ) elif "resnet-101" in model_name: UpperCAmelCase_ = ResNetConfig.from_pretrained('''microsoft/resnet-101''' ) else: raise ValueError('''Model name should include either resnet50 or resnet101''' ) UpperCAmelCase_ = DetrConfig(use_timm_backbone=__UpperCamelCase , backbone_config=__UpperCamelCase ) # set label attributes UpperCAmelCase_ = '''panoptic''' in model_name if is_panoptic: UpperCAmelCase_ = 250 else: UpperCAmelCase_ = 91 UpperCAmelCase_ = '''huggingface/label-files''' UpperCAmelCase_ = '''coco-detection-id2label.json''' UpperCAmelCase_ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='''dataset''' ) , '''r''' ) ) UpperCAmelCase_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} UpperCAmelCase_ = idalabel UpperCAmelCase_ = {v: k for k, v in idalabel.items()} return config, is_panoptic def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Union[str, Any]: # here we list all keys to be renamed (original name on the left, our name on the right) UpperCAmelCase_ = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.conv1.weight''', '''backbone.conv_encoder.model.embedder.embedder.convolution.weight''') ) rename_keys.append(('''backbone.0.body.bn1.weight''', '''backbone.conv_encoder.model.embedder.embedder.normalization.weight''') ) rename_keys.append(('''backbone.0.body.bn1.bias''', '''backbone.conv_encoder.model.embedder.embedder.normalization.bias''') ) rename_keys.append(('''backbone.0.body.bn1.running_mean''', '''backbone.conv_encoder.model.embedder.embedder.normalization.running_mean''') ) rename_keys.append(('''backbone.0.body.bn1.running_var''', '''backbone.conv_encoder.model.embedder.embedder.normalization.running_var''') ) # stages for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): # shortcut if layer_idx == 0: rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.0.weight', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.convolution.weight', ) ) rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.weight', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.weight', ) ) rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.bias', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.bias', ) ) rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.running_mean', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.running_mean', ) ) rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.running_var', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.running_var', ) ) # 3 convs for i in range(3 ): rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.conv{i+1}.weight', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.convolution.weight', ) ) rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.weight', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.weight', ) ) rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.bias', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.bias', ) ) rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.running_mean', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.running_mean', ) ) rename_keys.append( ( f'backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.running_var', f'backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.running_var', ) ) # fmt: on for i in range(config.encoder_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( ( f'transformer.encoder.layers.{i}.self_attn.out_proj.weight', f'encoder.layers.{i}.self_attn.out_proj.weight', ) ) rename_keys.append( (f'transformer.encoder.layers.{i}.self_attn.out_proj.bias', f'encoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'encoder.layers.{i}.fc2.bias') ) rename_keys.append( (f'transformer.encoder.layers.{i}.norm1.weight', f'encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append( (f'transformer.encoder.layers.{i}.norm1.bias', f'encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append( (f'transformer.encoder.layers.{i}.norm2.weight', f'encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'encoder.layers.{i}.final_layer_norm.bias') ) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( ( f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'decoder.layers.{i}.self_attn.out_proj.weight', ) ) rename_keys.append( (f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append( ( f'transformer.decoder.layers.{i}.multihead_attn.out_proj.weight', f'decoder.layers.{i}.encoder_attn.out_proj.weight', ) ) rename_keys.append( ( f'transformer.decoder.layers.{i}.multihead_attn.out_proj.bias', f'decoder.layers.{i}.encoder_attn.out_proj.bias', ) ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'decoder.layers.{i}.fc2.bias') ) rename_keys.append( (f'transformer.decoder.layers.{i}.norm1.weight', f'decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append( (f'transformer.decoder.layers.{i}.norm1.bias', f'decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append( (f'transformer.decoder.layers.{i}.norm2.weight', f'decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append( (f'transformer.decoder.layers.{i}.norm2.bias', f'decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append( (f'transformer.decoder.layers.{i}.norm3.weight', f'decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'decoder.layers.{i}.final_layer_norm.bias') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ] ) return rename_keys def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : int , __UpperCamelCase : Optional[int] ) -> Union[str, Any]: UpperCAmelCase_ = state_dict.pop(__UpperCamelCase ) UpperCAmelCase_ = val def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : List[Any]=False ) -> Dict: UpperCAmelCase_ = '''''' if is_panoptic: UpperCAmelCase_ = '''detr.''' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) UpperCAmelCase_ = state_dict.pop(f'{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight' ) UpperCAmelCase_ = state_dict.pop(f'{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict UpperCAmelCase_ = in_proj_weight[:256, :] UpperCAmelCase_ = in_proj_bias[:256] UpperCAmelCase_ = in_proj_weight[256:512, :] UpperCAmelCase_ = in_proj_bias[256:512] UpperCAmelCase_ = in_proj_weight[-256:, :] UpperCAmelCase_ = in_proj_bias[-256:] # next: transformer decoder (which is a bit more complex because it also includes cross-attention) for i in range(6 ): # read in weights + bias of input projection layer of self-attention UpperCAmelCase_ = state_dict.pop(f'{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) UpperCAmelCase_ = state_dict.pop(f'{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict UpperCAmelCase_ = in_proj_weight[:256, :] UpperCAmelCase_ = in_proj_bias[:256] UpperCAmelCase_ = in_proj_weight[256:512, :] UpperCAmelCase_ = in_proj_bias[256:512] UpperCAmelCase_ = in_proj_weight[-256:, :] UpperCAmelCase_ = in_proj_bias[-256:] # read in weights + bias of input projection layer of cross-attention UpperCAmelCase_ = state_dict.pop( f'{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_weight' ) UpperCAmelCase_ = state_dict.pop(f'{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_bias' ) # next, add query, keys and values (in that order) of cross-attention to the state dict UpperCAmelCase_ = in_proj_weight_cross_attn[:256, :] UpperCAmelCase_ = in_proj_bias_cross_attn[:256] UpperCAmelCase_ = in_proj_weight_cross_attn[256:512, :] UpperCAmelCase_ = in_proj_bias_cross_attn[256:512] UpperCAmelCase_ = in_proj_weight_cross_attn[-256:, :] UpperCAmelCase_ = in_proj_bias_cross_attn[-256:] def SCREAMING_SNAKE_CASE ( ) -> int: UpperCAmelCase_ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' UpperCAmelCase_ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple , __UpperCamelCase : Any=None , __UpperCamelCase : Optional[Any]=False ) -> Optional[Any]: UpperCAmelCase_ , UpperCAmelCase_ = get_detr_config(__UpperCamelCase ) # load original model from torch hub UpperCAmelCase_ = { '''detr-resnet-50''': '''detr_resnet50''', '''detr-resnet-101''': '''detr_resnet101''', } logger.info(f'Converting model {model_name}...' ) UpperCAmelCase_ = torch.hub.load('''facebookresearch/detr''' , model_name_to_original_name[model_name] , pretrained=__UpperCamelCase ).eval() UpperCAmelCase_ = detr.state_dict() # rename keys for src, dest in create_rename_keys(__UpperCamelCase ): if is_panoptic: UpperCAmelCase_ = '''detr.''' + src rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # query, key and value matrices need special treatment read_in_q_k_v(__UpperCamelCase , is_panoptic=__UpperCamelCase ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them UpperCAmelCase_ = '''detr.model.''' if is_panoptic else '''model.''' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('''detr''' ) and not key.startswith('''class_labels_classifier''' ) and not key.startswith('''bbox_predictor''' ) ): UpperCAmelCase_ = state_dict.pop(__UpperCamelCase ) UpperCAmelCase_ = val elif "class_labels_classifier" in key or "bbox_predictor" in key: UpperCAmelCase_ = state_dict.pop(__UpperCamelCase ) UpperCAmelCase_ = val elif key.startswith('''bbox_attention''' ) or key.startswith('''mask_head''' ): continue else: UpperCAmelCase_ = state_dict.pop(__UpperCamelCase ) UpperCAmelCase_ = val else: if not key.startswith('''class_labels_classifier''' ) and not key.startswith('''bbox_predictor''' ): UpperCAmelCase_ = state_dict.pop(__UpperCamelCase ) UpperCAmelCase_ = val # finally, create HuggingFace model and load state dict UpperCAmelCase_ = DetrForSegmentation(__UpperCamelCase ) if is_panoptic else DetrForObjectDetection(__UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) model.eval() # verify our conversion on an image UpperCAmelCase_ = '''coco_panoptic''' if is_panoptic else '''coco_detection''' UpperCAmelCase_ = DetrImageProcessor(format=__UpperCamelCase ) UpperCAmelCase_ = processor(images=prepare_img() , return_tensors='''pt''' ) UpperCAmelCase_ = encoding['''pixel_values'''] UpperCAmelCase_ = detr(__UpperCamelCase ) UpperCAmelCase_ = model(__UpperCamelCase ) assert torch.allclose(outputs.logits , original_outputs['''pred_logits'''] , atol=1e-3 ) assert torch.allclose(outputs.pred_boxes , original_outputs['''pred_boxes'''] , atol=1e-3 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['''pred_masks'''] , atol=1e-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: # Save model and image processor logger.info(f'Saving PyTorch model and image processor to {pytorch_dump_folder_path}...' ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) model.save_pretrained(__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) if push_to_hub: # Upload model and image processor to the hub logger.info('''Uploading PyTorch model and image processor to the hub...''' ) model.push_to_hub(f'nielsr/{model_name}' ) processor.push_to_hub(f'nielsr/{model_name}' ) if __name__ == "__main__": _lowerCamelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', default='detr-resnet-50', type=str, choices=['detr-resnet-50', 'detr-resnet-101'], help='Name of the DETR model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.' ) parser.add_argument('--push_to_hub', action='store_true', help='Whether to push the model to the hub or not.') _lowerCamelCase = parser.parse_args() convert_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
354
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=_A ) class a ( _A ): '''simple docstring''' lowerCAmelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) lowerCAmelCase : ClassVar[Features] = Features({'audio': Audio()} ) lowerCAmelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) lowerCAmelCase : str = "audio" lowerCAmelCase : str = "labels" def lowerCamelCase_ ( self : Optional[Any] , __snake_case : List[Any] ): if self.label_column not in features: raise ValueError(F'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __snake_case ): raise ValueError(F'Column {self.label_column} is not a ClassLabel.' ) UpperCAmelCase_ = copy.deepcopy(self ) UpperCAmelCase_ = self.label_schema.copy() UpperCAmelCase_ = features[self.label_column] UpperCAmelCase_ = label_schema return task_template @property def lowerCamelCase_ ( self : Tuple ): return { self.audio_column: "audio", self.label_column: "labels", }
177
0
"""simple docstring""" import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class _UpperCAmelCase : '''simple docstring''' @property def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' return self.get_dummy_input() @property def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(f"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'.""" ) def SCREAMING_SNAKE_CASE (self , a_=True , a_=False , a_=False , a_=False , ): '''simple docstring''' __snake_case : str = 4 __snake_case : List[Any] = 32 __snake_case : Union[str, Any] = (32, 32) __snake_case : Dict = torch.manual_seed(0 ) __snake_case : int = torch.device(a_ ) __snake_case : str = (batch_size, num_channels) + sizes __snake_case : List[Any] = randn_tensor(a_ , generator=a_ , device=a_ ) __snake_case : List[str] = {'''hidden_states''': hidden_states} if include_temb: __snake_case : Any = 1_28 __snake_case : Optional[int] = randn_tensor((batch_size, temb_channels) , generator=a_ , device=a_ ) if include_res_hidden_states_tuple: __snake_case : Any = torch.manual_seed(1 ) __snake_case : int = (randn_tensor(a_ , generator=a_ , device=a_ ),) if include_encoder_hidden_states: __snake_case : Optional[Any] = floats_tensor((batch_size, 32, 32) ).to(a_ ) if include_skip_sample: __snake_case : Optional[Any] = randn_tensor(((batch_size, 3) + sizes) , generator=a_ , device=a_ ) return dummy_input def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Any = { '''in_channels''': 32, '''out_channels''': 32, '''temb_channels''': 1_28, } if self.block_type == "up": __snake_case : Optional[int] = 32 if self.block_type == "mid": init_dict.pop('''out_channels''' ) __snake_case : int = self.dummy_input return init_dict, inputs_dict def SCREAMING_SNAKE_CASE (self , a_ ): '''simple docstring''' __snake_case , __snake_case : List[Any] = self.prepare_init_args_and_inputs_for_common() __snake_case : List[Any] = self.block_class(**a_ ) unet_block.to(a_ ) unet_block.eval() with torch.no_grad(): __snake_case : Union[str, Any] = unet_block(**a_ ) if isinstance(a_ , a_ ): __snake_case : Tuple = output[0] self.assertEqual(output.shape , self.output_shape ) __snake_case : Dict = output[0, -1, -3:, -3:] __snake_case : int = torch.tensor(a_ ).to(a_ ) assert torch_all_close(output_slice.flatten() , a_ , atol=5E-3 ) @unittest.skipIf(torch_device == '''mps''' , '''Training is not supported in mps''' ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case , __snake_case : str = self.prepare_init_args_and_inputs_for_common() __snake_case : Optional[int] = self.block_class(**a_ ) model.to(a_ ) model.train() __snake_case : int = model(**a_ ) if isinstance(a_ , a_ ): __snake_case : List[Any] = output[0] __snake_case : Optional[Any] = torch.device(a_ ) __snake_case : Tuple = randn_tensor(output.shape , device=a_ ) __snake_case : str = torch.nn.functional.mse_loss(a_ , a_ ) loss.backward()
102
"""simple docstring""" import math def lowercase ( _snake_case : int ) ->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(_snake_case ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( _snake_case : float = 0.1 ) ->int: """simple docstring""" __snake_case : Tuple = 3 __snake_case : Any = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(_snake_case ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
102
1
from sklearn.metrics import recall_score import datasets _lowerCamelCase : List[Any] = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ _lowerCamelCase : Any = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ _lowerCamelCase : Dict = """ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase_ ( datasets.Metric ): '''simple docstring''' def SCREAMING_SNAKE_CASE ( self : str) ->str: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''int32''')), '''references''': datasets.Sequence(datasets.Value('''int32''')), } if self.config_name == '''multilabel''' else { '''predictions''': datasets.Value('''int32'''), '''references''': datasets.Value('''int32'''), }) , reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html'''] , ) def SCREAMING_SNAKE_CASE ( self : Dict , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : int=1 , UpperCAmelCase__ : List[Any]="binary" , UpperCAmelCase__ : Optional[int]=None , UpperCAmelCase__ : List[Any]="warn" , ) ->int: '''simple docstring''' A__ = recall_score( UpperCAmelCase__ , UpperCAmelCase__ , labels=UpperCAmelCase__ , pos_label=UpperCAmelCase__ , average=UpperCAmelCase__ , sample_weight=UpperCAmelCase__ , zero_division=UpperCAmelCase__ , ) return {"recall": float(UpperCAmelCase__) if score.size == 1 else score}
231
# This script creates a super tiny model that is useful inside tests, when we just want to test that # the machinery works, without needing to the check the quality of the outcomes. # # This version creates a tiny model through reduction of a normal pre-trained model, but keeping the # full vocab, merges file, and thus also resulting in a larger model due to a large vocab size. # This gives ~3MB in total for all files. # # If you want a 50 times smaller than this see `fsmt-make-super-tiny-model.py`, which is slightly more complicated # # # It will be used then as "stas/tiny-wmt19-en-de" # Build from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration _lowerCamelCase : Optional[Any] = """facebook/wmt19-en-de""" _lowerCamelCase : Optional[Any] = FSMTTokenizer.from_pretrained(mname) # get the correct vocab sizes, etc. from the master model _lowerCamelCase : int = FSMTConfig.from_pretrained(mname) config.update( dict( d_model=4, encoder_layers=1, decoder_layers=1, encoder_ffn_dim=4, decoder_ffn_dim=4, encoder_attention_heads=1, decoder_attention_heads=1, ) ) _lowerCamelCase : Union[str, Any] = FSMTForConditionalGeneration(config) print(F'''num of params {tiny_model.num_parameters()}''') # Test _lowerCamelCase : int = tokenizer(["""Making tiny model"""], return_tensors="""pt""") _lowerCamelCase : int = tiny_model(**batch) print("""test output:""", len(outputs.logits[0])) # Save _lowerCamelCase : str = """tiny-wmt19-en-de""" tiny_model.half() # makes it smaller tiny_model.save_pretrained(mname_tiny) tokenizer.save_pretrained(mname_tiny) print(F'''Generated {mname_tiny}''') # Upload # transformers-cli upload tiny-wmt19-en-de
231
1
"""simple docstring""" from io import BytesIO from typing import List, Union import requests from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_decord_available(): import numpy as np from decord import VideoReader if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING SCREAMING_SNAKE_CASE_ : List[Any] = logging.get_logger(__name__) @add_end_docstrings(__a ) class a ( __a ): """simple docstring""" def __init__( self: List[str] , *UpperCamelCase: Optional[int] , **UpperCamelCase: Tuple ): """simple docstring""" super().__init__(*_UpperCamelCase , **_UpperCamelCase ) requires_backends(self , """decord""" ) self.check_model_type(_UpperCamelCase ) def UpperCamelCase ( self: List[str] , UpperCamelCase: Union[str, Any]=None , UpperCamelCase: List[Any]=None , UpperCamelCase: Tuple=None ): """simple docstring""" A__ = {} if frame_sampling_rate is not None: A__ = frame_sampling_rate if num_frames is not None: A__ = num_frames A__ = {} if top_k is not None: A__ = top_k return preprocess_params, {}, postprocess_params def __call__( self: List[Any] , UpperCamelCase: List[Any] , **UpperCamelCase: List[Any] ): """simple docstring""" return super().__call__(_UpperCamelCase , **_UpperCamelCase ) def UpperCamelCase ( self: int , UpperCamelCase: Any , UpperCamelCase: List[Any]=None , UpperCamelCase: Union[str, Any]=1 ): """simple docstring""" if num_frames is None: A__ = self.model.config.num_frames if video.startswith("""http://""" ) or video.startswith("""https://""" ): A__ = BytesIO(requests.get(_UpperCamelCase ).content ) A__ = VideoReader(_UpperCamelCase ) videoreader.seek(0 ) A__ = 0 A__ = num_frames * frame_sampling_rate - 1 A__ = np.linspace(_UpperCamelCase , _UpperCamelCase , num=_UpperCamelCase , dtype=np.intaa ) A__ = videoreader.get_batch(_UpperCamelCase ).asnumpy() A__ = list(_UpperCamelCase ) A__ = self.image_processor(_UpperCamelCase , return_tensors=self.framework ) return model_inputs def UpperCamelCase ( self: Any , UpperCamelCase: List[str] ): """simple docstring""" A__ = self.model(**_UpperCamelCase ) return model_outputs def UpperCamelCase ( self: List[Any] , UpperCamelCase: Optional[int] , UpperCamelCase: List[Any]=5 ): """simple docstring""" if top_k > self.model.config.num_labels: A__ = self.model.config.num_labels if self.framework == "pt": A__ = model_outputs.logits.softmax(-1 )[0] A__ , A__ = probs.topk(_UpperCamelCase ) else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) A__ = scores.tolist() A__ = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_UpperCamelCase , _UpperCamelCase )]
335
import unittest from datasets import load_dataset from transformers import BloomTokenizerFast from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _lowerCAmelCase ( __a , unittest.TestCase ): _lowercase =None _lowercase =BloomTokenizerFast _lowercase =BloomTokenizerFast _lowercase =True _lowercase =False _lowercase ='''tokenizer_file''' _lowercase ={'''bos_token''': '''<s>''', '''eos_token''': '''</s>''', '''unk_token''': '''<unk>''', '''pad_token''': '''<pad>'''} def __a ( self ) -> Dict: super().setUp() lowerCAmelCase_ = BloomTokenizerFast.from_pretrained("bigscience/tokenizer" ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self , **_UpperCamelCase ) -> Tuple: kwargs.update(self.special_tokens_map ) return BloomTokenizerFast.from_pretrained(self.tmpdirname , **_UpperCamelCase ) def __a ( self ) -> List[str]: lowerCAmelCase_ = self.get_rust_tokenizer() lowerCAmelCase_ = ["The quick brown fox</s>", "jumps over the lazy dog</s>"] lowerCAmelCase_ = [[2_175, 23_714, 73_173, 144_252, 2], [77, 132_619, 3_478, 368, 109_586, 35_433, 2]] lowerCAmelCase_ = tokenizer.batch_encode_plus(_UpperCamelCase )["input_ids"] self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) lowerCAmelCase_ = tokenizer.batch_decode(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) def __a ( self , _UpperCamelCase=6 ) -> Tuple: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): lowerCAmelCase_ = self.rust_tokenizer_class.from_pretrained(_UpperCamelCase , **_UpperCamelCase ) # tokenizer_r.pad_token = None # Hotfixing padding = None # Simple input lowerCAmelCase_ = "This is a simple input" lowerCAmelCase_ = ["This is a simple input 1", "This is a simple input 2"] lowerCAmelCase_ = ("This is a simple input", "This is a pair") lowerCAmelCase_ = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests try: tokenizer_r.encode(_UpperCamelCase , max_length=_UpperCamelCase ) tokenizer_r.encode_plus(_UpperCamelCase , max_length=_UpperCamelCase ) tokenizer_r.batch_encode_plus(_UpperCamelCase , max_length=_UpperCamelCase ) tokenizer_r.encode(_UpperCamelCase , max_length=_UpperCamelCase ) tokenizer_r.batch_encode_plus(_UpperCamelCase , max_length=_UpperCamelCase ) except ValueError: self.fail("Bloom Tokenizer should be able to deal with padding" ) lowerCAmelCase_ = None # Hotfixing padding = None self.assertRaises(_UpperCamelCase , tokenizer_r.encode , _UpperCamelCase , max_length=_UpperCamelCase , padding="max_length" ) # Simple input self.assertRaises(_UpperCamelCase , tokenizer_r.encode_plus , _UpperCamelCase , max_length=_UpperCamelCase , padding="max_length" ) # Simple input self.assertRaises( _UpperCamelCase , tokenizer_r.batch_encode_plus , _UpperCamelCase , max_length=_UpperCamelCase , padding="max_length" , ) # Pair input self.assertRaises(_UpperCamelCase , tokenizer_r.encode , _UpperCamelCase , max_length=_UpperCamelCase , padding="max_length" ) # Pair input self.assertRaises(_UpperCamelCase , tokenizer_r.encode_plus , _UpperCamelCase , max_length=_UpperCamelCase , padding="max_length" ) # Pair input self.assertRaises( _UpperCamelCase , tokenizer_r.batch_encode_plus , _UpperCamelCase , max_length=_UpperCamelCase , padding="max_length" , ) def __a ( self ) -> Any: lowerCAmelCase_ = self.get_rust_tokenizer() lowerCAmelCase_ = load_dataset("xnli" , "all_languages" , split="test" , streaming=_UpperCamelCase ) lowerCAmelCase_ = next(iter(_UpperCamelCase ) )["premise"] # pick up one data lowerCAmelCase_ = list(sample_data.values() ) lowerCAmelCase_ = list(map(tokenizer.encode , _UpperCamelCase ) ) lowerCAmelCase_ = [tokenizer.decode(_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase ) for x in output_tokens] self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) def __a ( self ) -> List[Any]: # The test has to be overriden because BLOOM uses ALiBi positional embeddings that does not have # any sequence length constraints. This test of the parent class will fail since it relies on the # maximum sequence length of the positoonal embeddings. self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map ) , 1 ) self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values() )[0] ) , 1 )
231
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available, ) snake_case_ = { """configuration_speecht5""": [ """SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP""", """SpeechT5Config""", """SpeechT5HifiGanConfig""", ], """feature_extraction_speecht5""": ["""SpeechT5FeatureExtractor"""], """processing_speecht5""": ["""SpeechT5Processor"""], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ = ["""SpeechT5Tokenizer"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ = [ """SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST""", """SpeechT5ForSpeechToText""", """SpeechT5ForSpeechToSpeech""", """SpeechT5ForTextToSpeech""", """SpeechT5Model""", """SpeechT5PreTrainedModel""", """SpeechT5HifiGan""", ] if TYPE_CHECKING: from .configuration_speechta import ( SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP, SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP, SpeechTaConfig, SpeechTaHifiGanConfig, ) from .feature_extraction_speechta import SpeechTaFeatureExtractor from .processing_speechta import SpeechTaProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speechta import SpeechTaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speechta import ( SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaModel, SpeechTaPreTrainedModel, ) else: import sys snake_case_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
181
"""simple docstring""" import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class A_ ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = WavaVecaPhonemeCTCTokenizer __UpperCamelCase = False def UpperCAmelCase__ ( self :Optional[int] ) -> int: super().setUp() UpperCAmelCase = ( '<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː ' 'ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː ' 'ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 ' 'oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ ' 'pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ ' 'yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ ' 'əʊ S ɡʲ onɡ2 u" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ ' 'ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ ' 'ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ ' 'uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ ' 'ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ ' 'ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ ' 'ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4' ).split(' ' ) UpperCAmelCase = dict(zip(lowercase_ , range(len(lowercase_ ) ) ) ) UpperCAmelCase = {'pad_token': '<pad>', 'unk_token': '<unk>', 'bos_token': '<s>', 'eos_token': '</s>'} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(lowercase_ ) + '\n' ) def UpperCAmelCase__ ( self :Dict , lowercase_ :Any , lowercase_ :Union[str, Any]=False , lowercase_ :int=20 , lowercase_ :Dict=5 ) -> Tuple[str, list]: UpperCAmelCase = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=lowercase_ )) for i in range(len(lowercase_ ) )] UpperCAmelCase = list(filter(lambda lowercase_ : [t[0]] == tokenizer.encode(t[1] , do_phonemize=lowercase_ ) , lowercase_ ) ) if max_length is not None and len(lowercase_ ) > max_length: UpperCAmelCase = toks[:max_length] if min_length is not None and len(lowercase_ ) < min_length and len(lowercase_ ) > 0: while len(lowercase_ ) < min_length: UpperCAmelCase = toks + toks # toks_str = [t[1] for t in toks] UpperCAmelCase = [t[0] for t in toks] # Ensure consistency UpperCAmelCase = tokenizer.decode(lowercase_ , clean_up_tokenization_spaces=lowercase_ ) if " " not in output_txt and len(lowercase_ ) > 1: UpperCAmelCase = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=lowercase_ ) + ' ' + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=lowercase_ ) ) if with_prefix_space: UpperCAmelCase = ' ' + output_txt UpperCAmelCase = tokenizer.encode(lowercase_ , add_special_tokens=lowercase_ ) return output_txt, output_ids def UpperCAmelCase__ ( self :Union[str, Any] , **lowercase_ :Union[str, Any] ) -> Optional[Any]: kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **lowercase_ ) def UpperCAmelCase__ ( self :int ) -> str: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) # check adding a single token tokenizer.add_tokens('xxx' ) UpperCAmelCase = tokenizer('m xxx ɪ' , do_phonemize=lowercase_ ).input_ids self.assertEqual(lowercase_ , [13, 3_92, 17] ) # xxx should be last token tokenizer.add_tokens(['aaa', 'bbb', 'ccc'] ) UpperCAmelCase = tokenizer('m aaa ɪ ccc' , do_phonemize=lowercase_ ).input_ids self.assertEqual(lowercase_ , [13, 3_93, 17, 3_95] ) # aaa and ccc should be after xxx and 2 after aaa UpperCAmelCase = tokenizer('maɪ c' , do_phonemize=lowercase_ ).input_ids self.assertEqual(lowercase_ , [3, 2_00] ) # mai should be <unk> (=3) def UpperCAmelCase__ ( self :Tuple ) -> Union[str, Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) self.assertEqual(lowercase_ , 'h ə l oʊ h aʊ ɑːɹ j uː' ) def UpperCAmelCase__ ( self :Dict ) -> int: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) self.assertEqual(tokenizer(lowercase_ ).input_ids , tokenizer(lowercase_ , do_phonemize=lowercase_ ).input_ids ) def UpperCAmelCase__ ( self :Optional[Any] ) -> Dict: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) UpperCAmelCase = tokenizer.decode(tokenizer(lowercase_ ).input_ids ) self.assertEqual(lowercase_ , lowercase_ ) def UpperCAmelCase__ ( self :Optional[int] ) -> str: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] UpperCAmelCase = tokenizer.decode(sample_ids[0] ) UpperCAmelCase = tokenizer.batch_decode(lowercase_ ) self.assertEqual(lowercase_ , batch_tokens[0] ) self.assertEqual(lowercase_ , ['k s ɾ ɾ l ɭʲ', 'j ð s j ð s oːɹ'] ) def UpperCAmelCase__ ( self :Any ) -> str: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) self.assertEqual(lowercase_ , 'h ə l oʊ | h aʊ | ɑːɹ | j uː |' ) def UpperCAmelCase__ ( self :Any ) -> Any: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) self.assertEqual(tokenizer(lowercase_ ).input_ids , tokenizer(lowercase_ , do_phonemize=lowercase_ ).input_ids ) def UpperCAmelCase__ ( self :Dict ) -> Union[str, Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) # fmt: off UpperCAmelCase = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter UpperCAmelCase = tokenizer.decode(sample_ids[0] ) UpperCAmelCase = tokenizer.batch_decode(lowercase_ ) self.assertEqual(lowercase_ , batch_tokens[0] ) self.assertEqual(lowercase_ , ['k s ɾ ɾ l ɭʲ', 'j ð s j ð s oːɹ'] ) # decode with no word_del_token filter UpperCAmelCase = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=lowercase_ ) UpperCAmelCase = tokenizer.batch_decode(lowercase_ , filter_word_delimiter_token=lowercase_ ) self.assertEqual(lowercase_ , batch_tokens[0] ) self.assertEqual(lowercase_ , ['k s ɾ | ɾ l | ɭʲ', '| j ð | s j ð s oːɹ'] ) def UpperCAmelCase__ ( self :int ) -> int: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) UpperCAmelCase = tokenizer.decode(tokenizer(lowercase_ ).input_ids , filter_word_delimiter_token=lowercase_ ) self.assertEqual(lowercase_ , lowercase_ ) def UpperCAmelCase__ ( self :Union[str, Any] ) -> Optional[Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) UpperCAmelCase = tokenizer.decode(tokenizer(lowercase_ ).input_ids , filter_word_delimiter_token=lowercase_ ) self.assertEqual(' '.join([p.strip() for p in phonemes.split(' |' )] ).strip() , lowercase_ ) def UpperCAmelCase__ ( self :int ) -> Optional[Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token=lowercase_ ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer(lowercase_ , phonemizer_lang='en-us' ).input_ids UpperCAmelCase = tokenizer(lowercase_ , phonemizer_lang='fr-fr' ).input_ids self.assertNotEqual(lowercase_ , lowercase_ ) UpperCAmelCase = tokenizer.decode(lowercase_ ) UpperCAmelCase = tokenizer.decode(lowercase_ ) self.assertEqual(lowercase_ , 'h ə l oʊ h aʊ ɑːɹ j uː' ) self.assertEqual(lowercase_ , 'ɛ l o h aʊ a ʁ j u' ) def UpperCAmelCase__ ( self :int ) -> List[Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = 'Hello how Are you' UpperCAmelCase = 'hello how are you' UpperCAmelCase = tokenizer(lowercase_ ).input_ids UpperCAmelCase = tokenizer(lowercase_ ).input_ids self.assertEqual(lowercase_ , lowercase_ ) def UpperCAmelCase__ ( self :Optional[Any] ) -> int: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) tokenizer.add_tokens(['!', '?'] ) tokenizer.add_special_tokens({'cls_token': '$$$'} ) # fmt: off UpperCAmelCase = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 3_92, 3_92, 3_93, 3_92, 3_92, 3_93, 3_94, 3_94], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 3_94, 3_94], ] # fmt: on UpperCAmelCase = tokenizer.batch_decode(lowercase_ ) self.assertEqual(lowercase_ , ['k s ɾ ɾ l ɭʲ!?!? $$$', 'j ð s j ð s oːɹ $$$'] ) @staticmethod def UpperCAmelCase__ ( lowercase_ :List[str] , lowercase_ :List[str] ) -> List[str]: UpperCAmelCase = [d[key] for d in offsets] return retrieved_list def UpperCAmelCase__ ( self :str ) -> Optional[int]: UpperCAmelCase = self.get_tokenizer(word_delimiter_token='|' ) tokenizer.add_tokens('|' ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" UpperCAmelCase = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on UpperCAmelCase = tokenizer.decode(lowercase_ , output_char_offsets=lowercase_ , filter_word_delimiter_token=lowercase_ ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue('text' in outputs ) self.assertTrue('char_offsets' in outputs ) self.assertTrue(isinstance(lowercase_ , lowercase_ ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(' '.join(self.get_from_offsets(outputs['char_offsets'] , 'char' ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs['char_offsets'] , 'char' ) , ['k', 's', 'ɾ', 'ɾ', '|', 'ɾ', 'l', '|', 'ɭʲ'] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs['char_offsets'] , 'start_offset' ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs['char_offsets'] , 'end_offset' ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def UpperCAmelCase__ ( self :Optional[int] ) -> Optional[int]: UpperCAmelCase = self.get_tokenizer(word_delimiter_token='|' ) def check_list_tuples_equal(lowercase_ :List[Any] , lowercase_ :str ): self.assertTrue(isinstance(lowercase_ , lowercase_ ) ) self.assertTrue(isinstance(outputs_list[0] , lowercase_ ) ) # transform list to ModelOutput UpperCAmelCase = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch['text'] , outputs_batch_a['text'] ) def recursive_check(lowercase_ :Any , lowercase_ :str ): if isinstance(lowercase_ , lowercase_ ): [recursive_check(lowercase_ , lowercase_ ) for la, la in zip(lowercase_ , lowercase_ )] self.assertEqual(lowercase_ , lowercase_ ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch['char_offsets'] , outputs_batch_a['char_offsets'] ) # fmt: off UpperCAmelCase = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char UpperCAmelCase = tokenizer.batch_decode(lowercase_ , output_char_offsets=lowercase_ ) UpperCAmelCase = [tokenizer.decode(lowercase_ , output_char_offsets=lowercase_ ) for ids in sample_ids] check_list_tuples_equal(lowercase_ , lowercase_ ) @unittest.skip('Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes' ) def UpperCAmelCase__ ( self :Any ) -> str: pass @unittest.skip('Wav2Vec2PhonemeTokenizer always puts spaces between phonemes' ) def UpperCAmelCase__ ( self :str ) -> List[str]: pass @unittest.skip('encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency' ) def UpperCAmelCase__ ( self :List[str] ) -> int: pass @unittest.skip('Wav2Vec2PhonemeModel has no max model length => no testing' ) def UpperCAmelCase__ ( self :List[Any] ) -> Optional[int]: pass def UpperCAmelCase__ ( self :int ) -> Optional[Any]: UpperCAmelCase = self.get_tokenizers(do_lower_case=lowercase_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): UpperCAmelCase = tokenizer.vocab_size UpperCAmelCase = len(lowercase_ ) self.assertNotEqual(lowercase_ , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) UpperCAmelCase = ['aaaaa bbbbbb', 'cccccccccdddddddd'] UpperCAmelCase = tokenizer.add_tokens(lowercase_ ) UpperCAmelCase = tokenizer.vocab_size UpperCAmelCase = len(lowercase_ ) self.assertNotEqual(lowercase_ , 0 ) self.assertEqual(lowercase_ , lowercase_ ) self.assertEqual(lowercase_ , len(lowercase_ ) ) self.assertEqual(lowercase_ , all_size + len(lowercase_ ) ) UpperCAmelCase = tokenizer.encode('aaaaa bbbbbb low cccccccccdddddddd l' , add_special_tokens=lowercase_ ) self.assertGreaterEqual(len(lowercase_ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) UpperCAmelCase = {'eos_token': '>>>>|||<||<<|<<', 'pad_token': '<<<<<|||>|>>>>|>'} UpperCAmelCase = tokenizer.add_special_tokens(lowercase_ ) UpperCAmelCase = tokenizer.vocab_size UpperCAmelCase = len(lowercase_ ) self.assertNotEqual(lowercase_ , 0 ) self.assertEqual(lowercase_ , lowercase_ ) self.assertEqual(lowercase_ , len(lowercase_ ) ) self.assertEqual(lowercase_ , all_size_a + len(lowercase_ ) ) UpperCAmelCase = tokenizer.encode( '>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l' , add_special_tokens=lowercase_ ) self.assertGreaterEqual(len(lowercase_ ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip('The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.' ) def UpperCAmelCase__ ( self :Tuple ) -> Optional[Any]: pass @unittest.skip('The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.' ) def UpperCAmelCase__ ( self :int ) -> Any: pass def UpperCAmelCase__ ( self :Tuple ) -> Dict: # The default common tokenizer tests assumes that the output of `convert_tokens_to_string` is a string which # is not the case for Wav2Vec2PhonemeCTCTokenizer. UpperCAmelCase = self.get_tokenizers(fast=lowercase_ , do_lower_case=lowercase_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): UpperCAmelCase = ['ð', 'ɪ', 's', 'ɪ', 'z', 'ɐ', 't', 'ɛ', 'k', 's', 't'] UpperCAmelCase = tokenizer.convert_tokens_to_string(lowercase_ ) self.assertIsInstance(output['text'] , lowercase_ )
181
1
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_convbert import ConvBertTokenizer __UpperCamelCase = logging.get_logger(__name__) __UpperCamelCase = {'''vocab_file''': '''vocab.txt'''} __UpperCamelCase = { '''vocab_file''': { '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt''', } } __UpperCamelCase = { '''YituTech/conv-bert-base''': 512, '''YituTech/conv-bert-medium-small''': 512, '''YituTech/conv-bert-small''': 512, } __UpperCamelCase = { '''YituTech/conv-bert-base''': {'''do_lower_case''': True}, '''YituTech/conv-bert-medium-small''': {'''do_lower_case''': True}, '''YituTech/conv-bert-small''': {'''do_lower_case''': True}, } class UpperCamelCase ( lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE_ = PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE_ = ConvBertTokenizer def __init__( self, lowerCAmelCase__=None, lowerCAmelCase__=None, lowerCAmelCase__=True, lowerCAmelCase__="[UNK]", lowerCAmelCase__="[SEP]", lowerCAmelCase__="[PAD]", lowerCAmelCase__="[CLS]", lowerCAmelCase__="[MASK]", lowerCAmelCase__=True, lowerCAmelCase__=None, **lowerCAmelCase__, ) -> Dict: super().__init__( lowerCAmelCase__, tokenizer_file=lowerCAmelCase__, do_lower_case=lowerCAmelCase__, unk_token=lowerCAmelCase__, sep_token=lowerCAmelCase__, pad_token=lowerCAmelCase__, cls_token=lowerCAmelCase__, mask_token=lowerCAmelCase__, tokenize_chinese_chars=lowerCAmelCase__, strip_accents=lowerCAmelCase__, **lowerCAmelCase__, ) snake_case_ = json.loads(self.backend_tokenizer.normalizer.__getstate__()) if ( normalizer_state.get('lowercase', lowerCAmelCase__) != do_lower_case or normalizer_state.get('strip_accents', lowerCAmelCase__) != strip_accents or normalizer_state.get('handle_chinese_chars', lowerCAmelCase__) != tokenize_chinese_chars ): snake_case_ = getattr(lowerCAmelCase__, normalizer_state.pop('type')) snake_case_ = do_lower_case snake_case_ = strip_accents snake_case_ = tokenize_chinese_chars snake_case_ = normalizer_class(**lowerCAmelCase__) snake_case_ = do_lower_case def a_ ( self, lowerCAmelCase__, lowerCAmelCase__=None) -> Optional[int]: snake_case_ = [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 a_ ( self, lowerCAmelCase__, lowerCAmelCase__ = None) -> List[int]: snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] def a_ ( self, lowerCAmelCase__, lowerCAmelCase__ = None) -> Tuple[str]: snake_case_ = self._tokenizer.model.save(lowerCAmelCase__, name=lowerCAmelCase__) return tuple(lowerCAmelCase__)
69
"""simple docstring""" def UpperCAmelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> bool: # 1. Validate that path exists between current and next vertices if graph[path[curr_ind - 1]][next_ver] == 0: return False # 2. Validate that next vertex is not already in path return not any(vertex == next_ver for vertex in path ) def UpperCAmelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> bool: # Base Case if curr_ind == len(UpperCAmelCase ): # return whether path exists between current and starting vertices return graph[path[curr_ind - 1]][path[0]] == 1 # Recursive Step for next_ver in range(0 , len(UpperCAmelCase ) ): if valid_connection(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): # Insert current vertex into path as next transition snake_case_ = next_ver # Validate created path if util_hamilton_cycle(UpperCAmelCase , UpperCAmelCase , curr_ind + 1 ): return True # Backtrack snake_case_ = -1 return False def UpperCAmelCase ( UpperCAmelCase , UpperCAmelCase = 0 ) -> list[int]: snake_case_ = [-1] * (len(UpperCAmelCase ) + 1) # initialize start and end of path with starting index snake_case_ = snake_case_ = start_index # evaluate and if we find answer return path either return empty array return path if util_hamilton_cycle(UpperCAmelCase , UpperCAmelCase , 1 ) else []
69
1
'''simple docstring''' def UpperCamelCase ( ): A__ = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] A__ = 6 A__ = 1 A__ = 19_01 A__ = 0 while year < 20_01: day += 7 if (year % 4 == 0 and year % 1_00 != 0) or (year % 4_00 == 0): if day > days_per_month[month - 1] and month != 2: month += 1 A__ = day - days_per_month[month - 2] elif day > 29 and month == 2: month += 1 A__ = day - 29 else: if day > days_per_month[month - 1]: month += 1 A__ = day - days_per_month[month - 2] if month > 12: year += 1 A__ = 1 if year < 20_01 and day == 1: sundays += 1 return sundays if __name__ == "__main__": print(solution())
360
'''simple docstring''' def UpperCamelCase ( _lowerCamelCase : List[Any] , _lowerCamelCase : List[str] , _lowerCamelCase : List[str] , _lowerCamelCase : Tuple ): # Return True if there is node that has not iterated. A__ = [False] * len(_lowerCamelCase ) A__ = [] queue.append(_lowerCamelCase ) A__ = True while queue: A__ = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(_lowerCamelCase ) A__ = True A__ = u return visited[t] def UpperCamelCase ( _lowerCamelCase : Optional[Any] , _lowerCamelCase : Optional[Any] , _lowerCamelCase : Optional[int] ): # This array is filled by BFS and to store path A__ = [-1] * (len(_lowerCamelCase )) A__ = 0 while bfs(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): A__ = float("Inf" ) A__ = sink while s != source: # Find the minimum value in select path A__ = min(_lowerCamelCase , graph[parent[s]][s] ) A__ = parent[s] max_flow += path_flow A__ = sink while v != source: A__ = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow A__ = parent[v] return max_flow __lowerCAmelCase : Any =[ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] __lowerCAmelCase , __lowerCAmelCase : int =0, 5 print(ford_fulkerson(graph, source, sink))
123
0
'''simple docstring''' import os import re from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { 'vocab_file': 'vocab.txt', 'merges_file': 'bpe.codes', } _snake_case = { 'vocab_file': { 'vinai/phobert-base': 'https://huggingface.co/vinai/phobert-base/resolve/main/vocab.txt', 'vinai/phobert-large': 'https://huggingface.co/vinai/phobert-large/resolve/main/vocab.txt', }, 'merges_file': { 'vinai/phobert-base': 'https://huggingface.co/vinai/phobert-base/resolve/main/bpe.codes', 'vinai/phobert-large': 'https://huggingface.co/vinai/phobert-large/resolve/main/bpe.codes', }, } _snake_case = { 'vinai/phobert-base': 256, 'vinai/phobert-large': 256, } def _A ( snake_case ) -> int: _lowercase : List[str] = set() _lowercase : int = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _lowercase : Optional[Any] = char _lowercase : int = set(__UpperCAmelCase ) return pairs class a__ ( _UpperCAmelCase ): _SCREAMING_SNAKE_CASE : Any = VOCAB_FILES_NAMES _SCREAMING_SNAKE_CASE : Optional[int] = PRETRAINED_VOCAB_FILES_MAP _SCREAMING_SNAKE_CASE : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase="<s>" , _UpperCamelCase="</s>" , _UpperCamelCase="</s>" , _UpperCamelCase="<s>" , _UpperCamelCase="<unk>" , _UpperCamelCase="<pad>" , _UpperCamelCase="<mask>" , **_UpperCamelCase , ): """simple docstring""" super().__init__( bos_token=_UpperCAmelCase , eos_token=_UpperCAmelCase , unk_token=_UpperCAmelCase , sep_token=_UpperCAmelCase , cls_token=_UpperCAmelCase , pad_token=_UpperCAmelCase , mask_token=_UpperCAmelCase , **_UpperCAmelCase , ) _lowercase : Any = vocab_file _lowercase : str = merges_file _lowercase : str = {} _lowercase : int = 0 _lowercase : Any = 1 _lowercase : Optional[int] = 2 _lowercase : Optional[int] = 3 self.add_from_file(_UpperCAmelCase ) _lowercase : Optional[int] = {v: k for k, v in self.encoder.items()} with open(_UpperCAmelCase , encoding="utf-8" ) as merges_handle: _lowercase : Any = merges_handle.read().split("\n" )[:-1] _lowercase : List[str] = [tuple(merge.split()[:-1] ) for merge in merges] _lowercase : Optional[int] = dict(zip(_UpperCAmelCase , range(len(_UpperCAmelCase ) ) ) ) _lowercase : Tuple = {} def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _lowercase : Dict = [self.cls_token_id] _lowercase : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase = None , _UpperCamelCase = False ): """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 _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase = None ): """simple docstring""" _lowercase : Any = [self.sep_token_id] _lowercase : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def _lowerCamelCase ( self ): """simple docstring""" return len(self.encoder ) def _lowerCamelCase ( self ): """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def _lowerCamelCase ( self , _UpperCamelCase ): """simple docstring""" if token in self.cache: return self.cache[token] _lowercase : Any = tuple(_UpperCAmelCase ) _lowercase : Union[str, Any] = tuple(list(word[:-1] ) + [word[-1] + "</w>"] ) _lowercase : int = get_pairs(_UpperCAmelCase ) if not pairs: return token while True: _lowercase : List[Any] = min(_UpperCAmelCase , key=lambda _UpperCamelCase : self.bpe_ranks.get(_UpperCAmelCase , float("inf" ) ) ) if bigram not in self.bpe_ranks: break _lowercase : Optional[int] = bigram _lowercase : str = [] _lowercase : Optional[Any] = 0 while i < len(_UpperCAmelCase ): try: _lowercase : List[str] = word.index(_UpperCAmelCase , _UpperCAmelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _lowercase : Any = j if word[i] == first and i < len(_UpperCAmelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _lowercase : Union[str, Any] = tuple(_UpperCAmelCase ) _lowercase : str = new_word if len(_UpperCAmelCase ) == 1: break else: _lowercase : List[Any] = get_pairs(_UpperCAmelCase ) _lowercase : str = '''@@ '''.join(_UpperCAmelCase ) _lowercase : Union[str, Any] = word[:-4] _lowercase : Union[str, Any] = word return word def _lowerCamelCase ( self , _UpperCamelCase ): """simple docstring""" _lowercase : Union[str, Any] = [] _lowercase : Any = re.findall(R"\S+\n?" , _UpperCAmelCase ) for token in words: split_tokens.extend(list(self.bpe(_UpperCAmelCase ).split(" " ) ) ) return split_tokens def _lowerCamelCase ( self , _UpperCamelCase ): """simple docstring""" return self.encoder.get(_UpperCAmelCase , self.encoder.get(self.unk_token ) ) def _lowerCamelCase ( self , _UpperCamelCase ): """simple docstring""" return self.decoder.get(_UpperCAmelCase , self.unk_token ) def _lowerCamelCase ( self , _UpperCamelCase ): """simple docstring""" _lowercase : Optional[int] = ''' '''.join(_UpperCAmelCase ).replace("@@ " , "" ).strip() return out_string def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase = None ): """simple docstring""" if not os.path.isdir(_UpperCAmelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return _lowercase : List[Any] = os.path.join( _UpperCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) _lowercase : Tuple = os.path.join( _UpperCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCAmelCase ): copyfile(self.vocab_file , _UpperCAmelCase ) if os.path.abspath(self.merges_file ) != os.path.abspath(_UpperCAmelCase ): copyfile(self.merges_file , _UpperCAmelCase ) return out_vocab_file, out_merge_file def _lowerCamelCase ( self , _UpperCamelCase ): """simple docstring""" if isinstance(_UpperCAmelCase , _UpperCAmelCase ): try: with open(_UpperCAmelCase , "r" , encoding="utf-8" ) as fd: self.add_from_file(_UpperCAmelCase ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception(f'''Incorrect encoding detected in {f}, please rebuild the dataset''' ) return _lowercase : Optional[int] = f.readlines() for lineTmp in lines: _lowercase : int = lineTmp.strip() _lowercase : Any = line.rfind(" " ) if idx == -1: raise ValueError("Incorrect dictionary format, expected \'<token> <cnt>\'" ) _lowercase : Tuple = line[:idx] _lowercase : int = len(self.encoder )
250
"""simple docstring""" def SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ) -> int: if n == 1 or not isinstance(__UpperCAmelCase , __UpperCAmelCase ): return 0 elif n == 2: return 1 else: lowercase__: List[Any] = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ) -> int: lowercase__: Union[str, Any] = 0 lowercase__: List[Any] = 2 while digits < n: index += 1 lowercase__: Dict = len(str(fibonacci(__UpperCAmelCase ) ) ) return index def SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase = 1_0_0_0 ) -> int: return fibonacci_digits_index(__UpperCAmelCase ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
177
0
def snake_case__ ( SCREAMING_SNAKE_CASE_ : int ): '''simple docstring''' lowercase__ : Dict = current_set.copy() for row_index, row in enumerate(__UpperCAmelCase ): lowercase__ : Dict = row[0] for column_index, column in enumerate(__UpperCAmelCase ): if magnitude == 0: lowercase__ : Optional[Any] = column continue lowercase__ : Optional[Any] = column / magnitude # Subtract to cancel term lowercase__ : Union[str, Any] = current_set[0] lowercase__ : str = [first_row] lowercase__ : List[str] = current_set[1::] for row in current_set: lowercase__ : Dict = [] # If first term is 0, it is already in form we want, so we preserve it if row[0] == 0: final_set.append(__UpperCAmelCase ) continue for column_index in range(len(__UpperCAmelCase ) ): temp_row.append(first_row[column_index] - row[column_index] ) final_set.append(__UpperCAmelCase ) # Create next recursion iteration set if len(final_set[0] ) != 3: lowercase__ : str = final_set[0] lowercase__ : Tuple = [] lowercase__ : Any = [] for row in final_set[1::]: current_first_column.append(row[0] ) next_iteration.append(row[1::] ) lowercase__ : Tuple = simplify(__UpperCAmelCase ) for i in range(len(__UpperCAmelCase ) ): resultant[i].insert(0 , current_first_column[i] ) resultant.insert(0 , __UpperCAmelCase ) lowercase__ : Optional[Any] = resultant return final_set def snake_case__ ( SCREAMING_SNAKE_CASE_ : Dict ): '''simple docstring''' if len(__UpperCAmelCase ) == 0: raise IndexError('solve_simultaneous() requires n lists of length n+1' ) lowercase__ : Any = len(__UpperCAmelCase ) + 1 if any(len(__UpperCAmelCase ) != _length for item in equations ): raise IndexError('solve_simultaneous() requires n lists of length n+1' ) for row in equations: if any(not isinstance(__UpperCAmelCase , (int, float) ) for column in row ): raise ValueError('solve_simultaneous() requires lists of integers' ) if len(__UpperCAmelCase ) == 1: return [equations[0][-1] / equations[0][0]] lowercase__ : Optional[int] = equations.copy() if any(0 in row for row in data_set ): lowercase__ : Any = data_set.copy() lowercase__ : List[Any] = [] for row_index, row in enumerate(__UpperCAmelCase ): if 0 not in row: lowercase__ : Tuple = data_set.pop(__UpperCAmelCase ) break if not full_row: raise ValueError('solve_simultaneous() requires at least 1 full equation' ) data_set.insert(0 , __UpperCAmelCase ) lowercase__ : List[Any] = data_set.copy() lowercase__ : List[Any] = simplify(__UpperCAmelCase ) lowercase__ : Tuple = simplified[::-1] lowercase__ : Tuple = [] for row in simplified: lowercase__ : Dict = row[-1] if not solutions: if row[-2] == 0: solutions.append(0 ) continue solutions.append(current_solution / row[-2] ) continue lowercase__ : Optional[Any] = row.copy()[: len(__UpperCAmelCase ) - 1 :] while temp_row[0] == 0: temp_row.pop(0 ) if len(__UpperCAmelCase ) == 0: solutions.append(0 ) continue lowercase__ : Optional[Any] = temp_row[1::] lowercase__ : Optional[int] = temp_row[::-1] for column_index, column in enumerate(__UpperCAmelCase ): current_solution -= column * solutions[column_index] solutions.append(__UpperCAmelCase ) lowercase__ : str = [] for item in solutions: final.append(float(round(__UpperCAmelCase , 5 ) ) ) return final[::-1] if __name__ == "__main__": import doctest doctest.testmod() snake_case_ = [ [2, 1, 1, 1, 1, 4], [1, 2, 1, 1, 1, 5], [1, 1, 2, 1, 1, 6], [1, 1, 1, 2, 1, 7], [1, 1, 1, 1, 2, 8], ] print(solve_simultaneous(eq)) print(solve_simultaneous([[4, 2]]))
356
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_gpta import GPTaTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation snake_case_ = logging.get_logger(__name__) snake_case_ = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} snake_case_ = { '''vocab_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/vocab.json''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/vocab.json''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/vocab.json''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/vocab.json''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/vocab.json''', }, '''merges_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/merges.txt''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/merges.txt''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/merges.txt''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/merges.txt''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/tokenizer.json''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/tokenizer.json''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/tokenizer.json''', }, } snake_case_ = { '''gpt2''': 1_024, '''gpt2-medium''': 1_024, '''gpt2-large''': 1_024, '''gpt2-xl''': 1_024, '''distilgpt2''': 1_024, } class SCREAMING_SNAKE_CASE__ (__snake_case ): __lowerCamelCase : List[str] = VOCAB_FILES_NAMES __lowerCamelCase : Any = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : int = ["""input_ids""", """attention_mask"""] __lowerCamelCase : str = GPTaTokenizer def __init__( self , a=None , a=None , a=None , a="<|endoftext|>" , a="<|endoftext|>" , a="<|endoftext|>" , a=False , **a , ): super().__init__( a , a , tokenizer_file=a , unk_token=a , bos_token=a , eos_token=a , add_prefix_space=a , **a , ) lowercase__ : int = kwargs.pop('add_bos_token' , a) lowercase__ : List[str] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()) if pre_tok_state.get('add_prefix_space' , a) != add_prefix_space: lowercase__ : Optional[Any] = getattr(a , pre_tok_state.pop('type')) lowercase__ : List[Any] = add_prefix_space lowercase__ : str = pre_tok_class(**a) lowercase__ : Tuple = add_prefix_space def snake_case_ ( self , *a , **a): lowercase__ : Tuple = kwargs.get('is_split_into_words' , a) assert self.add_prefix_space or not is_split_into_words, ( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*a , **a) def snake_case_ ( self , *a , **a): lowercase__ : Optional[Any] = kwargs.get('is_split_into_words' , a) assert self.add_prefix_space or not is_split_into_words, ( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._encode_plus(*a , **a) def snake_case_ ( self , a , a = None): lowercase__ : Any = self._tokenizer.model.save(a , name=a) return tuple(a) def snake_case_ ( self , a): lowercase__ : Union[str, Any] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(a , add_special_tokens=a) + [self.eos_token_id]) if len(a) > self.model_max_length: lowercase__ : Union[str, Any] = input_ids[-self.model_max_length :] return input_ids
216
0
from __future__ import annotations def lowerCamelCase__ ( __lowerCAmelCase : tuple[int, int] , __lowerCAmelCase : int ): """simple docstring""" lowerCAmelCase_ , lowerCAmelCase_ = position lowerCAmelCase_ = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] lowerCAmelCase_ = [] for position in positions: lowerCAmelCase_ , lowerCAmelCase_ = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__lowerCAmelCase ) return permissible_positions def lowerCamelCase__ ( __lowerCAmelCase : list[list[int]] ): """simple docstring""" return not any(elem == 0 for row in board for elem in row ) def lowerCamelCase__ ( __lowerCAmelCase : list[list[int]] , __lowerCAmelCase : tuple[int, int] , __lowerCAmelCase : int ): """simple docstring""" if is_complete(__lowerCAmelCase ): return True for position in get_valid_pos(__lowerCAmelCase , len(__lowerCAmelCase ) ): lowerCAmelCase_ , lowerCAmelCase_ = position if board[y][x] == 0: lowerCAmelCase_ = curr + 1 if open_knight_tour_helper(__lowerCAmelCase , __lowerCAmelCase , curr + 1 ): return True lowerCAmelCase_ = 0 return False def lowerCamelCase__ ( __lowerCAmelCase : int ): """simple docstring""" lowerCAmelCase_ = [[0 for i in range(__lowerCAmelCase )] for j in range(__lowerCAmelCase )] for i in range(__lowerCAmelCase ): for j in range(__lowerCAmelCase ): lowerCAmelCase_ = 1 if open_knight_tour_helper(__lowerCAmelCase , (i, j) , 1 ): return board lowerCAmelCase_ = 0 lowerCAmelCase_ = F"""Open Kight Tour cannot be performed on a board of size {n}""" raise ValueError(__lowerCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod()
231
import os import re import shutil import sys import tempfile import unittest import black _A = 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. _A = " \"\"\"\n Output class for the scheduler's step function output.\n\n Args:\n prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the\n denoising loop.\n pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n The predicted denoised sample (x_{0}) based on the model output from the current timestep.\n `pred_original_sample` can be used to preview progress or for guidance.\n \"\"\"\n\n prev_sample: torch.FloatTensor\n pred_original_sample: Optional[torch.FloatTensor] = None\n" class _lowerCAmelCase ( unittest.TestCase ): def __a ( self ) -> str: lowerCAmelCase_ = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , "schedulers/" ) ) lowerCAmelCase_ = self.diffusers_dir shutil.copy( os.path.join(_UpperCamelCase , "src/diffusers/schedulers/scheduling_ddpm.py" ) , os.path.join(self.diffusers_dir , "schedulers/scheduling_ddpm.py" ) , ) def __a ( self ) -> Optional[int]: lowerCAmelCase_ = "src/diffusers" shutil.rmtree(self.diffusers_dir ) def __a ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase=None ) -> Optional[Any]: lowerCAmelCase_ = comment + f"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: lowerCAmelCase_ = comment + f"""\nclass {class_name}(nn.Module):\n""" + overwrite_result lowerCAmelCase_ = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase_ = black.format_str(_UpperCamelCase , mode=_UpperCamelCase ) lowerCAmelCase_ = os.path.join(self.diffusers_dir , "new_code.py" ) with open(_UpperCamelCase , "w" , newline="\n" ) as f: f.write(_UpperCamelCase ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_UpperCamelCase ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_UpperCamelCase ) with open(_UpperCamelCase , "r" ) as f: self.assertTrue(f.read() , _UpperCamelCase ) def __a ( self ) -> Optional[Any]: lowerCAmelCase_ = check_copies.find_code_in_diffusers("schedulers.scheduling_ddpm.DDPMSchedulerOutput" ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def __a ( self ) -> Tuple: # Base copy consistency 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" , _UpperCamelCase , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , re.sub("DDPM" , "Test" , _UpperCamelCase ) , ) # Copy consistency with a really long name lowerCAmelCase_ = "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" , _UpperCamelCase , _UpperCamelCase ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , _UpperCamelCase , overwrite_result=re.sub("DDPM" , "Test" , _UpperCamelCase ) , )
231
1
"""simple docstring""" import argparse import torch from transformers import ( SpeechTaConfig, SpeechTaFeatureExtractor, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaProcessor, SpeechTaTokenizer, logging, ) from transformers.tokenization_utils import AddedToken logging.set_verbosity_info() A : List[str] = logging.get_logger("transformers.models.speecht5") A : Any = { 'speech_encoder_prenet.layer_norm': 'speecht5.encoder.prenet.feature_projection.layer_norm', 'speech_encoder_prenet.post_extract_proj': 'speecht5.encoder.prenet.feature_projection.projection', 'speech_encoder_prenet.pos_conv.0': 'speecht5.encoder.prenet.pos_conv_embed.conv', 'speech_encoder_prenet.mask_emb': 'speecht5.encoder.prenet.masked_spec_embed', } A : Dict = { 'text_encoder_prenet.encoder_prenet.0': 'speecht5.encoder.prenet.embed_tokens', 'text_encoder_prenet.encoder_prenet.1.alpha': 'speecht5.encoder.prenet.encode_positions.alpha', } A : int = { 'speech_decoder_prenet.decoder_prenet.0.0.prenet.0.0': 'speecht5.decoder.prenet.layers.0', 'speech_decoder_prenet.decoder_prenet.0.0.prenet.1.0': 'speecht5.decoder.prenet.layers.1', 'speech_decoder_prenet.decoder_prenet.0.1': 'speecht5.decoder.prenet.final_layer', 'speech_decoder_prenet.decoder_prenet.1.alpha': 'speecht5.decoder.prenet.encode_positions.alpha', 'speech_decoder_prenet.spkembs_layer.0': 'speecht5.decoder.prenet.speaker_embeds_layer', } A : List[str] = { 'speech_decoder_postnet.feat_out': 'speech_decoder_postnet.feat_out', 'speech_decoder_postnet.prob_out': 'speech_decoder_postnet.prob_out', 'speech_decoder_postnet.postnet.postnet.0.0': 'speech_decoder_postnet.layers.0.conv', 'speech_decoder_postnet.postnet.postnet.0.1': 'speech_decoder_postnet.layers.0.batch_norm', 'speech_decoder_postnet.postnet.postnet.1.0': 'speech_decoder_postnet.layers.1.conv', 'speech_decoder_postnet.postnet.postnet.1.1': 'speech_decoder_postnet.layers.1.batch_norm', 'speech_decoder_postnet.postnet.postnet.2.0': 'speech_decoder_postnet.layers.2.conv', 'speech_decoder_postnet.postnet.postnet.2.1': 'speech_decoder_postnet.layers.2.batch_norm', 'speech_decoder_postnet.postnet.postnet.3.0': 'speech_decoder_postnet.layers.3.conv', 'speech_decoder_postnet.postnet.postnet.3.1': 'speech_decoder_postnet.layers.3.batch_norm', 'speech_decoder_postnet.postnet.postnet.4.0': 'speech_decoder_postnet.layers.4.conv', 'speech_decoder_postnet.postnet.postnet.4.1': 'speech_decoder_postnet.layers.4.batch_norm', } A : List[str] = { 'text_decoder_prenet.embed_tokens': 'speecht5.decoder.prenet.embed_tokens', } A : Any = { 'text_decoder_postnet.output_projection': 'text_decoder_postnet.lm_head', } A : Any = { 'encoder.layers.*.self_attn.k_proj': 'speecht5.encoder.wrapped_encoder.layers.*.attention.k_proj', 'encoder.layers.*.self_attn.v_proj': 'speecht5.encoder.wrapped_encoder.layers.*.attention.v_proj', 'encoder.layers.*.self_attn.q_proj': 'speecht5.encoder.wrapped_encoder.layers.*.attention.q_proj', 'encoder.layers.*.self_attn.out_proj': 'speecht5.encoder.wrapped_encoder.layers.*.attention.out_proj', 'encoder.layers.*.self_attn_layer_norm': 'speecht5.encoder.wrapped_encoder.layers.*.layer_norm', 'encoder.layers.*.fc1': 'speecht5.encoder.wrapped_encoder.layers.*.feed_forward.intermediate_dense', 'encoder.layers.*.fc2': 'speecht5.encoder.wrapped_encoder.layers.*.feed_forward.output_dense', 'encoder.layers.*.final_layer_norm': 'speecht5.encoder.wrapped_encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'speecht5.encoder.wrapped_encoder.layer_norm', 'encoder.pos_emb.pe_k': 'speecht5.encoder.wrapped_encoder.embed_positions.pe_k', } A : Any = { 'decoder.layers.*.self_attn.k_proj': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn.k_proj', 'decoder.layers.*.self_attn.v_proj': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn.v_proj', 'decoder.layers.*.self_attn.q_proj': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn.q_proj', 'decoder.layers.*.self_attn.out_proj': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn.out_proj', 'decoder.layers.*.self_attn_layer_norm': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn_layer_norm', 'decoder.layers.*.encoder_attn.k_proj': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.k_proj', 'decoder.layers.*.encoder_attn.v_proj': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.v_proj', 'decoder.layers.*.encoder_attn.q_proj': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.q_proj', 'decoder.layers.*.encoder_attn.out_proj': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.out_proj', 'decoder.layers.*.encoder_attn_layer_norm': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn_layer_norm', 'decoder.layers.*.fc1': 'speecht5.decoder.wrapped_decoder.layers.*.feed_forward.intermediate_dense', 'decoder.layers.*.fc2': 'speecht5.decoder.wrapped_decoder.layers.*.feed_forward.output_dense', 'decoder.layers.*.final_layer_norm': 'speecht5.decoder.wrapped_decoder.layers.*.final_layer_norm', } A : Optional[Any] = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_TEXT_DECODER_PRENET, **MAPPING_TEXT_DECODER_POSTNET, } A : Any = { **MAPPING_TEXT_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } A : Union[str, Any] = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } A : Union[str, Any] = [] A : Union[str, Any] = [ 'encoder.version', 'encoder.layers.*.norm_k.weight', 'encoder.layers.*.norm_k.bias', 'decoder.version', 'decoder.layers.*.norm_k.weight', 'decoder.layers.*.norm_k.bias', 'decoder.pos_emb.pe_k', 'speech_encoder_prenet.embed_positions._float_tensor', 'text_decoder_prenet.embed_positions._float_tensor', ] A : Any = IGNORE_KEYS + [ 'encoder.proj', 'text_encoder_prenet.*', 'speech_decoder_prenet.*', 'speech_decoder_postnet.*', ] A : List[Any] = IGNORE_KEYS + [ 'encoder.proj', 'speech_encoder_prenet.*', 'text_decoder_prenet.*', 'text_decoder_postnet.*', ] A : str = IGNORE_KEYS + [ 'encoder.proj', 'text_encoder_prenet.*', 'text_decoder_prenet.*', 'text_decoder_postnet.*', ] def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' for attribute in key.split("." ): __lowerCAmelCase = getattr(lowerCamelCase_ , lowerCamelCase_ ) if weight_type is not None: __lowerCAmelCase = getattr(lowerCamelCase_ , lowerCamelCase_ ).shape else: __lowerCAmelCase = 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": __lowerCAmelCase = value elif weight_type == "weight_g": __lowerCAmelCase = value elif weight_type == "weight_v": __lowerCAmelCase = value elif weight_type == "bias": __lowerCAmelCase = value elif weight_type == "running_mean": __lowerCAmelCase = value elif weight_type == "running_var": __lowerCAmelCase = value elif weight_type == "num_batches_tracked": __lowerCAmelCase = value else: __lowerCAmelCase = value logger.info(f"{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}." ) def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' for key in ignore_keys: if key.endswith(".*" ): if name.startswith(key[:-1] ): return True elif ".*." in key: __lowerCAmelCase = key.split(".*." ) if prefix in name and suffix in name: return True elif key in name: return True return False def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = [] if task == "s2t": __lowerCAmelCase = hf_model.speechta.encoder.prenet.feature_encoder __lowerCAmelCase = MAPPING_S2T __lowerCAmelCase = IGNORE_KEYS_S2T elif task == "t2s": __lowerCAmelCase = None __lowerCAmelCase = MAPPING_T2S __lowerCAmelCase = IGNORE_KEYS_T2S elif task == "s2s": __lowerCAmelCase = hf_model.speechta.encoder.prenet.feature_encoder __lowerCAmelCase = MAPPING_S2S __lowerCAmelCase = IGNORE_KEYS_S2S else: raise ValueError(f"Unsupported task: {task}" ) for name, value in fairseq_dict.items(): if should_ignore(lowerCamelCase_ , lowerCamelCase_ ): logger.info(f"{name} was ignored" ) continue __lowerCAmelCase = False if "conv_layers" in name: load_conv_layer( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , hf_model.config.feat_extract_norm == "group" , ) __lowerCAmelCase = True else: for key, mapped_key in MAPPING.items(): # mapped_key = "speecht5." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if "*" in key: __lowerCAmelCase = key.split(".*." ) if prefix in name and suffix in name: __lowerCAmelCase = suffix # if key in name or key.split("w2v_model.")[-1] == name.split(".")[0]: if key in name: __lowerCAmelCase = True if "*" in mapped_key: __lowerCAmelCase = name.split(lowerCamelCase_ )[0].split("." )[-2] __lowerCAmelCase = mapped_key.replace("*" , lowerCamelCase_ ) if "weight_g" in name: __lowerCAmelCase = """weight_g""" elif "weight_v" in name: __lowerCAmelCase = """weight_v""" elif "bias" in name: __lowerCAmelCase = """bias""" elif "weight" in name: __lowerCAmelCase = """weight""" elif "running_mean" in name: __lowerCAmelCase = """running_mean""" elif "running_var" in name: __lowerCAmelCase = """running_var""" elif "num_batches_tracked" in name: __lowerCAmelCase = """num_batches_tracked""" else: __lowerCAmelCase = None set_recursively(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) continue if not is_used: unused_weights.append(lowerCamelCase_ ) logger.warning(f"Unused weights: {unused_weights}" ) def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = full_name.split("conv_layers." )[-1] __lowerCAmelCase = name.split("." ) __lowerCAmelCase = int(items[0] ) __lowerCAmelCase = 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." ) __lowerCAmelCase = 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." ) __lowerCAmelCase = 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." ) __lowerCAmelCase = 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." ) __lowerCAmelCase = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(lowerCamelCase_ ) @torch.no_grad() def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase=None , _UpperCamelCase=None , _UpperCamelCase=None , ): '''simple docstring''' if config_path is not None: __lowerCAmelCase = SpeechTaConfig.from_pretrained(lowerCamelCase_ ) else: __lowerCAmelCase = SpeechTaConfig() if task == "s2t": __lowerCAmelCase = config.max_text_positions __lowerCAmelCase = SpeechTaForSpeechToText(lowerCamelCase_ ) elif task == "t2s": __lowerCAmelCase = 1876 __lowerCAmelCase = 600 __lowerCAmelCase = config.max_speech_positions __lowerCAmelCase = SpeechTaForTextToSpeech(lowerCamelCase_ ) elif task == "s2s": __lowerCAmelCase = 1876 __lowerCAmelCase = config.max_speech_positions __lowerCAmelCase = SpeechTaForSpeechToSpeech(lowerCamelCase_ ) else: raise ValueError(f"Unknown task name: {task}" ) if vocab_path: __lowerCAmelCase = SpeechTaTokenizer(lowerCamelCase_ , model_max_length=config.max_text_positions ) # Mask token behaves like a normal word, i.e. include the space before it __lowerCAmelCase = AddedToken("<mask>" , lstrip=lowerCamelCase_ , rstrip=lowerCamelCase_ ) __lowerCAmelCase = mask_token tokenizer.add_special_tokens({"mask_token": mask_token} ) tokenizer.add_tokens(["<ctc_blank>"] ) __lowerCAmelCase = SpeechTaFeatureExtractor() __lowerCAmelCase = SpeechTaProcessor(tokenizer=lowerCamelCase_ , feature_extractor=lowerCamelCase_ ) processor.save_pretrained(lowerCamelCase_ ) __lowerCAmelCase = torch.load(lowerCamelCase_ ) recursively_load_weights(fairseq_checkpoint["model"] , lowerCamelCase_ , lowerCamelCase_ ) model.save_pretrained(lowerCamelCase_ ) if repo_id: print("Pushing to the hub..." ) processor.push_to_hub(lowerCamelCase_ ) model.push_to_hub(lowerCamelCase_ ) if __name__ == "__main__": A : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( "--task", default="s2t", type=str, help="Type of the SpeechT5 model you\'d like to convert. Should be one of \'s2t\', \'t2s\', \'s2s\'.", ) parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--vocab_path", default=None, type=str, help="Path to SentencePiece model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--pytorch_dump_folder_path", required=True, default=None, type=str, help="Path to the output PyTorch model." ) parser.add_argument( "--push_to_hub", default=None, type=str, help="Where to upload the converted model on the 🤗 hub." ) A : Any = parser.parse_args() convert_speechta_checkpoint( args.task, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.vocab_path, args.push_to_hub, )
358
"""simple docstring""" import string def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' for key in range(len(string.ascii_uppercase ) ): __lowerCAmelCase = "" for symbol in message: if symbol in string.ascii_uppercase: __lowerCAmelCase = string.ascii_uppercase.find(_UpperCamelCase ) __lowerCAmelCase = num - key if num < 0: __lowerCAmelCase = num + len(string.ascii_uppercase ) __lowerCAmelCase = translated + string.ascii_uppercase[num] else: __lowerCAmelCase = translated + symbol print(f"Decryption using Key #{key}: {translated}" ) def _lowerCamelCase ( ): '''simple docstring''' __lowerCAmelCase = input("Encrypted message: " ) __lowerCAmelCase = message.upper() decrypt(_UpperCamelCase ) if __name__ == "__main__": import doctest doctest.testmod() main()
259
0
'''simple docstring''' import logging import os import threading import time try: import warnings except ImportError: UpperCamelCase__ = None try: import msvcrt except ImportError: UpperCamelCase__ = None try: import fcntl except ImportError: UpperCamelCase__ = None # Backward compatibility # ------------------------------------------------ try: TimeoutError except NameError: UpperCamelCase__ = OSError # Data # ------------------------------------------------ UpperCamelCase__ = [ '''Timeout''', '''BaseFileLock''', '''WindowsFileLock''', '''UnixFileLock''', '''SoftFileLock''', '''FileLock''', ] UpperCamelCase__ = '''3.0.12''' UpperCamelCase__ = None def a__ ( ) -> str: global _logger UpperCAmelCase__ : Union[str, Any] = _logger or logging.getLogger(__name__ ) return _logger class lowerCamelCase_ ( __a ): def __init__( self : List[Any] , _A : int ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = lock_file return None def __str__( self : str ): '''simple docstring''' UpperCAmelCase__ : Any = f"""The file lock '{self.lock_file}' could not be acquired.""" return temp class lowerCamelCase_ : def __init__( self : Tuple , _A : str ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = lock return None def __enter__( self : int ): '''simple docstring''' return self.lock def __exit__( self : str , _A : List[str] , _A : List[Any] , _A : Any ): '''simple docstring''' self.lock.release() return None class lowerCamelCase_ : def __init__( self : Tuple , _A : Any , _A : Dict=-1 , _A : Optional[Any]=None ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = max_filename_length if max_filename_length is not None else 255 # Hash the filename if it's too long UpperCAmelCase__ : Union[str, Any] = self.hash_filename_if_too_long(_A , _A ) # The path to the lock file. UpperCAmelCase__ : Optional[int] = lock_file # The file descriptor for the *_lock_file* as it is returned by the # os.open() function. # This file lock is only NOT None, if the object currently holds the # lock. UpperCAmelCase__ : Tuple = None # The default timeout value. UpperCAmelCase__ : List[Any] = timeout # We use this lock primarily for the lock counter. UpperCAmelCase__ : Optional[Any] = threading.Lock() # The lock counter is used for implementing the nested locking # mechanism. Whenever the lock is acquired, the counter is increased and # the lock is only released, when this value is 0 again. UpperCAmelCase__ : str = 0 return None @property def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' return self._lock_file @property def lowercase_ ( self : str ): '''simple docstring''' return self._timeout @timeout.setter def lowercase_ ( self : str , _A : Dict ): '''simple docstring''' UpperCAmelCase__ : Tuple = float(_A ) return None def lowercase_ ( self : Optional[int] ): '''simple docstring''' raise NotImplementedError() def lowercase_ ( self : Tuple ): '''simple docstring''' raise NotImplementedError() @property def lowercase_ ( self : List[Any] ): '''simple docstring''' return self._lock_file_fd is not None def lowercase_ ( self : str , _A : List[Any]=None , _A : Optional[Any]=0.0_5 ): '''simple docstring''' if timeout is None: UpperCAmelCase__ : Union[str, Any] = self.timeout # Increment the number right at the beginning. # We can still undo it, if something fails. with self._thread_lock: self._lock_counter += 1 UpperCAmelCase__ : str = id(self ) UpperCAmelCase__ : int = self._lock_file UpperCAmelCase__ : Optional[int] = time.time() try: while True: with self._thread_lock: if not self.is_locked: logger().debug(f"""Attempting to acquire lock {lock_id} on {lock_filename}""" ) self._acquire() if self.is_locked: logger().debug(f"""Lock {lock_id} acquired on {lock_filename}""" ) break elif timeout >= 0 and time.time() - start_time > timeout: logger().debug(f"""Timeout on acquiring lock {lock_id} on {lock_filename}""" ) raise Timeout(self._lock_file ) else: logger().debug( f"""Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ...""" ) time.sleep(_A ) except: # noqa # Something did go wrong, so decrement the counter. with self._thread_lock: UpperCAmelCase__ : Optional[Any] = max(0 , self._lock_counter - 1 ) raise return _Acquire_ReturnProxy(lock=self ) def lowercase_ ( self : int , _A : Any=False ): '''simple docstring''' with self._thread_lock: if self.is_locked: self._lock_counter -= 1 if self._lock_counter == 0 or force: UpperCAmelCase__ : List[Any] = id(self ) UpperCAmelCase__ : Dict = self._lock_file logger().debug(f"""Attempting to release lock {lock_id} on {lock_filename}""" ) self._release() UpperCAmelCase__ : List[Any] = 0 logger().debug(f"""Lock {lock_id} released on {lock_filename}""" ) return None def __enter__( self : Union[str, Any] ): '''simple docstring''' self.acquire() return self def __exit__( self : Optional[int] , _A : int , _A : str , _A : Any ): '''simple docstring''' self.release() return None def __del__( self : Any ): '''simple docstring''' self.release(force=_A ) return None def lowercase_ ( self : List[str] , _A : str , _A : int ): '''simple docstring''' UpperCAmelCase__ : str = os.path.basename(_A ) if len(_A ) > max_length and max_length > 0: UpperCAmelCase__ : List[str] = os.path.dirname(_A ) UpperCAmelCase__ : Optional[Any] = str(hash(_A ) ) UpperCAmelCase__ : Dict = filename[: max_length - len(_A ) - 8] + '''...''' + hashed_filename + '''.lock''' return os.path.join(_A , _A ) else: return path class lowerCamelCase_ ( __a ): def __init__( self : Tuple , _A : Optional[int] , _A : List[str]=-1 , _A : Dict=None ): '''simple docstring''' from .file_utils import relative_to_absolute_path super().__init__(_A , timeout=_A , max_filename_length=_A ) UpperCAmelCase__ : Union[str, Any] = '''\\\\?\\''' + relative_to_absolute_path(self.lock_file ) def lowercase_ ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Dict = os.O_RDWR | os.O_CREAT | os.O_TRUNC try: UpperCAmelCase__ : Any = os.open(self._lock_file , _A ) except OSError: pass else: try: msvcrt.locking(_A , msvcrt.LK_NBLCK , 1 ) except OSError: os.close(_A ) else: UpperCAmelCase__ : str = fd return None def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self._lock_file_fd UpperCAmelCase__ : Optional[int] = None msvcrt.locking(_A , msvcrt.LK_UNLCK , 1 ) os.close(_A ) try: os.remove(self._lock_file ) # Probably another instance of the application # that acquired the file lock. except OSError: pass return None class lowerCamelCase_ ( __a ): def __init__( self : str , _A : Any , _A : Any=-1 , _A : List[str]=None ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = os.statvfs(os.path.dirname(_A ) ).f_namemax super().__init__(_A , timeout=_A , max_filename_length=_A ) def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = os.O_RDWR | os.O_CREAT | os.O_TRUNC UpperCAmelCase__ : int = os.open(self._lock_file , _A ) try: fcntl.flock(_A , fcntl.LOCK_EX | fcntl.LOCK_NB ) except OSError: os.close(_A ) else: UpperCAmelCase__ : List[str] = fd return None def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : int = self._lock_file_fd UpperCAmelCase__ : Optional[int] = None fcntl.flock(_A , fcntl.LOCK_UN ) os.close(_A ) return None class lowerCamelCase_ ( __a ): def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC try: UpperCAmelCase__ : Optional[Any] = os.open(self._lock_file , _A ) except OSError: pass else: UpperCAmelCase__ : int = fd return None def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' os.close(self._lock_file_fd ) UpperCAmelCase__ : List[str] = None try: os.remove(self._lock_file ) # The file is already deleted and that's what we want. except OSError: pass return None UpperCamelCase__ = None if msvcrt: UpperCamelCase__ = WindowsFileLock elif fcntl: UpperCamelCase__ = UnixFileLock else: UpperCamelCase__ = SoftFileLock if warnings is not None: warnings.warn('''only soft file lock is available''')
181
'''simple docstring''' import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCamelCase_ ( __a , unittest.TestCase ): lowerCAmelCase__ = LEDTokenizer lowerCAmelCase__ = LEDTokenizerFast lowerCAmelCase__ = True def lowercase_ ( self : int ): '''simple docstring''' super().setUp() UpperCAmelCase__ : List[Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] UpperCAmelCase__ : Optional[Any] = dict(zip(_A , range(len(_A ) ) ) ) UpperCAmelCase__ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCAmelCase__ : Any = {'''unk_token''': '''<unk>'''} UpperCAmelCase__ : Dict = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCAmelCase__ : Tuple = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_A ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_A ) ) def lowercase_ ( self : Optional[int] , **_A : Any ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_A ) def lowercase_ ( self : Union[str, Any] , **_A : Optional[Any] ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_A ) def lowercase_ ( self : Tuple , _A : List[str] ): '''simple docstring''' return "lower newer", "lower newer" @cached_property def lowercase_ ( self : List[Any] ): '''simple docstring''' return LEDTokenizer.from_pretrained('''allenai/led-base-16384''' ) @cached_property def lowercase_ ( self : Any ): '''simple docstring''' return LEDTokenizerFast.from_pretrained('''allenai/led-base-16384''' ) @require_torch def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] UpperCAmelCase__ : Dict = [0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ : Union[str, Any] = tokenizer(_A , max_length=len(_A ) , padding=_A , return_tensors='''pt''' ) self.assertIsInstance(_A , _A ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) UpperCAmelCase__ : int = batch.input_ids.tolist()[0] self.assertListEqual(_A , _A ) @require_torch def lowercase_ ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ : List[str] = tokenizer(_A , padding=_A , return_tensors='''pt''' ) self.assertIn('''input_ids''' , _A ) self.assertIn('''attention_mask''' , _A ) self.assertNotIn('''labels''' , _A ) self.assertNotIn('''decoder_attention_mask''' , _A ) @require_torch def lowercase_ ( self : str ): '''simple docstring''' UpperCAmelCase__ : Tuple = [ '''Summary of the text.''', '''Another summary.''', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ : Optional[Any] = tokenizer(text_target=_A , max_length=32 , padding='''max_length''' , return_tensors='''pt''' ) self.assertEqual(32 , targets['''input_ids'''].shape[1] ) @require_torch def lowercase_ ( self : Tuple ): '''simple docstring''' for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ : Any = tokenizer( ['''I am a small frog''' * 1_024, '''I am a small frog'''] , padding=_A , truncation=_A , return_tensors='''pt''' ) self.assertIsInstance(_A , _A ) self.assertEqual(batch.input_ids.shape , (2, 5_122) ) @require_torch def lowercase_ ( self : List[str] ): '''simple docstring''' UpperCAmelCase__ : Any = ['''A long paragraph for summarization.'''] UpperCAmelCase__ : List[Any] = [ '''Summary of the text.''', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ : Optional[Any] = tokenizer(_A , return_tensors='''pt''' ) UpperCAmelCase__ : int = tokenizer(text_target=_A , return_tensors='''pt''' ) UpperCAmelCase__ : str = inputs['''input_ids'''] UpperCAmelCase__ : Tuple = targets['''input_ids'''] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def lowercase_ ( self : List[str] ): '''simple docstring''' for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ : Tuple = ['''Summary of the text.''', '''Another summary.'''] UpperCAmelCase__ : List[str] = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] UpperCAmelCase__ : List[str] = tokenizer(_A , padding=_A ) UpperCAmelCase__ : str = [[0] * len(_A ) for x in encoded_output['''input_ids''']] UpperCAmelCase__ : Any = tokenizer.pad(_A ) self.assertSequenceEqual(outputs['''global_attention_mask'''] , _A ) def lowercase_ ( self : Tuple ): '''simple docstring''' pass def lowercase_ ( self : Dict ): '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): UpperCAmelCase__ : Optional[int] = self.rust_tokenizer_class.from_pretrained(_A , **_A ) UpperCAmelCase__ : int = self.tokenizer_class.from_pretrained(_A , **_A ) UpperCAmelCase__ : Any = '''A, <mask> AllenNLP sentence.''' UpperCAmelCase__ : Dict = tokenizer_r.encode_plus(_A , add_special_tokens=_A , return_token_type_ids=_A ) UpperCAmelCase__ : Optional[int] = tokenizer_p.encode_plus(_A , add_special_tokens=_A , return_token_type_ids=_A ) self.assertEqual(sum(tokens_r['''token_type_ids'''] ) , sum(tokens_p['''token_type_ids'''] ) ) self.assertEqual( sum(tokens_r['''attention_mask'''] ) / len(tokens_r['''attention_mask'''] ) , sum(tokens_p['''attention_mask'''] ) / len(tokens_p['''attention_mask'''] ) , ) UpperCAmelCase__ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) UpperCAmelCase__ : Tuple = tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids'''] ) self.assertSequenceEqual(tokens_p['''input_ids'''] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual( _A , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( _A , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] )
181
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : Optional[int] , *__a : Tuple , **__a : str ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Optional[Any] , *__a : Union[str, Any] , **__a : int ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : str , *__a : str , **__a : Any ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : List[Any] , *__a : List[Any] , **__a : List[Any] ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : int , *__a : Optional[int] , **__a : Optional[int] ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Optional[int] , *__a : Dict , **__a : Tuple ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : Optional[Any] , *__a : Tuple , **__a : Any ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : str , *__a : int , **__a : Dict ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Tuple , *__a : Optional[int] , **__a : str ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : int , *__a : str , **__a : int ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : str , *__a : Optional[int] , **__a : str ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : List[Any] , *__a : List[str] , **__a : Tuple ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : Tuple , *__a : Dict , **__a : Any ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Any , *__a : Tuple , **__a : str ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : int , *__a : Union[str, Any] , **__a : Any ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : Union[str, Any] , *__a : Dict , **__a : int ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Optional[int] , *__a : Dict , **__a : Union[str, Any] ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Optional[Any] , *__a : Tuple , **__a : Tuple ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : int , *__a : Optional[int] , **__a : Union[str, Any] ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Dict , *__a : str , **__a : Any ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : List[str] , *__a : Dict , **__a : Any ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : int , *__a : Tuple , **__a : Any ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Union[str, Any] , *__a : str , **__a : Optional[Any] ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Tuple , *__a : Optional[int] , **__a : Union[str, Any] ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : Tuple , *__a : Dict , **__a : Tuple ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Optional[int] , *__a : List[Any] , **__a : Dict ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Dict , *__a : List[str] , **__a : Any ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : Union[str, Any] , *__a : Union[str, Any] , **__a : List[str] ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Any , *__a : List[Any] , **__a : Optional[Any] ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Any , *__a : Any , **__a : Union[str, Any] ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : str , *__a : Optional[int] , **__a : Dict ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Dict , *__a : str , **__a : Dict ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : str , *__a : List[Any] , **__a : Optional[int] ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : Optional[Any] , *__a : Optional[Any] , **__a : str ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : List[Any] , *__a : Union[str, Any] , **__a : int ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : List[Any] , *__a : List[Any] , **__a : List[str] ): requires_backends(cls , ["flax"] ) class __SCREAMING_SNAKE_CASE (metaclass=lowerCamelCase_ ): """simple docstring""" __a =['flax'] def __init__( self : Optional[int] , *__a : Tuple , **__a : int ): requires_backends(self , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : Optional[int] , *__a : str , **__a : List[str] ): requires_backends(cls , ["flax"] ) @classmethod def UpperCamelCase__ ( cls : str , *__a : List[Any] , **__a : Optional[Any] ): requires_backends(cls , ["flax"] )
346
'''simple docstring''' def _lowerCamelCase ( lowercase : int = 10 ) -> str: if not isinstance(lowercase , lowercase ) or n < 0: raise ValueError("Invalid input" ) _a = 10**n _a = 2_8433 * (pow(2 , 783_0457 , lowercase )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(10) = }""")
346
1
_snake_case = range(2, 20 + 1) _snake_case = [10**k for k in range(ks[-1] + 1)] _snake_case = {} def lowercase_( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): '''simple docstring''' lowerCamelCase : Optional[int] = sum(a_i[j] for j in range(__lowerCamelCase , len(__lowerCamelCase ) ) ) lowerCamelCase : List[str] = sum(a_i[j] * base[j] for j in range(min(len(__lowerCamelCase ) , __lowerCamelCase ) ) ) lowerCamelCase : Optional[Any] = 0, 0 lowerCamelCase : str = n - i lowerCamelCase : int = memo.get(__lowerCamelCase ) if sub_memo is not None: lowerCamelCase : Any = sub_memo.get(__lowerCamelCase ) if jumps is not None and len(__lowerCamelCase ) > 0: # find and make the largest jump without going over lowerCamelCase : List[Any] = -1 for _k in range(len(__lowerCamelCase ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: lowerCamelCase : Dict = _k break if max_jump >= 0: lowerCamelCase : List[str] = jumps[max_jump] # since the difference between jumps is cached, add c lowerCamelCase : str = diff + c for j in range(min(__lowerCamelCase , len(__lowerCamelCase ) ) ): lowerCamelCase : List[str] = divmod(__lowerCamelCase , 10 ) if new_c > 0: add(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) else: lowerCamelCase : Any = [] else: lowerCamelCase : Tuple = {c: []} lowerCamelCase : Any = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps lowerCamelCase : Any = next_term(__lowerCamelCase , k - 1 , i + dn , __lowerCamelCase ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead lowerCamelCase : Union[str, Any] = compute(__lowerCamelCase , __lowerCamelCase , i + dn , __lowerCamelCase ) diff += _diff dn += terms_jumped lowerCamelCase : Union[str, Any] = sub_memo[c] # keep jumps sorted by # of terms skipped lowerCamelCase : List[Any] = 0 while j < len(__lowerCamelCase ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(__lowerCamelCase , (diff, dn, k) ) return (diff, dn) def lowercase_( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if i >= n: return 0, i if k > len(__lowerCamelCase ): a_i.extend([0 for _ in range(k - len(__lowerCamelCase ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) lowerCamelCase : Dict = i lowerCamelCase : str = 0, 0, 0 for j in range(len(__lowerCamelCase ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 lowerCamelCase : List[Any] = ds_c + ds_b diff += addend lowerCamelCase : List[str] = 0 for j in range(__lowerCamelCase ): lowerCamelCase : List[str] = a_i[j] + addend lowerCamelCase : Optional[Any] = divmod(__lowerCamelCase , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) return diff, i - start_i def lowercase_( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): '''simple docstring''' for j in range(__lowerCamelCase , len(__lowerCamelCase ) ): lowerCamelCase : Optional[Any] = digits[j] + addend if s >= 10: lowerCamelCase : Union[str, Any] = divmod(__lowerCamelCase , 10 ) lowerCamelCase : Optional[Any] = addend // 10 + quotient else: lowerCamelCase : Dict = s lowerCamelCase : List[str] = addend // 10 if addend == 0: break while addend > 0: lowerCamelCase : Dict = divmod(__lowerCamelCase , 10 ) digits.append(__lowerCamelCase ) def lowercase_( SCREAMING_SNAKE_CASE_ = 10**15 ): '''simple docstring''' lowerCamelCase : List[Any] = [1] lowerCamelCase : int = 1 lowerCamelCase : int = 0 while True: lowerCamelCase : Any = next_term(__lowerCamelCase , 20 , i + dn , __lowerCamelCase ) dn += terms_jumped if dn == n - i: break lowerCamelCase : List[str] = 0 for j in range(len(__lowerCamelCase ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(f'''{solution() = }''')
283
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 _snake_case : List[Any] = logging.get_logger(__name__) _snake_case : List[Any] = "▁" _snake_case : Union[str, Any] = {"vocab_file": "sentencepiece.bpe.model"} _snake_case : Any = { "vocab_file": { "facebook/xglm-564M": "https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model", } } _snake_case : Union[str, Any] = { "facebook/xglm-564M": 2_048, } class a (_lowerCAmelCase ): """simple docstring""" __UpperCAmelCase : Dict = VOCAB_FILES_NAMES __UpperCAmelCase : str = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : Any = ["input_ids", "attention_mask"] def __init__( self : Tuple , lowerCamelCase : Tuple , lowerCamelCase : Optional[int]="<s>" , lowerCamelCase : int="</s>" , lowerCamelCase : Dict="</s>" , lowerCamelCase : Tuple="<s>" , lowerCamelCase : List[str]="<unk>" , lowerCamelCase : str="<pad>" , lowerCamelCase : Optional[Dict[str, Any]] = None , **lowerCamelCase : int , ) -> None: __snake_case : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer __snake_case : Tuple = 7 __snake_case : Optional[int] = [F'<madeupword{i}>' for i in range(self.num_madeup_words )] __snake_case : Tuple = 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=lowerCamelCase , eos_token=lowerCamelCase , unk_token=lowerCamelCase , sep_token=lowerCamelCase , cls_token=lowerCamelCase , pad_token=lowerCamelCase , sp_model_kwargs=self.sp_model_kwargs , **lowerCamelCase , ) __snake_case : List[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(lowerCamelCase ) ) __snake_case : List[str] = 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 __snake_case : Optional[Any] = 1 # Mimic fairseq token-to-id alignment for the first 4 token __snake_case : List[Any] = {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3} __snake_case : Union[str, Any] = len(self.sp_model ) __snake_case : Union[str, Any] = {F'<madeupword{i}>': sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(lowerCamelCase ) __snake_case : Dict = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self : Dict ) -> List[Any]: __snake_case : Any = self.__dict__.copy() __snake_case : str = None __snake_case : str = self.sp_model.serialized_model_proto() return state def __setstate__( self : Tuple , lowerCamelCase : Union[str, Any] ) -> Optional[Any]: __snake_case : Optional[Any] = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): __snake_case : List[str] = {} __snake_case : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __snake_case ( self : List[str] , lowerCamelCase : List[int] , lowerCamelCase : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.sep_token_id] + token_ids_a __snake_case : int = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def __snake_case ( self : Dict , lowerCamelCase : List[int] , lowerCamelCase : Optional[List[int]] = None , lowerCamelCase : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase , token_ids_a=lowerCamelCase , already_has_special_tokens=lowerCamelCase ) if token_ids_a is None: return [1] + ([0] * len(lowerCamelCase )) return [1] + ([0] * len(lowerCamelCase )) + [1, 1] + ([0] * len(lowerCamelCase )) def __snake_case ( self : Dict , lowerCamelCase : List[int] , lowerCamelCase : Optional[List[int]] = None ) -> List[int]: __snake_case : str = [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 __snake_case ( self : Any ) -> List[str]: return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def __snake_case ( self : Union[str, Any] ) -> Tuple: __snake_case : Optional[int] = {self.convert_ids_to_tokens(lowerCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case ( self : int , lowerCamelCase : str ) -> List[str]: return self.sp_model.encode(lowerCamelCase , out_type=lowerCamelCase ) def __snake_case ( self : Any , lowerCamelCase : Any ) -> Optional[int]: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] __snake_case : Optional[Any] = self.sp_model.PieceToId(lowerCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __snake_case ( self : Any , lowerCamelCase : int ) -> List[str]: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case ( self : int , lowerCamelCase : str ) -> Tuple: __snake_case : Optional[Any] = "".join(lowerCamelCase ).replace(lowerCamelCase , " " ).strip() return out_string def __snake_case ( self : List[str] , lowerCamelCase : str , lowerCamelCase : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(lowerCamelCase ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return __snake_case : str = os.path.join( lowerCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase , "wb" ) as fi: __snake_case : Any = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase ) return (out_vocab_file,)
123
0
'''simple docstring''' import math def __a ( _UpperCamelCase: list , _UpperCamelCase: int = 0 , _UpperCamelCase: int = 0 ) -> list: """simple docstring""" _snake_case = end or len(_UpperCamelCase ) for i in range(_UpperCamelCase , _UpperCamelCase ): _snake_case = i _snake_case = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: _snake_case = array[temp_index - 1] temp_index -= 1 _snake_case = temp_index_value return array def __a ( _UpperCamelCase: list , _UpperCamelCase: int , _UpperCamelCase: int ) -> None: # Max Heap """simple docstring""" _snake_case = index _snake_case = 2 * index + 1 # Left Node _snake_case = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: _snake_case = left_index if right_index < heap_size and array[largest] < array[right_index]: _snake_case = right_index if largest != index: _snake_case , _snake_case = array[largest], array[index] heapify(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def __a ( _UpperCamelCase: list ) -> list: """simple docstring""" _snake_case = len(_UpperCamelCase ) for i in range(n // 2 , -1 , -1 ): heapify(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) for i in range(n - 1 , 0 , -1 ): _snake_case , _snake_case = array[0], array[i] heapify(_UpperCamelCase , 0 , _UpperCamelCase ) return array def __a ( _UpperCamelCase: list , _UpperCamelCase: int , _UpperCamelCase: int , _UpperCamelCase: int ) -> int: """simple docstring""" 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 __a ( _UpperCamelCase: list , _UpperCamelCase: int , _UpperCamelCase: int , _UpperCamelCase: int ) -> int: """simple docstring""" _snake_case = low _snake_case = 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 = array[j], array[i] i += 1 def __a ( _UpperCamelCase: list ) -> list: """simple docstring""" if len(_UpperCamelCase ) == 0: return array _snake_case = 2 * math.ceil(math.loga(len(_UpperCamelCase ) ) ) _snake_case = 16 return intro_sort(_UpperCamelCase , 0 , len(_UpperCamelCase ) , _UpperCamelCase , _UpperCamelCase ) def __a ( _UpperCamelCase: list , _UpperCamelCase: int , _UpperCamelCase: int , _UpperCamelCase: int , _UpperCamelCase: int ) -> list: """simple docstring""" while end - start > size_threshold: if max_depth == 0: return heap_sort(_UpperCamelCase ) max_depth -= 1 _snake_case = median_of_a(_UpperCamelCase , _UpperCamelCase , start + ((end - start) // 2) + 1 , end - 1 ) _snake_case = partition(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) intro_sort(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) _snake_case = p return insertion_sort(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase_ : Optional[int] = input('''Enter numbers separated by a comma : ''').strip() UpperCamelCase_ : List[str] = [float(item) for item in user_input.split(''',''')] print(sort(unsorted))
356
'''simple docstring''' import logging import os from dataclasses import dataclass, field from functools import partial from pathlib import Path from tempfile import TemporaryDirectory from typing import List, Optional import faiss import torch from datasets import Features, Sequence, Value, load_dataset from transformers import DPRContextEncoder, DPRContextEncoderTokenizerFast, HfArgumentParser UpperCamelCase_ : Any = logging.getLogger(__name__) torch.set_grad_enabled(False) UpperCamelCase_ : Optional[Any] = '''cuda''' if torch.cuda.is_available() else '''cpu''' def __a ( _UpperCamelCase: str , _UpperCamelCase: Union[str, Any]=100 , _UpperCamelCase: List[str]=" " ) -> List[str]: """simple docstring""" _snake_case = text.split(_UpperCamelCase ) return [character.join(text[i : i + n] ).strip() for i in range(0 , len(_UpperCamelCase ) , _UpperCamelCase )] def __a ( _UpperCamelCase: dict ) -> dict: """simple docstring""" _snake_case , _snake_case = [], [] for title, text in zip(documents["title"] , documents["text"] ): if text is not None: for passage in split_text(_UpperCamelCase ): titles.append(title if title is not None else "" ) texts.append(_UpperCamelCase ) return {"title": titles, "text": texts} def __a ( _UpperCamelCase: dict , _UpperCamelCase: DPRContextEncoder , _UpperCamelCase: DPRContextEncoderTokenizerFast ) -> dict: """simple docstring""" _snake_case = ctx_tokenizer( documents["title"] , documents["text"] , truncation=_UpperCamelCase , padding="longest" , return_tensors="pt" )["input_ids"] _snake_case = ctx_encoder(input_ids.to(device=_UpperCamelCase ) , return_dict=_UpperCamelCase ).pooler_output return {"embeddings": embeddings.detach().cpu().numpy()} def __a ( _UpperCamelCase: "RagExampleArguments" , _UpperCamelCase: "ProcessingArguments" , _UpperCamelCase: "IndexHnswArguments" , ) -> Dict: """simple docstring""" logger.info("Step 1 - Create the dataset" ) ###################################### # The dataset needed for RAG must have three columns: # - title (string): title of the document # - text (string): text of a passage of the document # - embeddings (array of dimension d): DPR representation of the passage # Let's say you have documents in tab-separated csv files with columns "title" and "text" assert os.path.isfile(rag_example_args.csv_path ), "Please provide a valid path to a csv file" # You can load a Dataset object this way _snake_case = load_dataset( "csv" , data_files=[rag_example_args.csv_path] , split="train" , delimiter="\t" , column_names=["title", "text"] ) # More info about loading csv files in the documentation: https://huggingface.co/docs/datasets/loading_datasets.html?highlight=csv#csv-files # Then split the documents into passages of 100 words _snake_case = dataset.map(_UpperCamelCase , batched=_UpperCamelCase , num_proc=processing_args.num_proc ) # And compute the embeddings _snake_case = DPRContextEncoder.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name ).to(device=_UpperCamelCase ) _snake_case = DPRContextEncoderTokenizerFast.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name ) _snake_case = Features( {"text": Value("string" ), "title": Value("string" ), "embeddings": Sequence(Value("float32" ) )} ) # optional, save as float32 instead of float64 to save space _snake_case = dataset.map( partial(_UpperCamelCase , ctx_encoder=_UpperCamelCase , ctx_tokenizer=_UpperCamelCase ) , batched=_UpperCamelCase , batch_size=processing_args.batch_size , features=_UpperCamelCase , ) # And finally save your dataset _snake_case = os.path.join(rag_example_args.output_dir , "my_knowledge_dataset" ) dataset.save_to_disk(_UpperCamelCase ) # from datasets import load_from_disk # dataset = load_from_disk(passages_path) # to reload the dataset ###################################### logger.info("Step 2 - Index the dataset" ) ###################################### # Let's use the Faiss implementation of HNSW for fast approximate nearest neighbor search _snake_case = faiss.IndexHNSWFlat(index_hnsw_args.d , index_hnsw_args.m , faiss.METRIC_INNER_PRODUCT ) dataset.add_faiss_index("embeddings" , custom_index=_UpperCamelCase ) # And save the index _snake_case = os.path.join(rag_example_args.output_dir , "my_knowledge_dataset_hnsw_index.faiss" ) dataset.get_index("embeddings" ).save(_UpperCamelCase ) # dataset.load_faiss_index("embeddings", index_path) # to reload the index @dataclass class _a : SCREAMING_SNAKE_CASE_ : str = field( default=str(Path(__lowerCAmelCase ).parent / """test_run""" / """dummy-kb""" / """my_knowledge_dataset.csv""" ) , metadata={"""help""": """Path to a tab-separated csv file with columns 'title' and 'text'"""} , ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=__lowerCAmelCase , metadata={"""help""": """Question that is passed as input to RAG. Default is 'What does Moses' rod turn into ?'."""} , ) SCREAMING_SNAKE_CASE_ : str = field( default="""facebook/rag-sequence-nq""" , metadata={"""help""": """The RAG model to use. Either 'facebook/rag-sequence-nq' or 'facebook/rag-token-nq'"""} , ) SCREAMING_SNAKE_CASE_ : str = field( default="""facebook/dpr-ctx_encoder-multiset-base""" , metadata={ """help""": ( """The DPR context encoder model to use. Either 'facebook/dpr-ctx_encoder-single-nq-base' or""" """ 'facebook/dpr-ctx_encoder-multiset-base'""" ) } , ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=str(Path(__lowerCAmelCase ).parent / """test_run""" / """dummy-kb""" ) , metadata={"""help""": """Path to a directory where the dataset passages and the index will be saved"""} , ) @dataclass class _a : SCREAMING_SNAKE_CASE_ : Optional[int] = field( default=__lowerCAmelCase , metadata={ """help""": """The number of processes to use to split the documents into passages. Default is single process.""" } , ) SCREAMING_SNAKE_CASE_ : int = field( default=16 , metadata={ """help""": """The batch size to use when computing the passages embeddings using the DPR context encoder.""" } , ) @dataclass class _a : SCREAMING_SNAKE_CASE_ : int = field( default=7_68 , metadata={"""help""": """The dimension of the embeddings to pass to the HNSW Faiss index."""} , ) SCREAMING_SNAKE_CASE_ : int = field( default=1_28 , metadata={ """help""": ( """The number of bi-directional links created for every new element during the HNSW index construction.""" ) } , ) if __name__ == "__main__": logging.basicConfig(level=logging.WARNING) logger.setLevel(logging.INFO) UpperCamelCase_ : List[str] = HfArgumentParser((RagExampleArguments, ProcessingArguments, IndexHnswArguments)) UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ : List[Any] = parser.parse_args_into_dataclasses() with TemporaryDirectory() as tmp_dir: UpperCamelCase_ : str = rag_example_args.output_dir or tmp_dir main(rag_example_args, processing_args, index_hnsw_args)
142
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={} class a__ ( UpperCAmelCase__ ): lowerCamelCase : Any ="llama" lowerCamelCase : Optional[int] =["past_key_values"] def __init__( self : Union[str, Any] , a : List[Any]=3_20_00 , a : Any=40_96 , a : Optional[Any]=1_10_08 , a : List[Any]=32 , a : Union[str, Any]=32 , a : Tuple=None , a : Any="silu" , a : Optional[Any]=20_48 , a : str=0.02 , a : Any=1e-6 , a : List[str]=True , a : int=0 , a : Union[str, Any]=1 , a : List[str]=2 , a : Tuple=1 , a : Union[str, Any]=False , a : Union[str, Any]=None , **a : str , ): """simple docstring""" __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = hidden_size __lowerCamelCase = intermediate_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads # for backward compatibility if num_key_value_heads is None: __lowerCamelCase = num_attention_heads __lowerCamelCase = num_key_value_heads __lowerCamelCase = hidden_act __lowerCamelCase = initializer_range __lowerCamelCase = rms_norm_eps __lowerCamelCase = pretraining_tp __lowerCamelCase = use_cache __lowerCamelCase = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=a , bos_token_id=a , eos_token_id=a , tie_word_embeddings=a , **a , ) def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" if self.rope_scaling is None: return if not isinstance(self.rope_scaling , a ) or len(self.rope_scaling ) != 2: raise ValueError( '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ''' f"""got {self.rope_scaling}""" ) __lowerCamelCase = self.rope_scaling.get('''type''' , a ) __lowerCamelCase = self.rope_scaling.get('''factor''' , a ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( f"""`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}""" ) if rope_scaling_factor is None or not isinstance(a , a ) or rope_scaling_factor <= 1.0: raise ValueError(f"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""" )
67
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 lowercase__ =['text', 'image', 'audio'] def __UpperCamelCase ( lowerCAmelCase__ : List[str] ): __a : Optional[int] = [] 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((5_1_2, 5_1_2) ) ) elif input_type == "audio": inputs.append(torch.ones(3_0_0_0 ) ) elif isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): inputs.append(create_inputs(lowerCAmelCase__ ) ) else: raise ValueError(f"Invalid type requested: {input_type}" ) return inputs def __UpperCamelCase ( lowerCAmelCase__ : List ): __a : List[str] = [] for output in outputs: if isinstance(lowerCAmelCase__ , (str, AgentText) ): output_types.append('''text''' ) elif isinstance(lowerCAmelCase__ , (Image.Image, AgentImage) ): output_types.append('''image''' ) elif isinstance(lowerCAmelCase__ , (torch.Tensor, AgentAudio) ): output_types.append('''audio''' ) else: raise ValueError(f"Invalid output: {output}" ) return output_types @is_tool_test class UpperCamelCase__ : def lowerCAmelCase (self : Any ): self.assertTrue(hasattr(self.tool , '''inputs''' ) ) self.assertTrue(hasattr(self.tool , '''outputs''' ) ) __a : Any = self.tool.inputs for _input in inputs: if isinstance(_input , snake_case_ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) __a : Optional[int] = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def lowerCAmelCase (self : List[Any] ): __a : Union[str, Any] = create_inputs(self.tool.inputs ) __a : List[Any] = self.tool(*snake_case_ ) # There is a single output if len(self.tool.outputs ) == 1: __a : Tuple = [outputs] self.assertListEqual(output_types(snake_case_ ) , self.tool.outputs ) def lowerCAmelCase (self : List[Any] ): 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 lowerCAmelCase (self : Any ): __a : Any = create_inputs(self.tool.inputs ) __a : Union[str, Any] = self.tool(*snake_case_ ) if not isinstance(snake_case_ , snake_case_ ): __a : Tuple = [outputs] self.assertEqual(len(snake_case_ ) , len(self.tool.outputs ) ) for output, output_type in zip(snake_case_ , self.tool.outputs ): __a : List[Any] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(snake_case_ , snake_case_ ) ) def lowerCAmelCase (self : Optional[int] ): __a : Any = create_inputs(self.tool.inputs ) __a : Dict = [] for _input, input_type in zip(snake_case_ , self.tool.inputs ): if isinstance(snake_case_ , snake_case_ ): _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 __a : Optional[Any] = self.tool(*snake_case_ ) if not isinstance(snake_case_ , snake_case_ ): __a : Dict = [outputs] self.assertEqual(len(snake_case_ ) , len(self.tool.outputs ) )
216
0
import gc import inspect import unittest import torch from parameterized import parameterized from diffusers import PriorTransformer from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin enable_full_determinism() class lowerCAmelCase ( __a , unittest.TestCase ): '''simple docstring''' _A : List[str] = PriorTransformer _A : Optional[int] = '''hidden_states''' @property def lowerCAmelCase ( self : List[Any] ) -> Any: """simple docstring""" __lowercase : Dict = 4 __lowercase : List[str] = 8 __lowercase : str = 7 __lowercase : Optional[Any] = floats_tensor((batch_size, embedding_dim) ).to(__a ) __lowercase : str = floats_tensor((batch_size, embedding_dim) ).to(__a ) __lowercase : List[Any] = floats_tensor((batch_size, num_embeddings, embedding_dim) ).to(__a ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def lowerCAmelCase ( self : List[Any] , __a : Optional[int]=0 ) -> Dict: """simple docstring""" torch.manual_seed(__a ) __lowercase : str = 4 __lowercase : str = 8 __lowercase : Dict = 7 __lowercase : Optional[int] = torch.randn((batch_size, embedding_dim) ).to(__a ) __lowercase : Optional[Any] = torch.randn((batch_size, embedding_dim) ).to(__a ) __lowercase : List[str] = torch.randn((batch_size, num_embeddings, embedding_dim) ).to(__a ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } @property def lowerCAmelCase ( self : str ) -> Optional[int]: """simple docstring""" return (4, 8) @property def lowerCAmelCase ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" return (4, 8) def lowerCAmelCase ( self : str ) -> Tuple: """simple docstring""" __lowercase : int = { """num_attention_heads""": 2, """attention_head_dim""": 4, """num_layers""": 2, """embedding_dim""": 8, """num_embeddings""": 7, """additional_embeddings""": 4, } __lowercase : List[Any] = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase ( self : List[str] ) -> Any: """simple docstring""" __lowercase , __lowercase : List[str] = PriorTransformer.from_pretrained( """hf-internal-testing/prior-dummy""" , output_loading_info=__a ) self.assertIsNotNone(__a ) self.assertEqual(len(loading_info["""missing_keys"""] ) , 0 ) model.to(__a ) __lowercase : Optional[Any] = model(**self.dummy_input )[0] assert hidden_states is not None, "Make sure output is not None" def lowerCAmelCase ( self : Any ) -> Dict: """simple docstring""" __lowercase , __lowercase : Dict = self.prepare_init_args_and_inputs_for_common() __lowercase : List[Any] = self.model_class(**__a ) __lowercase : Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowercase : List[str] = [*signature.parameters.keys()] __lowercase : List[Any] = ["""hidden_states""", """timestep"""] self.assertListEqual(arg_names[:2] , __a ) def lowerCAmelCase ( self : str ) -> Tuple: """simple docstring""" __lowercase : Optional[int] = PriorTransformer.from_pretrained("""hf-internal-testing/prior-dummy""" ) __lowercase : List[str] = model.to(__a ) if hasattr(__a , """set_default_attn_processor""" ): model.set_default_attn_processor() __lowercase : str = self.get_dummy_seed_input() with torch.no_grad(): __lowercase : int = model(**__a )[0] __lowercase : List[str] = output[0, :5].flatten().cpu() print(__a ) # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. __lowercase : Union[str, Any] = torch.tensor([-1.3436, -0.2870, 0.7538, 0.4368, -0.0239] ) self.assertTrue(torch_all_close(__a , __a , rtol=1E-2 ) ) @slow class lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase ( self : Tuple , __a : Union[str, Any]=1 , __a : str=768 , __a : Optional[int]=77 , __a : Union[str, Any]=0 ) -> Optional[Any]: """simple docstring""" torch.manual_seed(__a ) __lowercase : Union[str, Any] = batch_size __lowercase : Any = embedding_dim __lowercase : int = num_embeddings __lowercase : List[Any] = torch.randn((batch_size, embedding_dim) ).to(__a ) __lowercase : Optional[int] = torch.randn((batch_size, embedding_dim) ).to(__a ) __lowercase : str = torch.randn((batch_size, num_embeddings, embedding_dim) ).to(__a ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def lowerCAmelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @parameterized.expand( [ # fmt: off [13, [-0.5861, 0.1283, -0.0931, 0.0882, 0.4476, 0.1329, -0.0498, 0.0640]], [37, [-0.4913, 0.0110, -0.0483, 0.0541, 0.4954, -0.0170, 0.0354, 0.1651]], # fmt: on ] ) def lowerCAmelCase ( self : Union[str, Any] , __a : str , __a : str ) -> int: """simple docstring""" __lowercase : Any = PriorTransformer.from_pretrained("""kandinsky-community/kandinsky-2-1-prior""" , subfolder="""prior""" ) model.to(__a ) __lowercase : Union[str, Any] = self.get_dummy_seed_input(seed=__a ) with torch.no_grad(): __lowercase : int = model(**__a )[0] assert list(sample.shape ) == [1, 768] __lowercase : Any = sample[0, :8].flatten().cpu() print(__a ) __lowercase : Optional[int] = torch.tensor(__a ) assert torch_all_close(__a , __a , atol=1E-3 )
306
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConformerConfig, WavaVecaConformerForCTC, WavaVecaConformerForPreTraining, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() lowerCamelCase : Optional[int] = logging.get_logger(__name__) lowerCamelCase : str = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.linear_k''': '''encoder.layers.*.self_attn.linear_k''', '''self_attn.linear_v''': '''encoder.layers.*.self_attn.linear_v''', '''self_attn.linear_q''': '''encoder.layers.*.self_attn.linear_q''', '''self_attn.pos_bias_u''': '''encoder.layers.*.self_attn.pos_bias_u''', '''self_attn.pos_bias_v''': '''encoder.layers.*.self_attn.pos_bias_v''', '''self_attn.linear_out''': '''encoder.layers.*.self_attn.linear_out''', '''self_attn.linear_pos''': '''encoder.layers.*.self_attn.linear_pos''', '''self_attn.rotary_emb''': '''encoder.embed_positions''', '''self_attn_layer_norm''': '''encoder.layers.*.self_attn_layer_norm''', '''conv_module.pointwise_conv1''': '''encoder.layers.*.conv_module.pointwise_conv1''', '''conv_module.pointwise_conv2''': '''encoder.layers.*.conv_module.pointwise_conv2''', '''conv_module.depthwise_conv''': '''encoder.layers.*.conv_module.depthwise_conv''', '''conv_module.batch_norm''': '''encoder.layers.*.conv_module.batch_norm''', '''conv_module.layer_norm''': '''encoder.layers.*.conv_module.layer_norm''', '''ffn1.w_1''': '''encoder.layers.*.ffn1.intermediate_dense''', '''ffn1.w_2''': '''encoder.layers.*.ffn1.output_dense''', '''ffn1.layer_norm''': '''encoder.layers.*.ffn1_layer_norm''', '''ffn2.w_1''': '''encoder.layers.*.ffn2.intermediate_dense''', '''ffn2.w_2''': '''encoder.layers.*.ffn2.output_dense''', '''ffn2.layer_norm''': '''encoder.layers.*.ffn2_layer_norm''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } lowerCamelCase : Optional[Any] = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def snake_case_ ( lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : int , lowerCAmelCase_ : str , lowerCAmelCase_ : int ): for attribute in key.split(""".""" ): __lowercase : List[str] = getattr(lowerCAmelCase_ , lowerCAmelCase_ ) if weight_type is not None: __lowercase : Union[str, Any] = getattr(lowerCAmelCase_ , lowerCAmelCase_ ).shape else: __lowercase : 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": __lowercase : Dict = value elif weight_type == "weight_g": __lowercase : Union[str, Any] = value elif weight_type == "weight_v": __lowercase : List[Any] = value elif weight_type == "bias": __lowercase : int = value elif weight_type == "running_mean": __lowercase : List[Any] = value elif weight_type == "running_var": __lowercase : int = value elif weight_type == "num_batches_tracked": __lowercase : int = value elif weight_type == "inv_freq": __lowercase : Optional[Any] = value else: __lowercase : Any = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def snake_case_ ( lowerCAmelCase_ : Tuple , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Union[str, Any] ): __lowercase : str = [] __lowercase : Any = fairseq_model.state_dict() __lowercase : List[str] = hf_model.wavaveca_conformer.feature_extractor for name, value in fairseq_dict.items(): __lowercase : Optional[Any] = False if "conv_layers" in name: load_conv_layer( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , hf_model.config.feat_extract_norm == """group""" , ) __lowercase : List[str] = True else: for key, mapped_key in MAPPING.items(): __lowercase : Any = """wav2vec2_conformer.""" + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: __lowercase : Tuple = True if "*" in mapped_key: __lowercase : List[Any] = name.split(lowerCAmelCase_ )[0].split(""".""" )[-2] __lowercase : Any = mapped_key.replace("""*""" , lowerCAmelCase_ ) if "pos_bias_u" in name: __lowercase : Any = None elif "pos_bias_v" in name: __lowercase : Tuple = None elif "weight_g" in name: __lowercase : Union[str, Any] = """weight_g""" elif "weight_v" in name: __lowercase : Dict = """weight_v""" elif "bias" in name: __lowercase : Union[str, Any] = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj __lowercase : str = """weight""" elif "running_mean" in name: __lowercase : str = """running_mean""" elif "inv_freq" in name: __lowercase : List[Any] = """inv_freq""" elif "running_var" in name: __lowercase : Any = """running_var""" elif "num_batches_tracked" in name: __lowercase : Any = """num_batches_tracked""" else: __lowercase : Optional[int] = None set_recursively(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) continue if not is_used: unused_weights.append(lowerCAmelCase_ ) logger.warning(F"Unused weights: {unused_weights}" ) def snake_case_ ( lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Optional[Any] ): __lowercase : List[Any] = full_name.split("""conv_layers.""" )[-1] __lowercase : int = name.split(""".""" ) __lowercase : Optional[Any] = int(items[0] ) __lowercase : List[str] = 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." ) __lowercase : Union[str, Any] = 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." ) __lowercase : List[str] = 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." ) __lowercase : Union[str, Any] = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found." ) __lowercase : Dict = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(lowerCAmelCase_ ) @torch.no_grad() def snake_case_ ( lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : Any , lowerCAmelCase_ : Tuple=None , lowerCAmelCase_ : Any=None , lowerCAmelCase_ : Dict=True ): if config_path is not None: __lowercase : List[Any] = WavaVecaConformerConfig.from_pretrained(lowerCAmelCase_ , hidden_act="""swish""" ) else: __lowercase : List[Any] = WavaVecaConformerConfig() if "rope" in checkpoint_path: __lowercase : Tuple = """rotary""" if is_finetuned: if dict_path: __lowercase : Any = Dictionary.load(lowerCAmelCase_ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __lowercase : List[Any] = target_dict.pad_index __lowercase : Optional[int] = target_dict.bos_index __lowercase : List[Any] = target_dict.eos_index __lowercase : List[str] = len(target_dict.symbols ) __lowercase : Union[str, Any] = os.path.join(lowerCAmelCase_ , """vocab.json""" ) if not os.path.isdir(lowerCAmelCase_ ): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(lowerCAmelCase_ ) ) return os.makedirs(lowerCAmelCase_ , exist_ok=lowerCAmelCase_ ) __lowercase : Tuple = target_dict.indices # fairseq has the <pad> and <s> switched __lowercase : int = 0 __lowercase : Any = 1 with open(lowerCAmelCase_ , """w""" , encoding="""utf-8""" ) as vocab_handle: json.dump(lowerCAmelCase_ , lowerCAmelCase_ ) __lowercase : Dict = WavaVecaCTCTokenizer( lowerCAmelCase_ , 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=lowerCAmelCase_ , ) __lowercase : List[Any] = True if config.feat_extract_norm == """layer""" else False __lowercase : Optional[Any] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , ) __lowercase : Optional[int] = WavaVecaProcessor(feature_extractor=lowerCAmelCase_ , tokenizer=lowerCAmelCase_ ) processor.save_pretrained(lowerCAmelCase_ ) __lowercase : Union[str, Any] = WavaVecaConformerForCTC(lowerCAmelCase_ ) else: __lowercase : Optional[Any] = WavaVecaConformerForPreTraining(lowerCAmelCase_ ) if is_finetuned: __lowercase , __lowercase , __lowercase : Optional[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) else: __lowercase : List[Any] = argparse.Namespace(task="""audio_pretraining""" ) __lowercase : Optional[Any] = fairseq.tasks.setup_task(lowerCAmelCase_ ) __lowercase , __lowercase , __lowercase : List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=lowerCAmelCase_ ) __lowercase : Dict = model[0].eval() recursively_load_weights(lowerCAmelCase_ , lowerCAmelCase_ , not is_finetuned ) hf_wavavec.save_pretrained(lowerCAmelCase_ ) if __name__ == "__main__": lowerCamelCase : int = 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''' ) lowerCamelCase : Any = parser.parse_args() convert_wavaveca_conformer_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
306
1
'''simple docstring''' def lowerCAmelCase_ ( snake_case_ : float , snake_case_ : float , snake_case_ : int ) -> float: '''simple docstring''' if principal <= 0: raise Exception("Principal borrowed must be > 0" ) if rate_per_annum < 0: raise Exception("Rate of interest must be >= 0" ) if years_to_repay <= 0 or not isinstance(snake_case_ , snake_case_ ): raise Exception("Years to repay must be an integer > 0" ) # Yearly rate is divided by 12 to get monthly rate UpperCAmelCase_ = rate_per_annum / 12 # Years to repay is multiplied by 12 to get number of payments as payment is monthly UpperCAmelCase_ = years_to_repay * 12 return ( principal * rate_per_month * (1 + rate_per_month) ** number_of_payments / ((1 + rate_per_month) ** number_of_payments - 1) ) if __name__ == "__main__": import doctest doctest.testmod()
1
import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def _A ( SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str]=() , SCREAMING_SNAKE_CASE__ : List[Any]=None , SCREAMING_SNAKE_CASE__ : List[Any]="no" , SCREAMING_SNAKE_CASE__ : Dict="29500" ): UpperCamelCase :List[Any] = False UpperCamelCase :Tuple = False if any(key.startswith('''KAGGLE''' ) for key in os.environ.keys() ): UpperCamelCase :Dict = True elif "IPython" in sys.modules: UpperCamelCase :int = '''google.colab''' in str(sys.modules['''IPython'''].get_ipython() ) try: UpperCamelCase :Any = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( F'''Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.''' ) if (in_colab or in_kaggle) and (os.environ.get('''TPU_NAME''' , SCREAMING_SNAKE_CASE__ ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( '''To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside ''' '''your training function. Restart your notebook and make sure no cells initializes an ''' '''`Accelerator`.''' ) if num_processes is None: UpperCamelCase :Tuple = 8 UpperCamelCase :Optional[int] = PrepareForLaunch(SCREAMING_SNAKE_CASE__ , distributed_type='''TPU''' ) print(F'''Launching a training on {num_processes} TPU cores.''' ) xmp.spawn(SCREAMING_SNAKE_CASE__ , args=SCREAMING_SNAKE_CASE__ , nprocs=SCREAMING_SNAKE_CASE__ , start_method='''fork''' ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print('''Launching training on one GPU.''' ) else: print('''Launching training on one CPU.''' ) function(*SCREAMING_SNAKE_CASE__ ) else: if num_processes is None: raise ValueError( '''You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.''' ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( '''To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized ''' '''inside your training function. Restart your notebook and make sure no cells initializes an ''' '''`Accelerator`.''' ) if torch.cuda.is_initialized(): raise ValueError( '''To launch a multi-GPU training from your notebook, you need to avoid running any instruction ''' '''using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA ''' '''function.''' ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=SCREAMING_SNAKE_CASE__ , master_addr='''127.0.01''' , master_port=SCREAMING_SNAKE_CASE__ , mixed_precision=SCREAMING_SNAKE_CASE__ ): UpperCamelCase :Optional[Any] = PrepareForLaunch(SCREAMING_SNAKE_CASE__ , distributed_type='''MULTI_GPU''' ) print(F'''Launching training on {num_processes} GPUs.''' ) try: start_processes(SCREAMING_SNAKE_CASE__ , args=SCREAMING_SNAKE_CASE__ , nprocs=SCREAMING_SNAKE_CASE__ , start_method='''fork''' ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( '''CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. ''' '''This likely stems from an outside import causing issues once the `notebook_launcher()` is called. ''' '''Please review your imports and test them when running the `notebook_launcher()` to identify ''' '''which one is problematic.''' ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): UpperCamelCase :Any = '''1''' print('''Launching training on MPS.''' ) elif torch.cuda.is_available(): print('''Launching training on one GPU.''' ) else: print('''Launching training on CPU.''' ) function(*SCREAMING_SNAKE_CASE__ ) def _A ( SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Tuple=() , SCREAMING_SNAKE_CASE__ : int=2 ): from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=SCREAMING_SNAKE_CASE__ , master_addr='''127.0.01''' , master_port='''29500''' , accelerate_mixed_precision='''no''' , accelerate_debug_rdv_file=tmp_file.name , accelerate_use_cpu='''yes''' , ): UpperCamelCase :Optional[int] = PrepareForLaunch(SCREAMING_SNAKE_CASE__ , debug=SCREAMING_SNAKE_CASE__ ) start_processes(SCREAMING_SNAKE_CASE__ , args=SCREAMING_SNAKE_CASE__ , nprocs=SCREAMING_SNAKE_CASE__ , start_method='''fork''' )
259
0
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.esm.modeling_esmfold import EsmForProteinFolding class __lowercase : """simple docstring""" def __init__( self , A , A=1_3 , A=7 , A=False , A=True , A=False , A=False , A=1_9 , A=3_2 , A=5 , A=4 , A=3_7 , A="gelu" , A=0.1 , A=0.1 , A=5_1_2 , A=1_6 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> Optional[int]: snake_case : List[Any] = parent snake_case : Dict = batch_size snake_case : Any = seq_length snake_case : str = is_training snake_case : str = use_input_mask snake_case : Any = use_token_type_ids snake_case : Dict = use_labels snake_case : Tuple = vocab_size snake_case : Any = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : Dict = num_attention_heads snake_case : Dict = intermediate_size snake_case : str = hidden_act snake_case : List[Any] = hidden_dropout_prob snake_case : str = attention_probs_dropout_prob snake_case : List[str] = max_position_embeddings snake_case : Optional[Any] = type_vocab_size snake_case : Dict = type_sequence_label_size snake_case : Tuple = initializer_range snake_case : List[str] = num_labels snake_case : str = num_choices snake_case : Tuple = scope def UpperCAmelCase ( self ) -> Optional[Any]: snake_case : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case : Dict = None if self.use_input_mask: snake_case : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) snake_case : List[Any] = None snake_case : Tuple = None snake_case : str = None if self.use_labels: snake_case : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case : str = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case : Optional[Any] = ids_tensor([self.batch_size] , self.num_choices ) snake_case : Optional[int] = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase ( self ) -> int: snake_case : Any = EsmConfig( vocab_size=3_3 , hidden_size=self.hidden_size , pad_token_id=1 , 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 , is_folding_model=A , esmfold_config={"""trunk""": {"""num_blocks""": 2}, """fp16_esm""": False} , ) return config def UpperCAmelCase ( self , A , A , A , A , A , A ) -> List[str]: snake_case : Tuple = EsmForProteinFolding(config=A ).float() model.to(A ) model.eval() snake_case : List[str] = model(A , attention_mask=A ) snake_case : str = model(A ) snake_case : List[Any] = model(A ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 1_4, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def UpperCAmelCase ( self ) -> List[Any]: snake_case : int = self.prepare_config_and_inputs() ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : List[str] = config_and_inputs snake_case : List[Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class __lowercase (UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): """simple docstring""" _snake_case = False _snake_case = (EsmForProteinFolding,) if is_torch_available() else () _snake_case = () _snake_case = {} if is_torch_available() else {} _snake_case = False def UpperCAmelCase ( self ) -> Union[str, Any]: snake_case : List[Any] = EsmFoldModelTester(self ) snake_case : Tuple = ConfigTester(self , config_class=A , hidden_size=3_7 ) def UpperCAmelCase ( self ) -> Dict: self.config_tester.run_common_tests() def UpperCAmelCase ( self ) -> Union[str, Any]: snake_case : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @unittest.skip("""Does not support attention outputs""" ) def UpperCAmelCase ( self ) -> Optional[int]: pass @unittest.skip def UpperCAmelCase ( self ) -> Optional[int]: pass @unittest.skip("""Esm does not support embedding resizing""" ) def UpperCAmelCase ( self ) -> Dict: pass @unittest.skip("""Esm does not support embedding resizing""" ) def UpperCAmelCase ( self ) -> Optional[Any]: pass @unittest.skip("""ESMFold does not support passing input embeds!""" ) def UpperCAmelCase ( self ) -> List[Any]: pass @unittest.skip("""ESMFold does not support head pruning.""" ) def UpperCAmelCase ( self ) -> Dict: pass @unittest.skip("""ESMFold does not support head pruning.""" ) def UpperCAmelCase ( self ) -> Optional[Any]: pass @unittest.skip("""ESMFold does not support head pruning.""" ) def UpperCAmelCase ( self ) -> int: pass @unittest.skip("""ESMFold does not support head pruning.""" ) def UpperCAmelCase ( self ) -> List[str]: pass @unittest.skip("""ESMFold does not support head pruning.""" ) def UpperCAmelCase ( self ) -> Optional[Any]: pass @unittest.skip("""ESMFold does not output hidden states in the normal way.""" ) def UpperCAmelCase ( self ) -> List[Any]: pass @unittest.skip("""ESMfold does not output hidden states in the normal way.""" ) def UpperCAmelCase ( self ) -> Any: pass @unittest.skip("""ESMFold only has one output format.""" ) def UpperCAmelCase ( self ) -> Optional[int]: pass @unittest.skip("""This test doesn't work for ESMFold and doesn't test core functionality""" ) def UpperCAmelCase ( self ) -> Dict: pass @unittest.skip("""ESMFold does not support input chunking.""" ) def UpperCAmelCase ( self ) -> int: pass @unittest.skip("""ESMFold doesn't respect you and it certainly doesn't respect your initialization arguments.""" ) def UpperCAmelCase ( self ) -> int: pass @unittest.skip("""ESMFold doesn't support torchscript compilation.""" ) def UpperCAmelCase ( self ) -> str: pass @unittest.skip("""ESMFold doesn't support torchscript compilation.""" ) def UpperCAmelCase ( self ) -> Any: pass @unittest.skip("""ESMFold doesn't support torchscript compilation.""" ) def UpperCAmelCase ( self ) -> Tuple: pass @unittest.skip("""ESMFold doesn't support data parallel.""" ) def UpperCAmelCase ( self ) -> str: pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def UpperCAmelCase ( self ) -> Dict: pass @require_torch class __lowercase (UpperCamelCase__ ): """simple docstring""" @slow def UpperCAmelCase ( self ) -> List[str]: snake_case : int = EsmForProteinFolding.from_pretrained("""facebook/esmfold_v1""" ).float() model.eval() snake_case : Any = torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]] ) snake_case : Dict = model(A )["""positions"""] snake_case : Tuple = torch.tensor([2.58_28, 0.79_93, -10.93_34] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , A , atol=1e-4 ) )
176
# using dfs for finding eulerian path traversal def SCREAMING_SNAKE_CASE__ ( lowercase ,lowercase ,lowercase ,lowercase=None ) -> Any: snake_case : Union[str, Any] = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: snake_case , snake_case : int = True, True snake_case : List[Any] = dfs(lowercase ,lowercase ,lowercase ,lowercase ) return path def SCREAMING_SNAKE_CASE__ ( lowercase ,lowercase ) -> Tuple: snake_case : Union[str, Any] = 0 snake_case : Union[str, Any] = -1 for i in range(lowercase ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 snake_case : str = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def SCREAMING_SNAKE_CASE__ ( lowercase ,lowercase ) -> List[str]: snake_case : Tuple = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] snake_case , snake_case : Any = check_circuit_or_path(lowercase ,lowercase ) if check == 3: print("""graph is not Eulerian""" ) print("""no path""" ) return snake_case : str = 1 if check == 2: snake_case : Optional[int] = odd_node print("""graph has a Euler path""" ) if check == 1: print("""graph has a Euler cycle""" ) snake_case : Dict = dfs(lowercase ,lowercase ,lowercase ) print(lowercase ) def SCREAMING_SNAKE_CASE__ ( ) -> Optional[Any]: snake_case : Union[str, Any] = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} snake_case : Optional[int] = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} snake_case : Optional[int] = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} snake_case : List[Any] = {1: [2, 3], 2: [1, 3], 3: [1, 2]} snake_case : Tuple = { 1: [], 2: [] # all degree is zero } snake_case : Tuple = 10 check_euler(lowercase ,lowercase ) check_euler(lowercase ,lowercase ) check_euler(lowercase ,lowercase ) check_euler(lowercase ,lowercase ) check_euler(lowercase ,lowercase ) if __name__ == "__main__": main()
176
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : List[str] = ["""flax"""] def __init__( self : str , *_UpperCAmelCase : Optional[Any] , **_UpperCAmelCase : Optional[int] ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Dict , *_UpperCAmelCase : Tuple , **_UpperCAmelCase : Optional[int] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Union[str, Any] , *_UpperCAmelCase : Optional[Any] , **_UpperCAmelCase : Dict ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : str = ["""flax"""] def __init__( self : Optional[Any] , *_UpperCAmelCase : int , **_UpperCAmelCase : Dict ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Tuple , *_UpperCAmelCase : Any , **_UpperCAmelCase : str ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Any , *_UpperCAmelCase : Optional[int] , **_UpperCAmelCase : Optional[int] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : List[Any] = ["""flax"""] def __init__( self : List[Any] , *_UpperCAmelCase : Any , **_UpperCAmelCase : List[Any] ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[str] , *_UpperCAmelCase : Optional[int] , **_UpperCAmelCase : Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Dict , *_UpperCAmelCase : Tuple , **_UpperCAmelCase : List[Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Optional[Any] = ["""flax"""] def __init__( self : int , *_UpperCAmelCase : str , **_UpperCAmelCase : List[str] ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[str] , *_UpperCAmelCase : Tuple , **_UpperCAmelCase : Tuple ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Dict , *_UpperCAmelCase : Any , **_UpperCAmelCase : Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Optional[Any] = ["""flax"""] def __init__( self : Dict , *_UpperCAmelCase : List[Any] , **_UpperCAmelCase : str ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Tuple , *_UpperCAmelCase : Dict , **_UpperCAmelCase : List[Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[Any] , *_UpperCAmelCase : Optional[Any] , **_UpperCAmelCase : Tuple ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : List[str] = ["""flax"""] def __init__( self : List[str] , *_UpperCAmelCase : Union[str, Any] , **_UpperCAmelCase : int ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Optional[int] , *_UpperCAmelCase : Optional[int] , **_UpperCAmelCase : Optional[Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Tuple , *_UpperCAmelCase : Tuple , **_UpperCAmelCase : List[Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Dict = ["""flax"""] def __init__( self : Optional[Any] , *_UpperCAmelCase : Union[str, Any] , **_UpperCAmelCase : Tuple ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Optional[Any] , *_UpperCAmelCase : int , **_UpperCAmelCase : int ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Tuple , *_UpperCAmelCase : Optional[int] , **_UpperCAmelCase : Any ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : str = ["""flax"""] def __init__( self : Optional[Any] , *_UpperCAmelCase : List[Any] , **_UpperCAmelCase : int ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Union[str, Any] , *_UpperCAmelCase : Optional[Any] , **_UpperCAmelCase : Dict ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Union[str, Any] , *_UpperCAmelCase : Tuple , **_UpperCAmelCase : Optional[Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Union[str, Any] = ["""flax"""] def __init__( self : Optional[int] , *_UpperCAmelCase : Optional[Any] , **_UpperCAmelCase : Optional[int] ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Optional[Any] , *_UpperCAmelCase : Dict , **_UpperCAmelCase : Tuple ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : int , *_UpperCAmelCase : Dict , **_UpperCAmelCase : List[str] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Optional[Any] = ["""flax"""] def __init__( self : Tuple , *_UpperCAmelCase : int , **_UpperCAmelCase : List[str] ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Union[str, Any] , *_UpperCAmelCase : Optional[int] , **_UpperCAmelCase : Optional[Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Dict , *_UpperCAmelCase : Union[str, Any] , **_UpperCAmelCase : Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : List[str] = ["""flax"""] def __init__( self : List[str] , *_UpperCAmelCase : List[str] , **_UpperCAmelCase : Optional[Any] ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Tuple , *_UpperCAmelCase : Any , **_UpperCAmelCase : int ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[Any] , *_UpperCAmelCase : str , **_UpperCAmelCase : List[Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Optional[int] = ["""flax"""] def __init__( self : Union[str, Any] , *_UpperCAmelCase : str , **_UpperCAmelCase : int ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[str] , *_UpperCAmelCase : Tuple , **_UpperCAmelCase : Dict ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Any , *_UpperCAmelCase : Union[str, Any] , **_UpperCAmelCase : List[str] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Any = ["""flax"""] def __init__( self : Optional[int] , *_UpperCAmelCase : List[Any] , **_UpperCAmelCase : int ): """simple docstring""" requires_backends(self , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Union[str, Any] , *_UpperCAmelCase : List[Any] , **_UpperCAmelCase : Union[str, Any] ): """simple docstring""" requires_backends(cls , ["""flax"""] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Optional[int] , *_UpperCAmelCase : int , **_UpperCAmelCase : List[str] ): """simple docstring""" requires_backends(cls , ["""flax"""] )
346
'''simple docstring''' 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() UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = { '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', } UpperCAmelCase_ = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', 'projector', 'classifier', ] def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : Tuple ): '''simple docstring''' UpperCAmelCase__ = {} with open(SCREAMING_SNAKE_CASE__ , """r""" ) as file: for line_number, line in enumerate(SCREAMING_SNAKE_CASE__ ): UpperCAmelCase__ = line.strip() if line: UpperCAmelCase__ = line.split() UpperCAmelCase__ = line_number UpperCAmelCase__ = words[0] UpperCAmelCase__ = value return result def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): '''simple docstring''' for attribute in key.split(""".""" ): UpperCAmelCase__ = getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(SCREAMING_SNAKE_CASE__ ): UpperCAmelCase__ = PARAM_MAPPING[full_name.split(""".""" )[-1]] UpperCAmelCase__ = """param""" if weight_type is not None and weight_type != "param": UpperCAmelCase__ = getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).shape elif weight_type is not None and weight_type == "param": UpperCAmelCase__ = hf_pointer for attribute in hf_param_name.split(""".""" ): UpperCAmelCase__ = getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = shape_pointer.shape # let's reduce dimension UpperCAmelCase__ = value[0] else: UpperCAmelCase__ = 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": UpperCAmelCase__ = value elif weight_type == "weight_g": UpperCAmelCase__ = value elif weight_type == "weight_v": UpperCAmelCase__ = value elif weight_type == "bias": UpperCAmelCase__ = value elif weight_type == "param": for attribute in hf_param_name.split(""".""" ): UpperCAmelCase__ = getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = value else: UpperCAmelCase__ = value logger.info(F'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.''' ) def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(SCREAMING_SNAKE_CASE__ ): UpperCAmelCase__ = PARAM_MAPPING[full_name.split(""".""" )[-1]] UpperCAmelCase__ = """param""" if weight_type is not None and weight_type != "param": UpperCAmelCase__ = """.""".join([key, weight_type] ) elif weight_type is not None and weight_type == "param": UpperCAmelCase__ = """.""".join([key, hf_param_name] ) else: UpperCAmelCase__ = key UpperCAmelCase__ = value if """lm_head""" in full_key else value[0] UpperCAmelCase_ = { '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 _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Optional[Any]=None ): '''simple docstring''' UpperCAmelCase__ = False for key, mapped_key in MAPPING.items(): UpperCAmelCase__ = """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]: UpperCAmelCase__ = True if "*" in mapped_key: UpperCAmelCase__ = name.split(SCREAMING_SNAKE_CASE__ )[0].split(""".""" )[-2] UpperCAmelCase__ = mapped_key.replace("""*""" , SCREAMING_SNAKE_CASE__ ) if "weight_g" in name: UpperCAmelCase__ = """weight_g""" elif "weight_v" in name: UpperCAmelCase__ = """weight_v""" elif "bias" in name: UpperCAmelCase__ = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj UpperCAmelCase__ = """weight""" else: UpperCAmelCase__ = None if hf_dict is not None: rename_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: set_recursively(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return is_used return is_used def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] ): '''simple docstring''' UpperCAmelCase__ = [] UpperCAmelCase__ = fairseq_model.state_dict() UpperCAmelCase__ = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): UpperCAmelCase__ = False if "conv_layers" in name: load_conv_layer( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , hf_model.config.feat_extract_norm == """group""" , ) UpperCAmelCase__ = True else: UpperCAmelCase__ = load_wavaveca_layer(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if not is_used: unused_weights.append(SCREAMING_SNAKE_CASE__ ) logger.warning(F'''Unused weights: {unused_weights}''' ) def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : int ): '''simple docstring''' UpperCAmelCase__ = full_name.split("""conv_layers.""" )[-1] UpperCAmelCase__ = name.split(""".""" ) UpperCAmelCase__ = int(items[0] ) UpperCAmelCase__ = 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.''' ) UpperCAmelCase__ = 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.''' ) UpperCAmelCase__ = 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.''' ) UpperCAmelCase__ = 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.''' ) UpperCAmelCase__ = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(SCREAMING_SNAKE_CASE__ ) @torch.no_grad() def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str]=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Union[str, Any]=False ): '''simple docstring''' if config_path is not None: UpperCAmelCase__ = WavaVecaConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) else: UpperCAmelCase__ = WavaVecaConfig() if is_seq_class: UpperCAmelCase__ = read_txt_into_dict(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = idalabel UpperCAmelCase__ = WavaVecaForSequenceClassification(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=SCREAMING_SNAKE_CASE__ , return_attention_mask=SCREAMING_SNAKE_CASE__ , ) feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE__ ) elif is_finetuned: if dict_path: UpperCAmelCase__ = Dictionary.load(SCREAMING_SNAKE_CASE__ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq UpperCAmelCase__ = target_dict.pad_index UpperCAmelCase__ = target_dict.bos_index UpperCAmelCase__ = target_dict.eos_index UpperCAmelCase__ = len(target_dict.symbols ) UpperCAmelCase__ = os.path.join(SCREAMING_SNAKE_CASE__ , """vocab.json""" ) if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(SCREAMING_SNAKE_CASE__ ) ) return os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = target_dict.indices # fairseq has the <pad> and <s> switched UpperCAmelCase__ = 0 UpperCAmelCase__ = 1 with open(SCREAMING_SNAKE_CASE__ , """w""" , encoding="""utf-8""" ) as vocab_handle: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = WavaVecaCTCTokenizer( SCREAMING_SNAKE_CASE__ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="""|""" , do_lower_case=SCREAMING_SNAKE_CASE__ , ) UpperCAmelCase__ = True if config.feat_extract_norm == """layer""" else False UpperCAmelCase__ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=SCREAMING_SNAKE_CASE__ , return_attention_mask=SCREAMING_SNAKE_CASE__ , ) UpperCAmelCase__ = WavaVecaProcessor(feature_extractor=SCREAMING_SNAKE_CASE__ , tokenizer=SCREAMING_SNAKE_CASE__ ) processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = WavaVecaForCTC(SCREAMING_SNAKE_CASE__ ) else: UpperCAmelCase__ = WavaVecaForPreTraining(SCREAMING_SNAKE_CASE__ ) if is_finetuned or is_seq_class: UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) else: UpperCAmelCase__ = argparse.Namespace(task="""audio_pretraining""" ) UpperCAmelCase__ = fairseq.tasks.setup_task(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = model[0].eval() recursively_load_weights(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , not is_finetuned ) hf_wavavec.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": UpperCAmelCase_ = 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', ) UpperCAmelCase_ = parser.parse_args() UpperCAmelCase_ = 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, )
346
1
'''simple docstring''' import re from filelock import FileLock try: import nltk snake_case__ : Union[str, Any] = True except (ImportError, ModuleNotFoundError): snake_case__ : Any = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def _lowerCamelCase ( lowerCamelCase_ : str ): """simple docstring""" 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_ ) )
274
'''simple docstring''' from manim import * class __SCREAMING_SNAKE_CASE ( lowerCamelCase_ ): '''simple docstring''' def _UpperCamelCase ( self ): '''simple docstring''' UpperCAmelCase_ : Dict = Rectangle(height=0.5 , width=0.5 ) UpperCAmelCase_ : Any = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) UpperCAmelCase_ : List[str] = Rectangle(height=0.25 , width=0.25 ) UpperCAmelCase_ : Any = [mem.copy() for i in range(6 )] UpperCAmelCase_ : Union[str, Any] = [mem.copy() for i in range(6 )] UpperCAmelCase_ : Optional[int] = VGroup(*snake_case_ ).arrange(snake_case_ , buff=0 ) UpperCAmelCase_ : Any = VGroup(*snake_case_ ).arrange(snake_case_ , buff=0 ) UpperCAmelCase_ : str = VGroup(snake_case_ , snake_case_ ).arrange(snake_case_ , buff=0 ) UpperCAmelCase_ : Any = Text('CPU' , font_size=2_4 ) UpperCAmelCase_ : Tuple = Group(snake_case_ , snake_case_ ).arrange(snake_case_ , buff=0.5 , aligned_edge=snake_case_ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(snake_case_ ) UpperCAmelCase_ : str = [mem.copy() for i in range(4 )] UpperCAmelCase_ : Dict = VGroup(*snake_case_ ).arrange(snake_case_ , buff=0 ) UpperCAmelCase_ : List[str] = Text('GPU' , font_size=2_4 ) UpperCAmelCase_ : Optional[int] = Group(snake_case_ , snake_case_ ).arrange(snake_case_ , buff=0.5 , aligned_edge=snake_case_ ) gpu.move_to([-1, -1, 0] ) self.add(snake_case_ ) UpperCAmelCase_ : str = [mem.copy() for i in range(6 )] UpperCAmelCase_ : Tuple = VGroup(*snake_case_ ).arrange(snake_case_ , buff=0 ) UpperCAmelCase_ : str = Text('Model' , font_size=2_4 ) UpperCAmelCase_ : Optional[int] = Group(snake_case_ , snake_case_ ).arrange(snake_case_ , buff=0.5 , aligned_edge=snake_case_ ) model.move_to([3, -1.0, 0] ) self.add(snake_case_ ) UpperCAmelCase_ : str = [] UpperCAmelCase_ : Optional[Any] = [] for i, rect in enumerate(snake_case_ ): UpperCAmelCase_ : str = fill.copy().set_fill(snake_case_ , opacity=0.8 ) target.move_to(snake_case_ ) model_arr.append(snake_case_ ) UpperCAmelCase_ : int = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0.0 ).set_fill(snake_case_ , opacity=0.8 ) cpu_target.move_to(cpu_left_col_base[i] ) model_cpu_arr.append(snake_case_ ) self.add(*snake_case_ , *snake_case_ ) UpperCAmelCase_ : List[Any] = [meta_mem.copy() for i in range(6 )] UpperCAmelCase_ : List[str] = [meta_mem.copy() for i in range(6 )] UpperCAmelCase_ : Tuple = VGroup(*snake_case_ ).arrange(snake_case_ , buff=0 ) UpperCAmelCase_ : Dict = VGroup(*snake_case_ ).arrange(snake_case_ , buff=0 ) UpperCAmelCase_ : Optional[Any] = VGroup(snake_case_ , snake_case_ ).arrange(snake_case_ , buff=0 ) UpperCAmelCase_ : Tuple = Text('Disk' , font_size=2_4 ) UpperCAmelCase_ : Union[str, Any] = Group(snake_case_ , snake_case_ ).arrange(snake_case_ , buff=0.5 , aligned_edge=snake_case_ ) disk.move_to([-4, -1.25, 0] ) self.add(snake_case_ , snake_case_ ) UpperCAmelCase_ : List[Any] = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) UpperCAmelCase_ : Any = MarkupText( F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model''' , font_size=1_8 , ) key_text.move_to([-5, 2.4, 0] ) self.add(snake_case_ , snake_case_ ) UpperCAmelCase_ : Dict = MarkupText( F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint''' , font_size=1_8 , ) blue_text.next_to(snake_case_ , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(snake_case_ ) UpperCAmelCase_ : Optional[Any] = MarkupText( F'''Now watch as an input is passed through the model\nand how the memory is utilized and handled.''' , font_size=2_4 , ) step_a.move_to([2, 2, 0] ) self.play(Write(snake_case_ ) ) UpperCAmelCase_ : Tuple = Square(0.3 ) input.set_fill(snake_case_ , opacity=1.0 ) input.set_stroke(width=0.0 ) input.next_to(model_base[0] , snake_case_ , buff=0.5 ) self.play(Write(snake_case_ ) ) input.generate_target() input.target.next_to(model_arr[0] , direction=snake_case_ , buff=0.02 ) self.play(MoveToTarget(snake_case_ ) ) self.play(FadeOut(snake_case_ ) ) UpperCAmelCase_ : Any = Arrow(start=snake_case_ , end=snake_case_ , color=snake_case_ , buff=0.5 ) a.next_to(model_arr[0].get_left() , snake_case_ , buff=0.2 ) model_cpu_arr[0].generate_target() model_cpu_arr[0].target.move_to(gpu_rect[0] ) UpperCAmelCase_ : List[str] = MarkupText( F'''As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back.''' , font_size=2_4 , ) step_a.move_to([2, 2, 0] ) self.play(Write(snake_case_ , run_time=3 ) ) UpperCAmelCase_ : List[Any] = {'run_time': 1, 'fade_in': True, 'fade_out': True, 'buff': 0.02} self.play( Write(snake_case_ ) , Circumscribe(model_arr[0] , color=snake_case_ , **snake_case_ ) , Circumscribe(model_cpu_arr[0] , color=snake_case_ , **snake_case_ ) , Circumscribe(gpu_rect[0] , color=snake_case_ , **snake_case_ ) , ) self.play(MoveToTarget(model_cpu_arr[0] ) ) UpperCAmelCase_ : Union[str, Any] = a.copy() for i in range(6 ): a_c.next_to(model_arr[i].get_right() + 0.02 , snake_case_ , buff=0.2 ) input.generate_target() input.target.move_to(model_arr[i].get_right() + 0.02 ) UpperCAmelCase_ : Tuple = AnimationGroup( FadeOut(snake_case_ , run_time=0.5 ) , MoveToTarget(snake_case_ , run_time=0.5 ) , FadeIn(snake_case_ , run_time=0.5 ) , lag_ratio=0.2 ) self.play(snake_case_ ) model_cpu_arr[i].generate_target() model_cpu_arr[i].target.move_to(cpu_left_col_base[i] ) if i < 5: model_cpu_arr[i + 1].generate_target() model_cpu_arr[i + 1].target.move_to(gpu_rect[0] ) if i >= 1: UpperCAmelCase_ : Any = 0.7 self.play( Circumscribe(model_arr[i] , **snake_case_ ) , Circumscribe(cpu_left_col_base[i] , **snake_case_ ) , Circumscribe(cpu_left_col_base[i + 1] , color=snake_case_ , **snake_case_ ) , Circumscribe(gpu_rect[0] , color=snake_case_ , **snake_case_ ) , Circumscribe(model_arr[i + 1] , color=snake_case_ , **snake_case_ ) , ) if i < 1: self.play( MoveToTarget(model_cpu_arr[i] ) , MoveToTarget(model_cpu_arr[i + 1] ) , ) else: self.play( MoveToTarget(model_cpu_arr[i] , run_time=0.7 ) , MoveToTarget(model_cpu_arr[i + 1] , run_time=0.7 ) , ) else: model_cpu_arr[i].generate_target() model_cpu_arr[i].target.move_to(cpu_left_col_base[-1] ) input.generate_target() input.target.next_to(model_arr[-1].get_right() , RIGHT + 0.02 , buff=0.2 ) self.play( Circumscribe(model_arr[-1] , color=snake_case_ , **snake_case_ ) , Circumscribe(cpu_left_col_base[-1] , color=snake_case_ , **snake_case_ ) , Circumscribe(gpu_rect[0] , color=snake_case_ , **snake_case_ ) , ) self.play(MoveToTarget(model_cpu_arr[i] ) ) UpperCAmelCase_ : Any = a_c UpperCAmelCase_ : int = a_c.copy() input.generate_target() input.target.next_to(model_base[-1] , RIGHT + 0.02 , buff=0.5 ) self.play( FadeOut(snake_case_ ) , FadeOut(snake_case_ , run_time=0.5 ) , ) UpperCAmelCase_ : Optional[Any] = MarkupText(F'''Inference on a model too large for GPU memory\nis successfully completed.''' , font_size=2_4 ) step_a.move_to([2, 2, 0] ) self.play(Write(snake_case_ , run_time=3 ) , MoveToTarget(snake_case_ ) ) self.wait()
274
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) a_ = { 'configuration_convbert': ['CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConvBertConfig', 'ConvBertOnnxConfig'], 'tokenization_convbert': ['ConvBertTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['ConvBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'ConvBertForMaskedLM', 'ConvBertForMultipleChoice', 'ConvBertForQuestionAnswering', 'ConvBertForSequenceClassification', 'ConvBertForTokenClassification', 'ConvBertLayer', 'ConvBertModel', 'ConvBertPreTrainedModel', 'load_tf_weights_in_convbert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFConvBertForMaskedLM', 'TFConvBertForMultipleChoice', 'TFConvBertForQuestionAnswering', 'TFConvBertForSequenceClassification', 'TFConvBertForTokenClassification', 'TFConvBertLayer', 'TFConvBertModel', 'TFConvBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig from .tokenization_convbert import ConvBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_convbert_fast import ConvBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convbert import ( CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvBertForMaskedLM, ConvBertForMultipleChoice, ConvBertForQuestionAnswering, ConvBertForSequenceClassification, ConvBertForTokenClassification, ConvBertLayer, ConvBertModel, ConvBertPreTrainedModel, load_tf_weights_in_convbert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convbert import ( TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertLayer, TFConvBertModel, TFConvBertPreTrainedModel, ) else: import sys a_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
76
from typing import Any class __SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , A : Any ) ->Optional[int]: lowerCamelCase__ : Optional[int] = data lowerCamelCase__ : Any = None class __SCREAMING_SNAKE_CASE : def __init__( self : Optional[Any] ) ->str: lowerCamelCase__ : Any = None def __lowerCamelCase ( self : Tuple ) ->Any: lowerCamelCase__ : str = self.head while temp is not None: print(temp.data , end=''' ''' ) lowerCamelCase__ : Dict = temp.next print() def __lowerCamelCase ( self : Dict , A : Any ) ->Optional[int]: lowerCamelCase__ : Union[str, Any] = Node(A ) lowerCamelCase__ : Dict = self.head lowerCamelCase__ : List[str] = new_node def __lowerCamelCase ( self : Optional[int] , A : int , A : Tuple ) ->List[Any]: if node_data_a == node_data_a: return else: lowerCamelCase__ : Tuple = self.head while node_a is not None and node_a.data != node_data_a: lowerCamelCase__ : Union[str, Any] = node_a.next lowerCamelCase__ : int = self.head while node_a is not None and node_a.data != node_data_a: lowerCamelCase__ : Optional[int] = node_a.next if node_a is None or node_a is None: return lowerCamelCase__ , lowerCamelCase__ : str = node_a.data, node_a.data if __name__ == "__main__": _A : List[Any] = LinkedList() for i in range(5, 0, -1): ll.push(i) ll.print_list() ll.swap_nodes(1, 4) print('After swapping') ll.print_list()
142
0
from __future__ import annotations from dataclasses import dataclass @dataclass class UpperCAmelCase__ : """simple docstring""" UpperCAmelCase__ : float UpperCAmelCase__ : TreeNode | None = None UpperCAmelCase__ : TreeNode | None = None def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode | None ): # Validation def is_valid_tree(SCREAMING_SNAKE_CASE__ : TreeNode | None ) -> bool: if node is None: return True if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return False try: float(node.data ) except (TypeError, ValueError): return False return is_valid_tree(node.left ) and is_valid_tree(node.right ) if not is_valid_tree(SCREAMING_SNAKE_CASE__ ): raise ValueError( 'Each node should be type of TreeNode and data should be float.' ) def is_binary_search_tree_recursive_check( SCREAMING_SNAKE_CASE__ : TreeNode | None , SCREAMING_SNAKE_CASE__ : float , SCREAMING_SNAKE_CASE__ : float ) -> bool: if node is None: return True return ( left_bound < node.data < right_bound and is_binary_search_tree_recursive_check(node.left , SCREAMING_SNAKE_CASE__ , node.data ) and is_binary_search_tree_recursive_check( node.right , node.data , SCREAMING_SNAKE_CASE__ ) ) return is_binary_search_tree_recursive_check(SCREAMING_SNAKE_CASE__ , -float('inf' ) , float('inf' ) ) if __name__ == "__main__": import doctest doctest.testmod()
117
import json import os from collections import Counter import torch import torchvision import torchvision.transforms as transforms from PIL import Image from torch import nn from torch.utils.data import Dataset _A = {1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2), 5: (5, 1), 6: (3, 2), 7: (7, 1), 8: (4, 2), 9: (3, 3)} class UpperCAmelCase__ ( nn.Module ): """simple docstring""" def __init__( self , A_ ) -> Optional[int]: super().__init__() __UpperCamelCase =torchvision.models.resnetaaa(pretrained=A_ ) __UpperCamelCase =list(model.children() )[:-2] __UpperCamelCase =nn.Sequential(*A_ ) __UpperCamelCase =nn.AdaptiveAvgPoolad(POOLING_BREAKDOWN[args.num_image_embeds] ) def _a ( self , A_ ) -> int: # Bx3x224x224 -> Bx2048x7x7 -> Bx2048xN -> BxNx2048 __UpperCamelCase =self.pool(self.model(A_ ) ) __UpperCamelCase =torch.flatten(A_ , start_dim=2 ) __UpperCamelCase =out.transpose(1 , 2 ).contiguous() return out # BxNx2048 class UpperCAmelCase__ ( A_ ): """simple docstring""" def __init__( self , A_ , A_ , A_ , A_ , A_ ) -> List[str]: __UpperCamelCase =[json.loads(A_ ) for l in open(A_ )] __UpperCamelCase =os.path.dirname(A_ ) __UpperCamelCase =tokenizer __UpperCamelCase =labels __UpperCamelCase =len(A_ ) __UpperCamelCase =max_seq_length __UpperCamelCase =transforms def __len__( self ) -> Any: return len(self.data ) def __getitem__( self , A_ ) -> Union[str, Any]: __UpperCamelCase =torch.LongTensor(self.tokenizer.encode(self.data[index]['text'] , add_special_tokens=A_ ) ) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase =sentence[0], sentence[1:-1], sentence[-1] __UpperCamelCase =sentence[: self.max_seq_length] __UpperCamelCase =torch.zeros(self.n_classes ) __UpperCamelCase =1 __UpperCamelCase =Image.open(os.path.join(self.data_dir , self.data[index]['img'] ) ).convert('RGB' ) __UpperCamelCase =self.transforms(A_ ) return { "image_start_token": start_token, "image_end_token": end_token, "sentence": sentence, "image": image, "label": label, } def _a ( self ) -> List[str]: __UpperCamelCase =Counter() for row in self.data: label_freqs.update(row['label'] ) return label_freqs def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Union[str, Any] ): __UpperCamelCase =[len(row['sentence'] ) for row in batch] __UpperCamelCase , __UpperCamelCase =len(SCREAMING_SNAKE_CASE__ ), max(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =torch.zeros(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , dtype=torch.long ) __UpperCamelCase =torch.zeros(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , dtype=torch.long ) for i_batch, (input_row, length) in enumerate(zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): __UpperCamelCase =input_row['sentence'] __UpperCamelCase =1 __UpperCamelCase =torch.stack([row['image'] for row in batch] ) __UpperCamelCase =torch.stack([row['label'] for row in batch] ) __UpperCamelCase =torch.stack([row['image_start_token'] for row in batch] ) __UpperCamelCase =torch.stack([row['image_end_token'] for row in batch] ) return text_tensor, mask_tensor, img_tensor, img_start_token, img_end_token, tgt_tensor def _UpperCAmelCase ( ): return [ "Crime", "Drama", "Thriller", "Action", "Comedy", "Romance", "Documentary", "Short", "Mystery", "History", "Family", "Adventure", "Fantasy", "Sci-Fi", "Western", "Horror", "Sport", "War", "Music", "Musical", "Animation", "Biography", "Film-Noir", ] def _UpperCAmelCase ( ): return transforms.Compose( [ transforms.Resize(2_56 ), transforms.CenterCrop(2_24 ), transforms.ToTensor(), transforms.Normalize( mean=[0.46777044, 0.44531429, 0.40661017] , std=[0.12221994, 0.12145835, 0.14380469] , ), ] )
117
1
import argparse import hashlib # hashlib is only used inside the Test class import struct class __UpperCAmelCase : def __init__( self: List[str] , UpperCAmelCase_: Dict ): '''simple docstring''' _SCREAMING_SNAKE_CASE = data _SCREAMING_SNAKE_CASE = [0x67_452_301, 0xef_cda_b89, 0x98_bad_cfe, 0x10_325_476, 0xc3_d2e_1f0] @staticmethod def UpperCamelCase ( UpperCAmelCase_: int , UpperCAmelCase_: List[str] ): '''simple docstring''' return ((n << b) | (n >> (32 - b))) & 0xff_fff_fff def UpperCamelCase ( self: Any ): '''simple docstring''' _SCREAMING_SNAKE_CASE = B"""\x80""" + B"""\x00""" * (63 - (len(self.data ) + 8) % 64) _SCREAMING_SNAKE_CASE = self.data + padding + struct.pack(""">Q""" , 8 * len(self.data ) ) return padded_data def UpperCamelCase ( self: List[Any] ): '''simple docstring''' return [ self.padded_data[i : i + 64] for i in range(0 , len(self.padded_data ) , 64 ) ] def UpperCamelCase ( self: Optional[Any] , UpperCAmelCase_: Union[str, Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = list(struct.unpack(""">16L""" , UpperCAmelCase_ ) ) + [0] * 64 for i in range(16 , 80 ): _SCREAMING_SNAKE_CASE = self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]) , 1 ) return w def UpperCamelCase ( self: List[str] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.padding() _SCREAMING_SNAKE_CASE = self.split_blocks() for block in self.blocks: _SCREAMING_SNAKE_CASE = self.expand_block(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.h for i in range(0 , 80 ): if 0 <= i < 20: _SCREAMING_SNAKE_CASE = (b & c) | ((~b) & d) _SCREAMING_SNAKE_CASE = 0x5a_827_999 elif 20 <= i < 40: _SCREAMING_SNAKE_CASE = b ^ c ^ d _SCREAMING_SNAKE_CASE = 0x6e_d9e_ba1 elif 40 <= i < 60: _SCREAMING_SNAKE_CASE = (b & c) | (b & d) | (c & d) _SCREAMING_SNAKE_CASE = 0x8f_1bb_cdc elif 60 <= i < 80: _SCREAMING_SNAKE_CASE = b ^ c ^ d _SCREAMING_SNAKE_CASE = 0xca_62c_1d6 _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = ( self.rotate(UpperCAmelCase_ , 5 ) + f + e + k + expanded_block[i] & 0xff_fff_fff, a, self.rotate(UpperCAmelCase_ , 30 ), c, d, ) _SCREAMING_SNAKE_CASE = ( self.h[0] + a & 0xff_fff_fff, self.h[1] + b & 0xff_fff_fff, self.h[2] + c & 0xff_fff_fff, self.h[3] + d & 0xff_fff_fff, self.h[4] + e & 0xff_fff_fff, ) return ("{:08x}" * 5).format(*self.h ) def __lowerCamelCase ( ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = b"""Test String""" assert SHAaHash(snake_case__ ).final_hash() == hashlib.shaa(snake_case__ ).hexdigest() # noqa: S324 def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = 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""" ) _SCREAMING_SNAKE_CASE = parser.parse_args() _SCREAMING_SNAKE_CASE = args.input_string # In any case hash input should be a bytestring if args.input_file: with open(args.input_file ,"""rb""" ) as f: _SCREAMING_SNAKE_CASE = f.read() else: _SCREAMING_SNAKE_CASE = bytes(snake_case__ ,"""utf-8""" ) print(SHAaHash(snake_case__ ).final_hash() ) if __name__ == "__main__": main() import doctest doctest.testmod()
306
from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class __UpperCAmelCase : def __init__( self: Any , UpperCAmelCase_: int , UpperCAmelCase_: Optional[int]=13 , UpperCAmelCase_: str=7 , UpperCAmelCase_: int=True , UpperCAmelCase_: List[str]=True , UpperCAmelCase_: Dict=True , UpperCAmelCase_: Any=True , UpperCAmelCase_: Tuple=99 , UpperCAmelCase_: Optional[Any]=32 , UpperCAmelCase_: Optional[int]=2 , UpperCAmelCase_: Tuple=4 , UpperCAmelCase_: Tuple=37 , UpperCAmelCase_: Union[str, Any]="gelu" , UpperCAmelCase_: List[str]=0.1 , UpperCAmelCase_: int=0.1 , UpperCAmelCase_: str=512 , UpperCAmelCase_: Union[str, Any]=16 , UpperCAmelCase_: List[Any]=2 , UpperCAmelCase_: str=0.02 , UpperCAmelCase_: int=False , UpperCAmelCase_: Union[str, Any]=True , UpperCAmelCase_: Optional[Any]="None" , UpperCAmelCase_: Optional[int]=3 , UpperCAmelCase_: Any=4 , UpperCAmelCase_: Optional[int]=None , ): '''simple docstring''' _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = seq_length _SCREAMING_SNAKE_CASE = is_training _SCREAMING_SNAKE_CASE = use_input_mask _SCREAMING_SNAKE_CASE = use_token_type_ids _SCREAMING_SNAKE_CASE = use_labels _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = type_vocab_size _SCREAMING_SNAKE_CASE = type_sequence_label_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = num_labels _SCREAMING_SNAKE_CASE = num_choices _SCREAMING_SNAKE_CASE = relative_attention _SCREAMING_SNAKE_CASE = position_biased_input _SCREAMING_SNAKE_CASE = pos_att_type _SCREAMING_SNAKE_CASE = scope def UpperCamelCase ( self: int ): '''simple docstring''' _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) _SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = DebertaVaConfig( 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 , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , initializer_range=self.initializer_range , return_dict=UpperCAmelCase_ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase ( self: Optional[Any] , UpperCAmelCase_: int , UpperCAmelCase_: Optional[Any] , UpperCAmelCase_: str , UpperCAmelCase_: int , UpperCAmelCase_: List[str] , UpperCAmelCase_: List[str] , UpperCAmelCase_: Union[str, Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = TFDebertaVaModel(config=UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _SCREAMING_SNAKE_CASE = [input_ids, input_mask] _SCREAMING_SNAKE_CASE = model(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = model(UpperCAmelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self: Tuple , UpperCAmelCase_: Optional[int] , UpperCAmelCase_: List[str] , UpperCAmelCase_: Tuple , UpperCAmelCase_: int , UpperCAmelCase_: Optional[Any] , UpperCAmelCase_: str , UpperCAmelCase_: Union[str, Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = TFDebertaVaForMaskedLM(config=UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(UpperCAmelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self: Any , UpperCAmelCase_: Any , UpperCAmelCase_: List[str] , UpperCAmelCase_: Dict , UpperCAmelCase_: List[str] , UpperCAmelCase_: str , UpperCAmelCase_: int , UpperCAmelCase_: int ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFDebertaVaForSequenceClassification(config=UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(UpperCAmelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self: Optional[Any] , UpperCAmelCase_: Optional[int] , UpperCAmelCase_: Optional[int] , UpperCAmelCase_: Optional[int] , UpperCAmelCase_: List[Any] , UpperCAmelCase_: Any , UpperCAmelCase_: List[Any] , UpperCAmelCase_: Any ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFDebertaVaForTokenClassification(config=UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(UpperCAmelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase ( self: Any , UpperCAmelCase_: Optional[Any] , UpperCAmelCase_: Tuple , UpperCAmelCase_: Union[str, Any] , UpperCAmelCase_: str , UpperCAmelCase_: str , UpperCAmelCase_: Any , UpperCAmelCase_: Dict ): '''simple docstring''' _SCREAMING_SNAKE_CASE = TFDebertaVaForQuestionAnswering(config=UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = 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: List[Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ) = config_and_inputs _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class __UpperCAmelCase (_UpperCAmelCase ,_UpperCAmelCase ,unittest.TestCase ): __snake_case : int = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) __snake_case : Union[str, Any] = ( { "feature-extraction": TFDebertaVaModel, "fill-mask": TFDebertaVaForMaskedLM, "question-answering": TFDebertaVaForQuestionAnswering, "text-classification": TFDebertaVaForSequenceClassification, "token-classification": TFDebertaVaForTokenClassification, "zero-shot": TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) __snake_case : Dict = False __snake_case : Optional[Any] = False def UpperCamelCase ( self: Union[str, Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = TFDebertaVaModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=UpperCAmelCase_ , hidden_size=37 ) def UpperCamelCase ( self: Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCamelCase ( self: List[Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_ ) def UpperCamelCase ( self: List[Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_ ) def UpperCamelCase ( self: Any ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_ ) def UpperCamelCase ( self: Tuple ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase_ ) def UpperCamelCase ( self: Optional[int] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_ ) @slow def UpperCamelCase ( self: Any ): '''simple docstring''' _SCREAMING_SNAKE_CASE = TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""" ) self.assertIsNotNone(UpperCAmelCase_ ) @require_tf class __UpperCAmelCase (unittest.TestCase ): @unittest.skip(reason="""Model not available yet""" ) def UpperCamelCase ( self: Tuple ): '''simple docstring''' pass @slow def UpperCamelCase ( self: List[Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""" ) _SCREAMING_SNAKE_CASE = tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]] ) _SCREAMING_SNAKE_CASE = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _SCREAMING_SNAKE_CASE = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ )[0] _SCREAMING_SNAKE_CASE = tf.constant( [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]] ) tf.debugging.assert_near(output[:, 1:4, 1:4] , UpperCAmelCase_ , atol=1E-4 )
306
1
from ....utils import logging A = logging.get_logger(__name__) class __lowercase ( _UpperCamelCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=2048 ): __a : List[str] = config.__dict__ __a : List[str] = modal_hidden_size if num_labels: __a : int = num_labels
369
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''openai/whisper-base''' __lowerCAmelCase = ( '''This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the ''' '''transcribed text.''' ) __lowerCAmelCase = '''transcriber''' __lowerCAmelCase = WhisperProcessor __lowerCAmelCase = WhisperForConditionalGeneration __lowerCAmelCase = ['''audio'''] __lowerCAmelCase = ['''text'''] def _lowerCamelCase ( self , _UpperCAmelCase ): return self.pre_processor(_UpperCAmelCase , return_tensors='''pt''' ).input_features def _lowerCamelCase ( self , _UpperCAmelCase ): return self.model.generate(inputs=_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): return self.pre_processor.batch_decode(_UpperCAmelCase , skip_special_tokens=_UpperCAmelCase )[0]
188
0
import argparse import math import traceback import dateutil.parser as date_parser import requests def _lowercase ( UpperCamelCase_ ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE__ = {} SCREAMING_SNAKE_CASE__ = job['started_at'] SCREAMING_SNAKE_CASE__ = job['completed_at'] SCREAMING_SNAKE_CASE__ = date_parser.parse(UpperCamelCase_ ) SCREAMING_SNAKE_CASE__ = date_parser.parse(UpperCamelCase_ ) SCREAMING_SNAKE_CASE__ = round((end_datetime - start_datetime).total_seconds() / 60.0 ) SCREAMING_SNAKE_CASE__ = start SCREAMING_SNAKE_CASE__ = end SCREAMING_SNAKE_CASE__ = duration_in_min return job_info def _lowercase ( UpperCamelCase_ , UpperCamelCase_=None ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE__ = None if token is not None: SCREAMING_SNAKE_CASE__ = {'Accept': 'application/vnd.github+json', 'Authorization': F'Bearer {token}'} SCREAMING_SNAKE_CASE__ = F'https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100' SCREAMING_SNAKE_CASE__ = requests.get(UpperCamelCase_ , headers=UpperCamelCase_ ).json() SCREAMING_SNAKE_CASE__ = {} try: job_time.update({job['name']: extract_time_from_single_job(UpperCamelCase_ ) for job in result['jobs']} ) SCREAMING_SNAKE_CASE__ = math.ceil((result['total_count'] - 100) / 100 ) for i in range(UpperCamelCase_ ): SCREAMING_SNAKE_CASE__ = requests.get(url + F'&page={i + 2}' , headers=UpperCamelCase_ ).json() job_time.update({job['name']: extract_time_from_single_job(UpperCamelCase_ ) 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__": __snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument("""--workflow_run_id""", type=str, required=True, help="""A GitHub Actions workflow run id.""") __snake_case = parser.parse_args() __snake_case = get_job_time(args.workflow_run_id) __snake_case = 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"]}""")
176
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch __snake_case = logging.get_logger(__name__) @add_end_docstrings( _UpperCAmelCase , R""" top_k (`int`, defaults to 5): The number of predictions to return. targets (`str` or `List[str]`, *optional*): When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower). """ , ) class lowercase__ ( _UpperCAmelCase ): def A_ ( self : Any , UpperCAmelCase_ : GenericTensor ): if self.framework == "tf": SCREAMING_SNAKE_CASE__ = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": SCREAMING_SNAKE_CASE__ = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=UpperCAmelCase_ ) else: raise ValueError('Unsupported framework' ) return masked_index def A_ ( self : Optional[Any] , UpperCAmelCase_ : GenericTensor ): SCREAMING_SNAKE_CASE__ = self.get_masked_index(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( 'fill-mask' , self.model.base_model_prefix , F'No mask_token ({self.tokenizer.mask_token}) found on the input' , ) def A_ ( self : Union[str, Any] , UpperCAmelCase_ : GenericTensor ): if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['input_ids'][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(UpperCAmelCase_ ) def A_ ( self : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : str=None , **UpperCAmelCase_ : Union[str, Any] ): if return_tensors is None: SCREAMING_SNAKE_CASE__ = self.framework SCREAMING_SNAKE_CASE__ = self.tokenizer(UpperCAmelCase_ , return_tensors=UpperCAmelCase_ ) self.ensure_exactly_one_mask_token(UpperCAmelCase_ ) return model_inputs def A_ ( self : Tuple , UpperCAmelCase_ : Dict ): SCREAMING_SNAKE_CASE__ = self.model(**UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = model_inputs['input_ids'] return model_outputs def A_ ( self : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : Dict=None ): # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: SCREAMING_SNAKE_CASE__ = target_ids.shape[0] SCREAMING_SNAKE_CASE__ = model_outputs['input_ids'][0] SCREAMING_SNAKE_CASE__ = model_outputs['logits'] if self.framework == "tf": SCREAMING_SNAKE_CASE__ = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] SCREAMING_SNAKE_CASE__ = outputs.numpy() SCREAMING_SNAKE_CASE__ = outputs[0, masked_index, :] SCREAMING_SNAKE_CASE__ = stable_softmax(UpperCAmelCase_ , axis=-1 ) if target_ids is not None: SCREAMING_SNAKE_CASE__ = tf.gather_nd(tf.squeeze(UpperCAmelCase_ , 0 ) , target_ids.reshape(-1 , 1 ) ) SCREAMING_SNAKE_CASE__ = tf.expand_dims(UpperCAmelCase_ , 0 ) SCREAMING_SNAKE_CASE__ = tf.math.top_k(UpperCAmelCase_ , k=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = topk.values.numpy(), topk.indices.numpy() else: SCREAMING_SNAKE_CASE__ = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=UpperCAmelCase_ ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample SCREAMING_SNAKE_CASE__ = outputs[0, masked_index, :] SCREAMING_SNAKE_CASE__ = logits.softmax(dim=-1 ) if target_ids is not None: SCREAMING_SNAKE_CASE__ = probs[..., target_ids] SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = probs.topk(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): SCREAMING_SNAKE_CASE__ = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place SCREAMING_SNAKE_CASE__ = input_ids.numpy().copy() if target_ids is not None: SCREAMING_SNAKE_CASE__ = target_ids[p].tolist() SCREAMING_SNAKE_CASE__ = p # Filter padding out: SCREAMING_SNAKE_CASE__ = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back SCREAMING_SNAKE_CASE__ = self.tokenizer.decode(UpperCAmelCase_ , skip_special_tokens=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = {'score': v, 'token': p, 'token_str': self.tokenizer.decode([p] ), 'sequence': sequence} row.append(UpperCAmelCase_ ) result.append(UpperCAmelCase_ ) if single_mask: return result[0] return result def A_ ( self : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]=None ): if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): SCREAMING_SNAKE_CASE__ = [targets] try: SCREAMING_SNAKE_CASE__ = self.tokenizer.get_vocab() except Exception: SCREAMING_SNAKE_CASE__ = {} SCREAMING_SNAKE_CASE__ = [] for target in targets: SCREAMING_SNAKE_CASE__ = vocab.get(UpperCAmelCase_ , UpperCAmelCase_ ) if id_ is None: SCREAMING_SNAKE_CASE__ = self.tokenizer( UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_ , return_attention_mask=UpperCAmelCase_ , return_token_type_ids=UpperCAmelCase_ , max_length=1 , truncation=UpperCAmelCase_ , )['input_ids'] if len(UpperCAmelCase_ ) == 0: logger.warning( F'The specified target token `{target}` does not exist in the model vocabulary. ' 'We cannot replace it with anything meaningful, ignoring it' ) continue SCREAMING_SNAKE_CASE__ = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F'The specified target token `{target}` does not exist in the model vocabulary. ' F'Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`.' ) target_ids.append(id_ ) SCREAMING_SNAKE_CASE__ = list(set(UpperCAmelCase_ ) ) if len(UpperCAmelCase_ ) == 0: raise ValueError('At least one target must be provided when passed.' ) SCREAMING_SNAKE_CASE__ = np.array(UpperCAmelCase_ ) return target_ids def A_ ( self : List[str] , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : List[Any]=None ): SCREAMING_SNAKE_CASE__ = {} if targets is not None: SCREAMING_SNAKE_CASE__ = self.get_target_ids(UpperCAmelCase_ , UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = target_ids if top_k is not None: SCREAMING_SNAKE_CASE__ = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( 'fill-mask' , self.model.base_model_prefix , 'The tokenizer does not define a `mask_token`.' ) return {}, {}, postprocess_params def __call__( self : Tuple , UpperCAmelCase_ : Union[str, Any] , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Tuple ): SCREAMING_SNAKE_CASE__ = super().__call__(UpperCAmelCase_ , **UpperCAmelCase_ ) if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and len(UpperCAmelCase_ ) == 1: return outputs[0] return outputs
176
1
from __future__ import annotations __UpperCAmelCase = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def snake_case_ (__A : list[list[int]] , __A : list[int] , __A : list[int] , __A : int , __A : list[list[int]] , ) -> tuple[list[list[int]], list[list[int]]]: __lowerCAmelCase : Any = [ [0 for col in range(len(grid[0] ) )] for row in range(len(__A ) ) ] # the reference grid __lowerCAmelCase : Dict = 1 __lowerCAmelCase : List[str] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(__A ) ) ] # the action grid __lowerCAmelCase : Optional[int] = init[0] __lowerCAmelCase : int = init[1] __lowerCAmelCase : str = 0 __lowerCAmelCase : Optional[Any] = g + heuristic[x][y] # cost from starting cell to destination cell __lowerCAmelCase : Optional[int] = [[f, g, x, y]] __lowerCAmelCase : Optional[Any] = False # flag that is set when search is complete __lowerCAmelCase : str = False # flag set if we can't find expand while not found and not resign: if len(__A ) == 0: raise ValueError("""Algorithm is unable to find solution""" ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() __lowerCAmelCase : List[str] = cell.pop() __lowerCAmelCase : Optional[Any] = next_cell[2] __lowerCAmelCase : List[str] = next_cell[3] __lowerCAmelCase : Dict = next_cell[1] if x == goal[0] and y == goal[1]: __lowerCAmelCase : Union[str, Any] = True else: for i in range(len(__A ) ): # to try out different valid actions __lowerCAmelCase : List[Any] = x + DIRECTIONS[i][0] __lowerCAmelCase : Optional[Any] = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(__A ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: __lowerCAmelCase : Optional[int] = g + cost __lowerCAmelCase : Optional[int] = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) __lowerCAmelCase : str = 1 __lowerCAmelCase : Dict = i __lowerCAmelCase : Optional[Any] = [] __lowerCAmelCase : int = goal[0] __lowerCAmelCase : Tuple = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: __lowerCAmelCase : Tuple = x - DIRECTIONS[action[x][y]][0] __lowerCAmelCase : Optional[Any] = y - DIRECTIONS[action[x][y]][1] __lowerCAmelCase : str = xa __lowerCAmelCase : int = ya invpath.append([x, y] ) __lowerCAmelCase : Any = [] for i in range(len(__A ) ): path.append(invpath[len(__A ) - 1 - i] ) return path, action if __name__ == "__main__": __UpperCAmelCase = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] __UpperCAmelCase = [0, 0] # all coordinates are given in format [y,x] __UpperCAmelCase = [len(grid) - 1, len(grid[0]) - 1] __UpperCAmelCase = 1 # the cost map which pushes the path closer to the goal __UpperCAmelCase = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): __UpperCAmelCase = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map __UpperCAmelCase = 99 __UpperCAmelCase , __UpperCAmelCase = search(grid, init, goal, cost, heuristic) print("""ACTION MAP""") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
139
import unittest import numpy as np from transformers import BertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.bert.modeling_flax_bert import ( FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, ) class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" def __init__( self : Tuple , lowerCAmelCase : Tuple , lowerCAmelCase : str=13 , lowerCAmelCase : Optional[Any]=7 , lowerCAmelCase : Optional[Any]=True , lowerCAmelCase : Any=True , lowerCAmelCase : str=True , lowerCAmelCase : Optional[Any]=True , lowerCAmelCase : Dict=99 , lowerCAmelCase : Any=32 , lowerCAmelCase : int=5 , lowerCAmelCase : Tuple=4 , lowerCAmelCase : Optional[Any]=37 , lowerCAmelCase : str="gelu" , lowerCAmelCase : str=0.1 , lowerCAmelCase : str=0.1 , lowerCAmelCase : Any=5_12 , lowerCAmelCase : Optional[Any]=16 , lowerCAmelCase : Dict=2 , lowerCAmelCase : Tuple=0.02 , lowerCAmelCase : Optional[int]=4 , ) -> List[Any]: """simple docstring""" __lowerCAmelCase : int = parent __lowerCAmelCase : Dict = batch_size __lowerCAmelCase : Dict = seq_length __lowerCAmelCase : Union[str, Any] = is_training __lowerCAmelCase : List[Any] = use_attention_mask __lowerCAmelCase : List[Any] = use_token_type_ids __lowerCAmelCase : Optional[int] = use_labels __lowerCAmelCase : str = vocab_size __lowerCAmelCase : Any = hidden_size __lowerCAmelCase : Optional[int] = num_hidden_layers __lowerCAmelCase : Optional[int] = num_attention_heads __lowerCAmelCase : Dict = intermediate_size __lowerCAmelCase : Tuple = hidden_act __lowerCAmelCase : Dict = hidden_dropout_prob __lowerCAmelCase : Any = attention_probs_dropout_prob __lowerCAmelCase : Union[str, Any] = max_position_embeddings __lowerCAmelCase : int = type_vocab_size __lowerCAmelCase : Tuple = type_sequence_label_size __lowerCAmelCase : int = initializer_range __lowerCAmelCase : Optional[int] = num_choices def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Dict: """simple docstring""" __lowerCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCAmelCase : Dict = None if self.use_attention_mask: __lowerCAmelCase : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCAmelCase : Union[str, Any] = None if self.use_token_type_ids: __lowerCAmelCase : str = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCAmelCase : List[str] = BertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Optional[Any]: """simple docstring""" __lowerCAmelCase : Any = self.prepare_config_and_inputs() __lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase : List[str] = config_and_inputs __lowerCAmelCase : Any = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Tuple: """simple docstring""" __lowerCAmelCase : List[str] = self.prepare_config_and_inputs() __lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase : Dict = config_and_inputs __lowerCAmelCase : Any = True __lowerCAmelCase : str = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __lowerCAmelCase : int = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, attention_mask, encoder_hidden_states, encoder_attention_mask, ) @require_flax class SCREAMING_SNAKE_CASE ( a_ , unittest.TestCase ): """simple docstring""" lowerCamelCase : int =True lowerCamelCase : Any =( ( FlaxBertModel, FlaxBertForPreTraining, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForQuestionAnswering, FlaxBertForNextSentencePrediction, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertForQuestionAnswering, ) if is_flax_available() else () ) def SCREAMING_SNAKE_CASE ( self : str ) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase : Union[str, Any] = FlaxBertModelTester(self ) @slow def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: """simple docstring""" __lowerCAmelCase : int = FlaxBertModel.from_pretrained("""bert-base-cased""" ) __lowerCAmelCase : str = model(np.ones((1, 1) ) ) self.assertIsNotNone(lowerCAmelCase )
139
1
import contextlib import csv import json import os import sqlitea import tarfile import textwrap import zipfile import pyarrow as pa import pyarrow.parquet as pq import pytest import datasets import datasets.config @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( ) -> List[str]: """simple docstring""" A__ = 1_0 A__ = datasets.Features( { """tokens""": datasets.Sequence(datasets.Value("""string""" ) ), """labels""": datasets.Sequence(datasets.ClassLabel(names=["""negative""", """positive"""] ) ), """answers""": datasets.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), """id""": datasets.Value("""int64""" ), } ) A__ = datasets.Dataset.from_dict( { """tokens""": [["""foo"""] * 5] * n, """labels""": [[1] * 5] * n, """answers""": [{"""answer_start""": [9_7], """text""": ["""1976"""]}] * 1_0, """id""": list(range(__a ) ), } , features=__a , ) return dataset @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :Tuple ) -> Union[str, Any]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """file.arrow""" ) dataset.map(cache_file_name=__a ) return filename # FILE_CONTENT + files A : Union[str, Any] = '''\ Text data. Second line of data.''' @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Union[str, Any] ) -> List[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt""" A__ = FILE_CONTENT with open(__a , """w""" ) as f: f.write(__a ) return filename @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] ) -> Dict: """simple docstring""" import bza A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.bz2""" A__ = bytes(__a , """utf-8""" ) with bza.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :int ) -> int: """simple docstring""" import gzip A__ = str(tmp_path_factory.mktemp("""data""" ) / """file.txt.gz""" ) A__ = bytes(__a , """utf-8""" ) with gzip.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :str ) -> str: """simple docstring""" if datasets.config.LZ4_AVAILABLE: import lza.frame A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.lz4""" A__ = bytes(__a , """utf-8""" ) with lza.frame.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] , __a :Dict ) -> Optional[Any]: """simple docstring""" if datasets.config.PY7ZR_AVAILABLE: import pyazr A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.7z""" with pyazr.SevenZipFile(__a , """w""" ) as archive: archive.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] , __a :Optional[int] ) -> Union[str, Any]: """simple docstring""" import tarfile A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.tar""" with tarfile.TarFile(__a , """w""" ) as f: f.add(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Union[str, Any] ) -> Union[str, Any]: """simple docstring""" import lzma A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.xz""" A__ = bytes(__a , """utf-8""" ) with lzma.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[str] , __a :Optional[Any] ) -> Optional[int]: """simple docstring""" import zipfile A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] ) -> int: """simple docstring""" if datasets.config.ZSTANDARD_AVAILABLE: import zstandard as zstd A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.zst""" A__ = bytes(__a , """utf-8""" ) with zstd.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple ) -> int: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """file.xml""" A__ = textwrap.dedent( """\ <?xml version=\"1.0\" encoding=\"UTF-8\" ?> <tmx version=\"1.4\"> <header segtype=\"sentence\" srclang=\"ca\" /> <body> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 1</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 1</seg></tuv> </tu> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 2</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 2</seg></tuv> </tu> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 3</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 3</seg></tuv> </tu> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 4</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 4</seg></tuv> </tu> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 5</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 5</seg></tuv> </tu> </body> </tmx>""" ) with open(__a , """w""" ) as f: f.write(__a ) return filename A : Tuple = [ {'''col_1''': '''0''', '''col_2''': 0, '''col_3''': 0.0}, {'''col_1''': '''1''', '''col_2''': 1, '''col_3''': 1.0}, {'''col_1''': '''2''', '''col_2''': 2, '''col_3''': 2.0}, {'''col_1''': '''3''', '''col_2''': 3, '''col_3''': 3.0}, ] A : Optional[int] = [ {'''col_1''': '''4''', '''col_2''': 4, '''col_3''': 4.0}, {'''col_1''': '''5''', '''col_2''': 5, '''col_3''': 5.0}, ] A : Tuple = { '''col_1''': ['''0''', '''1''', '''2''', '''3'''], '''col_2''': [0, 1, 2, 3], '''col_3''': [0.0, 1.0, 2.0, 3.0], } A : Optional[Any] = [ {'''col_3''': 0.0, '''col_1''': '''0''', '''col_2''': 0}, {'''col_3''': 1.0, '''col_1''': '''1''', '''col_2''': 1}, ] A : Tuple = [ {'''col_1''': '''s0''', '''col_2''': 0, '''col_3''': 0.0}, {'''col_1''': '''s1''', '''col_2''': 1, '''col_3''': 1.0}, {'''col_1''': '''s2''', '''col_2''': 2, '''col_3''': 2.0}, {'''col_1''': '''s3''', '''col_2''': 3, '''col_3''': 3.0}, ] @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( ) -> Tuple: """simple docstring""" return DATA_DICT_OF_LISTS @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Any ) -> Union[str, Any]: """simple docstring""" A__ = datasets.Dataset.from_dict(__a ) A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.arrow""" ) dataset.map(cache_file_name=__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Dict ) -> Dict: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.sqlite""" ) with contextlib.closing(sqlitea.connect(__a ) ) as con: A__ = con.cursor() cur.execute("""CREATE TABLE dataset(col_1 text, col_2 int, col_3 real)""" ) for item in DATA: cur.execute("""INSERT INTO dataset(col_1, col_2, col_3) VALUES (?, ?, ?)""" , tuple(item.values() ) ) con.commit() return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Optional[int]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.csv""" ) with open(__a , """w""" , newline="""""" ) as f: A__ = csv.DictWriter(__a , fieldnames=["""col_1""", """col_2""", """col_3"""] ) writer.writeheader() for item in DATA: writer.writerow(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Optional[int]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.csv""" ) with open(__a , """w""" , newline="""""" ) as f: A__ = csv.DictWriter(__a , fieldnames=["""col_1""", """col_2""", """col_3"""] ) writer.writeheader() for item in DATA: writer.writerow(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[int] , __a :Dict ) -> Any: """simple docstring""" import bza A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.bz2""" with open(__a , """rb""" ) as f: A__ = f.read() # data = bytes(FILE_CONTENT, "utf-8") with bza.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] , __a :int , __a :List[str] ) -> List[str]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) f.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Union[str, Any] , __a :Optional[Any] , __a :Any ) -> Union[str, Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(csv_path.replace(""".csv""" , """.CSV""" ) ) ) f.write(__a , arcname=os.path.basename(csva_path.replace(""".csv""" , """.CSV""" ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Dict , __a :List[Any] , __a :Optional[int] ) -> int: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.csv.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple ) -> Any: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.parquet""" ) A__ = pa.schema( { """col_1""": pa.string(), """col_2""": pa.intaa(), """col_3""": pa.floataa(), } ) with open(__a , """wb""" ) as f: A__ = pq.ParquetWriter(__a , schema=__a ) A__ = pa.Table.from_pydict({k: [DATA[i][k] for i in range(len(__a ) )] for k in DATA[0]} , schema=__a ) writer.write_table(__a ) writer.close() return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Any ) -> Union[str, Any]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.json""" ) A__ = {"""data""": DATA} with open(__a , """w""" ) as f: json.dump(__a , __a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :int ) -> Any: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.json""" ) A__ = {"""data""": DATA_DICT_OF_LISTS} with open(__a , """w""" ) as f: json.dump(__a , __a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[int] ) -> Union[str, Any]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl""" ) with open(__a , """w""" ) as f: for item in DATA: f.write(json.dumps(__a ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple ) -> Tuple: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.jsonl""" ) with open(__a , """w""" ) as f: for item in DATA: f.write(json.dumps(__a ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] ) -> Dict: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset_312.jsonl""" ) with open(__a , """w""" ) as f: for item in DATA_312: f.write(json.dumps(__a ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Tuple: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset-str.jsonl""" ) with open(__a , """w""" ) as f: for item in DATA_STR: f.write(json.dumps(__a ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] , __a :str ) -> Optional[Any]: """simple docstring""" import gzip A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.txt.gz""" ) with open(__a , """rb""" ) as orig_file: with gzip.open(__a , """wb""" ) as zipped_file: zipped_file.writelines(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :int ) -> Optional[int]: """simple docstring""" import gzip A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.gz""" ) with open(__a , """rb""" ) as orig_file: with gzip.open(__a , """wb""" ) as zipped_file: zipped_file.writelines(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Any , __a :Dict , __a :int ) -> Optional[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) f.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] , __a :List[Any] , __a :Any , __a :str ) -> Dict: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_nested.jsonl.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.join("""nested""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :int , __a :Tuple , __a :str ) -> Any: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.jsonl.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :Optional[int] , __a :Tuple ) -> List[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.tar""" with tarfile.TarFile(__a , """w""" ) as f: f.add(__a , arcname=os.path.basename(__a ) ) f.add(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :str , __a :Dict , __a :str , __a :Union[str, Any] ) -> Optional[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_nested.jsonl.tar""" with tarfile.TarFile(__a , """w""" ) as f: f.add(__a , arcname=os.path.join("""nested""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Union[str, Any] ) -> Optional[int]: """simple docstring""" A__ = ["""0""", """1""", """2""", """3"""] A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.txt""" ) with open(__a , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Tuple: """simple docstring""" A__ = ["""0""", """1""", """2""", """3"""] A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.txt""" ) with open(__a , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] ) -> int: """simple docstring""" A__ = ["""0""", """1""", """2""", """3"""] A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.abc""" with open(__a , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :str , __a :Any , __a :List[str] ) -> List[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.text.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) f.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Any , __a :str , __a :int ) -> int: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.text.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :Any , __a :Dict ) -> List[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.ext.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename("""unsupported.ext""" ) ) f.write(__a , arcname=os.path.basename("""unsupported_2.ext""" ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Union[str, Any]: """simple docstring""" A__ = """\n""".join(["""First""", """Second\u2029with Unicode new line""", """Third"""] ) A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset_with_unicode_new_lines.txt""" ) with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( ) -> Tuple: """simple docstring""" return os.path.join("""tests""" , """features""" , """data""" , """test_image_rgb.jpg""" ) @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( ) -> Dict: """simple docstring""" return os.path.join("""tests""" , """features""" , """data""" , """test_audio_44100.wav""" ) @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :int ) -> Tuple: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.img.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) f.write(__a , arcname=os.path.basename(__a ).replace(""".jpg""" , """2.jpg""" ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Dict ) -> Dict: """simple docstring""" A__ = tmp_path_factory.mktemp("""data_dir""" ) (data_dir / "subdir").mkdir() with open(data_dir / """subdir""" / """train.txt""" , """w""" ) as f: f.write("""foo\n""" * 1_0 ) with open(data_dir / """subdir""" / """test.txt""" , """w""" ) as f: f.write("""bar\n""" * 1_0 ) # hidden file with open(data_dir / """subdir""" / """.test.txt""" , """w""" ) as f: f.write("""bar\n""" * 1_0 ) # hidden directory (data_dir / ".subdir").mkdir() with open(data_dir / """.subdir""" / """train.txt""" , """w""" ) as f: f.write("""foo\n""" * 1_0 ) with open(data_dir / """.subdir""" / """test.txt""" , """w""" ) as f: f.write("""bar\n""" * 1_0 ) return data_dir
274
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 1: raise ValueError( F'{duplicate_key} is present several times in the documentation table of content at ' """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """ """others.""" ) # Only add this once new_doc.append({"""local""": duplicate_key, """title""": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
1
from ..utils import DummyObject, requires_backends class __a ( metaclass=__UpperCamelCase ): __lowercase : List[str] = ['onnx'] def __init__( self , *lowerCAmelCase__ , **lowerCAmelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ['onnx'] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls , *lowerCAmelCase__ , **lowerCAmelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['onnx'] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls , *lowerCAmelCase__ , **lowerCAmelCase__ ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['onnx'] )
288
def snake_case_ ( snake_case ) -> list[int]: lowercase__: Dict = [0 for i in range(len(snake_case ) )] # initialize interval's left pointer and right pointer lowercase__ , lowercase__: Union[str, Any] = 0, 0 for i in range(1 , len(snake_case ) ): # case when current index is inside the interval if i <= right_pointer: lowercase__: List[Any] = min(right_pointer - i + 1 , z_result[i - left_pointer] ) lowercase__: List[str] = min_edge while go_next(snake_case , snake_case , snake_case ): z_result[i] += 1 # if new index's result gives us more right interval, # we've to update left_pointer and right_pointer if i + z_result[i] - 1 > right_pointer: lowercase__ , lowercase__: List[Any] = i, i + z_result[i] - 1 return z_result def snake_case_ ( snake_case , snake_case , snake_case ) -> bool: return i + z_result[i] < len(snake_case ) and s[z_result[i]] == s[i + z_result[i]] def snake_case_ ( snake_case , snake_case ) -> int: lowercase__: Tuple = 0 # concatenate 'pattern' and 'input_str' and call z_function # with concatenated string lowercase__: Any = z_function(pattern + input_str ) for val in z_result: # if value is greater then length of the pattern string # that means this index is starting position of substring # which is equal to pattern string if val >= len(snake_case ): answer += 1 return answer if __name__ == "__main__": import doctest doctest.testmod()
288
1
import re from filelock import FileLock try: import nltk snake_case__ : Any = True except (ImportError, ModuleNotFoundError): snake_case__ : int = False if NLTK_AVAILABLE: with FileLock('.lock') as lock: nltk.download('punkt', quiet=True) def _a ( lowerCamelCase: str ) -> str: '''simple docstring''' 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 ) )
117
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available snake_case__ : Optional[Any] = {'configuration_ibert': ['IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'IBertConfig', 'IBertOnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case__ : List[str] = [ 'IBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'IBertForMaskedLM', 'IBertForMultipleChoice', 'IBertForQuestionAnswering', 'IBertForSequenceClassification', 'IBertForTokenClassification', 'IBertModel', 'IBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ibert import ( IBERT_PRETRAINED_MODEL_ARCHIVE_LIST, IBertForMaskedLM, IBertForMultipleChoice, IBertForQuestionAnswering, IBertForSequenceClassification, IBertForTokenClassification, IBertModel, IBertPreTrainedModel, ) else: import sys snake_case__ : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
117
1
'''simple docstring''' from __future__ import annotations import pandas as pd def snake_case_ (UpperCamelCase : list[int] , UpperCamelCase : list[int] , UpperCamelCase : int ): '''simple docstring''' _a = [0] * no_of_processes _a = [0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(UpperCamelCase ): _a = burst_time[i] _a = 0 _a = 0 _a = 9_9999_9999 _a = 0 _a = False # Process until all processes are completed while complete != no_of_processes: for j in range(UpperCamelCase ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: _a = remaining_time[j] _a = j _a = True if not check: increment_time += 1 continue remaining_time[short] -= 1 _a = remaining_time[short] if minm == 0: _a = 9_9999_9999 if remaining_time[short] == 0: complete += 1 _a = False # Find finish time of current process _a = increment_time + 1 # Calculate waiting time _a = finish_time - arrival_time[short] _a = finar - burst_time[short] if waiting_time[short] < 0: _a = 0 # Increment time increment_time += 1 return waiting_time def snake_case_ (UpperCamelCase : list[int] , UpperCamelCase : int , UpperCamelCase : list[int] ): '''simple docstring''' _a = [0] * no_of_processes for i in range(UpperCamelCase ): _a = burst_time[i] + waiting_time[i] return turn_around_time def snake_case_ (UpperCamelCase : list[int] , UpperCamelCase : list[int] , UpperCamelCase : int ): '''simple docstring''' _a = 0 _a = 0 for i in range(UpperCamelCase ): _a = total_waiting_time + waiting_time[i] _a = total_turn_around_time + turn_around_time[i] print(f'Average waiting time = {total_waiting_time / no_of_processes:.5f}' ) print('''Average turn around time =''' , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print('Enter how many process you want to analyze') _snake_case : Any = int(input()) _snake_case : Union[str, Any] = [0] * no_of_processes _snake_case : Union[str, Any] = [0] * no_of_processes _snake_case : Tuple = list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print('Enter the arrival time and burst time for process:--' + str(i + 1)) _snake_case , _snake_case : Any = map(int, input().split()) _snake_case : Dict = calculate_waitingtime(arrival_time, burst_time, no_of_processes) _snake_case : Dict = burst_time _snake_case : List[Any] = no_of_processes _snake_case : Optional[Any] = waiting_time _snake_case : str = calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) _snake_case : Optional[Any] = pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ 'Process', 'BurstTime', 'ArrivalTime', 'WaitingTime', 'TurnAroundTime', ], ) # Printing the dataFrame pd.set_option('display.max_rows', fcfs.shape[0] + 1) print(fcfs)
179
'''simple docstring''' import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, AutoConfig, AutoFeatureExtractor, WavaVecaConfig, WavaVecaFeatureExtractor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 _snake_case : int = get_tests_dir('fixtures') _snake_case : Tuple = get_tests_dir('fixtures/dummy_feature_extractor_config.json') _snake_case : Optional[int] = get_tests_dir('fixtures/dummy-config.json') class A ( unittest.TestCase ): def __lowerCAmelCase ( self : int ) -> List[Any]: """simple docstring""" _a = 0 def __lowerCAmelCase ( self : List[str] ) -> int: """simple docstring""" _a = AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(lowerCAmelCase_ , lowerCAmelCase_ ) def __lowerCAmelCase ( self : str ) -> Tuple: """simple docstring""" _a = AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ ) self.assertIsInstance(lowerCAmelCase_ , lowerCAmelCase_ ) def __lowerCAmelCase ( self : List[str] ) -> Any: """simple docstring""" with tempfile.TemporaryDirectory() as tmpdirname: _a = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally _a = AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ ).to_dict() config_dict.pop('''feature_extractor_type''' ) _a = WavaVecaFeatureExtractor(**lowerCAmelCase_ ) # save in new folder model_config.save_pretrained(lowerCAmelCase_ ) config.save_pretrained(lowerCAmelCase_ ) _a = AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ ) # make sure private variable is not incorrectly saved _a = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(lowerCAmelCase_ , lowerCAmelCase_ ) def __lowerCAmelCase ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" _a = AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ ) self.assertIsInstance(lowerCAmelCase_ , lowerCAmelCase_ ) def __lowerCAmelCase ( self : Optional[int] ) -> str: """simple docstring""" with self.assertRaisesRegex( lowerCAmelCase_ , '''bert-base is not a local folder and is not a valid model identifier''' ): _a = AutoFeatureExtractor.from_pretrained('''bert-base''' ) def __lowerCAmelCase ( self : Tuple ) -> Union[str, Any]: """simple docstring""" with self.assertRaisesRegex( lowerCAmelCase_ , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): _a = AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ , revision='''aaaaaa''' ) def __lowerCAmelCase ( self : Any ) -> Dict: """simple docstring""" with self.assertRaisesRegex( lowerCAmelCase_ , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): _a = AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model''' ) def __lowerCAmelCase ( self : List[Any] ) -> Any: """simple docstring""" with self.assertRaises(lowerCAmelCase_ ): _a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(lowerCAmelCase_ ): _a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=lowerCAmelCase_ ) _a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=lowerCAmelCase_ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCAmelCase_ ) _a = AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ , trust_remote_code=lowerCAmelCase_ ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) def __lowerCAmelCase ( self : int ) -> Optional[Any]: """simple docstring""" try: AutoConfig.register('''custom''' , lowerCAmelCase_ ) AutoFeatureExtractor.register(lowerCAmelCase_ , lowerCAmelCase_ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCAmelCase_ ): AutoFeatureExtractor.register(lowerCAmelCase_ , lowerCAmelCase_ ) # Now that the config is registered, it can be used as any other config with the auto-API _a = CustomFeatureExtractor.from_pretrained(lowerCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCAmelCase_ ) _a = AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ ) self.assertIsInstance(lowerCAmelCase_ , lowerCAmelCase_ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] def __lowerCAmelCase ( self : Optional[int] ) -> Any: """simple docstring""" class A ( _a ): lowercase_ = True try: AutoConfig.register('''custom''' , lowerCAmelCase_ ) AutoFeatureExtractor.register(lowerCAmelCase_ , lowerCAmelCase_ ) # If remote code is not set, the default is to use local _a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(feature_extractor.is_local ) # If remote code is disabled, we load the local one. _a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=lowerCAmelCase_ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub _a = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=lowerCAmelCase_ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(not hasattr(lowerCAmelCase_ , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
179
1
import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) __lowerCamelCase = logging.getLogger() def UpperCamelCase ( ): snake_case : Any = argparse.ArgumentParser() parser.add_argument("-f" ) snake_case : Tuple = parser.parse_args() return args.f class UpperCAmelCase ( A_ ): def _SCREAMING_SNAKE_CASE (self : str ) -> None: '''simple docstring''' snake_case : int = logging.StreamHandler(sys.stdout ) logger.addHandler(snake_case__ ) def _SCREAMING_SNAKE_CASE (self : int , snake_case__ : Optional[int] ) -> str: '''simple docstring''' snake_case : int = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 , "run_glue_deebert.py" ) with patch.object(snake_case__ , "argv" , snake_case__ ): snake_case : List[Any] = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(snake_case__ , 0.666 ) @slow @require_torch_non_multi_gpu def _SCREAMING_SNAKE_CASE (self : Any ) -> List[Any]: '''simple docstring''' snake_case : Optional[Any] = "\n --model_type roberta\n --model_name_or_path roberta-base\n --task_name MRPC\n --do_train\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --max_seq_length 128\n --per_gpu_eval_batch_size=1\n --per_gpu_train_batch_size=8\n --learning_rate 2e-4\n --num_train_epochs 3\n --overwrite_output_dir\n --seed 42\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --save_steps 0\n --overwrite_cache\n --eval_after_first_stage\n ".split() self.run_and_check(snake_case__ ) snake_case : int = "\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --eval_each_highway\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n ".split() self.run_and_check(snake_case__ ) snake_case : int = "\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --early_exit_entropy 0.1\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n ".split() self.run_and_check(snake_case__ )
59
import math import numpy as np import qiskit from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute def UpperCAmelCase__ ( _A : int = 3 ): '''simple docstring''' if isinstance(_A , _A ): raise TypeError('''number of qubits must be a integer.''' ) if number_of_qubits <= 0: raise ValueError('''number of qubits must be > 0.''' ) if math.floor(_A ) != number_of_qubits: raise ValueError('''number of qubits must be exact integer.''' ) if number_of_qubits > 10: raise ValueError('''number of qubits too large to simulate(>10).''' ) a__ =QuantumRegister(_A , '''qr''' ) a__ =ClassicalRegister(_A , '''cr''' ) a__ =QuantumCircuit(_A , _A ) a__ =number_of_qubits for i in range(_A ): quantum_circuit.h(number_of_qubits - i - 1 ) counter -= 1 for j in range(_A ): quantum_circuit.cp(np.pi / 2 ** (counter - j) , _A , _A ) for k in range(number_of_qubits // 2 ): quantum_circuit.swap(_A , number_of_qubits - k - 1 ) # measure all the qubits quantum_circuit.measure(_A , _A ) # simulate with 10000 shots a__ =Aer.get_backend('''qasm_simulator''' ) a__ =execute(_A , _A , shots=1_00_00 ) return job.result().get_counts(_A ) if __name__ == "__main__": print( f"""Total count for quantum fourier transform state is: \ {quantum_fourier_transform(3)}""" )
188
0
'''simple docstring''' from __future__ import annotations from PIL import Image # Define glider example __a = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] # Define blinker example __a = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __snake_case( _lowerCAmelCase ) -> list[list[int]]: snake_case__ : Optional[int] = [] for i in range(len(_lowerCAmelCase ) ): snake_case__ : Any = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours snake_case__ : int = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(_lowerCAmelCase ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(_lowerCAmelCase ) - 1: neighbour_count += cells[i + 1][j] if i < len(_lowerCAmelCase ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. snake_case__ : Dict = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(_lowerCAmelCase ) return next_generation def __snake_case( _lowerCAmelCase , _lowerCAmelCase ) -> list[Image.Image]: snake_case__ : Optional[Any] = [] for _ in range(_lowerCAmelCase ): # Create output image snake_case__ : List[str] = Image.new("""RGB""" , (len(cells[0] ), len(_lowerCAmelCase )) ) snake_case__ : Dict = img.load() # Save cells to image for x in range(len(_lowerCAmelCase ) ): for y in range(len(cells[0] ) ): snake_case__ : Dict = 255 - cells[y][x] * 255 snake_case__ : Union[str, Any] = (colour, colour, colour) # Save image images.append(_lowerCAmelCase ) snake_case__ : Optional[Any] = new_generation(_lowerCAmelCase ) return images if __name__ == "__main__": __a = generate_images(GLIDER, 16) images[0].save("out.gif", save_all=True, append_images=images[1:])
43
'''simple docstring''' def __snake_case( _lowerCAmelCase ) -> int: if not isinstance(_lowerCAmelCase , _lowerCAmelCase ): raise TypeError("""Input value must be an 'int' type""" ) snake_case__ : List[str] = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
43
1
'''simple docstring''' from __future__ import annotations import math from collections.abc import Callable def A_ ( snake_case , snake_case , snake_case , snake_case = 100 , ): SCREAMING_SNAKE_CASE:Any = x_start SCREAMING_SNAKE_CASE:int = fnc(snake_case ) SCREAMING_SNAKE_CASE:int = 0.0 for _ in range(snake_case ): # Approximates curve as a sequence of linear lines and sums their length SCREAMING_SNAKE_CASE:Optional[int] = (x_end - x_start) / steps + xa SCREAMING_SNAKE_CASE:int = fnc(snake_case ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step SCREAMING_SNAKE_CASE:Tuple = xa SCREAMING_SNAKE_CASE:List[Any] = fxa return length if __name__ == "__main__": def A_ ( snake_case ): return math.sin(10 * x ) print("f(x) = sin(10 * x)") print("The length of the curve from x = -10 to x = 10 is:") A_ = 10 while i <= 10_00_00: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
139
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class _snake_case ( _a , unittest.TestCase ): _A : str = CTRLTokenizer _A : List[str] = False _A : int = False def __UpperCamelCase ( self : Tuple ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE:Dict = ["adapt", "re@@", "a@@", "apt", "c@@", "t", "<unk>"] SCREAMING_SNAKE_CASE:Optional[Any] = dict(zip(SCREAMING_SNAKE_CASE__ ,range(len(SCREAMING_SNAKE_CASE__ ) ) ) ) SCREAMING_SNAKE_CASE:str = ["#version: 0.2", "a p", "ap t</w>", "r e", "a d", "ad apt</w>", ""] SCREAMING_SNAKE_CASE:Union[str, Any] = {"unk_token": "<unk>"} SCREAMING_SNAKE_CASE:Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE:Union[str, Any] = 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(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 : Union[str, Any] ,**SCREAMING_SNAKE_CASE__ : Any ): kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname ,**SCREAMING_SNAKE_CASE__ ) def __UpperCamelCase ( self : int ,SCREAMING_SNAKE_CASE__ : Any ): SCREAMING_SNAKE_CASE:Optional[Any] = "adapt react readapt apt" SCREAMING_SNAKE_CASE:Tuple = "adapt react readapt apt" return input_text, output_text def __UpperCamelCase ( self : Tuple ): SCREAMING_SNAKE_CASE:List[str] = CTRLTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) SCREAMING_SNAKE_CASE:Any = "adapt react readapt apt" SCREAMING_SNAKE_CASE:Any = "adapt re@@ a@@ c@@ t re@@ adapt apt".split() SCREAMING_SNAKE_CASE:Union[str, Any] = tokenizer.tokenize(SCREAMING_SNAKE_CASE__ ) self.assertListEqual(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Union[str, Any] = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE:Optional[int] = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) ,SCREAMING_SNAKE_CASE__ )
139
1
from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging __A = logging.get_logger(__name__) __A = { 'huggingface/time-series-transformer-tourism-monthly': ( 'https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json' ), # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer } class SCREAMING_SNAKE_CASE ( snake_case ): """simple docstring""" A_ = "time_series_transformer" A_ = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self: int , __A: Optional[int] = None , __A: Optional[int] = None , __A: str = "student_t" , __A: str = "nll" , __A: int = 1 , __A: List[int] = [1, 2, 3, 4, 5, 6, 7] , __A: Optional[Union[str, bool]] = "mean" , __A: int = 0 , __A: int = 0 , __A: int = 0 , __A: int = 0 , __A: Optional[List[int]] = None , __A: Optional[List[int]] = None , __A: int = 32 , __A: int = 32 , __A: int = 2 , __A: int = 2 , __A: int = 2 , __A: int = 2 , __A: bool = True , __A: str = "gelu" , __A: int = 64 , __A: float = 0.1 , __A: float = 0.1 , __A: float = 0.1 , __A: float = 0.1 , __A: float = 0.1 , __A: int = 1_00 , __A: float = 0.02 , __A: Union[str, Any]=True , **__A: Dict , ) -> Optional[int]: # time series specific configuration _A = prediction_length _A = context_length or prediction_length _A = distribution_output _A = loss _A = input_size _A = num_time_features _A = lags_sequence _A = scaling _A = num_dynamic_real_features _A = num_static_real_features _A = num_static_categorical_features if cardinality and num_static_categorical_features > 0: if len(__A ) != num_static_categorical_features: raise ValueError( '''The cardinality should be a list of the same length as `num_static_categorical_features`''' ) _A = cardinality else: _A = [0] if embedding_dimension and num_static_categorical_features > 0: if len(__A ) != num_static_categorical_features: raise ValueError( '''The embedding dimension should be a list of the same length as `num_static_categorical_features`''' ) _A = embedding_dimension else: _A = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] _A = num_parallel_samples # Transformer architecture configuration _A = input_size * len(__A ) + self._number_of_features _A = d_model _A = encoder_attention_heads _A = decoder_attention_heads _A = encoder_ffn_dim _A = decoder_ffn_dim _A = encoder_layers _A = decoder_layers _A = dropout _A = attention_dropout _A = activation_dropout _A = encoder_layerdrop _A = decoder_layerdrop _A = activation_function _A = init_std _A = use_cache super().__init__(is_encoder_decoder=__A , **__A ) @property def __A ( self: List[str] ) -> int: return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
75
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem __A = importlib.util.find_spec('s3fs') is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 __A = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def __A ( _lowercase ): '''simple docstring''' if "://" in dataset_path: _A = dataset_path.split('''://''' )[1] return dataset_path def __A ( _lowercase ): '''simple docstring''' if fs is not None and fs.protocol != "file": return True else: return False def __A ( _lowercase , _lowercase , _lowercase ): '''simple docstring''' _A = not is_remote_filesystem(_lowercase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(_lowercase ) , fs._strip_protocol(_lowercase ) ) else: fs.mv(_lowercase , _lowercase , recursive=_lowercase ) def __A ( ): '''simple docstring''' if hasattr(fsspec.asyn , '''reset_lock''' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: _A = None _A = None _A = threading.Lock()
75
1
"""simple docstring""" import json import os from pathlib import Path import pytest from datasets.download.download_config import DownloadConfig from datasets.download.download_manager import DownloadManager from datasets.utils.file_utils import hash_url_to_filename UpperCAmelCase__ = 'http://www.mocksite.com/file1.txt' UpperCAmelCase__ = '"text": ["foo", "foo"]' UpperCAmelCase__ = '6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8' class lowerCAmelCase__ : __a = 200 __a = {"""Content-Length""": """100"""} __a = {} def lowercase ( self : List[str] , **_lowerCamelCase : List[str] ): return [bytes(_lowerCamelCase , '''utf-8''' )] def _UpperCAmelCase ( *__lowerCamelCase : List[str] , **__lowerCamelCase : Dict ) -> Dict: return MockResponse() @pytest.mark.parametrize('''urls_type''' , [str, list, dict] ) def _UpperCAmelCase ( __lowerCamelCase : Union[str, Any] , __lowerCamelCase : List[str] , __lowerCamelCase : str ) -> int: import requests monkeypatch.setattr(__lowerCamelCase , '''request''' , __lowerCamelCase ) _snake_case = URL if issubclass(__lowerCamelCase , __lowerCamelCase ): _snake_case = url elif issubclass(__lowerCamelCase , __lowerCamelCase ): _snake_case = [url] elif issubclass(__lowerCamelCase , __lowerCamelCase ): _snake_case = {'''train''': url} _snake_case = '''dummy''' _snake_case = '''downloads''' _snake_case = tmp_path _snake_case = DownloadConfig( cache_dir=os.path.join(__lowerCamelCase , __lowerCamelCase ) , use_etag=__lowerCamelCase , ) _snake_case = DownloadManager(dataset_name=__lowerCamelCase , download_config=__lowerCamelCase ) _snake_case = dl_manager.download(__lowerCamelCase ) _snake_case = urls for downloaded_paths in [downloaded_paths]: if isinstance(__lowerCamelCase , __lowerCamelCase ): _snake_case = [downloaded_paths] _snake_case = [urls] elif isinstance(__lowerCamelCase , __lowerCamelCase ): assert "train" in downloaded_paths.keys() _snake_case = downloaded_paths.values() _snake_case = urls.values() assert downloaded_paths for downloaded_path, input_url in zip(__lowerCamelCase , __lowerCamelCase ): assert downloaded_path == dl_manager.downloaded_paths[input_url] _snake_case = Path(__lowerCamelCase ) _snake_case = downloaded_path.parts assert parts[-1] == HASH assert parts[-2] == cache_subdir assert downloaded_path.exists() _snake_case = downloaded_path.read_text() assert content == CONTENT _snake_case = downloaded_path.with_suffix('''.json''' ) assert metadata_downloaded_path.exists() _snake_case = json.loads(metadata_downloaded_path.read_text() ) assert metadata_content == {"url": URL, "etag": None} @pytest.mark.parametrize('''paths_type''' , [str, list, dict] ) def _UpperCAmelCase ( __lowerCamelCase : int , __lowerCamelCase : str , __lowerCamelCase : Optional[int] ) -> int: _snake_case = str(__lowerCamelCase ) if issubclass(__lowerCamelCase , __lowerCamelCase ): _snake_case = filename elif issubclass(__lowerCamelCase , __lowerCamelCase ): _snake_case = [filename] elif issubclass(__lowerCamelCase , __lowerCamelCase ): _snake_case = {'''train''': filename} _snake_case = '''dummy''' _snake_case = xz_file.parent _snake_case = '''extracted''' _snake_case = DownloadConfig( cache_dir=__lowerCamelCase , use_etag=__lowerCamelCase , ) _snake_case = DownloadManager(dataset_name=__lowerCamelCase , download_config=__lowerCamelCase ) _snake_case = dl_manager.extract(__lowerCamelCase ) _snake_case = paths for extracted_paths in [extracted_paths]: if isinstance(__lowerCamelCase , __lowerCamelCase ): _snake_case = [extracted_paths] _snake_case = [paths] elif isinstance(__lowerCamelCase , __lowerCamelCase ): assert "train" in extracted_paths.keys() _snake_case = extracted_paths.values() _snake_case = paths.values() assert extracted_paths for extracted_path, input_path in zip(__lowerCamelCase , __lowerCamelCase ): assert extracted_path == dl_manager.extracted_paths[input_path] _snake_case = Path(__lowerCamelCase ) _snake_case = extracted_path.parts assert parts[-1] == hash_url_to_filename(__lowerCamelCase , etag=__lowerCamelCase ) assert parts[-2] == extracted_subdir assert extracted_path.exists() _snake_case = extracted_path.read_text() _snake_case = text_file.read_text() assert extracted_file_content == expected_file_content def _UpperCAmelCase ( __lowerCamelCase : Tuple , __lowerCamelCase : List[Any] ) -> Dict: assert path.endswith('''.jsonl''' ) for num_items, line in enumerate(__lowerCamelCase , start=1 ): _snake_case = json.loads(line.decode('''utf-8''' ) ) assert item.keys() == {"col_1", "col_2", "col_3"} assert num_items == 4 @pytest.mark.parametrize('''archive_jsonl''' , ['''tar_jsonl_path''', '''zip_jsonl_path'''] ) def _UpperCAmelCase ( __lowerCamelCase : Dict , __lowerCamelCase : str ) -> Dict: _snake_case = request.getfixturevalue(__lowerCamelCase ) _snake_case = DownloadManager() for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(__lowerCamelCase ) , start=1 ): _test_jsonl(__lowerCamelCase , __lowerCamelCase ) assert num_jsonl == 2 @pytest.mark.parametrize('''archive_nested_jsonl''' , ['''tar_nested_jsonl_path''', '''zip_nested_jsonl_path'''] ) def _UpperCAmelCase ( __lowerCamelCase : str , __lowerCamelCase : List[Any] ) -> Tuple: _snake_case = request.getfixturevalue(__lowerCamelCase ) _snake_case = DownloadManager() for num_tar, (path, file) in enumerate(dl_manager.iter_archive(__lowerCamelCase ) , start=1 ): for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(__lowerCamelCase ) , start=1 ): _test_jsonl(__lowerCamelCase , __lowerCamelCase ) assert num_tar == 1 assert num_jsonl == 2 def _UpperCAmelCase ( __lowerCamelCase : Tuple ) -> List[Any]: _snake_case = DownloadManager() for num_file, file in enumerate(dl_manager.iter_files(__lowerCamelCase ) , start=1 ): assert os.path.basename(__lowerCamelCase ) == ("test.txt" if num_file == 1 else "train.txt") assert num_file == 2
288
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available 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 TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowerCAmelCase__ : def __init__( self : Dict , _lowerCamelCase : int , _lowerCamelCase : Optional[int]=3 , _lowerCamelCase : List[str]=32 , _lowerCamelCase : Optional[int]=3 , _lowerCamelCase : Dict=10 , _lowerCamelCase : Tuple=[10, 20, 30, 40] , _lowerCamelCase : int=[1, 1, 2, 1] , _lowerCamelCase : int=True , _lowerCamelCase : Optional[int]=True , _lowerCamelCase : Optional[int]="relu" , _lowerCamelCase : List[Any]=3 , _lowerCamelCase : Dict=None , ): _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(_lowerCamelCase ) def lowercase ( self : Optional[int] ): _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 lowercase ( self : Tuple ): return ResNetConfig( 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 , image_size=self.image_size , ) def lowercase ( self : List[Any] , _lowerCamelCase : List[str] , _lowerCamelCase : str , _lowerCamelCase : List[Any] ): _snake_case = TFResNetModel(config=_lowerCamelCase ) _snake_case = model(_lowerCamelCase ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowercase ( self : Dict , _lowerCamelCase : str , _lowerCamelCase : Optional[Any] , _lowerCamelCase : Tuple ): _snake_case = self.num_labels _snake_case = TFResNetForImageClassification(_lowerCamelCase ) _snake_case = model(_lowerCamelCase , labels=_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase ( self : Tuple ): _snake_case = self.prepare_config_and_inputs() _snake_case , _snake_case , _snake_case = config_and_inputs _snake_case = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class lowerCAmelCase__ ( A_ , A_ , unittest.TestCase ): __a = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () __a = ( {"""feature-extraction""": TFResNetModel, """image-classification""": TFResNetForImageClassification} if is_tf_available() else {} ) __a = False __a = False __a = False __a = False __a = False def lowercase ( self : List[Any] ): _snake_case = TFResNetModelTester(self ) _snake_case = ConfigTester(self , config_class=_lowerCamelCase , has_text_modality=_lowerCamelCase ) def lowercase ( self : Tuple ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowercase ( self : List[Any] ): return @unittest.skip(reason='''ResNet does not use inputs_embeds''' ) def lowercase ( self : Any ): pass @unittest.skip(reason='''ResNet does not support input and output embeddings''' ) def lowercase ( self : List[str] ): pass def lowercase ( self : int ): _snake_case , _snake_case = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _snake_case = model_class(_lowerCamelCase ) _snake_case = inspect.signature(model.call ) # 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] , _lowerCamelCase ) def lowercase ( self : List[str] ): _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCamelCase ) def lowercase ( self : Union[str, Any] ): def check_hidden_states_output(_lowerCamelCase : int , _lowerCamelCase : List[Any] , _lowerCamelCase : str ): _snake_case = model_class(_lowerCamelCase ) _snake_case = model(**self._prepare_for_class(_lowerCamelCase , _lowerCamelCase ) ) _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(_lowerCamelCase ) , expected_num_stages + 1 ) # ResNet'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 , _snake_case = self.model_tester.prepare_config_and_inputs_for_common() _snake_case = ['''basic''', '''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(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _snake_case = True check_hidden_states_output(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) def lowercase ( self : Union[str, Any] ): _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase ) @slow def lowercase ( self : List[str] ): for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _snake_case = TFResNetModel.from_pretrained(_lowerCamelCase ) self.assertIsNotNone(_lowerCamelCase ) def _UpperCAmelCase ( ) -> Union[str, Any]: _snake_case = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class lowerCAmelCase__ ( unittest.TestCase ): @cached_property def lowercase ( self : Dict ): return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowercase ( self : List[Any] ): _snake_case = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) _snake_case = self.default_image_processor _snake_case = prepare_img() _snake_case = image_processor(images=_lowerCamelCase , return_tensors='''tf''' ) # forward pass _snake_case = model(**_lowerCamelCase ) # verify the logits _snake_case = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , _lowerCamelCase ) _snake_case = tf.constant([-1_1.1_0_6_9, -9.7_8_7_7, -8.3_7_7_7] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , _lowerCamelCase , atol=1e-4 ) )
288
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCAmelCase : List[Any] = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase : Optional[int] = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase : Optional[int] = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase : Optional[int] = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys _lowerCAmelCase : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
340
"""simple docstring""" from decimal import Decimal, getcontext from math import ceil, factorial def lowerCamelCase_( _lowerCamelCase ) -> str: '''simple docstring''' if not isinstance(_lowerCamelCase , _lowerCamelCase ): raise TypeError("Undefined for non-integers" ) elif precision < 1: raise ValueError("Undefined for non-natural numbers" ) _lowerCamelCase : int = precision _lowerCamelCase : Dict = ceil(precision / 14 ) _lowerCamelCase : Optional[Any] = 426880 * Decimal(10005 ).sqrt() _lowerCamelCase : int = 1 _lowerCamelCase : Optional[int] = 13591409 _lowerCamelCase : int = Decimal(_lowerCamelCase ) for k in range(1 , _lowerCamelCase ): _lowerCamelCase : Union[str, Any] = factorial(6 * k ) // (factorial(3 * k ) * factorial(_lowerCamelCase ) ** 3) linear_term += 545140134 exponential_term *= -262537412640768000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": _lowerCAmelCase : Union[str, Any] = 50 print(f'''The first {n} digits of pi is: {pi(n)}''')
340
1
"""simple docstring""" def __lowercase ( snake_case_ : List[str] ,snake_case_ : List[str] ) ->Optional[Any]: '''simple docstring''' __A : Tuple = 0 while b > 0: if b & 1: res += a a += a b >>= 1 return res def __lowercase ( snake_case_ : Any ,snake_case_ : int ,snake_case_ : str ) ->Dict: '''simple docstring''' __A : str = 0 while b > 0: if b & 1: __A : Tuple = ((res % c) + (a % c)) % c a += a b >>= 1 return res
179
"""simple docstring""" import requests a_ = """""" # <-- Put your OpenWeatherMap appid here! a_ = """https://api.openweathermap.org/data/2.5/""" def __lowercase ( snake_case_ : str = "Chicago" ,snake_case_ : str = APPID ) ->dict: '''simple docstring''' return requests.get(URL_BASE + '''weather''' ,params=locals() ).json() def __lowercase ( snake_case_ : str = "Kolkata, India" ,snake_case_ : str = APPID ) ->dict: '''simple docstring''' return requests.get(URL_BASE + '''forecast''' ,params=locals() ).json() def __lowercase ( snake_case_ : float = 55.68 ,snake_case_ : float = 12.57 ,snake_case_ : str = APPID ) ->dict: '''simple docstring''' return requests.get(URL_BASE + '''onecall''' ,params=locals() ).json() if __name__ == "__main__": from pprint import pprint while True: a_ = input("""Enter a location:""").strip() if location: pprint(current_weather(location)) else: break
179
1
'''simple docstring''' import unittest from knapsack import knapsack as k class A__ ( unittest.TestCase ): """simple docstring""" def _lowerCAmelCase ( self : Optional[Any] ) -> int: """simple docstring""" _UpperCAmelCase : Dict = 0 _UpperCAmelCase : List[str] = [0] _UpperCAmelCase : Optional[Any] = [0] _UpperCAmelCase : List[str] = len(lowerCAmelCase__ ) self.assertEqual(k.knapsack(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) , 0 ) _UpperCAmelCase : List[Any] = [6_0] _UpperCAmelCase : Optional[Any] = [1_0] _UpperCAmelCase : int = len(lowerCAmelCase__ ) self.assertEqual(k.knapsack(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) , 0 ) def _lowerCAmelCase ( self : Union[str, Any] ) -> List[str]: """simple docstring""" _UpperCAmelCase : int = 3 _UpperCAmelCase : Optional[Any] = [1, 2, 3] _UpperCAmelCase : List[Any] = [3, 2, 1] _UpperCAmelCase : Optional[Any] = len(lowerCAmelCase__ ) self.assertEqual(k.knapsack(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) , 5 ) def _lowerCAmelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" _UpperCAmelCase : Optional[Any] = 5_0 _UpperCAmelCase : Union[str, Any] = [6_0, 1_0_0, 1_2_0] _UpperCAmelCase : Union[str, Any] = [1_0, 2_0, 3_0] _UpperCAmelCase : List[str] = len(lowerCAmelCase__ ) self.assertEqual(k.knapsack(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) , 2_2_0 ) if __name__ == "__main__": unittest.main()
350
'''simple docstring''' from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass __a = (3, 9, -11, 0, 7, 5, 1, -1) __a = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class A__ : """simple docstring""" UpperCamelCase_ : int UpperCamelCase_ : Node | None class A__ : """simple docstring""" def __init__( self : Dict , lowerCAmelCase__ : Iterable[int] ) -> None: """simple docstring""" _UpperCAmelCase : Node | None = None for i in sorted(lowerCAmelCase__ , reverse=lowerCAmelCase__ ): _UpperCAmelCase : str = Node(lowerCAmelCase__ , self.head ) def __iter__( self : int ) -> Iterator[int]: """simple docstring""" _UpperCAmelCase : List[Any] = self.head while node: yield node.data _UpperCAmelCase : List[str] = node.next_node def __len__( self : Any ) -> int: """simple docstring""" return sum(1 for _ in self ) def __str__( self : Union[str, Any] ) -> str: """simple docstring""" return " -> ".join([str(lowerCAmelCase__ ) for node in self] ) def __UpperCAmelCase ( a_: SortedLinkedList, a_: SortedLinkedList ): return SortedLinkedList(list(a_ ) + list(a_ ) ) if __name__ == "__main__": import doctest doctest.testmod() __a = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
17
0
class lowerCamelCase_ : '''simple docstring''' def __init__( self , __lowercase , __lowercase=None , __lowercase=None) -> int: __UpperCamelCase :Optional[Any] = data __UpperCamelCase :Union[str, Any] = previous __UpperCamelCase :List[str] = next_node def __str__( self) -> str: return f"""{self.data}""" def UpperCamelCase__ ( self) -> int: return self.data def UpperCamelCase__ ( self) -> Optional[Any]: return self.next def UpperCamelCase__ ( self) -> Dict: return self.previous class lowerCamelCase_ : '''simple docstring''' def __init__( self , __lowercase) -> Dict: __UpperCamelCase :List[Any] = head def __iter__( self) -> List[str]: return self def UpperCamelCase__ ( self) -> Optional[Any]: if not self.current: raise StopIteration else: __UpperCamelCase :str = self.current.get_data() __UpperCamelCase :Optional[int] = self.current.get_next() return value class lowerCamelCase_ : '''simple docstring''' def __init__( self) -> List[str]: __UpperCamelCase :Any = None # First node in list __UpperCamelCase :Union[str, Any] = None # Last node in list def __str__( self) -> Any: __UpperCamelCase :Optional[int] = self.head __UpperCamelCase :Union[str, Any] = [] while current is not None: nodes.append(current.get_data()) __UpperCamelCase :List[str] = current.get_next() return " ".join(str(__lowercase) for node in nodes) def __contains__( self , __lowercase) -> List[str]: __UpperCamelCase :int = self.head while current: if current.get_data() == value: return True __UpperCamelCase :List[Any] = current.get_next() return False def __iter__( self) -> Dict: return LinkedListIterator(self.head) def UpperCamelCase__ ( self) -> Dict: if self.head: return self.head.get_data() return None def UpperCamelCase__ ( self) -> Dict: if self.tail: return self.tail.get_data() return None def UpperCamelCase__ ( self , __lowercase) -> None: if self.head is None: __UpperCamelCase :Any = node __UpperCamelCase :Any = node else: self.insert_before_node(self.head , __lowercase) def UpperCamelCase__ ( self , __lowercase) -> None: if self.head is None: self.set_head(__lowercase) else: self.insert_after_node(self.tail , __lowercase) def UpperCamelCase__ ( self , __lowercase) -> None: __UpperCamelCase :Any = Node(__lowercase) if self.head is None: self.set_head(__lowercase) else: self.set_tail(__lowercase) def UpperCamelCase__ ( self , __lowercase , __lowercase) -> None: __UpperCamelCase :Tuple = node __UpperCamelCase :Dict = node.previous if node.get_previous() is None: __UpperCamelCase :Any = node_to_insert else: __UpperCamelCase :str = node_to_insert __UpperCamelCase :List[str] = node_to_insert def UpperCamelCase__ ( self , __lowercase , __lowercase) -> None: __UpperCamelCase :Union[str, Any] = node __UpperCamelCase :int = node.next if node.get_next() is None: __UpperCamelCase :Dict = node_to_insert else: __UpperCamelCase :int = node_to_insert __UpperCamelCase :int = node_to_insert def UpperCamelCase__ ( self , __lowercase , __lowercase) -> None: __UpperCamelCase :Tuple = 1 __UpperCamelCase :List[str] = Node(__lowercase) __UpperCamelCase :List[str] = self.head while node: if current_position == position: self.insert_before_node(__lowercase , __lowercase) return current_position += 1 __UpperCamelCase :List[str] = node.next self.insert_after_node(self.tail , __lowercase) def UpperCamelCase__ ( self , __lowercase) -> Node: __UpperCamelCase :List[Any] = self.head while node: if node.get_data() == item: return node __UpperCamelCase :str = node.get_next() raise Exception('''Node not found''') def UpperCamelCase__ ( self , __lowercase) -> Union[str, Any]: if (node := self.get_node(__lowercase)) is not None: if node == self.head: __UpperCamelCase :Union[str, Any] = self.head.get_next() if node == self.tail: __UpperCamelCase :Optional[int] = self.tail.get_previous() self.remove_node_pointers(__lowercase) @staticmethod def UpperCamelCase__ ( __lowercase) -> None: if node.get_next(): __UpperCamelCase :str = node.previous if node.get_previous(): __UpperCamelCase :List[str] = node.next __UpperCamelCase :int = None __UpperCamelCase :int = None def UpperCamelCase__ ( self) -> Tuple: return self.head is None def lowerCamelCase ( ): '''simple docstring''' if __name__ == "__main__": import doctest doctest.testmod()
43
def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :Tuple = [0 for i in range(len(SCREAMING_SNAKE_CASE ) )] # initialize interval's left pointer and right pointer __UpperCamelCase , __UpperCamelCase :str = 0, 0 for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): # case when current index is inside the interval if i <= right_pointer: __UpperCamelCase :Union[str, Any] = min(right_pointer - i + 1 , z_result[i - left_pointer] ) __UpperCamelCase :Tuple = min_edge while go_next(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): z_result[i] += 1 # if new index's result gives us more right interval, # we've to update left_pointer and right_pointer if i + z_result[i] - 1 > right_pointer: __UpperCamelCase , __UpperCamelCase :Union[str, Any] = i, i + z_result[i] - 1 return z_result def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' return i + z_result[i] < len(SCREAMING_SNAKE_CASE ) and s[z_result[i]] == s[i + z_result[i]] def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :List[Any] = 0 # concatenate 'pattern' and 'input_str' and call z_function # with concatenated string __UpperCamelCase :Tuple = z_function(pattern + input_str ) for val in z_result: # if value is greater then length of the pattern string # that means this index is starting position of substring # which is equal to pattern string if val >= len(SCREAMING_SNAKE_CASE ): answer += 1 return answer if __name__ == "__main__": import doctest doctest.testmod()
43
1
"""simple docstring""" import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class A_ ( __lowercase , unittest.TestCase ): """simple docstring""" __UpperCamelCase = CTRLTokenizer __UpperCamelCase = False __UpperCamelCase = False def UpperCAmelCase__ ( self :Dict ) -> int: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCAmelCase = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] UpperCAmelCase = dict(zip(snake_case_ , range(len(snake_case_ ) ) ) ) UpperCAmelCase = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] UpperCAmelCase = {'''unk_token''': '''<unk>'''} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(snake_case_ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(snake_case_ ) ) def UpperCAmelCase__ ( self :Any , **lowercase_ :Tuple ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **snake_case_ ) def UpperCAmelCase__ ( self :List[str] , lowercase_ :Tuple ) -> str: UpperCAmelCase = '''adapt react readapt apt''' UpperCAmelCase = '''adapt react readapt apt''' return input_text, output_text def UpperCAmelCase__ ( self :Union[str, Any] ) -> Dict: UpperCAmelCase = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) UpperCAmelCase = '''adapt react readapt apt''' UpperCAmelCase = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() UpperCAmelCase = tokenizer.tokenize(snake_case_ ) self.assertListEqual(snake_case_ , snake_case_ ) UpperCAmelCase = tokens + [tokenizer.unk_token] UpperCAmelCase = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case_ ) , snake_case_ )
352
"""simple docstring""" import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class A_ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" __UpperCamelCase = (UniPCMultistepScheduler,) __UpperCamelCase = (("""num_inference_steps""", 25),) def UpperCAmelCase__ ( self :Any , **lowercase_ :Optional[Any] ) -> Dict: UpperCAmelCase = { 'num_train_timesteps': 10_00, 'beta_start': 0.0001, 'beta_end': 0.02, 'beta_schedule': 'linear', 'solver_order': 2, 'solver_type': 'bh2', } config.update(**lowercase_ ) return config def UpperCAmelCase__ ( self :Optional[Any] , lowercase_ :Optional[int]=0 , **lowercase_ :Optional[Any] ) -> List[str]: UpperCAmelCase = dict(self.forward_default_kwargs ) UpperCAmelCase = kwargs.pop('num_inference_steps' , lowercase_ ) UpperCAmelCase = self.dummy_sample UpperCAmelCase = 0.1 * sample UpperCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: UpperCAmelCase = self.get_scheduler_config(**lowercase_ ) UpperCAmelCase = scheduler_class(**lowercase_ ) scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals UpperCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowercase_ ) UpperCAmelCase = scheduler_class.from_pretrained(lowercase_ ) new_scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals UpperCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] UpperCAmelCase , UpperCAmelCase = sample, sample for t in range(lowercase_ , time_step + scheduler.config.solver_order + 1 ): UpperCAmelCase = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase = new_scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def UpperCAmelCase__ ( self :List[Any] , lowercase_ :Dict=0 , **lowercase_ :Optional[int] ) -> List[str]: UpperCAmelCase = dict(self.forward_default_kwargs ) UpperCAmelCase = kwargs.pop('num_inference_steps' , lowercase_ ) UpperCAmelCase = self.dummy_sample UpperCAmelCase = 0.1 * sample UpperCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: UpperCAmelCase = self.get_scheduler_config() UpperCAmelCase = scheduler_class(**lowercase_ ) scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals (must be after setting timesteps) UpperCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowercase_ ) UpperCAmelCase = scheduler_class.from_pretrained(lowercase_ ) # copy over dummy past residuals new_scheduler.set_timesteps(lowercase_ ) # copy over dummy past residual (must be after setting timesteps) UpperCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] UpperCAmelCase = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase = new_scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def UpperCAmelCase__ ( self :List[Any] , lowercase_ :Optional[int]=None , **lowercase_ :str ) -> Dict: if scheduler is None: UpperCAmelCase = self.scheduler_classes[0] UpperCAmelCase = self.get_scheduler_config(**lowercase_ ) UpperCAmelCase = scheduler_class(**lowercase_ ) UpperCAmelCase = self.scheduler_classes[0] UpperCAmelCase = self.get_scheduler_config(**lowercase_ ) UpperCAmelCase = scheduler_class(**lowercase_ ) UpperCAmelCase = 10 UpperCAmelCase = self.dummy_model() UpperCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(lowercase_ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase = model(lowercase_ , lowercase_ ) UpperCAmelCase = scheduler.step(lowercase_ , lowercase_ , lowercase_ ).prev_sample return sample def UpperCAmelCase__ ( self :Union[str, Any] ) -> Tuple: UpperCAmelCase = dict(self.forward_default_kwargs ) UpperCAmelCase = kwargs.pop('num_inference_steps' , lowercase_ ) for scheduler_class in self.scheduler_classes: UpperCAmelCase = self.get_scheduler_config() UpperCAmelCase = scheduler_class(**lowercase_ ) UpperCAmelCase = self.dummy_sample UpperCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(lowercase_ , 'set_timesteps' ): scheduler.set_timesteps(lowercase_ ) elif num_inference_steps is not None and not hasattr(lowercase_ , 'set_timesteps' ): UpperCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) UpperCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] UpperCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] UpperCAmelCase = scheduler.timesteps[5] UpperCAmelCase = scheduler.timesteps[6] UpperCAmelCase = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def UpperCAmelCase__ ( self :Optional[int] ) -> Optional[Any]: # make sure that iterating over schedulers with same config names gives same results # for defaults UpperCAmelCase = UniPCMultistepScheduler(**self.get_scheduler_config() ) UpperCAmelCase = self.full_loop(scheduler=lowercase_ ) UpperCAmelCase = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2464 ) < 1E-3 UpperCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) UpperCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase = self.full_loop(scheduler=lowercase_ ) UpperCAmelCase = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2464 ) < 1E-3 def UpperCAmelCase__ ( self :Optional[int] ) -> List[str]: for timesteps in [25, 50, 1_00, 9_99, 10_00]: self.check_over_configs(num_train_timesteps=lowercase_ ) def UpperCAmelCase__ ( self :int ) -> str: self.check_over_configs(thresholding=lowercase_ ) for order in [1, 2, 3]: for solver_type in ["bh1", "bh2"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=lowercase_ , prediction_type=lowercase_ , sample_max_value=lowercase_ , solver_order=lowercase_ , solver_type=lowercase_ , ) def UpperCAmelCase__ ( self :Any ) -> str: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowercase_ ) def UpperCAmelCase__ ( self :str ) -> Optional[Any]: for solver_type in ["bh1", "bh2"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=lowercase_ , solver_type=lowercase_ , prediction_type=lowercase_ , ) UpperCAmelCase = self.full_loop( solver_order=lowercase_ , solver_type=lowercase_ , prediction_type=lowercase_ , ) assert not torch.isnan(lowercase_ ).any(), "Samples have nan numbers" def UpperCAmelCase__ ( self :Dict ) -> List[Any]: self.check_over_configs(lower_order_final=lowercase_ ) self.check_over_configs(lower_order_final=lowercase_ ) def UpperCAmelCase__ ( self :Any ) -> List[Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 1_00, 9_99, 10_00]: self.check_over_forward(num_inference_steps=lowercase_ , time_step=0 ) def UpperCAmelCase__ ( self :int ) -> Union[str, Any]: UpperCAmelCase = self.full_loop() UpperCAmelCase = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2464 ) < 1E-3 def UpperCAmelCase__ ( self :Optional[int] ) -> str: UpperCAmelCase = self.full_loop(prediction_type='v_prediction' ) UpperCAmelCase = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.1014 ) < 1E-3 def UpperCAmelCase__ ( self :Tuple ) -> Tuple: UpperCAmelCase = self.scheduler_classes[0] UpperCAmelCase = self.get_scheduler_config(thresholding=lowercase_ , dynamic_thresholding_ratio=0 ) UpperCAmelCase = scheduler_class(**lowercase_ ) UpperCAmelCase = 10 UpperCAmelCase = self.dummy_model() UpperCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(lowercase_ ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase = model(lowercase_ , lowercase_ ) UpperCAmelCase = scheduler.step(lowercase_ , lowercase_ , lowercase_ ).prev_sample assert sample.dtype == torch.floataa def UpperCAmelCase__ ( self :List[Any] , **lowercase_ :Optional[Any] ) -> Dict: for scheduler_class in self.scheduler_classes: UpperCAmelCase = self.get_scheduler_config(**lowercase_ ) UpperCAmelCase = scheduler_class(**lowercase_ ) scheduler.set_timesteps(scheduler.config.num_train_timesteps ) assert len(scheduler.timesteps.unique() ) == scheduler.num_inference_steps
181
0
'''simple docstring''' from __future__ import annotations import requests a_ : List[Any] = set( """approved_at_utc approved_by author_flair_background_color author_flair_css_class author_flair_richtext author_flair_template_id author_fullname author_premium can_mod_post category clicked content_categories created_utc downs edited gilded gildings hidden hide_score is_created_from_ads_ui is_meta is_original_content is_reddit_media_domain is_video link_flair_css_class link_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title name permalink pwls quarantine saved score secure_media secure_media_embed selftext subreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type total_awards_received ups upvote_ratio url user_reports""".split() ) def a_ ( __snake_case : str , __snake_case : int = 1 , __snake_case : str = "new" , __snake_case : list | None = None ) -> dict: """simple docstring""" lowerCamelCase_ =wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(__snake_case ) - valid_terms ) ): lowerCamelCase_ =F'''Invalid search term: {invalid_search_terms}''' raise ValueError(__snake_case ) lowerCamelCase_ =requests.get( F'''https://reddit.com/r/{subreddit}/{age}.json?limit={limit}''' , headers={'''User-agent''': '''A random string'''} , ) if response.status_code == 429: raise requests.HTTPError lowerCamelCase_ =response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(__snake_case )} lowerCamelCase_ ={} for id_ in range(__snake_case ): lowerCamelCase_ ={ item: data['''data''']['''children'''][id_]['''data'''][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data("""learnpython""", wanted_data=["""title""", """url""", """selftext"""]))
75
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import is_speech_available from transformers.testing_utils import require_torch, require_torchaudio from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import SpeechaTextFeatureExtractor a_ : Union[str, Any] = random.Random() def a_ ( __snake_case : int , __snake_case : int=1.0 , __snake_case : Tuple=None , __snake_case : Union[str, Any]=None ) -> str: """simple docstring""" if rng is None: lowerCamelCase_ =global_rng lowerCamelCase_ =[] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class __UpperCamelCase ( unittest.TestCase ): def __init__( self, lowerCAmelCase, lowerCAmelCase=7, lowerCAmelCase=400, lowerCAmelCase=2_000, lowerCAmelCase=24, lowerCAmelCase=24, lowerCAmelCase=0.0, lowerCAmelCase=16_000, lowerCAmelCase=True, lowerCAmelCase=True, ): """simple docstring""" lowerCamelCase_ =parent lowerCamelCase_ =batch_size lowerCamelCase_ =min_seq_length lowerCamelCase_ =max_seq_length lowerCamelCase_ =(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) lowerCamelCase_ =feature_size lowerCamelCase_ =num_mel_bins lowerCamelCase_ =padding_value lowerCamelCase_ =sampling_rate lowerCamelCase_ =return_attention_mask lowerCamelCase_ =do_normalize def lowercase__ ( self ): """simple docstring""" return { "feature_size": self.feature_size, "num_mel_bins": self.num_mel_bins, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def lowercase__ ( self, lowerCAmelCase=False, lowerCAmelCase=False ): """simple docstring""" def _flatten(lowerCAmelCase ): return list(itertools.chain(*lowerCAmelCase ) ) if equal_length: lowerCamelCase_ =[floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size lowerCamelCase_ =[ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length, self.max_seq_length, self.seq_length_diff ) ] if numpify: lowerCamelCase_ =[np.asarray(lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __UpperCamelCase ( lowerCamelCase__ , unittest.TestCase ): lowercase : Any =SpeechaTextFeatureExtractor if is_speech_available() else None def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =SpeechaTextFeatureExtractionTester(self ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" self.assertTrue(np.all(np.mean(lowerCAmelCase, axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(lowerCAmelCase, axis=0 ) - 1 ) < 1e-3 ) ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 lowerCamelCase_ =[floats_list((1, x) )[0] for x in range(800, 1_400, 200 )] lowerCamelCase_ =[np.asarray(lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size lowerCamelCase_ =feature_extractor(lowerCAmelCase, padding=lowerCAmelCase, return_tensors='''np''' ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size ) # Test not batched input lowerCamelCase_ =feature_extractor(speech_inputs[0], return_tensors='''np''' ).input_features lowerCamelCase_ =feature_extractor(np_speech_inputs[0], return_tensors='''np''' ).input_features self.assertTrue(np.allclose(lowerCAmelCase, lowerCAmelCase, atol=1e-3 ) ) # Test batched lowerCamelCase_ =feature_extractor(lowerCAmelCase, return_tensors='''np''' ).input_features lowerCamelCase_ =feature_extractor(lowerCAmelCase, return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(lowerCAmelCase, lowerCAmelCase ): self.assertTrue(np.allclose(lowerCAmelCase, lowerCAmelCase, atol=1e-3 ) ) # Test 2-D numpy arrays are batched. lowerCamelCase_ =[floats_list((1, x) )[0] for x in (800, 800, 800)] lowerCamelCase_ =np.asarray(lowerCAmelCase ) lowerCamelCase_ =feature_extractor(lowerCAmelCase, return_tensors='''np''' ).input_features lowerCamelCase_ =feature_extractor(lowerCAmelCase, return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(lowerCAmelCase, lowerCAmelCase ): self.assertTrue(np.allclose(lowerCAmelCase, lowerCAmelCase, atol=1e-3 ) ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase_ =[floats_list((1, x) )[0] for x in range(800, 1_400, 200 )] lowerCamelCase_ =['''longest''', '''max_length''', '''do_not_pad'''] lowerCamelCase_ =[None, 16, None] for max_length, padding in zip(lowerCAmelCase, lowerCAmelCase ): lowerCamelCase_ =feature_extractor( lowerCAmelCase, padding=lowerCAmelCase, max_length=lowerCAmelCase, return_attention_mask=lowerCAmelCase ) lowerCamelCase_ =inputs.input_features lowerCamelCase_ =inputs.attention_mask lowerCamelCase_ =[np.sum(lowerCAmelCase ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase_ =[floats_list((1, x) )[0] for x in range(800, 1_400, 200 )] lowerCamelCase_ =['''longest''', '''max_length''', '''do_not_pad'''] lowerCamelCase_ =[None, 16, None] for max_length, padding in zip(lowerCAmelCase, lowerCAmelCase ): lowerCamelCase_ =feature_extractor( lowerCAmelCase, max_length=lowerCAmelCase, padding=lowerCAmelCase, return_tensors='''np''', return_attention_mask=lowerCAmelCase ) lowerCamelCase_ =inputs.input_features lowerCamelCase_ =inputs.attention_mask lowerCamelCase_ =[np.sum(lowerCAmelCase ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase_ =[floats_list((1, x) )[0] for x in range(800, 1_400, 200 )] lowerCamelCase_ =feature_extractor( lowerCAmelCase, padding='''max_length''', max_length=4, truncation=lowerCAmelCase, return_tensors='''np''', return_attention_mask=lowerCAmelCase, ) lowerCamelCase_ =inputs.input_features lowerCamelCase_ =inputs.attention_mask lowerCamelCase_ =np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1] ) self._check_zero_mean_unit_variance(input_features[2] ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase_ =[floats_list((1, x) )[0] for x in range(800, 1_400, 200 )] lowerCamelCase_ =feature_extractor( lowerCAmelCase, padding='''longest''', max_length=4, truncation=lowerCAmelCase, return_tensors='''np''', return_attention_mask=lowerCAmelCase, ) lowerCamelCase_ =inputs.input_features lowerCamelCase_ =inputs.attention_mask lowerCamelCase_ =np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 4, 24) ) lowerCamelCase_ =[floats_list((1, x) )[0] for x in range(800, 1_400, 200 )] lowerCamelCase_ =feature_extractor( lowerCAmelCase, padding='''longest''', max_length=16, truncation=lowerCAmelCase, return_tensors='''np''', return_attention_mask=lowerCAmelCase, ) lowerCamelCase_ =inputs.input_features lowerCamelCase_ =inputs.attention_mask lowerCamelCase_ =np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 6, 24) ) def lowercase__ ( self ): """simple docstring""" import torch lowerCamelCase_ =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase_ =np.random.rand(100, 32 ).astype(np.floataa ) lowerCamelCase_ =np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: lowerCamelCase_ =feature_extractor.pad([{'''input_features''': inputs}], return_tensors='''np''' ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) lowerCamelCase_ =feature_extractor.pad([{'''input_features''': inputs}], return_tensors='''pt''' ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" from datasets import load_dataset lowerCamelCase_ =load_dataset('''hf-internal-testing/librispeech_asr_dummy''', '''clean''', split='''validation''' ) # automatic decoding with librispeech lowerCamelCase_ =ds.sort('''id''' ).select(range(lowerCAmelCase ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =np.array([ -1.5_7_4_5, -1.7_7_1_3, -1.7_0_2_0, -1.6_0_6_9, -1.2_2_5_0, -1.1_1_0_5, -0.9_0_7_2, -0.8_2_4_1, -1.2_3_1_0, -0.8_0_9_8, -0.3_3_2_0, -0.4_1_0_1, -0.7_9_8_5, -0.4_9_9_6, -0.8_2_1_3, -0.9_1_2_8, -1.0_4_2_0, -1.1_2_8_6, -1.0_4_4_0, -0.7_9_9_9, -0.8_4_0_5, -1.2_2_7_5, -1.5_4_4_3, -1.4_6_2_5, ] ) # fmt: on lowerCamelCase_ =self._load_datasamples(1 ) lowerCamelCase_ =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase_ =feature_extractor(lowerCAmelCase, return_tensors='''pt''' ).input_features self.assertEquals(input_features.shape, (1, 584, 24) ) self.assertTrue(np.allclose(input_features[0, 0, :30], lowerCAmelCase, atol=1e-4 ) )
75
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) __snake_case :Optional[int] = {'''configuration_beit''': ['''BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BeitConfig''', '''BeitOnnxConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case :Optional[Any] = ['''BeitFeatureExtractor'''] __snake_case :Optional[int] = ['''BeitImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case :List[str] = [ '''BEIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BeitForImageClassification''', '''BeitForMaskedImageModeling''', '''BeitForSemanticSegmentation''', '''BeitModel''', '''BeitPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case :Union[str, Any] = [ '''FlaxBeitForImageClassification''', '''FlaxBeitForMaskedImageModeling''', '''FlaxBeitModel''', '''FlaxBeitPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys __snake_case :Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
131
def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): return base * power(_UpperCAmelCase , (exponent - 1) ) if exponent else 1 if __name__ == "__main__": print('''Raise base to the power of exponent using recursion...''') __snake_case :List[Any] = int(input('''Enter the base: ''').strip()) __snake_case :Dict = int(input('''Enter the exponent: ''').strip()) __snake_case :int = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents __snake_case :Optional[Any] = 1 / result print(f'{base} to the power of {exponent} is {result}')
131
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) a_ = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys a_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
340
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 OwlViTImageProcessor, OwlViTProcessor @require_vision class lowercase__ ( unittest.TestCase ): def UpperCAmelCase ( self )-> Dict: '''simple docstring''' lowerCAmelCase__ = tempfile.mkdtemp() # fmt: off lowerCAmelCase__ = ["", "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on lowerCAmelCase__ = dict(zip(__UpperCAmelCase , range(len(__UpperCAmelCase ) ) ) ) lowerCAmelCase__ = ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""] lowerCAmelCase__ = {"unk_token": "<unk>"} lowerCAmelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) lowerCAmelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(__UpperCAmelCase ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(__UpperCAmelCase ) ) lowerCAmelCase__ = { "do_resize": True, "size": 20, "do_center_crop": True, "crop_size": 18, "do_normalize": True, "image_mean": [0.48_145_466, 0.4_578_275, 0.40_821_073], "image_std": [0.26_862_954, 0.26_130_258, 0.27_577_711], } lowerCAmelCase__ = os.path.join(self.tmpdirname , __UpperCAmelCase ) with open(self.image_processor_file , "w" , encoding="utf-8" ) as fp: json.dump(__UpperCAmelCase , __UpperCAmelCase ) def UpperCAmelCase ( self , **__UpperCAmelCase )-> Union[str, Any]: '''simple docstring''' return CLIPTokenizer.from_pretrained(self.tmpdirname , pad_token="!" , **__UpperCAmelCase ) def UpperCAmelCase ( self , **__UpperCAmelCase )-> Any: '''simple docstring''' return CLIPTokenizerFast.from_pretrained(self.tmpdirname , pad_token="!" , **__UpperCAmelCase ) def UpperCAmelCase ( self , **__UpperCAmelCase )-> Optional[Any]: '''simple docstring''' return OwlViTImageProcessor.from_pretrained(self.tmpdirname , **__UpperCAmelCase ) def UpperCAmelCase ( self )-> Any: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def UpperCAmelCase ( self )-> int: '''simple docstring''' lowerCAmelCase__ = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowerCAmelCase__ = [Image.fromarray(np.moveaxis(__UpperCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def UpperCAmelCase ( self )-> Optional[int]: '''simple docstring''' lowerCAmelCase__ = self.get_tokenizer() lowerCAmelCase__ = self.get_rust_tokenizer() lowerCAmelCase__ = self.get_image_processor() lowerCAmelCase__ = OwlViTProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) lowerCAmelCase__ = OwlViTProcessor.from_pretrained(self.tmpdirname , use_fast=__UpperCAmelCase ) lowerCAmelCase__ = OwlViTProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) lowerCAmelCase__ = OwlViTProcessor.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 , __UpperCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __UpperCAmelCase ) 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 , __UpperCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __UpperCAmelCase ) def UpperCAmelCase ( self )-> List[Any]: '''simple docstring''' lowerCAmelCase__ = OwlViTProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCAmelCase__ = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) lowerCAmelCase__ = self.get_image_processor(do_normalize=__UpperCAmelCase ) lowerCAmelCase__ = OwlViTProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=__UpperCAmelCase ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __UpperCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __UpperCAmelCase ) def UpperCAmelCase ( self )-> List[str]: '''simple docstring''' lowerCAmelCase__ = self.get_image_processor() lowerCAmelCase__ = self.get_tokenizer() lowerCAmelCase__ = OwlViTProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) lowerCAmelCase__ = self.prepare_image_inputs() lowerCAmelCase__ = image_processor(__UpperCAmelCase , return_tensors="np" ) lowerCAmelCase__ = processor(images=__UpperCAmelCase , return_tensors="np" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def UpperCAmelCase ( self )-> Dict: '''simple docstring''' lowerCAmelCase__ = self.get_image_processor() lowerCAmelCase__ = self.get_tokenizer() lowerCAmelCase__ = OwlViTProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) lowerCAmelCase__ = "lower newer" lowerCAmelCase__ = processor(text=__UpperCAmelCase , return_tensors="np" ) lowerCAmelCase__ = tokenizer(__UpperCAmelCase , return_tensors="np" ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key][0].tolist() , encoded_processor[key][0].tolist() ) def UpperCAmelCase ( self )-> int: '''simple docstring''' lowerCAmelCase__ = self.get_image_processor() lowerCAmelCase__ = self.get_tokenizer() lowerCAmelCase__ = OwlViTProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) lowerCAmelCase__ = "lower newer" lowerCAmelCase__ = self.prepare_image_inputs() lowerCAmelCase__ = processor(text=__UpperCAmelCase , images=__UpperCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["input_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def UpperCAmelCase ( self )-> Any: '''simple docstring''' lowerCAmelCase__ = "google/owlvit-base-patch32" lowerCAmelCase__ = OwlViTProcessor.from_pretrained(__UpperCAmelCase ) lowerCAmelCase__ = ["cat", "nasa badge"] lowerCAmelCase__ = processor(text=__UpperCAmelCase ) lowerCAmelCase__ = 16 self.assertListEqual(list(inputs.keys() ) , ["input_ids", "attention_mask"] ) self.assertEqual(inputs["input_ids"].shape , (2, seq_length) ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def UpperCAmelCase ( self )-> Optional[int]: '''simple docstring''' lowerCAmelCase__ = "google/owlvit-base-patch32" lowerCAmelCase__ = OwlViTProcessor.from_pretrained(__UpperCAmelCase ) lowerCAmelCase__ = [["cat", "nasa badge"], ["person"]] lowerCAmelCase__ = processor(text=__UpperCAmelCase ) lowerCAmelCase__ = 16 lowerCAmelCase__ = len(__UpperCAmelCase ) lowerCAmelCase__ = max([len(__UpperCAmelCase ) for texts in input_texts] ) self.assertListEqual(list(inputs.keys() ) , ["input_ids", "attention_mask"] ) self.assertEqual(inputs["input_ids"].shape , (batch_size * num_max_text_queries, seq_length) ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def UpperCAmelCase ( self )-> str: '''simple docstring''' lowerCAmelCase__ = "google/owlvit-base-patch32" lowerCAmelCase__ = OwlViTProcessor.from_pretrained(__UpperCAmelCase ) lowerCAmelCase__ = ["cat", "nasa badge"] lowerCAmelCase__ = processor(text=__UpperCAmelCase ) lowerCAmelCase__ = 16 lowerCAmelCase__ = inputs["input_ids"] lowerCAmelCase__ = [ [49406, 2368, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [49406, 6841, 11301, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] self.assertListEqual(list(inputs.keys() ) , ["input_ids", "attention_mask"] ) self.assertEqual(inputs["input_ids"].shape , (2, seq_length) ) self.assertListEqual(list(input_ids[0] ) , predicted_ids[0] ) self.assertListEqual(list(input_ids[1] ) , predicted_ids[1] ) def UpperCAmelCase ( self )-> List[str]: '''simple docstring''' lowerCAmelCase__ = self.get_image_processor() lowerCAmelCase__ = self.get_tokenizer() lowerCAmelCase__ = OwlViTProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) lowerCAmelCase__ = self.prepare_image_inputs() lowerCAmelCase__ = self.prepare_image_inputs() lowerCAmelCase__ = processor(images=__UpperCAmelCase , query_images=__UpperCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["query_pixel_values", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(__UpperCAmelCase ): processor() def UpperCAmelCase ( self )-> Tuple: '''simple docstring''' lowerCAmelCase__ = self.get_image_processor() lowerCAmelCase__ = self.get_tokenizer() lowerCAmelCase__ = OwlViTProcessor(tokenizer=__UpperCAmelCase , image_processor=__UpperCAmelCase ) lowerCAmelCase__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCAmelCase__ = processor.batch_decode(__UpperCAmelCase ) lowerCAmelCase__ = tokenizer.batch_decode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase )
340
1
'''simple docstring''' def __SCREAMING_SNAKE_CASE ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _snake_case = 0 _snake_case = len(_SCREAMING_SNAKE_CASE ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None _snake_case = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(_SCREAMING_SNAKE_CASE ): return None _snake_case = sorted_collection[point] if current_item == item: return point else: if point < left: _snake_case = left _snake_case = point elif point > right: _snake_case = right _snake_case = point else: if item < current_item: _snake_case = point - 1 else: _snake_case = point + 1 return None def __SCREAMING_SNAKE_CASE ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None _snake_case = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(_SCREAMING_SNAKE_CASE ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) elif point > right: return interpolation_search_by_recursion(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , point - 1 ) else: return interpolation_search_by_recursion( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , point + 1 , _SCREAMING_SNAKE_CASE ) def __SCREAMING_SNAKE_CASE ( _SCREAMING_SNAKE_CASE ): if collection != sorted(_SCREAMING_SNAKE_CASE ): raise ValueError("""Collection must be ascending sorted""" ) return True if __name__ == "__main__": import sys __lowerCAmelCase = 0 if debug == 1: __lowerCAmelCase = [10, 30, 40, 45, 50, 66, 77, 93] try: __assert_sorted(collection) except ValueError: sys.exit('Sequence must be ascending sorted to apply interpolation search') __lowerCAmelCase = 67 __lowerCAmelCase = interpolation_search(collection, target) if result is not None: print(f'''{target} found at positions: {result}''') else: print('Not found')
361
'''simple docstring''' from __future__ import annotations import typing from collections import Counter def __SCREAMING_SNAKE_CASE ( _SCREAMING_SNAKE_CASE ): _snake_case = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(_SCREAMING_SNAKE_CASE , max_perimeter + 1 ): _snake_case = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(_SCREAMING_SNAKE_CASE ): _snake_case = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def __SCREAMING_SNAKE_CASE ( _SCREAMING_SNAKE_CASE = 1000 ): _snake_case = pythagorean_triple(_SCREAMING_SNAKE_CASE ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f'''Perimeter {solution()} has maximum solutions''')
270
0
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 lowercase ( unittest.TestCase ): @slow def a__ ( self ) -> Union[str, Any]: _A : List[Any] = FlaxMTaForConditionalGeneration.from_pretrained("""google/mt5-small""" ) _A : Optional[int] = AutoTokenizer.from_pretrained("""google/mt5-small""" ) _A : Optional[Any] = tokenizer("""Hello there""" , return_tensors="""np""" ).input_ids _A : Optional[int] = tokenizer("""Hi I am""" , return_tensors="""np""" ).input_ids _A : Union[str, Any] = shift_tokens_right(UpperCAmelCase__ , model.config.pad_token_id , model.config.decoder_start_token_id ) _A : Optional[Any] = model(UpperCAmelCase__ , decoder_input_ids=UpperCAmelCase__ ).logits _A : Optional[Any] = optax.softmax_cross_entropy(UpperCAmelCase__ , onehot(UpperCAmelCase__ , logits.shape[-1] ) ).mean() _A : Tuple = -(labels.shape[-1] * loss.item()) _A : int = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
26
"""simple docstring""" from __future__ import annotations from typing import Any class _lowerCAmelCase : """simple docstring""" def __init__( self : Any, UpperCAmelCase__ : int ): __lowercase = num_of_nodes __lowercase = [] __lowercase = {} def _lowercase ( self : Optional[Any], UpperCAmelCase__ : int, UpperCAmelCase__ : int, UpperCAmelCase__ : int ): self.m_edges.append([u_node, v_node, weight] ) def _lowercase ( self : Union[str, Any], UpperCAmelCase__ : int ): if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def _lowercase ( self : List[Any], UpperCAmelCase__ : int ): if self.m_component[u_node] != u_node: for k in self.m_component: __lowercase = self.find_component(UpperCAmelCase__ ) def _lowercase ( self : Union[str, Any], UpperCAmelCase__ : list[int], UpperCAmelCase__ : int, UpperCAmelCase__ : int ): if component_size[u_node] <= component_size[v_node]: __lowercase = v_node component_size[v_node] += component_size[u_node] self.set_component(UpperCAmelCase__ ) elif component_size[u_node] >= component_size[v_node]: __lowercase = self.find_component(UpperCAmelCase__ ) component_size[u_node] += component_size[v_node] self.set_component(UpperCAmelCase__ ) def _lowercase ( self : Any ): __lowercase = [] __lowercase = 0 __lowercase = [-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) __lowercase = self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: __lowercase ,__lowercase ,__lowercase = edge __lowercase = self.m_component[u] __lowercase = self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): __lowercase = [u, v, w] for edge in minimum_weight_edge: if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): __lowercase ,__lowercase ,__lowercase = edge __lowercase = self.m_component[u] __lowercase = self.m_component[v] if u_component != v_component: mst_weight += w self.union(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) print(F"""Added edge [{u} - {v}]\nAdded weight: {w}\n""" ) num_of_components -= 1 __lowercase = [-1] * self.m_num_of_nodes print(F"""The total weight of the minimal spanning tree is: {mst_weight}""" ) def _A ( ) -> None: '''simple docstring''' if __name__ == "__main__": import doctest doctest.testmod()
17
0
"""simple docstring""" from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class UpperCamelCase : '''simple docstring''' lowercase : str =field( metadata={"""help""": """The output directory where the model will be written."""} , ) lowercase : str =field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } , ) lowercase : str =field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } , ) lowercase : Optional[str] =field( default=lowercase__ , metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) lowercase : Optional[str] =field( default=lowercase__ , metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def UpperCamelCase ( ) -> Dict: '''simple docstring''' lowercase_ :Any = HfArgumentParser((ModelArguments,) ) (lowercase_ ) :Tuple = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: lowercase_ :str = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: lowercase_ :Any = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: lowercase_ :str = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: lowercase_ :Any = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed lowercase_ :List[Any] = True lowercase_ :Any = True lowercase_ :Optional[int] = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=_a , decoder_config=_a , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens lowercase_ :List[str] = decoder_config.decoder_start_token_id lowercase_ :Union[str, Any] = decoder_config.pad_token_id if decoder_start_token_id is None: lowercase_ :str = decoder_config.bos_token_id if pad_token_id is None: lowercase_ :str = decoder_config.eos_token_id # This is necessary to make Flax's generate() work lowercase_ :List[str] = decoder_config.eos_token_id lowercase_ :Optional[int] = decoder_start_token_id lowercase_ :Optional[int] = pad_token_id lowercase_ :Dict = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) lowercase_ :Dict = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) lowercase_ :Optional[Any] = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
355
from itertools import count def UpperCamelCase ( _a = 5_0 ) -> int: '''simple docstring''' lowercase_ :Dict = [1] * min_block_length for n in count(_a ): fill_count_functions.append(1 ) for block_length in range(_a , n + 1 ): for block_start in range(n - block_length ): fill_count_functions[n] += fill_count_functions[ n - block_start - block_length - 1 ] fill_count_functions[n] += 1 if fill_count_functions[n] > 1_0_0_0_0_0_0: break return n if __name__ == "__main__": print(f"{solution() = }")
252
0
"""simple docstring""" import os from typing import List, Optional, Union from ...tokenization_utils import PreTrainedTokenizer from ...tokenization_utils_base import AddedToken from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = {"""vocab_file""": """vocab.txt"""} lowerCamelCase__ = { """vocab_file""": { """facebook/esm2_t6_8M_UR50D""": """https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt""", """facebook/esm2_t12_35M_UR50D""": """https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt""", }, } lowerCamelCase__ = { """facebook/esm2_t6_8M_UR50D""": 1_024, """facebook/esm2_t12_35M_UR50D""": 1_024, } def __lowerCAmelCase (_UpperCamelCase ): with open(_UpperCamelCase , 'r' ) as f: __lowerCAmelCase : Dict = f.read().splitlines() return [l.strip() for l in lines] class A__ ( _lowerCamelCase): A_ : str = VOCAB_FILES_NAMES A_ : List[str] = PRETRAINED_VOCAB_FILES_MAP A_ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A_ : Optional[int] = ['input_ids', 'attention_mask'] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<cls>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="<eos>" , **_SCREAMING_SNAKE_CASE , ): super().__init__(**_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : int = load_vocab_file(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[int] = dict(enumerate(self.all_tokens ) ) __lowerCAmelCase : List[str] = {tok: ind for ind, tok in enumerate(self.all_tokens )} __lowerCAmelCase : List[Any] = unk_token __lowerCAmelCase : int = cls_token __lowerCAmelCase : Dict = pad_token __lowerCAmelCase : List[Any] = mask_token __lowerCAmelCase : int = eos_token __lowerCAmelCase : Union[str, Any] = self.all_tokens self._create_trie(self.unique_no_split_tokens ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): return self._id_to_token.get(_SCREAMING_SNAKE_CASE , self.unk_token ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): return self._token_to_id.get(_SCREAMING_SNAKE_CASE , self._token_to_id.get(self.unk_token ) ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ): return text.split() def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE=False ): return len(self._id_to_token ) def __lowerCamelCase ( self ): return {token: i for i, token in enumerate(self.all_tokens )} def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): return self._token_to_id.get(_SCREAMING_SNAKE_CASE , self._token_to_id.get(self.unk_token ) ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): return self._id_to_token.get(_SCREAMING_SNAKE_CASE , self.unk_token ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ): __lowerCAmelCase : List[Any] = [self.cls_token_id] __lowerCAmelCase : Optional[Any] = [self.eos_token_id] # No sep token in ESM vocabulary if token_ids_a is None: if self.eos_token_id is None: return cls + token_ids_a else: return cls + token_ids_a + sep elif self.eos_token_id is None: raise ValueError('Cannot tokenize multiple sequences when EOS token is not set!' ) return cls + token_ids_a + sep + token_ids_a + sep # Multiple inputs always have an EOS token def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ): if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if token in self.all_special_ids else 0 for token in token_ids_a] __lowerCAmelCase : Any = [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] if token_ids_a is not None: mask += [0] * len(_SCREAMING_SNAKE_CASE ) + [1] return mask def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Any = os.path.join(_SCREAMING_SNAKE_CASE , (filename_prefix + '-' if filename_prefix else '') + 'vocab.txt' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write('\n'.join(self.all_tokens ) ) return (vocab_file,) @property def __lowerCamelCase ( self ): return self.get_vocab_size(with_added_tokens=_SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = False ): return super()._add_tokens(_SCREAMING_SNAKE_CASE , special_tokens=_SCREAMING_SNAKE_CASE )
86
'''simple docstring''' import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class lowerCamelCase_ ( __a ): def __init__( self : List[str] , _A : NestedDataStructureLike[PathLike] , _A : Optional[NamedSplit] = None , _A : Optional[Features] = None , _A : str = None , _A : bool = False , _A : bool = False , _A : Optional[str] = None , _A : Optional[int] = None , **_A : Union[str, Any] , ): '''simple docstring''' super().__init__( _A , split=_A , features=_A , cache_dir=_A , keep_in_memory=_A , streaming=_A , num_proc=_A , **_A , ) UpperCAmelCase__ : List[str] = field UpperCAmelCase__ : Optional[Any] = path_or_paths if isinstance(_A , _A ) else {self.split: path_or_paths} UpperCAmelCase__ : Any = Json( cache_dir=_A , data_files=_A , features=_A , field=_A , **_A , ) def lowercase_ ( self : Dict ): '''simple docstring''' if self.streaming: UpperCAmelCase__ : int = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: UpperCAmelCase__ : List[str] = None UpperCAmelCase__ : Dict = None UpperCAmelCase__ : Optional[int] = None UpperCAmelCase__ : List[Any] = None self.builder.download_and_prepare( download_config=_A , download_mode=_A , verification_mode=_A , base_path=_A , num_proc=self.num_proc , ) UpperCAmelCase__ : str = self.builder.as_dataset( split=self.split , verification_mode=_A , in_memory=self.keep_in_memory ) return dataset class lowerCamelCase_ : def __init__( self : Optional[Any] , _A : Dataset , _A : Union[PathLike, BinaryIO] , _A : Optional[int] = None , _A : Optional[int] = None , **_A : List[str] , ): '''simple docstring''' if num_proc is not None and num_proc <= 0: raise ValueError(f"""num_proc {num_proc} must be an integer > 0.""" ) UpperCAmelCase__ : Dict = dataset UpperCAmelCase__ : Any = path_or_buf UpperCAmelCase__ : List[str] = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE UpperCAmelCase__ : Tuple = num_proc UpperCAmelCase__ : Any = '''utf-8''' UpperCAmelCase__ : Optional[int] = to_json_kwargs def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : List[str] = self.to_json_kwargs.pop('''path_or_buf''' , _A ) UpperCAmelCase__ : Optional[int] = self.to_json_kwargs.pop('''orient''' , '''records''' ) UpperCAmelCase__ : Tuple = self.to_json_kwargs.pop('''lines''' , True if orient == '''records''' else False ) UpperCAmelCase__ : str = self.to_json_kwargs.pop('''index''' , False if orient in ['''split''', '''table'''] else True ) UpperCAmelCase__ : Optional[Any] = self.to_json_kwargs.pop('''compression''' , _A ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f"""`datasets` currently does not support {compression} compression""" ) if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf , '''wb''' , compression=_A ) as buffer: UpperCAmelCase__ : Union[str, Any] = self._write(file_obj=_A , orient=_A , lines=_A , index=_A , **self.to_json_kwargs ) else: if compression: raise NotImplementedError( f"""The compression parameter is not supported when writing to a buffer, but compression={compression}""" ''' was passed. Please provide a local path instead.''' ) UpperCAmelCase__ : Union[str, Any] = self._write( file_obj=self.path_or_buf , orient=_A , lines=_A , index=_A , **self.to_json_kwargs ) return written def lowercase_ ( self : Optional[int] , _A : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ : Dict = args UpperCAmelCase__ : Dict = query_table( table=self.dataset.data , key=slice(_A , offset + self.batch_size ) , indices=self.dataset._indices , ) UpperCAmelCase__ : str = batch.to_pandas().to_json( path_or_buf=_A , orient=_A , lines=_A , index=_A , **_A ) if not json_str.endswith('''\n''' ): json_str += "\n" return json_str.encode(self.encoding ) def lowercase_ ( self : Union[str, Any] , _A : BinaryIO , _A : Optional[int] , _A : int , _A : Any , **_A : Any , ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating json from Arrow format''' , ): UpperCAmelCase__ : Optional[int] = self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(_A ) else: UpperCAmelCase__ , UpperCAmelCase__ : str = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json , [(offset, orient, lines, index, to_json_kwargs) for offset in range(0 , _A , _A )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating json from Arrow format''' , ): written += file_obj.write(_A ) return written
181
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 MobileViTImageProcessor class snake_case_ (unittest.TestCase ): def __init__( self :Dict ,__snake_case :List[Any] ,__snake_case :Dict=7 ,__snake_case :int=3 ,__snake_case :Union[str, Any]=18 ,__snake_case :Union[str, Any]=30 ,__snake_case :Any=4_00 ,__snake_case :List[Any]=True ,__snake_case :str=None ,__snake_case :List[Any]=True ,__snake_case :Any=None ,__snake_case :Optional[Any]=True ,) -> int: a__ = size if size is not None else {'shortest_edge': 20} a__ = crop_size if crop_size is not None else {'height': 18, 'width': 18} a__ = parent a__ = batch_size a__ = num_channels a__ = image_size a__ = min_resolution a__ = max_resolution a__ = do_resize a__ = size a__ = do_center_crop a__ = crop_size a__ = do_flip_channel_order def lowerCamelCase__( self :Any ) -> Any: return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class snake_case_ (lowerCamelCase_ , unittest.TestCase ): UpperCAmelCase__ : int = MobileViTImageProcessor if is_vision_available() else None def lowerCamelCase__( self :Any ) -> Optional[Any]: a__ = MobileViTImageProcessingTester(self ) @property def lowerCamelCase__( self :Union[str, Any] ) -> Any: return self.image_processor_tester.prepare_image_processor_dict() def lowerCamelCase__( self :List[str] ) -> Union[str, Any]: a__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__snake_case ,'do_resize' ) ) self.assertTrue(hasattr(__snake_case ,'size' ) ) self.assertTrue(hasattr(__snake_case ,'do_center_crop' ) ) self.assertTrue(hasattr(__snake_case ,'center_crop' ) ) self.assertTrue(hasattr(__snake_case ,'do_flip_channel_order' ) ) def lowerCamelCase__( self :Any ) -> int: a__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'shortest_edge': 20} ) self.assertEqual(image_processor.crop_size ,{'height': 18, 'width': 18} ) a__ = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{'shortest_edge': 42} ) self.assertEqual(image_processor.crop_size ,{'height': 84, 'width': 84} ) def lowerCamelCase__( self :Optional[int] ) -> str: pass def lowerCamelCase__( self :Optional[Any] ) -> Optional[int]: # Initialize image_processing a__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a__ = prepare_image_inputs(self.image_processor_tester ,equal_resolution=__snake_case ) for image in image_inputs: self.assertIsInstance(__snake_case ,Image.Image ) # Test not batched input a__ = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) # Test batched a__ = image_processing(__snake_case ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) def lowerCamelCase__( self :int ) -> List[Any]: # Initialize image_processing a__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a__ = prepare_image_inputs(self.image_processor_tester ,equal_resolution=__snake_case ,numpify=__snake_case ) for image in image_inputs: self.assertIsInstance(__snake_case ,np.ndarray ) # Test not batched input a__ = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) # Test batched a__ = image_processing(__snake_case ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) def lowerCamelCase__( self :List[str] ) -> Dict: # Initialize image_processing a__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a__ = prepare_image_inputs(self.image_processor_tester ,equal_resolution=__snake_case ,torchify=__snake_case ) for image in image_inputs: self.assertIsInstance(__snake_case ,torch.Tensor ) # Test not batched input a__ = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) # Test batched a__ = image_processing(__snake_case ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,)
109
from math import ceil, sqrt def __lowercase ( __lowerCAmelCase : int = 1_0_0_0_0_0_0 ): a__ = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: a__ = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: a__ = 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() = }""")
109
1
import argparse import json import re from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileNetVaConfig, MobileNetVaForImageClassification, MobileNetVaImageProcessor, load_tf_weights_in_mobilenet_va, ) from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase = logging.get_logger(__name__) def lowerCamelCase_ ( _a ): """simple docstring""" lowerCAmelCase__ : Tuple = MobileNetVaConfig(layer_norm_eps=0.0_01 ) if "_quant" in model_name: raise ValueError('''Quantized models are not supported.''' ) lowerCAmelCase__ : Union[str, Any] = re.match(R'''^mobilenet_v1_([^_]*)_([^_]*)$''' , _a ) if matches: lowerCAmelCase__ : int = float(matches[1] ) lowerCAmelCase__ : Optional[int] = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". lowerCAmelCase__ : Tuple = 1_001 lowerCAmelCase__ : Dict = '''imagenet-1k-id2label.json''' lowerCAmelCase__ : Dict = '''huggingface/label-files''' lowerCAmelCase__ : Optional[int] = json.load(open(hf_hub_download(_a , _a , repo_type='''dataset''' ) , '''r''' ) ) lowerCAmelCase__ : int = {int(_a ) + 1: v for k, v in idalabel.items()} lowerCAmelCase__ : str = '''background''' lowerCAmelCase__ : Any = idalabel lowerCAmelCase__ : List[Any] = {v: k for k, v in idalabel.items()} return config def lowerCamelCase_ ( ): """simple docstring""" lowerCAmelCase__ : Any = '''http://images.cocodataset.org/val2017/000000039769.jpg''' lowerCAmelCase__ : Tuple = Image.open(requests.get(_a , stream=_a ).raw ) return im @torch.no_grad() def lowerCamelCase_ ( _a , _a , _a , _a=False ): """simple docstring""" lowerCAmelCase__ : Tuple = get_mobilenet_va_config(_a ) # Load 🤗 model lowerCAmelCase__ : Union[str, Any] = MobileNetVaForImageClassification(_a ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(_a , _a , _a ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor lowerCAmelCase__ : int = MobileNetVaImageProcessor( crop_size={'''width''': config.image_size, '''height''': config.image_size} , size={'''shortest_edge''': config.image_size + 32} , ) lowerCAmelCase__ : Dict = image_processor(images=prepare_img() , return_tensors='''pt''' ) lowerCAmelCase__ : Optional[int] = model(**_a ) lowerCAmelCase__ : Union[str, Any] = outputs.logits assert logits.shape == (1, 1_001) if model_name == "mobilenet_v1_1.0_224": lowerCAmelCase__ : List[str] = torch.tensor([-4.17_39, -1.12_33, 3.12_05] ) elif model_name == "mobilenet_v1_0.75_192": lowerCAmelCase__ : int = torch.tensor([-3.94_40, -2.31_41, -0.33_33] ) else: lowerCAmelCase__ : Optional[Any] = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , _a , atol=1e-4 ) Path(_a ).mkdir(exist_ok=_a ) print(f'Saving model {model_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(_a ) print(f'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(_a ) if push_to_hub: print('''Pushing to the hub...''' ) lowerCAmelCase__ : Union[str, Any] = '''google/''' + model_name image_processor.push_to_hub(_a ) model.push_to_hub(_a ) if __name__ == "__main__": lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''mobilenet_v1_1.0_224''', type=str, help='''Name of the MobileNetV1 model you\'d like to convert. Should in the form \'mobilenet_v1_<depth>_<size>\'.''', ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original TensorFlow checkpoint (.ckpt file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) lowerCamelCase = parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
131
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch lowerCamelCase = logging.get_logger(__name__) class _a ( _lowercase): _a : Optional[Any] = ['''pixel_values'''] def __init__( self : List[Any] , _SCREAMING_SNAKE_CASE : bool = True , _SCREAMING_SNAKE_CASE : Optional[Dict[str, int]] = None , _SCREAMING_SNAKE_CASE : PILImageResampling = PILImageResampling.BILINEAR , _SCREAMING_SNAKE_CASE : bool = True , _SCREAMING_SNAKE_CASE : Dict[str, int] = None , _SCREAMING_SNAKE_CASE : bool = True , _SCREAMING_SNAKE_CASE : Union[int, float] = 1 / 255 , _SCREAMING_SNAKE_CASE : bool = True , _SCREAMING_SNAKE_CASE : Optional[Union[float, List[float]]] = None , _SCREAMING_SNAKE_CASE : Optional[Union[float, List[float]]] = None , **_SCREAMING_SNAKE_CASE : int , )-> None: super().__init__(**_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : Dict = size if size is not None else {'''shortest_edge''': 256} lowerCAmelCase__ : Tuple = get_size_dict(_SCREAMING_SNAKE_CASE , default_to_square=_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : List[Any] = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} lowerCAmelCase__ : Optional[Any] = get_size_dict(_SCREAMING_SNAKE_CASE , param_name='''crop_size''' ) lowerCAmelCase__ : List[str] = do_resize lowerCAmelCase__ : Optional[Any] = size lowerCAmelCase__ : Any = resample lowerCAmelCase__ : str = do_center_crop lowerCAmelCase__ : Dict = crop_size lowerCAmelCase__ : str = do_rescale lowerCAmelCase__ : List[str] = rescale_factor lowerCAmelCase__ : int = do_normalize lowerCAmelCase__ : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCAmelCase__ : Optional[int] = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCAmelCase__( self : List[Any] , _SCREAMING_SNAKE_CASE : np.ndarray , _SCREAMING_SNAKE_CASE : Dict[str, int] , _SCREAMING_SNAKE_CASE : PILImageResampling = PILImageResampling.BICUBIC , _SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **_SCREAMING_SNAKE_CASE : Dict , )-> np.ndarray: lowerCAmelCase__ : str = get_size_dict(_SCREAMING_SNAKE_CASE , default_to_square=_SCREAMING_SNAKE_CASE ) if "shortest_edge" not in size: raise ValueError(F'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) lowerCAmelCase__ : List[str] = get_resize_output_image_size(_SCREAMING_SNAKE_CASE , size=size['''shortest_edge'''] , default_to_square=_SCREAMING_SNAKE_CASE ) return resize(_SCREAMING_SNAKE_CASE , size=_SCREAMING_SNAKE_CASE , resample=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__( self : List[str] , _SCREAMING_SNAKE_CASE : np.ndarray , _SCREAMING_SNAKE_CASE : Dict[str, int] , _SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **_SCREAMING_SNAKE_CASE : List[str] , )-> np.ndarray: lowerCAmelCase__ : Dict = get_size_dict(_SCREAMING_SNAKE_CASE ) if "height" not in size or "width" not in size: raise ValueError(F'The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}' ) return center_crop(_SCREAMING_SNAKE_CASE , size=(size['''height'''], size['''width''']) , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__( self : int , _SCREAMING_SNAKE_CASE : np.ndarray , _SCREAMING_SNAKE_CASE : float , _SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **_SCREAMING_SNAKE_CASE : Optional[int] )-> np.ndarray: return rescale(_SCREAMING_SNAKE_CASE , scale=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__( self : Optional[int] , _SCREAMING_SNAKE_CASE : np.ndarray , _SCREAMING_SNAKE_CASE : Union[float, List[float]] , _SCREAMING_SNAKE_CASE : Union[float, List[float]] , _SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **_SCREAMING_SNAKE_CASE : str , )-> np.ndarray: return normalize(_SCREAMING_SNAKE_CASE , mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__( self : Any , _SCREAMING_SNAKE_CASE : ImageInput , _SCREAMING_SNAKE_CASE : Optional[bool] = None , _SCREAMING_SNAKE_CASE : Dict[str, int] = None , _SCREAMING_SNAKE_CASE : PILImageResampling = None , _SCREAMING_SNAKE_CASE : bool = None , _SCREAMING_SNAKE_CASE : Dict[str, int] = None , _SCREAMING_SNAKE_CASE : Optional[bool] = None , _SCREAMING_SNAKE_CASE : Optional[float] = None , _SCREAMING_SNAKE_CASE : Optional[bool] = None , _SCREAMING_SNAKE_CASE : Optional[Union[float, List[float]]] = None , _SCREAMING_SNAKE_CASE : Optional[Union[float, List[float]]] = None , _SCREAMING_SNAKE_CASE : Optional[Union[str, TensorType]] = None , _SCREAMING_SNAKE_CASE : Union[str, ChannelDimension] = ChannelDimension.FIRST , **_SCREAMING_SNAKE_CASE : Tuple , )-> Optional[Any]: lowerCAmelCase__ : List[str] = do_resize if do_resize is not None else self.do_resize lowerCAmelCase__ : List[str] = size if size is not None else self.size lowerCAmelCase__ : Any = get_size_dict(_SCREAMING_SNAKE_CASE , default_to_square=_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : str = resample if resample is not None else self.resample lowerCAmelCase__ : Dict = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase__ : Dict = crop_size if crop_size is not None else self.crop_size lowerCAmelCase__ : Any = get_size_dict(_SCREAMING_SNAKE_CASE , param_name='''crop_size''' ) lowerCAmelCase__ : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase__ : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase__ : str = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase__ : List[Any] = image_mean if image_mean is not None else self.image_mean lowerCAmelCase__ : List[str] = image_std if image_std is not None else self.image_std lowerCAmelCase__ : Optional[int] = make_list_of_images(_SCREAMING_SNAKE_CASE ) if not valid_images(_SCREAMING_SNAKE_CASE ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) 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. lowerCAmelCase__ : List[Any] = [to_numpy_array(_SCREAMING_SNAKE_CASE ) for image in images] if do_resize: lowerCAmelCase__ : Dict = [self.resize(image=_SCREAMING_SNAKE_CASE , size=_SCREAMING_SNAKE_CASE , resample=_SCREAMING_SNAKE_CASE ) for image in images] if do_center_crop: lowerCAmelCase__ : Dict = [self.center_crop(image=_SCREAMING_SNAKE_CASE , size=_SCREAMING_SNAKE_CASE ) for image in images] if do_rescale: lowerCAmelCase__ : List[Any] = [self.rescale(image=_SCREAMING_SNAKE_CASE , scale=_SCREAMING_SNAKE_CASE ) for image in images] if do_normalize: lowerCAmelCase__ : Tuple = [self.normalize(image=_SCREAMING_SNAKE_CASE , mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE ) for image in images] lowerCAmelCase__ : Dict = [to_channel_dimension_format(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for image in images] lowerCAmelCase__ : Dict = {'''pixel_values''': images} return BatchFeature(data=_SCREAMING_SNAKE_CASE , tensor_type=_SCREAMING_SNAKE_CASE ) def UpperCAmelCase__( self : int , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : List[Tuple] = None )-> List[Any]: lowerCAmelCase__ : Union[str, Any] = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_SCREAMING_SNAKE_CASE ) != len(_SCREAMING_SNAKE_CASE ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(_SCREAMING_SNAKE_CASE ): lowerCAmelCase__ : Tuple = target_sizes.numpy() lowerCAmelCase__ : Tuple = [] for idx in range(len(_SCREAMING_SNAKE_CASE ) ): lowerCAmelCase__ : int = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : str = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_SCREAMING_SNAKE_CASE ) else: lowerCAmelCase__ : Any = logits.argmax(dim=1 ) lowerCAmelCase__ : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
131
1
'''simple docstring''' from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging A_ : Union[str, Any] = logging.get_logger(__name__) A_ : Union[str, Any] = { """EleutherAI/gpt-j-6B""": """https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json""", # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class lowercase ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = """gptj""" UpperCAmelCase = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self ,a_=50_400 ,a_=2_048 ,a_=4_096 ,a_=28 ,a_=16 ,a_=64 ,a_=None ,a_="gelu_new" ,a_=0.0 ,a_=0.0 ,a_=0.0 ,a_=1E-5 ,a_=0.02 ,a_=True ,a_=50_256 ,a_=50_256 ,a_=False ,**a_ ,) -> Dict: _UpperCAmelCase : Union[str, Any] = vocab_size _UpperCAmelCase : str = n_positions _UpperCAmelCase : Optional[int] = n_embd _UpperCAmelCase : List[str] = n_layer _UpperCAmelCase : List[Any] = n_head _UpperCAmelCase : Tuple = n_inner _UpperCAmelCase : List[str] = rotary_dim _UpperCAmelCase : List[str] = activation_function _UpperCAmelCase : Union[str, Any] = resid_pdrop _UpperCAmelCase : Union[str, Any] = embd_pdrop _UpperCAmelCase : Optional[int] = attn_pdrop _UpperCAmelCase : List[str] = layer_norm_epsilon _UpperCAmelCase : int = initializer_range _UpperCAmelCase : str = use_cache _UpperCAmelCase : Any = bos_token_id _UpperCAmelCase : List[Any] = eos_token_id super().__init__( bos_token_id=a_ ,eos_token_id=a_ ,tie_word_embeddings=a_ ,**a_ ) class lowercase ( _lowerCamelCase ): """simple docstring""" def __init__( self ,a_ ,a_ = "default" ,a_ = None ,a_ = False ,) -> int: super().__init__(a_ ,task=a_ ,patching_specs=a_ ,use_past=a_ ) if not getattr(self._config ,"""pad_token_id""" ,a_ ): # TODO: how to do that better? _UpperCAmelCase : List[str] = 0 @property def _snake_case ( self ) -> Mapping[str, Mapping[int, str]]: _UpperCAmelCase : List[str] = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(a_ ,direction="""inputs""" ) _UpperCAmelCase : Any = {0: """batch""", 1: """past_sequence + sequence"""} else: _UpperCAmelCase : Optional[int] = {0: """batch""", 1: """sequence"""} return common_inputs @property def _snake_case ( self ) -> int: return self._config.n_layer @property def _snake_case ( self ) -> int: return self._config.n_head def _snake_case ( self ,a_ ,a_ = -1 ,a_ = -1 ,a_ = False ,a_ = None ,) -> Mapping[str, Any]: _UpperCAmelCase : Any = super(a_ ,self ).generate_dummy_inputs( a_ ,batch_size=a_ ,seq_length=a_ ,is_pair=a_ ,framework=a_ ) # We need to order the input in the way they appears in the forward() _UpperCAmelCase : Optional[int] = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch _UpperCAmelCase ,_UpperCAmelCase : Dict = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values _UpperCAmelCase : Union[str, Any] = seqlen + 2 _UpperCAmelCase : List[str] = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) _UpperCAmelCase : Optional[Any] = [ (torch.zeros(a_ ), torch.zeros(a_ )) for _ in range(self.num_layers ) ] _UpperCAmelCase : int = common_inputs["""attention_mask"""] if self.use_past: _UpperCAmelCase : Any = ordered_inputs["""attention_mask"""].dtype _UpperCAmelCase : Optional[int] = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(a_ ,a_ ,dtype=a_ )] ,dim=1 ) return ordered_inputs @property def _snake_case ( self ) -> int: return 13
349
'''simple docstring''' from __future__ import annotations from collections.abc import Callable def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = 100 , )-> float: '''simple docstring''' _UpperCAmelCase : str = x_start _UpperCAmelCase : Union[str, Any] = fnc(lowerCAmelCase_ ) _UpperCAmelCase : Tuple = 0.0 for _ in range(lowerCAmelCase_ ): # Approximates small segments of curve as linear and solve # for trapezoidal area _UpperCAmelCase : Any = (x_end - x_start) / steps + xa _UpperCAmelCase : List[Any] = fnc(lowerCAmelCase_ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step _UpperCAmelCase : Any = xa _UpperCAmelCase : str = fxa return area if __name__ == "__main__": def snake_case_ ( lowerCAmelCase_ )-> Any: '''simple docstring''' return x**3 + x**2 print("""f(x) = x^3 + x^2""") print("""The area between the curve, x = -5, x = 5 and the x axis is:""") A_ : List[str] = 1_0 while i <= 1_0_0_0_0_0: print(f"""with {i} steps: {trapezoidal_area(f, -5, 5, i)}""") i *= 1_0
349
1
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: int = 1_0_0_0 ) -> int: '''simple docstring''' A__ , A__ = 1, 1 A__ = 2 while True: A__ = 0 A__ = fa + fa A__ , A__ = fa, f index += 1 for _ in str(__lowerCAmelCase ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
68
from abc import ABC, abstractmethod from argparse import ArgumentParser class lowerCAmelCase__ ( __lowercase ): @staticmethod @abstractmethod def __A ( SCREAMING_SNAKE_CASE__ : ArgumentParser ) -> str: raise NotImplementedError() @abstractmethod def __A ( self : Optional[int] ) -> Union[str, Any]: raise NotImplementedError()
270
0
"""simple docstring""" def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->int: """simple docstring""" if index == number_of_items: return 0 lowerCAmelCase__ :Dict = 0 lowerCAmelCase__ :Union[str, Any] = 0 lowerCAmelCase__ :List[Any] = knapsack(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , index + 1 ) if weights[index] <= max_weight: lowerCAmelCase__ :Any = values[index] + knapsack( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , max_weight - weights[index] , index + 1 ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
354
"""simple docstring""" __A = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} __A = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->list[int]: """simple docstring""" lowerCAmelCase__ :Union[str, Any] = True lowerCAmelCase__ :Tuple = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) order.append(_SCREAMING_SNAKE_CASE ) return order def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->list[int]: """simple docstring""" lowerCAmelCase__ :Optional[int] = True lowerCAmelCase__ :Union[str, Any] = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return component def __A (_SCREAMING_SNAKE_CASE ) ->list[list[int]]: """simple docstring""" lowerCAmelCase__ :Any = len(_SCREAMING_SNAKE_CASE ) * [False] lowerCAmelCase__ :dict[int, list[int]] = {vert: [] for vert in range(len(_SCREAMING_SNAKE_CASE ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ :Dict = [] for i, was_visited in enumerate(_SCREAMING_SNAKE_CASE ): if not was_visited: order += topology_sort(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCAmelCase__ :List[Any] = [] lowerCAmelCase__ :int = len(_SCREAMING_SNAKE_CASE ) * [False] for i in range(len(_SCREAMING_SNAKE_CASE ) ): lowerCAmelCase__ :Dict = order[len(_SCREAMING_SNAKE_CASE ) - i - 1] if not visited[vert]: lowerCAmelCase__ :Union[str, Any] = find_components(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) components_list.append(_SCREAMING_SNAKE_CASE ) return components_list
254
0
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, 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.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class __magic_name__ ( unittest.TestCase ): def __init__( self : str ,_UpperCAmelCase : Optional[Any] ,_UpperCAmelCase : Any=13 ,_UpperCAmelCase : List[Any]=7 ,_UpperCAmelCase : List[Any]=True ,_UpperCAmelCase : Any=True ,_UpperCAmelCase : Optional[int]=True ,_UpperCAmelCase : str=True ,_UpperCAmelCase : Dict=99 ,_UpperCAmelCase : Optional[Any]=32 ,_UpperCAmelCase : str=5 ,_UpperCAmelCase : Union[str, Any]=4 ,_UpperCAmelCase : int=37 ,_UpperCAmelCase : Optional[int]="gelu" ,_UpperCAmelCase : Union[str, Any]=0.1 ,_UpperCAmelCase : int=0.1 ,_UpperCAmelCase : Optional[int]=512 ,_UpperCAmelCase : int=16 ,_UpperCAmelCase : Optional[int]=2 ,_UpperCAmelCase : Dict=0.02 ,_UpperCAmelCase : Dict=4 ,): _a : int = parent _a : int = batch_size _a : str = seq_length _a : Optional[int] = is_training _a : Optional[int] = use_attention_mask _a : Optional[Any] = use_token_type_ids _a : Union[str, Any] = use_labels _a : List[Any] = vocab_size _a : str = hidden_size _a : str = num_hidden_layers _a : List[Any] = num_attention_heads _a : Optional[Any] = intermediate_size _a : Optional[Any] = hidden_act _a : str = hidden_dropout_prob _a : Tuple = attention_probs_dropout_prob _a : Tuple = max_position_embeddings _a : List[Any] = type_vocab_size _a : str = type_sequence_label_size _a : Tuple = initializer_range _a : Union[str, Any] = num_choices def __lowercase ( self : Union[str, Any] ): _a : Optional[int] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) _a : Union[str, Any] = None if self.use_attention_mask: _a : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) _a : List[Any] = None if self.use_token_type_ids: _a : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) _a : str = RoFormerConfig( 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 ,) return config, input_ids, token_type_ids, attention_mask def __lowercase ( self : int ): _a : Optional[Any] = self.prepare_config_and_inputs() _a , _a , _a , _a : Tuple = config_and_inputs _a : List[Any] = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_flax class __magic_name__ ( a_ , unittest.TestCase ): lowerCAmelCase : Tuple = True lowerCAmelCase : Dict = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def __lowercase ( self : List[str] ): _a : str = FlaxRoFormerModelTester(self ) @slow def __lowercase ( self : List[Any] ): for model_class_name in self.all_model_classes: _a : int = model_class_name.from_pretrained('junnyu/roformer_chinese_small' ,from_pt=_UpperCAmelCase ) _a : Union[str, Any] = model(np.ones((1, 1) ) ) self.assertIsNotNone(_UpperCAmelCase ) @require_flax class __magic_name__ ( unittest.TestCase ): @slow def __lowercase ( self : List[str] ): _a : Tuple = FlaxRoFormerForMaskedLM.from_pretrained('junnyu/roformer_chinese_base' ) _a : Optional[Any] = jnp.array([[0, 1, 2, 3, 4, 5]] ) _a : List[str] = model(_UpperCAmelCase )[0] _a : Optional[int] = 50000 _a : Any = (1, 6, vocab_size) self.assertEqual(output.shape ,_UpperCAmelCase ) _a : str = jnp.array( [[[-0.12_05, -1.02_65, 0.29_22], [-1.51_34, 0.19_74, 0.15_19], [-5.01_35, -3.90_03, -0.84_04]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] ,_UpperCAmelCase ,atol=1E-4 ) )
89
import json import os from typing import Optional, Tuple import regex as re from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCAmelCase : List[str] = logging.get_logger(__name__) UpperCAmelCase : Tuple = { "vocab_file": "vocab.json", "merges_file": "merges.txt", } UpperCAmelCase : Optional[Any] = { "vocab_file": {"ctrl": "https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-vocab.json"}, "merges_file": {"ctrl": "https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-merges.txt"}, } UpperCAmelCase : Union[str, Any] = { "ctrl": 2_56, } UpperCAmelCase : List[str] = { "Pregnancy": 16_86_29, "Christianity": 76_75, "Explain": 10_64_23, "Fitness": 6_34_40, "Saving": 6_31_63, "Ask": 2_71_71, "Ass": 9_59_85, "Joke": 16_35_09, "Questions": 4_56_22, "Thoughts": 4_96_05, "Retail": 5_23_42, "Feminism": 16_43_38, "Writing": 1_19_92, "Atheism": 19_22_63, "Netflix": 4_86_16, "Computing": 3_96_39, "Opinion": 4_32_13, "Alone": 4_49_67, "Funny": 5_89_17, "Gaming": 4_03_58, "Human": 40_88, "India": 13_31, "Joker": 7_71_38, "Diet": 3_62_06, "Legal": 1_18_59, "Norman": 49_39, "Tip": 7_26_89, "Weight": 5_23_43, "Movies": 4_62_73, "Running": 2_34_25, "Science": 20_90, "Horror": 3_77_93, "Confession": 6_05_72, "Finance": 1_22_50, "Politics": 1_63_60, "Scary": 19_19_85, "Support": 1_26_54, "Technologies": 3_25_16, "Teenage": 6_61_60, "Event": 3_27_69, "Learned": 6_74_60, "Notion": 18_27_70, "Wikipedia": 3_75_83, "Books": 66_65, "Extract": 7_60_50, "Confessions": 10_27_01, "Conspiracy": 7_59_32, "Links": 6_36_74, "Narcissus": 15_04_25, "Relationship": 5_47_66, "Relationships": 13_47_96, "Reviews": 4_16_71, "News": 42_56, "Translation": 2_68_20, "multilingual": 12_84_06, } def __lowerCamelCase ( lowerCamelCase__ : Any ): '''simple docstring''' lowerCamelCase = set() lowerCamelCase = word[0] for char in word[1:]: pairs.add((prev_char, char) ) lowerCamelCase = char lowerCamelCase = set(lowerCamelCase__ ) return pairs class __lowercase ( a_ ): """simple docstring""" UpperCamelCase : Any = VOCAB_FILES_NAMES UpperCamelCase : str = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase : Optional[int] = CONTROL_CODES def __init__( self , A , A , A="<unk>" , **A ) -> int: '''simple docstring''' super().__init__(unk_token=A , **A ) with open(A , encoding="""utf-8""" ) as vocab_handle: lowerCamelCase = json.load(A ) lowerCamelCase = {v: k for k, v in self.encoder.items()} with open(A , encoding="""utf-8""" ) as merges_handle: lowerCamelCase = merges_handle.read().split("""\n""" )[1:-1] lowerCamelCase = [tuple(merge.split() ) for merge in merges] lowerCamelCase = dict(zip(A , range(len(A ) ) ) ) lowerCamelCase = {} @property def __A ( self ) -> Union[str, Any]: '''simple docstring''' return len(self.encoder ) def __A ( self ) -> List[str]: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def __A ( self , A ) -> str: '''simple docstring''' if token in self.cache: return self.cache[token] lowerCamelCase = tuple(A ) lowerCamelCase = tuple(list(word[:-1] ) + [word[-1] + """</w>"""] ) lowerCamelCase = get_pairs(A ) if not pairs: return token while True: lowerCamelCase = min(A , key=lambda A : self.bpe_ranks.get(A , float("""inf""" ) ) ) if bigram not in self.bpe_ranks: break lowerCamelCase , lowerCamelCase = bigram lowerCamelCase = [] lowerCamelCase = 0 while i < len(A ): try: lowerCamelCase = word.index(A , A ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) lowerCamelCase = j if word[i] == first and i < len(A ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 lowerCamelCase = tuple(A ) lowerCamelCase = new_word if len(A ) == 1: break else: lowerCamelCase = get_pairs(A ) lowerCamelCase = """@@ """.join(A ) lowerCamelCase = word[:-4] lowerCamelCase = word return word def __A ( self , A ) -> Optional[int]: '''simple docstring''' lowerCamelCase = [] lowerCamelCase = re.findall(r"""\S+\n?""" , A ) for token in words: split_tokens.extend(list(self.bpe(A ).split(""" """ ) ) ) return split_tokens def __A ( self , A ) -> int: '''simple docstring''' return self.encoder.get(A , self.encoder.get(self.unk_token ) ) def __A ( self , A ) -> Any: '''simple docstring''' return self.decoder.get(A , self.unk_token ) def __A ( self , A ) -> str: '''simple docstring''' lowerCamelCase = """ """.join(A ).replace("""@@ """ , """""" ).strip() return out_string def __A ( self , A , A = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(A ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return lowerCamelCase = os.path.join( A , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCamelCase = os.path.join( A , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""] ) with open(A , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=A , ensure_ascii=A ) + """\n""" ) lowerCamelCase = 0 with open(A , """w""" , encoding="""utf-8""" ) as writer: writer.write("""#version: 0.2\n""" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda A : kv[1] ): if index != token_index: logger.warning( F'Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.' """ Please check that the tokenizer is not corrupted!""" ) lowerCamelCase = token_index writer.write(""" """.join(A ) + """\n""" ) index += 1 return vocab_file, merge_file # def decode(self, token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=True): # filtered_tokens = ' '.join(self.convert_ids_to_tokens(token_ids, skip_special_tokens=skip_special_tokens)) # tokens_generated_so_far = re.sub('(@@ )', '', string=filtered_tokens) # tokens_generated_so_far = re.sub('(@@ ?$)', '', string=tokens_generated_so_far) # return ''.join(tokens_generated_so_far)
252
0
import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers __A = '''python tqdm regex requests packaging filelock numpy tokenizers'''.split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append('''dataclasses''') if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append('''importlib_metadata''') for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(f'can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py') def __a ( lowerCAmelCase_ : Dict ,lowerCAmelCase_ : List[Any]=None ) -> Dict: '''simple docstring''' require_version(deps[pkg] ,_SCREAMING_SNAKE_CASE )
351
from typing import List, Optional, Tuple, Union import torch from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class lowercase ( snake_case__): """simple docstring""" def __init__( self : Dict , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[Any] ) -> List[str]: super().__init__() # make sure scheduler can always be converted to DDIM UpperCAmelCase_= DDIMScheduler.from_config(scheduler.config ) self.register_modules(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) @torch.no_grad() def __call__( self : Union[str, Any] , __UpperCAmelCase : int = 1 , __UpperCAmelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : Optional[bool] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , ) -> Union[ImagePipelineOutput, Tuple]: # Sample gaussian noise to begin loop if isinstance(self.unet.config.sample_size , __UpperCAmelCase ): UpperCAmelCase_= ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size, ) else: UpperCAmelCase_= (batch_size, self.unet.config.in_channels, *self.unet.config.sample_size) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) and len(__UpperCAmelCase ) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(__UpperCAmelCase )}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) UpperCAmelCase_= randn_tensor(__UpperCAmelCase , generator=__UpperCAmelCase , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(__UpperCAmelCase ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output UpperCAmelCase_= self.unet(__UpperCAmelCase , __UpperCAmelCase ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 UpperCAmelCase_= self.scheduler.step( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , eta=__UpperCAmelCase , use_clipped_model_output=__UpperCAmelCase , generator=__UpperCAmelCase ).prev_sample UpperCAmelCase_= (image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase_= image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase_= self.numpy_to_pil(__UpperCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=__UpperCAmelCase )
277
0
"""simple docstring""" import argparse import torch # Step 1. clone https://github.com/microsoft/unilm # Step 2. git checkout to https://github.com/microsoft/unilm/commit/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd # Step 3. cd unilm # Step 4. ln -s $(realpath wavlm/modules.py) ./ # create simlink # import classes from unilm.wavlm.WavLM import WavLM as WavLMOrig from unilm.wavlm.WavLM import WavLMConfig as WavLMConfigOrig from transformers import WavLMConfig, WavLMModel, logging logging.set_verbosity_info() A: Optional[int] = logging.get_logger(__name__) A: Tuple = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn.grep_linear": "encoder.layers.*.attention.gru_rel_pos_linear", "self_attn.relative_attention_bias": "encoder.layers.*.attention.rel_attn_embed", "self_attn.grep_a": "encoder.layers.*.attention.gru_rel_pos_const", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "ctc_proj", "mask_emb": "masked_spec_embed", } A: List[str] = [ "ctc_proj", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def _snake_case ( UpperCamelCase : str , UpperCamelCase : Tuple , UpperCamelCase : Optional[int] , UpperCamelCase : Tuple , UpperCamelCase : Any ): for attribute in key.split(""".""" ): UpperCAmelCase : Optional[Any] = getattr(UpperCamelCase , UpperCamelCase ) if weight_type is not None: UpperCAmelCase : List[Any] = getattr(UpperCamelCase , UpperCamelCase ).shape else: UpperCAmelCase : str = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": UpperCAmelCase : Optional[Any] = value elif weight_type == "weight_g": UpperCAmelCase : str = value elif weight_type == "weight_v": UpperCAmelCase : Union[str, Any] = value elif weight_type == "bias": UpperCAmelCase : str = value else: UpperCAmelCase : Union[str, Any] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def _snake_case ( UpperCamelCase : List[Any] , UpperCamelCase : Optional[Any] ): UpperCAmelCase : Tuple = [] UpperCAmelCase : Any = fairseq_model.state_dict() UpperCAmelCase : Tuple = hf_model.feature_extractor for name, value in fairseq_dict.items(): UpperCAmelCase : str = False if "conv_layers" in name: load_conv_layer( UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , hf_model.config.feat_extract_norm == """group""" , ) UpperCAmelCase : Union[str, Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: UpperCAmelCase : Dict = True if "*" in mapped_key: UpperCAmelCase : str = name.split(UpperCamelCase )[0].split(""".""" )[-2] UpperCAmelCase : Tuple = mapped_key.replace("""*""" , UpperCamelCase ) if "weight_g" in name: UpperCAmelCase : Any = """weight_g""" elif "weight_v" in name: UpperCAmelCase : Optional[Any] = """weight_v""" elif "bias" in name and "relative_attention_bias" not in name: UpperCAmelCase : Union[str, Any] = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj UpperCAmelCase : str = """weight""" else: UpperCAmelCase : Optional[Any] = None set_recursively(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) continue if not is_used: unused_weights.append(UpperCamelCase ) logger.warning(F"Unused weights: {unused_weights}" ) def _snake_case ( UpperCamelCase : Optional[Any] , UpperCamelCase : Dict , UpperCamelCase : Tuple , UpperCamelCase : Any , UpperCamelCase : Any ): UpperCAmelCase : str = full_name.split("""conv_layers.""" )[-1] UpperCAmelCase : Dict = name.split(""".""" ) UpperCAmelCase : List[str] = int(items[0] ) UpperCAmelCase : Any = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) UpperCAmelCase : Optional[Any] = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) UpperCAmelCase : Tuple = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) UpperCAmelCase : str = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) UpperCAmelCase : Optional[Any] = 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 _snake_case ( UpperCamelCase : int , UpperCamelCase : List[Any] , UpperCamelCase : List[Any]=None ): # load the pre-trained checkpoints UpperCAmelCase : List[Any] = torch.load(UpperCamelCase ) UpperCAmelCase : List[str] = WavLMConfigOrig(checkpoint["""cfg"""] ) UpperCAmelCase : Optional[int] = WavLMOrig(UpperCamelCase ) model.load_state_dict(checkpoint["""model"""] ) model.eval() if config_path is not None: UpperCAmelCase : List[str] = WavLMConfig.from_pretrained(UpperCamelCase ) else: UpperCAmelCase : List[Any] = WavLMConfig() UpperCAmelCase : Any = WavLMModel(UpperCamelCase ) recursively_load_weights(UpperCamelCase , UpperCamelCase ) hf_wavlm.save_pretrained(UpperCamelCase ) if __name__ == "__main__": A: int = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") A: Tuple = parser.parse_args() convert_wavlm_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
109
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A: str = { "configuration_distilbert": [ "DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DistilBertConfig", "DistilBertOnnxConfig", ], "tokenization_distilbert": ["DistilBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A: Dict = ["DistilBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A: List[Any] = [ "DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "DistilBertForMaskedLM", "DistilBertForMultipleChoice", "DistilBertForQuestionAnswering", "DistilBertForSequenceClassification", "DistilBertForTokenClassification", "DistilBertModel", "DistilBertPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A: Dict = [ "TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDistilBertForMaskedLM", "TFDistilBertForMultipleChoice", "TFDistilBertForQuestionAnswering", "TFDistilBertForSequenceClassification", "TFDistilBertForTokenClassification", "TFDistilBertMainLayer", "TFDistilBertModel", "TFDistilBertPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A: int = [ "FlaxDistilBertForMaskedLM", "FlaxDistilBertForMultipleChoice", "FlaxDistilBertForQuestionAnswering", "FlaxDistilBertForSequenceClassification", "FlaxDistilBertForTokenClassification", "FlaxDistilBertModel", "FlaxDistilBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_distilbert import ( DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, DistilBertConfig, DistilBertOnnxConfig, ) from .tokenization_distilbert import DistilBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_distilbert_fast import DistilBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_distilbert import ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, DistilBertPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_distilbert import ( TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDistilBertForMaskedLM, TFDistilBertForMultipleChoice, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertMainLayer, TFDistilBertModel, TFDistilBertPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_distilbert import ( FlaxDistilBertForMaskedLM, FlaxDistilBertForMultipleChoice, FlaxDistilBertForQuestionAnswering, FlaxDistilBertForSequenceClassification, FlaxDistilBertForTokenClassification, FlaxDistilBertModel, FlaxDistilBertPreTrainedModel, ) else: import sys A: Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
109
1
# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.: # python ./utils/get_modified_files.py utils src tests examples # # it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered # since the output of this script is fed into Makefile commands it doesn't print a newline after the results import re import subprocess import sys UpperCamelCase__ = subprocess.check_output("""git merge-base main HEAD""".split()).decode("""utf-8""") UpperCamelCase__ = ( subprocess.check_output(f'''git diff --diff-filter=d --name-only {fork_point_sha}'''.split()).decode("""utf-8""").split() ) UpperCamelCase__ = """|""".join(sys.argv[1:]) UpperCamelCase__ = re.compile(Rf'''^({joined_dirs}).*?\.py$''') UpperCamelCase__ = [x for x in modified_files if regex.match(x)] print(""" """.join(relevant_modified_files), end="""""")
355
import unittest import numpy as np import torch from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class a__ ( unittest.TestCase ): @property def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" torch.manual_seed(0 ) __lowerCAmelCase = UNetaDModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=3 , out_channels=3 , down_block_types=("DownBlock2D", "AttnDownBlock2D") , up_block_types=("AttnUpBlock2D", "UpBlock2D") , ) return model def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = self.dummy_uncond_unet __lowerCAmelCase = ScoreSdeVeScheduler() __lowerCAmelCase = ScoreSdeVePipeline(unet=_A , scheduler=_A ) sde_ve.to(_A ) sde_ve.set_progress_bar_config(disable=_A ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = sde_ve(num_inference_steps=2 , output_type="numpy" , generator=_A ).images __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = sde_ve(num_inference_steps=2 , output_type="numpy" , generator=_A , return_dict=_A )[ 0 ] __lowerCAmelCase = image[0, -3:, -3:, -1] __lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) __lowerCAmelCase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch class a__ ( unittest.TestCase ): def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = "google/ncsnpp-church-256" __lowerCAmelCase = UNetaDModel.from_pretrained(_A ) __lowerCAmelCase = ScoreSdeVeScheduler.from_pretrained(_A ) __lowerCAmelCase = ScoreSdeVePipeline(unet=_A , scheduler=_A ) sde_ve.to(_A ) sde_ve.set_progress_bar_config(disable=_A ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = sde_ve(num_inference_steps=1_0 , output_type="numpy" , generator=_A ).images __lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) __lowerCAmelCase = np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
102
0
'''simple docstring''' from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging a__ : Optional[Any] = logging.get_logger(__name__) a__ : Dict = { 'EleutherAI/gpt-j-6B': 'https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json', # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class UpperCAmelCase__ ( UpperCAmelCase_): __SCREAMING_SNAKE_CASE = '''gptj''' __SCREAMING_SNAKE_CASE = { '''max_position_embeddings''': '''n_positions''', '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self , lowercase=5_0_4_0_0 , lowercase=2_0_4_8 , lowercase=4_0_9_6 , lowercase=2_8 , lowercase=1_6 , lowercase=6_4 , lowercase=None , lowercase="gelu_new" , lowercase=0.0 , lowercase=0.0 , lowercase=0.0 , lowercase=1E-5 , lowercase=0.02 , lowercase=True , lowercase=5_0_2_5_6 , lowercase=5_0_2_5_6 , lowercase=False , **lowercase , ) -> Tuple: __UpperCamelCase = vocab_size __UpperCamelCase = n_positions __UpperCamelCase = n_embd __UpperCamelCase = n_layer __UpperCamelCase = n_head __UpperCamelCase = n_inner __UpperCamelCase = rotary_dim __UpperCamelCase = activation_function __UpperCamelCase = resid_pdrop __UpperCamelCase = embd_pdrop __UpperCamelCase = attn_pdrop __UpperCamelCase = layer_norm_epsilon __UpperCamelCase = initializer_range __UpperCamelCase = use_cache __UpperCamelCase = bos_token_id __UpperCamelCase = eos_token_id super().__init__( bos_token_id=lowercase , eos_token_id=lowercase , tie_word_embeddings=lowercase , **lowercase ) class UpperCAmelCase__ ( UpperCAmelCase_): def __init__( self , lowercase , lowercase = "default" , lowercase = None , lowercase = False , ) -> List[str]: super().__init__(lowercase , task=lowercase , patching_specs=lowercase , use_past=lowercase ) if not getattr(self._config , """pad_token_id""" , lowercase ): # TODO: how to do that better? __UpperCamelCase = 0 @property def __lowerCamelCase ( self ) -> Mapping[str, Mapping[int, str]]: __UpperCamelCase = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(lowercase , direction="""inputs""" ) __UpperCamelCase = {0: """batch""", 1: """past_sequence + sequence"""} else: __UpperCamelCase = {0: """batch""", 1: """sequence"""} return common_inputs @property def __lowerCamelCase ( self ) -> int: return self._config.n_layer @property def __lowerCamelCase ( self ) -> int: return self._config.n_head def __lowerCamelCase ( self , lowercase , lowercase = -1 , lowercase = -1 , lowercase = False , lowercase = None , ) -> Mapping[str, Any]: __UpperCamelCase = super(lowercase , self ).generate_dummy_inputs( lowercase , batch_size=lowercase , seq_length=lowercase , is_pair=lowercase , framework=lowercase ) # We need to order the input in the way they appears in the forward() __UpperCamelCase = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch __UpperCamelCase , __UpperCamelCase = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values __UpperCamelCase = seqlen + 2 __UpperCamelCase = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) __UpperCamelCase = [ (torch.zeros(lowercase ), torch.zeros(lowercase )) for _ in range(self.num_layers ) ] __UpperCamelCase = common_inputs["""attention_mask"""] if self.use_past: __UpperCamelCase = ordered_inputs["""attention_mask"""].dtype __UpperCamelCase = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(lowercase , lowercase , dtype=lowercase )] , dim=1 ) return ordered_inputs @property def __lowerCamelCase ( self ) -> int: return 1_3
349
'''simple docstring''' 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() a__ : Any = logging.get_logger(__name__) a__ : Optional[int] = { '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', } a__ : List[str] = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', 'projector', 'classifier', ] def _lowercase ( __A ): '''simple docstring''' __UpperCamelCase = {} with open(__A ,"""r""" ) as file: for line_number, line in enumerate(__A ): __UpperCamelCase = line.strip() if line: __UpperCamelCase = line.split() __UpperCamelCase = line_number __UpperCamelCase = words[0] __UpperCamelCase = value return result def _lowercase ( __A ,__A ,__A ,__A ,__A ): '''simple docstring''' for attribute in key.split(""".""" ): __UpperCamelCase = getattr(__A ,__A ) __UpperCamelCase = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(__A ): __UpperCamelCase = PARAM_MAPPING[full_name.split(""".""" )[-1]] __UpperCamelCase = """param""" if weight_type is not None and weight_type != "param": __UpperCamelCase = getattr(__A ,__A ).shape elif weight_type is not None and weight_type == "param": __UpperCamelCase = hf_pointer for attribute in hf_param_name.split(""".""" ): __UpperCamelCase = getattr(__A ,__A ) __UpperCamelCase = shape_pointer.shape # let's reduce dimension __UpperCamelCase = value[0] else: __UpperCamelCase = 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": __UpperCamelCase = value elif weight_type == "weight_g": __UpperCamelCase = value elif weight_type == "weight_v": __UpperCamelCase = value elif weight_type == "bias": __UpperCamelCase = value elif weight_type == "param": for attribute in hf_param_name.split(""".""" ): __UpperCamelCase = getattr(__A ,__A ) __UpperCamelCase = value else: __UpperCamelCase = value logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def _lowercase ( __A ,__A ,__A ,__A ,__A ): '''simple docstring''' __UpperCamelCase = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(__A ): __UpperCamelCase = PARAM_MAPPING[full_name.split(""".""" )[-1]] __UpperCamelCase = """param""" if weight_type is not None and weight_type != "param": __UpperCamelCase = """.""".join([key, weight_type] ) elif weight_type is not None and weight_type == "param": __UpperCamelCase = """.""".join([key, hf_param_name] ) else: __UpperCamelCase = key __UpperCamelCase = value if """lm_head""" in full_key else value[0] a__ : Dict = { '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 _lowercase ( __A ,__A ,__A=None ,__A=None ): '''simple docstring''' __UpperCamelCase = False for key, mapped_key in MAPPING.items(): __UpperCamelCase = """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]: __UpperCamelCase = True if "*" in mapped_key: __UpperCamelCase = name.split(__A )[0].split(""".""" )[-2] __UpperCamelCase = mapped_key.replace("""*""" ,__A ) if "weight_g" in name: __UpperCamelCase = """weight_g""" elif "weight_v" in name: __UpperCamelCase = """weight_v""" elif "bias" in name: __UpperCamelCase = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj __UpperCamelCase = """weight""" else: __UpperCamelCase = None if hf_dict is not None: rename_dict(__A ,__A ,__A ,__A ,__A ) else: set_recursively(__A ,__A ,__A ,__A ,__A ) return is_used return is_used def _lowercase ( __A ,__A ,__A ): '''simple docstring''' __UpperCamelCase = [] __UpperCamelCase = fairseq_model.state_dict() __UpperCamelCase = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): __UpperCamelCase = False if "conv_layers" in name: load_conv_layer( __A ,__A ,__A ,__A ,hf_model.config.feat_extract_norm == """group""" ,) __UpperCamelCase = True else: __UpperCamelCase = load_wavaveca_layer(__A ,__A ,__A ) if not is_used: unused_weights.append(__A ) logger.warning(f"Unused weights: {unused_weights}" ) def _lowercase ( __A ,__A ,__A ,__A ,__A ): '''simple docstring''' __UpperCamelCase = full_name.split("""conv_layers.""" )[-1] __UpperCamelCase = name.split(""".""" ) __UpperCamelCase = int(items[0] ) __UpperCamelCase = 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." ) __UpperCamelCase = 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." ) __UpperCamelCase = 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." ) __UpperCamelCase = 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." ) __UpperCamelCase = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(__A ) @torch.no_grad() def _lowercase ( __A ,__A ,__A=None ,__A=None ,__A=True ,__A=False ): '''simple docstring''' if config_path is not None: __UpperCamelCase = WavaVecaConfig.from_pretrained(__A ) else: __UpperCamelCase = WavaVecaConfig() if is_seq_class: __UpperCamelCase = read_txt_into_dict(__A ) __UpperCamelCase = idalabel __UpperCamelCase = WavaVecaForSequenceClassification(__A ) __UpperCamelCase = WavaVecaFeatureExtractor( feature_size=1 ,sampling_rate=16_000 ,padding_value=0 ,do_normalize=__A ,return_attention_mask=__A ,) feature_extractor.save_pretrained(__A ) elif is_finetuned: if dict_path: __UpperCamelCase = Dictionary.load(__A ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __UpperCamelCase = target_dict.pad_index __UpperCamelCase = target_dict.bos_index __UpperCamelCase = target_dict.eos_index __UpperCamelCase = len(target_dict.symbols ) __UpperCamelCase = os.path.join(__A ,"""vocab.json""" ) if not os.path.isdir(__A ): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(__A ) ) return os.makedirs(__A ,exist_ok=__A ) __UpperCamelCase = target_dict.indices # fairseq has the <pad> and <s> switched __UpperCamelCase = 0 __UpperCamelCase = 1 with open(__A ,"""w""" ,encoding="""utf-8""" ) as vocab_handle: json.dump(__A ,__A ) __UpperCamelCase = WavaVecaCTCTokenizer( __A ,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=__A ,) __UpperCamelCase = True if config.feat_extract_norm == """layer""" else False __UpperCamelCase = WavaVecaFeatureExtractor( feature_size=1 ,sampling_rate=16_000 ,padding_value=0 ,do_normalize=__A ,return_attention_mask=__A ,) __UpperCamelCase = WavaVecaProcessor(feature_extractor=__A ,tokenizer=__A ) processor.save_pretrained(__A ) __UpperCamelCase = WavaVecaForCTC(__A ) else: __UpperCamelCase = WavaVecaForPreTraining(__A ) if is_finetuned or is_seq_class: __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] ,arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) else: __UpperCamelCase = argparse.Namespace(task="""audio_pretraining""" ) __UpperCamelCase = fairseq.tasks.setup_task(__A ) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ,task=__A ) __UpperCamelCase = model[0].eval() recursively_load_weights(__A ,__A ,not is_finetuned ) hf_wavavec.save_pretrained(__A ) if __name__ == "__main__": a__ : int = 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', ) a__ : Optional[int] = parser.parse_args() a__ : 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, )
349
1
"""simple docstring""" A_ : List[str] ={ """meter""": """m""", """kilometer""": """km""", """megametre""": """Mm""", """gigametre""": """Gm""", """terametre""": """Tm""", """petametre""": """Pm""", """exametre""": """Em""", """zettametre""": """Zm""", """yottametre""": """Ym""", } # Exponent of the factor(meter) A_ : Any ={ """m""": 0, """km""": 3, """Mm""": 6, """Gm""": 9, """Tm""": 1_2, """Pm""": 1_5, """Em""": 1_8, """Zm""": 2_1, """Ym""": 2_4, } def SCREAMING_SNAKE_CASE_ ( snake_case : float , snake_case : str , snake_case : str )-> float: _lowerCamelCase = from_type.lower().strip('s' ) _lowerCamelCase = to_type.lower().strip('s' ) _lowerCamelCase = UNIT_SYMBOL.get(snake_case , snake_case ) _lowerCamelCase = UNIT_SYMBOL.get(snake_case , snake_case ) if from_sanitized not in METRIC_CONVERSION: _lowerCamelCase = ( f'Invalid \'from_type\' value: {from_type!r}.\n' f'Conversion abbreviations are: {", ".join(snake_case )}' ) raise ValueError(snake_case ) if to_sanitized not in METRIC_CONVERSION: _lowerCamelCase = ( f'Invalid \'to_type\' value: {to_type!r}.\n' f'Conversion abbreviations are: {", ".join(snake_case )}' ) raise ValueError(snake_case ) _lowerCamelCase = METRIC_CONVERSION[from_sanitized] _lowerCamelCase = METRIC_CONVERSION[to_sanitized] _lowerCamelCase = 1 if from_exponent > to_exponent: _lowerCamelCase = from_exponent - to_exponent else: _lowerCamelCase = -(to_exponent - from_exponent) return value * pow(10 , snake_case ) if __name__ == "__main__": from doctest import testmod testmod()
80
"""simple docstring""" from dataclasses import dataclass from typing import Dict, Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .attention_processor import AttentionProcessor, AttnProcessor from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder @dataclass class __a ( lowerCAmelCase__ ): SCREAMING_SNAKE_CASE__ : "DiagonalGaussianDistribution" class __a ( lowerCAmelCase__ , lowerCAmelCase__ ): SCREAMING_SNAKE_CASE__ : Any = True @register_to_config def __init__( self , a__ = 3 , a__ = 3 , a__ = ("DownEncoderBlock2D",) , a__ = ("UpDecoderBlock2D",) , a__ = (64,) , a__ = 1 , a__ = "silu" , a__ = 4 , a__ = 32 , a__ = 32 , a__ = 0.18215 , ): super().__init__() # pass init params to Encoder _lowerCamelCase = Encoder( in_channels=a__ , out_channels=a__ , down_block_types=a__ , block_out_channels=a__ , layers_per_block=a__ , act_fn=a__ , norm_num_groups=a__ , double_z=a__ , ) # pass init params to Decoder _lowerCamelCase = Decoder( in_channels=a__ , out_channels=a__ , up_block_types=a__ , block_out_channels=a__ , layers_per_block=a__ , norm_num_groups=a__ , act_fn=a__ , ) _lowerCamelCase = nn.Convad(2 * latent_channels , 2 * latent_channels , 1 ) _lowerCamelCase = nn.Convad(a__ , a__ , 1 ) _lowerCamelCase = False _lowerCamelCase = False # only relevant if vae tiling is enabled _lowerCamelCase = self.config.sample_size _lowerCamelCase = ( self.config.sample_size[0] if isinstance(self.config.sample_size , (list, tuple) ) else self.config.sample_size ) _lowerCamelCase = int(sample_size / (2 ** (len(self.config.block_out_channels ) - 1)) ) _lowerCamelCase = 0.25 def snake_case_ ( self , a__ , a__=False ): if isinstance(a__ , (Encoder, Decoder) ): _lowerCamelCase = value def snake_case_ ( self , a__ = True ): _lowerCamelCase = use_tiling def snake_case_ ( self ): self.enable_tiling(a__ ) def snake_case_ ( self ): _lowerCamelCase = True def snake_case_ ( self ): _lowerCamelCase = False @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def snake_case_ ( self ): _lowerCamelCase = {} def fn_recursive_add_processors(a__ , a__ , a__ ): if hasattr(a__ , 'set_processor' ): _lowerCamelCase = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(F'{name}.{sub_name}' , a__ , a__ ) return processors for name, module in self.named_children(): fn_recursive_add_processors(a__ , a__ , a__ ) return processors def snake_case_ ( self , a__ ): _lowerCamelCase = len(self.attn_processors.keys() ) if isinstance(a__ , a__ ) and len(a__ ) != count: raise ValueError( F'A dict of processors was passed, but the number of processors {len(a__ )} does not match the' F' number of attention layers: {count}. Please make sure to pass {count} processor classes.' ) def fn_recursive_attn_processor(a__ , a__ , a__ ): if hasattr(a__ , 'set_processor' ): if not isinstance(a__ , a__ ): module.set_processor(a__ ) else: module.set_processor(processor.pop(F'{name}.processor' ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(F'{name}.{sub_name}' , a__ , a__ ) for name, module in self.named_children(): fn_recursive_attn_processor(a__ , a__ , a__ ) def snake_case_ ( self ): self.set_attn_processor(AttnProcessor() ) @apply_forward_hook def snake_case_ ( self , a__ , a__ = True ): if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size): return self.tiled_encode(a__ , return_dict=a__ ) if self.use_slicing and x.shape[0] > 1: _lowerCamelCase = [self.encoder(a__ ) for x_slice in x.split(1 )] _lowerCamelCase = torch.cat(a__ ) else: _lowerCamelCase = self.encoder(a__ ) _lowerCamelCase = self.quant_conv(a__ ) _lowerCamelCase = DiagonalGaussianDistribution(a__ ) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=a__ ) def snake_case_ ( self , a__ , a__ = True ): if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size): return self.tiled_decode(a__ , return_dict=a__ ) _lowerCamelCase = self.post_quant_conv(a__ ) _lowerCamelCase = self.decoder(a__ ) if not return_dict: return (dec,) return DecoderOutput(sample=a__ ) @apply_forward_hook def snake_case_ ( self , a__ , a__ = True ): if self.use_slicing and z.shape[0] > 1: _lowerCamelCase = [self._decode(a__ ).sample for z_slice in z.split(1 )] _lowerCamelCase = torch.cat(a__ ) else: _lowerCamelCase = self._decode(a__ ).sample if not return_dict: return (decoded,) return DecoderOutput(sample=a__ ) def snake_case_ ( self , a__ , a__ , a__ ): _lowerCamelCase = min(a.shape[2] , b.shape[2] , a__ ) for y in range(a__ ): _lowerCamelCase = a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent) return b def snake_case_ ( self , a__ , a__ , a__ ): _lowerCamelCase = min(a.shape[3] , b.shape[3] , a__ ) for x in range(a__ ): _lowerCamelCase = a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent) return b def snake_case_ ( self , a__ , a__ = True ): _lowerCamelCase = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor) ) _lowerCamelCase = int(self.tile_latent_min_size * self.tile_overlap_factor ) _lowerCamelCase = self.tile_latent_min_size - blend_extent # Split the image into 512x512 tiles and encode them separately. _lowerCamelCase = [] for i in range(0 , x.shape[2] , a__ ): _lowerCamelCase = [] for j in range(0 , x.shape[3] , a__ ): _lowerCamelCase = x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size] _lowerCamelCase = self.encoder(a__ ) _lowerCamelCase = self.quant_conv(a__ ) row.append(a__ ) rows.append(a__ ) _lowerCamelCase = [] for i, row in enumerate(a__ ): _lowerCamelCase = [] for j, tile in enumerate(a__ ): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: _lowerCamelCase = self.blend_v(rows[i - 1][j] , a__ , a__ ) if j > 0: _lowerCamelCase = self.blend_h(row[j - 1] , a__ , a__ ) result_row.append(tile[:, :, :row_limit, :row_limit] ) result_rows.append(torch.cat(a__ , dim=3 ) ) _lowerCamelCase = torch.cat(a__ , dim=2 ) _lowerCamelCase = DiagonalGaussianDistribution(a__ ) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=a__ ) def snake_case_ ( self , a__ , a__ = True ): _lowerCamelCase = int(self.tile_latent_min_size * (1 - self.tile_overlap_factor) ) _lowerCamelCase = int(self.tile_sample_min_size * self.tile_overlap_factor ) _lowerCamelCase = self.tile_sample_min_size - blend_extent # Split z into overlapping 64x64 tiles and decode them separately. # The tiles have an overlap to avoid seams between tiles. _lowerCamelCase = [] for i in range(0 , z.shape[2] , a__ ): _lowerCamelCase = [] for j in range(0 , z.shape[3] , a__ ): _lowerCamelCase = z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size] _lowerCamelCase = self.post_quant_conv(a__ ) _lowerCamelCase = self.decoder(a__ ) row.append(a__ ) rows.append(a__ ) _lowerCamelCase = [] for i, row in enumerate(a__ ): _lowerCamelCase = [] for j, tile in enumerate(a__ ): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: _lowerCamelCase = self.blend_v(rows[i - 1][j] , a__ , a__ ) if j > 0: _lowerCamelCase = self.blend_h(row[j - 1] , a__ , a__ ) result_row.append(tile[:, :, :row_limit, :row_limit] ) result_rows.append(torch.cat(a__ , dim=3 ) ) _lowerCamelCase = torch.cat(a__ , dim=2 ) if not return_dict: return (dec,) return DecoderOutput(sample=a__ ) def snake_case_ ( self , a__ , a__ = False , a__ = True , a__ = None , ): _lowerCamelCase = sample _lowerCamelCase = self.encode(a__ ).latent_dist if sample_posterior: _lowerCamelCase = posterior.sample(generator=a__ ) else: _lowerCamelCase = posterior.mode() _lowerCamelCase = self.decode(a__ ).sample if not return_dict: return (dec,) return DecoderOutput(sample=a__ )
80
1
"""simple docstring""" import os # Precomputes a list of the 100 first triangular numbers lowerCAmelCase__ = [int(0.5 * n * (n + 1)) for n in range(1, 101)] def a__ ( ): '''simple docstring''' lowerCAmelCase : Dict = os.path.dirname(os.path.realpath(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : List[Any] = os.path.join(SCREAMING_SNAKE_CASE , "words.txt" ) lowerCAmelCase : Dict = "" with open(SCREAMING_SNAKE_CASE ) as f: lowerCAmelCase : Tuple = f.readline() lowerCAmelCase : Union[str, Any] = [word.strip("\"" ) for word in words.strip("\r\n" ).split("," )] lowerCAmelCase : List[str] = [ word for word in [sum(ord(SCREAMING_SNAKE_CASE ) - 6_4 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": print(solution())
108
'''simple docstring''' import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def lowercase_ ( lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Dict ): """simple docstring""" return params[f'{prefix}/{prefix}/relpos_bias/rel_embedding'][:, i, :] def lowercase_ ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any="attention" ): """simple docstring""" __UpperCAmelCase : int = np.ascontiguousarray(params[f'{prefix}/{prefix}/{layer_name}/key/kernel'][:, i, :, :] ) __UpperCAmelCase : Tuple = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) __UpperCAmelCase : Tuple = np.ascontiguousarray(params[f'{prefix}/{prefix}/{layer_name}/out/kernel'][:, i, :, :] ) __UpperCAmelCase : List[str] = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) __UpperCAmelCase : List[str] = np.ascontiguousarray(params[f'{prefix}/{prefix}/{layer_name}/query/kernel'][:, i, :, :] ) __UpperCAmelCase : List[str] = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) __UpperCAmelCase : Optional[Any] = np.ascontiguousarray(params[f'{prefix}/{prefix}/{layer_name}/value/kernel'][:, i, :, :] ) __UpperCAmelCase : Dict = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def lowercase_ ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[Any]=False ): """simple docstring""" if split_mlp_wi: __UpperCAmelCase : List[str] = params[f'{prefix}/{prefix}/mlp/wi_0/kernel'][:, i, :] __UpperCAmelCase : Union[str, Any] = params[f'{prefix}/{prefix}/mlp/wi_1/kernel'][:, i, :] __UpperCAmelCase : Dict = (wi_a, wi_a) else: __UpperCAmelCase : Union[str, Any] = params[f'{prefix}/{prefix}/mlp/wi/kernel'][:, i, :] __UpperCAmelCase : Tuple = params[f'{prefix}/{prefix}/mlp/wo/kernel'][:, i, :] return wi, wo def lowercase_ ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any] ): """simple docstring""" return params[f'{prefix}/{prefix}/{layer_name}/scale'][:, i] def lowercase_ ( lowerCAmelCase__ : dict , *, lowerCAmelCase__ : int , lowerCAmelCase__ : bool , lowerCAmelCase__ : bool = False ): """simple docstring""" __UpperCAmelCase : Tuple = traverse_util.flatten_dict(variables["""target"""] ) __UpperCAmelCase : Union[str, Any] = {"""/""".join(lowerCAmelCase__ ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi __UpperCAmelCase : Any = """encoder/encoder/mlp/wi_0/kernel""" in old print("""Split MLP:""" , lowerCAmelCase__ ) __UpperCAmelCase : Any = collections.OrderedDict() # Shared embeddings. __UpperCAmelCase : int = old["""token_embedder/embedding"""] # Encoder. for i in range(lowerCAmelCase__ ): # Block i, layer 0 (Self Attention). __UpperCAmelCase : Union[str, Any] = tax_layer_norm_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """encoder""" , """pre_attention_layer_norm""" ) __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : Tuple = tax_attention_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """encoder""" , """attention""" ) __UpperCAmelCase : Any = layer_norm __UpperCAmelCase : List[Any] = k.T __UpperCAmelCase : Optional[int] = o.T __UpperCAmelCase : str = q.T __UpperCAmelCase : Any = v.T # Block i, layer 1 (MLP). __UpperCAmelCase : List[str] = tax_layer_norm_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """encoder""" , """pre_mlp_layer_norm""" ) __UpperCAmelCase , __UpperCAmelCase : int = tax_mlp_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """encoder""" , lowerCAmelCase__ ) __UpperCAmelCase : Optional[int] = layer_norm if split_mlp_wi: __UpperCAmelCase : List[Any] = wi[0].T __UpperCAmelCase : Any = wi[1].T else: __UpperCAmelCase : Tuple = wi.T __UpperCAmelCase : Tuple = wo.T if scalable_attention: # convert the rel_embedding of each layer __UpperCAmelCase : Dict = tax_relpos_bias_lookup( lowerCAmelCase__ , lowerCAmelCase__ , """encoder""" ).T __UpperCAmelCase : Optional[int] = old["""encoder/encoder_norm/scale"""] if not scalable_attention: __UpperCAmelCase : Any = tax_relpos_bias_lookup( lowerCAmelCase__ , 0 , """encoder""" ).T __UpperCAmelCase : Dict = tax_relpos_bias_lookup( lowerCAmelCase__ , 0 , """decoder""" ).T if not is_encoder_only: # Decoder. for i in range(lowerCAmelCase__ ): # Block i, layer 0 (Self Attention). __UpperCAmelCase : str = tax_layer_norm_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """decoder""" , """pre_self_attention_layer_norm""" ) __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : Dict = tax_attention_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """decoder""" , """self_attention""" ) __UpperCAmelCase : int = layer_norm __UpperCAmelCase : Optional[Any] = k.T __UpperCAmelCase : Dict = o.T __UpperCAmelCase : int = q.T __UpperCAmelCase : List[str] = v.T # Block i, layer 1 (Cross Attention). __UpperCAmelCase : Any = tax_layer_norm_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """decoder""" , """pre_cross_attention_layer_norm""" ) __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : str = tax_attention_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """decoder""" , """encoder_decoder_attention""" ) __UpperCAmelCase : Union[str, Any] = layer_norm __UpperCAmelCase : List[Any] = k.T __UpperCAmelCase : int = o.T __UpperCAmelCase : Optional[int] = q.T __UpperCAmelCase : Optional[int] = v.T # Block i, layer 2 (MLP). __UpperCAmelCase : Tuple = tax_layer_norm_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """decoder""" , """pre_mlp_layer_norm""" ) __UpperCAmelCase , __UpperCAmelCase : Any = tax_mlp_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """decoder""" , lowerCAmelCase__ ) __UpperCAmelCase : Optional[int] = layer_norm if split_mlp_wi: __UpperCAmelCase : Optional[Any] = wi[0].T __UpperCAmelCase : Optional[int] = wi[1].T else: __UpperCAmelCase : str = wi.T __UpperCAmelCase : int = wo.T if scalable_attention: # convert the rel_embedding of each layer __UpperCAmelCase : Union[str, Any] = tax_relpos_bias_lookup(lowerCAmelCase__ , lowerCAmelCase__ , """decoder""" ).T __UpperCAmelCase : Dict = old["""decoder/decoder_norm/scale"""] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: __UpperCAmelCase : List[str] = old["""decoder/logits_dense/kernel"""].T return new def lowercase_ ( lowerCAmelCase__ : str , lowerCAmelCase__ : bool ): """simple docstring""" __UpperCAmelCase : Union[str, Any] = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: __UpperCAmelCase : str = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: __UpperCAmelCase : List[str] = state_dict["""shared.weight"""] if "lm_head.weight" not in state_dict: # For old 1.0 models. print("""Using shared word embeddings as lm_head.""" ) __UpperCAmelCase : Union[str, Any] = state_dict["""shared.weight"""] return state_dict def lowercase_ ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Any ): """simple docstring""" __UpperCAmelCase : Tuple = checkpoints.load_tax_checkpoint(lowerCAmelCase__ ) __UpperCAmelCase : Any = convert_tax_to_pytorch( lowerCAmelCase__ , num_layers=config.num_layers , is_encoder_only=lowerCAmelCase__ , scalable_attention=lowerCAmelCase__ ) __UpperCAmelCase : str = make_state_dict(lowerCAmelCase__ , lowerCAmelCase__ ) model.load_state_dict(lowerCAmelCase__ , strict=lowerCAmelCase__ ) def lowercase_ ( lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : bool = False , ): """simple docstring""" __UpperCAmelCase : Optional[int] = MTaConfig.from_json_file(lowerCAmelCase__ ) print(f'Building PyTorch model from configuration: {config}' ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: __UpperCAmelCase : List[Any] = UMTaEncoderModel(lowerCAmelCase__ ) else: __UpperCAmelCase : Dict = UMTaForConditionalGeneration(lowerCAmelCase__ ) # Load weights from tf checkpoint load_tax_weights_in_ta(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save pytorch-model print(f'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(lowerCAmelCase__ ) # Verify that we can load the checkpoint. model.from_pretrained(lowerCAmelCase__ ) print("""Done""" ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser(description='''Converts a native T5X checkpoint into a PyTorch checkpoint.''') # Required parameters parser.add_argument( '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path to the T5X checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--is_encoder_only''', action='''store_true''', help='''Check if the model is encoder-decoder model''', default=False ) parser.add_argument( '''--scalable_attention''', action='''store_true''', help='''Whether the model uses scaled attention (umt5 model)''', default=False, ) _UpperCamelCase = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
254
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __A : str = { '''configuration_nezha''': ['''NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''NezhaConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Tuple = [ '''NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''NezhaForNextSentencePrediction''', '''NezhaForMaskedLM''', '''NezhaForPreTraining''', '''NezhaForMultipleChoice''', '''NezhaForQuestionAnswering''', '''NezhaForSequenceClassification''', '''NezhaForTokenClassification''', '''NezhaModel''', '''NezhaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_nezha import NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP, NezhaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_nezha import ( NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, NezhaModel, NezhaPreTrainedModel, ) else: import sys __A : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
354
"""simple docstring""" # DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion # and https://github.com/hojonathanho/diffusion import math from dataclasses import dataclass from typing import List, Optional, Tuple, Union import numpy as np import torch from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.schedulers.scheduling_utils import SchedulerMixin from diffusers.utils import BaseOutput, deprecate @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM class _a ( lowerCAmelCase): """simple docstring""" UpperCamelCase__ = 42 UpperCamelCase__ = None def lowercase ( _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Dict=0.999 , _SCREAMING_SNAKE_CASE : Any="cosine" , ): '''simple docstring''' if alpha_transform_type == "cosine": def alpha_bar_fn(_SCREAMING_SNAKE_CASE : Tuple ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(_SCREAMING_SNAKE_CASE : Any ): return math.exp(t * -12.0 ) else: raise ValueError(f'Unsupported alpha_tranform_type: {alpha_transform_type}' ) _UpperCAmelCase = [] for i in range(_SCREAMING_SNAKE_CASE ): _UpperCAmelCase = i / num_diffusion_timesteps _UpperCAmelCase = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(_SCREAMING_SNAKE_CASE ) / alpha_bar_fn(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) ) return torch.tensor(_SCREAMING_SNAKE_CASE , dtype=torch.floataa ) class _a ( lowerCAmelCase , lowerCAmelCase): """simple docstring""" UpperCamelCase__ = 1 @register_to_config def __init__( self : List[Any] , __UpperCamelCase : int = 1_0_0_0 , __UpperCamelCase : float = 0.0_0_0_1 , __UpperCamelCase : float = 0.0_2 , __UpperCamelCase : str = "linear" , __UpperCamelCase : Optional[Union[np.ndarray, List[float]]] = None , __UpperCamelCase : bool = True , __UpperCamelCase : bool = True , __UpperCamelCase : int = 0 , __UpperCamelCase : str = "epsilon" , __UpperCamelCase : float = 1.0 , **__UpperCamelCase : Optional[int] , )->Dict: if kwargs.get('''set_alpha_to_one''' , __UpperCamelCase ) is not None: _UpperCAmelCase = ( '''The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.''' ) deprecate('''set_alpha_to_one''' , '''1.0.0''' , __UpperCamelCase , standard_warn=__UpperCamelCase ) _UpperCAmelCase = kwargs['''set_alpha_to_one'''] if trained_betas is not None: _UpperCAmelCase = torch.tensor(__UpperCamelCase , dtype=torch.floataa ) elif beta_schedule == "linear": _UpperCAmelCase = torch.linspace(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. _UpperCAmelCase = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , __UpperCamelCase , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule _UpperCAmelCase = betas_for_alpha_bar(__UpperCamelCase ) else: raise NotImplementedError(F'{beta_schedule} does is not implemented for {self.__class__}' ) _UpperCAmelCase = 1.0 - self.betas _UpperCAmelCase = torch.cumprod(self.alphas , dim=0 ) # At every step in inverted ddim, we are looking into the next alphas_cumprod # For the final step, there is no next alphas_cumprod, and the index is out of bounds # `set_alpha_to_zero` decides whether we set this parameter simply to zero # in this case, self.step() just output the predicted noise # or whether we use the final alpha of the "non-previous" one. _UpperCAmelCase = torch.tensor(0.0 ) if set_alpha_to_zero else self.alphas_cumprod[-1] # standard deviation of the initial noise distribution _UpperCAmelCase = 1.0 # setable values _UpperCAmelCase = None _UpperCAmelCase = torch.from_numpy(np.arange(0 , __UpperCamelCase ).copy().astype(np.intaa ) ) def lowercase__ ( self : str , __UpperCamelCase : torch.FloatTensor , __UpperCamelCase : Optional[int] = None )->torch.FloatTensor: return sample def lowercase__ ( self : Any , __UpperCamelCase : int , __UpperCamelCase : Union[str, torch.device] = None )->Any: if num_inference_steps > self.config.num_train_timesteps: raise ValueError( F'`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:' F' {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle' F' maximal {self.config.num_train_timesteps} timesteps.' ) _UpperCAmelCase = num_inference_steps _UpperCAmelCase = self.config.num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _UpperCAmelCase = (np.arange(0 , __UpperCamelCase ) * step_ratio).round().copy().astype(np.intaa ) _UpperCAmelCase = torch.from_numpy(__UpperCamelCase ).to(__UpperCamelCase ) self.timesteps += self.config.steps_offset def lowercase__ ( self : Any , __UpperCamelCase : torch.FloatTensor , __UpperCamelCase : int , __UpperCamelCase : torch.FloatTensor , __UpperCamelCase : float = 0.0 , __UpperCamelCase : bool = False , __UpperCamelCase : Optional[torch.FloatTensor] = None , __UpperCamelCase : bool = True , )->Union[DDIMSchedulerOutput, Tuple]: # 1. get previous step value (=t+1) _UpperCAmelCase = timestep + self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas # change original implementation to exactly match noise levels for analogous forward process _UpperCAmelCase = self.alphas_cumprod[timestep] _UpperCAmelCase = ( self.alphas_cumprod[prev_timestep] if prev_timestep < self.config.num_train_timesteps else self.final_alpha_cumprod ) _UpperCAmelCase = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf if self.config.prediction_type == "epsilon": _UpperCAmelCase = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 _UpperCAmelCase = model_output elif self.config.prediction_type == "sample": _UpperCAmelCase = model_output _UpperCAmelCase = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 elif self.config.prediction_type == "v_prediction": _UpperCAmelCase = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output _UpperCAmelCase = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample else: raise ValueError( F'prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or' ''' `v_prediction`''' ) # 4. Clip or threshold "predicted x_0" if self.config.clip_sample: _UpperCAmelCase = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _UpperCAmelCase = (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _UpperCAmelCase = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if not return_dict: return (prev_sample, pred_original_sample) return DDIMSchedulerOutput(prev_sample=__UpperCamelCase , pred_original_sample=__UpperCamelCase ) def __len__( self : Any )->str: return self.config.num_train_timesteps
326
0
"""simple docstring""" class snake_case_: def __init__( self : str , UpperCamelCase_ : Union[str, Any] ): lowerCAmelCase : List[Any] = val lowerCAmelCase : Dict = None lowerCAmelCase : Optional[int] = None def lowerCamelCase__ ( self : Union[str, Any] , UpperCamelCase_ : List[str] ): if self.val: if val < self.val: if self.left is None: lowerCAmelCase : Any = Node(UpperCamelCase_ ) else: self.left.insert(UpperCamelCase_ ) elif val > self.val: if self.right is None: lowerCAmelCase : List[str] = Node(UpperCamelCase_ ) else: self.right.insert(UpperCamelCase_ ) else: lowerCAmelCase : Dict = val def _snake_case ( _snake_case : List[str] , _snake_case : str ): # Recursive traversal if root: inorder(root.left , _snake_case ) res.append(root.val ) inorder(root.right , _snake_case ) def _snake_case ( _snake_case : Tuple ): # Build BST if len(_snake_case ) == 0: return arr lowerCAmelCase : Any = Node(arr[0] ) for i in range(1 , len(_snake_case ) ): root.insert(arr[i] ) # Traverse BST in order. lowerCAmelCase : str = [] inorder(_snake_case , _snake_case ) return res if __name__ == "__main__": print(tree_sort([10, 1, 3, 2, 9, 14, 13]))
60
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool class snake_case__ ( lowerCAmelCase_ ): """simple docstring""" _SCREAMING_SNAKE_CASE = """philschmid/bart-large-cnn-samsum""" _SCREAMING_SNAKE_CASE = ( """This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, """ """and returns a summary of the text.""" ) _SCREAMING_SNAKE_CASE = """summarizer""" _SCREAMING_SNAKE_CASE = AutoTokenizer _SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM _SCREAMING_SNAKE_CASE = ["""text"""] _SCREAMING_SNAKE_CASE = ["""text"""] def lowercase_ ( self : Optional[Any], _snake_case : str ) ->Any: return self.pre_processor(_snake_case, return_tensors='pt', truncation=_snake_case ) def lowercase_ ( self : int, _snake_case : List[Any] ) ->Any: return self.model.generate(**_snake_case )[0] def lowercase_ ( self : int, _snake_case : int ) ->str: return self.pre_processor.decode(_snake_case, skip_special_tokens=_snake_case, clean_up_tokenization_spaces=_snake_case )
277
0
import gc import inspect import unittest import torch from parameterized import parameterized from diffusers import PriorTransformer from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin enable_full_determinism() class __magic_name__ ( __lowerCAmelCase , unittest.TestCase): A: str = PriorTransformer A: Optional[Any] = "hidden_states" @property def UpperCAmelCase__ ( self : str ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ : Dict = 4 UpperCamelCase__ : Any = 8 UpperCamelCase__ : Optional[int] = 7 UpperCamelCase__ : str = floats_tensor((batch_size, embedding_dim) ).to(lowerCamelCase__ ) UpperCamelCase__ : List[Any] = floats_tensor((batch_size, embedding_dim) ).to(lowerCamelCase__ ) UpperCamelCase__ : Any = floats_tensor((batch_size, num_embeddings, embedding_dim) ).to(lowerCamelCase__ ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def UpperCAmelCase__ ( self : Optional[int] , lowerCamelCase__ : Any=0 ) -> str: '''simple docstring''' torch.manual_seed(lowerCamelCase__ ) UpperCamelCase__ : Tuple = 4 UpperCamelCase__ : str = 8 UpperCamelCase__ : Optional[int] = 7 UpperCamelCase__ : List[str] = torch.randn((batch_size, embedding_dim) ).to(lowerCamelCase__ ) UpperCamelCase__ : Dict = torch.randn((batch_size, embedding_dim) ).to(lowerCamelCase__ ) UpperCamelCase__ : Optional[Any] = torch.randn((batch_size, num_embeddings, embedding_dim) ).to(lowerCamelCase__ ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } @property def UpperCAmelCase__ ( self : Dict ) -> Optional[Any]: '''simple docstring''' return (4, 8) @property def UpperCAmelCase__ ( self : Tuple ) -> Union[str, Any]: '''simple docstring''' return (4, 8) def UpperCAmelCase__ ( self : Optional[Any] ) -> Tuple: '''simple docstring''' UpperCamelCase__ : Optional[Any] = { '''num_attention_heads''': 2, '''attention_head_dim''': 4, '''num_layers''': 2, '''embedding_dim''': 8, '''num_embeddings''': 7, '''additional_embeddings''': 4, } UpperCamelCase__ : Any = self.dummy_input return init_dict, inputs_dict def UpperCAmelCase__ ( self : str ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : Tuple = PriorTransformer.from_pretrained( '''hf-internal-testing/prior-dummy''' , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(lowerCamelCase__ ) UpperCamelCase__ : Union[str, Any] = model(**self.dummy_input )[0] assert hidden_states is not None, "Make sure output is not None" def UpperCAmelCase__ ( self : Any ) -> List[str]: '''simple docstring''' UpperCamelCase__ : Dict = self.prepare_init_args_and_inputs_for_common() UpperCamelCase__ : List[str] = self.model_class(**lowerCamelCase__ ) UpperCamelCase__ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase__ : Optional[int] = [*signature.parameters.keys()] UpperCamelCase__ : int = ['''hidden_states''', '''timestep'''] self.assertListEqual(arg_names[:2] , lowerCamelCase__ ) def UpperCAmelCase__ ( self : List[str] ) -> Dict: '''simple docstring''' UpperCamelCase__ : str = PriorTransformer.from_pretrained('''hf-internal-testing/prior-dummy''' ) UpperCamelCase__ : int = model.to(lowerCamelCase__ ) if hasattr(lowerCamelCase__ , '''set_default_attn_processor''' ): model.set_default_attn_processor() UpperCamelCase__ : Tuple = self.get_dummy_seed_input() with torch.no_grad(): UpperCamelCase__ : str = model(**lowerCamelCase__ )[0] UpperCamelCase__ : Dict = output[0, :5].flatten().cpu() print(lowerCamelCase__ ) # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. UpperCamelCase__ : Optional[Any] = torch.tensor([-1.3436, -0.2870, 0.7538, 0.4368, -0.0239] ) self.assertTrue(torch_all_close(lowerCamelCase__ , lowerCamelCase__ , rtol=1E-2 ) ) @slow class __magic_name__ ( unittest.TestCase): def UpperCAmelCase__ ( self : List[Any] , lowerCamelCase__ : Union[str, Any]=1 , lowerCamelCase__ : Dict=768 , lowerCamelCase__ : List[Any]=77 , lowerCamelCase__ : Tuple=0 ) -> Any: '''simple docstring''' torch.manual_seed(lowerCamelCase__ ) UpperCamelCase__ : Union[str, Any] = batch_size UpperCamelCase__ : List[Any] = embedding_dim UpperCamelCase__ : Optional[int] = num_embeddings UpperCamelCase__ : Union[str, Any] = torch.randn((batch_size, embedding_dim) ).to(lowerCamelCase__ ) UpperCamelCase__ : Optional[int] = torch.randn((batch_size, embedding_dim) ).to(lowerCamelCase__ ) UpperCamelCase__ : Union[str, Any] = torch.randn((batch_size, num_embeddings, embedding_dim) ).to(lowerCamelCase__ ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def UpperCAmelCase__ ( self : Any ) -> int: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @parameterized.expand( [ # fmt: off [13, [-0.5861, 0.1283, -0.0931, 0.0882, 0.4476, 0.1329, -0.0498, 0.0640]], [37, [-0.4913, 0.0110, -0.0483, 0.0541, 0.4954, -0.0170, 0.0354, 0.1651]], # fmt: on ] ) def UpperCAmelCase__ ( self : Any , lowerCamelCase__ : Tuple , lowerCamelCase__ : int ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ : Any = PriorTransformer.from_pretrained('''kandinsky-community/kandinsky-2-1-prior''' , subfolder='''prior''' ) model.to(lowerCamelCase__ ) UpperCamelCase__ : str = self.get_dummy_seed_input(seed=lowerCamelCase__ ) with torch.no_grad(): UpperCamelCase__ : Tuple = model(**lowerCamelCase__ )[0] assert list(sample.shape ) == [1, 768] UpperCamelCase__ : Dict = sample[0, :8].flatten().cpu() print(lowerCamelCase__ ) UpperCamelCase__ : Tuple = torch.tensor(lowerCamelCase__ ) assert torch_all_close(lowerCamelCase__ , lowerCamelCase__ , atol=1E-3 )
355
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __UpperCamelCase : List[Any] = pytest.mark.integration @pytest.mark.parametrize('''path''' , ['''paws''', '''csv'''] ) def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[Any] ): """simple docstring""" inspect_dataset(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[str] = path + '''.py''' assert script_name in os.listdir(SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(SCREAMING_SNAKE_CASE ) @pytest.mark.filterwarnings('''ignore:inspect_metric is deprecated:FutureWarning''' ) @pytest.mark.filterwarnings('''ignore:metric_module_factory is deprecated:FutureWarning''' ) @pytest.mark.parametrize('''path''' , ['''accuracy'''] ) def _a ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Union[str, Any] ): """simple docstring""" inspect_metric(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : int = path + '''.py''' assert script_name in os.listdir(SCREAMING_SNAKE_CASE ) assert "__pycache__" not in os.listdir(SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( '''path, config_name, expected_splits''' , [ ('''squad''', '''plain_text''', ['''train''', '''validation''']), ('''dalle-mini/wit''', '''dalle-mini--wit''', ['''train''']), ('''paws''', '''labeled_final''', ['''train''', '''test''', '''validation''']), ] , ) def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : str ): """simple docstring""" UpperCamelCase__ : int = get_dataset_config_info(SCREAMING_SNAKE_CASE , config_name=SCREAMING_SNAKE_CASE ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( '''path, config_name, expected_exception''' , [ ('''paws''', None, ValueError), ] , ) def _a ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : str ): """simple docstring""" with pytest.raises(SCREAMING_SNAKE_CASE ): get_dataset_config_info(SCREAMING_SNAKE_CASE , config_name=SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( '''path, expected''' , [ ('''squad''', '''plain_text'''), ('''acronym_identification''', '''default'''), ('''lhoestq/squad''', '''plain_text'''), ('''lhoestq/test''', '''default'''), ('''lhoestq/demo1''', '''lhoestq--demo1'''), ('''dalle-mini/wit''', '''dalle-mini--wit'''), ] , ) def _a ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : List[str] ): """simple docstring""" UpperCamelCase__ : List[Any] = get_dataset_config_names(SCREAMING_SNAKE_CASE ) assert expected in config_names @pytest.mark.parametrize( '''path, expected_configs, expected_splits_in_first_config''' , [ ('''squad''', ['''plain_text'''], ['''train''', '''validation''']), ('''dalle-mini/wit''', ['''dalle-mini--wit'''], ['''train''']), ('''paws''', ['''labeled_final''', '''labeled_swap''', '''unlabeled_final'''], ['''train''', '''test''', '''validation''']), ] , ) def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[int] ): """simple docstring""" UpperCamelCase__ : Optional[int] = get_dataset_infos(SCREAMING_SNAKE_CASE ) assert list(infos.keys() ) == expected_configs UpperCamelCase__ : List[str] = expected_configs[0] assert expected_config in infos UpperCamelCase__ : Union[str, Any] = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( '''path, expected_config, expected_splits''' , [ ('''squad''', '''plain_text''', ['''train''', '''validation''']), ('''dalle-mini/wit''', '''dalle-mini--wit''', ['''train''']), ('''paws''', '''labeled_final''', ['''train''', '''test''', '''validation''']), ] , ) def _a ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : int ): """simple docstring""" UpperCamelCase__ : Optional[Any] = get_dataset_infos(SCREAMING_SNAKE_CASE ) assert expected_config in infos UpperCamelCase__ : Optional[int] = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( '''path, config_name, expected_exception''' , [ ('''paws''', None, ValueError), ] , ) def _a ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Any ): """simple docstring""" with pytest.raises(SCREAMING_SNAKE_CASE ): get_dataset_split_names(SCREAMING_SNAKE_CASE , config_name=SCREAMING_SNAKE_CASE )
51
0
'''simple docstring''' import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel if is_vision_available(): from transformers import MaskFormerImageProcessor if is_vision_available(): from PIL import Image class lowercase : """simple docstring""" def __init__( self , UpperCamelCase_ , UpperCamelCase_=2 , UpperCamelCase_=True , UpperCamelCase_=False , UpperCamelCase_=10 , UpperCamelCase_=3 , UpperCamelCase_=32 * 4 , UpperCamelCase_=32 * 6 , UpperCamelCase_=4 , UpperCamelCase_=32 , ): '''simple docstring''' UpperCamelCase__ :List[Any] = parent UpperCamelCase__ :List[Any] = batch_size UpperCamelCase__ :Optional[Any] = is_training UpperCamelCase__ :int = use_auxiliary_loss UpperCamelCase__ :Dict = num_queries UpperCamelCase__ :Optional[Any] = num_channels UpperCamelCase__ :Any = min_size UpperCamelCase__ :Union[str, Any] = max_size UpperCamelCase__ :int = num_labels UpperCamelCase__ :Tuple = mask_feature_size def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :str = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( a_ ) UpperCamelCase__ :str = torch.ones([self.batch_size, self.min_size, self.max_size] , device=a_ ) UpperCamelCase__ :List[Any] = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=a_ ) > 0.5 ).float() UpperCamelCase__ :Any = (torch.rand((self.batch_size, self.num_labels) , device=a_ ) > 0.5).long() UpperCamelCase__ :Optional[Any] = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def lowerCAmelCase__ ( self ): '''simple docstring''' return MaskFormerConfig.from_backbone_and_decoder_configs( backbone_config=SwinConfig( depths=[1, 1, 1, 1] , ) , decoder_config=DetrConfig( decoder_ffn_dim=128 , num_queries=self.num_queries , decoder_attention_heads=2 , d_model=self.mask_feature_size , ) , mask_feature_size=self.mask_feature_size , fpn_feature_size=self.mask_feature_size , num_channels=self.num_channels , num_labels=self.num_labels , ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Tuple = self.prepare_config_and_inputs() UpperCamelCase__ :Dict = {'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask} return config, inputs_dict def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :Any = output.encoder_hidden_states UpperCamelCase__ :List[str] = output.pixel_decoder_hidden_states UpperCamelCase__ :int = output.transformer_decoder_hidden_states self.parent.assertTrue(len(a_ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(a_ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(a_ ) , config.decoder_config.decoder_layers ) def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_=False ): '''simple docstring''' with torch.no_grad(): UpperCamelCase__ :Optional[int] = MaskFormerModel(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase__ :Optional[Any] = model(pixel_values=a_ , pixel_mask=a_ ) UpperCamelCase__ :int = model(a_ , output_hidden_states=a_ ) # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the # encoder and pixel decoder self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.mask_feature_size) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(a_ , a_ ) def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ :Any = MaskFormerForInstanceSegmentation(config=a_ ) model.to(a_ ) model.eval() def comm_check_on_output(UpperCamelCase_ ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): UpperCamelCase__ :Optional[Any] = model(pixel_values=a_ , pixel_mask=a_ ) UpperCamelCase__ :Optional[int] = model(a_ ) comm_check_on_output(a_ ) UpperCamelCase__ :List[str] = model( pixel_values=a_ , pixel_mask=a_ , mask_labels=a_ , class_labels=a_ ) comm_check_on_output(a_ ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class lowercase ( __snake_case , __snake_case , unittest.TestCase ): """simple docstring""" _a = (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else () _a = ( {'feature-extraction': MaskFormerModel, 'image-segmentation': MaskFormerForInstanceSegmentation} if is_torch_available() else {} ) _a = False _a = False _a = False _a = False def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Optional[Any] = MaskFormerModelTester(self ) UpperCamelCase__ :int = ConfigTester(self , config_class=a_ , has_text_modality=a_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' self.config_tester.run_common_tests() def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :str = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(a_ , **a_ , output_hidden_states=a_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*a_ ) @unittest.skip(reason='''MaskFormer does not use inputs_embeds''' ) def lowerCAmelCase__ ( self ): '''simple docstring''' pass @unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method''' ) def lowerCAmelCase__ ( self ): '''simple docstring''' pass @unittest.skip(reason='''MaskFormer is not a generative model''' ) def lowerCAmelCase__ ( self ): '''simple docstring''' pass @unittest.skip(reason='''MaskFormer does not use token embeddings''' ) def lowerCAmelCase__ ( self ): '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip( reason='''MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`''' ) def lowerCAmelCase__ ( self ): '''simple docstring''' pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def lowerCAmelCase__ ( self ): '''simple docstring''' pass def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ :Optional[int] = model_class(a_ ) UpperCamelCase__ :Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase__ :Union[str, Any] = [*signature.parameters.keys()] UpperCamelCase__ :str = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , a_ ) @slow def lowerCAmelCase__ ( self ): '''simple docstring''' for model_name in ["facebook/maskformer-swin-small-coco"]: UpperCamelCase__ :Optional[Any] = MaskFormerModel.from_pretrained(a_ ) self.assertIsNotNone(a_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :List[Any] = (self.model_tester.min_size,) * 2 UpperCamelCase__ :List[str] = { '''pixel_values''': torch.randn((2, 3, *size) , device=a_ ), '''mask_labels''': torch.randn((2, 10, *size) , device=a_ ), '''class_labels''': torch.zeros(2 , 10 , device=a_ ).long(), } UpperCamelCase__ :List[Any] = MaskFormerForInstanceSegmentation(MaskFormerConfig() ).to(a_ ) UpperCamelCase__ :Optional[Any] = model(**a_ ) self.assertTrue(outputs.loss is not None ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(a_ , **a_ , output_hidden_states=a_ ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ :int = model_class(a_ ).to(a_ ) UpperCamelCase__ :str = model(**a_ , output_attentions=a_ ) self.assertTrue(outputs.attentions is not None ) def lowerCAmelCase__ ( self ): '''simple docstring''' if not self.model_tester.is_training: return # only MaskFormerForInstanceSegmentation has the loss UpperCamelCase__ :Union[str, Any] = self.all_model_classes[1] UpperCamelCase__ :List[Any] = self.model_tester.prepare_config_and_inputs() UpperCamelCase__ :Union[str, Any] = model_class(a_ ) model.to(a_ ) model.train() UpperCamelCase__ :Any = model(a_ , mask_labels=a_ , class_labels=a_ ).loss loss.backward() def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Dict = self.all_model_classes[1] UpperCamelCase__ :str = self.model_tester.prepare_config_and_inputs() UpperCamelCase__ :str = True UpperCamelCase__ :str = True UpperCamelCase__ :Optional[int] = model_class(a_ ) model.to(a_ ) model.train() UpperCamelCase__ :Union[str, Any] = model(a_ , mask_labels=a_ , class_labels=a_ ) UpperCamelCase__ :Optional[Any] = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() UpperCamelCase__ :Union[str, Any] = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't UpperCamelCase__ :int = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() UpperCamelCase__ :Any = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=a_ ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) __snake_case = 1e-4 def a ( ) -> int: '''simple docstring''' UpperCamelCase__ :Any = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_vision @slow class lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def lowerCAmelCase__ ( self ): '''simple docstring''' return ( MaskFormerImageProcessor.from_pretrained('''facebook/maskformer-swin-small-coco''' ) if is_vision_available() else None ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Optional[int] = MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco''' ).to(a_ ) UpperCamelCase__ :Tuple = self.default_image_processor UpperCamelCase__ :List[str] = prepare_img() UpperCamelCase__ :Dict = image_processor(a_ , return_tensors='''pt''' ).to(a_ ) UpperCamelCase__ :str = inputs['''pixel_values'''].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(a_ , (1, 3, 800, 1088) ) with torch.no_grad(): UpperCamelCase__ :Tuple = model(**a_ ) UpperCamelCase__ :Dict = torch.tensor( [[-0.0482, 0.9228, 0.4951], [-0.2547, 0.8017, 0.8527], [-0.0069, 0.3385, -0.0089]] ).to(a_ ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , a_ , atol=a_ ) ) UpperCamelCase__ :int = torch.tensor( [[-0.8422, -0.8434, -0.9718], [-1.0144, -0.5565, -0.4195], [-1.0038, -0.4484, -0.1961]] ).to(a_ ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , a_ , atol=a_ ) ) UpperCamelCase__ :Any = torch.tensor( [[0.2852, -0.0159, 0.9735], [0.6254, 0.1858, 0.8529], [-0.0680, -0.4116, 1.8413]] ).to(a_ ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , a_ , atol=a_ ) ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :int = ( MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco''' ) .to(a_ ) .eval() ) UpperCamelCase__ :str = self.default_image_processor UpperCamelCase__ :Union[str, Any] = prepare_img() UpperCamelCase__ :Tuple = image_processor(a_ , return_tensors='''pt''' ).to(a_ ) UpperCamelCase__ :str = inputs['''pixel_values'''].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(a_ , (1, 3, 800, 1088) ) with torch.no_grad(): UpperCamelCase__ :List[Any] = model(**a_ ) # masks_queries_logits UpperCamelCase__ :Tuple = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) UpperCamelCase__ :Any = [ [-1.3737124, -1.7724937, -1.9364233], [-1.5977281, -1.9867939, -2.1523695], [-1.5795398, -1.9269832, -2.093942], ] UpperCamelCase__ :List[Any] = torch.tensor(a_ ).to(a_ ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , a_ , atol=a_ ) ) # class_queries_logits UpperCamelCase__ :Tuple = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) UpperCamelCase__ :str = torch.tensor( [ [1.65_12e00, -5.25_72e00, -3.35_19e00], [3.61_69e-02, -5.90_25e00, -2.93_13e00], [1.07_66e-04, -7.76_30e00, -5.12_63e00], ] ).to(a_ ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , a_ , atol=a_ ) ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :List[str] = ( MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff''' ) .to(a_ ) .eval() ) UpperCamelCase__ :Dict = self.default_image_processor UpperCamelCase__ :Optional[Any] = prepare_img() UpperCamelCase__ :List[Any] = image_processor(a_ , return_tensors='''pt''' ).to(a_ ) UpperCamelCase__ :Optional[int] = inputs['''pixel_values'''].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(a_ , (1, 3, 800, 1088) ) with torch.no_grad(): UpperCamelCase__ :List[str] = model(**a_ ) # masks_queries_logits UpperCamelCase__ :str = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) UpperCamelCase__ :int = [[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7711]] UpperCamelCase__ :str = torch.tensor(a_ ).to(a_ ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , a_ , atol=a_ ) ) # class_queries_logits UpperCamelCase__ :str = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) UpperCamelCase__ :int = torch.tensor( [[4.7188, -3.2585, -2.8857], [6.6871, -2.9181, -1.2487], [7.2449, -2.2764, -2.1874]] ).to(a_ ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , a_ , atol=a_ ) ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Union[str, Any] = ( MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco''' ) .to(a_ ) .eval() ) UpperCamelCase__ :Dict = self.default_image_processor UpperCamelCase__ :Any = image_processor( [np.zeros((3, 800, 1333) ), np.zeros((3, 800, 1333) )] , segmentation_maps=[np.zeros((384, 384) ).astype(np.floataa ), np.zeros((384, 384) ).astype(np.floataa )] , return_tensors='''pt''' , ) UpperCamelCase__ :Any = inputs['''pixel_values'''].to(a_ ) UpperCamelCase__ :int = [el.to(a_ ) for el in inputs['''mask_labels''']] UpperCamelCase__ :int = [el.to(a_ ) for el in inputs['''class_labels''']] with torch.no_grad(): UpperCamelCase__ :List[Any] = model(**a_ ) self.assertTrue(outputs.loss is not None )
97
"""simple docstring""" import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging SCREAMING_SNAKE_CASE : List[str] = logging.get_logger(__name__) # pylint: disable=invalid-name class _UpperCAmelCase ( __snake_case ): '''simple docstring''' def __init__(self , a_ , a_ , a_ , a_ , a_ , a_ , a_ , ): '''simple docstring''' super().__init__() self.register_modules( vae=a_ , text_encoder=a_ , tokenizer=a_ , unet=a_ , scheduler=a_ , safety_checker=a_ , feature_extractor=a_ , ) def SCREAMING_SNAKE_CASE (self , a_ = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory __snake_case : Optional[int] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(a_ ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' self.enable_attention_slicing(a_ ) @torch.no_grad() def __call__(self , a_ , a_ = 5_12 , a_ = 5_12 , a_ = 50 , a_ = 7.5 , a_ = None , a_ = 1 , a_ = 0.0 , a_ = None , a_ = None , a_ = "pil" , a_ = True , a_ = None , a_ = 1 , a_ = None , **a_ , ): '''simple docstring''' if isinstance(a_ , a_ ): __snake_case : Any = 1 elif isinstance(a_ , a_ ): __snake_case : Any = len(a_ ) else: raise ValueError(f"""`prompt` has to be of type `str` or `list` but is {type(a_ )}""" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"""`height` and `width` have to be divisible by 8 but are {height} and {width}.""" ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(a_ , a_ ) or callback_steps <= 0) ): raise ValueError( f"""`callback_steps` has to be a positive integer but is {callback_steps} of type""" f""" {type(a_ )}.""" ) # get prompt text embeddings __snake_case : int = self.tokenizer( a_ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) __snake_case : int = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: __snake_case : List[Any] = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f""" {self.tokenizer.model_max_length} tokens: {removed_text}""" ) __snake_case : Optional[int] = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: __snake_case : Optional[int] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method __snake_case , __snake_case , __snake_case : Union[str, Any] = text_embeddings.shape __snake_case : Optional[int] = text_embeddings.repeat(1 , a_ , 1 ) __snake_case : Dict = text_embeddings.view(bs_embed * num_images_per_prompt , a_ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. __snake_case : Dict = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: __snake_case : List[str] if negative_prompt is None: __snake_case : List[Any] = [''''''] elif type(a_ ) is not type(a_ ): raise TypeError( f"""`negative_prompt` should be the same type to `prompt`, but got {type(a_ )} !=""" f""" {type(a_ )}.""" ) elif isinstance(a_ , a_ ): __snake_case : List[str] = [negative_prompt] elif batch_size != len(a_ ): raise ValueError( f"""`negative_prompt`: {negative_prompt} has batch size {len(a_ )}, but `prompt`:""" f""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches""" ''' the batch size of `prompt`.''' ) else: __snake_case : Optional[int] = negative_prompt __snake_case : Optional[int] = text_input_ids.shape[-1] __snake_case : List[Any] = self.tokenizer( a_ , padding='''max_length''' , max_length=a_ , truncation=a_ , return_tensors='''pt''' , ) __snake_case : str = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method __snake_case : str = uncond_embeddings.shape[1] __snake_case : int = uncond_embeddings.repeat(a_ , a_ , 1 ) __snake_case : int = uncond_embeddings.view(batch_size * num_images_per_prompt , a_ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes __snake_case : Any = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. __snake_case : Any = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) __snake_case : Dict = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) __snake_case : Dict = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps __snake_case : Union[str, Any] = torch.randn( a_ , generator=a_ , device='''cpu''' , dtype=a_ ).to(self.device ) __snake_case : Tuple = torch.randn(a_ , generator=a_ , device='''cpu''' , dtype=a_ ).to( self.device ) else: __snake_case : Dict = torch.randn( a_ , generator=a_ , device=self.device , dtype=a_ ) __snake_case : Dict = torch.randn(a_ , generator=a_ , device=self.device , dtype=a_ ) else: if latents_reference.shape != latents_shape: raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) __snake_case : Union[str, Any] = latents_reference.to(self.device ) __snake_case : Dict = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images __snake_case : int = (latents_shape[3] - latents_shape_reference[3]) // 2 __snake_case : Tuple = (latents_shape[2] - latents_shape_reference[2]) // 2 __snake_case : Optional[int] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx __snake_case : Union[str, Any] = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy __snake_case : int = 0 if dx < 0 else dx __snake_case : Union[str, Any] = 0 if dy < 0 else dy __snake_case : str = max(-dx , 0 ) __snake_case : Tuple = max(-dy , 0 ) # import pdb # pdb.set_trace() __snake_case : Any = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(a_ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand __snake_case : Optional[Any] = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler __snake_case : List[Any] = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] __snake_case : Any = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) __snake_case : Tuple = {} if accepts_eta: __snake_case : List[str] = eta for i, t in enumerate(self.progress_bar(a_ ) ): # expand the latents if we are doing classifier free guidance __snake_case : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __snake_case : Tuple = self.scheduler.scale_model_input(a_ , a_ ) # predict the noise residual __snake_case : int = self.unet(a_ , a_ , encoder_hidden_states=a_ ).sample # perform guidance if do_classifier_free_guidance: __snake_case , __snake_case : Tuple = noise_pred.chunk(2 ) __snake_case : str = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 __snake_case : Optional[Any] = self.scheduler.step(a_ , a_ , a_ , **a_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(a_ , a_ , a_ ) __snake_case : Union[str, Any] = 1 / 0.1_8215 * latents __snake_case : Optional[Any] = self.vae.decode(a_ ).sample __snake_case : Union[str, Any] = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __snake_case : Union[str, Any] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: __snake_case : Optional[int] = self.feature_extractor(self.numpy_to_pil(a_ ) , return_tensors='''pt''' ).to( self.device ) __snake_case , __snake_case : List[Any] = self.safety_checker( images=a_ , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: __snake_case : Union[str, Any] = None if output_type == "pil": __snake_case : Union[str, Any] = self.numpy_to_pil(a_ ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=a_ , nsfw_content_detected=a_ )
102
0
SCREAMING_SNAKE_CASE_ = [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(100000)] def __SCREAMING_SNAKE_CASE ( lowerCAmelCase: int ) -> int: _UpperCAmelCase : Dict = 0 while number: # Increased Speed Slightly by checking every 5 digits together. sum_of_digits_squared += DIGITS_SQUARED[number % 10_0000] number //= 10_0000 return sum_of_digits_squared # There are 2 Chains made, # One ends with 89 with the chain member 58 being the one which when declared first, # there will be the least number of iterations for all the members to be checked. # The other one ends with 1 and has only one element 1. # So 58 and 1 are chosen to be declared at the starting. # Changed dictionary to an array to quicken the solution SCREAMING_SNAKE_CASE_ = [None] * 10000000 SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = False def __SCREAMING_SNAKE_CASE ( lowerCAmelCase: int ) -> bool: if CHAINS[number - 1] is not None: return CHAINS[number - 1] # type: ignore _UpperCAmelCase : List[str] = chain(next_number(_UpperCAmelCase ) ) _UpperCAmelCase : Optional[Any] = number_chain while number < 1000_0000: _UpperCAmelCase : Tuple = number_chain number *= 10 return number_chain def __SCREAMING_SNAKE_CASE ( lowerCAmelCase: int = 1000_0000 ) -> int: for i in range(1 , _UpperCAmelCase ): if CHAINS[i] is None: chain(i + 1 ) return CHAINS[:number].count(_UpperCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod() print(F'''{solution() = }''')
350
import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class a ( UpperCAmelCase ): _lowercase = (PNDMScheduler,) _lowercase = (("num_inference_steps", 5_0),) def _UpperCAmelCase ( self , **A_ ): '''simple docstring''' _UpperCAmelCase : Tuple = { "num_train_timesteps": 1000, "beta_start": 0.00_01, "beta_end": 0.02, "beta_schedule": "linear", } config.update(**A_ ) return config def _UpperCAmelCase ( self , A_=0 , **A_ ): '''simple docstring''' _UpperCAmelCase : int = dict(self.forward_default_kwargs ) _UpperCAmelCase : Optional[Any] = kwargs.pop("num_inference_steps" , A_ ) _UpperCAmelCase : int = self.dummy_sample _UpperCAmelCase : Dict = 0.1 * sample _UpperCAmelCase : Optional[int] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: _UpperCAmelCase : Dict = self.get_scheduler_config(**A_ ) _UpperCAmelCase : Any = scheduler_class(**A_ ) scheduler.set_timesteps(A_ ) # copy over dummy past residuals _UpperCAmelCase : Union[str, Any] = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(A_ ) _UpperCAmelCase : str = scheduler_class.from_pretrained(A_ ) new_scheduler.set_timesteps(A_ ) # copy over dummy past residuals _UpperCAmelCase : Union[str, Any] = dummy_past_residuals[:] _UpperCAmelCase : Tuple = scheduler.step_prk(A_ , A_ , A_ , **A_ ).prev_sample _UpperCAmelCase : Any = new_scheduler.step_prk(A_ , A_ , A_ , **A_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" _UpperCAmelCase : Union[str, Any] = scheduler.step_plms(A_ , A_ , A_ , **A_ ).prev_sample _UpperCAmelCase : Optional[Any] = new_scheduler.step_plms(A_ , A_ , A_ , **A_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def _UpperCAmelCase ( self ): '''simple docstring''' pass def _UpperCAmelCase ( self , A_=0 , **A_ ): '''simple docstring''' _UpperCAmelCase : Any = dict(self.forward_default_kwargs ) _UpperCAmelCase : int = kwargs.pop("num_inference_steps" , A_ ) _UpperCAmelCase : int = self.dummy_sample _UpperCAmelCase : Any = 0.1 * sample _UpperCAmelCase : Tuple = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: _UpperCAmelCase : Optional[Any] = self.get_scheduler_config() _UpperCAmelCase : Dict = scheduler_class(**A_ ) scheduler.set_timesteps(A_ ) # copy over dummy past residuals (must be after setting timesteps) _UpperCAmelCase : Any = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(A_ ) _UpperCAmelCase : str = scheduler_class.from_pretrained(A_ ) # copy over dummy past residuals new_scheduler.set_timesteps(A_ ) # copy over dummy past residual (must be after setting timesteps) _UpperCAmelCase : List[Any] = dummy_past_residuals[:] _UpperCAmelCase : Tuple = scheduler.step_prk(A_ , A_ , A_ , **A_ ).prev_sample _UpperCAmelCase : Union[str, Any] = new_scheduler.step_prk(A_ , A_ , A_ , **A_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" _UpperCAmelCase : Optional[Any] = scheduler.step_plms(A_ , A_ , A_ , **A_ ).prev_sample _UpperCAmelCase : List[str] = new_scheduler.step_plms(A_ , A_ , A_ , **A_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def _UpperCAmelCase ( self , **A_ ): '''simple docstring''' _UpperCAmelCase : Optional[Any] = self.scheduler_classes[0] _UpperCAmelCase : Union[str, Any] = self.get_scheduler_config(**A_ ) _UpperCAmelCase : List[str] = scheduler_class(**A_ ) _UpperCAmelCase : List[str] = 10 _UpperCAmelCase : Optional[int] = self.dummy_model() _UpperCAmelCase : List[Any] = self.dummy_sample_deter scheduler.set_timesteps(A_ ) for i, t in enumerate(scheduler.prk_timesteps ): _UpperCAmelCase : Dict = model(A_ , A_ ) _UpperCAmelCase : int = scheduler.step_prk(A_ , A_ , A_ ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): _UpperCAmelCase : Any = model(A_ , A_ ) _UpperCAmelCase : Any = scheduler.step_plms(A_ , A_ , A_ ).prev_sample return sample def _UpperCAmelCase ( self ): '''simple docstring''' _UpperCAmelCase : List[Any] = dict(self.forward_default_kwargs ) _UpperCAmelCase : str = kwargs.pop("num_inference_steps" , A_ ) for scheduler_class in self.scheduler_classes: _UpperCAmelCase : int = self.get_scheduler_config() _UpperCAmelCase : Optional[Any] = scheduler_class(**A_ ) _UpperCAmelCase : int = self.dummy_sample _UpperCAmelCase : str = 0.1 * sample if num_inference_steps is not None and hasattr(A_ , "set_timesteps" ): scheduler.set_timesteps(A_ ) elif num_inference_steps is not None and not hasattr(A_ , "set_timesteps" ): _UpperCAmelCase : Union[str, Any] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) _UpperCAmelCase : Tuple = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] _UpperCAmelCase : Any = dummy_past_residuals[:] _UpperCAmelCase : Any = scheduler.step_prk(A_ , 0 , A_ , **A_ ).prev_sample _UpperCAmelCase : Optional[Any] = scheduler.step_prk(A_ , 1 , A_ , **A_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) _UpperCAmelCase : Optional[int] = scheduler.step_plms(A_ , 0 , A_ , **A_ ).prev_sample _UpperCAmelCase : Optional[Any] = scheduler.step_plms(A_ , 1 , A_ , **A_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def _UpperCAmelCase ( self ): '''simple docstring''' for timesteps in [100, 1000]: self.check_over_configs(num_train_timesteps=A_ ) def _UpperCAmelCase ( self ): '''simple docstring''' for steps_offset in [0, 1]: self.check_over_configs(steps_offset=A_ ) _UpperCAmelCase : List[str] = self.scheduler_classes[0] _UpperCAmelCase : Optional[int] = self.get_scheduler_config(steps_offset=1 ) _UpperCAmelCase : str = scheduler_class(**A_ ) scheduler.set_timesteps(10 ) assert torch.equal( scheduler.timesteps , torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1] ) , ) def _UpperCAmelCase ( self ): '''simple docstring''' for beta_start, beta_end in zip([0.00_01, 0.0_01] , [0.0_02, 0.02] ): self.check_over_configs(beta_start=A_ , beta_end=A_ ) def _UpperCAmelCase ( self ): '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=A_ ) def _UpperCAmelCase ( self ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=A_ ) def _UpperCAmelCase ( self ): '''simple docstring''' for t in [1, 5, 10]: self.check_over_forward(time_step=A_ ) def _UpperCAmelCase ( self ): '''simple docstring''' for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=A_ ) def _UpperCAmelCase ( self ): '''simple docstring''' _UpperCAmelCase : int = 27 for scheduler_class in self.scheduler_classes: _UpperCAmelCase : str = self.dummy_sample _UpperCAmelCase : str = 0.1 * sample _UpperCAmelCase : str = self.get_scheduler_config() _UpperCAmelCase : Tuple = scheduler_class(**A_ ) scheduler.set_timesteps(A_ ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): _UpperCAmelCase : Dict = scheduler.step_prk(A_ , A_ , A_ ).prev_sample def _UpperCAmelCase ( self ): '''simple docstring''' with self.assertRaises(A_ ): _UpperCAmelCase : Union[str, Any] = self.scheduler_classes[0] _UpperCAmelCase : Dict = self.get_scheduler_config() _UpperCAmelCase : Union[str, Any] = scheduler_class(**A_ ) scheduler.step_plms(self.dummy_sample , 1 , self.dummy_sample ).prev_sample def _UpperCAmelCase ( self ): '''simple docstring''' _UpperCAmelCase : Dict = self.full_loop() _UpperCAmelCase : int = torch.sum(torch.abs(A_ ) ) _UpperCAmelCase : Optional[Any] = torch.mean(torch.abs(A_ ) ) assert abs(result_sum.item() - 1_98.13_18 ) < 1e-2 assert abs(result_mean.item() - 0.25_80 ) < 1e-3 def _UpperCAmelCase ( self ): '''simple docstring''' _UpperCAmelCase : Optional[Any] = self.full_loop(prediction_type="v_prediction" ) _UpperCAmelCase : Union[str, Any] = torch.sum(torch.abs(A_ ) ) _UpperCAmelCase : List[Any] = torch.mean(torch.abs(A_ ) ) assert abs(result_sum.item() - 67.39_86 ) < 1e-2 assert abs(result_mean.item() - 0.08_78 ) < 1e-3 def _UpperCAmelCase ( self ): '''simple docstring''' _UpperCAmelCase : Tuple = self.full_loop(set_alpha_to_one=A_ , beta_start=0.01 ) _UpperCAmelCase : Dict = torch.sum(torch.abs(A_ ) ) _UpperCAmelCase : Optional[int] = torch.mean(torch.abs(A_ ) ) assert abs(result_sum.item() - 2_30.03_99 ) < 1e-2 assert abs(result_mean.item() - 0.29_95 ) < 1e-3 def _UpperCAmelCase ( self ): '''simple docstring''' _UpperCAmelCase : List[Any] = self.full_loop(set_alpha_to_one=A_ , beta_start=0.01 ) _UpperCAmelCase : Optional[Any] = torch.sum(torch.abs(A_ ) ) _UpperCAmelCase : Dict = torch.mean(torch.abs(A_ ) ) assert abs(result_sum.item() - 1_86.94_82 ) < 1e-2 assert abs(result_mean.item() - 0.24_34 ) < 1e-3
189
0
'''simple docstring''' import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging a__ : Dict = logging.get_logger(__name__) def _UpperCamelCase ( __A , __A ) -> List[Any]: '''simple docstring''' UpperCamelCase__ = set() UpperCamelCase__ = [] def parse_line(__A ): for line in fp: if isinstance(__A , __A ): UpperCamelCase__ = line.decode("UTF-8" ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(" " ): # process a single warning and move it to `selected_warnings`. if len(__A ) > 0: UpperCamelCase__ = "\n".join(__A ) # Only keep the warnings specified in `targets` if any(F''': {x}: ''' in warning for x in targets ): selected_warnings.add(__A ) buffer.clear() continue else: UpperCamelCase__ = line.strip() buffer.append(__A ) if from_gh: for filename in os.listdir(__A ): UpperCamelCase__ = os.path.join(__A , __A ) if not os.path.isdir(__A ): # read the file if filename != "warnings.txt": continue with open(__A ) as fp: parse_line(__A ) else: try: with zipfile.ZipFile(__A ) as z: for filename in z.namelist(): if not os.path.isdir(__A ): # read the file if filename != "warnings.txt": continue with z.open(__A ) as fp: parse_line(__A ) except Exception: logger.warning( F'''{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.''' ) return selected_warnings def _UpperCamelCase ( __A , __A ) -> List[Any]: '''simple docstring''' UpperCamelCase__ = set() UpperCamelCase__ = [os.path.join(__A , __A ) for p in os.listdir(__A ) if (p.endswith(".zip" ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(__A , __A ) ) return selected_warnings if __name__ == "__main__": def _UpperCamelCase ( __A ) -> int: '''simple docstring''' return values.split("," ) a__ : int = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') parser.add_argument( '--output_dir', type=str, required=True, help='Where to store the downloaded artifacts and other result files.', ) parser.add_argument('--token', default=None, type=str, help='A token that has actions:read permission.') # optional parameters parser.add_argument( '--targets', default='DeprecationWarning,UserWarning,FutureWarning', type=list_str, help='Comma-separated list of target warning(s) which we want to extract.', ) parser.add_argument( '--from_gh', action='store_true', help='If running from a GitHub action workflow and collecting warnings from its artifacts.', ) a__ : List[Any] = parser.parse_args() a__ : int = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links a__ : Dict = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, 'artifacts.json'), 'w', encoding='UTF-8') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print('=' * 8_0) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts a__ : Any = extract_warnings(args.output_dir, args.targets) a__ : str = sorted(selected_warnings) with open(os.path.join(args.output_dir, 'selected_warnings.json'), 'w', encoding='UTF-8') as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
80
'''simple docstring''' from __future__ import annotations def _UpperCamelCase ( __A , __A , __A ) -> dict[str, float]: '''simple docstring''' if (voltage, current, resistance).count(0 ) != 1: raise ValueError("One and only one argument must be 0" ) if resistance < 0: raise ValueError("Resistance cannot be negative" ) if voltage == 0: return {"voltage": float(current * resistance )} elif current == 0: return {"current": voltage / resistance} elif resistance == 0: return {"resistance": voltage / current} else: raise ValueError("Exactly one argument must be 0" ) if __name__ == "__main__": import doctest doctest.testmod()
80
1
'''simple docstring''' import torch from diffusers import UnCLIPScheduler from .test_schedulers import SchedulerCommonTest class _snake_case ( a__ ): lowerCAmelCase :int = (UnCLIPScheduler,) def snake_case__ ( self , **_lowerCamelCase): UpperCAmelCase__ : Tuple = { """num_train_timesteps""": 1000, """variance_type""": """fixed_small_log""", """clip_sample""": True, """clip_sample_range""": 1.0, """prediction_type""": """epsilon""", } config.update(**_lowerCamelCase) return config def snake_case__ ( self): for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_lowerCamelCase) def snake_case__ ( self): for variance in ["fixed_small_log", "learned_range"]: self.check_over_configs(variance_type=_lowerCamelCase) def snake_case__ ( self): for clip_sample in [True, False]: self.check_over_configs(clip_sample=_lowerCamelCase) def snake_case__ ( self): for clip_sample_range in [1, 5, 10, 20]: self.check_over_configs(clip_sample_range=_lowerCamelCase) def snake_case__ ( self): for prediction_type in ["epsilon", "sample"]: self.check_over_configs(prediction_type=_lowerCamelCase) def snake_case__ ( self): for time_step in [0, 500, 999]: for prev_timestep in [None, 5, 100, 250, 500, 750]: if prev_timestep is not None and prev_timestep >= time_step: continue self.check_over_forward(time_step=_lowerCamelCase , prev_timestep=_lowerCamelCase) def snake_case__ ( self): UpperCAmelCase__ : Tuple = self.scheduler_classes[0] UpperCAmelCase__ : List[str] = self.get_scheduler_config(variance_type="""fixed_small_log""") UpperCAmelCase__ : Optional[int] = scheduler_class(**_lowerCamelCase) assert torch.sum(torch.abs(scheduler._get_variance(0) - 1.0_0_0_0e-1_0)) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487) - 0.0549625)) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999) - 0.9994987)) < 1e-5 def snake_case__ ( self): UpperCAmelCase__ : List[Any] = self.scheduler_classes[0] UpperCAmelCase__ : Tuple = self.get_scheduler_config(variance_type="""learned_range""") UpperCAmelCase__ : Tuple = scheduler_class(**_lowerCamelCase) UpperCAmelCase__ : Union[str, Any] = 0.5 assert scheduler._get_variance(1 , predicted_variance=_lowerCamelCase) - -10.1712790 < 1e-5 assert scheduler._get_variance(487 , predicted_variance=_lowerCamelCase) - -5.7998052 < 1e-5 assert scheduler._get_variance(999 , predicted_variance=_lowerCamelCase) - -0.0010011 < 1e-5 def snake_case__ ( self): UpperCAmelCase__ : Optional[Any] = self.scheduler_classes[0] UpperCAmelCase__ : Tuple = self.get_scheduler_config() UpperCAmelCase__ : Any = scheduler_class(**_lowerCamelCase) UpperCAmelCase__ : List[Any] = scheduler.timesteps UpperCAmelCase__ : Union[str, Any] = self.dummy_model() UpperCAmelCase__ : int = self.dummy_sample_deter UpperCAmelCase__ : Any = torch.manual_seed(0) for i, t in enumerate(_lowerCamelCase): # 1. predict noise residual UpperCAmelCase__ : Tuple = model(_lowerCamelCase , _lowerCamelCase) # 2. predict previous mean of sample x_t-1 UpperCAmelCase__ : Tuple = scheduler.step(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , generator=_lowerCamelCase).prev_sample UpperCAmelCase__ : Dict = pred_prev_sample UpperCAmelCase__ : Union[str, Any] = torch.sum(torch.abs(_lowerCamelCase)) UpperCAmelCase__ : Dict = torch.mean(torch.abs(_lowerCamelCase)) assert abs(result_sum.item() - 252.2682495) < 1e-2 assert abs(result_mean.item() - 0.3284743) < 1e-3 def snake_case__ ( self): UpperCAmelCase__ : str = self.scheduler_classes[0] UpperCAmelCase__ : str = self.get_scheduler_config() UpperCAmelCase__ : List[str] = scheduler_class(**_lowerCamelCase) scheduler.set_timesteps(25) UpperCAmelCase__ : Tuple = scheduler.timesteps UpperCAmelCase__ : List[Any] = self.dummy_model() UpperCAmelCase__ : Dict = self.dummy_sample_deter UpperCAmelCase__ : List[Any] = torch.manual_seed(0) for i, t in enumerate(_lowerCamelCase): # 1. predict noise residual UpperCAmelCase__ : Union[str, Any] = model(_lowerCamelCase , _lowerCamelCase) if i + 1 == timesteps.shape[0]: UpperCAmelCase__ : Dict = None else: UpperCAmelCase__ : Union[str, Any] = timesteps[i + 1] # 2. predict previous mean of sample x_t-1 UpperCAmelCase__ : Optional[Any] = scheduler.step( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , prev_timestep=_lowerCamelCase , generator=_lowerCamelCase).prev_sample UpperCAmelCase__ : Tuple = pred_prev_sample UpperCAmelCase__ : Optional[int] = torch.sum(torch.abs(_lowerCamelCase)) UpperCAmelCase__ : Union[str, Any] = torch.mean(torch.abs(_lowerCamelCase)) assert abs(result_sum.item() - 258.2044983) < 1e-2 assert abs(result_mean.item() - 0.3362038) < 1e-3 def snake_case__ ( self): pass def snake_case__ ( self): pass
283
'''simple docstring''' import colorsys from PIL import Image # type: ignore def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase__ : Optional[Any] = x UpperCAmelCase__ : Optional[int] = y for step in range(UpperCamelCase__ ): # noqa: B007 UpperCAmelCase__ : List[str] = a * a - b * b + x UpperCAmelCase__ : Optional[int] = 2 * a * b + y UpperCAmelCase__ : int = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def _UpperCamelCase ( UpperCamelCase__ ): if distance == 1: return (0, 0, 0) else: return (2_5_5, 2_5_5, 2_5_5) def _UpperCamelCase ( UpperCamelCase__ ): if distance == 1: return (0, 0, 0) else: return tuple(round(i * 2_5_5 ) for i in colorsys.hsv_to_rgb(UpperCamelCase__ , 1 , 1 ) ) def _UpperCamelCase ( UpperCamelCase__ = 8_0_0 , UpperCamelCase__ = 6_0_0 , UpperCamelCase__ = -0.6 , UpperCamelCase__ = 0 , UpperCamelCase__ = 3.2 , UpperCamelCase__ = 5_0 , UpperCamelCase__ = True , ): UpperCAmelCase__ : str = Image.new("""RGB""" , (image_width, image_height) ) UpperCAmelCase__ : Optional[int] = img.load() # loop through the image-coordinates for image_x in range(UpperCamelCase__ ): for image_y in range(UpperCamelCase__ ): # determine the figure-coordinates based on the image-coordinates UpperCAmelCase__ : Union[str, Any] = figure_width / image_width * image_height UpperCAmelCase__ : Union[str, Any] = figure_center_x + (image_x / image_width - 0.5) * figure_width UpperCAmelCase__ : Tuple = figure_center_y + (image_y / image_height - 0.5) * figure_height UpperCAmelCase__ : List[str] = get_distance(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: UpperCAmelCase__ : Any = get_color_coded_rgb(UpperCamelCase__ ) else: UpperCAmelCase__ : Tuple = get_black_and_white_rgb(UpperCamelCase__ ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure __A =get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
283
1