code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
from __future__ import annotations def a__ ( A__, A__, A__, A__ ): SCREAMING_SNAKE_CASE_ : List[Any] = [] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = input_list[low:mid], input_list[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0 ) ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = result + left + right return input_list def a__ ( A__ ): if len(A__ ) <= 1: return input_list SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(A__ ) # iteration for two-way merging SCREAMING_SNAKE_CASE_ : Any = 2 while p <= len(A__ ): # getting low, high and middle value for merge-sort of single list for i in range(0, len(A__ ), A__ ): SCREAMING_SNAKE_CASE_ : str = i SCREAMING_SNAKE_CASE_ : Union[str, Any] = i + p - 1 SCREAMING_SNAKE_CASE_ : Optional[int] = (low + high + 1) // 2 SCREAMING_SNAKE_CASE_ : List[Any] = merge(A__, A__, A__, A__ ) # final merge of last two parts if p * 2 >= len(A__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = i SCREAMING_SNAKE_CASE_ : int = merge(A__, 0, A__, len(A__ ) - 1 ) break p *= 2 return input_list if __name__ == "__main__": lowerCAmelCase__ : Optional[Any] =input('Enter numbers separated by a comma:\n').strip() if user_input == "": lowerCAmelCase__ : Dict =[] else: lowerCAmelCase__ : Optional[int] =[int(item.strip()) for item in user_input.split(',')] print(iter_merge_sort(unsorted))
101
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=3 , _UpperCAmelCase=18 , _UpperCAmelCase=30 , _UpperCAmelCase=400 , _UpperCAmelCase=True , _UpperCAmelCase=None , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=[0.5, 0.5, 0.5] , _UpperCAmelCase=[0.5, 0.5, 0.5] , ): __a : int = parent __a : str = batch_size __a : List[Any] = num_channels __a : Union[str, Any] = image_size __a : List[Any] = min_resolution __a : str = max_resolution __a : List[str] = do_resize __a : Optional[int] = size if size is not None else {'''height''': 18, '''width''': 20} __a : str = do_thumbnail __a : str = do_align_axis __a : Dict = do_pad __a : Union[str, Any] = do_normalize __a : List[str] = image_mean __a : Optional[int] = image_std def _lowerCamelCase ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = DonutImageProcessor if is_vision_available() else None def _lowerCamelCase ( self ): __a : Tuple = DonutImageProcessingTester(self ) @property def _lowerCamelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_resize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_thumbnail''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_align_long_axis''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_pad''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_mean''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_std''' ) ) def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 20} ) __a : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) # Previous config had dimensions in (width, height) order __a : int = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'''height''': 84, '''width''': 42} ) def _lowerCamelCase ( self ): pass @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , Image.Image ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : int = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , numpify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , np.ndarray ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : str = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , torchify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , torch.Tensor ) # Test not batched input __a : Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : List[str] = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
52
0
"""simple docstring""" import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __magic_name__ : int = logging.get_logger(__name__) class lowercase__ ( __SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase : Any = ["""input_features""", """is_longer"""] def __init__( self , _A=6_4 , _A=4_8_0_0_0 , _A=4_8_0 , _A=1_0 , _A=1_0_2_4 , _A=0.0 , _A=False , _A = 0 , _A = 1_4_0_0_0 , _A = None , _A = "fusion" , _A = "repeatpad" , **_A , ): '''simple docstring''' super().__init__( feature_size=_A , sampling_rate=_A , padding_value=_A , return_attention_mask=_A , **_A , ) UpperCamelCase : Optional[int] = top_db UpperCamelCase : Tuple = truncation UpperCamelCase : str = padding UpperCamelCase : str = fft_window_size UpperCamelCase : int = (fft_window_size >> 1) + 1 UpperCamelCase : Optional[int] = hop_length UpperCamelCase : Tuple = max_length_s UpperCamelCase : Optional[Any] = max_length_s * sampling_rate UpperCamelCase : List[str] = sampling_rate UpperCamelCase : List[Any] = frequency_min UpperCamelCase : Union[str, Any] = frequency_max UpperCamelCase : Optional[int] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_A , min_frequency=_A , max_frequency=_A , sampling_rate=_A , norm=_A , mel_scale="""htk""" , ) UpperCamelCase : str = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_A , min_frequency=_A , max_frequency=_A , sampling_rate=_A , norm="""slaney""" , mel_scale="""slaney""" , ) def _a ( self ): '''simple docstring''' UpperCamelCase : Tuple = copy.deepcopy(self.__dict__ ) UpperCamelCase : Dict = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def _a ( self , _A , _A = None ): '''simple docstring''' UpperCamelCase : Dict = spectrogram( _A , window_function(self.fft_window_size , """hann""" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_A , log_mel="""dB""" , ) return log_mel_spectrogram.T def _a ( self , _A , _A , _A ): '''simple docstring''' UpperCamelCase : int = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk UpperCamelCase : int = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk UpperCamelCase : Union[str, Any] = [0] # randomly choose index for each part UpperCamelCase : Optional[int] = np.random.choice(ranges[0] ) UpperCamelCase : List[Any] = np.random.choice(ranges[1] ) UpperCamelCase : int = np.random.choice(ranges[2] ) UpperCamelCase : str = mel[idx_front : idx_front + chunk_frames, :] UpperCamelCase : List[Any] = mel[idx_middle : idx_middle + chunk_frames, :] UpperCamelCase : int = mel[idx_back : idx_back + chunk_frames, :] UpperCamelCase : int = torch.tensor(mel[None, None, :] ) UpperCamelCase : Union[str, Any] = torch.nn.functional.interpolate( _A , size=[chunk_frames, 6_4] , mode="""bilinear""" , align_corners=_A ) UpperCamelCase : List[Any] = mel_shrink[0][0].numpy() UpperCamelCase : Optional[int] = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def _a ( self , _A , _A , _A , _A ): '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": UpperCamelCase : Any = True # random crop to max_length (for compatibility) -> this should be handled by self.pad UpperCamelCase : Union[str, Any] = len(_A ) - max_length UpperCamelCase : Optional[int] = np.random.randint(0 , overflow + 1 ) UpperCamelCase : Optional[int] = waveform[idx : idx + max_length] UpperCamelCase : Union[str, Any] = self._np_extract_fbank_features(_A , self.mel_filters_slaney )[None, :] elif truncation == "fusion": UpperCamelCase : List[Any] = self._np_extract_fbank_features(_A , self.mel_filters ) UpperCamelCase : Union[str, Any] = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed UpperCamelCase : Any = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. UpperCamelCase : Optional[Any] = np.stack([mel, mel, mel, mel] , axis=0 ) UpperCamelCase : Optional[Any] = False else: UpperCamelCase : Optional[int] = self._random_mel_fusion(_A , _A , _A ) UpperCamelCase : Any = True else: raise NotImplementedError(f"""data_truncating {truncation} not implemented""" ) else: UpperCamelCase : Union[str, Any] = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": UpperCamelCase : int = int(max_length / len(_A ) ) UpperCamelCase : Tuple = np.stack(np.tile(_A , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": UpperCamelCase : Any = int(max_length / len(_A ) ) UpperCamelCase : str = np.stack(np.tile(_A , _A ) ) UpperCamelCase : Optional[int] = np.pad(_A , (0, max_length - waveform.shape[0]) , mode="""constant""" , constant_values=0 ) if truncation == "fusion": UpperCamelCase : List[Any] = self._np_extract_fbank_features(_A , self.mel_filters ) UpperCamelCase : Any = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: UpperCamelCase : Union[str, Any] = self._np_extract_fbank_features(_A , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self , _A , _A = None , _A = None , _A = None , _A = None , _A = None , **_A , ): '''simple docstring''' UpperCamelCase : str = truncation if truncation is not None else self.truncation UpperCamelCase : int = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a""" f""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input""" f""" was sampled with {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) UpperCamelCase : int = isinstance(_A , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) UpperCamelCase : int = is_batched_numpy or ( isinstance(_A , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: UpperCamelCase : Any = [np.asarray(_A , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_A , np.ndarray ): UpperCamelCase : Any = np.asarray(_A , dtype=np.floataa ) elif isinstance(_A , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): UpperCamelCase : List[Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: UpperCamelCase : Optional[int] = [np.asarray(_A )] # convert to mel spectrogram, truncate and pad if needed. UpperCamelCase : List[str] = [ self._get_input_mel(_A , max_length if max_length else self.nb_max_samples , _A , _A ) for waveform in raw_speech ] UpperCamelCase : Union[str, Any] = [] UpperCamelCase : Optional[int] = [] for mel, longer in padded_inputs: input_mel.append(_A ) is_longer.append(_A ) if truncation == "fusion" and sum(_A ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer UpperCamelCase : int = np.random.randint(0 , len(_A ) ) UpperCamelCase : List[str] = True if isinstance(input_mel[0] , _A ): UpperCamelCase : Dict = [np.asarray(_A , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool UpperCamelCase : Optional[int] = [[longer] for longer in is_longer] UpperCamelCase : Union[str, Any] = {"""input_features""": input_mel, """is_longer""": is_longer} UpperCamelCase : List[Any] = BatchFeature(_A ) if return_tensors is not None: UpperCamelCase : str = input_features.convert_to_tensors(_A ) return input_features
102
"""simple docstring""" from __future__ import annotations def __A ( a_ :list[int]) -> int: if not nums: return 0 __a : Any = nums[0] __a : Optional[Any] = 0 for num in nums[1:]: __a , __a : Optional[Any] = ( max_excluding + num, max(a_ , a_), ) return max(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" import random from typing import Any def snake_case ( lowerCAmelCase_ ) -> list[Any]: for _ in range(len(lowerCAmelCase_ ) ): _snake_case = random.randint(0 , len(lowerCAmelCase_ ) - 1 ) _snake_case = random.randint(0 , len(lowerCAmelCase_ ) - 1 ) _snake_case , _snake_case = data[b], data[a] return data if __name__ == "__main__": snake_case = [0, 1, 2, 3, 4, 5, 6, 7] snake_case = ['''python''', '''says''', '''hello''', '''!'''] print('''Fisher-Yates Shuffle:''') print('''List''', integers, strings) print('''FY Shuffle''', fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))
103
"""simple docstring""" import unittest from transformers import BigBirdTokenizer, BigBirdTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A = '''▁''' A = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = BigBirdTokenizer __lowerCAmelCase = BigBirdTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = True def _lowerCamelCase ( self ): super().setUp() __a : Dict = self.tokenizer_class(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCamelCase ( self ): __a : List[str] = '''<s>''' __a : Tuple = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''[MASK]''' ) self.assertEqual(len(_UpperCAmelCase ) , 1004 ) def _lowerCamelCase ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def _lowerCamelCase ( self ): if not self.test_rust_tokenizer: return __a : Dict = self.get_tokenizer() __a : Any = self.get_rust_tokenizer() __a : int = '''I was born in 92000, and this is falsé.''' __a : Optional[Any] = tokenizer.tokenize(_UpperCAmelCase ) __a : List[str] = rust_tokenizer.tokenize(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Dict = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) __a : Any = rust_tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Tuple = self.get_rust_tokenizer() __a : Tuple = tokenizer.encode(_UpperCAmelCase ) __a : List[Any] = rust_tokenizer.encode(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = BigBirdTokenizer(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) __a : Optional[int] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) __a : Optional[int] = 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''', '''é''', '''.''', ] , ) __a : Optional[Any] = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) __a : Optional[int] = 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>''', '''.''', ] , ) @cached_property def _lowerCamelCase ( self ): return BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) @slow def _lowerCamelCase ( self ): __a : str = '''Hello World!''' __a : str = [65, 18536, 2260, 101, 66] self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @slow def _lowerCamelCase ( self ): __a : 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 __a : Optional[Any] = [65, 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, 66] # noqa: E231 # fmt: on self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @require_torch @slow def _lowerCamelCase ( self ): import torch from transformers import BigBirdConfig, BigBirdModel # Build sequence __a : List[Any] = list(self.big_tokenizer.get_vocab().keys() )[:10] __a : List[str] = ''' '''.join(_UpperCAmelCase ) __a : Tuple = self.big_tokenizer.encode_plus(_UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Any = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Optional[Any] = BigBirdConfig(attention_type='''original_full''' ) __a : Tuple = BigBirdModel(_UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_UpperCAmelCase ) model(**_UpperCAmelCase ) @slow def _lowerCamelCase ( self ): __a : Union[str, Any] = BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) __a : List[Any] = tokenizer.decode(tokenizer('''Paris is the [MASK].''' ).input_ids ) self.assertTrue(decoded_text == '''[CLS] Paris is the[MASK].[SEP]''' ) @slow def _lowerCamelCase ( self ): # fmt: off __a : Optional[Any] = {'''input_ids''': [[65, 39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114, 66], [65, 448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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], [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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name='''google/bigbird-roberta-base''' , revision='''215c99f1600e06f83acce68422f2035b2b5c3510''' , )
52
0
"""simple docstring""" import requests from bsa import BeautifulSoup def _lowerCamelCase ( UpperCAmelCase_ : str, UpperCAmelCase_ : dict ) -> str: """simple docstring""" A__ = BeautifulSoup(requests.get(UpperCAmelCase_, params=UpperCAmelCase_ ).content, "html.parser" ) A__ = soup.find("div", attrs={"class": "gs_ri"} ) A__ = div.find("div", attrs={"class": "gs_fl"} ).find_all("a" ) return anchors[2].get_text() if __name__ == "__main__": UpperCamelCase = { """title""": ( """Precisely geometry controlled microsupercapacitors for ultrahigh areal """ """capacitance, volumetric capacitance, and energy density""" ), """journal""": """Chem. Mater.""", """volume""": 30, """pages""": """3979-3990""", """year""": 2018, """hl""": """en""", } print(get_citation("""https://scholar.google.com/scholar_lookup""", params=params))
104
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A = logging.get_logger(__name__) A = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''convnextv2''' def __init__( self , _UpperCAmelCase=3 , _UpperCAmelCase=4 , _UpperCAmelCase=4 , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=224 , _UpperCAmelCase=None , _UpperCAmelCase=None , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : List[str] = num_channels __a : str = patch_size __a : Dict = num_stages __a : List[str] = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes __a : List[str] = [3, 3, 9, 3] if depths is None else depths __a : List[Any] = hidden_act __a : Any = initializer_range __a : Optional[int] = layer_norm_eps __a : List[Any] = drop_path_rate __a : Any = image_size __a : str = ['''stem'''] + [f"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] __a , __a : Optional[int] = get_aligned_output_features_output_indices( out_features=_UpperCAmelCase , out_indices=_UpperCAmelCase , stage_names=self.stage_names )
52
0
from math import factorial def __UpperCAmelCase ( lowerCamelCase_ : int = 1_00 ) -> int: """simple docstring""" return sum(int(lowerCamelCase_ ) for x in str(factorial(lowerCamelCase_ ) ) ) if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
105
"""simple docstring""" import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = (DDPMScheduler,) def _lowerCamelCase ( self , **_UpperCAmelCase ): __a : int = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**_UpperCAmelCase ) return config def _lowerCamelCase ( self ): for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_UpperCAmelCase , beta_end=_UpperCAmelCase ) def _lowerCamelCase ( self ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_UpperCAmelCase ) def _lowerCamelCase ( self ): for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=_UpperCAmelCase ) def _lowerCamelCase ( self ): self.check_over_configs(thresholding=_UpperCAmelCase ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_UpperCAmelCase , prediction_type=_UpperCAmelCase , sample_max_value=_UpperCAmelCase , ) def _lowerCamelCase ( self ): for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for t in [0, 500, 999]: self.check_over_forward(time_step=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Dict = scheduler_class(**_UpperCAmelCase ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def _lowerCamelCase ( self ): __a : int = self.scheduler_classes[0] __a : int = self.get_scheduler_config() __a : Optional[Any] = scheduler_class(**_UpperCAmelCase ) __a : int = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[Any] = self.dummy_sample_deter __a : Union[str, Any] = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Optional[int] = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : List[Any] = pred_prev_sample __a : int = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : Union[str, Any] = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def _lowerCamelCase ( self ): __a : Dict = self.scheduler_classes[0] __a : int = self.get_scheduler_config(prediction_type='''v_prediction''' ) __a : int = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[str] = self.dummy_sample_deter __a : str = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Dict = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : Optional[int] = pred_prev_sample __a : Optional[int] = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : int = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Any = self.get_scheduler_config() __a : str = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_UpperCAmelCase ) __a : List[Any] = scheduler.timesteps for i, timestep in enumerate(_UpperCAmelCase ): if i == len(_UpperCAmelCase ) - 1: __a : Union[str, Any] = -1 else: __a : str = timesteps[i + 1] __a : Dict = scheduler.previous_timestep(_UpperCAmelCase ) __a : str = prev_t.item() self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Tuple = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Optional[Any] = [100, 87, 50, 51, 0] with self.assertRaises(_UpperCAmelCase , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] __a : Optional[int] = len(_UpperCAmelCase ) with self.assertRaises(_UpperCAmelCase , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=_UpperCAmelCase , timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : List[str] = scheduler_class(**_UpperCAmelCase ) __a : List[Any] = [scheduler.config.num_train_timesteps] with self.assertRaises( _UpperCAmelCase , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=_UpperCAmelCase )
52
0
import math def lowerCamelCase_ ( lowerCAmelCase__ : int = 100 ) -> int: '''simple docstring''' A = sum(i * i for i in range(1 , n + 1 ) ) A = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(F'''{solution() = }''')
106
"""simple docstring""" import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset A = random.Random() def __A ( a_ :Tuple , a_ :Dict=1.0 , a_ :str=None , a_ :List[Any]=None) -> Dict: if rng is None: __a : Any = global_rng __a : Tuple = [] for batch_idx in range(shape[0]): values.append([]) for _ in range(shape[1]): values[-1].append(rng.random() * scale) return values class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=400 , _UpperCAmelCase=2000 , _UpperCAmelCase=2048 , _UpperCAmelCase=128 , _UpperCAmelCase=1 , _UpperCAmelCase=512 , _UpperCAmelCase=30 , _UpperCAmelCase=44100 , ): __a : Any = parent __a : Tuple = batch_size __a : Tuple = min_seq_length __a : List[str] = max_seq_length __a : List[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __a : Tuple = spectrogram_length __a : int = feature_size __a : int = num_audio_channels __a : Tuple = hop_length __a : List[Any] = chunk_length __a : Any = sampling_rate def _lowerCamelCase ( self ): return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def _lowerCamelCase ( self , _UpperCAmelCase=False , _UpperCAmelCase=False ): def _flatten(_UpperCAmelCase ): return list(itertools.chain(*_UpperCAmelCase ) ) if equal_length: __a : Tuple = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __a : Tuple = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __a : Optional[Any] = [np.asarray(_UpperCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = TvltFeatureExtractor def _lowerCamelCase ( self ): __a : Optional[Any] = TvltFeatureExtractionTester(self ) def _lowerCamelCase ( self ): __a : int = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''spectrogram_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''feature_size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''num_audio_channels''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''hop_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''chunk_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''sampling_rate''' ) ) def _lowerCamelCase ( self ): __a : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : List[str] = feat_extract_first.save_pretrained(_UpperCAmelCase )[0] check_json_file_has_correct_format(_UpperCAmelCase ) __a : Union[str, Any] = self.feature_extraction_class.from_pretrained(_UpperCAmelCase ) __a : Tuple = feat_extract_first.to_dict() __a : List[Any] = feat_extract_second.to_dict() __a : int = dict_first.pop('''mel_filters''' ) __a : List[Any] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : int = os.path.join(_UpperCAmelCase , '''feat_extract.json''' ) feat_extract_first.to_json_file(_UpperCAmelCase ) __a : Optional[Any] = self.feature_extraction_class.from_json_file(_UpperCAmelCase ) __a : Optional[Any] = feat_extract_first.to_dict() __a : Any = feat_extract_second.to_dict() __a : Optional[Any] = dict_first.pop('''mel_filters''' ) __a : Dict = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): # Initialize feature_extractor __a : str = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 __a : Any = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __a : Union[str, Any] = [np.asarray(_UpperCAmelCase ) for speech_input in speech_inputs] # Test not batched input __a : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched __a : int = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking __a : List[Any] = feature_extractor( _UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 , mask_audio=_UpperCAmelCase ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. __a : str = [floats_list((1, x) )[0] for x in (800, 800, 800)] __a : Any = np.asarray(_UpperCAmelCase ) __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def _lowerCamelCase ( self , _UpperCAmelCase ): __a : int = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech __a : int = ds.sort('''id''' ).select(range(_UpperCAmelCase ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowerCamelCase ( self ): __a : List[str] = self._load_datasamples(1 ) __a : Tuple = TvltFeatureExtractor() __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) __a : Dict = torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _UpperCAmelCase , atol=1e-4 ) )
52
0
'''simple docstring''' from __future__ import annotations from random import choice def _SCREAMING_SNAKE_CASE ( __snake_case : Optional[int] ): return choice(__snake_case ) def _SCREAMING_SNAKE_CASE ( __snake_case : list[int] , __snake_case : int ): _A = random_pivot(__snake_case ) # partition based on pivot # linear time _A = [e for e in lst if e < pivot] _A = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(__snake_case ) == k - 1: return pivot # pivot is in elements bigger than k elif len(__snake_case ) < k - 1: return kth_number(__snake_case , k - len(__snake_case ) - 1 ) # pivot is in elements smaller than k else: return kth_number(__snake_case , __snake_case ) if __name__ == "__main__": import doctest doctest.testmod()
107
"""simple docstring""" from __future__ import annotations class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a , __a : List[Any] = text, pattern __a , __a : Tuple = len(_UpperCAmelCase ), len(_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def _lowerCamelCase ( self ): # searches pattern in text and returns index positions __a : Dict = [] for i in range(self.textLen - self.patLen + 1 ): __a : List[str] = self.mismatch_in_text(_UpperCAmelCase ) if mismatch_index == -1: positions.append(_UpperCAmelCase ) else: __a : Tuple = self.match_in_pattern(self.text[mismatch_index] ) __a : Optional[int] = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions A = '''ABAABA''' A = '''AB''' A = BoyerMooreSearch(text, pattern) A = bms.bad_character_heuristic() if len(positions) == 0: print('''No match found''') else: print('''Pattern found in following positions: ''') print(positions)
52
0
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm __a: Optional[int] = re.compile('''[^A-Za-z_0-9]''') # parameters used in DuplicationIndex __a: List[str] = 10 __a: List[str] = 256 def _SCREAMING_SNAKE_CASE ( __snake_case ) -> Optional[MinHash]: if len(__snake_case ) < MIN_NUM_TOKENS: return None _UpperCAmelCase = MinHash(num_perm=__snake_case ) for token in set(__snake_case ): min_hash.update(token.encode() ) return min_hash def _SCREAMING_SNAKE_CASE ( __snake_case ) -> Set[str]: return {t for t in NON_ALPHA.split(__snake_case ) if len(t.strip() ) > 0} class SCREAMING_SNAKE_CASE__ : '''simple docstring''' def __init__( self : Any , *, lowerCamelCase : float = 0.85 , ) -> Optional[int]: """simple docstring""" _UpperCAmelCase = duplication_jaccard_threshold _UpperCAmelCase = NUM_PERM _UpperCAmelCase = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _UpperCAmelCase = defaultdict(lowerCamelCase ) def lowerCamelCase ( self : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : MinHash ) -> None: """simple docstring""" _UpperCAmelCase = self._index.query(lowerCamelCase ) if code_key in self._index.keys: print(f"""Duplicate key {code_key}""" ) return self._index.insert(lowerCamelCase , lowerCamelCase ) if len(lowerCamelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(lowerCamelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(lowerCamelCase ) def lowerCamelCase ( self : List[Any] ) -> List[List[Dict]]: """simple docstring""" _UpperCAmelCase = [] for base, duplicates in self._duplicate_clusters.items(): _UpperCAmelCase = [base] + list(lowerCamelCase ) # reformat the cluster to be a list of dict _UpperCAmelCase = [{"""base_index""": el[0], """repo_name""": el[1], """path""": el[2]} for el in cluster] duplicate_clusters.append(lowerCamelCase ) return duplicate_clusters def lowerCamelCase ( self : Optional[int] , lowerCamelCase : int ) -> None: """simple docstring""" _UpperCAmelCase = self.get_duplicate_clusters() with open(lowerCamelCase , """w""" ) as f: json.dump(lowerCamelCase , lowerCamelCase ) def _SCREAMING_SNAKE_CASE ( __snake_case ) -> Optional[Any]: _UpperCAmelCase , _UpperCAmelCase = element _UpperCAmelCase = get_min_hash([t for t in NON_ALPHA.split(data["""content"""] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _SCREAMING_SNAKE_CASE ( __snake_case ) -> List[Any]: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__snake_case , max_queue_size=1_0_0_0_0 ) , chunksize=1_0_0 , ): if data is not None: yield data def _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case ) -> Tuple: _UpperCAmelCase = DuplicationIndex(duplication_jaccard_threshold=__snake_case ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__snake_case ) ) , max_queue_size=1_0_0 ) ): di.add(__snake_case , __snake_case ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case ) -> float: _UpperCAmelCase = get_tokens(__snake_case ) _UpperCAmelCase = get_tokens(__snake_case ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) __a: List[str] = None def _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case ) -> Union[str, Any]: _UpperCAmelCase = [] for elementa in cluster: _UpperCAmelCase = _shared_dataset[elementa["""base_index"""]]["""content"""] for elementa in extremes: _UpperCAmelCase = _shared_dataset[elementa["""base_index"""]]["""content"""] if jaccard_similarity(__snake_case , __snake_case ) >= jaccard_threshold: elementa["copies"] += 1 break else: _UpperCAmelCase = 1 extremes.append(__snake_case ) return extremes def _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case , __snake_case ) -> List[str]: global _shared_dataset _UpperCAmelCase = dataset _UpperCAmelCase = [] _UpperCAmelCase = partial(_find_cluster_extremes_shared , jaccard_threshold=__snake_case ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __snake_case , __snake_case , ) , total=len(__snake_case ) , ): extremes_list.append(__snake_case ) return extremes_list def _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: _UpperCAmelCase = make_duplicate_clusters(__snake_case , __snake_case ) _UpperCAmelCase = {x["""base_index"""] for cluster in duplicate_clusters for x in cluster} _UpperCAmelCase = {} _UpperCAmelCase = find_extremes(__snake_case , __snake_case , __snake_case ) for extremes in extremes_clusters: for element in extremes: _UpperCAmelCase = element _UpperCAmelCase = duplicate_indices - set(extreme_dict.keys() ) _UpperCAmelCase = dataset.filter(lambda __snake_case , __snake_case : idx not in remove_indices , with_indices=__snake_case ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _UpperCAmelCase = element["""base_index"""] in extreme_dict if element["is_extreme"]: _UpperCAmelCase = extreme_dict[element["""base_index"""]]["""copies"""] print(f"""Original dataset size: {len(__snake_case )}""" ) print(f"""Number of duplicate clusters: {len(__snake_case )}""" ) print(f"""Files in duplicate cluster: {len(__snake_case )}""" ) print(f"""Unique files in duplicate cluster: {len(__snake_case )}""" ) print(f"""Filtered dataset size: {len(__snake_case )}""" ) return ds_filter, duplicate_clusters
108
"""simple docstring""" import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": A = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') A = F'https://www.google.com/search?q={query}&num=100' A = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: A = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: A = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
0
'''simple docstring''' from ...processing_utils import ProcessorMixin class __a ( _snake_case ): __UpperCamelCase : str = ['image_processor', 'feature_extractor'] __UpperCamelCase : List[str] = 'TvltImageProcessor' __UpperCamelCase : int = 'TvltFeatureExtractor' def __init__( self : List[str] ,lowerCamelCase : List[Any] ,lowerCamelCase : Any ): '''simple docstring''' super().__init__(image_processor=lowerCamelCase ,feature_extractor=lowerCamelCase ) __SCREAMING_SNAKE_CASE = image_processor __SCREAMING_SNAKE_CASE = feature_extractor def __call__( self : Union[str, Any] ,lowerCamelCase : Union[str, Any]=None ,lowerCamelCase : Optional[Any]=None ,lowerCamelCase : Any=None ,lowerCamelCase : int=None ,lowerCamelCase : Optional[int]=False ,lowerCamelCase : List[Any]=False ,*lowerCamelCase : List[str] ,**lowerCamelCase : Optional[int] ,): '''simple docstring''' if images is None and audio is None: raise ValueError("""You need to specify either an `images` or `audio` input to process.""" ) __SCREAMING_SNAKE_CASE = None if images is not None: __SCREAMING_SNAKE_CASE = self.image_processor(lowerCamelCase ,mask_pixel=lowerCamelCase ,*lowerCamelCase ,**lowerCamelCase ) if images_mixed is not None: __SCREAMING_SNAKE_CASE = self.image_processor(lowerCamelCase ,is_mixed=lowerCamelCase ,*lowerCamelCase ,**lowerCamelCase ) if audio is not None: __SCREAMING_SNAKE_CASE = self.feature_extractor( lowerCamelCase ,*lowerCamelCase ,sampling_rate=lowerCamelCase ,mask_audio=lowerCamelCase ,**lowerCamelCase ) __SCREAMING_SNAKE_CASE = {} if audio is not None: output_dict.update(lowerCamelCase ) if images is not None: output_dict.update(lowerCamelCase ) if images_mixed_dict is not None: output_dict.update(lowerCamelCase ) return output_dict @property def UpperCAmelCase__ ( self : Union[str, Any] ): '''simple docstring''' __SCREAMING_SNAKE_CASE = self.image_processor.model_input_names __SCREAMING_SNAKE_CASE = self.feature_extractor.model_input_names return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names ) )
109
"""simple docstring""" import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = 0 __lowerCAmelCase = False __lowerCAmelCase = 3.0 class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_UpperCAmelCase ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.2_5 ).to_kwargs() , {'''a''': 2, '''c''': 2.2_5} ) @require_cuda def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a : List[Any] = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() __a : int = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a : Optional[Any] = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_0_2_4.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _UpperCAmelCase ) @require_multi_gpu def _lowerCamelCase ( self ): __a : Dict = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] execute_subprocess_async(_UpperCAmelCase , env=os.environ.copy() ) if __name__ == "__main__": A = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) A = Accelerator(kwargs_handlers=[ddp_scaler]) A = torch.nn.Linear(100, 200) A = accelerator.prepare(model) # Check the values changed in kwargs A = '''''' A = model.bucket_bytes_cap // (1_024 * 1_024) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
52
0
"""simple docstring""" import requests UpperCamelCase__ = 'https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=' def lowerCamelCase ( _snake_case ): # fetching a list of articles in json format UpperCAmelCase__ : int = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page['articles'] ,1 ): print(F'''{i}.) {article['title']}''' ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='<Your BBC News API key goes here>')
110
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A_: Union[str, Any] = { 'configuration_roc_bert': ['ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RoCBertConfig'], 'tokenization_roc_bert': ['RoCBertTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: pass try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_: Any = [ 'ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RoCBertForCausalLM', 'RoCBertForMaskedLM', 'RoCBertForMultipleChoice', 'RoCBertForPreTraining', 'RoCBertForQuestionAnswering', 'RoCBertForSequenceClassification', 'RoCBertForTokenClassification', 'RoCBertLayer', 'RoCBertModel', 'RoCBertPreTrainedModel', 'load_tf_weights_in_roc_bert', ] if TYPE_CHECKING: from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig from .tokenization_roc_bert import RoCBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: raise OptionalDependencyNotAvailable() try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roc_bert import ( ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, RoCBertForCausalLM, RoCBertForMaskedLM, RoCBertForMultipleChoice, RoCBertForPreTraining, RoCBertForQuestionAnswering, RoCBertForSequenceClassification, RoCBertForTokenClassification, RoCBertLayer, RoCBertModel, RoCBertPreTrainedModel, load_tf_weights_in_roc_bert, ) else: import sys A_: Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
398
"""simple docstring""" import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger A = get_logger(__name__) A = Path(__file__).parent / '''model_card_template.md''' A = uuida().hex A = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES A = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES A = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def __A ( a_ :Union[Dict, str, None] = None) -> str: __a : Union[str, Any] = F"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F"""; torch/{_torch_version}""" if is_flax_available(): ua += F"""; jax/{_jax_version}""" ua += F"""; flax/{_flax_version}""" if is_onnx_available(): ua += F"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''').upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(a_ , a_): ua += "; " + "; ".join(F"""{k}/{v}""" for k, v in user_agent.items()) elif isinstance(a_ , a_): ua += "; " + user_agent return ua def __A ( a_ :str , a_ :Optional[str] = None , a_ :Optional[str] = None) -> Optional[int]: if token is None: __a : Any = HfFolder.get_token() if organization is None: __a : List[Any] = whoami(a_)['''name'''] return F"""{username}/{model_id}""" else: return F"""{organization}/{model_id}""" def __A ( a_ :Union[str, Any] , a_ :List[str]) -> Optional[Any]: if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''') if hasattr(a_ , '''local_rank''') and args.local_rank not in [-1, 0]: return __a : int = args.hub_token if hasattr(a_ , '''hub_token''') else None __a : Any = get_full_repo_name(a_ , token=a_) __a : Tuple = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=a_ , model_name=a_ , repo_name=a_ , dataset_name=args.dataset_name if hasattr(a_ , '''dataset_name''') else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(a_ , '''gradient_accumulation_steps''') else None ) , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta1''') else None , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta2''') else None , adam_weight_decay=args.adam_weight_decay if hasattr(a_ , '''adam_weight_decay''') else None , adam_epsilon=args.adam_epsilon if hasattr(a_ , '''adam_epsilon''') else None , lr_scheduler=args.lr_scheduler if hasattr(a_ , '''lr_scheduler''') else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(a_ , '''lr_warmup_steps''') else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(a_ , '''ema_inv_gamma''') else None , ema_power=args.ema_power if hasattr(a_ , '''ema_power''') else None , ema_max_decay=args.ema_max_decay if hasattr(a_ , '''ema_max_decay''') else None , mixed_precision=args.mixed_precision , ) __a : List[Any] = os.path.join(args.output_dir , '''README.md''') model_card.save(a_) def __A ( a_ :Optional[str] , a_ :Optional[str] = None) -> Union[str, Any]: if resolved_file is None or commit_hash is not None: return commit_hash __a : Any = str(Path(a_).as_posix()) __a : Optional[int] = re.search(R'''snapshots/([^/]+)/''' , a_) if search is None: return None __a : Dict = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(a_) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. A = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) A = os.path.join(hf_cache_home, '''diffusers''') def __A ( a_ :Optional[str] = None , a_ :Optional[str] = None) -> None: if new_cache_dir is None: __a : Dict = DIFFUSERS_CACHE if old_cache_dir is None: __a : List[Any] = old_diffusers_cache __a : Union[str, Any] = Path(a_).expanduser() __a : Dict = Path(a_).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*'''): if old_blob_path.is_file() and not old_blob_path.is_symlink(): __a : List[Any] = new_cache_dir / old_blob_path.relative_to(a_) new_blob_path.parent.mkdir(parents=a_ , exist_ok=a_) os.replace(a_ , a_) try: os.symlink(a_ , a_) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''') # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). A = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): A = 0 else: with open(cache_version_file) as f: try: A = int(f.read()) except ValueError: A = 0 if cache_version < 1: A = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: A = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F'There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F'There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ' '''the directory exists and can be written to.''' ) def __A ( a_ :str , a_ :Optional[str] = None) -> str: if variant is not None: __a : Dict = weights_name.split('''.''') __a : List[Any] = splits[:-1] + [variant] + splits[-1:] __a : Tuple = '''.'''.join(a_) return weights_name def __A ( a_ :List[Any] , *, a_ :Union[str, Any] , a_ :Dict , a_ :Union[str, Any] , a_ :Optional[int] , a_ :str , a_ :Any , a_ :str , a_ :Optional[int] , a_ :str , a_ :Tuple , a_ :List[str]=None , ) -> Dict: __a : int = str(a_) if os.path.isfile(a_): return pretrained_model_name_or_path elif os.path.isdir(a_): if os.path.isfile(os.path.join(a_ , a_)): # Load from a PyTorch checkpoint __a : Union[str, Any] = os.path.join(a_ , a_) return model_file elif subfolder is not None and os.path.isfile( os.path.join(a_ , a_ , a_)): __a : Optional[Any] = os.path.join(a_ , a_ , a_) return model_file else: raise EnvironmentError( F"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""") else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(a_).base_version) >= version.parse('''0.20.0''') ): try: __a : Any = hf_hub_download( a_ , filename=_add_variant(a_ , a_) , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) warnings.warn( F"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.""" , a_ , ) return model_file except: # noqa: E722 warnings.warn( F"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(a_ , a_)} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(a_ , a_)}' so that the correct variant file can be added.""" , a_ , ) try: # 2. Load model file as usual __a : Optional[Any] = hf_hub_download( a_ , filename=a_ , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''') except RevisionNotFoundError: raise EnvironmentError( F"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ '''this model name. Check the model page at ''' F"""'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions.""") except EntryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""") except HTTPError as err: raise EnvironmentError( F"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""") except ValueError: raise EnvironmentError( F"""We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it""" F""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" F""" directory containing a file named {weights_name} or""" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''') except EnvironmentError: raise EnvironmentError( F"""Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from """ '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F"""Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory """ F"""containing a file named {weights_name}""")
52
0
'''simple docstring''' import os def __lowerCAmelCase ( ) -> Tuple: with open(os.path.dirname(a_ ) + '''/grid.txt''' ) as f: __lowerCamelCase = [] # noqa: E741 for _ in range(20 ): l.append([int(a_ ) for x in f.readline().split()] ) __lowerCamelCase = 0 # right for i in range(20 ): for j in range(17 ): __lowerCamelCase = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] if temp > maximum: __lowerCamelCase = temp # down for i in range(17 ): for j in range(20 ): __lowerCamelCase = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] if temp > maximum: __lowerCamelCase = temp # diagonal 1 for i in range(17 ): for j in range(17 ): __lowerCamelCase = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: __lowerCamelCase = temp # diagonal 2 for i in range(17 ): for j in range(3 , 20 ): __lowerCamelCase = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: __lowerCamelCase = temp return maximum if __name__ == "__main__": print(solution())
546
"""simple docstring""" import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''kakaobrain/align-base''': '''https://huggingface.co/kakaobrain/align-base/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_text_model''' def __init__( self , _UpperCAmelCase=30522 , _UpperCAmelCase=768 , _UpperCAmelCase=12 , _UpperCAmelCase=12 , _UpperCAmelCase=3072 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0 , _UpperCAmelCase="absolute" , _UpperCAmelCase=True , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = vocab_size __a : Optional[int] = hidden_size __a : Dict = num_hidden_layers __a : List[Any] = num_attention_heads __a : Optional[int] = hidden_act __a : List[Any] = intermediate_size __a : List[Any] = hidden_dropout_prob __a : List[str] = attention_probs_dropout_prob __a : Optional[int] = max_position_embeddings __a : List[str] = type_vocab_size __a : Tuple = initializer_range __a : Dict = layer_norm_eps __a : Any = position_embedding_type __a : Dict = use_cache __a : Dict = pad_token_id @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : List[str] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the text config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Dict = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_vision_model''' def __init__( self , _UpperCAmelCase = 3 , _UpperCAmelCase = 600 , _UpperCAmelCase = 2.0 , _UpperCAmelCase = 3.1 , _UpperCAmelCase = 8 , _UpperCAmelCase = [3, 3, 5, 3, 5, 5, 3] , _UpperCAmelCase = [32, 16, 24, 40, 80, 112, 192] , _UpperCAmelCase = [16, 24, 40, 80, 112, 192, 320] , _UpperCAmelCase = [] , _UpperCAmelCase = [1, 2, 2, 2, 1, 2, 1] , _UpperCAmelCase = [1, 2, 2, 3, 3, 4, 1] , _UpperCAmelCase = [1, 6, 6, 6, 6, 6, 6] , _UpperCAmelCase = 0.2_5 , _UpperCAmelCase = "swish" , _UpperCAmelCase = 2560 , _UpperCAmelCase = "mean" , _UpperCAmelCase = 0.0_2 , _UpperCAmelCase = 0.0_0_1 , _UpperCAmelCase = 0.9_9 , _UpperCAmelCase = 0.2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : Tuple = num_channels __a : str = image_size __a : List[Any] = width_coefficient __a : Optional[int] = depth_coefficient __a : Union[str, Any] = depth_divisor __a : int = kernel_sizes __a : Dict = in_channels __a : List[str] = out_channels __a : Any = depthwise_padding __a : str = strides __a : Optional[Any] = num_block_repeats __a : Optional[Any] = expand_ratios __a : Any = squeeze_expansion_ratio __a : int = hidden_act __a : Union[str, Any] = hidden_dim __a : Union[str, Any] = pooling_type __a : Tuple = initializer_range __a : List[str] = batch_norm_eps __a : List[Any] = batch_norm_momentum __a : Union[str, Any] = drop_connect_rate __a : List[Any] = sum(_UpperCAmelCase ) * 4 @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : Optional[Any] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the vision config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Optional[Any] = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align''' __lowerCAmelCase = True def __init__( self , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=640 , _UpperCAmelCase=1.0 , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) if text_config is None: __a : Dict = {} logger.info('''text_config is None. Initializing the AlignTextConfig with default values.''' ) if vision_config is None: __a : Any = {} logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.''' ) __a : Any = AlignTextConfig(**_UpperCAmelCase ) __a : Any = AlignVisionConfig(**_UpperCAmelCase ) __a : Optional[int] = projection_dim __a : Union[str, Any] = temperature_init_value __a : int = initializer_range @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = copy.deepcopy(self.__dict__ ) __a : Tuple = self.text_config.to_dict() __a : Union[str, Any] = self.vision_config.to_dict() __a : int = self.__class__.model_type return output
52
0
def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = [0 for i in range(r + 1 )] # nc0 = 1 snake_case_ = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. snake_case_ = min(a_ , a_ ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
362
"""simple docstring""" from __future__ import annotations from random import choice def __A ( a_ :Tuple) -> List[str]: return choice(a_) def __A ( a_ :list[int] , a_ :int) -> int: __a : Optional[int] = random_pivot(a_) # partition based on pivot # linear time __a : Union[str, Any] = [e for e in lst if e < pivot] __a : Any = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(a_) == k - 1: return pivot # pivot is in elements bigger than k elif len(a_) < k - 1: return kth_number(a_ , k - len(a_) - 1) # pivot is in elements smaller than k else: return kth_number(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" import logging from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import arg_to_scheduler from transformers import TrainingArguments UpperCAmelCase__ = logging.getLogger(__name__) @dataclass class a ( _UpperCamelCase ): _snake_case : Tuple = field( default=0.0 , metadata={'help': 'The label smoothing epsilon to apply (if not zero).'} ) _snake_case : str = field(default=_UpperCamelCase , metadata={'help': 'Whether to SortishSamler or not.'} ) _snake_case : List[str] = field( default=_UpperCamelCase , metadata={'help': 'Whether to use generate to calculate generative metrics (ROUGE, BLEU).'} ) _snake_case : List[Any] = field(default=_UpperCamelCase , metadata={'help': 'whether to use adafactor'} ) _snake_case : Tuple = field( default=_UpperCamelCase , metadata={'help': 'Encoder layer dropout probability. Goes into model.config.'} ) _snake_case : Union[str, Any] = field( default=_UpperCamelCase , metadata={'help': 'Decoder layer dropout probability. Goes into model.config.'} ) _snake_case : int = field(default=_UpperCamelCase , metadata={'help': 'Dropout probability. Goes into model.config.'} ) _snake_case : List[str] = field( default=_UpperCamelCase , metadata={'help': 'Attention dropout probability. Goes into model.config.'} ) _snake_case : str = field( default='linear' , metadata={'help': F'''Which lr scheduler to use. Selected in {sorted(arg_to_scheduler.keys() )}'''} , )
277
"""simple docstring""" import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A = logging.getLogger(__name__) def __A ( a_ :Union[str, Any] , a_ :Dict) -> Union[str, Any]: __a : Optional[int] = np.argmax(a_ , axis=1) return np.sum(outputs == labels) def __A ( a_ :Any) -> str: with open(a_ , encoding='''utf_8''') as f: __a : List[Any] = csv.reader(a_) __a : List[str] = [] next(a_) # skip the first line for line in tqdm(a_): output.append((''' '''.join(line[1:5]), line[5], line[6], int(line[-1]) - 1)) return output def __A ( a_ :Dict , a_ :str , a_ :str , a_ :List[Any] , a_ :Tuple , a_ :List[Any]) -> Any: __a : List[str] = [] for dataset in encoded_datasets: __a : List[str] = len(a_) __a : List[str] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa) __a : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa) __a : Tuple = np.full((n_batch, 2, input_len) , fill_value=-1_00 , dtype=np.intaa) __a : Optional[Any] = np.zeros((n_batch,) , dtype=np.intaa) for ( i, (story, conta, conta, mc_label), ) in enumerate(a_): __a : str = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __a : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __a : Tuple = with_conta __a : int = with_conta __a : List[str] = len(a_) - 1 __a : int = len(a_) - 1 __a : Optional[int] = with_conta __a : Tuple = with_conta __a : List[Any] = mc_label __a : Any = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(a_) for t in all_inputs)) return tensor_datasets def __A ( ) -> Union[str, Any]: __a : List[str] = argparse.ArgumentParser() parser.add_argument('''--model_name''' , type=a_ , default='''openai-gpt''' , help='''pretrained model name''') parser.add_argument('''--do_train''' , action='''store_true''' , help='''Whether to run training.''') parser.add_argument('''--do_eval''' , action='''store_true''' , help='''Whether to run eval on the dev set.''') parser.add_argument( '''--output_dir''' , default=a_ , type=a_ , required=a_ , help='''The output directory where the model predictions and checkpoints will be written.''' , ) parser.add_argument('''--train_dataset''' , type=a_ , default='''''') parser.add_argument('''--eval_dataset''' , type=a_ , default='''''') parser.add_argument('''--seed''' , type=a_ , default=42) parser.add_argument('''--num_train_epochs''' , type=a_ , default=3) parser.add_argument('''--train_batch_size''' , type=a_ , default=8) parser.add_argument('''--eval_batch_size''' , type=a_ , default=16) parser.add_argument('''--adam_epsilon''' , default=1e-8 , type=a_ , help='''Epsilon for Adam optimizer.''') parser.add_argument('''--max_grad_norm''' , type=a_ , default=1) parser.add_argument( '''--max_steps''' , default=-1 , type=a_ , help=( '''If > 0: set total number of training steps to perform. Override num_train_epochs.''' ) , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=a_ , default=1 , help='''Number of updates steps to accumulate before performing a backward/update pass.''' , ) parser.add_argument('''--learning_rate''' , type=a_ , default=6.25e-5) parser.add_argument('''--warmup_steps''' , default=0 , type=a_ , help='''Linear warmup over warmup_steps.''') parser.add_argument('''--lr_schedule''' , type=a_ , default='''warmup_linear''') parser.add_argument('''--weight_decay''' , type=a_ , default=0.0_1) parser.add_argument('''--lm_coef''' , type=a_ , default=0.9) parser.add_argument('''--n_valid''' , type=a_ , default=3_74) parser.add_argument('''--server_ip''' , type=a_ , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=a_ , default='''''' , help='''Can be used for distant debugging.''') __a : str = parser.parse_args() print(a_) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=a_) ptvsd.wait_for_attach() random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) __a : Tuple = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''') __a : str = torch.cuda.device_count() logger.info('''device: {}, n_gpu {}'''.format(a_ , a_)) if not args.do_train and not args.do_eval: raise ValueError('''At least one of `do_train` or `do_eval` must be True.''') if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset __a : List[str] = ['''_start_''', '''_delimiter_''', '''_classify_'''] __a : Union[str, Any] = OpenAIGPTTokenizer.from_pretrained(args.model_name) tokenizer.add_tokens(a_) __a : Union[str, Any] = tokenizer.convert_tokens_to_ids(a_) __a : Optional[Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name) model.resize_token_embeddings(len(a_)) model.to(a_) # Load and encode the datasets def tokenize_and_encode(a_ :List[Any]): if isinstance(a_ , a_): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(a_)) elif isinstance(a_ , a_): return obj return [tokenize_and_encode(a_) for o in obj] logger.info('''Encoding dataset...''') __a : Dict = load_rocstories_dataset(args.train_dataset) __a : int = load_rocstories_dataset(args.eval_dataset) __a : Optional[int] = (train_dataset, eval_dataset) __a : List[Any] = tokenize_and_encode(a_) # Compute the max input length for the Transformer __a : List[Any] = model.config.n_positions // 2 - 2 __a : int = max( len(story[:max_length]) + max(len(conta[:max_length]) , len(conta[:max_length])) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset) __a : Union[str, Any] = min(a_ , model.config.n_positions) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders __a : Tuple = pre_process_datasets(a_ , a_ , a_ , *a_) __a , __a : Tuple = tensor_datasets[0], tensor_datasets[1] __a : List[str] = TensorDataset(*a_) __a : Optional[Any] = RandomSampler(a_) __a : str = DataLoader(a_ , sampler=a_ , batch_size=args.train_batch_size) __a : List[str] = TensorDataset(*a_) __a : Optional[int] = SequentialSampler(a_) __a : Optional[Any] = DataLoader(a_ , sampler=a_ , batch_size=args.eval_batch_size) # Prepare optimizer if args.do_train: if args.max_steps > 0: __a : int = args.max_steps __a : Optional[int] = args.max_steps // (len(a_) // args.gradient_accumulation_steps) + 1 else: __a : str = len(a_) // args.gradient_accumulation_steps * args.num_train_epochs __a : List[Any] = list(model.named_parameters()) __a : Optional[int] = ['''bias''', '''LayerNorm.bias''', '''LayerNorm.weight'''] __a : List[str] = [ { '''params''': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], '''weight_decay''': args.weight_decay, }, {'''params''': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], '''weight_decay''': 0.0}, ] __a : int = AdamW(a_ , lr=args.learning_rate , eps=args.adam_epsilon) __a : Union[str, Any] = get_linear_schedule_with_warmup( a_ , num_warmup_steps=args.warmup_steps , num_training_steps=a_) if args.do_train: __a , __a , __a : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs) , desc='''Epoch'''): __a : Dict = 0 __a : Dict = 0 __a : List[str] = tqdm(a_ , desc='''Training''') for step, batch in enumerate(a_): __a : Dict = tuple(t.to(a_) for t in batch) __a , __a , __a , __a : str = batch __a : List[Any] = model(a_ , mc_token_ids=a_ , lm_labels=a_ , mc_labels=a_) __a : Optional[Any] = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() __a : int = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 __a : Tuple = '''Training loss: {:.2e} lr: {:.2e}'''.format(a_ , scheduler.get_lr()[0]) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer __a : Dict = model.module if hasattr(a_ , '''module''') else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` __a : int = os.path.join(args.output_dir , a_) __a : str = os.path.join(args.output_dir , a_) torch.save(model_to_save.state_dict() , a_) model_to_save.config.to_json_file(a_) tokenizer.save_vocabulary(args.output_dir) # Load a trained model and vocabulary that you have fine-tuned __a : str = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir) __a : Union[str, Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir) model.to(a_) if args.do_eval: model.eval() __a , __a : List[Any] = 0, 0 __a , __a : Union[str, Any] = 0, 0 for batch in tqdm(a_ , desc='''Evaluating'''): __a : str = tuple(t.to(a_) for t in batch) __a , __a , __a , __a : List[Any] = batch with torch.no_grad(): __a , __a , __a , __a : str = model( a_ , mc_token_ids=a_ , lm_labels=a_ , mc_labels=a_) __a : List[str] = mc_logits.detach().cpu().numpy() __a : Optional[Any] = mc_labels.to('''cpu''').numpy() __a : str = accuracy(a_ , a_) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0) nb_eval_steps += 1 __a : Tuple = eval_loss / nb_eval_steps __a : List[str] = eval_accuracy / nb_eval_examples __a : List[Any] = tr_loss / nb_tr_steps if args.do_train else None __a : List[str] = {'''eval_loss''': eval_loss, '''eval_accuracy''': eval_accuracy, '''train_loss''': train_loss} __a : Dict = os.path.join(args.output_dir , '''eval_results.txt''') with open(a_ , '''w''') as writer: logger.info('''***** Eval results *****''') for key in sorted(result.keys()): logger.info(''' %s = %s''' , a_ , str(result[key])) writer.write('''%s = %s\n''' % (key, str(result[key]))) if __name__ == "__main__": main()
52
0
import argparse import math import os import torch from neural_compressor.utils.pytorch import load from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, StableDiffusionPipeline, UNetaDConditionModel def SCREAMING_SNAKE_CASE__ ( ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] =argparse.ArgumentParser() parser.add_argument( '-m' ,'--pretrained_model_name_or_path' ,type=a_ ,default=a_ ,required=a_ ,help='Path to pretrained model or model identifier from huggingface.co/models.' ,) parser.add_argument( '-c' ,'--caption' ,type=a_ ,default='robotic cat with wings' ,help='Text used to generate images.' ,) parser.add_argument( '-n' ,'--images_num' ,type=a_ ,default=4 ,help='How much images to generate.' ,) parser.add_argument( '-s' ,'--seed' ,type=a_ ,default=42 ,help='Seed for random process.' ,) parser.add_argument( '-ci' ,'--cuda_id' ,type=a_ ,default=0 ,help='cuda_id.' ,) SCREAMING_SNAKE_CASE_ : Any =parser.parse_args() return args def SCREAMING_SNAKE_CASE__ ( lowerCAmelCase_ : List[str] ,lowerCAmelCase_ : Dict ,lowerCAmelCase_ : int ) -> List[Any]: """simple docstring""" if not len(a_ ) == rows * cols: raise ValueError('The specified number of rows and columns are not correct.' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] =imgs[0].size SCREAMING_SNAKE_CASE_ : List[str] =Image.new('RGB' ,size=(cols * w, rows * h) ) SCREAMING_SNAKE_CASE_ : Optional[int] =grid.size for i, img in enumerate(a_ ): grid.paste(a_ ,box=(i % cols * w, i // cols * h) ) return grid def SCREAMING_SNAKE_CASE__ ( lowerCAmelCase_ : int ,lowerCAmelCase_ : Dict="robotic cat with wings" ,lowerCAmelCase_ : Any=7.5 ,lowerCAmelCase_ : Optional[Any]=50 ,lowerCAmelCase_ : Optional[int]=1 ,lowerCAmelCase_ : int=42 ,) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any =torch.Generator(pipeline.device ).manual_seed(a_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] =pipeline( a_ ,guidance_scale=a_ ,num_inference_steps=a_ ,generator=a_ ,num_images_per_prompt=a_ ,).images SCREAMING_SNAKE_CASE_ : Dict =int(math.sqrt(a_ ) ) SCREAMING_SNAKE_CASE_ : Dict =image_grid(a_ ,rows=_rows ,cols=num_images_per_prompt // _rows ) return grid, images __SCREAMING_SNAKE_CASE = parse_args() # Load models and create wrapper for stable diffusion __SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder='tokenizer') __SCREAMING_SNAKE_CASE = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='text_encoder') __SCREAMING_SNAKE_CASE = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder='vae') __SCREAMING_SNAKE_CASE = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='unet') __SCREAMING_SNAKE_CASE = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) __SCREAMING_SNAKE_CASE = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, 'best_model.pt')): __SCREAMING_SNAKE_CASE = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, 'unet', unet) else: __SCREAMING_SNAKE_CASE = unet.to(torch.device('cuda', args.cuda_id)) __SCREAMING_SNAKE_CASE = pipeline.to(unet.device) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = generate_images(pipeline, prompt=args.caption, num_images_per_prompt=args.images_num, seed=args.seed) grid.save(os.path.join(args.pretrained_model_name_or_path, '{}.png'.format('_'.join(args.caption.split())))) __SCREAMING_SNAKE_CASE = os.path.join(args.pretrained_model_name_or_path, '_'.join(args.caption.split())) os.makedirs(dirname, exist_ok=True) for idx, image in enumerate(images): image.save(os.path.join(dirname, '{}.png'.format(idx + 1)))
220
"""simple docstring""" import unittest import numpy as np from transformers import RobertaConfig, 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.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=13 , _UpperCAmelCase=7 , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=99 , _UpperCAmelCase=32 , _UpperCAmelCase=5 , _UpperCAmelCase=4 , _UpperCAmelCase=37 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=16 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=4 , ): __a : Any = parent __a : Optional[int] = batch_size __a : str = seq_length __a : List[str] = is_training __a : Optional[Any] = use_attention_mask __a : Optional[Any] = use_token_type_ids __a : List[str] = use_labels __a : Union[str, Any] = vocab_size __a : int = hidden_size __a : Union[str, Any] = num_hidden_layers __a : Union[str, Any] = num_attention_heads __a : Dict = intermediate_size __a : List[str] = hidden_act __a : Dict = hidden_dropout_prob __a : Union[str, Any] = attention_probs_dropout_prob __a : int = max_position_embeddings __a : Tuple = type_vocab_size __a : Optional[int] = type_sequence_label_size __a : Optional[Any] = initializer_range __a : Optional[int] = num_choices def _lowerCamelCase ( self ): __a : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a : Union[str, Any] = None if self.use_attention_mask: __a : Any = random_attention_mask([self.batch_size, self.seq_length] ) __a : Optional[int] = None if self.use_token_type_ids: __a : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a : Any = RobertaConfig( 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 _lowerCamelCase ( self ): __a : Dict = self.prepare_config_and_inputs() __a , __a , __a , __a : str = config_and_inputs __a : str = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def _lowerCamelCase ( self ): __a : Any = self.prepare_config_and_inputs() __a , __a , __a , __a : Union[str, Any] = config_and_inputs __a : Optional[int] = True __a : str = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = True __lowerCAmelCase = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCamelCase ( self ): __a : Dict = FlaxRobertaModelTester(self ) @slow def _lowerCamelCase ( self ): for model_class_name in self.all_model_classes: __a : int = model_class_name.from_pretrained('''roberta-base''' , from_pt=_UpperCAmelCase ) __a : List[str] = model(np.ones((1, 1) ) ) self.assertIsNotNone(_UpperCAmelCase )
52
0
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer UpperCAmelCase_ : int = '''bart''' UpperCAmelCase_ : Any = True @st.cache(allow_output_mutation=a_ ) def __SCREAMING_SNAKE_CASE ( ) -> Dict: if LOAD_DENSE_INDEX: __A : Optional[Any] = AutoTokenizer.from_pretrained("""yjernite/retribert-base-uncased""" ) __A : Dict = AutoModel.from_pretrained("""yjernite/retribert-base-uncased""" ).to("""cuda:0""" ) __A : Union[str, Any] = qar_model.eval() else: __A : Optional[int] = (None, None) if MODEL_TYPE == "bart": __A : List[Any] = AutoTokenizer.from_pretrained("""yjernite/bart_eli5""" ) __A : Tuple = AutoModelForSeqaSeqLM.from_pretrained("""yjernite/bart_eli5""" ).to("""cuda:0""" ) __A : Optional[int] = torch.load("""seq2seq_models/eli5_bart_model_blm_2.pth""" ) sas_model.load_state_dict(save_dict["""model"""] ) __A : List[Any] = sas_model.eval() else: __A : List[str] = make_qa_sas_model( model_name="""t5-small""" ,from_file="""seq2seq_models/eli5_t5_model_1024_4.pth""" ,device="""cuda:0""" ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=a_ ) def __SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: if LOAD_DENSE_INDEX: __A : str = faiss.StandardGpuResources() __A : Tuple = datasets.load_dataset(path="""wiki_snippets""" ,name="""wiki40b_en_100_0""" )['''train'''] __A : Dict = np.memmap( """wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat""" ,dtype="""float32""" ,mode="""r""" ,shape=(wikiaab_passages.num_rows, 128) ,) __A : Dict = faiss.IndexFlatIP(128 ) __A : Optional[int] = faiss.index_cpu_to_gpu(a_ ,1 ,a_ ) wikiaab_gpu_index_flat.add(a_ ) # TODO fix for larger GPU else: __A : List[str] = (None, None) __A : int = Elasticsearch([{"""host""": """localhost""", """port""": """9200"""}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=a_ ) def __SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: __A : Union[str, Any] = datasets.load_dataset("""eli5""" ,name="""LFQA_reddit""" ) __A : List[str] = elia['''train_eli5'''] __A : Tuple = np.memmap( """eli5_questions_reps.dat""" ,dtype="""float32""" ,mode="""r""" ,shape=(elia_train.num_rows, 128) ) __A : Any = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(a_ ) return (elia_train, eli5_train_q_index) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = load_indexes() UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[str] = load_models() UpperCAmelCase_ , UpperCAmelCase_ : str = load_train_data() def __SCREAMING_SNAKE_CASE ( a__ : str ,a__ : str=10 ) -> Dict: __A : List[Any] = embed_questions_for_retrieval([question] ,a_ ,a_ ) __A : Tuple = eli5_train_q_index.search(a_ ,a_ ) __A : Union[str, Any] = [elia_train[int(a_ )] for i in I[0]] return nn_examples def __SCREAMING_SNAKE_CASE ( a__ : List[Any] ,a__ : int="wiki40b" ,a__ : Any="dense" ,a__ : Dict=10 ) -> Any: if source == "none": __A : Any = (''' <P> '''.join(["""""" for _ in range(11 )] ).strip(), []) else: if method == "dense": __A : Optional[Any] = query_qa_dense_index( a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) else: __A : int = query_es_index( a_ ,a_ ,index_name="""english_wiki40b_snippets_100w""" ,n_results=a_ ,) __A : Any = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] __A : Any = '''question: {} context: {}'''.format(a_ ,a_ ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda a__ : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda a__ : None), } ) def __SCREAMING_SNAKE_CASE ( a__ : Tuple ,a__ : Any ,a__ : Tuple ,a__ : List[Any]=64 ,a__ : int=256 ,a__ : Any=False ,a__ : Dict=2 ,a__ : Dict=0.95 ,a__ : List[Any]=0.8 ) -> List[Any]: with torch.no_grad(): __A : str = qa_sas_generate( a_ ,a_ ,a_ ,num_answers=1 ,num_beams=a_ ,min_len=a_ ,max_len=a_ ,do_sample=a_ ,temp=a_ ,top_p=a_ ,top_k=a_ ,max_input_length=1024 ,device="""cuda:0""" ,)[0] return (answer, support_list) st.title('''Long Form Question Answering with ELI5''') # Start sidebar UpperCAmelCase_ : Tuple = '''<img src=\'https://huggingface.co/front/assets/huggingface_logo.svg\'>''' UpperCAmelCase_ : str = ''' <html> <head> <style> .img-container { padding-left: 90px; padding-right: 90px; padding-top: 50px; padding-bottom: 50px; background-color: #f0f3f9; } </style> </head> <body> <span class="img-container"> <!-- Inline parent element --> %s </span> </body> </html> ''' % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia UpperCAmelCase_ : List[str] = ''' This demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html). First, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset, a pre-processed fixed snapshot of Wikipedia. ''' st.sidebar.markdown(description, unsafe_allow_html=True) UpperCAmelCase_ : Tuple = [ '''Answer the question''', '''View the retrieved document only''', '''View the most similar ELI5 question and answer''', '''Show me everything, please!''', ] UpperCAmelCase_ : List[str] = st.sidebar.checkbox('''Demo options''') if demo_options: UpperCAmelCase_ : Dict = st.sidebar.selectbox( '''''', action_list, index=3, ) UpperCAmelCase_ : Any = action_list.index(action_st) UpperCAmelCase_ : str = st.sidebar.selectbox( '''''', ['''Show full text of passages''', '''Show passage section titles'''], index=0, ) UpperCAmelCase_ : Optional[Any] = show_type == '''Show full text of passages''' else: UpperCAmelCase_ : Any = 3 UpperCAmelCase_ : int = True UpperCAmelCase_ : List[str] = st.sidebar.checkbox('''Retrieval options''') if retrieval_options: UpperCAmelCase_ : Union[str, Any] = ''' ### Information retriever options The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs. The answer is then generated by sequence to sequence model which takes the question and retrieved document as input. ''' st.sidebar.markdown(retriever_info) UpperCAmelCase_ : Dict = st.sidebar.selectbox('''Which Wikipedia format should the model use?''', ['''wiki40b''', '''none''']) UpperCAmelCase_ : Optional[int] = st.sidebar.selectbox('''Which Wikipedia indexer should the model use?''', ['''dense''', '''sparse''', '''mixed''']) else: UpperCAmelCase_ : Union[str, Any] = '''wiki40b''' UpperCAmelCase_ : Optional[Any] = '''dense''' UpperCAmelCase_ : Any = '''beam''' UpperCAmelCase_ : List[str] = 2 UpperCAmelCase_ : str = 64 UpperCAmelCase_ : Union[str, Any] = 256 UpperCAmelCase_ : List[str] = None UpperCAmelCase_ : str = None UpperCAmelCase_ : Optional[int] = st.sidebar.checkbox('''Generation options''') if generate_options: UpperCAmelCase_ : Union[str, Any] = ''' ### Answer generation options The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large) weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with **beam** search, or **sample** from the decoder\'s output probabilities. ''' st.sidebar.markdown(generate_info) UpperCAmelCase_ : Any = st.sidebar.selectbox('''Would you like to use beam search or sample an answer?''', ['''beam''', '''sampled''']) UpperCAmelCase_ : Tuple = st.sidebar.slider( '''Minimum generation length''', min_value=8, max_value=256, value=64, step=8, format=None, key=None ) UpperCAmelCase_ : Tuple = st.sidebar.slider( '''Maximum generation length''', min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": UpperCAmelCase_ : List[Any] = st.sidebar.slider('''Beam size''', min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: UpperCAmelCase_ : List[str] = st.sidebar.slider( '''Nucleus sampling p''', min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) UpperCAmelCase_ : Optional[Any] = st.sidebar.slider( '''Temperature''', min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) UpperCAmelCase_ : Dict = None # start main text UpperCAmelCase_ : Optional[int] = [ '''<MY QUESTION>''', '''How do people make chocolate?''', '''Why do we get a fever when we are sick?''', '''How can different animals perceive different colors?''', '''What is natural language processing?''', '''What\'s the best way to treat a sunburn?''', '''What exactly are vitamins ?''', '''How does nuclear energy provide electricity?''', '''What\'s the difference between viruses and bacteria?''', '''Why are flutes classified as woodwinds when most of them are made out of metal ?''', '''Why do people like drinking coffee even though it tastes so bad?''', '''What happens when wine ages? How does it make the wine taste better?''', '''If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?''', '''How can we set a date to the beginning or end of an artistic period? Doesn\'t the change happen gradually?''', '''How does New Zealand have so many large bird predators?''', ] UpperCAmelCase_ : int = st.selectbox( '''What would you like to ask? ---- select <MY QUESTION> to enter a new query''', questions_list, index=1, ) if question_s == "<MY QUESTION>": UpperCAmelCase_ : Union[str, Any] = st.text_input('''Enter your question here:''', '''''') else: UpperCAmelCase_ : List[Any] = question_s if st.button('''Show me!'''): if action in [0, 1, 3]: if index_type == "mixed": UpperCAmelCase_ , UpperCAmelCase_ : Any = make_support(question, source=wiki_source, method='''dense''', n_results=10) UpperCAmelCase_ , UpperCAmelCase_ : Any = make_support(question, source=wiki_source, method='''sparse''', n_results=10) UpperCAmelCase_ : Optional[Any] = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] UpperCAmelCase_ : List[Any] = support_list[:10] UpperCAmelCase_ : Any = '''<P> ''' + ''' <P> '''.join([res[-1] for res in support_list]) else: UpperCAmelCase_ , UpperCAmelCase_ : Any = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == '''sampled'''), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown('''### The model generated answer is:''') st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown('''--- \n ### The model is drawing information from the following Wikipedia passages:''') for i, res in enumerate(support_list): UpperCAmelCase_ : int = '''https://en.wikipedia.org/wiki/{}'''.format(res[0].replace(''' ''', '''_''')) UpperCAmelCase_ : Optional[int] = res[1].strip() if sec_titles == "": UpperCAmelCase_ : Tuple = '''[{}]({})'''.format(res[0], wiki_url) else: UpperCAmelCase_ : Tuple = sec_titles.split(''' & ''') UpperCAmelCase_ : str = ''' & '''.join( ['''[{}]({}#{})'''.format(sec.strip(), wiki_url, sec.strip().replace(''' ''', '''_''')) for sec in sec_list] ) st.markdown( '''{0:02d} - **Article**: {1:<18} <br> _Section_: {2}'''.format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( '''> <span style="font-family:arial; font-size:10pt;">''' + res[-1] + '''</span>''', unsafe_allow_html=True ) if action in [2, 3]: UpperCAmelCase_ : Optional[int] = find_nearest_training(question) UpperCAmelCase_ : Optional[int] = nn_train_list[0] st.markdown( '''--- \n ### The most similar question in the ELI5 training set was: \n\n {}'''.format(train_exple['''title''']) ) UpperCAmelCase_ : Tuple = [ '''{}. {}'''.format(i + 1, ''' \n'''.join([line.strip() for line in ans.split('''\n''') if line.strip() != ''''''])) for i, (ans, sc) in enumerate(zip(train_exple['''answers''']['''text'''], train_exple['''answers''']['''score'''])) if i == 0 or sc > 2 ] st.markdown('''##### Its answers were: \n\n {}'''.format('''\n'''.join(answers_st))) UpperCAmelCase_ : Optional[Any] = ''' --- **Disclaimer** *The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system. Evaluating biases of such a model and ensuring factual generations are still very much open research problems. Therefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.* ''' st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
17
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''facebook/levit-128S''': '''https://huggingface.co/facebook/levit-128S/resolve/main/config.json''', # See all LeViT models at https://huggingface.co/models?filter=levit } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''levit''' def __init__( self , _UpperCAmelCase=224 , _UpperCAmelCase=3 , _UpperCAmelCase=3 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=16 , _UpperCAmelCase=[128, 256, 384] , _UpperCAmelCase=[4, 8, 12] , _UpperCAmelCase=[4, 4, 4] , _UpperCAmelCase=[16, 16, 16] , _UpperCAmelCase=0 , _UpperCAmelCase=[2, 2, 2] , _UpperCAmelCase=[2, 2, 2] , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = image_size __a : List[Any] = num_channels __a : Dict = kernel_size __a : Optional[int] = stride __a : Optional[int] = padding __a : Dict = hidden_sizes __a : int = num_attention_heads __a : Optional[int] = depths __a : str = key_dim __a : Union[str, Any] = drop_path_rate __a : Optional[Any] = patch_size __a : Tuple = attention_ratio __a : int = mlp_ratio __a : int = initializer_range __a : int = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = version.parse('''1.11''' ) @property def _lowerCamelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowerCamelCase ( self ): return 1e-4
52
0
import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel A__ : List[Any] = { """text_branch""": """text_model""", """audio_branch""": """audio_model.audio_encoder""", """attn""": """attention.self""", """self.proj""": """output.dense""", """attention.self_mask""": """attn_mask""", """mlp.fc1""": """intermediate.dense""", """mlp.fc2""": """output.dense""", """norm1""": """layernorm_before""", """norm2""": """layernorm_after""", """bn0""": """batch_norm""", } A__ : Optional[int] = AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""") def _a ( __UpperCamelCase : Any ,__UpperCamelCase : Union[str, Any]=False ): lowerCAmelCase__ : str = create_model( '''HTSAT-tiny''' ,'''roberta''' ,a_ ,precision='''fp32''' ,device='''cuda:0''' if torch.cuda.is_available() else '''cpu''' ,enable_fusion=a_ ,fusion_type='''aff_2d''' if enable_fusion else None ,) return model, model_cfg def _a ( __UpperCamelCase : Union[str, Any] ): lowerCAmelCase__ : str = {} lowerCAmelCase__ : str = R'''.*sequential.(\d+).*''' lowerCAmelCase__ : Optional[int] = R'''.*_projection.(\d+).*''' for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: lowerCAmelCase__ : List[Any] = key.replace(a_ ,a_ ) if re.match(a_ ,a_ ): # replace sequential layers with list lowerCAmelCase__ : str = re.match(a_ ,a_ ).group(1 ) lowerCAmelCase__ : Union[str, Any] = key.replace(f'''sequential.{sequential_layer}.''' ,f'''layers.{int(a_ )//3}.linear.''' ) elif re.match(a_ ,a_ ): lowerCAmelCase__ : Tuple = int(re.match(a_ ,a_ ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... lowerCAmelCase__ : Tuple = 1 if projecton_layer == 0 else 2 lowerCAmelCase__ : Optional[Any] = key.replace(f'''_projection.{projecton_layer}.''' ,f'''_projection.linear{transformers_projection_layer}.''' ) if "audio" and "qkv" in key: # split qkv into query key and value lowerCAmelCase__ : Optional[Any] = value lowerCAmelCase__ : Tuple = mixed_qkv.size(0 ) // 3 lowerCAmelCase__ : List[str] = mixed_qkv[:qkv_dim] lowerCAmelCase__ : Optional[int] = mixed_qkv[qkv_dim : qkv_dim * 2] lowerCAmelCase__ : Tuple = mixed_qkv[qkv_dim * 2 :] lowerCAmelCase__ : Union[str, Any] = query_layer lowerCAmelCase__ : int = key_layer lowerCAmelCase__ : Tuple = value_layer else: lowerCAmelCase__ : str = value return model_state_dict def _a ( __UpperCamelCase : str ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Optional[Any]=False ): lowerCAmelCase__ : List[str] = init_clap(a_ ,enable_fusion=a_ ) clap_model.eval() lowerCAmelCase__ : Optional[Any] = clap_model.state_dict() lowerCAmelCase__ : List[str] = rename_state_dict(a_ ) lowerCAmelCase__ : Optional[int] = ClapConfig() lowerCAmelCase__ : int = enable_fusion lowerCAmelCase__ : Optional[int] = ClapModel(a_ ) # ignore the spectrogram embedding layer model.load_state_dict(a_ ,strict=a_ ) model.save_pretrained(a_ ) transformers_config.save_pretrained(a_ ) if __name__ == "__main__": A__ : Any = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""") A__ : Any = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
233
"""simple docstring""" def __A ( a_ :Tuple , a_ :Union[str, Any] , a_ :int=False) -> List[str]: if isinstance(a_ , a_) and isinstance(a_ , a_): __a : List[str] = len(set_a.intersection(a_)) if alternative_union: __a : List[str] = len(a_) + len(a_) else: __a : int = len(set_a.union(a_)) return intersection / union if isinstance(a_ , (list, tuple)) and isinstance(a_ , (list, tuple)): __a : Union[str, Any] = [element for element in set_a if element in set_b] if alternative_union: __a : Union[str, Any] = len(a_) + len(a_) return len(a_) / union else: __a : List[Any] = set_a + [element for element in set_b if element not in set_a] return len(a_) / len(a_) return len(a_) / len(a_) return None if __name__ == "__main__": A = {'''a''', '''b''', '''c''', '''d''', '''e'''} A = {'''c''', '''d''', '''e''', '''f''', '''h''', '''i'''} print(jaccard_similarity(set_a, set_b))
52
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case_ : Optional[int] = logging.get_logger(__name__) snake_case_ : Union[str, Any] = { """transfo-xl-wt103""": """https://huggingface.co/transfo-xl-wt103/resolve/main/config.json""", } class snake_case__ ( _UpperCamelCase ): SCREAMING_SNAKE_CASE__ = '''transfo-xl''' SCREAMING_SNAKE_CASE__ = ['''mems'''] SCREAMING_SNAKE_CASE__ = { '''n_token''': '''vocab_size''', '''hidden_size''': '''d_model''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : int , lowercase : Dict=26_77_35 , lowercase : Optional[int]=[2_00_00, 4_00_00, 20_00_00] , lowercase : int=10_24 , lowercase : List[str]=10_24 , lowercase : Union[str, Any]=16 , lowercase : Optional[int]=64 , lowercase : Tuple=40_96 , lowercase : List[Any]=4 , lowercase : Optional[int]=False , lowercase : Optional[int]=18 , lowercase : List[Any]=16_00 , lowercase : Union[str, Any]=10_00 , lowercase : List[str]=True , lowercase : int=True , lowercase : Optional[int]=0 , lowercase : Union[str, Any]=-1 , lowercase : Optional[Any]=True , lowercase : Optional[int]=0.1 , lowercase : Any=0.0 , lowercase : List[Any]=True , lowercase : List[str]="normal" , lowercase : Tuple=0.0_1 , lowercase : Optional[Any]=0.0_1 , lowercase : Tuple=0.0_2 , lowercase : Optional[Any]=1E-5 , lowercase : Union[str, Any]=0 , **lowercase : List[str] , ): '''simple docstring''' UpperCAmelCase : str = vocab_size UpperCAmelCase : int = [] self.cutoffs.extend(_UpperCAmelCase ) if proj_share_all_but_first: UpperCAmelCase : List[Any] = [False] + [True] * len(self.cutoffs ) else: UpperCAmelCase : List[Any] = [False] + [False] * len(self.cutoffs ) UpperCAmelCase : Tuple = d_model UpperCAmelCase : str = d_embed UpperCAmelCase : Tuple = d_head UpperCAmelCase : int = d_inner UpperCAmelCase : str = div_val UpperCAmelCase : str = pre_lnorm UpperCAmelCase : Optional[Any] = n_layer UpperCAmelCase : Union[str, Any] = n_head UpperCAmelCase : Dict = mem_len UpperCAmelCase : Optional[Any] = same_length UpperCAmelCase : Any = attn_type UpperCAmelCase : Any = clamp_len UpperCAmelCase : List[str] = sample_softmax UpperCAmelCase : List[Any] = adaptive UpperCAmelCase : List[str] = dropout UpperCAmelCase : str = dropatt UpperCAmelCase : Any = untie_r UpperCAmelCase : Optional[int] = init UpperCAmelCase : Tuple = init_range UpperCAmelCase : List[Any] = proj_init_std UpperCAmelCase : Tuple = init_std UpperCAmelCase : Tuple = layer_norm_epsilon super().__init__(eos_token_id=_UpperCAmelCase , **_UpperCAmelCase ) @property def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' logger.info(f"""The model {self.model_type} is one of the few models that has no sequence length limit.""" ) return -1 @max_position_embeddings.setter def __lowerCAmelCase ( self : Tuple , lowercase : Tuple ): '''simple docstring''' raise NotImplementedError( f"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
595
"""simple docstring""" from __future__ import annotations import os from collections.abc import Mapping A = tuple[int, int] class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a : set[int] = vertices __a : dict[EdgeT, int] = { (min(_UpperCAmelCase ), max(_UpperCAmelCase )): weight for edge, weight in edges.items() } def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase ): self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) __a : Dict = weight def _lowerCamelCase ( self ): __a : Graph = Graph({min(self.vertices )} , {} ) __a : EdgeT __a : int __a : EdgeT __a : int while len(subgraph.vertices ) < len(self.vertices ): __a : Any = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: __a : List[str] = edge __a : Optional[int] = weight subgraph.add_edge(_UpperCAmelCase , _UpperCAmelCase ) return subgraph def __A ( a_ :str = "p107_network.txt") -> int: __a : str = os.path.abspath(os.path.dirname(a_)) __a : str = os.path.join(a_ , a_) __a : dict[EdgeT, int] = {} __a : list[str] __a : int __a : int with open(a_) as f: __a : Optional[int] = f.read().strip().split('''\n''') __a : Dict = [line.split(''',''') for line in data] for edgea in range(1 , len(a_)): for edgea in range(a_): if adjaceny_matrix[edgea][edgea] != "-": __a : Tuple = int(adjaceny_matrix[edgea][edgea]) __a : Graph = Graph(set(range(len(a_))) , a_) __a : Graph = graph.prims_algorithm() __a : int = sum(graph.edges.values()) __a : int = sum(subgraph.edges.values()) return initial_total - optimal_total if __name__ == "__main__": print(F'{solution() = }')
52
0
'''simple docstring''' def __A ( a_ : List[Any] ): lowerCAmelCase : int = [], [] while len(a_ ) > 1: lowerCAmelCase : Any = min(a_ ), max(a_ ) start.append(a_ ) end.append(a_ ) collection.remove(a_ ) collection.remove(a_ ) end.reverse() return start + collection + end if __name__ == "__main__": lowerCAmelCase = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase = [int(item) for item in user_input.split(""",""")] print(*merge_sort(unsorted), sep=""",""")
525
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''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 __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''trocr''' __lowerCAmelCase = ['''past_key_values'''] __lowerCAmelCase = { '''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model''', '''num_hidden_layers''': '''decoder_layers''', } def __init__( self , _UpperCAmelCase=50265 , _UpperCAmelCase=1024 , _UpperCAmelCase=12 , _UpperCAmelCase=16 , _UpperCAmelCase=4096 , _UpperCAmelCase="gelu" , _UpperCAmelCase=512 , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.0 , _UpperCAmelCase=0.0 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=1 , _UpperCAmelCase=0 , _UpperCAmelCase=2 , **_UpperCAmelCase , ): __a : List[str] = vocab_size __a : Optional[Any] = d_model __a : Optional[Any] = decoder_layers __a : Union[str, Any] = decoder_attention_heads __a : int = decoder_ffn_dim __a : List[Any] = activation_function __a : Any = max_position_embeddings __a : Dict = dropout __a : List[Any] = attention_dropout __a : Optional[Any] = activation_dropout __a : str = init_std __a : List[str] = decoder_layerdrop __a : Union[str, Any] = use_cache __a : Optional[Any] = scale_embedding __a : List[Any] = use_learned_position_embeddings __a : Optional[int] = layernorm_embedding super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , decoder_start_token_id=_UpperCAmelCase , **_UpperCAmelCase , )
52
0
'''simple docstring''' import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process __snake_case : Optional[int] = logging.getLogger(__name__) def lowerCamelCase__ ( A_ , A_ ): return (preds == labels).mean() @dataclass class lowercase_ : a_ = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) a_ = field( default=_UpperCamelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) a_ = field( default=_UpperCamelCase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) a_ = field( default=_UpperCamelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) @dataclass class lowercase_ : a_ = field(metadata={"""help""": """The name of the task to train on: """ + """, """.join(processors.keys() )} ) a_ = field(metadata={"""help""": """Should contain the data files for the task."""} ) a_ = field( default=128 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) a_ = field( default=_UpperCamelCase , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) def lowerCamelCase__ ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCAmelCase_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) UpperCAmelCase_ = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" " --overwrite_output_dir to overcome." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("Training/evaluation parameters %s" , a_ ) # Set seed set_seed(training_args.seed ) try: UpperCAmelCase_ = processors[data_args.task_name]() UpperCAmelCase_ = processor.get_labels() UpperCAmelCase_ = len(a_ ) except KeyError: raise ValueError("Task not found: %s" % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCAmelCase_ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a_ , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) UpperCAmelCase_ = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , ) # Get datasets UpperCAmelCase_ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=a_ , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) UpperCAmelCase_ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=a_ , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(A_ ) -> Dict: UpperCAmelCase_ = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(a_ , p.label_ids )} # Data collator UpperCAmelCase_ = DataCollatorWithPadding(a_ , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer UpperCAmelCase_ = Trainer( model=a_ , args=a_ , train_dataset=a_ , eval_dataset=a_ , compute_metrics=a_ , data_collator=a_ , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation UpperCAmelCase_ = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) UpperCAmelCase_ = trainer.evaluate() UpperCAmelCase_ = os.path.join(training_args.output_dir , "eval_results.txt" ) if trainer.is_world_master(): with open(a_ , "w" ) as writer: logger.info("***** Eval results *****" ) for key, value in result.items(): logger.info(" %s = %s" , a_ , a_ ) writer.write("%s = %s\n" % (key, value) ) results.update(a_ ) return results def lowerCamelCase__ ( A_ ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
660
"""simple docstring""" import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def __A ( a_ :Union[str, Any] , a_ :Union[str, Any] , a_ :Optional[Any] , a_ :Optional[int]=5) -> List[Any]: # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count('''<mask>''') == 1 __a : Optional[Any] = torch.tensor(tokenizer.encode(a_ , add_special_tokens=a_)).unsqueeze(0) # Batch size 1 __a : Dict = model(a_)[0] # The last hidden-state is the first element of the output tuple __a : Tuple = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() __a : Any = logits[0, masked_index, :] __a : Any = logits.softmax(dim=0) __a , __a : Optional[Any] = prob.topk(k=a_ , dim=0) __a : Optional[int] = ''' '''.join( [tokenizer.convert_ids_to_tokens(indices[i].item()) for i in range(len(a_))]) __a : List[str] = tokenizer.mask_token __a : Optional[int] = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(''' ''')): __a : Optional[Any] = predicted_token_bpe.replace('''\u2581''' , ''' ''') if " {0}".format(a_) in masked_input: topk_filled_outputs.append( ( masked_input.replace(''' {0}'''.format(a_) , a_), values[index].item(), predicted_token, )) else: topk_filled_outputs.append( ( masked_input.replace(a_ , a_), values[index].item(), predicted_token, )) return topk_filled_outputs A = CamembertTokenizer.from_pretrained('''camembert-base''') A = CamembertForMaskedLM.from_pretrained('''camembert-base''') model.eval() A = '''Le camembert est <mask> :)''' print(fill_mask(masked_input, model, tokenizer, topk=3))
52
0
import re from ..models.auto import AutoProcessor from ..models.vision_encoder_decoder import VisionEncoderDecoderModel from ..utils import is_vision_available from .base import PipelineTool if is_vision_available(): from PIL import Image class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" __UpperCAmelCase : Any = '''naver-clova-ix/donut-base-finetuned-docvqa''' __UpperCAmelCase : Optional[Any] = ( '''This is a tool that answers a question about an document (pdf). It takes an input named `document` which ''' '''should be the document containing the information, as well as a `question` that is the question about the ''' '''document. It returns a text that contains the answer to the question.''' ) __UpperCAmelCase : Optional[Any] = '''document_qa''' __UpperCAmelCase : List[Any] = AutoProcessor __UpperCAmelCase : int = VisionEncoderDecoderModel __UpperCAmelCase : Dict = ['''image''', '''text'''] __UpperCAmelCase : Tuple = ['''text'''] def __init__( self , *a_ , **a_ ): if not is_vision_available(): raise ValueError("Pillow must be installed to use the DocumentQuestionAnsweringTool." ) super().__init__(*_UpperCAmelCase , **_UpperCAmelCase ) def _UpperCamelCase ( self , a_ , a_ ): lowerCamelCase_ : Dict = '''<s_docvqa><s_question>{user_input}</s_question><s_answer>''' lowerCamelCase_ : List[str] = task_prompt.replace("{user_input}" , _UpperCAmelCase ) lowerCamelCase_ : List[Any] = self.pre_processor.tokenizer( _UpperCAmelCase , add_special_tokens=_UpperCAmelCase , return_tensors="pt" ).input_ids lowerCamelCase_ : Union[str, Any] = self.pre_processor(_UpperCAmelCase , return_tensors="pt" ).pixel_values return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values} def _UpperCamelCase ( self , a_ ): return self.model.generate( inputs["pixel_values"].to(self.device ) , decoder_input_ids=inputs["decoder_input_ids"].to(self.device ) , max_length=self.model.decoder.config.max_position_embeddings , early_stopping=_UpperCAmelCase , pad_token_id=self.pre_processor.tokenizer.pad_token_id , eos_token_id=self.pre_processor.tokenizer.eos_token_id , use_cache=_UpperCAmelCase , num_beams=1 , bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]] , return_dict_in_generate=_UpperCAmelCase , ).sequences def _UpperCamelCase ( self , a_ ): lowerCamelCase_ : List[str] = self.pre_processor.batch_decode(_UpperCAmelCase )[0] lowerCamelCase_ : Union[str, Any] = sequence.replace(self.pre_processor.tokenizer.eos_token , "" ) lowerCamelCase_ : Any = sequence.replace(self.pre_processor.tokenizer.pad_token , "" ) lowerCamelCase_ : Union[str, Any] = re.sub(R"<.*?>" , "" , _UpperCAmelCase , count=1 ).strip() # remove first task start token lowerCamelCase_ : Optional[int] = self.pre_processor.tokenajson(_UpperCAmelCase ) return sequence["answer"]
250
"""simple docstring""" import unittest from knapsack import greedy_knapsack as kp class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): __a : Optional[int] = [10, 20, 30, 40, 50, 60] __a : Union[str, Any] = [2, 4, 6, 8, 10, 12] __a : List[str] = 100 self.assertEqual(kp.calc_profit(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) , 210 ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''max_weight must greater than zero.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''Weight can not be negative.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''Profit can not be negative.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''max_weight must greater than zero.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex( _UpperCAmelCase , '''The length of profit and weight must be same.''' ) if __name__ == "__main__": unittest.main()
52
0
import io import json import fsspec import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.json import JsonDatasetReader, JsonDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __lowerCAmelCase ( _A ,_A ): """simple docstring""" assert isinstance(a_ ,a_ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" ,[False, True] ) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" _lowercase = tmp_path / '''cache''' _lowercase = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _lowercase = JsonDatasetReader(a_ ,cache_dir=a_ ,keep_in_memory=a_ ).read() _check_json_dataset(a_ ,a_ ) @pytest.mark.parametrize( """features""" ,[ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] ,) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" _lowercase = tmp_path / '''cache''' _lowercase = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase = features.copy() if features else default_expected_features _lowercase = ( Features({feature: Value(a_ ) for feature, dtype in features.items()} ) if features is not None else None ) _lowercase = JsonDatasetReader(a_ ,features=a_ ,cache_dir=a_ ).read() _check_json_dataset(a_ ,a_ ) @pytest.mark.parametrize( """features""" ,[ None, {"""col_3""": """float64""", """col_1""": """string""", """col_2""": """int64"""}, ] ,) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" _lowercase = tmp_path / '''cache''' _lowercase = {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''} _lowercase = features.copy() if features else default_expected_features _lowercase = ( Features({feature: Value(a_ ) for feature, dtype in features.items()} ) if features is not None else None ) _lowercase = JsonDatasetReader(a_ ,features=a_ ,cache_dir=a_ ).read() assert isinstance(a_ ,a_ ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_3", "col_1", "col_2"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype def __lowerCAmelCase ( _A ,_A ): """simple docstring""" _lowercase = {'''col_2''': '''int64''', '''col_3''': '''float64''', '''col_1''': '''string'''} _lowercase = features.copy() _lowercase = ( Features({feature: Value(a_ ) for feature, dtype in features.items()} ) if features is not None else None ) _lowercase = tmp_path / '''cache''' _lowercase = JsonDatasetReader(a_ ,features=a_ ,cache_dir=a_ ).read() assert isinstance(a_ ,a_ ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_2", "col_3", "col_1"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""split""" ,[None, NamedSplit("""train""" ), """train""", """test"""] ) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" _lowercase = tmp_path / '''cache''' _lowercase = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase = JsonDatasetReader(a_ ,cache_dir=a_ ,split=a_ ).read() _check_json_dataset(a_ ,a_ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("""path_type""" ,[str, list] ) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" if issubclass(a_ ,a_ ): _lowercase = jsonl_path elif issubclass(a_ ,a_ ): _lowercase = [jsonl_path] _lowercase = tmp_path / '''cache''' _lowercase = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase = JsonDatasetReader(a_ ,cache_dir=a_ ).read() _check_json_dataset(a_ ,a_ ) def __lowerCAmelCase ( _A ,_A ,_A=("train",) ): """simple docstring""" assert isinstance(a_ ,a_ ) for split in splits: _lowercase = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" ,[False, True] ) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" _lowercase = tmp_path / '''cache''' _lowercase = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _lowercase = JsonDatasetReader({"""train""": jsonl_path} ,cache_dir=a_ ,keep_in_memory=a_ ).read() _check_json_datasetdict(a_ ,a_ ) @pytest.mark.parametrize( """features""" ,[ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] ,) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" _lowercase = tmp_path / '''cache''' _lowercase = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase = features.copy() if features else default_expected_features _lowercase = ( Features({feature: Value(a_ ) for feature, dtype in features.items()} ) if features is not None else None ) _lowercase = JsonDatasetReader({"""train""": jsonl_path} ,features=a_ ,cache_dir=a_ ).read() _check_json_datasetdict(a_ ,a_ ) @pytest.mark.parametrize("""split""" ,[None, NamedSplit("""train""" ), """train""", """test"""] ) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" if split: _lowercase = {split: jsonl_path} else: _lowercase = '''train''' _lowercase = {'''train''': jsonl_path, '''test''': jsonl_path} _lowercase = tmp_path / '''cache''' _lowercase = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} _lowercase = JsonDatasetReader(a_ ,cache_dir=a_ ).read() _check_json_datasetdict(a_ ,a_ ,splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __lowerCAmelCase ( _A ): """simple docstring""" return json.load(a_ ) def __lowerCAmelCase ( _A ): """simple docstring""" return [json.loads(a_ ) for line in buffer] class _lowercase : """simple docstring""" @pytest.mark.parametrize("""lines, load_json_function""" , [(True, load_json_lines), (False, load_json)] ) def _UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): '''simple docstring''' with io.BytesIO() as buffer: JsonDatasetWriter(_UpperCAmelCase , _UpperCAmelCase , lines=_UpperCAmelCase ).write() buffer.seek(0 ) _lowercase = load_json_function(_UpperCAmelCase ) assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) assert isinstance(exported_content[0] , _UpperCAmelCase ) assert len(_UpperCAmelCase ) == 10 @pytest.mark.parametrize( """orient, container, keys, len_at""" , [ ("""records""", list, {"""tokens""", """labels""", """answers""", """id"""}, None), ("""split""", dict, {"""columns""", """data"""}, """data"""), ("""index""", dict, set("""0123456789""" ), None), ("""columns""", dict, {"""tokens""", """labels""", """answers""", """id"""}, """tokens"""), ("""values""", list, None, None), ("""table""", dict, {"""schema""", """data"""}, """data"""), ] , ) def _UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): '''simple docstring''' with io.BytesIO() as buffer: JsonDatasetWriter(_UpperCAmelCase , _UpperCAmelCase , lines=_UpperCAmelCase , orient=_UpperCAmelCase ).write() buffer.seek(0 ) _lowercase = load_json(_UpperCAmelCase ) assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(_UpperCAmelCase , """keys""" ) and not hasattr(exported_content[0] , """keys""" ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(_UpperCAmelCase ) == 10 @pytest.mark.parametrize("""lines, load_json_function""" , [(True, load_json_lines), (False, load_json)] ) def _UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): '''simple docstring''' with io.BytesIO() as buffer: JsonDatasetWriter(_UpperCAmelCase , _UpperCAmelCase , lines=_UpperCAmelCase , num_proc=2 ).write() buffer.seek(0 ) _lowercase = load_json_function(_UpperCAmelCase ) assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) assert isinstance(exported_content[0] , _UpperCAmelCase ) assert len(_UpperCAmelCase ) == 10 @pytest.mark.parametrize( """orient, container, keys, len_at""" , [ ("""records""", list, {"""tokens""", """labels""", """answers""", """id"""}, None), ("""split""", dict, {"""columns""", """data"""}, """data"""), ("""index""", dict, set("""0123456789""" ), None), ("""columns""", dict, {"""tokens""", """labels""", """answers""", """id"""}, """tokens"""), ("""values""", list, None, None), ("""table""", dict, {"""schema""", """data"""}, """data"""), ] , ) def _UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): '''simple docstring''' with io.BytesIO() as buffer: JsonDatasetWriter(_UpperCAmelCase , _UpperCAmelCase , lines=_UpperCAmelCase , orient=_UpperCAmelCase , num_proc=2 ).write() buffer.seek(0 ) _lowercase = load_json(_UpperCAmelCase ) assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(_UpperCAmelCase , """keys""" ) and not hasattr(exported_content[0] , """keys""" ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(_UpperCAmelCase ) == 10 def _UpperCAmelCase ( self , UpperCAmelCase ): '''simple docstring''' with pytest.raises(_UpperCAmelCase ): with io.BytesIO() as buffer: JsonDatasetWriter(_UpperCAmelCase , _UpperCAmelCase , num_proc=0 ) @pytest.mark.parametrize("""compression, extension""" , [("""gzip""", """gz"""), ("""bz2""", """bz2"""), ("""xz""", """xz""")] ) def _UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): '''simple docstring''' _lowercase = tmp_path_factory.mktemp("""data""" ) / F'''test.json.{extension}''' _lowercase = str(shared_datadir / F'''test_file.json.{extension}''' ) JsonDatasetWriter(_UpperCAmelCase , _UpperCAmelCase , compression=_UpperCAmelCase ).write() with fsspec.open(_UpperCAmelCase , """rb""" , compression="""infer""" ) as f: _lowercase = f.read() with fsspec.open(_UpperCAmelCase , """rb""" , compression="""infer""" ) as f: _lowercase = f.read() assert exported_content == original_content
398
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = {} class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''llama''' __lowerCAmelCase = ['''past_key_values'''] def __init__( self , _UpperCAmelCase=32000 , _UpperCAmelCase=4096 , _UpperCAmelCase=11008 , _UpperCAmelCase=32 , _UpperCAmelCase=32 , _UpperCAmelCase=None , _UpperCAmelCase="silu" , _UpperCAmelCase=2048 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-6 , _UpperCAmelCase=True , _UpperCAmelCase=0 , _UpperCAmelCase=1 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=False , _UpperCAmelCase=None , **_UpperCAmelCase , ): __a : Dict = vocab_size __a : Union[str, Any] = max_position_embeddings __a : str = hidden_size __a : List[str] = intermediate_size __a : Any = num_hidden_layers __a : int = num_attention_heads # for backward compatibility if num_key_value_heads is None: __a : Union[str, Any] = num_attention_heads __a : Optional[int] = num_key_value_heads __a : Dict = hidden_act __a : Union[str, Any] = initializer_range __a : int = rms_norm_eps __a : Optional[int] = pretraining_tp __a : Optional[Any] = use_cache __a : Optional[Any] = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , tie_word_embeddings=_UpperCAmelCase , **_UpperCAmelCase , ) def _lowerCamelCase ( self ): if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _UpperCAmelCase ) 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}""" ) __a : Tuple = self.rope_scaling.get('''type''' , _UpperCAmelCase ) __a : Optional[int] = self.rope_scaling.get('''factor''' , _UpperCAmelCase ) 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(_UpperCAmelCase , _UpperCAmelCase ) or rope_scaling_factor <= 1.0: raise ValueError(f"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""" )
52
0
'''simple docstring''' import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, PLBartTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin __UpperCAmelCase =get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.plbart.modeling_plbart import shift_tokens_right __UpperCAmelCase =5_0_0_0_3 __UpperCAmelCase =5_0_0_0_2 @require_sentencepiece @require_tokenizers class a__ ( _UpperCamelCase , unittest.TestCase ): lowerCamelCase : Any =PLBartTokenizer lowerCamelCase : Optional[Any] =None lowerCamelCase : str =False def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing __lowerCamelCase = PLBartTokenizer(_UpperCAmelCase , language_codes='''base''' , keep_accents=_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = PLBartTokenizer(_UpperCAmelCase , language_codes='''base''' , keep_accents=_UpperCAmelCase ) __lowerCamelCase = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) __lowerCamelCase = 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''', '''é''', '''.''', ] , ) __lowerCamelCase = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) __lowerCamelCase = 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>''', '''.''', ] , ) __lowerCamelCase = tokenizer.vocab_size __lowerCamelCase = [tokenizer.convert_ids_to_tokens(_UpperCAmelCase ) for x in range(end - 4 , _UpperCAmelCase )] self.assertListEqual(_UpperCAmelCase , ['''__java__''', '''__python__''', '''__en_XX__''', '''<mask>'''] ) __lowerCamelCase = '''java.lang.Exception, python.lang.Exception, javascript, php, ruby, go''' __lowerCamelCase = tokenizer(_UpperCAmelCase ).input_ids self.assertEqual( tokenizer.decode(_UpperCAmelCase , skip_special_tokens=_UpperCAmelCase , clean_up_tokenization_spaces=_UpperCAmelCase ) , _UpperCAmelCase , ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = PLBartTokenizer(_UpperCAmelCase , language_codes='''multi''' , keep_accents=_UpperCAmelCase ) __lowerCamelCase = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) __lowerCamelCase = 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''', '''é''', '''.''', ] , ) __lowerCamelCase = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) __lowerCamelCase = 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>''', '''.''', ] , ) __lowerCamelCase = tokenizer.vocab_size __lowerCamelCase = [tokenizer.convert_ids_to_tokens(_UpperCAmelCase ) for x in range(end - 7 , _UpperCAmelCase )] self.assertListEqual( _UpperCAmelCase , ['''__java__''', '''__python__''', '''__en_XX__''', '''__javascript__''', '''__php__''', '''__ruby__''', '''__go__'''] ) __lowerCamelCase = '''java.lang.Exception, python.lang.Exception, javascript, php, ruby, go''' __lowerCamelCase = tokenizer(_UpperCAmelCase ).input_ids self.assertEqual( tokenizer.decode(_UpperCAmelCase , skip_special_tokens=_UpperCAmelCase , clean_up_tokenization_spaces=_UpperCAmelCase ) , _UpperCAmelCase , ) @require_torch @require_sentencepiece @require_tokenizers class a__ ( unittest.TestCase ): lowerCamelCase : Optional[Any] ="uclanlp/plbart-python-en_XX" lowerCamelCase : Dict =[ "def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])", "def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])", ] lowerCamelCase : List[str] =[ "Returns the maximum value of a b c.", "Sums the values of a b c.", ] lowerCamelCase : Tuple =[ 1_3_4, 5_4_5_2, 3_3_4_6_0, 3_3_4_4_1, 3_3_4_6_3, 3_3_4_6_5, 3_3_4_6_3, 3_3_4_4_9, 9_8_8, 2_0, 3_3_4_5_6, 1_9, 3_3_4_5_6, 7_7_1, 3_9, 4_2_5_8, 8_8_9, 3_3_1_8, 3_3_4_4_1, 3_3_4_6_3, 3_3_4_6_5, 3_3_4_6_3, 3_3_4_4_9, 2_4_7_1, 2, PYTHON_CODE, ] @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Dict ): """simple docstring""" __lowerCamelCase = PLBartTokenizer.from_pretrained( cls.checkpoint_name , language_codes='''base''' , src_lang='''python''' , tgt_lang='''en_XX''' ) __lowerCamelCase = 1 return cls def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__java__'''] , 5_00_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__python__'''] , 5_00_02 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__en_XX__'''] , 5_00_03 ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , _UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" self.assertIn(_UpperCAmelCase , self.tokenizer.all_special_ids ) __lowerCamelCase = [EN_CODE, 90_37, 3_34_42, 57, 7_52, 1_53, 14, 56, 18, 9, 2] __lowerCamelCase = self.tokenizer.decode(_UpperCAmelCase , skip_special_tokens=_UpperCAmelCase ) __lowerCamelCase = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=_UpperCAmelCase ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) self.assertNotIn(self.tokenizer.eos_token , _UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = ['''def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])''' * 20] self.assertIsInstance(src_text[0] , _UpperCAmelCase ) __lowerCamelCase = 10 __lowerCamelCase = self.tokenizer(_UpperCAmelCase , max_length=_UpperCAmelCase , truncation=_UpperCAmelCase ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , _UpperCAmelCase ) self.assertEqual(len(_UpperCAmelCase ) , _UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''__java__'''] ) , [5_00_04, 5_00_01] ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(_UpperCAmelCase ) __lowerCamelCase = PLBartTokenizer.from_pretrained(_UpperCAmelCase ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , _UpperCAmelCase ) @require_torch def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=_UpperCAmelCase , return_tensors='''pt''' ) __lowerCamelCase = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 self.assertEqual(batch.input_ids[1][-2:].tolist() , [2, PYTHON_CODE] ) self.assertEqual(batch.decoder_input_ids[1][0] , _UpperCAmelCase ) self.assertEqual(batch.decoder_input_ids[1][-1] , 2 ) self.assertEqual(batch.labels[1][-2:].tolist() , [2, EN_CODE] ) @require_torch def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" __lowerCamelCase = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , max_length=len(self.expected_src_tokens ) , return_tensors='''pt''' , ) __lowerCamelCase = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase ) self.assertEqual((2, 26) , batch.input_ids.shape ) self.assertEqual((2, 26) , batch.attention_mask.shape ) __lowerCamelCase = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , _UpperCAmelCase ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, PYTHON_CODE] ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.tokenizer(self.src_text , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , max_length=3 , return_tensors='''pt''' ) __lowerCamelCase = self.tokenizer( text_target=self.tgt_text , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , max_length=10 , return_tensors='''pt''' ) __lowerCamelCase = targets['''input_ids'''] __lowerCamelCase = shift_tokens_right(_UpperCAmelCase , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''en_XX''' , tgt_lang='''java''' ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { # A, test, EOS, en_XX '''input_ids''': [[1_50, 2_42, 2, 5_00_03]], '''attention_mask''': [[1, 1, 1, 1]], # java '''forced_bos_token_id''': 5_00_01, } , )
546
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=3 , _UpperCAmelCase=18 , _UpperCAmelCase=30 , _UpperCAmelCase=400 , _UpperCAmelCase=True , _UpperCAmelCase=None , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=[0.5, 0.5, 0.5] , _UpperCAmelCase=[0.5, 0.5, 0.5] , ): __a : int = parent __a : str = batch_size __a : List[Any] = num_channels __a : Union[str, Any] = image_size __a : List[Any] = min_resolution __a : str = max_resolution __a : List[str] = do_resize __a : Optional[int] = size if size is not None else {'''height''': 18, '''width''': 20} __a : str = do_thumbnail __a : str = do_align_axis __a : Dict = do_pad __a : Union[str, Any] = do_normalize __a : List[str] = image_mean __a : Optional[int] = image_std def _lowerCamelCase ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = DonutImageProcessor if is_vision_available() else None def _lowerCamelCase ( self ): __a : Tuple = DonutImageProcessingTester(self ) @property def _lowerCamelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_resize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_thumbnail''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_align_long_axis''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_pad''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_mean''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_std''' ) ) def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 20} ) __a : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) # Previous config had dimensions in (width, height) order __a : int = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'''height''': 84, '''width''': 42} ) def _lowerCamelCase ( self ): pass @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , Image.Image ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : int = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , numpify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , np.ndarray ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : str = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , torchify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , torch.Tensor ) # Test not batched input __a : Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : List[str] = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
52
0
import math import tensorflow as tf from packaging import version def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = tf.convert_to_tensor(a_ ) snake_case_ = 0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0 ) , x.dtype ) )) return x * cdf def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = tf.convert_to_tensor(a_ ) snake_case_ = tf.cast(math.pi , x.dtype ) snake_case_ = tf.cast(0.04_47_15 , x.dtype ) snake_case_ = 0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi ) * (x + coeff * tf.pow(a_ , 3 )) )) return x * cdf def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = tf.convert_to_tensor(a_ ) return x * tf.tanh(tf.math.softplus(a_ ) ) def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = tf.convert_to_tensor(a_ ) snake_case_ = tf.cast(0.04_47_15 , x.dtype ) snake_case_ = tf.cast(0.79_78_84_56_08 , x.dtype ) return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x) )) def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = tf.convert_to_tensor(a_ ) snake_case_ = tf.cast(1.7_02 , x.dtype ) return x * tf.math.sigmoid(coeff * x ) def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' return tf.clip_by_value(_gelu(a_ ) , -10 , 10 ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__=-1 ): '''simple docstring''' snake_case_ = tf.split(a_ , 2 , axis=a_ ) return a * tf.math.sigmoid(a_ ) if version.parse(tf.version.VERSION) >= version.parse("""2.4"""): def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' return tf.keras.activations.gelu(a_ , approximate=a_ ) _UpperCAmelCase : Any = tf.keras.activations.gelu _UpperCAmelCase : int = approximate_gelu_wrap else: _UpperCAmelCase : Optional[Any] = _gelu _UpperCAmelCase : int = _gelu_new _UpperCAmelCase : Dict = { """gelu""": gelu, """gelu_10""": gelu_aa, """gelu_fast""": gelu_fast, """gelu_new""": gelu_new, """glu""": glu, """mish""": mish, """quick_gelu""": quick_gelu, """relu""": tf.keras.activations.relu, """sigmoid""": tf.keras.activations.sigmoid, """silu""": tf.keras.activations.swish, """swish""": tf.keras.activations.swish, """tanh""": tf.keras.activations.tanh, } def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' if activation_string in ACTaFN: return ACTaFN[activation_string] else: raise KeyError(F'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys() )}''' )
362
"""simple docstring""" from __future__ import annotations def __A ( a_ :list[int]) -> int: if not nums: return 0 __a : Any = nums[0] __a : Optional[Any] = 0 for num in nums[1:]: __a , __a : Optional[Any] = ( max_excluding + num, max(a_ , a_), ) return max(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" from collections import defaultdict def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = 1 _UpperCAmelCase = True for v in tree[start]: if v not in visited: ret += dfs(a_ ) if ret % 2 == 0: cuts.append(a_ ) return ret def __UpperCAmelCase ( ): """simple docstring""" dfs(1 ) if __name__ == "__main__": UpperCAmelCase__ , UpperCAmelCase__ = 1_0, 9 UpperCAmelCase__ = defaultdict(list) UpperCAmelCase__ = {} UpperCAmelCase__ = [] UpperCAmelCase__ = 0 UpperCAmelCase__ = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (1_0, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
277
"""simple docstring""" import unittest from transformers import BigBirdTokenizer, BigBirdTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A = '''▁''' A = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = BigBirdTokenizer __lowerCAmelCase = BigBirdTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = True def _lowerCamelCase ( self ): super().setUp() __a : Dict = self.tokenizer_class(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCamelCase ( self ): __a : List[str] = '''<s>''' __a : Tuple = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''[MASK]''' ) self.assertEqual(len(_UpperCAmelCase ) , 1004 ) def _lowerCamelCase ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def _lowerCamelCase ( self ): if not self.test_rust_tokenizer: return __a : Dict = self.get_tokenizer() __a : Any = self.get_rust_tokenizer() __a : int = '''I was born in 92000, and this is falsé.''' __a : Optional[Any] = tokenizer.tokenize(_UpperCAmelCase ) __a : List[str] = rust_tokenizer.tokenize(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Dict = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) __a : Any = rust_tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Tuple = self.get_rust_tokenizer() __a : Tuple = tokenizer.encode(_UpperCAmelCase ) __a : List[Any] = rust_tokenizer.encode(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = BigBirdTokenizer(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) __a : Optional[int] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) __a : Optional[int] = 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''', '''é''', '''.''', ] , ) __a : Optional[Any] = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) __a : Optional[int] = 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>''', '''.''', ] , ) @cached_property def _lowerCamelCase ( self ): return BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) @slow def _lowerCamelCase ( self ): __a : str = '''Hello World!''' __a : str = [65, 18536, 2260, 101, 66] self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @slow def _lowerCamelCase ( self ): __a : 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 __a : Optional[Any] = [65, 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, 66] # noqa: E231 # fmt: on self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @require_torch @slow def _lowerCamelCase ( self ): import torch from transformers import BigBirdConfig, BigBirdModel # Build sequence __a : List[Any] = list(self.big_tokenizer.get_vocab().keys() )[:10] __a : List[str] = ''' '''.join(_UpperCAmelCase ) __a : Tuple = self.big_tokenizer.encode_plus(_UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Any = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Optional[Any] = BigBirdConfig(attention_type='''original_full''' ) __a : Tuple = BigBirdModel(_UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_UpperCAmelCase ) model(**_UpperCAmelCase ) @slow def _lowerCamelCase ( self ): __a : Union[str, Any] = BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) __a : List[Any] = tokenizer.decode(tokenizer('''Paris is the [MASK].''' ).input_ids ) self.assertTrue(decoded_text == '''[CLS] Paris is the[MASK].[SEP]''' ) @slow def _lowerCamelCase ( self ): # fmt: off __a : Optional[Any] = {'''input_ids''': [[65, 39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114, 66], [65, 448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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], [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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name='''google/bigbird-roberta-base''' , revision='''215c99f1600e06f83acce68422f2035b2b5c3510''' , )
52
0
from __future__ import annotations class lowerCAmelCase_ : '''simple docstring''' def __init__( self , __UpperCAmelCase , __UpperCAmelCase ): SCREAMING_SNAKE_CASE_ : List[Any] =text, pattern SCREAMING_SNAKE_CASE_ : Tuple =len(_UpperCAmelCase ), len(_UpperCAmelCase ) def __lowerCamelCase ( self , __UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def __lowerCamelCase ( self , __UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def __lowerCamelCase ( self ): # searches pattern in text and returns index positions SCREAMING_SNAKE_CASE_ : Dict =[] for i in range(self.textLen - self.patLen + 1 ): SCREAMING_SNAKE_CASE_ : List[str] =self.mismatch_in_text(_UpperCAmelCase ) if mismatch_index == -1: positions.append(_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ : Tuple =self.match_in_pattern(self.text[mismatch_index] ) SCREAMING_SNAKE_CASE_ : Optional[int] =( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions __SCREAMING_SNAKE_CASE = 'ABAABA' __SCREAMING_SNAKE_CASE = 'AB' __SCREAMING_SNAKE_CASE = BoyerMooreSearch(text, pattern) __SCREAMING_SNAKE_CASE = bms.bad_character_heuristic() if len(positions) == 0: print('No match found') else: print('Pattern found in following positions: ') print(positions)
220
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A = logging.get_logger(__name__) A = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''convnextv2''' def __init__( self , _UpperCAmelCase=3 , _UpperCAmelCase=4 , _UpperCAmelCase=4 , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=224 , _UpperCAmelCase=None , _UpperCAmelCase=None , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : List[str] = num_channels __a : str = patch_size __a : Dict = num_stages __a : List[str] = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes __a : List[str] = [3, 3, 9, 3] if depths is None else depths __a : List[Any] = hidden_act __a : Any = initializer_range __a : Optional[int] = layer_norm_eps __a : List[Any] = drop_path_rate __a : Any = image_size __a : str = ['''stem'''] + [f"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] __a , __a : Optional[int] = get_aligned_output_features_output_indices( out_features=_UpperCAmelCase , out_indices=_UpperCAmelCase , stage_names=self.stage_names )
52
0
import fcntl import os import socket import torch import torch.distributed as dist def __SCREAMING_SNAKE_CASE ( *a__ : Union[str, Any] ) -> Union[str, Any]: with open(a_ ,"""r""" ) as fh: fcntl.flock(a_ ,fcntl.LOCK_EX ) try: print(*a_ ) finally: fcntl.flock(a_ ,fcntl.LOCK_UN ) UpperCAmelCase_ : int = int(os.environ['''LOCAL_RANK''']) torch.cuda.set_device(local_rank) UpperCAmelCase_ : int = torch.device('''cuda''', local_rank) UpperCAmelCase_ : Dict = socket.gethostname() UpperCAmelCase_ : Any = f"""[{hostname}-{local_rank}]""" try: # test distributed dist.init_process_group('''nccl''') dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM) dist.barrier() # test cuda is available and can allocate memory torch.cuda.is_available() torch.ones(1).cuda(local_rank) # global rank UpperCAmelCase_ : Optional[int] = dist.get_rank() UpperCAmelCase_ : str = dist.get_world_size() printflock(f"""{gpu} is OK (global rank: {rank}/{world_size})""") dist.barrier() if rank == 0: printflock(f"""pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}""") except Exception: printflock(f"""{gpu} is broken""") raise
17
"""simple docstring""" import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = (DDPMScheduler,) def _lowerCamelCase ( self , **_UpperCAmelCase ): __a : int = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**_UpperCAmelCase ) return config def _lowerCamelCase ( self ): for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_UpperCAmelCase , beta_end=_UpperCAmelCase ) def _lowerCamelCase ( self ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_UpperCAmelCase ) def _lowerCamelCase ( self ): for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=_UpperCAmelCase ) def _lowerCamelCase ( self ): self.check_over_configs(thresholding=_UpperCAmelCase ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_UpperCAmelCase , prediction_type=_UpperCAmelCase , sample_max_value=_UpperCAmelCase , ) def _lowerCamelCase ( self ): for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for t in [0, 500, 999]: self.check_over_forward(time_step=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Dict = scheduler_class(**_UpperCAmelCase ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def _lowerCamelCase ( self ): __a : int = self.scheduler_classes[0] __a : int = self.get_scheduler_config() __a : Optional[Any] = scheduler_class(**_UpperCAmelCase ) __a : int = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[Any] = self.dummy_sample_deter __a : Union[str, Any] = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Optional[int] = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : List[Any] = pred_prev_sample __a : int = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : Union[str, Any] = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def _lowerCamelCase ( self ): __a : Dict = self.scheduler_classes[0] __a : int = self.get_scheduler_config(prediction_type='''v_prediction''' ) __a : int = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[str] = self.dummy_sample_deter __a : str = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Dict = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : Optional[int] = pred_prev_sample __a : Optional[int] = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : int = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Any = self.get_scheduler_config() __a : str = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_UpperCAmelCase ) __a : List[Any] = scheduler.timesteps for i, timestep in enumerate(_UpperCAmelCase ): if i == len(_UpperCAmelCase ) - 1: __a : Union[str, Any] = -1 else: __a : str = timesteps[i + 1] __a : Dict = scheduler.previous_timestep(_UpperCAmelCase ) __a : str = prev_t.item() self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Tuple = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Optional[Any] = [100, 87, 50, 51, 0] with self.assertRaises(_UpperCAmelCase , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] __a : Optional[int] = len(_UpperCAmelCase ) with self.assertRaises(_UpperCAmelCase , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=_UpperCAmelCase , timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : List[str] = scheduler_class(**_UpperCAmelCase ) __a : List[Any] = [scheduler.config.num_train_timesteps] with self.assertRaises( _UpperCAmelCase , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=_UpperCAmelCase )
52
0
from ...processing_utils import ProcessorMixin class lowercase ( _UpperCamelCase ): __a = """SpeechT5FeatureExtractor""" __a = """SpeechT5Tokenizer""" def __init__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): """simple docstring""" super().__init__(_UpperCAmelCase , _UpperCAmelCase ) def __call__( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCAmelCase__ : Tuple = kwargs.pop('''audio''' , _UpperCAmelCase ) lowerCAmelCase__ : List[Any] = kwargs.pop('''text''' , _UpperCAmelCase ) lowerCAmelCase__ : List[Any] = kwargs.pop('''text_target''' , _UpperCAmelCase ) lowerCAmelCase__ : Tuple = kwargs.pop('''audio_target''' , _UpperCAmelCase ) lowerCAmelCase__ : List[str] = kwargs.pop('''sampling_rate''' , _UpperCAmelCase ) if audio is not None and text is not None: raise ValueError( '''Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?''' ) if audio_target is not None and text_target is not None: raise ValueError( '''Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?''' ) if audio is None and audio_target is None and text is None and text_target is None: raise ValueError( '''You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.''' ) if audio is not None: lowerCAmelCase__ : Dict = self.feature_extractor(_UpperCAmelCase , *_UpperCAmelCase , sampling_rate=_UpperCAmelCase , **_UpperCAmelCase ) elif text is not None: lowerCAmelCase__ : Union[str, Any] = self.tokenizer(_UpperCAmelCase , **_UpperCAmelCase ) else: lowerCAmelCase__ : str = None if audio_target is not None: lowerCAmelCase__ : Optional[int] = self.feature_extractor(audio_target=_UpperCAmelCase , *_UpperCAmelCase , sampling_rate=_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase__ : Any = targets['''input_values'''] elif text_target is not None: lowerCAmelCase__ : int = self.tokenizer(_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase__ : Optional[Any] = targets['''input_ids'''] else: lowerCAmelCase__ : List[Any] = None if inputs is None: return targets if targets is not None: lowerCAmelCase__ : Dict = labels lowerCAmelCase__ : List[Any] = targets.get('''attention_mask''' ) if decoder_attention_mask is not None: lowerCAmelCase__ : List[str] = decoder_attention_mask return inputs def lowercase_ ( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCAmelCase__ : List[str] = kwargs.pop('''input_values''' , _UpperCAmelCase ) lowerCAmelCase__ : Optional[Any] = kwargs.pop('''input_ids''' , _UpperCAmelCase ) lowerCAmelCase__ : Dict = kwargs.pop('''labels''' , _UpperCAmelCase ) if input_values is not None and input_ids is not None: raise ValueError('''Cannot process both `input_values` and `input_ids` inputs.''' ) if input_values is None and input_ids is None and labels is None: raise ValueError( '''You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.''' ) if input_values is not None: lowerCAmelCase__ : str = self.feature_extractor.pad(_UpperCAmelCase , *_UpperCAmelCase , **_UpperCAmelCase ) elif input_ids is not None: lowerCAmelCase__ : str = self.tokenizer.pad(_UpperCAmelCase , **_UpperCAmelCase ) else: lowerCAmelCase__ : List[Any] = None if labels is not None: if "input_ids" in labels or (isinstance(_UpperCAmelCase , _UpperCAmelCase ) and "input_ids" in labels[0]): lowerCAmelCase__ : str = self.tokenizer.pad(_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase__ : str = targets['''input_ids'''] else: lowerCAmelCase__ : Optional[Any] = self.feature_extractor.feature_size lowerCAmelCase__ : Tuple = self.feature_extractor.num_mel_bins lowerCAmelCase__ : str = self.feature_extractor.pad(_UpperCAmelCase , *_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase__ : List[str] = feature_size_hack lowerCAmelCase__ : int = targets['''input_values'''] else: lowerCAmelCase__ : Tuple = None if inputs is None: return targets if targets is not None: lowerCAmelCase__ : List[Any] = labels lowerCAmelCase__ : Optional[int] = targets.get('''attention_mask''' ) if decoder_attention_mask is not None: lowerCAmelCase__ : Any = decoder_attention_mask return inputs def lowercase_ ( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" return self.tokenizer.batch_decode(*_UpperCAmelCase , **_UpperCAmelCase ) def lowercase_ ( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" return self.tokenizer.decode(*_UpperCAmelCase , **_UpperCAmelCase )
233
"""simple docstring""" import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset A = random.Random() def __A ( a_ :Tuple , a_ :Dict=1.0 , a_ :str=None , a_ :List[Any]=None) -> Dict: if rng is None: __a : Any = global_rng __a : Tuple = [] for batch_idx in range(shape[0]): values.append([]) for _ in range(shape[1]): values[-1].append(rng.random() * scale) return values class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=400 , _UpperCAmelCase=2000 , _UpperCAmelCase=2048 , _UpperCAmelCase=128 , _UpperCAmelCase=1 , _UpperCAmelCase=512 , _UpperCAmelCase=30 , _UpperCAmelCase=44100 , ): __a : Any = parent __a : Tuple = batch_size __a : Tuple = min_seq_length __a : List[str] = max_seq_length __a : List[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __a : Tuple = spectrogram_length __a : int = feature_size __a : int = num_audio_channels __a : Tuple = hop_length __a : List[Any] = chunk_length __a : Any = sampling_rate def _lowerCamelCase ( self ): return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def _lowerCamelCase ( self , _UpperCAmelCase=False , _UpperCAmelCase=False ): def _flatten(_UpperCAmelCase ): return list(itertools.chain(*_UpperCAmelCase ) ) if equal_length: __a : Tuple = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __a : Tuple = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __a : Optional[Any] = [np.asarray(_UpperCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = TvltFeatureExtractor def _lowerCamelCase ( self ): __a : Optional[Any] = TvltFeatureExtractionTester(self ) def _lowerCamelCase ( self ): __a : int = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''spectrogram_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''feature_size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''num_audio_channels''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''hop_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''chunk_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''sampling_rate''' ) ) def _lowerCamelCase ( self ): __a : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : List[str] = feat_extract_first.save_pretrained(_UpperCAmelCase )[0] check_json_file_has_correct_format(_UpperCAmelCase ) __a : Union[str, Any] = self.feature_extraction_class.from_pretrained(_UpperCAmelCase ) __a : Tuple = feat_extract_first.to_dict() __a : List[Any] = feat_extract_second.to_dict() __a : int = dict_first.pop('''mel_filters''' ) __a : List[Any] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : int = os.path.join(_UpperCAmelCase , '''feat_extract.json''' ) feat_extract_first.to_json_file(_UpperCAmelCase ) __a : Optional[Any] = self.feature_extraction_class.from_json_file(_UpperCAmelCase ) __a : Optional[Any] = feat_extract_first.to_dict() __a : Any = feat_extract_second.to_dict() __a : Optional[Any] = dict_first.pop('''mel_filters''' ) __a : Dict = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): # Initialize feature_extractor __a : str = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 __a : Any = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __a : Union[str, Any] = [np.asarray(_UpperCAmelCase ) for speech_input in speech_inputs] # Test not batched input __a : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched __a : int = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking __a : List[Any] = feature_extractor( _UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 , mask_audio=_UpperCAmelCase ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. __a : str = [floats_list((1, x) )[0] for x in (800, 800, 800)] __a : Any = np.asarray(_UpperCAmelCase ) __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def _lowerCamelCase ( self , _UpperCAmelCase ): __a : int = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech __a : int = ds.sort('''id''' ).select(range(_UpperCAmelCase ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowerCamelCase ( self ): __a : List[str] = self._load_datasamples(1 ) __a : Tuple = TvltFeatureExtractor() __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) __a : Dict = torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _UpperCAmelCase , atol=1e-4 ) )
52
0
"""simple docstring""" from __future__ import annotations def lowercase_ ( _lowercase : int | str ): '''simple docstring''' UpperCAmelCase : Dict = str(a_ ) return n == n[::-1] def lowercase_ ( _lowercase : int = 1_00_00_00 ): '''simple docstring''' UpperCAmelCase : Tuple = 0 for i in range(1 , a_ ): if is_palindrome(a_ ) and is_palindrome(bin(a_ ).split("b" )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
595
"""simple docstring""" from __future__ import annotations class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a , __a : List[Any] = text, pattern __a , __a : Tuple = len(_UpperCAmelCase ), len(_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def _lowerCamelCase ( self ): # searches pattern in text and returns index positions __a : Dict = [] for i in range(self.textLen - self.patLen + 1 ): __a : List[str] = self.mismatch_in_text(_UpperCAmelCase ) if mismatch_index == -1: positions.append(_UpperCAmelCase ) else: __a : Tuple = self.match_in_pattern(self.text[mismatch_index] ) __a : Optional[int] = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions A = '''ABAABA''' A = '''AB''' A = BoyerMooreSearch(text, pattern) A = bms.bad_character_heuristic() if len(positions) == 0: print('''No match found''') else: print('''Pattern found in following positions: ''') print(positions)
52
0
'''simple docstring''' import argparse import struct import unittest class lowerCamelCase : def __init__( self , a_ ): lowerCAmelCase : Any = data # Initialize hash values lowerCAmelCase : int = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ] # Initialize round constants lowerCAmelCase : Union[str, Any] = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ] lowerCAmelCase : Tuple = self.preprocessing(self.data ) self.final_hash() @staticmethod def _lowerCamelCase ( a_ ): lowerCAmelCase : Union[str, Any] = b'''\x80''' + (b'''\x00''' * (63 - (len(_UpperCAmelCase ) + 8) % 64)) lowerCAmelCase : Tuple = struct.pack(">Q" , (len(_UpperCAmelCase ) * 8) ) return data + padding + big_endian_integer def _lowerCamelCase ( self ): # Convert into blocks of 64 bytes lowerCAmelCase : Dict = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers lowerCAmelCase : Union[str, Any] = list(struct.unpack(">16L" , _UpperCAmelCase ) ) # add 48 0-ed integers words += [0] * 48 lowerCAmelCase : str = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array lowerCAmelCase : int = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) lowerCAmelCase : Optional[Any] = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) lowerCAmelCase : Optional[int] = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x100000000 # Compression lowerCAmelCase : int = self.ror(_UpperCAmelCase , 6 ) ^ self.ror(_UpperCAmelCase , 11 ) ^ self.ror(_UpperCAmelCase , 25 ) lowerCAmelCase : Optional[int] = (e & f) ^ ((~e & 0xffffffff) & g) lowerCAmelCase : Dict = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x100000000 lowerCAmelCase : Tuple = self.ror(_UpperCAmelCase , 2 ) ^ self.ror(_UpperCAmelCase , 13 ) ^ self.ror(_UpperCAmelCase , 22 ) lowerCAmelCase : Optional[int] = (a & b) ^ (a & c) ^ (b & c) lowerCAmelCase : Any = (sa + maj) % 0x100000000 lowerCAmelCase : Optional[Any] = ( g, f, e, ((d + tempa) % 0x100000000), c, b, a, ((tempa + tempa) % 0x100000000), ) lowerCAmelCase : Tuple = [a, b, c, d, e, f, g, h] # Modify final values lowerCAmelCase : Optional[Any] = [ ((element + mutated_hash_values[index]) % 0x100000000) for index, element in enumerate(self.hashes ) ] lowerCAmelCase : List[Any] = ''''''.join([hex(_UpperCAmelCase )[2:].zfill(8 ) for value in self.hashes] ) def _lowerCamelCase ( self , a_ , a_ ): return 0xffffffff & (value << (32 - rotations)) | (value >> rotations) class lowerCamelCase ( unittest.TestCase ): def _lowerCamelCase ( self ): import hashlib lowerCAmelCase : Any = bytes("Test String" , "utf-8" ) self.assertEqual(SHAaaa(_UpperCAmelCase ).hash , hashlib.shaaaa(_UpperCAmelCase ).hexdigest() ) def __A ( ): import doctest doctest.testmod() lowerCAmelCase : List[Any] = argparse.ArgumentParser() parser.add_argument( "-s" ,"--string" ,dest="input_string" ,default="Hello World!! Welcome to Cryptography" ,help="Hash the string" ,) parser.add_argument( "-f" ,"--file" ,dest="input_file" ,help="Hash contents of a file" ) lowerCAmelCase : Dict = parser.parse_args() lowerCAmelCase : Dict = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file ,"rb" ) as f: lowerCAmelCase : Any = f.read() else: lowerCAmelCase : Optional[Any] = bytes(a_ ,"utf-8" ) print(SHAaaa(a_ ).hash ) if __name__ == "__main__": main()
525
"""simple docstring""" import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": A = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') A = F'https://www.google.com/search?q={query}&num=100' A = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: A = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: A = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
0
'''simple docstring''' import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def lowerCamelCase__ ( A_ , A_ ): UpperCAmelCase_ = args.log_outputs UpperCAmelCase_ = '''_'''.join(args.dataset.split("/" ) + [args.config, args.split] ) # load metric UpperCAmelCase_ = load_metric("wer" ) UpperCAmelCase_ = load_metric("cer" ) # compute metrics UpperCAmelCase_ = wer.compute(references=result["target"] , predictions=result["prediction"] ) UpperCAmelCase_ = cer.compute(references=result["target"] , predictions=result["prediction"] ) # print & log results UpperCAmelCase_ = F"""WER: {wer_result}\nCER: {cer_result}""" print(a_ ) with open(F"""{dataset_id}_eval_results.txt""" , "w" ) as f: f.write(a_ ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: UpperCAmelCase_ = F"""log_{dataset_id}_predictions.txt""" UpperCAmelCase_ = F"""log_{dataset_id}_targets.txt""" with open(a_ , "w" ) as p, open(a_ , "w" ) as t: # mapping function to write output def write_to_file(A_ , A_ ): p.write(F"""{i}""" + "\n" ) p.write(batch["prediction"] + "\n" ) t.write(F"""{i}""" + "\n" ) t.write(batch["target"] + "\n" ) result.map(a_ , with_indices=a_ ) def lowerCamelCase__ ( A_ ): UpperCAmelCase_ = '''[,?.!\-\;\:"“%‘”�—’…–]''' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training UpperCAmelCase_ = re.sub(a_ , "" , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! UpperCAmelCase_ = ['''\n\n''', '''\n''', ''' ''', ''' '''] for t in token_sequences_to_ignore: UpperCAmelCase_ = ''' '''.join(text.split(a_ ) ) return text def lowerCamelCase__ ( A_ ): # load dataset UpperCAmelCase_ = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=a_ ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor UpperCAmelCase_ = AutoFeatureExtractor.from_pretrained(args.model_id ) UpperCAmelCase_ = feature_extractor.sampling_rate # resample audio UpperCAmelCase_ = dataset.cast_column("audio" , Audio(sampling_rate=a_ ) ) # load eval pipeline if args.device is None: UpperCAmelCase_ = 0 if torch.cuda.is_available() else -1 UpperCAmelCase_ = pipeline("automatic-speech-recognition" , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(A_ ): UpperCAmelCase_ = asr( batch["audio"]["array"] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) UpperCAmelCase_ = prediction['''text'''] UpperCAmelCase_ = normalize_text(batch["sentence"] ) return batch # run inference on all examples UpperCAmelCase_ = dataset.map(a_ , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(a_ , a_ ) if __name__ == "__main__": __snake_case : Any = argparse.ArgumentParser() parser.add_argument( '''--model_id''', type=str, required=True, help='''Model identifier. Should be loadable with 🤗 Transformers''' ) parser.add_argument( '''--dataset''', type=str, required=True, help='''Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets''', ) parser.add_argument( '''--config''', type=str, required=True, help='''Config of the dataset. *E.g.* `\'en\'` for Common Voice''' ) parser.add_argument('''--split''', type=str, required=True, help='''Split of the dataset. *E.g.* `\'test\'`''') parser.add_argument( '''--chunk_length_s''', type=float, default=None, help='''Chunk length in seconds. Defaults to 5 seconds.''' ) parser.add_argument( '''--stride_length_s''', type=float, default=None, help='''Stride of the audio chunks. Defaults to 1 second.''' ) parser.add_argument( '''--log_outputs''', action='''store_true''', help='''If defined, write outputs to log file for analysis.''' ) parser.add_argument( '''--device''', type=int, default=None, help='''The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.''', ) __snake_case : Optional[int] = parser.parse_args() main(args)
660
"""simple docstring""" import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = 0 __lowerCAmelCase = False __lowerCAmelCase = 3.0 class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_UpperCAmelCase ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.2_5 ).to_kwargs() , {'''a''': 2, '''c''': 2.2_5} ) @require_cuda def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a : List[Any] = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() __a : int = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a : Optional[Any] = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_0_2_4.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _UpperCAmelCase ) @require_multi_gpu def _lowerCamelCase ( self ): __a : Dict = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] execute_subprocess_async(_UpperCAmelCase , env=os.environ.copy() ) if __name__ == "__main__": A = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) A = Accelerator(kwargs_handlers=[ddp_scaler]) A = torch.nn.Linear(100, 200) A = accelerator.prepare(model) # Check the values changed in kwargs A = '''''' A = model.bucket_bytes_cap // (1_024 * 1_024) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
52
0
import math import random def __magic_name__ ( lowerCAmelCase_ , lowerCAmelCase_ = False): '''simple docstring''' if deriv: return value * (1 - value) return 1 / (1 + math.exp(-value)) # Initial Value __magic_name__ = 0.02 def __magic_name__ ( lowerCAmelCase_ , lowerCAmelCase_): '''simple docstring''' lowerCamelCase_ : List[Any] = float(2 * (random.randint(1 , 100)) - 1) for _ in range(a_): # Forward propagation lowerCamelCase_ : int = sigmoid_function(INITIAL_VALUE * weight) # How much did we miss? lowerCamelCase_ : int = (expected / 100) - layer_a # Error delta lowerCamelCase_ : Dict = layer_1_error * sigmoid_function(a_ , a_) # Update weight weight += INITIAL_VALUE * layer_1_delta return layer_a * 100 if __name__ == "__main__": import doctest doctest.testmod() __magic_name__ = int(input('''Expected value: ''')) __magic_name__ = int(input('''Number of propagations: ''')) print(forward_propagation(expected, number_propagations))
250
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
0
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip A_: Optional[Any] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def __lowerCAmelCase ( _A ): """simple docstring""" if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" return max(metric_fn(a_ ,a_ ) for gt in ground_truths ) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" _lowercase = [line.strip() for line in open(a_ ,"""r""" ).readlines()] _lowercase = [] if args.gold_data_mode == "qa": _lowercase = pd.read_csv(a_ ,sep="""\t""" ,header=a_ ) for answer_list in data[1]: _lowercase = ast.literal_eval(a_ ) answers.append(a_ ) else: _lowercase = [line.strip() for line in open(a_ ,"""r""" ).readlines()] _lowercase = [[reference] for reference in references] _lowercase = 0 for prediction, ground_truths in zip(a_ ,a_ ): total += 1 em += metric_max_over_ground_truths(a_ ,a_ ,a_ ) fa += metric_max_over_ground_truths(a_ ,a_ ,a_ ) _lowercase = 100.0 * em / total _lowercase = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" _lowercase = args.k _lowercase = [line.strip() for line in open(a_ ,"""r""" ).readlines()] _lowercase = [line.strip() for line in open(a_ ,"""r""" ).readlines()] _lowercase = 0 for hypo, reference in zip(a_ ,a_ ): _lowercase = set(hypo.split("""\t""" )[:k] ) _lowercase = set(reference.split("""\t""" ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k _lowercase = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" def strip_title(_A ): if title.startswith("""\"""" ): _lowercase = title[1:] if title.endswith("""\"""" ): _lowercase = title[:-1] return title _lowercase = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ ,return_tensors="""pt""" ,padding=a_ ,truncation=a_ ,)['''input_ids'''].to(args.device ) _lowercase = rag_model.rag.question_encoder(a_ ) _lowercase = question_enc_outputs[0] _lowercase = rag_model.retriever( a_ ,question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() ,prefix=rag_model.rag.generator.config.prefix ,n_docs=rag_model.config.n_docs ,return_tensors="""pt""" ,) _lowercase = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) _lowercase = [] for docs in all_docs: _lowercase = [strip_title(a_ ) for title in docs['''title''']] provenance_strings.append("""\t""".join(a_ ) ) return provenance_strings def __lowerCAmelCase ( _A ,_A ,_A ): """simple docstring""" with torch.no_grad(): _lowercase = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ ,return_tensors="""pt""" ,padding=a_ ,truncation=a_ ) _lowercase = inputs_dict.input_ids.to(args.device ) _lowercase = inputs_dict.attention_mask.to(args.device ) _lowercase = rag_model.generate( # rag_model overwrites generate a_ ,attention_mask=a_ ,num_beams=args.num_beams ,min_length=args.min_length ,max_length=args.max_length ,early_stopping=a_ ,num_return_sequences=1 ,bad_words_ids=[[0, 0]] ,) _lowercase = rag_model.retriever.generator_tokenizer.batch_decode(a_ ,skip_special_tokens=a_ ) if args.print_predictions: for q, a in zip(a_ ,a_ ): logger.info("""Q: {} - A: {}""".format(a_ ,a_ ) ) return answers def __lowerCAmelCase ( ): """simple docstring""" _lowercase = argparse.ArgumentParser() parser.add_argument( """--model_type""" ,choices=["""rag_sequence""", """rag_token""", """bart"""] ,type=a_ ,help=( """RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the""" """ model_name_or_path""" ) ,) parser.add_argument( """--index_name""" ,default=a_ ,choices=["""exact""", """compressed""", """legacy"""] ,type=a_ ,help="""RAG model retriever type""" ,) parser.add_argument( """--index_path""" ,default=a_ ,type=a_ ,help="""Path to the retrieval index""" ,) parser.add_argument("""--n_docs""" ,default=5 ,type=a_ ,help="""Number of retrieved docs""" ) parser.add_argument( """--model_name_or_path""" ,default=a_ ,type=a_ ,required=a_ ,help="""Path to pretrained checkpoints or model identifier from huggingface.co/models""" ,) parser.add_argument( """--eval_mode""" ,choices=["""e2e""", """retrieval"""] ,default="""e2e""" ,type=a_ ,help=( """Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates""" """ precision@k.""" ) ,) parser.add_argument("""--k""" ,default=1 ,type=a_ ,help="""k for the precision@k calculation""" ) parser.add_argument( """--evaluation_set""" ,default=a_ ,type=a_ ,required=a_ ,help="""Path to a file containing evaluation samples""" ,) parser.add_argument( """--gold_data_path""" ,default=a_ ,type=a_ ,required=a_ ,help="""Path to a tab-separated file with gold samples""" ,) parser.add_argument( """--gold_data_mode""" ,default="""qa""" ,type=a_ ,choices=["""qa""", """ans"""] ,help=( """Format of the gold data file""" """qa - a single line in the following format: question [tab] answer_list""" """ans - a single line of the gold file contains the expected answer string""" ) ,) parser.add_argument( """--predictions_path""" ,type=a_ ,default="""predictions.txt""" ,help="""Name of the predictions file, to be stored in the checkpoints directory""" ,) parser.add_argument( """--eval_all_checkpoints""" ,action="""store_true""" ,help="""Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number""" ,) parser.add_argument( """--eval_batch_size""" ,default=8 ,type=a_ ,help="""Batch size per GPU/CPU for evaluation.""" ,) parser.add_argument( """--recalculate""" ,help="""Recalculate predictions even if the prediction file exists""" ,action="""store_true""" ,) parser.add_argument( """--num_beams""" ,default=4 ,type=a_ ,help="""Number of beams to be used when generating answers""" ,) parser.add_argument("""--min_length""" ,default=1 ,type=a_ ,help="""Min length of the generated answers""" ) parser.add_argument("""--max_length""" ,default=50 ,type=a_ ,help="""Max length of the generated answers""" ) parser.add_argument( """--print_predictions""" ,action="""store_true""" ,help="""If True, prints predictions while evaluating.""" ,) parser.add_argument( """--print_docs""" ,action="""store_true""" ,help="""If True, prints docs retried while generating.""" ,) _lowercase = parser.parse_args() _lowercase = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) return args def __lowerCAmelCase ( _A ): """simple docstring""" _lowercase = {} if args.model_type is None: _lowercase = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith("""rag""" ): _lowercase = RagTokenForGeneration if args.model_type == '''rag_token''' else RagSequenceForGeneration _lowercase = args.n_docs if args.index_name is not None: _lowercase = args.index_name if args.index_path is not None: _lowercase = args.index_path else: _lowercase = BartForConditionalGeneration _lowercase = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info("""Evaluate the following checkpoints: %s""" ,a_ ) _lowercase = get_scores if args.eval_mode == '''e2e''' else get_precision_at_k _lowercase = evaluate_batch_eae if args.eval_mode == '''e2e''' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info("""Calculating metrics based on an existing predictions file: {}""".format(args.predictions_path ) ) score_fn(a_ ,args.predictions_path ,args.gold_data_path ) continue logger.info("""***** Running evaluation for {} *****""".format(a_ ) ) logger.info(""" Batch size = %d""" ,args.eval_batch_size ) logger.info(""" Predictions will be stored under {}""".format(args.predictions_path ) ) if args.model_type.startswith("""rag""" ): _lowercase = RagRetriever.from_pretrained(a_ ,**a_ ) _lowercase = model_class.from_pretrained(a_ ,retriever=a_ ,**a_ ) model.retriever.init_retrieval() else: _lowercase = model_class.from_pretrained(a_ ,**a_ ) model.to(args.device ) with open(args.evaluation_set ,"""r""" ) as eval_file, open(args.predictions_path ,"""w""" ) as preds_file: _lowercase = [] for line in tqdm(a_ ): questions.append(line.strip() ) if len(a_ ) == args.eval_batch_size: _lowercase = evaluate_batch_fn(a_ ,a_ ,a_ ) preds_file.write("""\n""".join(a_ ) + """\n""" ) preds_file.flush() _lowercase = [] if len(a_ ) > 0: _lowercase = evaluate_batch_fn(a_ ,a_ ,a_ ) preds_file.write("""\n""".join(a_ ) ) preds_file.flush() score_fn(a_ ,args.predictions_path ,args.gold_data_path ) if __name__ == "__main__": A_: int = get_args() main(args)
398
"""simple docstring""" import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger A = get_logger(__name__) A = Path(__file__).parent / '''model_card_template.md''' A = uuida().hex A = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES A = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES A = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def __A ( a_ :Union[Dict, str, None] = None) -> str: __a : Union[str, Any] = F"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F"""; torch/{_torch_version}""" if is_flax_available(): ua += F"""; jax/{_jax_version}""" ua += F"""; flax/{_flax_version}""" if is_onnx_available(): ua += F"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''').upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(a_ , a_): ua += "; " + "; ".join(F"""{k}/{v}""" for k, v in user_agent.items()) elif isinstance(a_ , a_): ua += "; " + user_agent return ua def __A ( a_ :str , a_ :Optional[str] = None , a_ :Optional[str] = None) -> Optional[int]: if token is None: __a : Any = HfFolder.get_token() if organization is None: __a : List[Any] = whoami(a_)['''name'''] return F"""{username}/{model_id}""" else: return F"""{organization}/{model_id}""" def __A ( a_ :Union[str, Any] , a_ :List[str]) -> Optional[Any]: if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''') if hasattr(a_ , '''local_rank''') and args.local_rank not in [-1, 0]: return __a : int = args.hub_token if hasattr(a_ , '''hub_token''') else None __a : Any = get_full_repo_name(a_ , token=a_) __a : Tuple = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=a_ , model_name=a_ , repo_name=a_ , dataset_name=args.dataset_name if hasattr(a_ , '''dataset_name''') else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(a_ , '''gradient_accumulation_steps''') else None ) , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta1''') else None , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta2''') else None , adam_weight_decay=args.adam_weight_decay if hasattr(a_ , '''adam_weight_decay''') else None , adam_epsilon=args.adam_epsilon if hasattr(a_ , '''adam_epsilon''') else None , lr_scheduler=args.lr_scheduler if hasattr(a_ , '''lr_scheduler''') else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(a_ , '''lr_warmup_steps''') else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(a_ , '''ema_inv_gamma''') else None , ema_power=args.ema_power if hasattr(a_ , '''ema_power''') else None , ema_max_decay=args.ema_max_decay if hasattr(a_ , '''ema_max_decay''') else None , mixed_precision=args.mixed_precision , ) __a : List[Any] = os.path.join(args.output_dir , '''README.md''') model_card.save(a_) def __A ( a_ :Optional[str] , a_ :Optional[str] = None) -> Union[str, Any]: if resolved_file is None or commit_hash is not None: return commit_hash __a : Any = str(Path(a_).as_posix()) __a : Optional[int] = re.search(R'''snapshots/([^/]+)/''' , a_) if search is None: return None __a : Dict = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(a_) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. A = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) A = os.path.join(hf_cache_home, '''diffusers''') def __A ( a_ :Optional[str] = None , a_ :Optional[str] = None) -> None: if new_cache_dir is None: __a : Dict = DIFFUSERS_CACHE if old_cache_dir is None: __a : List[Any] = old_diffusers_cache __a : Union[str, Any] = Path(a_).expanduser() __a : Dict = Path(a_).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*'''): if old_blob_path.is_file() and not old_blob_path.is_symlink(): __a : List[Any] = new_cache_dir / old_blob_path.relative_to(a_) new_blob_path.parent.mkdir(parents=a_ , exist_ok=a_) os.replace(a_ , a_) try: os.symlink(a_ , a_) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''') # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). A = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): A = 0 else: with open(cache_version_file) as f: try: A = int(f.read()) except ValueError: A = 0 if cache_version < 1: A = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: A = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F'There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F'There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ' '''the directory exists and can be written to.''' ) def __A ( a_ :str , a_ :Optional[str] = None) -> str: if variant is not None: __a : Dict = weights_name.split('''.''') __a : List[Any] = splits[:-1] + [variant] + splits[-1:] __a : Tuple = '''.'''.join(a_) return weights_name def __A ( a_ :List[Any] , *, a_ :Union[str, Any] , a_ :Dict , a_ :Union[str, Any] , a_ :Optional[int] , a_ :str , a_ :Any , a_ :str , a_ :Optional[int] , a_ :str , a_ :Tuple , a_ :List[str]=None , ) -> Dict: __a : int = str(a_) if os.path.isfile(a_): return pretrained_model_name_or_path elif os.path.isdir(a_): if os.path.isfile(os.path.join(a_ , a_)): # Load from a PyTorch checkpoint __a : Union[str, Any] = os.path.join(a_ , a_) return model_file elif subfolder is not None and os.path.isfile( os.path.join(a_ , a_ , a_)): __a : Optional[Any] = os.path.join(a_ , a_ , a_) return model_file else: raise EnvironmentError( F"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""") else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(a_).base_version) >= version.parse('''0.20.0''') ): try: __a : Any = hf_hub_download( a_ , filename=_add_variant(a_ , a_) , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) warnings.warn( F"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.""" , a_ , ) return model_file except: # noqa: E722 warnings.warn( F"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(a_ , a_)} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(a_ , a_)}' so that the correct variant file can be added.""" , a_ , ) try: # 2. Load model file as usual __a : Optional[Any] = hf_hub_download( a_ , filename=a_ , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''') except RevisionNotFoundError: raise EnvironmentError( F"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ '''this model name. Check the model page at ''' F"""'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions.""") except EntryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""") except HTTPError as err: raise EnvironmentError( F"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""") except ValueError: raise EnvironmentError( F"""We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it""" F""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" F""" directory containing a file named {weights_name} or""" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''') except EnvironmentError: raise EnvironmentError( F"""Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from """ '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F"""Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory """ F"""containing a file named {weights_name}""")
52
0
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ) -> List[str]: if isinstance(a_ , a_ ) and isinstance(a_ , a_ ): __lowerCamelCase = len(set_a.intersection(a_ ) ) if alternative_union: __lowerCamelCase = len(a_ ) + len(a_ ) else: __lowerCamelCase = len(set_a.union(a_ ) ) return intersection / union if isinstance(a_ , (list, tuple) ) and isinstance(a_ , (list, tuple) ): __lowerCamelCase = [element for element in set_a if element in set_b] if alternative_union: __lowerCamelCase = len(a_ ) + len(a_ ) return len(a_ ) / union else: __lowerCamelCase = set_a + [element for element in set_b if element not in set_a] return len(a_ ) / len(a_ ) return len(a_ ) / len(a_ ) return None if __name__ == "__main__": __UpperCAmelCase ={"a", "b", "c", "d", "e"} __UpperCAmelCase ={"c", "d", "e", "f", "h", "i"} print(jaccard_similarity(set_a, set_b))
546
"""simple docstring""" import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''kakaobrain/align-base''': '''https://huggingface.co/kakaobrain/align-base/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_text_model''' def __init__( self , _UpperCAmelCase=30522 , _UpperCAmelCase=768 , _UpperCAmelCase=12 , _UpperCAmelCase=12 , _UpperCAmelCase=3072 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0 , _UpperCAmelCase="absolute" , _UpperCAmelCase=True , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = vocab_size __a : Optional[int] = hidden_size __a : Dict = num_hidden_layers __a : List[Any] = num_attention_heads __a : Optional[int] = hidden_act __a : List[Any] = intermediate_size __a : List[Any] = hidden_dropout_prob __a : List[str] = attention_probs_dropout_prob __a : Optional[int] = max_position_embeddings __a : List[str] = type_vocab_size __a : Tuple = initializer_range __a : Dict = layer_norm_eps __a : Any = position_embedding_type __a : Dict = use_cache __a : Dict = pad_token_id @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : List[str] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the text config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Dict = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_vision_model''' def __init__( self , _UpperCAmelCase = 3 , _UpperCAmelCase = 600 , _UpperCAmelCase = 2.0 , _UpperCAmelCase = 3.1 , _UpperCAmelCase = 8 , _UpperCAmelCase = [3, 3, 5, 3, 5, 5, 3] , _UpperCAmelCase = [32, 16, 24, 40, 80, 112, 192] , _UpperCAmelCase = [16, 24, 40, 80, 112, 192, 320] , _UpperCAmelCase = [] , _UpperCAmelCase = [1, 2, 2, 2, 1, 2, 1] , _UpperCAmelCase = [1, 2, 2, 3, 3, 4, 1] , _UpperCAmelCase = [1, 6, 6, 6, 6, 6, 6] , _UpperCAmelCase = 0.2_5 , _UpperCAmelCase = "swish" , _UpperCAmelCase = 2560 , _UpperCAmelCase = "mean" , _UpperCAmelCase = 0.0_2 , _UpperCAmelCase = 0.0_0_1 , _UpperCAmelCase = 0.9_9 , _UpperCAmelCase = 0.2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : Tuple = num_channels __a : str = image_size __a : List[Any] = width_coefficient __a : Optional[int] = depth_coefficient __a : Union[str, Any] = depth_divisor __a : int = kernel_sizes __a : Dict = in_channels __a : List[str] = out_channels __a : Any = depthwise_padding __a : str = strides __a : Optional[Any] = num_block_repeats __a : Optional[Any] = expand_ratios __a : Any = squeeze_expansion_ratio __a : int = hidden_act __a : Union[str, Any] = hidden_dim __a : Union[str, Any] = pooling_type __a : Tuple = initializer_range __a : List[str] = batch_norm_eps __a : List[Any] = batch_norm_momentum __a : Union[str, Any] = drop_connect_rate __a : List[Any] = sum(_UpperCAmelCase ) * 4 @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : Optional[Any] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the vision config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Optional[Any] = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align''' __lowerCAmelCase = True def __init__( self , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=640 , _UpperCAmelCase=1.0 , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) if text_config is None: __a : Dict = {} logger.info('''text_config is None. Initializing the AlignTextConfig with default values.''' ) if vision_config is None: __a : Any = {} logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.''' ) __a : Any = AlignTextConfig(**_UpperCAmelCase ) __a : Any = AlignVisionConfig(**_UpperCAmelCase ) __a : Optional[int] = projection_dim __a : Union[str, Any] = temperature_init_value __a : int = initializer_range @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = copy.deepcopy(self.__dict__ ) __a : Tuple = self.text_config.to_dict() __a : Union[str, Any] = self.vision_config.to_dict() __a : int = self.__class__.model_type return output
52
0
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class lowercase ( _UpperCamelCase ): def __init__( self , snake_case , snake_case , snake_case = None , snake_case = None , snake_case = False , **snake_case , ): super().__init__(features=_UpperCAmelCase , cache_dir=_UpperCAmelCase , keep_in_memory=_UpperCAmelCase , **_UpperCAmelCase ) snake_case_ = Sql( cache_dir=_UpperCAmelCase , features=_UpperCAmelCase , sql=_UpperCAmelCase , con=_UpperCAmelCase , **_UpperCAmelCase , ) def a ( self ): snake_case_ = None snake_case_ = None snake_case_ = None snake_case_ = None self.builder.download_and_prepare( download_config=_UpperCAmelCase , download_mode=_UpperCAmelCase , verification_mode=_UpperCAmelCase , base_path=_UpperCAmelCase , ) # Build dataset for splits snake_case_ = self.builder.as_dataset( split='train' , verification_mode=_UpperCAmelCase , in_memory=self.keep_in_memory ) return dataset class lowercase : def __init__( self , snake_case , snake_case , snake_case , snake_case = None , snake_case = None , **snake_case , ): if num_proc is not None and num_proc <= 0: raise ValueError(F'''num_proc {num_proc} must be an integer > 0.''' ) snake_case_ = dataset snake_case_ = name snake_case_ = con snake_case_ = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE snake_case_ = num_proc snake_case_ = to_sql_kwargs def a ( self ): snake_case_ = self.to_sql_kwargs.pop('sql' , _UpperCAmelCase ) snake_case_ = self.to_sql_kwargs.pop('con' , _UpperCAmelCase ) snake_case_ = self.to_sql_kwargs.pop('index' , _UpperCAmelCase ) snake_case_ = self._write(index=_UpperCAmelCase , **self.to_sql_kwargs ) return written def a ( self , snake_case ): snake_case_ = args snake_case_ = {**to_sql_kwargs, '''if_exists''': '''append'''} if offset > 0 else to_sql_kwargs snake_case_ = query_table( table=self.dataset.data , key=slice(_UpperCAmelCase , offset + self.batch_size ) , indices=self.dataset._indices , ) snake_case_ = batch.to_pandas() snake_case_ = df.to_sql(self.name , self.con , index=_UpperCAmelCase , **_UpperCAmelCase ) return num_rows or len(_UpperCAmelCase ) def a ( self , snake_case , **snake_case ): snake_case_ = 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 SQL from Arrow format' , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: snake_case_ = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , _UpperCAmelCase , _UpperCAmelCase )] , ) , 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 SQL from Arrow format' , ): written += num_rows return written
362
"""simple docstring""" from __future__ import annotations from random import choice def __A ( a_ :Tuple) -> List[str]: return choice(a_) def __A ( a_ :list[int] , a_ :int) -> int: __a : Optional[int] = random_pivot(a_) # partition based on pivot # linear time __a : Union[str, Any] = [e for e in lst if e < pivot] __a : Any = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(a_) == k - 1: return pivot # pivot is in elements bigger than k elif len(a_) < k - 1: return kth_number(a_ , k - len(a_) - 1) # pivot is in elements smaller than k else: return kth_number(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" import tensorflow as tf from ...tf_utils import shape_list class a ( tf.keras.layers.Layer ): def __init__( self : Optional[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[Any]=1 , __lowerCAmelCase : Any=False , **__lowerCAmelCase : Union[str, Any] ): super().__init__(**_UpperCAmelCase ) _UpperCAmelCase = vocab_size _UpperCAmelCase = d_embed _UpperCAmelCase = d_proj _UpperCAmelCase = cutoffs + [vocab_size] _UpperCAmelCase = [0] + self.cutoffs _UpperCAmelCase = div_val _UpperCAmelCase = self.cutoffs[0] _UpperCAmelCase = len(self.cutoffs ) - 1 _UpperCAmelCase = self.shortlist_size + self.n_clusters _UpperCAmelCase = keep_order _UpperCAmelCase = [] _UpperCAmelCase = [] def lowerCAmelCase_ ( self : Any , __lowerCAmelCase : str ): if self.n_clusters > 0: _UpperCAmelCase = self.add_weight( shape=(self.n_clusters, self.d_embed) , initializer="""zeros""" , trainable=_UpperCAmelCase , name="""cluster_weight""" ) _UpperCAmelCase = self.add_weight( shape=(self.n_clusters,) , initializer="""zeros""" , trainable=_UpperCAmelCase , name="""cluster_bias""" ) if self.div_val == 1: for i in range(len(self.cutoffs ) ): if self.d_proj != self.d_embed: _UpperCAmelCase = self.add_weight( shape=(self.d_embed, self.d_proj) , initializer="""zeros""" , trainable=_UpperCAmelCase , name=f'''out_projs_._{i}''' , ) self.out_projs.append(_UpperCAmelCase ) else: self.out_projs.append(_UpperCAmelCase ) _UpperCAmelCase = self.add_weight( shape=(self.vocab_size, self.d_embed) , initializer="""zeros""" , trainable=_UpperCAmelCase , name=f'''out_layers_._{i}_._weight''' , ) _UpperCAmelCase = self.add_weight( shape=(self.vocab_size,) , initializer="""zeros""" , trainable=_UpperCAmelCase , name=f'''out_layers_._{i}_._bias''' , ) self.out_layers.append((weight, bias) ) else: for i in range(len(self.cutoffs ) ): _UpperCAmelCase = self.cutoff_ends[i], self.cutoff_ends[i + 1] _UpperCAmelCase = self.d_embed // (self.div_val**i) _UpperCAmelCase = self.add_weight( shape=(d_emb_i, self.d_proj) , initializer="""zeros""" , trainable=_UpperCAmelCase , name=f'''out_projs_._{i}''' ) self.out_projs.append(_UpperCAmelCase ) _UpperCAmelCase = self.add_weight( shape=(r_idx - l_idx, d_emb_i) , initializer="""zeros""" , trainable=_UpperCAmelCase , name=f'''out_layers_._{i}_._weight''' , ) _UpperCAmelCase = self.add_weight( shape=(r_idx - l_idx,) , initializer="""zeros""" , trainable=_UpperCAmelCase , name=f'''out_layers_._{i}_._bias''' , ) self.out_layers.append((weight, bias) ) super().build(_UpperCAmelCase ) @staticmethod def lowerCAmelCase_ ( __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Tuple , __lowerCAmelCase : List[str]=None ): _UpperCAmelCase = x if proj is not None: _UpperCAmelCase = tf.einsum("""ibd,ed->ibe""" , _UpperCAmelCase , _UpperCAmelCase ) return tf.einsum("""ibd,nd->ibn""" , _UpperCAmelCase , _UpperCAmelCase ) + b @staticmethod def lowerCAmelCase_ ( __lowerCAmelCase : Optional[int] , __lowerCAmelCase : int ): _UpperCAmelCase = shape_list(_UpperCAmelCase ) _UpperCAmelCase = tf.range(lp_size[0] , dtype=target.dtype ) _UpperCAmelCase = tf.stack([r, target] , 1 ) return tf.gather_nd(_UpperCAmelCase , _UpperCAmelCase ) def lowerCAmelCase_ ( self : str , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : Dict=False ): _UpperCAmelCase = 0 if self.n_clusters == 0: _UpperCAmelCase = self._logit(_UpperCAmelCase , self.out_layers[0][0] , self.out_layers[0][1] , self.out_projs[0] ) if target is not None: _UpperCAmelCase = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=_UpperCAmelCase , logits=_UpperCAmelCase ) _UpperCAmelCase = tf.nn.log_softmax(_UpperCAmelCase , axis=-1 ) else: _UpperCAmelCase = shape_list(_UpperCAmelCase ) _UpperCAmelCase = [] _UpperCAmelCase = tf.zeros(hidden_sizes[:2] ) for i in range(len(self.cutoffs ) ): _UpperCAmelCase = self.cutoff_ends[i], self.cutoff_ends[i + 1] if target is not None: _UpperCAmelCase = (target >= l_idx) & (target < r_idx) _UpperCAmelCase = tf.where(_UpperCAmelCase ) _UpperCAmelCase = tf.boolean_mask(_UpperCAmelCase , _UpperCAmelCase ) - l_idx if self.div_val == 1: _UpperCAmelCase = self.out_layers[0][0][l_idx:r_idx] _UpperCAmelCase = self.out_layers[0][1][l_idx:r_idx] else: _UpperCAmelCase = self.out_layers[i][0] _UpperCAmelCase = self.out_layers[i][1] if i == 0: _UpperCAmelCase = tf.concat([cur_W, self.cluster_weight] , 0 ) _UpperCAmelCase = tf.concat([cur_b, self.cluster_bias] , 0 ) _UpperCAmelCase = self._logit(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , self.out_projs[0] ) _UpperCAmelCase = tf.nn.log_softmax(_UpperCAmelCase ) out.append(head_logprob[..., : self.cutoffs[0]] ) if target is not None: _UpperCAmelCase = tf.boolean_mask(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase = self._gather_logprob(_UpperCAmelCase , _UpperCAmelCase ) else: _UpperCAmelCase = self._logit(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , self.out_projs[i] ) _UpperCAmelCase = tf.nn.log_softmax(_UpperCAmelCase ) _UpperCAmelCase = self.cutoffs[0] + i - 1 # No probability for the head cluster _UpperCAmelCase = head_logprob[..., cluster_prob_idx, None] + tail_logprob out.append(_UpperCAmelCase ) if target is not None: _UpperCAmelCase = tf.boolean_mask(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase = tf.boolean_mask(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase = self._gather_logprob(_UpperCAmelCase , _UpperCAmelCase ) cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1] if target is not None: loss += tf.scatter_nd(_UpperCAmelCase , -cur_logprob , shape_list(_UpperCAmelCase ) ) _UpperCAmelCase = tf.concat(_UpperCAmelCase , axis=-1 ) if target is not None: if return_mean: _UpperCAmelCase = tf.reduce_mean(_UpperCAmelCase ) # Add the training-time loss value to the layer using `self.add_loss()`. self.add_loss(_UpperCAmelCase ) # Log the loss as a metric (we could log arbitrary metrics, # including different metrics for training and inference. self.add_metric(_UpperCAmelCase , name=self.name , aggregation="""mean""" if return_mean else """""" ) return out
277
"""simple docstring""" import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A = logging.getLogger(__name__) def __A ( a_ :Union[str, Any] , a_ :Dict) -> Union[str, Any]: __a : Optional[int] = np.argmax(a_ , axis=1) return np.sum(outputs == labels) def __A ( a_ :Any) -> str: with open(a_ , encoding='''utf_8''') as f: __a : List[Any] = csv.reader(a_) __a : List[str] = [] next(a_) # skip the first line for line in tqdm(a_): output.append((''' '''.join(line[1:5]), line[5], line[6], int(line[-1]) - 1)) return output def __A ( a_ :Dict , a_ :str , a_ :str , a_ :List[Any] , a_ :Tuple , a_ :List[Any]) -> Any: __a : List[str] = [] for dataset in encoded_datasets: __a : List[str] = len(a_) __a : List[str] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa) __a : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa) __a : Tuple = np.full((n_batch, 2, input_len) , fill_value=-1_00 , dtype=np.intaa) __a : Optional[Any] = np.zeros((n_batch,) , dtype=np.intaa) for ( i, (story, conta, conta, mc_label), ) in enumerate(a_): __a : str = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __a : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __a : Tuple = with_conta __a : int = with_conta __a : List[str] = len(a_) - 1 __a : int = len(a_) - 1 __a : Optional[int] = with_conta __a : Tuple = with_conta __a : List[Any] = mc_label __a : Any = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(a_) for t in all_inputs)) return tensor_datasets def __A ( ) -> Union[str, Any]: __a : List[str] = argparse.ArgumentParser() parser.add_argument('''--model_name''' , type=a_ , default='''openai-gpt''' , help='''pretrained model name''') parser.add_argument('''--do_train''' , action='''store_true''' , help='''Whether to run training.''') parser.add_argument('''--do_eval''' , action='''store_true''' , help='''Whether to run eval on the dev set.''') parser.add_argument( '''--output_dir''' , default=a_ , type=a_ , required=a_ , help='''The output directory where the model predictions and checkpoints will be written.''' , ) parser.add_argument('''--train_dataset''' , type=a_ , default='''''') parser.add_argument('''--eval_dataset''' , type=a_ , default='''''') parser.add_argument('''--seed''' , type=a_ , default=42) parser.add_argument('''--num_train_epochs''' , type=a_ , default=3) parser.add_argument('''--train_batch_size''' , type=a_ , default=8) parser.add_argument('''--eval_batch_size''' , type=a_ , default=16) parser.add_argument('''--adam_epsilon''' , default=1e-8 , type=a_ , help='''Epsilon for Adam optimizer.''') parser.add_argument('''--max_grad_norm''' , type=a_ , default=1) parser.add_argument( '''--max_steps''' , default=-1 , type=a_ , help=( '''If > 0: set total number of training steps to perform. Override num_train_epochs.''' ) , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=a_ , default=1 , help='''Number of updates steps to accumulate before performing a backward/update pass.''' , ) parser.add_argument('''--learning_rate''' , type=a_ , default=6.25e-5) parser.add_argument('''--warmup_steps''' , default=0 , type=a_ , help='''Linear warmup over warmup_steps.''') parser.add_argument('''--lr_schedule''' , type=a_ , default='''warmup_linear''') parser.add_argument('''--weight_decay''' , type=a_ , default=0.0_1) parser.add_argument('''--lm_coef''' , type=a_ , default=0.9) parser.add_argument('''--n_valid''' , type=a_ , default=3_74) parser.add_argument('''--server_ip''' , type=a_ , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=a_ , default='''''' , help='''Can be used for distant debugging.''') __a : str = parser.parse_args() print(a_) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=a_) ptvsd.wait_for_attach() random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) __a : Tuple = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''') __a : str = torch.cuda.device_count() logger.info('''device: {}, n_gpu {}'''.format(a_ , a_)) if not args.do_train and not args.do_eval: raise ValueError('''At least one of `do_train` or `do_eval` must be True.''') if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset __a : List[str] = ['''_start_''', '''_delimiter_''', '''_classify_'''] __a : Union[str, Any] = OpenAIGPTTokenizer.from_pretrained(args.model_name) tokenizer.add_tokens(a_) __a : Union[str, Any] = tokenizer.convert_tokens_to_ids(a_) __a : Optional[Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name) model.resize_token_embeddings(len(a_)) model.to(a_) # Load and encode the datasets def tokenize_and_encode(a_ :List[Any]): if isinstance(a_ , a_): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(a_)) elif isinstance(a_ , a_): return obj return [tokenize_and_encode(a_) for o in obj] logger.info('''Encoding dataset...''') __a : Dict = load_rocstories_dataset(args.train_dataset) __a : int = load_rocstories_dataset(args.eval_dataset) __a : Optional[int] = (train_dataset, eval_dataset) __a : List[Any] = tokenize_and_encode(a_) # Compute the max input length for the Transformer __a : List[Any] = model.config.n_positions // 2 - 2 __a : int = max( len(story[:max_length]) + max(len(conta[:max_length]) , len(conta[:max_length])) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset) __a : Union[str, Any] = min(a_ , model.config.n_positions) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders __a : Tuple = pre_process_datasets(a_ , a_ , a_ , *a_) __a , __a : Tuple = tensor_datasets[0], tensor_datasets[1] __a : List[str] = TensorDataset(*a_) __a : Optional[Any] = RandomSampler(a_) __a : str = DataLoader(a_ , sampler=a_ , batch_size=args.train_batch_size) __a : List[str] = TensorDataset(*a_) __a : Optional[int] = SequentialSampler(a_) __a : Optional[Any] = DataLoader(a_ , sampler=a_ , batch_size=args.eval_batch_size) # Prepare optimizer if args.do_train: if args.max_steps > 0: __a : int = args.max_steps __a : Optional[int] = args.max_steps // (len(a_) // args.gradient_accumulation_steps) + 1 else: __a : str = len(a_) // args.gradient_accumulation_steps * args.num_train_epochs __a : List[Any] = list(model.named_parameters()) __a : Optional[int] = ['''bias''', '''LayerNorm.bias''', '''LayerNorm.weight'''] __a : List[str] = [ { '''params''': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], '''weight_decay''': args.weight_decay, }, {'''params''': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], '''weight_decay''': 0.0}, ] __a : int = AdamW(a_ , lr=args.learning_rate , eps=args.adam_epsilon) __a : Union[str, Any] = get_linear_schedule_with_warmup( a_ , num_warmup_steps=args.warmup_steps , num_training_steps=a_) if args.do_train: __a , __a , __a : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs) , desc='''Epoch'''): __a : Dict = 0 __a : Dict = 0 __a : List[str] = tqdm(a_ , desc='''Training''') for step, batch in enumerate(a_): __a : Dict = tuple(t.to(a_) for t in batch) __a , __a , __a , __a : str = batch __a : List[Any] = model(a_ , mc_token_ids=a_ , lm_labels=a_ , mc_labels=a_) __a : Optional[Any] = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() __a : int = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 __a : Tuple = '''Training loss: {:.2e} lr: {:.2e}'''.format(a_ , scheduler.get_lr()[0]) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer __a : Dict = model.module if hasattr(a_ , '''module''') else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` __a : int = os.path.join(args.output_dir , a_) __a : str = os.path.join(args.output_dir , a_) torch.save(model_to_save.state_dict() , a_) model_to_save.config.to_json_file(a_) tokenizer.save_vocabulary(args.output_dir) # Load a trained model and vocabulary that you have fine-tuned __a : str = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir) __a : Union[str, Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir) model.to(a_) if args.do_eval: model.eval() __a , __a : List[Any] = 0, 0 __a , __a : Union[str, Any] = 0, 0 for batch in tqdm(a_ , desc='''Evaluating'''): __a : str = tuple(t.to(a_) for t in batch) __a , __a , __a , __a : List[Any] = batch with torch.no_grad(): __a , __a , __a , __a : str = model( a_ , mc_token_ids=a_ , lm_labels=a_ , mc_labels=a_) __a : List[str] = mc_logits.detach().cpu().numpy() __a : Optional[Any] = mc_labels.to('''cpu''').numpy() __a : str = accuracy(a_ , a_) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0) nb_eval_steps += 1 __a : Tuple = eval_loss / nb_eval_steps __a : List[str] = eval_accuracy / nb_eval_examples __a : List[Any] = tr_loss / nb_tr_steps if args.do_train else None __a : List[str] = {'''eval_loss''': eval_loss, '''eval_accuracy''': eval_accuracy, '''train_loss''': train_loss} __a : Dict = os.path.join(args.output_dir , '''eval_results.txt''') with open(a_ , '''w''') as writer: logger.info('''***** Eval results *****''') for key in sorted(result.keys()): logger.info(''' %s = %s''' , a_ , str(result[key])) writer.write('''%s = %s\n''' % (key, str(result[key]))) if __name__ == "__main__": main()
52
0
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 lowerCAmelCase_ ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' _lowercase = ProphetNetTokenizer _lowercase = False def __lowerCamelCase ( self ): super().setUp() SCREAMING_SNAKE_CASE_ : List[Any] =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE_ : str =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def __lowerCamelCase ( self , __UpperCAmelCase ): SCREAMING_SNAKE_CASE_ : List[str] ='''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE_ : List[Any] ='''unwanted, running''' return input_text, output_text def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] =self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE_ : Any =tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(_UpperCAmelCase , ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [9, 6, 7, 12, 10, 11] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Any =BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ) , ['ah', '\u535A', '\u63A8', 'zz'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] =BasicTokenizer(do_lower_case=_UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['hello', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : List[Any] =BasicTokenizer(do_lower_case=_UpperCAmelCase , strip_accents=_UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hällo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['h\u00E9llo'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] =BasicTokenizer(do_lower_case=_UpperCAmelCase , strip_accents=_UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : int =BasicTokenizer(do_lower_case=_UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : List[Any] =BasicTokenizer(do_lower_case=_UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] =BasicTokenizer(do_lower_case=_UpperCAmelCase , strip_accents=_UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HäLLo', '!', 'how', 'Are', 'yoU', '?'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : List[Any] =BasicTokenizer(do_lower_case=_UpperCAmelCase , strip_accents=_UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HaLLo', '!', 'how', 'Are', 'yoU', '?'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Any =BasicTokenizer(do_lower_case=_UpperCAmelCase , never_split=['[UNK]'] ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]'] ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Tuple =['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing'''] SCREAMING_SNAKE_CASE_ : str ={} for i, token in enumerate(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_ : Tuple =i SCREAMING_SNAKE_CASE_ : Dict =WordpieceTokenizer(vocab=_UpperCAmelCase , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('unwanted running' ) , ['un', '##want', '##ed', 'runn', '##ing'] ) self.assertListEqual(tokenizer.tokenize('unwantedX running' ) , ['[UNK]', 'runn', '##ing'] ) @require_torch def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] =self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) SCREAMING_SNAKE_CASE_ : Tuple =['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] SCREAMING_SNAKE_CASE_ : List[Any] =[1_037, 2_146, 20_423, 2_005, 7_680, 7_849, 3_989, 1_012, 102] SCREAMING_SNAKE_CASE_ : List[str] =tokenizer(_UpperCAmelCase , padding=_UpperCAmelCase , return_tensors='pt' ) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_ : Dict =list(batch.input_ids.numpy()[0] ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) def __lowerCamelCase ( 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 __lowerCamelCase ( 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 __lowerCamelCase ( 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 __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : Tuple =self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) SCREAMING_SNAKE_CASE_ : List[Any] =tokenizer.encode('sequence builders' , add_special_tokens=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ : Any =tokenizer.encode('multi-sequence build' , add_special_tokens=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ : Optional[Any] =tokenizer.build_inputs_with_special_tokens(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ : List[str] =tokenizer.build_inputs_with_special_tokens(_UpperCAmelCase , _UpperCAmelCase ) assert encoded_sentence == text + [102] assert encoded_pair == text + [102] + text_a + [102]
220
"""simple docstring""" import unittest import numpy as np from transformers import RobertaConfig, 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.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=13 , _UpperCAmelCase=7 , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=99 , _UpperCAmelCase=32 , _UpperCAmelCase=5 , _UpperCAmelCase=4 , _UpperCAmelCase=37 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=16 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=4 , ): __a : Any = parent __a : Optional[int] = batch_size __a : str = seq_length __a : List[str] = is_training __a : Optional[Any] = use_attention_mask __a : Optional[Any] = use_token_type_ids __a : List[str] = use_labels __a : Union[str, Any] = vocab_size __a : int = hidden_size __a : Union[str, Any] = num_hidden_layers __a : Union[str, Any] = num_attention_heads __a : Dict = intermediate_size __a : List[str] = hidden_act __a : Dict = hidden_dropout_prob __a : Union[str, Any] = attention_probs_dropout_prob __a : int = max_position_embeddings __a : Tuple = type_vocab_size __a : Optional[int] = type_sequence_label_size __a : Optional[Any] = initializer_range __a : Optional[int] = num_choices def _lowerCamelCase ( self ): __a : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a : Union[str, Any] = None if self.use_attention_mask: __a : Any = random_attention_mask([self.batch_size, self.seq_length] ) __a : Optional[int] = None if self.use_token_type_ids: __a : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a : Any = RobertaConfig( 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 _lowerCamelCase ( self ): __a : Dict = self.prepare_config_and_inputs() __a , __a , __a , __a : str = config_and_inputs __a : str = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def _lowerCamelCase ( self ): __a : Any = self.prepare_config_and_inputs() __a , __a , __a , __a : Union[str, Any] = config_and_inputs __a : Optional[int] = True __a : str = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = True __lowerCAmelCase = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCamelCase ( self ): __a : Dict = FlaxRobertaModelTester(self ) @slow def _lowerCamelCase ( self ): for model_class_name in self.all_model_classes: __a : int = model_class_name.from_pretrained('''roberta-base''' , from_pt=_UpperCAmelCase ) __a : List[str] = model(np.ones((1, 1) ) ) self.assertIsNotNone(_UpperCAmelCase )
52
0
def __SCREAMING_SNAKE_CASE ( a__ : Optional[int] ) -> int: __A : int = 0 __A : Optional[int] = len(a_ ) for i in range(n - 1 ): for j in range(i + 1 ,a_ ): if arr[i] > arr[j]: num_inversions += 1 return num_inversions def __SCREAMING_SNAKE_CASE ( a__ : str ) -> List[str]: if len(a_ ) <= 1: return arr, 0 __A : Optional[Any] = len(a_ ) // 2 __A : str = arr[0:mid] __A : List[Any] = arr[mid:] __A : List[str] = count_inversions_recursive(a_ ) __A : str = count_inversions_recursive(a_ ) __A : int = _count_cross_inversions(a_ ,a_ ) __A : List[str] = inversion_p + inversions_q + cross_inversions return c, num_inversions def __SCREAMING_SNAKE_CASE ( a__ : Tuple ,a__ : List[str] ) -> str: __A : List[str] = [] __A : Dict = 0 while i < len(a_ ) and j < len(a_ ): if p[i] > q[j]: # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. num_inversion += len(a_ ) - i r.append(q[j] ) j += 1 else: r.append(p[i] ) i += 1 if i < len(a_ ): r.extend(p[i:] ) else: r.extend(q[j:] ) return r, num_inversion def __SCREAMING_SNAKE_CASE ( ) -> Any: __A : Dict = [10, 2, 1, 5, 5, 2, 11] # this arr has 8 inversions: # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) __A : Any = count_inversions_bf(a_ ) __A : Dict = count_inversions_recursive(a_ ) assert num_inversions_bf == num_inversions_recursive == 8 print("""number of inversions = """ ,a_ ) # testing an array with zero inversion (a sorted arr_1) arr_a.sort() __A : str = count_inversions_bf(a_ ) __A : List[Any] = count_inversions_recursive(a_ ) assert num_inversions_bf == num_inversions_recursive == 0 print("""number of inversions = """ ,a_ ) # an empty list should also have zero inversions __A : Dict = [] __A : List[Any] = count_inversions_bf(a_ ) __A : Tuple = count_inversions_recursive(a_ ) assert num_inversions_bf == num_inversions_recursive == 0 print("""number of inversions = """ ,a_ ) if __name__ == "__main__": main()
17
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''facebook/levit-128S''': '''https://huggingface.co/facebook/levit-128S/resolve/main/config.json''', # See all LeViT models at https://huggingface.co/models?filter=levit } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''levit''' def __init__( self , _UpperCAmelCase=224 , _UpperCAmelCase=3 , _UpperCAmelCase=3 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=16 , _UpperCAmelCase=[128, 256, 384] , _UpperCAmelCase=[4, 8, 12] , _UpperCAmelCase=[4, 4, 4] , _UpperCAmelCase=[16, 16, 16] , _UpperCAmelCase=0 , _UpperCAmelCase=[2, 2, 2] , _UpperCAmelCase=[2, 2, 2] , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = image_size __a : List[Any] = num_channels __a : Dict = kernel_size __a : Optional[int] = stride __a : Optional[int] = padding __a : Dict = hidden_sizes __a : int = num_attention_heads __a : Optional[int] = depths __a : str = key_dim __a : Union[str, Any] = drop_path_rate __a : Optional[Any] = patch_size __a : Tuple = attention_ratio __a : int = mlp_ratio __a : int = initializer_range __a : int = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = version.parse('''1.11''' ) @property def _lowerCamelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowerCamelCase ( self ): return 1e-4
52
0
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase A__ : str = logging.get_logger(__name__) A__ : Optional[Any] = { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json""", """allenai/longformer-large-4096""": """https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json""", """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json""" ), } class lowercase ( _UpperCamelCase ): __a = """longformer""" def __init__( self , SCREAMING_SNAKE_CASE__ = 512 , SCREAMING_SNAKE_CASE__ = 2 , SCREAMING_SNAKE_CASE__ = 1 , SCREAMING_SNAKE_CASE__ = 0 , SCREAMING_SNAKE_CASE__ = 2 , SCREAMING_SNAKE_CASE__ = 30522 , SCREAMING_SNAKE_CASE__ = 768 , SCREAMING_SNAKE_CASE__ = 12 , SCREAMING_SNAKE_CASE__ = 12 , SCREAMING_SNAKE_CASE__ = 3072 , SCREAMING_SNAKE_CASE__ = "gelu" , SCREAMING_SNAKE_CASE__ = 0.1 , SCREAMING_SNAKE_CASE__ = 0.1 , SCREAMING_SNAKE_CASE__ = 512 , SCREAMING_SNAKE_CASE__ = 2 , SCREAMING_SNAKE_CASE__ = 0.02 , SCREAMING_SNAKE_CASE__ = 1E-12 , SCREAMING_SNAKE_CASE__ = False , **SCREAMING_SNAKE_CASE__ , ): """simple docstring""" super().__init__(pad_token_id=_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase__ : Union[str, Any] = attention_window lowerCAmelCase__ : List[Any] = sep_token_id lowerCAmelCase__ : Optional[Any] = bos_token_id lowerCAmelCase__ : List[str] = eos_token_id lowerCAmelCase__ : List[Any] = vocab_size lowerCAmelCase__ : Optional[Any] = hidden_size lowerCAmelCase__ : Union[str, Any] = num_hidden_layers lowerCAmelCase__ : Optional[int] = num_attention_heads lowerCAmelCase__ : Optional[Any] = hidden_act lowerCAmelCase__ : Dict = intermediate_size lowerCAmelCase__ : Dict = hidden_dropout_prob lowerCAmelCase__ : Optional[int] = attention_probs_dropout_prob lowerCAmelCase__ : Tuple = max_position_embeddings lowerCAmelCase__ : List[Any] = type_vocab_size lowerCAmelCase__ : int = initializer_range lowerCAmelCase__ : Optional[int] = layer_norm_eps lowerCAmelCase__ : str = onnx_export class lowercase ( _UpperCamelCase ): def __init__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = "default" , SCREAMING_SNAKE_CASE__ = None ): """simple docstring""" super().__init__(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase__ : List[Any] = True @property def lowercase_ ( self ): """simple docstring""" if self.task == "multiple-choice": lowerCAmelCase__ : List[str] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: lowerCAmelCase__ : Tuple = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''global_attention_mask''', dynamic_axis), ] ) @property def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : str = super().outputs if self.task == "default": lowerCAmelCase__ : Optional[Any] = {0: '''batch'''} return outputs @property def lowercase_ ( self ): """simple docstring""" return 1E-4 @property def lowercase_ ( self ): """simple docstring""" return max(super().default_onnx_opset , 14 ) def lowercase_ ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = None , ): """simple docstring""" lowerCAmelCase__ : Dict = super().generate_dummy_inputs( preprocessor=_UpperCAmelCase , batch_size=_UpperCAmelCase , seq_length=_UpperCAmelCase , is_pair=_UpperCAmelCase , framework=_UpperCAmelCase ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly lowerCAmelCase__ : str = torch.zeros_like(inputs['''input_ids'''] ) # make every second token global lowerCAmelCase__ : int = 1 return inputs
233
"""simple docstring""" def __A ( a_ :Tuple , a_ :Union[str, Any] , a_ :int=False) -> List[str]: if isinstance(a_ , a_) and isinstance(a_ , a_): __a : List[str] = len(set_a.intersection(a_)) if alternative_union: __a : List[str] = len(a_) + len(a_) else: __a : int = len(set_a.union(a_)) return intersection / union if isinstance(a_ , (list, tuple)) and isinstance(a_ , (list, tuple)): __a : Union[str, Any] = [element for element in set_a if element in set_b] if alternative_union: __a : Union[str, Any] = len(a_) + len(a_) return len(a_) / union else: __a : List[Any] = set_a + [element for element in set_b if element not in set_a] return len(a_) / len(a_) return len(a_) / len(a_) return None if __name__ == "__main__": A = {'''a''', '''b''', '''c''', '''d''', '''e'''} A = {'''c''', '''d''', '''e''', '''f''', '''h''', '''i'''} print(jaccard_similarity(set_a, set_b))
52
0
"""simple docstring""" import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": snake_case_ : Dict = """%20""".join(argv[1:]) if len(argv) > 1 else quote(str(input("""Search: """))) print("""Googling.....""") snake_case_ : Optional[int] = f'''https://www.google.com/search?q={query}&num=100''' snake_case_ : List[str] = requests.get( url, headers={"""User-Agent""": str(UserAgent().random)}, ) try: snake_case_ : Dict = ( BeautifulSoup(res.text, """html.parser""") .find("""div""", attrs={"""class""": """yuRUbf"""}) .find("""a""") .get("""href""") ) except AttributeError: snake_case_ : int = parse_qs( BeautifulSoup(res.text, """html.parser""") .find("""div""", attrs={"""class""": """kCrYT"""}) .find("""a""") .get("""href""") )["""url"""][0] webbrowser.open(link)
595
"""simple docstring""" from __future__ import annotations import os from collections.abc import Mapping A = tuple[int, int] class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a : set[int] = vertices __a : dict[EdgeT, int] = { (min(_UpperCAmelCase ), max(_UpperCAmelCase )): weight for edge, weight in edges.items() } def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase ): self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) __a : Dict = weight def _lowerCamelCase ( self ): __a : Graph = Graph({min(self.vertices )} , {} ) __a : EdgeT __a : int __a : EdgeT __a : int while len(subgraph.vertices ) < len(self.vertices ): __a : Any = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: __a : List[str] = edge __a : Optional[int] = weight subgraph.add_edge(_UpperCAmelCase , _UpperCAmelCase ) return subgraph def __A ( a_ :str = "p107_network.txt") -> int: __a : str = os.path.abspath(os.path.dirname(a_)) __a : str = os.path.join(a_ , a_) __a : dict[EdgeT, int] = {} __a : list[str] __a : int __a : int with open(a_) as f: __a : Optional[int] = f.read().strip().split('''\n''') __a : Dict = [line.split(''',''') for line in data] for edgea in range(1 , len(a_)): for edgea in range(a_): if adjaceny_matrix[edgea][edgea] != "-": __a : Tuple = int(adjaceny_matrix[edgea][edgea]) __a : Graph = Graph(set(range(len(a_))) , a_) __a : Graph = graph.prims_algorithm() __a : int = sum(graph.edges.values()) __a : int = sum(subgraph.edges.values()) return initial_total - optimal_total if __name__ == "__main__": print(F'{solution() = }')
52
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mvp import MvpTokenizer lowerCAmelCase = logging.get_logger(__name__) lowerCAmelCase = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} # See all MVP models at https://huggingface.co/models?filter=mvp lowerCAmelCase = { """vocab_file""": { """RUCAIBox/mvp""": """https://huggingface.co/RUCAIBox/mvp/resolve/main/vocab.json""", }, """added_tokens.json""": { """RUCAIBox/mvp""": """https://huggingface.co/RUCAIBox/mvp/resolve/main/added_tokens.json""", }, """merges_file""": { """RUCAIBox/mvp""": """https://huggingface.co/RUCAIBox/mvp/resolve/main/merges.txt""", }, """tokenizer_file""": { """RUCAIBox/mvp""": """https://huggingface.co/RUCAIBox/mvp/resolve/main/tokenizer.json""", }, } lowerCAmelCase = { """RUCAIBox/mvp""": 10_24, } class lowerCamelCase ( _UpperCamelCase ): snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] snake_case_ = MvpTokenizer def __init__( self , a_=None , a_=None , a_=None , a_="replace" , a_="<s>" , a_="</s>" , a_="</s>" , a_="<s>" , a_="<unk>" , a_="<pad>" , a_="<mask>" , a_=False , a_=True , **a_ , ): super().__init__( _UpperCAmelCase , _UpperCAmelCase , tokenizer_file=_UpperCAmelCase , errors=_UpperCAmelCase , bos_token=_UpperCAmelCase , eos_token=_UpperCAmelCase , sep_token=_UpperCAmelCase , cls_token=_UpperCAmelCase , unk_token=_UpperCAmelCase , pad_token=_UpperCAmelCase , mask_token=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase , **_UpperCAmelCase , ) lowerCAmelCase : str = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , _UpperCAmelCase ) != add_prefix_space: lowerCAmelCase : Union[str, Any] = getattr(_UpperCAmelCase , pre_tok_state.pop("type" ) ) lowerCAmelCase : str = add_prefix_space lowerCAmelCase : Optional[Any] = pre_tok_class(**_UpperCAmelCase ) lowerCAmelCase : List[str] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` lowerCAmelCase : Tuple = '''post_processor''' lowerCAmelCase : Dict = getattr(self.backend_tokenizer , _UpperCAmelCase , _UpperCAmelCase ) if tokenizer_component_instance: lowerCAmelCase : List[str] = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowerCAmelCase : Any = tuple(state["sep"] ) if "cls" in state: lowerCAmelCase : Optional[int] = tuple(state["cls"] ) lowerCAmelCase : Any = False if state.get("add_prefix_space" , _UpperCAmelCase ) != add_prefix_space: lowerCAmelCase : Union[str, Any] = add_prefix_space lowerCAmelCase : str = True if state.get("trim_offsets" , _UpperCAmelCase ) != trim_offsets: lowerCAmelCase : List[Any] = trim_offsets lowerCAmelCase : Optional[Any] = True if changes_to_apply: lowerCAmelCase : List[str] = getattr(_UpperCAmelCase , state.pop("type" ) ) lowerCAmelCase : List[str] = component_class(**_UpperCAmelCase ) setattr(self.backend_tokenizer , _UpperCAmelCase , _UpperCAmelCase ) @property def _lowerCamelCase ( self ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def _lowerCamelCase ( self , a_ ): lowerCAmelCase : str = AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else value lowerCAmelCase : Tuple = value def _lowerCamelCase ( self , *a_ , **a_ ): lowerCAmelCase : List[Any] = kwargs.get("is_split_into_words" , _UpperCAmelCase ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_UpperCAmelCase , **_UpperCAmelCase ) def _lowerCamelCase ( self , *a_ , **a_ ): lowerCAmelCase : List[str] = kwargs.get("is_split_into_words" , _UpperCAmelCase ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*_UpperCAmelCase , **_UpperCAmelCase ) def _lowerCamelCase ( self , a_ , a_ = None ): lowerCAmelCase : Any = self._tokenizer.model.save(_UpperCAmelCase , name=_UpperCAmelCase ) return tuple(_UpperCAmelCase ) def _lowerCamelCase ( self , a_ , a_=None ): lowerCAmelCase : str = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _lowerCamelCase ( self , a_ , a_ = None ): lowerCAmelCase : str = [self.sep_token_id] lowerCAmelCase : 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]
525
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''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 __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''trocr''' __lowerCAmelCase = ['''past_key_values'''] __lowerCAmelCase = { '''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model''', '''num_hidden_layers''': '''decoder_layers''', } def __init__( self , _UpperCAmelCase=50265 , _UpperCAmelCase=1024 , _UpperCAmelCase=12 , _UpperCAmelCase=16 , _UpperCAmelCase=4096 , _UpperCAmelCase="gelu" , _UpperCAmelCase=512 , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.0 , _UpperCAmelCase=0.0 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=1 , _UpperCAmelCase=0 , _UpperCAmelCase=2 , **_UpperCAmelCase , ): __a : List[str] = vocab_size __a : Optional[Any] = d_model __a : Optional[Any] = decoder_layers __a : Union[str, Any] = decoder_attention_heads __a : int = decoder_ffn_dim __a : List[Any] = activation_function __a : Any = max_position_embeddings __a : Dict = dropout __a : List[Any] = attention_dropout __a : Optional[Any] = activation_dropout __a : str = init_std __a : List[str] = decoder_layerdrop __a : Union[str, Any] = use_cache __a : Optional[Any] = scale_embedding __a : List[Any] = use_learned_position_embeddings __a : Optional[int] = layernorm_embedding super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , decoder_start_token_id=_UpperCAmelCase , **_UpperCAmelCase , )
52
0
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=_UpperCamelCase ) class lowercase_ ( _UpperCamelCase ): a_ = field(default="""audio-classification""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) a_ = Features({"""audio""": Audio()} ) a_ = Features({"""labels""": ClassLabel} ) a_ = """audio""" a_ = """labels""" def lowerCamelCase_ ( self , UpperCamelCase__ ) -> Tuple: """simple docstring""" if self.label_column not in features: raise ValueError(F"""Column {self.label_column} is not present in features.""" ) if not isinstance(features[self.label_column] , _UpperCAmelCase ): 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 ) -> Optional[int]: """simple docstring""" return { self.audio_column: "audio", self.label_column: "labels", }
660
"""simple docstring""" import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def __A ( a_ :Union[str, Any] , a_ :Union[str, Any] , a_ :Optional[Any] , a_ :Optional[int]=5) -> List[Any]: # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count('''<mask>''') == 1 __a : Optional[Any] = torch.tensor(tokenizer.encode(a_ , add_special_tokens=a_)).unsqueeze(0) # Batch size 1 __a : Dict = model(a_)[0] # The last hidden-state is the first element of the output tuple __a : Tuple = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() __a : Any = logits[0, masked_index, :] __a : Any = logits.softmax(dim=0) __a , __a : Optional[Any] = prob.topk(k=a_ , dim=0) __a : Optional[int] = ''' '''.join( [tokenizer.convert_ids_to_tokens(indices[i].item()) for i in range(len(a_))]) __a : List[str] = tokenizer.mask_token __a : Optional[int] = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(''' ''')): __a : Optional[Any] = predicted_token_bpe.replace('''\u2581''' , ''' ''') if " {0}".format(a_) in masked_input: topk_filled_outputs.append( ( masked_input.replace(''' {0}'''.format(a_) , a_), values[index].item(), predicted_token, )) else: topk_filled_outputs.append( ( masked_input.replace(a_ , a_), values[index].item(), predicted_token, )) return topk_filled_outputs A = CamembertTokenizer.from_pretrained('''camembert-base''') A = CamembertForMaskedLM.from_pretrained('''camembert-base''') model.eval() A = '''Le camembert est <mask> :)''' print(fill_mask(masked_input, model, tokenizer, topk=3))
52
0
import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger __magic_name__ = get_logger(__name__) __magic_name__ = R''' Args: input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search kwargs (`Dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. ''' class lowerCAmelCase__ : """simple docstring""" @add_start_docstrings(_UpperCAmelCase ) def __call__( self , a_ , a_ ): raise NotImplementedError( F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called.""" ) class lowerCAmelCase__ : """simple docstring""" @add_start_docstrings(_UpperCAmelCase ) def __call__( self , a_ , a_ ): raise NotImplementedError( F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called.""" ) class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" @add_start_docstrings(_UpperCAmelCase ) def __call__( self , a_ , a_ , a_ , **a_ ): for processor in self: lowerCamelCase_ : int = inspect.signature(processor.__call__ ).parameters if len(_UpperCAmelCase ) > 3: if not all(arg in kwargs for arg in list(function_args.keys() )[2:] ): raise ValueError( F"""Make sure that all the required parameters: {list(function_args.keys() )} for """ F"""{processor.__class__} are passed to the logits processor.""" ) lowerCamelCase_ : Optional[Any] = processor(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ) else: lowerCamelCase_ : Union[str, Any] = processor(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ) or not (temperature > 0): raise ValueError(F"""`temperature` has to be a strictly positive float, but is {temperature}""" ) lowerCamelCase_ : int = temperature def __call__( self , a_ , a_ , a_ ): lowerCamelCase_ : Tuple = scores / self.temperature return scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ , a_ = -float("Inf" ) , a_ = 1 ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ) or (top_p < 0 or top_p > 1.0): raise ValueError(F"""`top_p` has to be a float > 0 and < 1, but is {top_p}""" ) if not isinstance(_UpperCAmelCase , _UpperCAmelCase ) or (min_tokens_to_keep < 1): raise ValueError(F"""`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}""" ) lowerCamelCase_ : List[str] = top_p lowerCamelCase_ : str = filter_value lowerCamelCase_ : List[str] = min_tokens_to_keep def __call__( self , a_ , a_ , a_ ): lowerCamelCase_ : str = lax.top_k(_UpperCAmelCase , scores.shape[-1] ) lowerCamelCase_ : Any = jnp.full_like(_UpperCAmelCase , self.filter_value ) lowerCamelCase_ : List[Any] = jax.nn.softmax(_UpperCAmelCase , axis=-1 ).cumsum(axis=-1 ) lowerCamelCase_ : int = cumulative_probs < self.top_p # include the token that is higher than top_p as well lowerCamelCase_ : Any = jnp.roll(_UpperCAmelCase , 1 ) score_mask |= score_mask.at[:, 0].set(_UpperCAmelCase ) # min tokens to keep lowerCamelCase_ : List[str] = score_mask.at[:, : self.min_tokens_to_keep].set(_UpperCAmelCase ) lowerCamelCase_ : Tuple = jnp.where(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase_ : Any = jax.lax.sort_key_val(_UpperCAmelCase , _UpperCAmelCase )[-1] return next_scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ , a_ = -float("Inf" ) , a_ = 1 ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ) or top_k <= 0: raise ValueError(F"""`top_k` has to be a strictly positive integer, but is {top_k}""" ) lowerCamelCase_ : Any = max(_UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase_ : int = filter_value def __call__( self , a_ , a_ , a_ ): lowerCamelCase_ : Optional[int] = scores.shape lowerCamelCase_ : Optional[Any] = jnp.full(batch_size * vocab_size , self.filter_value ) lowerCamelCase_ : Any = min(self.top_k , scores.shape[-1] ) # Safety check lowerCamelCase_ : str = lax.top_k(_UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase_ : Tuple = jnp.broadcast_to((jnp.arange(_UpperCAmelCase ) * vocab_size)[:, None] , (batch_size, topk) ).flatten() lowerCamelCase_ : Dict = topk_scores.flatten() lowerCamelCase_ : Optional[Any] = topk_indices.flatten() + shift lowerCamelCase_ : Tuple = next_scores_flat.at[topk_indices_flat].set(_UpperCAmelCase ) lowerCamelCase_ : Any = next_scores_flat.reshape(_UpperCAmelCase , _UpperCAmelCase ) return next_scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ ): lowerCamelCase_ : int = bos_token_id def __call__( self , a_ , a_ , a_ ): lowerCamelCase_ : Dict = jnp.full(scores.shape , -float("inf" ) ) lowerCamelCase_ : str = 1 - jnp.bool_(cur_len - 1 ) lowerCamelCase_ : Tuple = jnp.where(_UpperCAmelCase , new_scores.at[:, self.bos_token_id].set(0 ) , _UpperCAmelCase ) return scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ , a_ ): lowerCamelCase_ : List[Any] = max_length lowerCamelCase_ : Optional[int] = eos_token_id def __call__( self , a_ , a_ , a_ ): lowerCamelCase_ : List[Any] = jnp.full(scores.shape , -float("inf" ) ) lowerCamelCase_ : Optional[int] = 1 - jnp.bool_(cur_len - self.max_length + 1 ) lowerCamelCase_ : Tuple = jnp.where(_UpperCAmelCase , new_scores.at[:, self.eos_token_id].set(0 ) , _UpperCAmelCase ) return scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ , a_ ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ) or min_length < 0: raise ValueError(F"""`min_length` has to be a positive integer, but is {min_length}""" ) if not isinstance(_UpperCAmelCase , _UpperCAmelCase ) or eos_token_id < 0: raise ValueError(F"""`eos_token_id` has to be a positive integer, but is {eos_token_id}""" ) lowerCamelCase_ : Union[str, Any] = min_length lowerCamelCase_ : int = eos_token_id def __call__( self , a_ , a_ , a_ ): # create boolean flag to decide if min length penalty should be applied lowerCamelCase_ : Dict = 1 - jnp.clip(cur_len - self.min_length , 0 , 1 ) lowerCamelCase_ : Dict = jnp.where(_UpperCAmelCase , scores.at[:, self.eos_token_id].set(-float("inf" ) ) , _UpperCAmelCase ) return scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ , a_ ): lowerCamelCase_ : int = list(_UpperCAmelCase ) lowerCamelCase_ : List[Any] = begin_index def __call__( self , a_ , a_ , a_ ): lowerCamelCase_ : List[Any] = 1 - jnp.bool_(cur_len - self.begin_index ) lowerCamelCase_ : int = jnp.where(_UpperCAmelCase , scores.at[:, self.begin_suppress_tokens].set(-float("inf" ) ) , _UpperCAmelCase ) return scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ ): lowerCamelCase_ : Union[str, Any] = list(_UpperCAmelCase ) def __call__( self , a_ , a_ , a_ ): lowerCamelCase_ : Dict = scores.at[..., self.suppress_tokens].set(-float("inf" ) ) return scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ ): lowerCamelCase_ : Optional[int] = dict(_UpperCAmelCase ) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. lowerCamelCase_ : Optional[int] = jnp.ones((max(force_token_map.keys() ) + 1) , dtype=jnp.intaa ) * -1 for index, token in force_token_map.items(): if token is not None: lowerCamelCase_ : Optional[int] = force_token_array.at[index].set(_UpperCAmelCase ) lowerCamelCase_ : List[Any] = jnp.intaa(_UpperCAmelCase ) def __call__( self , a_ , a_ , a_ ): def _force_token(a_ ): lowerCamelCase_ : Tuple = scores.shape[0] lowerCamelCase_ : Tuple = self.force_token_array[generation_idx] lowerCamelCase_ : int = jnp.ones_like(_UpperCAmelCase , dtype=scores.dtype ) * -float("inf" ) lowerCamelCase_ : int = jnp.zeros((batch_size, 1) , dtype=scores.dtype ) lowerCamelCase_ : Dict = lax.dynamic_update_slice(_UpperCAmelCase , _UpperCAmelCase , (0, current_token) ) return new_scores lowerCamelCase_ : Union[str, Any] = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(_UpperCAmelCase ) , lambda: scores , ) , ) return scores class lowerCAmelCase__ ( _UpperCamelCase ): """simple docstring""" def __init__( self , a_ , a_ , a_ ): lowerCamelCase_ : Union[str, Any] = generate_config.eos_token_id lowerCamelCase_ : Dict = generate_config.no_timestamps_token_id lowerCamelCase_ : Optional[int] = generate_config.no_timestamps_token_id + 1 lowerCamelCase_ : List[Any] = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(_UpperCAmelCase , "max_initial_timestamp_index" ): lowerCamelCase_ : List[Any] = generate_config.max_initial_timestamp_index else: lowerCamelCase_ : List[str] = model_config.vocab_size if self.max_initial_timestamp_index is None: lowerCamelCase_ : Tuple = model_config.vocab_size def __call__( self , a_ , a_ , a_ ): # suppress <|notimestamps|> which is handled by without_timestamps lowerCamelCase_ : int = scores.at[:, self.no_timestamps_token_id].set(-float("inf" ) ) def handle_pairs(a_ , a_ ): lowerCamelCase_ : Any = jnp.where((cur_len - self.begin_index) >= 1 , _UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase_ : int = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , _UpperCAmelCase , ) lowerCamelCase_ : List[Any] = jnp.where((cur_len - self.begin_index) < 2 , _UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase_ : Optional[Any] = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , _UpperCAmelCase , _UpperCAmelCase , ) return jnp.where( _UpperCAmelCase , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float("inf" ) ) , scores_k.at[: self.eos_token_id].set(-float("inf" ) ) , ) , _UpperCAmelCase , ) lowerCamelCase_ : Optional[int] = jax.vmap(_UpperCAmelCase )(_UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase_ : int = jnp.where(cur_len == self.begin_index , _UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase_ : Union[str, Any] = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , _UpperCAmelCase , ) lowerCamelCase_ : List[str] = self.timestamp_begin + self.max_initial_timestamp_index lowerCamelCase_ : Optional[int] = jnp.where( _UpperCAmelCase , scores.at[:, last_allowed + 1 :].set(-float("inf" ) ) , _UpperCAmelCase , ) # if sum of probability over timestamps is above any other token, sample timestamp lowerCamelCase_ : Optional[Any] = jax.nn.log_softmax(_UpperCAmelCase , axis=-1 ) def handle_cumulative_probs(a_ , a_ ): lowerCamelCase_ : Any = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1 ) lowerCamelCase_ : Tuple = jnp.max(logprobs_k[: self.timestamp_begin] ) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float("inf" ) ) , _UpperCAmelCase , ) lowerCamelCase_ : Dict = jax.vmap(_UpperCAmelCase )(_UpperCAmelCase , _UpperCAmelCase ) return scores
250
"""simple docstring""" import unittest from knapsack import greedy_knapsack as kp class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): __a : Optional[int] = [10, 20, 30, 40, 50, 60] __a : Union[str, Any] = [2, 4, 6, 8, 10, 12] __a : List[str] = 100 self.assertEqual(kp.calc_profit(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) , 210 ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''max_weight must greater than zero.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''Weight can not be negative.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''Profit can not be negative.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''max_weight must greater than zero.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex( _UpperCAmelCase , '''The length of profit and weight must be same.''' ) if __name__ == "__main__": unittest.main()
52
0
from typing import TYPE_CHECKING from ..utils import _LazyModule A_: str = { 'config': [ 'EXTERNAL_DATA_FORMAT_SIZE_LIMIT', 'OnnxConfig', 'OnnxConfigWithPast', 'OnnxSeq2SeqConfigWithPast', 'PatchingSpec', ], 'convert': ['export', 'validate_model_outputs'], 'features': ['FeaturesManager'], 'utils': ['ParameterFormat', 'compute_serialized_parameters_size'], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys A_: Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
398
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = {} class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''llama''' __lowerCAmelCase = ['''past_key_values'''] def __init__( self , _UpperCAmelCase=32000 , _UpperCAmelCase=4096 , _UpperCAmelCase=11008 , _UpperCAmelCase=32 , _UpperCAmelCase=32 , _UpperCAmelCase=None , _UpperCAmelCase="silu" , _UpperCAmelCase=2048 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-6 , _UpperCAmelCase=True , _UpperCAmelCase=0 , _UpperCAmelCase=1 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=False , _UpperCAmelCase=None , **_UpperCAmelCase , ): __a : Dict = vocab_size __a : Union[str, Any] = max_position_embeddings __a : str = hidden_size __a : List[str] = intermediate_size __a : Any = num_hidden_layers __a : int = num_attention_heads # for backward compatibility if num_key_value_heads is None: __a : Union[str, Any] = num_attention_heads __a : Optional[int] = num_key_value_heads __a : Dict = hidden_act __a : Union[str, Any] = initializer_range __a : int = rms_norm_eps __a : Optional[int] = pretraining_tp __a : Optional[Any] = use_cache __a : Optional[Any] = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , tie_word_embeddings=_UpperCAmelCase , **_UpperCAmelCase , ) def _lowerCamelCase ( self ): if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _UpperCAmelCase ) 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}""" ) __a : Tuple = self.rope_scaling.get('''type''' , _UpperCAmelCase ) __a : Optional[int] = self.rope_scaling.get('''factor''' , _UpperCAmelCase ) 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(_UpperCAmelCase , _UpperCAmelCase ) or rope_scaling_factor <= 1.0: raise ValueError(f"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""" )
52
0
'''simple docstring''' from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __UpperCAmelCase ={"configuration_mra": ["MRA_PRETRAINED_CONFIG_ARCHIVE_MAP", "MraConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "MRA_PRETRAINED_MODEL_ARCHIVE_LIST", "MraForMaskedLM", "MraForMultipleChoice", "MraForQuestionAnswering", "MraForSequenceClassification", "MraForTokenClassification", "MraLayer", "MraModel", "MraPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure)
546
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=3 , _UpperCAmelCase=18 , _UpperCAmelCase=30 , _UpperCAmelCase=400 , _UpperCAmelCase=True , _UpperCAmelCase=None , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=[0.5, 0.5, 0.5] , _UpperCAmelCase=[0.5, 0.5, 0.5] , ): __a : int = parent __a : str = batch_size __a : List[Any] = num_channels __a : Union[str, Any] = image_size __a : List[Any] = min_resolution __a : str = max_resolution __a : List[str] = do_resize __a : Optional[int] = size if size is not None else {'''height''': 18, '''width''': 20} __a : str = do_thumbnail __a : str = do_align_axis __a : Dict = do_pad __a : Union[str, Any] = do_normalize __a : List[str] = image_mean __a : Optional[int] = image_std def _lowerCamelCase ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = DonutImageProcessor if is_vision_available() else None def _lowerCamelCase ( self ): __a : Tuple = DonutImageProcessingTester(self ) @property def _lowerCamelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_resize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_thumbnail''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_align_long_axis''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_pad''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_mean''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_std''' ) ) def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 20} ) __a : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) # Previous config had dimensions in (width, height) order __a : int = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'''height''': 84, '''width''': 42} ) def _lowerCamelCase ( self ): pass @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , Image.Image ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : int = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , numpify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , np.ndarray ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : str = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , torchify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , torch.Tensor ) # Test not batched input __a : Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : List[str] = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
52
0
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' assert isinstance(a_ , a_ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @require_sqlalchemy @pytest.mark.parametrize('keep_in_memory' , [False, True] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = tmp_path / '''cache''' snake_case_ = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): snake_case_ = SqlDatasetReader( 'dataset' , 'sqlite:///' + sqlite_path , cache_dir=a_ , keep_in_memory=a_ ).read() _check_sql_dataset(a_ , a_ ) @require_sqlalchemy @pytest.mark.parametrize( 'features' , [ None, {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}, {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'}, {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'}, {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'}, ] , ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = tmp_path / '''cache''' snake_case_ = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} snake_case_ = features.copy() if features else default_expected_features snake_case_ = ( Features({feature: Value(a_ ) for feature, dtype in features.items()} ) if features is not None else None ) snake_case_ = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , features=a_ , cache_dir=a_ ).read() _check_sql_dataset(a_ , a_ ) def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' with contextlib.closing(sqlitea.connect(a_ ) ) as con: snake_case_ = con.cursor() cur.execute('SELECT * FROM dataset' ) for row in cur: yield row @require_sqlalchemy def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = tmp_path / '''cache''' snake_case_ = os.path.join(a_ , 'tmp.sql' ) snake_case_ = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=a_ ).read() SqlDatasetWriter(a_ , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=1 ).write() snake_case_ = iter_sql_file(a_ ) snake_case_ = iter_sql_file(a_ ) for rowa, rowa in zip(a_ , a_ ): assert rowa == rowa @require_sqlalchemy def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = tmp_path / '''cache''' snake_case_ = os.path.join(a_ , 'tmp.sql' ) snake_case_ = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=a_ ).read() SqlDatasetWriter(a_ , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=2 ).write() snake_case_ = iter_sql_file(a_ ) snake_case_ = iter_sql_file(a_ ) for rowa, rowa in zip(a_ , a_ ): assert rowa == rowa @require_sqlalchemy def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = tmp_path / '''cache''' snake_case_ = os.path.join(a_ , 'tmp.sql' ) snake_case_ = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=a_ ).read() with pytest.raises(a_ ): SqlDatasetWriter(a_ , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=0 ).write()
362
"""simple docstring""" from __future__ import annotations def __A ( a_ :list[int]) -> int: if not nums: return 0 __a : Any = nums[0] __a : Optional[Any] = 0 for num in nums[1:]: __a , __a : Optional[Any] = ( max_excluding + num, max(a_ , a_), ) return max(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo UpperCAmelCase__ = """\ @misc{wu2016googles, title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } """ UpperCAmelCase__ = """\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the \'GLEU score\'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score\'s range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. """ UpperCAmelCase__ = """\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: \'google_bleu\': google_bleu score Examples: Example 1: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.44 Example 2: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results[\"google_bleu\"], 2)) 0.61 Example 3: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results[\"google_bleu\"], 2)) 0.53 Example 4: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric(\"google_bleu\") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results[\"google_bleu\"], 2)) 0.4 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): def lowerCAmelCase_ ( self : Union[str, Any] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""string""" , id="""token""" ) , id="""sequence""" ), """references""": datasets.Sequence( datasets.Sequence(datasets.Value("""string""" , id="""token""" ) , id="""sequence""" ) , id="""references""" ), } ) , ) def lowerCAmelCase_ ( self : List[str] , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str = 1 , __lowerCAmelCase : str = 4 , ): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_UpperCAmelCase , hypotheses=_UpperCAmelCase , min_len=_UpperCAmelCase , max_len=_UpperCAmelCase ) }
277
"""simple docstring""" import unittest from transformers import BigBirdTokenizer, BigBirdTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A = '''▁''' A = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = BigBirdTokenizer __lowerCAmelCase = BigBirdTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = True def _lowerCamelCase ( self ): super().setUp() __a : Dict = self.tokenizer_class(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCamelCase ( self ): __a : List[str] = '''<s>''' __a : Tuple = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''[MASK]''' ) self.assertEqual(len(_UpperCAmelCase ) , 1004 ) def _lowerCamelCase ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def _lowerCamelCase ( self ): if not self.test_rust_tokenizer: return __a : Dict = self.get_tokenizer() __a : Any = self.get_rust_tokenizer() __a : int = '''I was born in 92000, and this is falsé.''' __a : Optional[Any] = tokenizer.tokenize(_UpperCAmelCase ) __a : List[str] = rust_tokenizer.tokenize(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Dict = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) __a : Any = rust_tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Tuple = self.get_rust_tokenizer() __a : Tuple = tokenizer.encode(_UpperCAmelCase ) __a : List[Any] = rust_tokenizer.encode(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = BigBirdTokenizer(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) __a : Optional[int] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) __a : Optional[int] = 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''', '''é''', '''.''', ] , ) __a : Optional[Any] = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) __a : Optional[int] = 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>''', '''.''', ] , ) @cached_property def _lowerCamelCase ( self ): return BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) @slow def _lowerCamelCase ( self ): __a : str = '''Hello World!''' __a : str = [65, 18536, 2260, 101, 66] self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @slow def _lowerCamelCase ( self ): __a : 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 __a : Optional[Any] = [65, 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, 66] # noqa: E231 # fmt: on self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @require_torch @slow def _lowerCamelCase ( self ): import torch from transformers import BigBirdConfig, BigBirdModel # Build sequence __a : List[Any] = list(self.big_tokenizer.get_vocab().keys() )[:10] __a : List[str] = ''' '''.join(_UpperCAmelCase ) __a : Tuple = self.big_tokenizer.encode_plus(_UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Any = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Optional[Any] = BigBirdConfig(attention_type='''original_full''' ) __a : Tuple = BigBirdModel(_UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_UpperCAmelCase ) model(**_UpperCAmelCase ) @slow def _lowerCamelCase ( self ): __a : Union[str, Any] = BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) __a : List[Any] = tokenizer.decode(tokenizer('''Paris is the [MASK].''' ).input_ids ) self.assertTrue(decoded_text == '''[CLS] Paris is the[MASK].[SEP]''' ) @slow def _lowerCamelCase ( self ): # fmt: off __a : Optional[Any] = {'''input_ids''': [[65, 39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114, 66], [65, 448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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], [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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name='''google/bigbird-roberta-base''' , revision='''215c99f1600e06f83acce68422f2035b2b5c3510''' , )
52
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __SCREAMING_SNAKE_CASE = {'configuration_reformer': ['REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ReformerConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE = ['ReformerTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE = ['ReformerTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE = [ 'REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'ReformerAttention', 'ReformerForMaskedLM', 'ReformerForQuestionAnswering', 'ReformerForSequenceClassification', 'ReformerLayer', 'ReformerModel', 'ReformerModelWithLMHead', 'ReformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer import ReformerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer_fast import ReformerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_reformer import ( REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ReformerAttention, ReformerForMaskedLM, ReformerForQuestionAnswering, ReformerForSequenceClassification, ReformerLayer, ReformerModel, ReformerModelWithLMHead, ReformerPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
220
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A = logging.get_logger(__name__) A = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''convnextv2''' def __init__( self , _UpperCAmelCase=3 , _UpperCAmelCase=4 , _UpperCAmelCase=4 , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=224 , _UpperCAmelCase=None , _UpperCAmelCase=None , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : List[str] = num_channels __a : str = patch_size __a : Dict = num_stages __a : List[str] = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes __a : List[str] = [3, 3, 9, 3] if depths is None else depths __a : List[Any] = hidden_act __a : Any = initializer_range __a : Optional[int] = layer_norm_eps __a : List[Any] = drop_path_rate __a : Any = image_size __a : str = ['''stem'''] + [f"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] __a , __a : Optional[int] = get_aligned_output_features_output_indices( out_features=_UpperCAmelCase , out_indices=_UpperCAmelCase , stage_names=self.stage_names )
52
0
UpperCAmelCase_ : Dict = { '''joule''': 1.0, '''kilojoule''': 1_000, '''megajoule''': 1_000_000, '''gigajoule''': 1_000_000_000, '''wattsecond''': 1.0, '''watthour''': 3_600, '''kilowatthour''': 3_600_000, '''newtonmeter''': 1.0, '''calorie_nutr''': 4_186.8, '''kilocalorie_nutr''': 4_186_800.00, '''electronvolt''': 1.6_0217_6634e-19, '''britishthermalunit_it''': 1_055.05_585, '''footpound''': 1.35_5818, } def __SCREAMING_SNAKE_CASE ( a__ : str ,a__ : str ,a__ : float ) -> float: if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: __A : Dict = ( f"""Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n""" f"""Valid values are: {", ".join(a_ )}""" ) raise ValueError(a_ ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
17
"""simple docstring""" import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = (DDPMScheduler,) def _lowerCamelCase ( self , **_UpperCAmelCase ): __a : int = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**_UpperCAmelCase ) return config def _lowerCamelCase ( self ): for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_UpperCAmelCase , beta_end=_UpperCAmelCase ) def _lowerCamelCase ( self ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_UpperCAmelCase ) def _lowerCamelCase ( self ): for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=_UpperCAmelCase ) def _lowerCamelCase ( self ): self.check_over_configs(thresholding=_UpperCAmelCase ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_UpperCAmelCase , prediction_type=_UpperCAmelCase , sample_max_value=_UpperCAmelCase , ) def _lowerCamelCase ( self ): for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for t in [0, 500, 999]: self.check_over_forward(time_step=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Dict = scheduler_class(**_UpperCAmelCase ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def _lowerCamelCase ( self ): __a : int = self.scheduler_classes[0] __a : int = self.get_scheduler_config() __a : Optional[Any] = scheduler_class(**_UpperCAmelCase ) __a : int = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[Any] = self.dummy_sample_deter __a : Union[str, Any] = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Optional[int] = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : List[Any] = pred_prev_sample __a : int = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : Union[str, Any] = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def _lowerCamelCase ( self ): __a : Dict = self.scheduler_classes[0] __a : int = self.get_scheduler_config(prediction_type='''v_prediction''' ) __a : int = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[str] = self.dummy_sample_deter __a : str = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Dict = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : Optional[int] = pred_prev_sample __a : Optional[int] = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : int = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Any = self.get_scheduler_config() __a : str = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_UpperCAmelCase ) __a : List[Any] = scheduler.timesteps for i, timestep in enumerate(_UpperCAmelCase ): if i == len(_UpperCAmelCase ) - 1: __a : Union[str, Any] = -1 else: __a : str = timesteps[i + 1] __a : Dict = scheduler.previous_timestep(_UpperCAmelCase ) __a : str = prev_t.item() self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Tuple = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Optional[Any] = [100, 87, 50, 51, 0] with self.assertRaises(_UpperCAmelCase , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] __a : Optional[int] = len(_UpperCAmelCase ) with self.assertRaises(_UpperCAmelCase , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=_UpperCAmelCase , timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : List[str] = scheduler_class(**_UpperCAmelCase ) __a : List[Any] = [scheduler.config.num_train_timesteps] with self.assertRaises( _UpperCAmelCase , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=_UpperCAmelCase )
52
0
import string from math import logaa def _a ( __UpperCamelCase : str ,__UpperCamelCase : str ): lowerCAmelCase__ : Optional[int] = document.translate( str.maketrans('''''' ,'''''' ,string.punctuation ) ).replace('''\n''' ,'''''' ) lowerCAmelCase__ : int = document_without_punctuation.split(''' ''' ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def _a ( __UpperCamelCase : str ,__UpperCamelCase : str ): lowerCAmelCase__ : List[Any] = corpus.lower().translate( str.maketrans('''''' ,'''''' ,string.punctuation ) ) # strip all punctuation and replace it with '' lowerCAmelCase__ : List[str] = corpus_without_punctuation.split('''\n''' ) lowerCAmelCase__ : Optional[int] = term.lower() return (len([doc for doc in docs if term in doc] ), len(a_ )) def _a ( __UpperCamelCase : int ,__UpperCamelCase : int ,__UpperCamelCase : List[str]=False ): if smoothing: if n == 0: raise ValueError('''log10(0) is undefined.''' ) return round(1 + logaa(n / (1 + df) ) ,3 ) if df == 0: raise ZeroDivisionError('''df must be > 0''' ) elif n == 0: raise ValueError('''log10(0) is undefined.''' ) return round(logaa(n / df ) ,3 ) def _a ( __UpperCamelCase : int ,__UpperCamelCase : int ): return round(tf * idf ,3 )
233
"""simple docstring""" import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset A = random.Random() def __A ( a_ :Tuple , a_ :Dict=1.0 , a_ :str=None , a_ :List[Any]=None) -> Dict: if rng is None: __a : Any = global_rng __a : Tuple = [] for batch_idx in range(shape[0]): values.append([]) for _ in range(shape[1]): values[-1].append(rng.random() * scale) return values class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=400 , _UpperCAmelCase=2000 , _UpperCAmelCase=2048 , _UpperCAmelCase=128 , _UpperCAmelCase=1 , _UpperCAmelCase=512 , _UpperCAmelCase=30 , _UpperCAmelCase=44100 , ): __a : Any = parent __a : Tuple = batch_size __a : Tuple = min_seq_length __a : List[str] = max_seq_length __a : List[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __a : Tuple = spectrogram_length __a : int = feature_size __a : int = num_audio_channels __a : Tuple = hop_length __a : List[Any] = chunk_length __a : Any = sampling_rate def _lowerCamelCase ( self ): return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def _lowerCamelCase ( self , _UpperCAmelCase=False , _UpperCAmelCase=False ): def _flatten(_UpperCAmelCase ): return list(itertools.chain(*_UpperCAmelCase ) ) if equal_length: __a : Tuple = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __a : Tuple = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __a : Optional[Any] = [np.asarray(_UpperCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = TvltFeatureExtractor def _lowerCamelCase ( self ): __a : Optional[Any] = TvltFeatureExtractionTester(self ) def _lowerCamelCase ( self ): __a : int = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''spectrogram_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''feature_size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''num_audio_channels''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''hop_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''chunk_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''sampling_rate''' ) ) def _lowerCamelCase ( self ): __a : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : List[str] = feat_extract_first.save_pretrained(_UpperCAmelCase )[0] check_json_file_has_correct_format(_UpperCAmelCase ) __a : Union[str, Any] = self.feature_extraction_class.from_pretrained(_UpperCAmelCase ) __a : Tuple = feat_extract_first.to_dict() __a : List[Any] = feat_extract_second.to_dict() __a : int = dict_first.pop('''mel_filters''' ) __a : List[Any] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : int = os.path.join(_UpperCAmelCase , '''feat_extract.json''' ) feat_extract_first.to_json_file(_UpperCAmelCase ) __a : Optional[Any] = self.feature_extraction_class.from_json_file(_UpperCAmelCase ) __a : Optional[Any] = feat_extract_first.to_dict() __a : Any = feat_extract_second.to_dict() __a : Optional[Any] = dict_first.pop('''mel_filters''' ) __a : Dict = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): # Initialize feature_extractor __a : str = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 __a : Any = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __a : Union[str, Any] = [np.asarray(_UpperCAmelCase ) for speech_input in speech_inputs] # Test not batched input __a : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched __a : int = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking __a : List[Any] = feature_extractor( _UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 , mask_audio=_UpperCAmelCase ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. __a : str = [floats_list((1, x) )[0] for x in (800, 800, 800)] __a : Any = np.asarray(_UpperCAmelCase ) __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def _lowerCamelCase ( self , _UpperCAmelCase ): __a : int = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech __a : int = ds.sort('''id''' ).select(range(_UpperCAmelCase ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowerCamelCase ( self ): __a : List[str] = self._load_datasamples(1 ) __a : Tuple = TvltFeatureExtractor() __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) __a : Dict = torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _UpperCAmelCase , atol=1e-4 ) )
52
0
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset from utils import logger class snake_case__ ( _UpperCamelCase ): def __init__( self : str , lowercase : List[Any] , lowercase : Any ): '''simple docstring''' UpperCAmelCase : str = params UpperCAmelCase : Optional[Any] = np.array(_UpperCAmelCase ) UpperCAmelCase : Tuple = np.array([len(_UpperCAmelCase ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self : Union[str, Any] , lowercase : Tuple ): '''simple docstring''' return (self.token_ids[index], self.lengths[index]) def __len__( self : List[Any] ): '''simple docstring''' return len(self.lengths ) def __lowerCAmelCase ( self : str ): '''simple docstring''' assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def __lowerCAmelCase ( self : int ): '''simple docstring''' UpperCAmelCase : Union[str, Any] = self.params.max_model_input_size UpperCAmelCase : List[str] = self.lengths > max_len logger.info(f"""Splitting {sum(_UpperCAmelCase )} too long sequences.""" ) def divide_chunks(lowercase : List[str] , lowercase : Any ): return [l[i : i + n] for i in range(0 , len(_UpperCAmelCase ) , _UpperCAmelCase )] UpperCAmelCase : Any = [] UpperCAmelCase : Tuple = [] if self.params.mlm: UpperCAmelCase : Tuple = self.params.special_tok_ids['''cls_token'''], self.params.special_tok_ids['''sep_token'''] else: UpperCAmelCase : Any = self.params.special_tok_ids['''bos_token'''], self.params.special_tok_ids['''eos_token'''] for seq_, len_ in zip(self.token_ids , self.lengths ): assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_ if len_ <= max_len: new_tok_ids.append(seq_ ) new_lengths.append(len_ ) else: UpperCAmelCase : List[str] = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: UpperCAmelCase : Tuple = np.insert(_UpperCAmelCase , 0 , _UpperCAmelCase ) if sub_s[-1] != sep_id: UpperCAmelCase : Any = np.insert(_UpperCAmelCase , len(_UpperCAmelCase ) , _UpperCAmelCase ) assert len(_UpperCAmelCase ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(_UpperCAmelCase ) new_tok_ids.extend(_UpperCAmelCase ) new_lengths.extend([len(_UpperCAmelCase ) for l in sub_seqs] ) UpperCAmelCase : Optional[Any] = np.array(_UpperCAmelCase ) UpperCAmelCase : List[Any] = np.array(_UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase : Union[str, Any] = len(self ) UpperCAmelCase : List[Any] = self.lengths > 11 UpperCAmelCase : int = self.token_ids[indices] UpperCAmelCase : Tuple = self.lengths[indices] UpperCAmelCase : Optional[Any] = len(self ) logger.info(f"""Remove {init_size - new_size} too short (<=11 tokens) sequences.""" ) def __lowerCAmelCase ( self : Any ): '''simple docstring''' if "unk_token" not in self.params.special_tok_ids: return else: UpperCAmelCase : int = self.params.special_tok_ids['''unk_token'''] UpperCAmelCase : Union[str, Any] = len(self ) UpperCAmelCase : Optional[int] = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) UpperCAmelCase : str = (unk_occs / self.lengths) < 0.5 UpperCAmelCase : Tuple = self.token_ids[indices] UpperCAmelCase : List[Any] = self.lengths[indices] UpperCAmelCase : Tuple = len(self ) logger.info(f"""Remove {init_size - new_size} sequences with a high level of unknown tokens (50%).""" ) def __lowerCAmelCase ( self : Dict ): '''simple docstring''' if not self.params.is_master: return logger.info(f"""{len(self )} sequences""" ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def __lowerCAmelCase ( self : Dict , lowercase : Union[str, Any] ): '''simple docstring''' UpperCAmelCase : List[Any] = [t[0] for t in batch] UpperCAmelCase : Union[str, Any] = [t[1] for t in batch] assert len(_UpperCAmelCase ) == len(_UpperCAmelCase ) # Max for paddings UpperCAmelCase : List[str] = max(_UpperCAmelCase ) # Pad token ids if self.params.mlm: UpperCAmelCase : Optional[int] = self.params.special_tok_ids['''pad_token'''] else: UpperCAmelCase : Dict = self.params.special_tok_ids['''unk_token'''] UpperCAmelCase : List[str] = [list(t.astype(_UpperCAmelCase ) ) + [pad_idx] * (max_seq_len_ - len(_UpperCAmelCase )) for t in token_ids] assert len(tk_ ) == len(_UpperCAmelCase ) assert all(len(_UpperCAmelCase ) == max_seq_len_ for t in tk_ ) UpperCAmelCase : Union[str, Any] = torch.tensor(tk_ ) # (bs, max_seq_len_) UpperCAmelCase : Any = torch.tensor(_UpperCAmelCase ) # (bs) return tk_t, lg_t
595
"""simple docstring""" from __future__ import annotations class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a , __a : List[Any] = text, pattern __a , __a : Tuple = len(_UpperCAmelCase ), len(_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def _lowerCamelCase ( self ): # searches pattern in text and returns index positions __a : Dict = [] for i in range(self.textLen - self.patLen + 1 ): __a : List[str] = self.mismatch_in_text(_UpperCAmelCase ) if mismatch_index == -1: positions.append(_UpperCAmelCase ) else: __a : Tuple = self.match_in_pattern(self.text[mismatch_index] ) __a : Optional[int] = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions A = '''ABAABA''' A = '''AB''' A = BoyerMooreSearch(text, pattern) A = bms.bad_character_heuristic() if len(positions) == 0: print('''No match found''') else: print('''Pattern found in following positions: ''') print(positions)
52
0
'''simple docstring''' import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class lowerCamelCase ( ctypes.Structure ): snake_case_ = [("size", ctypes.c_int), ("visible", ctypes.c_byte)] def __A ( ): if os.name == "nt": lowerCAmelCase : Optional[Any] = CursorInfo() lowerCAmelCase : str = ctypes.windll.kernelaa.GetStdHandle(-1_1 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(a_ ,ctypes.byref(a_ ) ) lowerCAmelCase : List[Any] = False ctypes.windll.kernelaa.SetConsoleCursorInfo(a_ ,ctypes.byref(a_ ) ) elif os.name == "posix": sys.stdout.write("\033[?25l" ) sys.stdout.flush() def __A ( ): if os.name == "nt": lowerCAmelCase : Optional[int] = CursorInfo() lowerCAmelCase : List[str] = ctypes.windll.kernelaa.GetStdHandle(-1_1 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(a_ ,ctypes.byref(a_ ) ) lowerCAmelCase : str = True ctypes.windll.kernelaa.SetConsoleCursorInfo(a_ ,ctypes.byref(a_ ) ) elif os.name == "posix": sys.stdout.write("\033[?25h" ) sys.stdout.flush() @contextmanager def __A ( ): try: hide_cursor() yield finally: show_cursor()
525
"""simple docstring""" import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": A = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') A = F'https://www.google.com/search?q={query}&num=100' A = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: A = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: A = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
0
'''simple docstring''' import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation def lowerCamelCase__ ( A_ ): UpperCAmelCase_ = 384 if "tiny" in model_name: UpperCAmelCase_ = [3, 3, 9, 3] UpperCAmelCase_ = [96, 192, 384, 768] if "small" in model_name: UpperCAmelCase_ = [3, 3, 27, 3] UpperCAmelCase_ = [96, 192, 384, 768] if "base" in model_name: UpperCAmelCase_ = [3, 3, 27, 3] UpperCAmelCase_ = [128, 256, 512, 1_024] UpperCAmelCase_ = 512 if "large" in model_name: UpperCAmelCase_ = [3, 3, 27, 3] UpperCAmelCase_ = [192, 384, 768, 1_536] UpperCAmelCase_ = 768 if "xlarge" in model_name: UpperCAmelCase_ = [3, 3, 27, 3] UpperCAmelCase_ = [256, 512, 1_024, 2_048] UpperCAmelCase_ = 1_024 # set label information UpperCAmelCase_ = 150 UpperCAmelCase_ = '''huggingface/label-files''' UpperCAmelCase_ = '''ade20k-id2label.json''' UpperCAmelCase_ = json.load(open(hf_hub_download(a_ , a_ , repo_type="dataset" ) , "r" ) ) UpperCAmelCase_ = {int(a_ ): v for k, v in idalabel.items()} UpperCAmelCase_ = {v: k for k, v in idalabel.items()} UpperCAmelCase_ = ConvNextConfig( depths=a_ , hidden_sizes=a_ , out_features=["stage1", "stage2", "stage3", "stage4"] ) UpperCAmelCase_ = UperNetConfig( backbone_config=a_ , auxiliary_in_channels=a_ , num_labels=a_ , idalabel=a_ , labelaid=a_ , ) return config def lowerCamelCase__ ( A_ ): UpperCAmelCase_ = [] # fmt: off # stem rename_keys.append(("backbone.downsample_layers.0.0.weight", "backbone.embeddings.patch_embeddings.weight") ) rename_keys.append(("backbone.downsample_layers.0.0.bias", "backbone.embeddings.patch_embeddings.bias") ) rename_keys.append(("backbone.downsample_layers.0.1.weight", "backbone.embeddings.layernorm.weight") ) rename_keys.append(("backbone.downsample_layers.0.1.bias", "backbone.embeddings.layernorm.bias") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.stages.{i}.{j}.gamma""", F"""backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.depthwise_conv.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.dwconv.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.depthwise_conv.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.dwconv.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.norm.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.layernorm.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.norm.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.layernorm.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv1.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv1.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv2.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv2.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias""") ) if i > 0: rename_keys.append((F"""backbone.downsample_layers.{i}.0.weight""", F"""backbone.encoder.stages.{i}.downsampling_layer.0.weight""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.0.bias""", F"""backbone.encoder.stages.{i}.downsampling_layer.0.bias""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.1.weight""", F"""backbone.encoder.stages.{i}.downsampling_layer.1.weight""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.1.bias""", F"""backbone.encoder.stages.{i}.downsampling_layer.1.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""backbone.hidden_states_norms.stage{i+1}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""backbone.hidden_states_norms.stage{i+1}.bias""") ) # decode head rename_keys.extend( [ ("decode_head.conv_seg.weight", "decode_head.classifier.weight"), ("decode_head.conv_seg.bias", "decode_head.classifier.bias"), ("auxiliary_head.conv_seg.weight", "auxiliary_head.classifier.weight"), ("auxiliary_head.conv_seg.bias", "auxiliary_head.classifier.bias"), ] ) # fmt: on return rename_keys def lowerCamelCase__ ( A_ , A_ , A_ ): UpperCAmelCase_ = dct.pop(a_ ) UpperCAmelCase_ = val def lowerCamelCase__ ( A_ , A_ , A_ ): UpperCAmelCase_ = { '''upernet-convnext-tiny''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth''', '''upernet-convnext-small''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth''', '''upernet-convnext-base''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth''', '''upernet-convnext-large''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth''', '''upernet-convnext-xlarge''': '''https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth''', } UpperCAmelCase_ = model_name_to_url[model_name] UpperCAmelCase_ = torch.hub.load_state_dict_from_url(a_ , map_location="cpu" )['''state_dict'''] UpperCAmelCase_ = get_upernet_config(a_ ) UpperCAmelCase_ = UperNetForSemanticSegmentation(a_ ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): UpperCAmelCase_ = state_dict.pop(a_ ) if "bn" in key: UpperCAmelCase_ = key.replace("bn" , "batch_norm" ) UpperCAmelCase_ = val # rename keys UpperCAmelCase_ = create_rename_keys(a_ ) for src, dest in rename_keys: rename_key(a_ , a_ , a_ ) model.load_state_dict(a_ ) # verify on image UpperCAmelCase_ = '''https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg''' UpperCAmelCase_ = Image.open(requests.get(a_ , stream=a_ ).raw ).convert("RGB" ) UpperCAmelCase_ = SegformerImageProcessor() UpperCAmelCase_ = processor(a_ , return_tensors="pt" ).pixel_values with torch.no_grad(): UpperCAmelCase_ = model(a_ ) if model_name == "upernet-convnext-tiny": UpperCAmelCase_ = torch.tensor( [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ) elif model_name == "upernet-convnext-small": UpperCAmelCase_ = torch.tensor( [[-8.8236, -8.8236, -8.6771], [-8.8236, -8.8236, -8.6771], [-8.7638, -8.7638, -8.6240]] ) elif model_name == "upernet-convnext-base": UpperCAmelCase_ = torch.tensor( [[-8.8558, -8.8558, -8.6905], [-8.8558, -8.8558, -8.6905], [-8.7669, -8.7669, -8.6021]] ) elif model_name == "upernet-convnext-large": UpperCAmelCase_ = torch.tensor( [[-8.6660, -8.6660, -8.6210], [-8.6660, -8.6660, -8.6210], [-8.6310, -8.6310, -8.5964]] ) elif model_name == "upernet-convnext-xlarge": UpperCAmelCase_ = torch.tensor( [[-8.4980, -8.4980, -8.3977], [-8.4980, -8.4980, -8.3977], [-8.4379, -8.4379, -8.3412]] ) print("Logits:" , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , a_ , atol=1e-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(a_ ) print(F"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(a_ ) if push_to_hub: print(F"""Pushing model and processor for {model_name} to hub""" ) model.push_to_hub(F"""openmmlab/{model_name}""" ) processor.push_to_hub(F"""openmmlab/{model_name}""" ) if __name__ == "__main__": __snake_case : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''upernet-convnext-tiny''', type=str, choices=[F'''upernet-convnext-{size}''' for size in ['''tiny''', '''small''', '''base''', '''large''', '''xlarge''']], help='''Name of the ConvNext UperNet model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) __snake_case : Optional[int] = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
660
"""simple docstring""" import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = 0 __lowerCAmelCase = False __lowerCAmelCase = 3.0 class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_UpperCAmelCase ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.2_5 ).to_kwargs() , {'''a''': 2, '''c''': 2.2_5} ) @require_cuda def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a : List[Any] = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() __a : int = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a : Optional[Any] = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_0_2_4.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _UpperCAmelCase ) @require_multi_gpu def _lowerCamelCase ( self ): __a : Dict = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] execute_subprocess_async(_UpperCAmelCase , env=os.environ.copy() ) if __name__ == "__main__": A = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) A = Accelerator(kwargs_handlers=[ddp_scaler]) A = torch.nn.Linear(100, 200) A = accelerator.prepare(model) # Check the values changed in kwargs A = '''''' A = model.bucket_bytes_cap // (1_024 * 1_024) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
52
0
from datetime import datetime import requests def __magic_name__ ( lowerCAmelCase_): '''simple docstring''' lowerCamelCase_ : int = '''https://downloadgram.net/wp-json/wppress/video-downloader/video?url=''' lowerCamelCase_ : Optional[Any] = requests.get(base_url + url).json()[0]['''urls'''][0]['''src'''] return requests.get(a_).content if __name__ == "__main__": __magic_name__ = input('''Enter Video/IGTV url: ''').strip() __magic_name__ = f'''{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4''' with open(file_name, '''wb''') as fp: fp.write(download_video(url)) print(f'''Done. Video saved to disk as {file_name}.''')
250
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A_: Any = {'configuration_wavlm': ['WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WavLMConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_: str = [ 'WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'WavLMForAudioFrameClassification', 'WavLMForCTC', 'WavLMForSequenceClassification', 'WavLMForXVector', 'WavLMModel', 'WavLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys A_: Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
398
"""simple docstring""" import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger A = get_logger(__name__) A = Path(__file__).parent / '''model_card_template.md''' A = uuida().hex A = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES A = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES A = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def __A ( a_ :Union[Dict, str, None] = None) -> str: __a : Union[str, Any] = F"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F"""; torch/{_torch_version}""" if is_flax_available(): ua += F"""; jax/{_jax_version}""" ua += F"""; flax/{_flax_version}""" if is_onnx_available(): ua += F"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''').upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(a_ , a_): ua += "; " + "; ".join(F"""{k}/{v}""" for k, v in user_agent.items()) elif isinstance(a_ , a_): ua += "; " + user_agent return ua def __A ( a_ :str , a_ :Optional[str] = None , a_ :Optional[str] = None) -> Optional[int]: if token is None: __a : Any = HfFolder.get_token() if organization is None: __a : List[Any] = whoami(a_)['''name'''] return F"""{username}/{model_id}""" else: return F"""{organization}/{model_id}""" def __A ( a_ :Union[str, Any] , a_ :List[str]) -> Optional[Any]: if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''') if hasattr(a_ , '''local_rank''') and args.local_rank not in [-1, 0]: return __a : int = args.hub_token if hasattr(a_ , '''hub_token''') else None __a : Any = get_full_repo_name(a_ , token=a_) __a : Tuple = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=a_ , model_name=a_ , repo_name=a_ , dataset_name=args.dataset_name if hasattr(a_ , '''dataset_name''') else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(a_ , '''gradient_accumulation_steps''') else None ) , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta1''') else None , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta2''') else None , adam_weight_decay=args.adam_weight_decay if hasattr(a_ , '''adam_weight_decay''') else None , adam_epsilon=args.adam_epsilon if hasattr(a_ , '''adam_epsilon''') else None , lr_scheduler=args.lr_scheduler if hasattr(a_ , '''lr_scheduler''') else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(a_ , '''lr_warmup_steps''') else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(a_ , '''ema_inv_gamma''') else None , ema_power=args.ema_power if hasattr(a_ , '''ema_power''') else None , ema_max_decay=args.ema_max_decay if hasattr(a_ , '''ema_max_decay''') else None , mixed_precision=args.mixed_precision , ) __a : List[Any] = os.path.join(args.output_dir , '''README.md''') model_card.save(a_) def __A ( a_ :Optional[str] , a_ :Optional[str] = None) -> Union[str, Any]: if resolved_file is None or commit_hash is not None: return commit_hash __a : Any = str(Path(a_).as_posix()) __a : Optional[int] = re.search(R'''snapshots/([^/]+)/''' , a_) if search is None: return None __a : Dict = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(a_) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. A = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) A = os.path.join(hf_cache_home, '''diffusers''') def __A ( a_ :Optional[str] = None , a_ :Optional[str] = None) -> None: if new_cache_dir is None: __a : Dict = DIFFUSERS_CACHE if old_cache_dir is None: __a : List[Any] = old_diffusers_cache __a : Union[str, Any] = Path(a_).expanduser() __a : Dict = Path(a_).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*'''): if old_blob_path.is_file() and not old_blob_path.is_symlink(): __a : List[Any] = new_cache_dir / old_blob_path.relative_to(a_) new_blob_path.parent.mkdir(parents=a_ , exist_ok=a_) os.replace(a_ , a_) try: os.symlink(a_ , a_) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''') # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). A = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): A = 0 else: with open(cache_version_file) as f: try: A = int(f.read()) except ValueError: A = 0 if cache_version < 1: A = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: A = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F'There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F'There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ' '''the directory exists and can be written to.''' ) def __A ( a_ :str , a_ :Optional[str] = None) -> str: if variant is not None: __a : Dict = weights_name.split('''.''') __a : List[Any] = splits[:-1] + [variant] + splits[-1:] __a : Tuple = '''.'''.join(a_) return weights_name def __A ( a_ :List[Any] , *, a_ :Union[str, Any] , a_ :Dict , a_ :Union[str, Any] , a_ :Optional[int] , a_ :str , a_ :Any , a_ :str , a_ :Optional[int] , a_ :str , a_ :Tuple , a_ :List[str]=None , ) -> Dict: __a : int = str(a_) if os.path.isfile(a_): return pretrained_model_name_or_path elif os.path.isdir(a_): if os.path.isfile(os.path.join(a_ , a_)): # Load from a PyTorch checkpoint __a : Union[str, Any] = os.path.join(a_ , a_) return model_file elif subfolder is not None and os.path.isfile( os.path.join(a_ , a_ , a_)): __a : Optional[Any] = os.path.join(a_ , a_ , a_) return model_file else: raise EnvironmentError( F"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""") else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(a_).base_version) >= version.parse('''0.20.0''') ): try: __a : Any = hf_hub_download( a_ , filename=_add_variant(a_ , a_) , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) warnings.warn( F"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.""" , a_ , ) return model_file except: # noqa: E722 warnings.warn( F"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(a_ , a_)} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(a_ , a_)}' so that the correct variant file can be added.""" , a_ , ) try: # 2. Load model file as usual __a : Optional[Any] = hf_hub_download( a_ , filename=a_ , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''') except RevisionNotFoundError: raise EnvironmentError( F"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ '''this model name. Check the model page at ''' F"""'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions.""") except EntryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""") except HTTPError as err: raise EnvironmentError( F"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""") except ValueError: raise EnvironmentError( F"""We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it""" F""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" F""" directory containing a file named {weights_name} or""" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''') except EnvironmentError: raise EnvironmentError( F"""Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from """ '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F"""Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory """ F"""containing a file named {weights_name}""")
52
0
'''simple docstring''' import copy import json import os import tempfile from transformers import is_torch_available from .test_configuration_utils import config_common_kwargs class a__ ( _UpperCamelCase ): def __init__( self : str , a : List[Any] , a : int=None , a : Dict=True , a : Dict=None , **a : int ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = config_class __lowerCamelCase = has_text_modality __lowerCamelCase = kwargs __lowerCamelCase = common_properties def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = self.config_class(**self.inputs_dict ) __lowerCamelCase = ( ['''hidden_size''', '''num_attention_heads''', '''num_hidden_layers'''] if self.common_properties is None else self.common_properties ) # Add common fields for text models if self.has_text_modality: common_properties.extend(['''vocab_size'''] ) # Test that config has the common properties as getters for prop in common_properties: self.parent.assertTrue(hasattr(_UpperCAmelCase , _UpperCAmelCase ) , msg=f"""`{prop}` does not exist""" ) # Test that config has the common properties as setter for idx, name in enumerate(_UpperCAmelCase ): try: setattr(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) self.parent.assertEqual( getattr(_UpperCAmelCase , _UpperCAmelCase ) , _UpperCAmelCase , msg=f"""`{name} value {idx} expected, but was {getattr(_UpperCAmelCase , _UpperCAmelCase )}""" ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass # Test if config class can be called with Config(prop_name=..) for idx, name in enumerate(_UpperCAmelCase ): try: __lowerCamelCase = self.config_class(**{name: idx} ) self.parent.assertEqual( getattr(_UpperCAmelCase , _UpperCAmelCase ) , _UpperCAmelCase , msg=f"""`{name} value {idx} expected, but was {getattr(_UpperCAmelCase , _UpperCAmelCase )}""" ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = self.config_class(**self.inputs_dict ) __lowerCamelCase = json.loads(config.to_json_string() ) for key, value in self.inputs_dict.items(): self.parent.assertEqual(obj[key] , _UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __lowerCamelCase = os.path.join(_UpperCAmelCase , '''config.json''' ) config_first.to_json_file(_UpperCAmelCase ) __lowerCamelCase = self.config_class.from_json_file(_UpperCAmelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: config_first.save_pretrained(_UpperCAmelCase ) __lowerCamelCase = self.config_class.from_pretrained(_UpperCAmelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.config_class(**self.inputs_dict ) __lowerCamelCase = '''test''' with tempfile.TemporaryDirectory() as tmpdirname: __lowerCamelCase = os.path.join(_UpperCAmelCase , _UpperCAmelCase ) config_first.save_pretrained(_UpperCAmelCase ) __lowerCamelCase = self.config_class.from_pretrained(_UpperCAmelCase , subfolder=_UpperCAmelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.config_class(**self.inputs_dict , num_labels=5 ) self.parent.assertEqual(len(config.idalabel ) , 5 ) self.parent.assertEqual(len(config.labelaid ) , 5 ) __lowerCamelCase = 3 self.parent.assertEqual(len(config.idalabel ) , 3 ) self.parent.assertEqual(len(config.labelaid ) , 3 ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" if self.config_class.is_composition: return __lowerCamelCase = self.config_class() self.parent.assertIsNotNone(_UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = copy.deepcopy(_UpperCAmelCase ) __lowerCamelCase = self.config_class(**_UpperCAmelCase ) __lowerCamelCase = [] for key, value in config_common_kwargs.items(): if key == "torch_dtype": if not is_torch_available(): continue else: import torch if config.torch_dtype != torch.floataa: wrong_values.append(('''torch_dtype''', config.torch_dtype, torch.floataa) ) elif getattr(_UpperCAmelCase , _UpperCAmelCase ) != value: wrong_values.append((key, getattr(_UpperCAmelCase , _UpperCAmelCase ), value) ) if len(_UpperCAmelCase ) > 0: __lowerCamelCase = '''\n'''.join([f"""- {v[0]}: got {v[1]} instead of {v[2]}""" for v in wrong_values] ) raise ValueError(f"""The following keys were not properly set in the config:\n{errors}""" ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" self.create_and_test_config_common_properties() self.create_and_test_config_to_json_string() self.create_and_test_config_to_json_file() self.create_and_test_config_from_and_save_pretrained() self.create_and_test_config_from_and_save_pretrained_subfolder() self.create_and_test_config_with_num_labels() self.check_config_can_be_init_without_params() self.check_config_arguments_init()
546
"""simple docstring""" import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''kakaobrain/align-base''': '''https://huggingface.co/kakaobrain/align-base/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_text_model''' def __init__( self , _UpperCAmelCase=30522 , _UpperCAmelCase=768 , _UpperCAmelCase=12 , _UpperCAmelCase=12 , _UpperCAmelCase=3072 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0 , _UpperCAmelCase="absolute" , _UpperCAmelCase=True , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = vocab_size __a : Optional[int] = hidden_size __a : Dict = num_hidden_layers __a : List[Any] = num_attention_heads __a : Optional[int] = hidden_act __a : List[Any] = intermediate_size __a : List[Any] = hidden_dropout_prob __a : List[str] = attention_probs_dropout_prob __a : Optional[int] = max_position_embeddings __a : List[str] = type_vocab_size __a : Tuple = initializer_range __a : Dict = layer_norm_eps __a : Any = position_embedding_type __a : Dict = use_cache __a : Dict = pad_token_id @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : List[str] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the text config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Dict = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_vision_model''' def __init__( self , _UpperCAmelCase = 3 , _UpperCAmelCase = 600 , _UpperCAmelCase = 2.0 , _UpperCAmelCase = 3.1 , _UpperCAmelCase = 8 , _UpperCAmelCase = [3, 3, 5, 3, 5, 5, 3] , _UpperCAmelCase = [32, 16, 24, 40, 80, 112, 192] , _UpperCAmelCase = [16, 24, 40, 80, 112, 192, 320] , _UpperCAmelCase = [] , _UpperCAmelCase = [1, 2, 2, 2, 1, 2, 1] , _UpperCAmelCase = [1, 2, 2, 3, 3, 4, 1] , _UpperCAmelCase = [1, 6, 6, 6, 6, 6, 6] , _UpperCAmelCase = 0.2_5 , _UpperCAmelCase = "swish" , _UpperCAmelCase = 2560 , _UpperCAmelCase = "mean" , _UpperCAmelCase = 0.0_2 , _UpperCAmelCase = 0.0_0_1 , _UpperCAmelCase = 0.9_9 , _UpperCAmelCase = 0.2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : Tuple = num_channels __a : str = image_size __a : List[Any] = width_coefficient __a : Optional[int] = depth_coefficient __a : Union[str, Any] = depth_divisor __a : int = kernel_sizes __a : Dict = in_channels __a : List[str] = out_channels __a : Any = depthwise_padding __a : str = strides __a : Optional[Any] = num_block_repeats __a : Optional[Any] = expand_ratios __a : Any = squeeze_expansion_ratio __a : int = hidden_act __a : Union[str, Any] = hidden_dim __a : Union[str, Any] = pooling_type __a : Tuple = initializer_range __a : List[str] = batch_norm_eps __a : List[Any] = batch_norm_momentum __a : Union[str, Any] = drop_connect_rate __a : List[Any] = sum(_UpperCAmelCase ) * 4 @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : Optional[Any] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the vision config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Optional[Any] = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align''' __lowerCAmelCase = True def __init__( self , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=640 , _UpperCAmelCase=1.0 , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) if text_config is None: __a : Dict = {} logger.info('''text_config is None. Initializing the AlignTextConfig with default values.''' ) if vision_config is None: __a : Any = {} logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.''' ) __a : Any = AlignTextConfig(**_UpperCAmelCase ) __a : Any = AlignVisionConfig(**_UpperCAmelCase ) __a : Optional[int] = projection_dim __a : Union[str, Any] = temperature_init_value __a : int = initializer_range @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = copy.deepcopy(self.__dict__ ) __a : Tuple = self.text_config.to_dict() __a : Union[str, Any] = self.vision_config.to_dict() __a : int = self.__class__.model_type return output
52
0
from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar _UpperCAmelCase : int = TypeVar("""KEY""") _UpperCAmelCase : Dict = TypeVar("""VAL""") @dataclass(frozen=_UpperCamelCase , slots=_UpperCamelCase ) class lowercase ( Generic[KEY, VAL] ): __SCREAMING_SNAKE_CASE : Tuple = 42 __SCREAMING_SNAKE_CASE : List[str] = 42 class lowercase ( _Item ): def __init__( self ): super().__init__(_UpperCAmelCase , _UpperCAmelCase ) def __bool__( self ): return False _UpperCAmelCase : Optional[int] = _DeletedItem() class lowercase ( MutableMapping[KEY, VAL] ): def __init__( self , snake_case = 8 , snake_case = 0.75 ): snake_case_ = initial_block_size snake_case_ = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 snake_case_ = capacity_factor snake_case_ = 0 def a ( self , snake_case ): return hash(_UpperCAmelCase ) % len(self._buckets ) def a ( self , snake_case ): return (ind + 1) % len(self._buckets ) def a ( self , snake_case , snake_case , snake_case ): snake_case_ = self._buckets[ind] if not stored: snake_case_ = _Item(_UpperCAmelCase , _UpperCAmelCase ) self._len += 1 return True elif stored.key == key: snake_case_ = _Item(_UpperCAmelCase , _UpperCAmelCase ) return True else: return False def a ( self ): snake_case_ = len(self._buckets ) * self._capacity_factor return len(self ) >= int(_UpperCAmelCase ) def a ( self ): if len(self._buckets ) <= self._initial_block_size: return False snake_case_ = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def a ( self , snake_case ): snake_case_ = self._buckets snake_case_ = [None] * new_size snake_case_ = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def a ( self ): self._resize(len(self._buckets ) * 2 ) def a ( self ): self._resize(len(self._buckets ) // 2 ) def a ( self , snake_case ): snake_case_ = self._get_bucket_index(_UpperCAmelCase ) for _ in range(len(self._buckets ) ): yield ind snake_case_ = self._get_next_ind(_UpperCAmelCase ) def a ( self , snake_case , snake_case ): for ind in self._iterate_buckets(_UpperCAmelCase ): if self._try_set(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): break def __setitem__( self , snake_case , snake_case ): if self._is_full(): self._size_up() self._add_item(_UpperCAmelCase , _UpperCAmelCase ) def __delitem__( self , snake_case ): for ind in self._iterate_buckets(_UpperCAmelCase ): snake_case_ = self._buckets[ind] if item is None: raise KeyError(_UpperCAmelCase ) if item is _deleted: continue if item.key == key: snake_case_ = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self , snake_case ): for ind in self._iterate_buckets(_UpperCAmelCase ): snake_case_ = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(_UpperCAmelCase ) def __len__( self ): return self._len def __iter__( self ): yield from (item.key for item in self._buckets if item) def __repr__( self ): snake_case_ = ''' ,'''.join( F'''{item.key}: {item.val}''' for item in self._buckets if item ) return F'''HashMap({val_string})'''
362
"""simple docstring""" from __future__ import annotations from random import choice def __A ( a_ :Tuple) -> List[str]: return choice(a_) def __A ( a_ :list[int] , a_ :int) -> int: __a : Optional[int] = random_pivot(a_) # partition based on pivot # linear time __a : Union[str, Any] = [e for e in lst if e < pivot] __a : Any = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(a_) == k - 1: return pivot # pivot is in elements bigger than k elif len(a_) < k - 1: return kth_number(a_ , k - len(a_) - 1) # pivot is in elements smaller than k else: return kth_number(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [[0 for _ in range(a_ )] for _ in range(m + 1 )] for i in range(m + 1 ): _UpperCAmelCase = 1 for n in range(m + 1 ): for k in range(1 ,a_ ): memo[n][k] += memo[n][k - 1] if n - k > 0: memo[n][k] += memo[n - k - 1][k] return memo[m][m - 1] if __name__ == "__main__": import sys if len(sys.argv) == 1: try: UpperCAmelCase__ = int(input("""Enter a number: """).strip()) print(partition(n)) except ValueError: print("""Please enter a number.""") else: try: UpperCAmelCase__ = int(sys.argv[1]) print(partition(n)) except ValueError: print("""Please pass a number.""")
277
"""simple docstring""" import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A = logging.getLogger(__name__) def __A ( a_ :Union[str, Any] , a_ :Dict) -> Union[str, Any]: __a : Optional[int] = np.argmax(a_ , axis=1) return np.sum(outputs == labels) def __A ( a_ :Any) -> str: with open(a_ , encoding='''utf_8''') as f: __a : List[Any] = csv.reader(a_) __a : List[str] = [] next(a_) # skip the first line for line in tqdm(a_): output.append((''' '''.join(line[1:5]), line[5], line[6], int(line[-1]) - 1)) return output def __A ( a_ :Dict , a_ :str , a_ :str , a_ :List[Any] , a_ :Tuple , a_ :List[Any]) -> Any: __a : List[str] = [] for dataset in encoded_datasets: __a : List[str] = len(a_) __a : List[str] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa) __a : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa) __a : Tuple = np.full((n_batch, 2, input_len) , fill_value=-1_00 , dtype=np.intaa) __a : Optional[Any] = np.zeros((n_batch,) , dtype=np.intaa) for ( i, (story, conta, conta, mc_label), ) in enumerate(a_): __a : str = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __a : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __a : Tuple = with_conta __a : int = with_conta __a : List[str] = len(a_) - 1 __a : int = len(a_) - 1 __a : Optional[int] = with_conta __a : Tuple = with_conta __a : List[Any] = mc_label __a : Any = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(a_) for t in all_inputs)) return tensor_datasets def __A ( ) -> Union[str, Any]: __a : List[str] = argparse.ArgumentParser() parser.add_argument('''--model_name''' , type=a_ , default='''openai-gpt''' , help='''pretrained model name''') parser.add_argument('''--do_train''' , action='''store_true''' , help='''Whether to run training.''') parser.add_argument('''--do_eval''' , action='''store_true''' , help='''Whether to run eval on the dev set.''') parser.add_argument( '''--output_dir''' , default=a_ , type=a_ , required=a_ , help='''The output directory where the model predictions and checkpoints will be written.''' , ) parser.add_argument('''--train_dataset''' , type=a_ , default='''''') parser.add_argument('''--eval_dataset''' , type=a_ , default='''''') parser.add_argument('''--seed''' , type=a_ , default=42) parser.add_argument('''--num_train_epochs''' , type=a_ , default=3) parser.add_argument('''--train_batch_size''' , type=a_ , default=8) parser.add_argument('''--eval_batch_size''' , type=a_ , default=16) parser.add_argument('''--adam_epsilon''' , default=1e-8 , type=a_ , help='''Epsilon for Adam optimizer.''') parser.add_argument('''--max_grad_norm''' , type=a_ , default=1) parser.add_argument( '''--max_steps''' , default=-1 , type=a_ , help=( '''If > 0: set total number of training steps to perform. Override num_train_epochs.''' ) , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=a_ , default=1 , help='''Number of updates steps to accumulate before performing a backward/update pass.''' , ) parser.add_argument('''--learning_rate''' , type=a_ , default=6.25e-5) parser.add_argument('''--warmup_steps''' , default=0 , type=a_ , help='''Linear warmup over warmup_steps.''') parser.add_argument('''--lr_schedule''' , type=a_ , default='''warmup_linear''') parser.add_argument('''--weight_decay''' , type=a_ , default=0.0_1) parser.add_argument('''--lm_coef''' , type=a_ , default=0.9) parser.add_argument('''--n_valid''' , type=a_ , default=3_74) parser.add_argument('''--server_ip''' , type=a_ , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=a_ , default='''''' , help='''Can be used for distant debugging.''') __a : str = parser.parse_args() print(a_) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=a_) ptvsd.wait_for_attach() random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) __a : Tuple = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''') __a : str = torch.cuda.device_count() logger.info('''device: {}, n_gpu {}'''.format(a_ , a_)) if not args.do_train and not args.do_eval: raise ValueError('''At least one of `do_train` or `do_eval` must be True.''') if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset __a : List[str] = ['''_start_''', '''_delimiter_''', '''_classify_'''] __a : Union[str, Any] = OpenAIGPTTokenizer.from_pretrained(args.model_name) tokenizer.add_tokens(a_) __a : Union[str, Any] = tokenizer.convert_tokens_to_ids(a_) __a : Optional[Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name) model.resize_token_embeddings(len(a_)) model.to(a_) # Load and encode the datasets def tokenize_and_encode(a_ :List[Any]): if isinstance(a_ , a_): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(a_)) elif isinstance(a_ , a_): return obj return [tokenize_and_encode(a_) for o in obj] logger.info('''Encoding dataset...''') __a : Dict = load_rocstories_dataset(args.train_dataset) __a : int = load_rocstories_dataset(args.eval_dataset) __a : Optional[int] = (train_dataset, eval_dataset) __a : List[Any] = tokenize_and_encode(a_) # Compute the max input length for the Transformer __a : List[Any] = model.config.n_positions // 2 - 2 __a : int = max( len(story[:max_length]) + max(len(conta[:max_length]) , len(conta[:max_length])) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset) __a : Union[str, Any] = min(a_ , model.config.n_positions) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders __a : Tuple = pre_process_datasets(a_ , a_ , a_ , *a_) __a , __a : Tuple = tensor_datasets[0], tensor_datasets[1] __a : List[str] = TensorDataset(*a_) __a : Optional[Any] = RandomSampler(a_) __a : str = DataLoader(a_ , sampler=a_ , batch_size=args.train_batch_size) __a : List[str] = TensorDataset(*a_) __a : Optional[int] = SequentialSampler(a_) __a : Optional[Any] = DataLoader(a_ , sampler=a_ , batch_size=args.eval_batch_size) # Prepare optimizer if args.do_train: if args.max_steps > 0: __a : int = args.max_steps __a : Optional[int] = args.max_steps // (len(a_) // args.gradient_accumulation_steps) + 1 else: __a : str = len(a_) // args.gradient_accumulation_steps * args.num_train_epochs __a : List[Any] = list(model.named_parameters()) __a : Optional[int] = ['''bias''', '''LayerNorm.bias''', '''LayerNorm.weight'''] __a : List[str] = [ { '''params''': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], '''weight_decay''': args.weight_decay, }, {'''params''': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], '''weight_decay''': 0.0}, ] __a : int = AdamW(a_ , lr=args.learning_rate , eps=args.adam_epsilon) __a : Union[str, Any] = get_linear_schedule_with_warmup( a_ , num_warmup_steps=args.warmup_steps , num_training_steps=a_) if args.do_train: __a , __a , __a : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs) , desc='''Epoch'''): __a : Dict = 0 __a : Dict = 0 __a : List[str] = tqdm(a_ , desc='''Training''') for step, batch in enumerate(a_): __a : Dict = tuple(t.to(a_) for t in batch) __a , __a , __a , __a : str = batch __a : List[Any] = model(a_ , mc_token_ids=a_ , lm_labels=a_ , mc_labels=a_) __a : Optional[Any] = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() __a : int = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 __a : Tuple = '''Training loss: {:.2e} lr: {:.2e}'''.format(a_ , scheduler.get_lr()[0]) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer __a : Dict = model.module if hasattr(a_ , '''module''') else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` __a : int = os.path.join(args.output_dir , a_) __a : str = os.path.join(args.output_dir , a_) torch.save(model_to_save.state_dict() , a_) model_to_save.config.to_json_file(a_) tokenizer.save_vocabulary(args.output_dir) # Load a trained model and vocabulary that you have fine-tuned __a : str = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir) __a : Union[str, Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir) model.to(a_) if args.do_eval: model.eval() __a , __a : List[Any] = 0, 0 __a , __a : Union[str, Any] = 0, 0 for batch in tqdm(a_ , desc='''Evaluating'''): __a : str = tuple(t.to(a_) for t in batch) __a , __a , __a , __a : List[Any] = batch with torch.no_grad(): __a , __a , __a , __a : str = model( a_ , mc_token_ids=a_ , lm_labels=a_ , mc_labels=a_) __a : List[str] = mc_logits.detach().cpu().numpy() __a : Optional[Any] = mc_labels.to('''cpu''').numpy() __a : str = accuracy(a_ , a_) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0) nb_eval_steps += 1 __a : Tuple = eval_loss / nb_eval_steps __a : List[str] = eval_accuracy / nb_eval_examples __a : List[Any] = tr_loss / nb_tr_steps if args.do_train else None __a : List[str] = {'''eval_loss''': eval_loss, '''eval_accuracy''': eval_accuracy, '''train_loss''': train_loss} __a : Dict = os.path.join(args.output_dir , '''eval_results.txt''') with open(a_ , '''w''') as writer: logger.info('''***** Eval results *****''') for key in sorted(result.keys()): logger.info(''' %s = %s''' , a_ , str(result[key])) writer.write('''%s = %s\n''' % (key, str(result[key]))) if __name__ == "__main__": main()
52
0
from typing import Dict from .base import GenericTensor, Pipeline class lowerCAmelCase_ ( _UpperCamelCase ): '''simple docstring''' def __lowerCamelCase ( self , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase=None , **__UpperCAmelCase ): if tokenize_kwargs is None: SCREAMING_SNAKE_CASE_ : Dict ={} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( 'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)' ) SCREAMING_SNAKE_CASE_ : Tuple =truncation SCREAMING_SNAKE_CASE_ : List[Any] =tokenize_kwargs SCREAMING_SNAKE_CASE_ : List[str] ={} if return_tensors is not None: SCREAMING_SNAKE_CASE_ : Optional[Any] =return_tensors return preprocess_params, {}, postprocess_params def __lowerCamelCase ( self , __UpperCAmelCase , **__UpperCAmelCase ): SCREAMING_SNAKE_CASE_ : Optional[Any] =self.framework SCREAMING_SNAKE_CASE_ : Optional[Any] =self.tokenizer(_UpperCAmelCase , return_tensors=_UpperCAmelCase , **_UpperCAmelCase ) return model_inputs def __lowerCamelCase ( self , __UpperCAmelCase ): SCREAMING_SNAKE_CASE_ : int =self.model(**_UpperCAmelCase ) return model_outputs def __lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase=False ): # [0] is the first available tensor, logits or last_hidden_state. if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self , *__UpperCAmelCase , **__UpperCAmelCase ): return super().__call__(*_UpperCAmelCase , **_UpperCAmelCase )
220
"""simple docstring""" import unittest import numpy as np from transformers import RobertaConfig, 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.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=13 , _UpperCAmelCase=7 , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=99 , _UpperCAmelCase=32 , _UpperCAmelCase=5 , _UpperCAmelCase=4 , _UpperCAmelCase=37 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=16 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=4 , ): __a : Any = parent __a : Optional[int] = batch_size __a : str = seq_length __a : List[str] = is_training __a : Optional[Any] = use_attention_mask __a : Optional[Any] = use_token_type_ids __a : List[str] = use_labels __a : Union[str, Any] = vocab_size __a : int = hidden_size __a : Union[str, Any] = num_hidden_layers __a : Union[str, Any] = num_attention_heads __a : Dict = intermediate_size __a : List[str] = hidden_act __a : Dict = hidden_dropout_prob __a : Union[str, Any] = attention_probs_dropout_prob __a : int = max_position_embeddings __a : Tuple = type_vocab_size __a : Optional[int] = type_sequence_label_size __a : Optional[Any] = initializer_range __a : Optional[int] = num_choices def _lowerCamelCase ( self ): __a : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a : Union[str, Any] = None if self.use_attention_mask: __a : Any = random_attention_mask([self.batch_size, self.seq_length] ) __a : Optional[int] = None if self.use_token_type_ids: __a : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a : Any = RobertaConfig( 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 _lowerCamelCase ( self ): __a : Dict = self.prepare_config_and_inputs() __a , __a , __a , __a : str = config_and_inputs __a : str = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def _lowerCamelCase ( self ): __a : Any = self.prepare_config_and_inputs() __a , __a , __a , __a : Union[str, Any] = config_and_inputs __a : Optional[int] = True __a : str = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = True __lowerCAmelCase = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCamelCase ( self ): __a : Dict = FlaxRobertaModelTester(self ) @slow def _lowerCamelCase ( self ): for model_class_name in self.all_model_classes: __a : int = model_class_name.from_pretrained('''roberta-base''' , from_pt=_UpperCAmelCase ) __a : List[str] = model(np.ones((1, 1) ) ) self.assertIsNotNone(_UpperCAmelCase )
52
0
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset UpperCAmelCase_ : int = random.Random() def __SCREAMING_SNAKE_CASE ( a__ : Tuple ,a__ : Dict=1.0 ,a__ : str=None ,a__ : List[Any]=None ) -> Dict: if rng is None: __A : Any = global_rng __A : Tuple = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowerCamelCase_ ( unittest.TestCase ): def __init__( self : Any , __A : List[str] , __A : Optional[Any]=7 , __A : Dict=400 , __A : int=2000 , __A : Optional[int]=2048 , __A : Union[str, Any]=128 , __A : Optional[Any]=1 , __A : Optional[int]=512 , __A : Tuple=30 , __A : Optional[Any]=4_4100 , ): __A : Any = parent __A : Tuple = batch_size __A : Tuple = min_seq_length __A : List[str] = max_seq_length __A : List[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __A : Tuple = spectrogram_length __A : int = feature_size __A : int = num_audio_channels __A : Tuple = hop_length __A : List[Any] = chunk_length __A : Any = sampling_rate def lowerCAmelCase_ ( self : Tuple ): return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def lowerCAmelCase_ ( self : Union[str, Any] , __A : Tuple=False , __A : str=False ): def _flatten(__A : Union[str, Any] ): return list(itertools.chain(*_UpperCAmelCase ) ) if equal_length: __A : Tuple = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __A : Tuple = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __A : Optional[Any] = [np.asarray(_UpperCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowerCamelCase_ ( _UpperCamelCase , unittest.TestCase ): _lowercase : Optional[Any] = TvltFeatureExtractor def lowerCAmelCase_ ( self : Any ): __A : Optional[Any] = TvltFeatureExtractionTester(self ) def lowerCAmelCase_ ( self : List[str] ): __A : int = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_UpperCAmelCase , """spectrogram_length""" ) ) self.assertTrue(hasattr(_UpperCAmelCase , """feature_size""" ) ) self.assertTrue(hasattr(_UpperCAmelCase , """num_audio_channels""" ) ) self.assertTrue(hasattr(_UpperCAmelCase , """hop_length""" ) ) self.assertTrue(hasattr(_UpperCAmelCase , """chunk_length""" ) ) self.assertTrue(hasattr(_UpperCAmelCase , """sampling_rate""" ) ) def lowerCAmelCase_ ( self : List[Any] ): __A : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __A : List[str] = feat_extract_first.save_pretrained(_UpperCAmelCase )[0] check_json_file_has_correct_format(_UpperCAmelCase ) __A : Union[str, Any] = self.feature_extraction_class.from_pretrained(_UpperCAmelCase ) __A : Tuple = feat_extract_first.to_dict() __A : List[Any] = feat_extract_second.to_dict() __A : int = dict_first.pop("""mel_filters""" ) __A : List[Any] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def lowerCAmelCase_ ( self : Dict ): __A : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __A : int = os.path.join(_UpperCAmelCase , """feat_extract.json""" ) feat_extract_first.to_json_file(_UpperCAmelCase ) __A : Optional[Any] = self.feature_extraction_class.from_json_file(_UpperCAmelCase ) __A : Optional[Any] = feat_extract_first.to_dict() __A : Any = feat_extract_second.to_dict() __A : Optional[Any] = dict_first.pop("""mel_filters""" ) __A : Dict = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def lowerCAmelCase_ ( self : Dict ): # Initialize feature_extractor __A : str = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 __A : Any = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __A : Union[str, Any] = [np.asarray(_UpperCAmelCase ) for speech_input in speech_inputs] # Test not batched input __A : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched __A : int = feature_extractor(_UpperCAmelCase , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking __A : List[Any] = feature_extractor( _UpperCAmelCase , return_tensors="""np""" , sampling_rate=4_4100 , mask_audio=_UpperCAmelCase ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. __A : str = [floats_list((1, x) )[0] for x in (800, 800, 800)] __A : Any = np.asarray(_UpperCAmelCase ) __A : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def lowerCAmelCase_ ( self : Dict , __A : Dict ): __A : int = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech __A : int = ds.sort("""id""" ).select(range(_UpperCAmelCase ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def lowerCAmelCase_ ( self : Any ): __A : List[str] = self._load_datasamples(1 ) __A : Tuple = TvltFeatureExtractor() __A : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors="""pt""" ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) __A : Dict = torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _UpperCAmelCase , atol=1e-4 ) )
17
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''facebook/levit-128S''': '''https://huggingface.co/facebook/levit-128S/resolve/main/config.json''', # See all LeViT models at https://huggingface.co/models?filter=levit } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''levit''' def __init__( self , _UpperCAmelCase=224 , _UpperCAmelCase=3 , _UpperCAmelCase=3 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=16 , _UpperCAmelCase=[128, 256, 384] , _UpperCAmelCase=[4, 8, 12] , _UpperCAmelCase=[4, 4, 4] , _UpperCAmelCase=[16, 16, 16] , _UpperCAmelCase=0 , _UpperCAmelCase=[2, 2, 2] , _UpperCAmelCase=[2, 2, 2] , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = image_size __a : List[Any] = num_channels __a : Dict = kernel_size __a : Optional[int] = stride __a : Optional[int] = padding __a : Dict = hidden_sizes __a : int = num_attention_heads __a : Optional[int] = depths __a : str = key_dim __a : Union[str, Any] = drop_path_rate __a : Optional[Any] = patch_size __a : Tuple = attention_ratio __a : int = mlp_ratio __a : int = initializer_range __a : int = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = version.parse('''1.11''' ) @property def _lowerCamelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowerCamelCase ( self ): return 1e-4
52
0
from __future__ import annotations import unittest from transformers import AutoTokenizer, PegasusConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFPegasusForConditionalGeneration, TFPegasusModel @require_tf class lowercase : __a = PegasusConfig __a = {} __a = """gelu""" def __init__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=13 , SCREAMING_SNAKE_CASE__=7 , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=99 , SCREAMING_SNAKE_CASE__=32 , SCREAMING_SNAKE_CASE__=2 , SCREAMING_SNAKE_CASE__=4 , SCREAMING_SNAKE_CASE__=37 , SCREAMING_SNAKE_CASE__=0.1 , SCREAMING_SNAKE_CASE__=0.1 , SCREAMING_SNAKE_CASE__=40 , SCREAMING_SNAKE_CASE__=2 , SCREAMING_SNAKE_CASE__=1 , SCREAMING_SNAKE_CASE__=0 , ): """simple docstring""" lowerCAmelCase__ : Union[str, Any] = parent lowerCAmelCase__ : Tuple = batch_size lowerCAmelCase__ : Tuple = seq_length lowerCAmelCase__ : List[Any] = is_training lowerCAmelCase__ : Optional[int] = use_labels lowerCAmelCase__ : int = vocab_size lowerCAmelCase__ : Optional[Any] = hidden_size lowerCAmelCase__ : Union[str, Any] = num_hidden_layers lowerCAmelCase__ : Union[str, Any] = num_attention_heads lowerCAmelCase__ : Union[str, Any] = intermediate_size lowerCAmelCase__ : Tuple = hidden_dropout_prob lowerCAmelCase__ : int = attention_probs_dropout_prob lowerCAmelCase__ : Tuple = max_position_embeddings lowerCAmelCase__ : List[Any] = eos_token_id lowerCAmelCase__ : Optional[int] = pad_token_id lowerCAmelCase__ : Optional[int] = bos_token_id def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Dict = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) lowerCAmelCase__ : Union[str, Any] = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) lowerCAmelCase__ : Dict = tf.concat([input_ids, eos_tensor] , axis=1 ) lowerCAmelCase__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase__ : Optional[int] = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) lowerCAmelCase__ : Optional[Any] = prepare_pegasus_inputs_dict(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return config, inputs_dict def lowercase_ ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCAmelCase__ : Dict = TFPegasusModel(config=_UpperCAmelCase ).get_decoder() lowerCAmelCase__ : int = inputs_dict['''input_ids'''] lowerCAmelCase__ : List[str] = input_ids[:1, :] lowerCAmelCase__ : List[str] = inputs_dict['''attention_mask'''][:1, :] lowerCAmelCase__ : Optional[int] = inputs_dict['''head_mask'''] lowerCAmelCase__ : List[Any] = 1 # first forward pass lowerCAmelCase__ : Union[str, Any] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase , head_mask=_UpperCAmelCase , use_cache=_UpperCAmelCase ) lowerCAmelCase__ : Tuple = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids lowerCAmelCase__ : List[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowerCAmelCase__ : int = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and lowerCAmelCase__ : Optional[Any] = tf.concat([input_ids, next_tokens] , axis=-1 ) lowerCAmelCase__ : Dict = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) lowerCAmelCase__ : Tuple = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase )[0] lowerCAmelCase__ : Union[str, Any] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase , past_key_values=_UpperCAmelCase )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice lowerCAmelCase__ : Optional[Any] = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) lowerCAmelCase__ : Union[str, Any] = output_from_no_past[:, -3:, random_slice_idx] lowerCAmelCase__ : str = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_UpperCAmelCase , _UpperCAmelCase , rtol=1E-3 ) def _a ( __UpperCamelCase : List[Any] ,__UpperCamelCase : str ,__UpperCamelCase : int ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : Optional[Any]=None ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : List[Any]=None ,__UpperCamelCase : List[str]=None ,): if attention_mask is None: lowerCAmelCase__ : Dict = tf.cast(tf.math.not_equal(a_ ,config.pad_token_id ) ,tf.inta ) if decoder_attention_mask is None: lowerCAmelCase__ : Optional[Any] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape ,dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] ,config.pad_token_id ) ,tf.inta ), ] ,axis=-1 ,) if head_mask is None: lowerCAmelCase__ : Optional[int] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: lowerCAmelCase__ : Union[str, Any] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: lowerCAmelCase__ : Optional[Any] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowercase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): __a = (TFPegasusForConditionalGeneration, TFPegasusModel) if is_tf_available() else () __a = (TFPegasusForConditionalGeneration,) if is_tf_available() else () __a = ( { """conversational""": TFPegasusForConditionalGeneration, """feature-extraction""": TFPegasusModel, """summarization""": TFPegasusForConditionalGeneration, """text2text-generation""": TFPegasusForConditionalGeneration, """translation""": TFPegasusForConditionalGeneration, } if is_tf_available() else {} ) __a = True __a = False __a = False def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : List[str] = TFPegasusModelTester(self ) lowerCAmelCase__ : int = ConfigTester(self , config_class=_UpperCAmelCase ) def lowercase_ ( self ): """simple docstring""" self.config_tester.run_common_tests() def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_UpperCAmelCase ) @require_sentencepiece @require_tokenizers @require_tf class lowercase ( unittest.TestCase ): __a = [ """ PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.""", """ The London trio are up for best UK act and best album, as well as getting two nominations in the best song category.\"We got told like this morning \'Oh I think you\'re nominated\'\", said Dappy.\"And I was like \'Oh yeah, which one?\' And now we\'ve got nominated for four awards. I mean, wow!\"Bandmate Fazer added: \"We thought it\'s best of us to come down and mingle with everyone and say hello to the cameras. And now we find we\'ve got four nominations.\"The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn\'t be too disappointed if they didn\'t win this time around.\"At the end of the day we\'re grateful to be where we are in our careers.\"If it don\'t happen then it don\'t happen - live to fight another day and keep on making albums and hits for the fans.\"Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers\' All These Things That I\'ve Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year\'s Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border.\"We just done Edinburgh the other day,\" said Dappy.\"We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!\" """, ] __a = [ """California\'s largest electricity provider has cut power to hundreds of thousands of customers in an effort to""" """ reduce the risk of wildfires.""", """N-Dubz have revealed they\'re \"grateful\" to have been nominated for four Mobo Awards.""", ] # differs slightly from pytorch, likely due to numerical differences in linear layers __a = """google/pegasus-xsum""" @cached_property def lowercase_ ( self ): """simple docstring""" return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Any = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def lowercase_ ( self , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCAmelCase__ : str = self.translate_src_text(**_UpperCAmelCase ) assert self.expected_text == generated_words def lowercase_ ( self , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCAmelCase__ : List[Any] = self.tokenizer(self.src_text , **_UpperCAmelCase , padding=_UpperCAmelCase , return_tensors='''tf''' ) lowerCAmelCase__ : Tuple = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=_UpperCAmelCase , ) lowerCAmelCase__ : Any = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=_UpperCAmelCase ) return generated_words @slow def lowercase_ ( self ): """simple docstring""" self._assert_generated_batch_equal_expected()
233
"""simple docstring""" def __A ( a_ :Tuple , a_ :Union[str, Any] , a_ :int=False) -> List[str]: if isinstance(a_ , a_) and isinstance(a_ , a_): __a : List[str] = len(set_a.intersection(a_)) if alternative_union: __a : List[str] = len(a_) + len(a_) else: __a : int = len(set_a.union(a_)) return intersection / union if isinstance(a_ , (list, tuple)) and isinstance(a_ , (list, tuple)): __a : Union[str, Any] = [element for element in set_a if element in set_b] if alternative_union: __a : Union[str, Any] = len(a_) + len(a_) return len(a_) / union else: __a : List[Any] = set_a + [element for element in set_b if element not in set_a] return len(a_) / len(a_) return len(a_) / len(a_) return None if __name__ == "__main__": A = {'''a''', '''b''', '''c''', '''d''', '''e'''} A = {'''c''', '''d''', '''e''', '''f''', '''h''', '''i'''} print(jaccard_similarity(set_a, set_b))
52
0
"""simple docstring""" import numpy as np import torch from torch.nn import CrossEntropyLoss from transformers import AutoModelForCausalLM, AutoTokenizer import datasets from datasets import logging snake_case_ : List[Any] = """\ """ snake_case_ : List[str] = """ Perplexity (PPL) is one of the most common metrics for evaluating language models. It is defined as the exponentiated average negative log-likelihood of a sequence. For more information, see https://huggingface.co/docs/transformers/perplexity """ snake_case_ : Union[str, Any] = """ Args: model_id (str): model used for calculating Perplexity NOTE: Perplexity can only be calculated for causal language models. This includes models such as gpt2, causal variations of bert, causal versions of t5, and more (the full list can be found in the AutoModelForCausalLM documentation here: https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM ) input_texts (list of str): input text, each separate text snippet is one list entry. batch_size (int): the batch size to run texts through the model. Defaults to 16. add_start_token (bool): whether to add the start token to the texts, so the perplexity can include the probability of the first word. Defaults to True. device (str): device to run on, defaults to \'cuda\' when available Returns: perplexity: dictionary containing the perplexity scores for the texts in the input list, as well as the mean perplexity. If one of the input texts is longer than the max input length of the model, then it is truncated to the max length for the perplexity computation. Examples: Example 1: >>> perplexity = datasets.load_metric(\"perplexity\") >>> input_texts = [\"lorem ipsum\", \"Happy Birthday!\", \"Bienvenue\"] >>> results = perplexity.compute(model_id=\'gpt2\', ... add_start_token=False, ... input_texts=input_texts) # doctest:+ELLIPSIS >>> print(list(results.keys())) [\'perplexities\', \'mean_perplexity\'] >>> print(round(results[\"mean_perplexity\"], 2)) 78.22 >>> print(round(results[\"perplexities\"][0], 2)) 11.11 Example 2: >>> perplexity = datasets.load_metric(\"perplexity\") >>> input_texts = datasets.load_dataset(\"wikitext\", ... \"wikitext-2-raw-v1\", ... split=\"test\")[\"text\"][:50] # doctest:+ELLIPSIS [...] >>> input_texts = [s for s in input_texts if s!=\'\'] >>> results = perplexity.compute(model_id=\'gpt2\', ... input_texts=input_texts) # doctest:+ELLIPSIS >>> print(list(results.keys())) [\'perplexities\', \'mean_perplexity\'] >>> print(round(results[\"mean_perplexity\"], 2)) 60.35 >>> print(round(results[\"perplexities\"][0], 2)) 81.12 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case__ ( datasets.Metric ): def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "input_texts": datasets.Value("string" ), } ) , reference_urls=["https://huggingface.co/docs/transformers/perplexity"] , ) def __lowerCAmelCase ( self : Any , lowercase : int , lowercase : Optional[int] , lowercase : Union[str, Any] = 16 , lowercase : List[str] = True , lowercase : Dict=None ): '''simple docstring''' if device is not None: assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu." if device == "gpu": UpperCAmelCase : str = '''cuda''' else: UpperCAmelCase : str = '''cuda''' if torch.cuda.is_available() else '''cpu''' UpperCAmelCase : Any = AutoModelForCausalLM.from_pretrained(_UpperCAmelCase ) UpperCAmelCase : Union[str, Any] = model.to(_UpperCAmelCase ) UpperCAmelCase : str = AutoTokenizer.from_pretrained(_UpperCAmelCase ) # if batch_size > 1 (which generally leads to padding being required), and # if there is not an already assigned pad_token, assign an existing # special token to also be the padding token if tokenizer.pad_token is None and batch_size > 1: UpperCAmelCase : Optional[int] = list(tokenizer.special_tokens_map_extended.values() ) # check that the model already has at least one special token defined assert ( len(_UpperCAmelCase ) > 0 ), "If batch_size > 1, model must have at least one special token to use for padding. Please use a different model or set batch_size=1." # assign one of the special tokens to also be the pad token tokenizer.add_special_tokens({"pad_token": existing_special_tokens[0]} ) if add_start_token: # leave room for <BOS> token to be added: assert ( tokenizer.bos_token is not None ), "Input model must already have a BOS token if using add_start_token=True. Please use a different model, or set add_start_token=False" UpperCAmelCase : List[str] = model.config.max_length - 1 else: UpperCAmelCase : Optional[int] = model.config.max_length UpperCAmelCase : int = tokenizer( _UpperCAmelCase , add_special_tokens=_UpperCAmelCase , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , max_length=_UpperCAmelCase , return_tensors="pt" , return_attention_mask=_UpperCAmelCase , ).to(_UpperCAmelCase ) UpperCAmelCase : List[Any] = encodings['''input_ids'''] UpperCAmelCase : Tuple = encodings['''attention_mask'''] # check that each input is long enough: if add_start_token: assert torch.all(torch.ge(attn_masks.sum(1 ) , 1 ) ), "Each input text must be at least one token long." else: assert torch.all( torch.ge(attn_masks.sum(1 ) , 2 ) ), "When add_start_token=False, each input text must be at least two tokens long. Run with add_start_token=True if inputting strings of only one token, and remove all empty input strings." UpperCAmelCase : Any = [] UpperCAmelCase : int = CrossEntropyLoss(reduction="none" ) for start_index in logging.tqdm(range(0 , len(_UpperCAmelCase ) , _UpperCAmelCase ) ): UpperCAmelCase : List[Any] = min(start_index + batch_size , len(_UpperCAmelCase ) ) UpperCAmelCase : Optional[int] = encoded_texts[start_index:end_index] UpperCAmelCase : Dict = attn_masks[start_index:end_index] if add_start_token: UpperCAmelCase : Dict = torch.tensor([[tokenizer.bos_token_id]] * encoded_batch.size(dim=0 ) ).to(_UpperCAmelCase ) UpperCAmelCase : List[str] = torch.cat([bos_tokens_tensor, encoded_batch] , dim=1 ) UpperCAmelCase : Tuple = torch.cat( [torch.ones(bos_tokens_tensor.size() , dtype=torch.intaa ).to(_UpperCAmelCase ), attn_mask] , dim=1 ) UpperCAmelCase : List[str] = encoded_batch with torch.no_grad(): UpperCAmelCase : Union[str, Any] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase ).logits UpperCAmelCase : Optional[int] = out_logits[..., :-1, :].contiguous() UpperCAmelCase : Tuple = labels[..., 1:].contiguous() UpperCAmelCase : Tuple = attn_mask[..., 1:].contiguous() UpperCAmelCase : int = torch.expa( (loss_fct(shift_logits.transpose(1 , 2 ) , _UpperCAmelCase ) * shift_attention_mask_batch).sum(1 ) / shift_attention_mask_batch.sum(1 ) ) ppls += perplexity_batch.tolist() return {"perplexities": ppls, "mean_perplexity": np.mean(_UpperCAmelCase )}
595
"""simple docstring""" from __future__ import annotations import os from collections.abc import Mapping A = tuple[int, int] class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a : set[int] = vertices __a : dict[EdgeT, int] = { (min(_UpperCAmelCase ), max(_UpperCAmelCase )): weight for edge, weight in edges.items() } def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase ): self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) __a : Dict = weight def _lowerCamelCase ( self ): __a : Graph = Graph({min(self.vertices )} , {} ) __a : EdgeT __a : int __a : EdgeT __a : int while len(subgraph.vertices ) < len(self.vertices ): __a : Any = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: __a : List[str] = edge __a : Optional[int] = weight subgraph.add_edge(_UpperCAmelCase , _UpperCAmelCase ) return subgraph def __A ( a_ :str = "p107_network.txt") -> int: __a : str = os.path.abspath(os.path.dirname(a_)) __a : str = os.path.join(a_ , a_) __a : dict[EdgeT, int] = {} __a : list[str] __a : int __a : int with open(a_) as f: __a : Optional[int] = f.read().strip().split('''\n''') __a : Dict = [line.split(''',''') for line in data] for edgea in range(1 , len(a_)): for edgea in range(a_): if adjaceny_matrix[edgea][edgea] != "-": __a : Tuple = int(adjaceny_matrix[edgea][edgea]) __a : Graph = Graph(set(range(len(a_))) , a_) __a : Graph = graph.prims_algorithm() __a : int = sum(graph.edges.values()) __a : int = sum(subgraph.edges.values()) return initial_total - optimal_total if __name__ == "__main__": print(F'{solution() = }')
52
0
'''simple docstring''' import unittest import numpy as np from transformers import RobertaConfig, 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.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class lowerCamelCase ( unittest.TestCase ): def __init__( self , a_ , a_=13 , a_=7 , a_=True , a_=True , a_=True , a_=True , a_=99 , a_=32 , a_=5 , a_=4 , a_=37 , a_="gelu" , a_=0.1 , a_=0.1 , a_=512 , a_=16 , a_=2 , a_=0.02 , a_=4 , ): lowerCAmelCase : Any = parent lowerCAmelCase : Optional[int] = batch_size lowerCAmelCase : str = seq_length lowerCAmelCase : List[str] = is_training lowerCAmelCase : Optional[Any] = use_attention_mask lowerCAmelCase : Optional[Any] = use_token_type_ids lowerCAmelCase : List[str] = use_labels lowerCAmelCase : Union[str, Any] = vocab_size lowerCAmelCase : int = hidden_size lowerCAmelCase : Union[str, Any] = num_hidden_layers lowerCAmelCase : Union[str, Any] = num_attention_heads lowerCAmelCase : Dict = intermediate_size lowerCAmelCase : List[str] = hidden_act lowerCAmelCase : Dict = hidden_dropout_prob lowerCAmelCase : Union[str, Any] = attention_probs_dropout_prob lowerCAmelCase : int = max_position_embeddings lowerCAmelCase : Tuple = type_vocab_size lowerCAmelCase : Optional[int] = type_sequence_label_size lowerCAmelCase : Optional[Any] = initializer_range lowerCAmelCase : Optional[int] = num_choices def _lowerCamelCase ( self ): lowerCAmelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase : Union[str, Any] = None if self.use_attention_mask: lowerCAmelCase : Any = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase : Optional[int] = None if self.use_token_type_ids: lowerCAmelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCAmelCase : Any = RobertaConfig( 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 _lowerCamelCase ( self ): lowerCAmelCase : Dict = self.prepare_config_and_inputs() lowerCAmelCase : str = config_and_inputs lowerCAmelCase : str = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def _lowerCamelCase ( self ): lowerCAmelCase : Any = self.prepare_config_and_inputs() lowerCAmelCase : Union[str, Any] = config_and_inputs lowerCAmelCase : Optional[int] = True lowerCAmelCase : str = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowerCAmelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class lowerCamelCase ( _UpperCamelCase , unittest.TestCase ): snake_case_ = True snake_case_ = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCamelCase ( self ): lowerCAmelCase : Dict = FlaxRobertaModelTester(self ) @slow def _lowerCamelCase ( self ): for model_class_name in self.all_model_classes: lowerCAmelCase : int = model_class_name.from_pretrained("roberta-base" , from_pt=_UpperCAmelCase ) lowerCAmelCase : List[str] = model(np.ones((1, 1) ) ) self.assertIsNotNone(_UpperCAmelCase )
525
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''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 __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''trocr''' __lowerCAmelCase = ['''past_key_values'''] __lowerCAmelCase = { '''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model''', '''num_hidden_layers''': '''decoder_layers''', } def __init__( self , _UpperCAmelCase=50265 , _UpperCAmelCase=1024 , _UpperCAmelCase=12 , _UpperCAmelCase=16 , _UpperCAmelCase=4096 , _UpperCAmelCase="gelu" , _UpperCAmelCase=512 , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.0 , _UpperCAmelCase=0.0 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=1 , _UpperCAmelCase=0 , _UpperCAmelCase=2 , **_UpperCAmelCase , ): __a : List[str] = vocab_size __a : Optional[Any] = d_model __a : Optional[Any] = decoder_layers __a : Union[str, Any] = decoder_attention_heads __a : int = decoder_ffn_dim __a : List[Any] = activation_function __a : Any = max_position_embeddings __a : Dict = dropout __a : List[Any] = attention_dropout __a : Optional[Any] = activation_dropout __a : str = init_std __a : List[str] = decoder_layerdrop __a : Union[str, Any] = use_cache __a : Optional[Any] = scale_embedding __a : List[Any] = use_learned_position_embeddings __a : Optional[int] = layernorm_embedding super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , decoder_start_token_id=_UpperCAmelCase , **_UpperCAmelCase , )
52
0
'''simple docstring''' def lowerCamelCase__ ( A_ , A_ , A_ , A_=None ): UpperCAmelCase_ = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: UpperCAmelCase_ = True, True UpperCAmelCase_ = dfs(a_ , a_ , a_ , a_ ) return path def lowerCamelCase__ ( A_ , A_ ): UpperCAmelCase_ = 0 UpperCAmelCase_ = -1 for i in range(a_ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 UpperCAmelCase_ = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def lowerCamelCase__ ( A_ , A_ ): UpperCAmelCase_ = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] UpperCAmelCase_ = check_circuit_or_path(a_ , a_ ) if check == 3: print("graph is not Eulerian" ) print("no path" ) return UpperCAmelCase_ = 1 if check == 2: UpperCAmelCase_ = odd_node print("graph has a Euler path" ) if check == 1: print("graph has a Euler cycle" ) UpperCAmelCase_ = dfs(a_ , a_ , a_ ) print(a_ ) def lowerCamelCase__ ( ): UpperCAmelCase_ = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} UpperCAmelCase_ = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} UpperCAmelCase_ = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} UpperCAmelCase_ = {1: [2, 3], 2: [1, 3], 3: [1, 2]} UpperCAmelCase_ = { 1: [], 2: [] # all degree is zero } UpperCAmelCase_ = 10 check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) if __name__ == "__main__": main()
660
"""simple docstring""" import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def __A ( a_ :Union[str, Any] , a_ :Union[str, Any] , a_ :Optional[Any] , a_ :Optional[int]=5) -> List[Any]: # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count('''<mask>''') == 1 __a : Optional[Any] = torch.tensor(tokenizer.encode(a_ , add_special_tokens=a_)).unsqueeze(0) # Batch size 1 __a : Dict = model(a_)[0] # The last hidden-state is the first element of the output tuple __a : Tuple = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() __a : Any = logits[0, masked_index, :] __a : Any = logits.softmax(dim=0) __a , __a : Optional[Any] = prob.topk(k=a_ , dim=0) __a : Optional[int] = ''' '''.join( [tokenizer.convert_ids_to_tokens(indices[i].item()) for i in range(len(a_))]) __a : List[str] = tokenizer.mask_token __a : Optional[int] = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(''' ''')): __a : Optional[Any] = predicted_token_bpe.replace('''\u2581''' , ''' ''') if " {0}".format(a_) in masked_input: topk_filled_outputs.append( ( masked_input.replace(''' {0}'''.format(a_) , a_), values[index].item(), predicted_token, )) else: topk_filled_outputs.append( ( masked_input.replace(a_ , a_), values[index].item(), predicted_token, )) return topk_filled_outputs A = CamembertTokenizer.from_pretrained('''camembert-base''') A = CamembertForMaskedLM.from_pretrained('''camembert-base''') model.eval() A = '''Le camembert est <mask> :)''' print(fill_mask(masked_input, model, tokenizer, topk=3))
52
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __magic_name__ = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __magic_name__ = [ '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMForMultipleChoice''', '''XLMForQuestionAnswering''', '''XLMForQuestionAnsweringSimple''', '''XLMForSequenceClassification''', '''XLMForTokenClassification''', '''XLMModel''', '''XLMPreTrainedModel''', '''XLMWithLMHeadModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __magic_name__ = [ '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMForMultipleChoice''', '''TFXLMForQuestionAnsweringSimple''', '''TFXLMForSequenceClassification''', '''TFXLMForTokenClassification''', '''TFXLMMainLayer''', '''TFXLMModel''', '''TFXLMPreTrainedModel''', '''TFXLMWithLMHeadModel''', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys __magic_name__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
250
"""simple docstring""" import unittest from knapsack import greedy_knapsack as kp class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): __a : Optional[int] = [10, 20, 30, 40, 50, 60] __a : Union[str, Any] = [2, 4, 6, 8, 10, 12] __a : List[str] = 100 self.assertEqual(kp.calc_profit(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) , 210 ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''max_weight must greater than zero.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''Weight can not be negative.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''Profit can not be negative.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''max_weight must greater than zero.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex( _UpperCAmelCase , '''The length of profit and weight must be same.''' ) if __name__ == "__main__": unittest.main()
52
0
from __future__ import annotations import numpy as np def __lowerCAmelCase ( _A ): """simple docstring""" _lowercase = np.shape(a_ ) if rows != columns: _lowercase = ( '''\'table\' has to be of square shaped array but got a ''' f'''{rows}x{columns} array:\n{table}''' ) raise ValueError(a_ ) _lowercase = np.zeros((rows, columns) ) _lowercase = np.zeros((rows, columns) ) for i in range(a_ ): for j in range(a_ ): _lowercase = sum(lower[i][k] * upper[k][j] for k in range(a_ ) ) if upper[j][j] == 0: raise ArithmeticError("""No LU decomposition exists""" ) _lowercase = (table[i][j] - total) / upper[j][j] _lowercase = 1 for j in range(a_ ,a_ ): _lowercase = sum(lower[i][k] * upper[k][j] for k in range(a_ ) ) _lowercase = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
398
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = {} class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''llama''' __lowerCAmelCase = ['''past_key_values'''] def __init__( self , _UpperCAmelCase=32000 , _UpperCAmelCase=4096 , _UpperCAmelCase=11008 , _UpperCAmelCase=32 , _UpperCAmelCase=32 , _UpperCAmelCase=None , _UpperCAmelCase="silu" , _UpperCAmelCase=2048 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-6 , _UpperCAmelCase=True , _UpperCAmelCase=0 , _UpperCAmelCase=1 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=False , _UpperCAmelCase=None , **_UpperCAmelCase , ): __a : Dict = vocab_size __a : Union[str, Any] = max_position_embeddings __a : str = hidden_size __a : List[str] = intermediate_size __a : Any = num_hidden_layers __a : int = num_attention_heads # for backward compatibility if num_key_value_heads is None: __a : Union[str, Any] = num_attention_heads __a : Optional[int] = num_key_value_heads __a : Dict = hidden_act __a : Union[str, Any] = initializer_range __a : int = rms_norm_eps __a : Optional[int] = pretraining_tp __a : Optional[Any] = use_cache __a : Optional[Any] = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , tie_word_embeddings=_UpperCAmelCase , **_UpperCAmelCase , ) def _lowerCamelCase ( self ): if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _UpperCAmelCase ) 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}""" ) __a : Tuple = self.rope_scaling.get('''type''' , _UpperCAmelCase ) __a : Optional[int] = self.rope_scaling.get('''factor''' , _UpperCAmelCase ) 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(_UpperCAmelCase , _UpperCAmelCase ) or rope_scaling_factor <= 1.0: raise ValueError(f"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""" )
52
0
'''simple docstring''' import argparse import os import sys from unittest.mock import patch import pytorch_lightning as pl import timeout_decorator import torch from distillation import SummarizationDistiller, distill_main from finetune import SummarizationModule, main from transformers import MarianMTModel from transformers.file_utils import cached_path from transformers.testing_utils import TestCasePlus, require_torch_gpu, slow from utils import load_json __UpperCAmelCase ="sshleifer/mar_enro_6_3_student" class a__ ( _UpperCamelCase ): def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" super().setUp() __lowerCamelCase = cached_path( '''https://cdn-datasets.huggingface.co/translation/wmt_en_ro-tr40k-va0.5k-te0.5k.tar.gz''' , extract_compressed_file=_UpperCAmelCase , ) __lowerCamelCase = f"""{data_cached}/wmt_en_ro-tr40k-va0.5k-te0.5k""" @slow @require_torch_gpu def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" MarianMTModel.from_pretrained(_UpperCAmelCase ) @slow @require_torch_gpu def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = { '''$MAX_LEN''': 64, '''$BS''': 64, '''$GAS''': 1, '''$ENRO_DIR''': self.data_dir, '''facebook/mbart-large-cc25''': MARIAN_MODEL, # "val_check_interval=0.25": "val_check_interval=1.0", '''--learning_rate=3e-5''': '''--learning_rate 3e-4''', '''--num_train_epochs 6''': '''--num_train_epochs 1''', } # Clean up bash script __lowerCamelCase = (self.test_file_dir / '''train_mbart_cc25_enro.sh''').open().read().split('''finetune.py''' )[1].strip() __lowerCamelCase = bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) for k, v in env_vars_to_replace.items(): __lowerCamelCase = bash_script.replace(_UpperCAmelCase , str(_UpperCAmelCase ) ) __lowerCamelCase = self.get_auto_remove_tmp_dir() # bash_script = bash_script.replace("--fp16 ", "") __lowerCamelCase = f""" --output_dir {output_dir} --tokenizer_name Helsinki-NLP/opus-mt-en-ro --sortish_sampler --do_predict --gpus 1 --freeze_encoder --n_train 40000 --n_val 500 --n_test 500 --fp16_opt_level O1 --num_sanity_val_steps 0 --eval_beams 2 """.split() # XXX: args.gpus > 1 : handle multi_gpu in the future __lowerCamelCase = ['''finetune.py'''] + bash_script.split() + args with patch.object(_UpperCAmelCase , '''argv''' , _UpperCAmelCase ): __lowerCamelCase = argparse.ArgumentParser() __lowerCamelCase = pl.Trainer.add_argparse_args(_UpperCAmelCase ) __lowerCamelCase = SummarizationModule.add_model_specific_args(_UpperCAmelCase , os.getcwd() ) __lowerCamelCase = parser.parse_args() __lowerCamelCase = main(_UpperCAmelCase ) # Check metrics __lowerCamelCase = load_json(model.metrics_save_path ) __lowerCamelCase = metrics['''val'''][0] __lowerCamelCase = metrics['''val'''][-1] self.assertEqual(len(metrics['''val'''] ) , (args.max_epochs / args.val_check_interval) ) assert isinstance(last_step_stats[f"""val_avg_{model.val_metric}"""] , _UpperCAmelCase ) self.assertGreater(last_step_stats['''val_avg_gen_time'''] , 0.01 ) # model hanging on generate. Maybe bad config was saved. (XXX: old comment/assert?) self.assertLessEqual(last_step_stats['''val_avg_gen_time'''] , 1.0 ) # test learning requirements: # 1. BLEU improves over the course of training by more than 2 pts self.assertGreater(last_step_stats['''val_avg_bleu'''] - first_step_stats['''val_avg_bleu'''] , 2 ) # 2. BLEU finishes above 17 self.assertGreater(last_step_stats['''val_avg_bleu'''] , 17 ) # 3. test BLEU and val BLEU within ~1.1 pt. self.assertLess(abs(metrics['''val'''][-1]['''val_avg_bleu'''] - metrics['''test'''][-1]['''test_avg_bleu'''] ) , 1.1 ) # check lightning ckpt can be loaded and has a reasonable statedict __lowerCamelCase = os.listdir(_UpperCAmelCase ) __lowerCamelCase = [x for x in contents if x.endswith('''.ckpt''' )][0] __lowerCamelCase = os.path.join(args.output_dir , _UpperCAmelCase ) __lowerCamelCase = torch.load(_UpperCAmelCase , map_location='''cpu''' ) __lowerCamelCase = '''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: __lowerCamelCase = {os.path.basename(_UpperCAmelCase ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1 class a__ ( _UpperCamelCase ): @timeout_decorator.timeout(6_00 ) @slow @require_torch_gpu def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = f"""{self.test_file_dir_str}/test_data/wmt_en_ro""" __lowerCamelCase = { '''--fp16_opt_level=O1''': '''''', '''$MAX_LEN''': 1_28, '''$BS''': 16, '''$GAS''': 1, '''$ENRO_DIR''': data_dir, '''$m''': '''sshleifer/student_marian_en_ro_6_1''', '''val_check_interval=0.25''': '''val_check_interval=1.0''', } # Clean up bash script __lowerCamelCase = ( (self.test_file_dir / '''distil_marian_no_teacher.sh''').open().read().split('''distillation.py''' )[1].strip() ) __lowerCamelCase = bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) __lowerCamelCase = bash_script.replace('''--fp16 ''' , ''' ''' ) for k, v in env_vars_to_replace.items(): __lowerCamelCase = bash_script.replace(_UpperCAmelCase , str(_UpperCAmelCase ) ) __lowerCamelCase = self.get_auto_remove_tmp_dir() __lowerCamelCase = bash_script.replace('''--fp16''' , '''''' ) __lowerCamelCase = 6 __lowerCamelCase = ( ['''distillation.py'''] + bash_script.split() + [ f"""--output_dir={output_dir}""", '''--gpus=1''', '''--learning_rate=1e-3''', f"""--num_train_epochs={epochs}""", '''--warmup_steps=10''', '''--val_check_interval=1.0''', '''--do_predict''', ] ) with patch.object(_UpperCAmelCase , '''argv''' , _UpperCAmelCase ): __lowerCamelCase = argparse.ArgumentParser() __lowerCamelCase = pl.Trainer.add_argparse_args(_UpperCAmelCase ) __lowerCamelCase = SummarizationDistiller.add_model_specific_args(_UpperCAmelCase , os.getcwd() ) __lowerCamelCase = parser.parse_args() # assert args.gpus == gpus THIS BREAKS for multi_gpu __lowerCamelCase = distill_main(_UpperCAmelCase ) # Check metrics __lowerCamelCase = load_json(model.metrics_save_path ) __lowerCamelCase = metrics['''val'''][0] __lowerCamelCase = metrics['''val'''][-1] assert len(metrics['''val'''] ) >= (args.max_epochs / args.val_check_interval) # +1 accounts for val_sanity_check assert last_step_stats["val_avg_gen_time"] >= 0.01 assert first_step_stats["val_avg_bleu"] < last_step_stats["val_avg_bleu"] # model learned nothing assert 1.0 >= last_step_stats["val_avg_gen_time"] # model hanging on generate. Maybe bad config was saved. assert isinstance(last_step_stats[f"""val_avg_{model.val_metric}"""] , _UpperCAmelCase ) # check lightning ckpt can be loaded and has a reasonable statedict __lowerCamelCase = os.listdir(_UpperCAmelCase ) __lowerCamelCase = [x for x in contents if x.endswith('''.ckpt''' )][0] __lowerCamelCase = os.path.join(args.output_dir , _UpperCAmelCase ) __lowerCamelCase = torch.load(_UpperCAmelCase , map_location='''cpu''' ) __lowerCamelCase = '''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: __lowerCamelCase = {os.path.basename(_UpperCAmelCase ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1
546
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=3 , _UpperCAmelCase=18 , _UpperCAmelCase=30 , _UpperCAmelCase=400 , _UpperCAmelCase=True , _UpperCAmelCase=None , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=[0.5, 0.5, 0.5] , _UpperCAmelCase=[0.5, 0.5, 0.5] , ): __a : int = parent __a : str = batch_size __a : List[Any] = num_channels __a : Union[str, Any] = image_size __a : List[Any] = min_resolution __a : str = max_resolution __a : List[str] = do_resize __a : Optional[int] = size if size is not None else {'''height''': 18, '''width''': 20} __a : str = do_thumbnail __a : str = do_align_axis __a : Dict = do_pad __a : Union[str, Any] = do_normalize __a : List[str] = image_mean __a : Optional[int] = image_std def _lowerCamelCase ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = DonutImageProcessor if is_vision_available() else None def _lowerCamelCase ( self ): __a : Tuple = DonutImageProcessingTester(self ) @property def _lowerCamelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_resize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_thumbnail''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_align_long_axis''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_pad''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_mean''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_std''' ) ) def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 20} ) __a : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) # Previous config had dimensions in (width, height) order __a : int = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'''height''': 84, '''width''': 42} ) def _lowerCamelCase ( self ): pass @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , Image.Image ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : int = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , numpify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , np.ndarray ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : str = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , torchify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , torch.Tensor ) # Test not batched input __a : Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : List[str] = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
52
0
import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class lowercase ( _UpperCamelCase ): def a ( self ): snake_case_ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_UpperCAmelCase , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(_UpperCAmelCase , 'num_attention_heads' ) ) self.parent.assertTrue(hasattr(_UpperCAmelCase , 'num_encoder_blocks' ) ) class lowercase : def __init__( self , snake_case , snake_case=13 , snake_case=64 , snake_case=3 , snake_case=4 , snake_case=[2, 2, 2, 2] , snake_case=[8, 4, 2, 1] , snake_case=[16, 32, 64, 128] , snake_case=[1, 4, 8, 16] , snake_case=[1, 2, 4, 8] , snake_case=True , snake_case=True , snake_case="gelu" , snake_case=0.1 , snake_case=0.1 , snake_case=0.02 , snake_case=3 , snake_case=None , ): snake_case_ = parent snake_case_ = batch_size snake_case_ = image_size snake_case_ = num_channels snake_case_ = num_encoder_blocks snake_case_ = sr_ratios snake_case_ = depths snake_case_ = hidden_sizes snake_case_ = downsampling_rates snake_case_ = num_attention_heads snake_case_ = is_training snake_case_ = use_labels snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = initializer_range snake_case_ = num_labels snake_case_ = scope def a ( self ): 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.image_size, self.image_size] , self.num_labels ) snake_case_ = self.get_config() return config, pixel_values, labels def a ( self ): return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def a ( self , snake_case , snake_case , snake_case ): snake_case_ = SegformerModel(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() snake_case_ = model(_UpperCAmelCase ) snake_case_ = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def a ( self , snake_case , snake_case , snake_case ): snake_case_ = self.num_labels snake_case_ = SegformerForSemanticSegmentation(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() snake_case_ = model(_UpperCAmelCase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) snake_case_ = model(_UpperCAmelCase , labels=_UpperCAmelCase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def a ( self , snake_case , snake_case , snake_case ): snake_case_ = 1 snake_case_ = SegformerForSemanticSegmentation(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() snake_case_ = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(_UpperCAmelCase ) snake_case_ = model(_UpperCAmelCase , labels=_UpperCAmelCase ) self.parent.assertGreater(result.loss , 0.0 ) def a ( self ): snake_case_ = self.prepare_config_and_inputs() snake_case_ = config_and_inputs snake_case_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowercase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): __SCREAMING_SNAKE_CASE : Tuple = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) __SCREAMING_SNAKE_CASE : Tuple = ( { '''feature-extraction''': SegformerModel, '''image-classification''': SegformerForImageClassification, '''image-segmentation''': SegformerForSemanticSegmentation, } if is_torch_available() else {} ) __SCREAMING_SNAKE_CASE : Optional[Any] = True __SCREAMING_SNAKE_CASE : int = False __SCREAMING_SNAKE_CASE : Dict = False __SCREAMING_SNAKE_CASE : Any = False def a ( self ): snake_case_ = SegformerModelTester(self ) snake_case_ = SegformerConfigTester(self , config_class=_UpperCAmelCase ) def a ( self ): self.config_tester.run_common_tests() def a ( self ): snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCAmelCase ) def a ( self ): snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*_UpperCAmelCase ) def a ( self ): snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*_UpperCAmelCase ) @unittest.skip('SegFormer does not use inputs_embeds' ) def a ( self ): pass @unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods' ) def a ( self ): pass def a ( self ): snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = model_class(_UpperCAmelCase ) snake_case_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ = [*signature.parameters.keys()] snake_case_ = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _UpperCAmelCase ) def a ( self ): snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = True for model_class in self.all_model_classes: snake_case_ = True snake_case_ = False snake_case_ = True snake_case_ = model_class(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() with torch.no_grad(): snake_case_ = model(**self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) ) snake_case_ = outputs.attentions snake_case_ = sum(self.model_tester.depths ) self.assertEqual(len(_UpperCAmelCase ) , _UpperCAmelCase ) # check that output_attentions also work using config del inputs_dict["output_attentions"] snake_case_ = True snake_case_ = model_class(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() with torch.no_grad(): snake_case_ = model(**self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) ) snake_case_ = outputs.attentions self.assertEqual(len(_UpperCAmelCase ) , _UpperCAmelCase ) # verify the first attentions (first block, first layer) snake_case_ = (self.model_tester.image_size // 4) ** 2 snake_case_ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) snake_case_ = (self.model_tester.image_size // 32) ** 2 snake_case_ = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) snake_case_ = len(_UpperCAmelCase ) # Check attention is always last and order is fine snake_case_ = True snake_case_ = True snake_case_ = model_class(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() with torch.no_grad(): snake_case_ = model(**self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(out_len + 1 , len(_UpperCAmelCase ) ) snake_case_ = outputs.attentions self.assertEqual(len(_UpperCAmelCase ) , _UpperCAmelCase ) # verify the first attentions (first block, first layer) snake_case_ = (self.model_tester.image_size // 4) ** 2 snake_case_ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def a ( self ): def check_hidden_states_output(snake_case , snake_case , snake_case ): snake_case_ = model_class(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() with torch.no_grad(): snake_case_ = model(**self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) ) snake_case_ = outputs.hidden_states snake_case_ = self.model_tester.num_encoder_blocks self.assertEqual(len(_UpperCAmelCase ) , _UpperCAmelCase ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = True check_hidden_states_output(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ = True check_hidden_states_output(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def a ( self ): if not self.model_tester.is_training: return snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = True for model_class in self.all_model_classes: if model_class in get_values(_UpperCAmelCase ): continue snake_case_ = model_class(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.train() snake_case_ = self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase , return_labels=_UpperCAmelCase ) snake_case_ = model(**_UpperCAmelCase ).loss loss.backward() @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def a ( self ): pass @slow def a ( self ): for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = SegformerModel.from_pretrained(_UpperCAmelCase ) self.assertIsNotNone(_UpperCAmelCase ) def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class lowercase ( unittest.TestCase ): @slow def a ( self ): # only resize + normalize snake_case_ = SegformerImageProcessor( image_scale=(512, 512) , keep_ratio=_UpperCAmelCase , align=_UpperCAmelCase , do_random_crop=_UpperCAmelCase ) snake_case_ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _UpperCAmelCase ) snake_case_ = prepare_img() snake_case_ = image_processor(images=_UpperCAmelCase , return_tensors='pt' ) snake_case_ = encoded_inputs.pixel_values.to(_UpperCAmelCase ) with torch.no_grad(): snake_case_ = model(_UpperCAmelCase ) snake_case_ = torch.Size((1, model.config.num_labels, 128, 128) ) self.assertEqual(outputs.logits.shape , _UpperCAmelCase ) snake_case_ = torch.tensor( [ [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]], [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]], [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]], ] ).to(_UpperCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _UpperCAmelCase , atol=1e-4 ) ) @slow def a ( self ): # only resize + normalize snake_case_ = SegformerImageProcessor( image_scale=(512, 512) , keep_ratio=_UpperCAmelCase , align=_UpperCAmelCase , do_random_crop=_UpperCAmelCase ) snake_case_ = SegformerForSemanticSegmentation.from_pretrained( 'nvidia/segformer-b1-finetuned-cityscapes-1024-1024' ).to(_UpperCAmelCase ) snake_case_ = prepare_img() snake_case_ = image_processor(images=_UpperCAmelCase , return_tensors='pt' ) snake_case_ = encoded_inputs.pixel_values.to(_UpperCAmelCase ) with torch.no_grad(): snake_case_ = model(_UpperCAmelCase ) snake_case_ = torch.Size((1, model.config.num_labels, 128, 128) ) self.assertEqual(outputs.logits.shape , _UpperCAmelCase ) snake_case_ = torch.tensor( [ [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]], [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]], [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]], ] ).to(_UpperCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _UpperCAmelCase , atol=1e-1 ) ) @slow def a ( self ): # only resize + normalize snake_case_ = SegformerImageProcessor( image_scale=(512, 512) , keep_ratio=_UpperCAmelCase , align=_UpperCAmelCase , do_random_crop=_UpperCAmelCase ) snake_case_ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _UpperCAmelCase ) snake_case_ = prepare_img() snake_case_ = image_processor(images=_UpperCAmelCase , return_tensors='pt' ) snake_case_ = encoded_inputs.pixel_values.to(_UpperCAmelCase ) with torch.no_grad(): snake_case_ = model(_UpperCAmelCase ) snake_case_ = outputs.logits.detach().cpu() snake_case_ = image_processor.post_process_semantic_segmentation(outputs=_UpperCAmelCase , target_sizes=[(500, 300)] ) snake_case_ = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , _UpperCAmelCase ) snake_case_ = image_processor.post_process_semantic_segmentation(outputs=_UpperCAmelCase ) snake_case_ = torch.Size((128, 128) ) self.assertEqual(segmentation[0].shape , _UpperCAmelCase )
362
"""simple docstring""" from __future__ import annotations def __A ( a_ :list[int]) -> int: if not nums: return 0 __a : Any = nums[0] __a : Optional[Any] = 0 for num in nums[1:]: __a , __a : Optional[Any] = ( max_excluding + num, max(a_ , a_), ) return max(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ,lowercase=5 ): """simple docstring""" # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count("""<mask>""" ) == 1 _UpperCAmelCase = torch.tensor(tokenizer.encode(a_ ,add_special_tokens=a_ ) ).unsqueeze(0 ) # Batch size 1 _UpperCAmelCase = model(a_ )[0] # The last hidden-state is the first element of the output tuple _UpperCAmelCase = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() _UpperCAmelCase = logits[0, masked_index, :] _UpperCAmelCase = logits.softmax(dim=0 ) _UpperCAmelCase = prob.topk(k=a_ ,dim=0 ) _UpperCAmelCase = ''' '''.join( [tokenizer.convert_ids_to_tokens(indices[i].item() ) for i in range(len(a_ ) )] ) _UpperCAmelCase = tokenizer.mask_token _UpperCAmelCase = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(""" """ ) ): _UpperCAmelCase = predicted_token_bpe.replace("""\u2581""" ,""" """ ) if " {0}".format(a_ ) in masked_input: topk_filled_outputs.append( ( masked_input.replace(""" {0}""".format(a_ ) ,a_ ), values[index].item(), predicted_token, ) ) else: topk_filled_outputs.append( ( masked_input.replace(a_ ,a_ ), values[index].item(), predicted_token, ) ) return topk_filled_outputs UpperCAmelCase__ = CamembertTokenizer.from_pretrained("""camembert-base""") UpperCAmelCase__ = CamembertForMaskedLM.from_pretrained("""camembert-base""") model.eval() UpperCAmelCase__ = """Le camembert est <mask> :)""" print(fill_mask(masked_input, model, tokenizer, topk=3))
277
"""simple docstring""" import unittest from transformers import BigBirdTokenizer, BigBirdTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A = '''▁''' A = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = BigBirdTokenizer __lowerCAmelCase = BigBirdTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = True def _lowerCamelCase ( self ): super().setUp() __a : Dict = self.tokenizer_class(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCamelCase ( self ): __a : List[str] = '''<s>''' __a : Tuple = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''[MASK]''' ) self.assertEqual(len(_UpperCAmelCase ) , 1004 ) def _lowerCamelCase ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def _lowerCamelCase ( self ): if not self.test_rust_tokenizer: return __a : Dict = self.get_tokenizer() __a : Any = self.get_rust_tokenizer() __a : int = '''I was born in 92000, and this is falsé.''' __a : Optional[Any] = tokenizer.tokenize(_UpperCAmelCase ) __a : List[str] = rust_tokenizer.tokenize(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Dict = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) __a : Any = rust_tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Tuple = self.get_rust_tokenizer() __a : Tuple = tokenizer.encode(_UpperCAmelCase ) __a : List[Any] = rust_tokenizer.encode(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = BigBirdTokenizer(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) __a : Optional[int] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) __a : Optional[int] = 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''', '''é''', '''.''', ] , ) __a : Optional[Any] = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) __a : Optional[int] = 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>''', '''.''', ] , ) @cached_property def _lowerCamelCase ( self ): return BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) @slow def _lowerCamelCase ( self ): __a : str = '''Hello World!''' __a : str = [65, 18536, 2260, 101, 66] self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @slow def _lowerCamelCase ( self ): __a : 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 __a : Optional[Any] = [65, 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, 66] # noqa: E231 # fmt: on self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @require_torch @slow def _lowerCamelCase ( self ): import torch from transformers import BigBirdConfig, BigBirdModel # Build sequence __a : List[Any] = list(self.big_tokenizer.get_vocab().keys() )[:10] __a : List[str] = ''' '''.join(_UpperCAmelCase ) __a : Tuple = self.big_tokenizer.encode_plus(_UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Any = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Optional[Any] = BigBirdConfig(attention_type='''original_full''' ) __a : Tuple = BigBirdModel(_UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_UpperCAmelCase ) model(**_UpperCAmelCase ) @slow def _lowerCamelCase ( self ): __a : Union[str, Any] = BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) __a : List[Any] = tokenizer.decode(tokenizer('''Paris is the [MASK].''' ).input_ids ) self.assertTrue(decoded_text == '''[CLS] Paris is the[MASK].[SEP]''' ) @slow def _lowerCamelCase ( self ): # fmt: off __a : Optional[Any] = {'''input_ids''': [[65, 39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114, 66], [65, 448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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], [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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name='''google/bigbird-roberta-base''' , revision='''215c99f1600e06f83acce68422f2035b2b5c3510''' , )
52
0
import random def SCREAMING_SNAKE_CASE__ ( lowerCAmelCase_ : int ,lowerCAmelCase_ : float ,lowerCAmelCase_ : bool = False ) -> dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : dict ={i: [] for i in range(a_ )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(a_ ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(a_ ): for j in range(i + 1 ,a_ ): if random.random() < probability: graph[i].append(a_ ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(a_ ) return graph def SCREAMING_SNAKE_CASE__ ( lowerCAmelCase_ : int ) -> dict: """simple docstring""" return { i: [j for j in range(a_ ) if i != j] for i in range(a_ ) } if __name__ == "__main__": import doctest doctest.testmod()
220
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A = logging.get_logger(__name__) A = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''convnextv2''' def __init__( self , _UpperCAmelCase=3 , _UpperCAmelCase=4 , _UpperCAmelCase=4 , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=224 , _UpperCAmelCase=None , _UpperCAmelCase=None , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : List[str] = num_channels __a : str = patch_size __a : Dict = num_stages __a : List[str] = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes __a : List[str] = [3, 3, 9, 3] if depths is None else depths __a : List[Any] = hidden_act __a : Any = initializer_range __a : Optional[int] = layer_norm_eps __a : List[Any] = drop_path_rate __a : Any = image_size __a : str = ['''stem'''] + [f"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] __a , __a : Optional[int] = get_aligned_output_features_output_indices( out_features=_UpperCAmelCase , out_indices=_UpperCAmelCase , stage_names=self.stage_names )
52
0
import requests UpperCAmelCase_ : Union[str, Any] = '''''' # <-- Put your OpenWeatherMap appid here! UpperCAmelCase_ : Tuple = '''https://api.openweathermap.org/data/2.5/''' def __SCREAMING_SNAKE_CASE ( a__ : str = "Chicago" ,a__ : str = APPID ) -> dict: return requests.get(URL_BASE + """weather""" ,params=locals() ).json() def __SCREAMING_SNAKE_CASE ( a__ : str = "Kolkata, India" ,a__ : str = APPID ) -> dict: return requests.get(URL_BASE + """forecast""" ,params=locals() ).json() def __SCREAMING_SNAKE_CASE ( a__ : float = 55.68 ,a__ : float = 12.57 ,a__ : str = APPID ) -> dict: return requests.get(URL_BASE + """onecall""" ,params=locals() ).json() if __name__ == "__main__": from pprint import pprint while True: UpperCAmelCase_ : Optional[Any] = input('''Enter a location:''').strip() if location: pprint(current_weather(location)) else: break
17
"""simple docstring""" import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = (DDPMScheduler,) def _lowerCamelCase ( self , **_UpperCAmelCase ): __a : int = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**_UpperCAmelCase ) return config def _lowerCamelCase ( self ): for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_UpperCAmelCase , beta_end=_UpperCAmelCase ) def _lowerCamelCase ( self ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_UpperCAmelCase ) def _lowerCamelCase ( self ): for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=_UpperCAmelCase ) def _lowerCamelCase ( self ): self.check_over_configs(thresholding=_UpperCAmelCase ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_UpperCAmelCase , prediction_type=_UpperCAmelCase , sample_max_value=_UpperCAmelCase , ) def _lowerCamelCase ( self ): for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for t in [0, 500, 999]: self.check_over_forward(time_step=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Dict = scheduler_class(**_UpperCAmelCase ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def _lowerCamelCase ( self ): __a : int = self.scheduler_classes[0] __a : int = self.get_scheduler_config() __a : Optional[Any] = scheduler_class(**_UpperCAmelCase ) __a : int = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[Any] = self.dummy_sample_deter __a : Union[str, Any] = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Optional[int] = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : List[Any] = pred_prev_sample __a : int = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : Union[str, Any] = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def _lowerCamelCase ( self ): __a : Dict = self.scheduler_classes[0] __a : int = self.get_scheduler_config(prediction_type='''v_prediction''' ) __a : int = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[str] = self.dummy_sample_deter __a : str = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Dict = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : Optional[int] = pred_prev_sample __a : Optional[int] = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : int = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Any = self.get_scheduler_config() __a : str = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_UpperCAmelCase ) __a : List[Any] = scheduler.timesteps for i, timestep in enumerate(_UpperCAmelCase ): if i == len(_UpperCAmelCase ) - 1: __a : Union[str, Any] = -1 else: __a : str = timesteps[i + 1] __a : Dict = scheduler.previous_timestep(_UpperCAmelCase ) __a : str = prev_t.item() self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Tuple = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Optional[Any] = [100, 87, 50, 51, 0] with self.assertRaises(_UpperCAmelCase , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] __a : Optional[int] = len(_UpperCAmelCase ) with self.assertRaises(_UpperCAmelCase , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=_UpperCAmelCase , timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : List[str] = scheduler_class(**_UpperCAmelCase ) __a : List[Any] = [scheduler.config.num_train_timesteps] with self.assertRaises( _UpperCAmelCase , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=_UpperCAmelCase )
52
0
from math import factorial A__ : Union[str, Any] = {str(digit): factorial(digit) for digit in range(1_0)} def _a ( __UpperCamelCase : int ): if not isinstance(a_ ,a_ ): raise TypeError('''Parameter number must be int''' ) if number < 0: raise ValueError('''Parameter number must be greater than or equal to 0''' ) # Converts number in string to iterate on its digits and adds its factorial. return sum(DIGIT_FACTORIAL[digit] for digit in str(a_ ) ) def _a ( __UpperCamelCase : int = 60 ,__UpperCamelCase : int = 100_0000 ): if not isinstance(a_ ,a_ ) or not isinstance(a_ ,a_ ): raise TypeError('''Parameters chain_length and number_limit must be int''' ) if chain_length <= 0 or number_limit <= 0: raise ValueError( '''Parameters chain_length and number_limit must be greater than 0''' ) # the counter for the chains with the exact desired length lowerCAmelCase__ : int = 0 # the cached sizes of the previous chains lowerCAmelCase__ : dict[int, int] = {} for start_chain_element in range(1 ,a_ ): # The temporary set will contain the elements of the chain lowerCAmelCase__ : Tuple = set() lowerCAmelCase__ : Union[str, Any] = 0 # Stop computing the chain when you find a cached size, a repeating item or the # length is greater then the desired one. lowerCAmelCase__ : str = start_chain_element while ( chain_element not in chain_sets_lengths and chain_element not in chain_set and chain_set_length <= chain_length ): chain_set.add(a_ ) chain_set_length += 1 lowerCAmelCase__ : Optional[int] = digit_factorial_sum(a_ ) if chain_element in chain_sets_lengths: chain_set_length += chain_sets_lengths[chain_element] lowerCAmelCase__ : Optional[int] = chain_set_length # If chain contains the exact amount of elements increase the counter if chain_set_length == chain_length: chains_counter += 1 return chains_counter if __name__ == "__main__": import doctest doctest.testmod() print(f"""{solution()}""")
233
"""simple docstring""" import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset A = random.Random() def __A ( a_ :Tuple , a_ :Dict=1.0 , a_ :str=None , a_ :List[Any]=None) -> Dict: if rng is None: __a : Any = global_rng __a : Tuple = [] for batch_idx in range(shape[0]): values.append([]) for _ in range(shape[1]): values[-1].append(rng.random() * scale) return values class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=400 , _UpperCAmelCase=2000 , _UpperCAmelCase=2048 , _UpperCAmelCase=128 , _UpperCAmelCase=1 , _UpperCAmelCase=512 , _UpperCAmelCase=30 , _UpperCAmelCase=44100 , ): __a : Any = parent __a : Tuple = batch_size __a : Tuple = min_seq_length __a : List[str] = max_seq_length __a : List[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __a : Tuple = spectrogram_length __a : int = feature_size __a : int = num_audio_channels __a : Tuple = hop_length __a : List[Any] = chunk_length __a : Any = sampling_rate def _lowerCamelCase ( self ): return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def _lowerCamelCase ( self , _UpperCAmelCase=False , _UpperCAmelCase=False ): def _flatten(_UpperCAmelCase ): return list(itertools.chain(*_UpperCAmelCase ) ) if equal_length: __a : Tuple = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __a : Tuple = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __a : Optional[Any] = [np.asarray(_UpperCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = TvltFeatureExtractor def _lowerCamelCase ( self ): __a : Optional[Any] = TvltFeatureExtractionTester(self ) def _lowerCamelCase ( self ): __a : int = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''spectrogram_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''feature_size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''num_audio_channels''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''hop_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''chunk_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''sampling_rate''' ) ) def _lowerCamelCase ( self ): __a : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : List[str] = feat_extract_first.save_pretrained(_UpperCAmelCase )[0] check_json_file_has_correct_format(_UpperCAmelCase ) __a : Union[str, Any] = self.feature_extraction_class.from_pretrained(_UpperCAmelCase ) __a : Tuple = feat_extract_first.to_dict() __a : List[Any] = feat_extract_second.to_dict() __a : int = dict_first.pop('''mel_filters''' ) __a : List[Any] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : int = os.path.join(_UpperCAmelCase , '''feat_extract.json''' ) feat_extract_first.to_json_file(_UpperCAmelCase ) __a : Optional[Any] = self.feature_extraction_class.from_json_file(_UpperCAmelCase ) __a : Optional[Any] = feat_extract_first.to_dict() __a : Any = feat_extract_second.to_dict() __a : Optional[Any] = dict_first.pop('''mel_filters''' ) __a : Dict = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): # Initialize feature_extractor __a : str = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 __a : Any = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __a : Union[str, Any] = [np.asarray(_UpperCAmelCase ) for speech_input in speech_inputs] # Test not batched input __a : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched __a : int = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking __a : List[Any] = feature_extractor( _UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 , mask_audio=_UpperCAmelCase ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. __a : str = [floats_list((1, x) )[0] for x in (800, 800, 800)] __a : Any = np.asarray(_UpperCAmelCase ) __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def _lowerCamelCase ( self , _UpperCAmelCase ): __a : int = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech __a : int = ds.sort('''id''' ).select(range(_UpperCAmelCase ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowerCamelCase ( self ): __a : List[str] = self._load_datasamples(1 ) __a : Tuple = TvltFeatureExtractor() __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) __a : Dict = torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _UpperCAmelCase , atol=1e-4 ) )
52
0
"""simple docstring""" import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotSmallConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html snake_case_ : List[str] = """platform""" import jax import jax.numpy as jnp from transformers.models.blenderbot_small.modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, shift_tokens_right, ) def lowercase_ ( _lowercase : Optional[int] , _lowercase : List[str] , _lowercase : Any=None , _lowercase : Tuple=None , _lowercase : int=None , _lowercase : Optional[Any]=None , _lowercase : int=None , _lowercase : str=None , ): '''simple docstring''' if attention_mask is None: UpperCAmelCase : Dict = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: UpperCAmelCase : Any = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: UpperCAmelCase : str = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : str = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : str = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class snake_case__ : def __init__( self : List[Any] , lowercase : Union[str, Any] , lowercase : Optional[int]=13 , lowercase : int=7 , lowercase : Union[str, Any]=True , lowercase : Tuple=False , lowercase : Optional[Any]=99 , lowercase : Dict=16 , lowercase : Tuple=2 , lowercase : List[str]=4 , lowercase : int=4 , lowercase : Tuple="gelu" , lowercase : int=0.1 , lowercase : str=0.1 , lowercase : Union[str, Any]=32 , lowercase : Optional[int]=2 , lowercase : Union[str, Any]=1 , lowercase : Union[str, Any]=0 , lowercase : List[Any]=0.0_2 , ): '''simple docstring''' UpperCAmelCase : Union[str, Any] = parent UpperCAmelCase : Optional[int] = batch_size UpperCAmelCase : Union[str, Any] = seq_length UpperCAmelCase : Tuple = is_training UpperCAmelCase : Union[str, Any] = use_labels UpperCAmelCase : Optional[int] = vocab_size UpperCAmelCase : Tuple = hidden_size UpperCAmelCase : Optional[int] = num_hidden_layers UpperCAmelCase : Tuple = num_attention_heads UpperCAmelCase : Any = intermediate_size UpperCAmelCase : str = hidden_act UpperCAmelCase : List[Any] = hidden_dropout_prob UpperCAmelCase : str = attention_probs_dropout_prob UpperCAmelCase : Tuple = max_position_embeddings UpperCAmelCase : str = eos_token_id UpperCAmelCase : Tuple = pad_token_id UpperCAmelCase : List[str] = bos_token_id UpperCAmelCase : Optional[int] = initializer_range def __lowerCAmelCase ( self : Any ): '''simple docstring''' UpperCAmelCase : Any = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) , 3 , self.vocab_size ) UpperCAmelCase : int = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) , dtype=np.intaa )) , -1 ) UpperCAmelCase : int = shift_tokens_right(_UpperCAmelCase , 1 , 2 ) UpperCAmelCase : Union[str, Any] = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , initializer_range=self.initializer_range , use_cache=_UpperCAmelCase , ) UpperCAmelCase : Tuple = prepare_blenderbot_inputs_dict(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return config, inputs_dict def __lowerCAmelCase ( self : Any ): '''simple docstring''' UpperCAmelCase : List[Any] = self.prepare_config_and_inputs() return config, inputs_dict def __lowerCAmelCase ( self : Dict , lowercase : Any , lowercase : List[str] , lowercase : Optional[int] ): '''simple docstring''' UpperCAmelCase : Optional[int] = 20 UpperCAmelCase : List[Any] = model_class_name(_UpperCAmelCase ) UpperCAmelCase : int = model.encode(inputs_dict["input_ids"] ) UpperCAmelCase : List[str] = ( inputs_dict['''decoder_input_ids'''], inputs_dict['''decoder_attention_mask'''], ) UpperCAmelCase : str = model.init_cache(decoder_input_ids.shape[0] , _UpperCAmelCase , _UpperCAmelCase ) UpperCAmelCase : Union[str, Any] = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype="i4" ) UpperCAmelCase : Any = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) UpperCAmelCase : int = model.decode( decoder_input_ids[:, :-1] , _UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase , past_key_values=_UpperCAmelCase , decoder_position_ids=_UpperCAmelCase , ) UpperCAmelCase : Dict = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) UpperCAmelCase : Union[str, Any] = model.decode( decoder_input_ids[:, -1:] , _UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase , past_key_values=outputs_cache.past_key_values , decoder_position_ids=_UpperCAmelCase , ) UpperCAmelCase : Optional[int] = model.decode(_UpperCAmelCase , _UpperCAmelCase ) UpperCAmelCase : int = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f"""Max diff is {diff}""" ) def __lowerCAmelCase ( self : str , lowercase : Union[str, Any] , lowercase : Optional[int] , lowercase : Union[str, Any] ): '''simple docstring''' UpperCAmelCase : Union[str, Any] = 20 UpperCAmelCase : str = model_class_name(_UpperCAmelCase ) UpperCAmelCase : Dict = model.encode(inputs_dict["input_ids"] ) UpperCAmelCase : Optional[int] = ( inputs_dict['''decoder_input_ids'''], inputs_dict['''decoder_attention_mask'''], ) UpperCAmelCase : Any = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) UpperCAmelCase : Optional[Any] = model.init_cache(decoder_input_ids.shape[0] , _UpperCAmelCase , _UpperCAmelCase ) UpperCAmelCase : Tuple = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) UpperCAmelCase : Tuple = model.decode( decoder_input_ids[:, :-1] , _UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase , past_key_values=_UpperCAmelCase , decoder_position_ids=_UpperCAmelCase , ) UpperCAmelCase : Union[str, Any] = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) UpperCAmelCase : Tuple = model.decode( decoder_input_ids[:, -1:] , _UpperCAmelCase , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=_UpperCAmelCase , decoder_position_ids=_UpperCAmelCase , ) UpperCAmelCase : Optional[Any] = model.decode(_UpperCAmelCase , _UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase ) UpperCAmelCase : int = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f"""Max diff is {diff}""" ) @require_flax class snake_case__ ( unittest.TestCase ): SCREAMING_SNAKE_CASE__ = 99 def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' UpperCAmelCase : Optional[int] = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] , dtype=np.intaa , ) UpperCAmelCase : List[Any] = input_ids.shape[0] UpperCAmelCase : int = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=24 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=32 , decoder_ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size def __lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase : List[str] = self._get_config_and_data() UpperCAmelCase : List[str] = FlaxBlenderbotSmallForConditionalGeneration(_UpperCAmelCase ) UpperCAmelCase : Tuple = lm_model(input_ids=_UpperCAmelCase ) UpperCAmelCase : Any = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs["logits"].shape , _UpperCAmelCase ) def __lowerCAmelCase ( self : str ): '''simple docstring''' UpperCAmelCase : Dict = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=14 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=8 , decoder_ffn_dim=8 , max_position_embeddings=48 , ) UpperCAmelCase : List[Any] = FlaxBlenderbotSmallForConditionalGeneration(_UpperCAmelCase ) UpperCAmelCase : Optional[int] = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] , dtype=np.intaa ) UpperCAmelCase : Dict = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] , dtype=np.intaa ) UpperCAmelCase : Dict = lm_model(input_ids=_UpperCAmelCase , decoder_input_ids=_UpperCAmelCase ) UpperCAmelCase : Dict = (*summary.shape, config.vocab_size) self.assertEqual(outputs["logits"].shape , _UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase : Tuple = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] , dtype=np.intaa ) UpperCAmelCase : Union[str, Any] = shift_tokens_right(_UpperCAmelCase , 1 , 2 ) UpperCAmelCase : Tuple = np.equal(_UpperCAmelCase , 1 ).astype(np.floataa ).sum() UpperCAmelCase : Union[str, Any] = np.equal(_UpperCAmelCase , 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape , input_ids.shape ) self.assertEqual(_UpperCAmelCase , n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] , 2 ).all() ) @require_flax class snake_case__ ( _UpperCamelCase , unittest.TestCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = ( ( FlaxBlenderbotSmallModel, FlaxBlenderbotSmallForConditionalGeneration, ) if is_flax_available() else () ) SCREAMING_SNAKE_CASE__ = (FlaxBlenderbotSmallForConditionalGeneration,) if is_flax_available() else () def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' UpperCAmelCase : List[Any] = FlaxBlenderbotSmallModelTester(self ) def __lowerCAmelCase ( self : Any ): '''simple docstring''' UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ): '''simple docstring''' UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): UpperCAmelCase : Optional[Any] = self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) UpperCAmelCase : int = model_class(_UpperCAmelCase ) @jax.jit def encode_jitted(lowercase : str , lowercase : Optional[int]=None , **lowercase : Optional[Any] ): return model.encode(input_ids=_UpperCAmelCase , attention_mask=_UpperCAmelCase ) with self.subTest("JIT Enabled" ): UpperCAmelCase : List[Any] = encode_jitted(**_UpperCAmelCase ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): UpperCAmelCase : int = encode_jitted(**_UpperCAmelCase ).to_tuple() self.assertEqual(len(_UpperCAmelCase ) , len(_UpperCAmelCase ) ) for jitted_output, output in zip(_UpperCAmelCase , _UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def __lowerCAmelCase ( self : Any ): '''simple docstring''' UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): UpperCAmelCase : Tuple = model_class(_UpperCAmelCase ) UpperCAmelCase : Tuple = model.encode(inputs_dict["input_ids"] , inputs_dict["attention_mask"] ) UpperCAmelCase : Dict = { '''decoder_input_ids''': inputs_dict['''decoder_input_ids'''], '''decoder_attention_mask''': inputs_dict['''decoder_attention_mask'''], '''encoder_outputs''': encoder_outputs, } @jax.jit def decode_jitted(lowercase : Optional[Any] , lowercase : Tuple , lowercase : List[Any] ): return model.decode( decoder_input_ids=_UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase , encoder_outputs=_UpperCAmelCase , ) with self.subTest("JIT Enabled" ): UpperCAmelCase : Any = decode_jitted(**_UpperCAmelCase ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): UpperCAmelCase : Optional[int] = decode_jitted(**_UpperCAmelCase ).to_tuple() self.assertEqual(len(_UpperCAmelCase ) , len(_UpperCAmelCase ) ) for jitted_output, output in zip(_UpperCAmelCase , _UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) @slow def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : Tuple = model_class_name.from_pretrained("facebook/blenderbot_small-90M" ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids UpperCAmelCase : Optional[Any] = np.ones((1, 1) ) * model.config.eos_token_id UpperCAmelCase : Tuple = model(_UpperCAmelCase ) self.assertIsNotNone(_UpperCAmelCase )
595
"""simple docstring""" from __future__ import annotations class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a , __a : List[Any] = text, pattern __a , __a : Tuple = len(_UpperCAmelCase ), len(_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def _lowerCamelCase ( self ): # searches pattern in text and returns index positions __a : Dict = [] for i in range(self.textLen - self.patLen + 1 ): __a : List[str] = self.mismatch_in_text(_UpperCAmelCase ) if mismatch_index == -1: positions.append(_UpperCAmelCase ) else: __a : Tuple = self.match_in_pattern(self.text[mismatch_index] ) __a : Optional[int] = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions A = '''ABAABA''' A = '''AB''' A = BoyerMooreSearch(text, pattern) A = bms.bad_character_heuristic() if len(positions) == 0: print('''No match found''') else: print('''Pattern found in following positions: ''') print(positions)
52
0
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCamelCase ( _UpperCamelCase , unittest.TestCase ): snake_case_ = LongformerTokenizer snake_case_ = True snake_case_ = LongformerTokenizerFast snake_case_ = True def _lowerCamelCase ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowerCAmelCase : Dict = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] lowerCAmelCase : List[str] = dict(zip(_UpperCAmelCase , range(len(_UpperCAmelCase ) ) ) ) lowerCAmelCase : Optional[int] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] lowerCAmelCase : List[str] = {'''unk_token''': '''<unk>'''} lowerCAmelCase : Optional[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) lowerCAmelCase : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(_UpperCAmelCase ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(_UpperCAmelCase ) ) def _lowerCamelCase ( self , **a_ ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_UpperCAmelCase ) def _lowerCamelCase ( self , **a_ ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_UpperCAmelCase ) def _lowerCamelCase ( self , a_ ): lowerCAmelCase : List[str] = '''lower newer''' lowerCAmelCase : List[Any] = '''lower newer''' return input_text, output_text def _lowerCamelCase ( self ): lowerCAmelCase : Dict = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) lowerCAmelCase : Tuple = '''lower newer''' lowerCAmelCase : Dict = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] lowerCAmelCase : Union[str, Any] = tokenizer.tokenize(_UpperCAmelCase ) # , add_prefix_space=True) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : Optional[int] = tokens + [tokenizer.unk_token] lowerCAmelCase : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , _UpperCAmelCase ) def _lowerCamelCase ( self ): lowerCAmelCase : Tuple = self.get_tokenizer() self.assertListEqual(tokenizer.encode("Hello world!" , add_special_tokens=_UpperCAmelCase ) , [0, 31_414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode("Hello world! cécé herlolip 418" , add_special_tokens=_UpperCAmelCase ) , [0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2] , ) @slow def _lowerCamelCase ( self ): lowerCAmelCase : Dict = self.tokenizer_class.from_pretrained("allenai/longformer-base-4096" ) lowerCAmelCase : Any = tokenizer.encode("sequence builders" , add_special_tokens=_UpperCAmelCase ) lowerCAmelCase : List[Any] = tokenizer.encode("multi-sequence build" , add_special_tokens=_UpperCAmelCase ) lowerCAmelCase : str = tokenizer.encode( "sequence builders" , add_special_tokens=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase ) lowerCAmelCase : Tuple = tokenizer.encode( "sequence builders" , "multi-sequence build" , add_special_tokens=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase ) lowerCAmelCase : Optional[Any] = tokenizer.build_inputs_with_special_tokens(_UpperCAmelCase ) lowerCAmelCase : Dict = tokenizer.build_inputs_with_special_tokens(_UpperCAmelCase , _UpperCAmelCase ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _lowerCamelCase ( self ): lowerCAmelCase : Tuple = self.get_tokenizer() lowerCAmelCase : Union[str, Any] = '''Encode this sequence.''' lowerCAmelCase : List[str] = tokenizer.byte_encoder[''' '''.encode("utf-8" )[0]] # Testing encoder arguments lowerCAmelCase : Any = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase ) lowerCAmelCase : List[str] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : Union[str, Any] = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase ) lowerCAmelCase : int = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) tokenizer.add_special_tokens({"bos_token": "<s>"} ) lowerCAmelCase : List[str] = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) lowerCAmelCase : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_UpperCAmelCase , _UpperCAmelCase ) # Testing spaces after special tokens lowerCAmelCase : Dict = '''<mask>''' tokenizer.add_special_tokens( {"mask_token": AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase )} ) # mask token has a left space lowerCAmelCase : Any = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) lowerCAmelCase : Optional[Any] = '''Encode <mask> sequence''' lowerCAmelCase : Union[str, Any] = '''Encode <mask>sequence''' lowerCAmelCase : Tuple = tokenizer.encode(_UpperCAmelCase ) lowerCAmelCase : int = encoded.index(_UpperCAmelCase ) lowerCAmelCase : int = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : Union[str, Any] = tokenizer.encode(_UpperCAmelCase ) lowerCAmelCase : Any = encoded.index(_UpperCAmelCase ) lowerCAmelCase : Any = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): pass def _lowerCamelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): lowerCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained(_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase : List[Any] = self.tokenizer_class.from_pretrained(_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase : Optional[Any] = '''A, <mask> AllenNLP sentence.''' lowerCAmelCase : Tuple = tokenizer_r.encode_plus(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase , return_token_type_ids=_UpperCAmelCase ) lowerCAmelCase : Union[str, Any] = tokenizer_p.encode_plus(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase , return_token_type_ids=_UpperCAmelCase ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r["token_type_ids"] ) , sum(tokens_p["token_type_ids"] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) , sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) , ) lowerCAmelCase : int = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) lowerCAmelCase : Any = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) # Rust correctly handles the space before the mask while python doesnt 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( _UpperCAmelCase , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( _UpperCAmelCase , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) def _lowerCamelCase ( self ): for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): lowerCAmelCase : List[str] = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase ) lowerCAmelCase : Optional[int] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) lowerCAmelCase : List[str] = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state["add_prefix_space"] , _UpperCAmelCase ) self.assertEqual(post_processor_state["add_prefix_space"] , _UpperCAmelCase ) self.assertEqual(post_processor_state["trim_offsets"] , _UpperCAmelCase ) def _lowerCamelCase ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): lowerCAmelCase : Any = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` lowerCAmelCase : Union[str, Any] = F'''{text_of_1_token} {text_of_1_token}''' lowerCAmelCase : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( _UpperCAmelCase , use_fast=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase ) lowerCAmelCase : List[Any] = tokenizer_r(_UpperCAmelCase , return_offsets_mapping=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_UpperCAmelCase ) + 1, len(_UpperCAmelCase ) + 1 + len(_UpperCAmelCase )) , ) lowerCAmelCase : Optional[Any] = self.rust_tokenizer_class.from_pretrained( _UpperCAmelCase , use_fast=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase ) lowerCAmelCase : Any = tokenizer_r(_UpperCAmelCase , return_offsets_mapping=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_UpperCAmelCase ) + 1, len(_UpperCAmelCase ) + 1 + len(_UpperCAmelCase )) , ) lowerCAmelCase : str = self.rust_tokenizer_class.from_pretrained( _UpperCAmelCase , use_fast=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase ) lowerCAmelCase : Any = tokenizer_r(_UpperCAmelCase , return_offsets_mapping=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_UpperCAmelCase ), len(_UpperCAmelCase ) + 1 + len(_UpperCAmelCase )) , ) lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained( _UpperCAmelCase , use_fast=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase ) lowerCAmelCase : Union[str, Any] = tokenizer_r(_UpperCAmelCase , return_offsets_mapping=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_UpperCAmelCase ), len(_UpperCAmelCase ) + 1 + len(_UpperCAmelCase )) , ) lowerCAmelCase : Optional[Any] = F''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) lowerCAmelCase : int = self.rust_tokenizer_class.from_pretrained( _UpperCAmelCase , use_fast=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase ) lowerCAmelCase : Tuple = tokenizer_r(_UpperCAmelCase , return_offsets_mapping=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_UpperCAmelCase ) + 1, 1 + len(_UpperCAmelCase ) + 1 + len(_UpperCAmelCase )) , ) lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained( _UpperCAmelCase , use_fast=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase ) lowerCAmelCase : List[Any] = tokenizer_r(_UpperCAmelCase , return_offsets_mapping=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_UpperCAmelCase ), 1 + len(_UpperCAmelCase ) + 1 + len(_UpperCAmelCase )) , ) lowerCAmelCase : List[str] = self.rust_tokenizer_class.from_pretrained( _UpperCAmelCase , use_fast=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , trim_offsets=_UpperCAmelCase ) lowerCAmelCase : Union[str, Any] = tokenizer_r(_UpperCAmelCase , return_offsets_mapping=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_UpperCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_UpperCAmelCase ), 1 + len(_UpperCAmelCase ) + 1 + len(_UpperCAmelCase )) , )
525
"""simple docstring""" import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": A = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') A = F'https://www.google.com/search?q={query}&num=100' A = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: A = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: A = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
0
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class lowercase_ ( unittest.TestCase ): def __init__( self , UpperCamelCase__ , UpperCamelCase__=7 , UpperCamelCase__=3 , UpperCamelCase__=1_8 , UpperCamelCase__=3_0 , UpperCamelCase__=4_0_0 , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=[0.5, 0.5, 0.5] , UpperCamelCase__=[0.5, 0.5, 0.5] , ) -> Any: """simple docstring""" UpperCAmelCase_ = parent UpperCAmelCase_ = batch_size UpperCAmelCase_ = num_channels UpperCAmelCase_ = image_size UpperCAmelCase_ = min_resolution UpperCAmelCase_ = max_resolution UpperCAmelCase_ = do_resize UpperCAmelCase_ = size if size is not None else {'''height''': 1_8, '''width''': 2_0} UpperCAmelCase_ = do_thumbnail UpperCAmelCase_ = do_align_axis UpperCAmelCase_ = do_pad UpperCAmelCase_ = do_normalize UpperCAmelCase_ = image_mean UpperCAmelCase_ = image_std def lowerCamelCase_ ( self ) -> Any: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class lowercase_ ( _UpperCamelCase , unittest.TestCase ): a_ = DonutImageProcessor if is_vision_available() else None def lowerCamelCase_ ( self ) -> int: """simple docstring""" UpperCAmelCase_ = DonutImageProcessingTester(self ) @property def lowerCamelCase_ ( self ) -> Dict: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def lowerCamelCase_ ( self ) -> Tuple: """simple docstring""" UpperCAmelCase_ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCAmelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCAmelCase , "size" ) ) self.assertTrue(hasattr(_UpperCAmelCase , "do_thumbnail" ) ) self.assertTrue(hasattr(_UpperCAmelCase , "do_align_long_axis" ) ) self.assertTrue(hasattr(_UpperCAmelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCAmelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCAmelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCAmelCase , "image_std" ) ) def lowerCamelCase_ ( self ) -> str: """simple docstring""" UpperCAmelCase_ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 1_8, "width": 2_0} ) UpperCAmelCase_ = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 ) self.assertEqual(image_processor.size , {"height": 4_2, "width": 4_2} ) # Previous config had dimensions in (width, height) order UpperCAmelCase_ = self.image_processing_class.from_dict(self.image_processor_dict , size=(4_2, 8_4) ) self.assertEqual(image_processor.size , {"height": 8_4, "width": 4_2} ) def lowerCamelCase_ ( self ) -> Optional[int]: """simple docstring""" pass @is_flaky() def lowerCamelCase_ ( self ) -> List[str]: """simple docstring""" UpperCAmelCase_ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCAmelCase_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , Image.Image ) # Test not batched input UpperCAmelCase_ = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched UpperCAmelCase_ = image_processing(_UpperCAmelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) @is_flaky() def lowerCamelCase_ ( self ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCAmelCase_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , numpify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , np.ndarray ) # Test not batched input UpperCAmelCase_ = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched UpperCAmelCase_ = image_processing(_UpperCAmelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) @is_flaky() def lowerCamelCase_ ( self ) -> Tuple: """simple docstring""" UpperCAmelCase_ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCAmelCase_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , torchify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , torch.Tensor ) # Test not batched input UpperCAmelCase_ = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched UpperCAmelCase_ = image_processing(_UpperCAmelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
660
"""simple docstring""" import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = 0 __lowerCAmelCase = False __lowerCAmelCase = 3.0 class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_UpperCAmelCase ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.2_5 ).to_kwargs() , {'''a''': 2, '''c''': 2.2_5} ) @require_cuda def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a : List[Any] = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() __a : int = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a : Optional[Any] = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_0_2_4.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _UpperCAmelCase ) @require_multi_gpu def _lowerCamelCase ( self ): __a : Dict = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] execute_subprocess_async(_UpperCAmelCase , env=os.environ.copy() ) if __name__ == "__main__": A = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) A = Accelerator(kwargs_handlers=[ddp_scaler]) A = torch.nn.Linear(100, 200) A = accelerator.prepare(model) # Check the values changed in kwargs A = '''''' A = model.bucket_bytes_cap // (1_024 * 1_024) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
52
0
import itertools import string from collections.abc import Generator, Iterable def __magic_name__ ( lowerCAmelCase_ , lowerCAmelCase_): '''simple docstring''' lowerCamelCase_ : List[str] = iter(a_) while True: lowerCamelCase_ : List[Any] = tuple(itertools.islice(a_ , a_)) if not chunk: return yield chunk def __magic_name__ ( lowerCAmelCase_): '''simple docstring''' lowerCamelCase_ : int = ''''''.join([c.upper() for c in dirty if c in string.ascii_letters]) lowerCamelCase_ : Tuple = '''''' if len(a_) < 2: return dirty for i in range(len(a_) - 1): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(a_) & 1: clean += "X" return clean def __magic_name__ ( lowerCAmelCase_): '''simple docstring''' lowerCamelCase_ : Optional[Any] = '''ABCDEFGHIKLMNOPQRSTUVWXYZ''' # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler lowerCamelCase_ : Tuple = [] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(a_) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(a_) return table def __magic_name__ ( lowerCAmelCase_ , lowerCAmelCase_): '''simple docstring''' lowerCamelCase_ : Optional[Any] = generate_table(a_) lowerCamelCase_ : Optional[int] = prepare_input(a_) lowerCamelCase_ : List[str] = '''''' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(a_ , 2): lowerCamelCase_ : Optional[Any] = divmod(table.index(a_) , 5) lowerCamelCase_ : Tuple = divmod(table.index(a_) , 5) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def __magic_name__ ( lowerCAmelCase_ , lowerCAmelCase_): '''simple docstring''' lowerCamelCase_ : Any = generate_table(a_) lowerCamelCase_ : Any = '''''' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(a_ , 2): lowerCamelCase_ : Any = divmod(table.index(a_) , 5) lowerCamelCase_ : Union[str, Any] = divmod(table.index(a_) , 5) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
250
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A_: str = logging.get_logger(__name__) A_: Any = { 'distilbert-base-uncased': 'https://huggingface.co/distilbert-base-uncased/resolve/main/config.json', 'distilbert-base-uncased-distilled-squad': ( 'https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json' ), 'distilbert-base-cased': 'https://huggingface.co/distilbert-base-cased/resolve/main/config.json', 'distilbert-base-cased-distilled-squad': ( 'https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json' ), 'distilbert-base-german-cased': 'https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json', 'distilbert-base-multilingual-cased': ( 'https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json' ), 'distilbert-base-uncased-finetuned-sst-2-english': ( 'https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json' ), } class _lowercase ( _UpperCamelCase ): """simple docstring""" lowerCAmelCase__ = 'distilbert' lowerCAmelCase__ = { 'hidden_size': 'dim', 'num_attention_heads': 'n_heads', 'num_hidden_layers': 'n_layers', } def __init__( self , UpperCAmelCase=30522 , UpperCAmelCase=512 , UpperCAmelCase=False , UpperCAmelCase=6 , UpperCAmelCase=12 , UpperCAmelCase=768 , UpperCAmelCase=4 * 768 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase="gelu" , UpperCAmelCase=0.02 , UpperCAmelCase=0.1 , UpperCAmelCase=0.2 , UpperCAmelCase=0 , **UpperCAmelCase , ): '''simple docstring''' _lowercase = vocab_size _lowercase = max_position_embeddings _lowercase = sinusoidal_pos_embds _lowercase = n_layers _lowercase = n_heads _lowercase = dim _lowercase = hidden_dim _lowercase = dropout _lowercase = attention_dropout _lowercase = activation _lowercase = initializer_range _lowercase = qa_dropout _lowercase = seq_classif_dropout super().__init__(**_UpperCAmelCase , pad_token_id=_UpperCAmelCase ) class _lowercase ( _UpperCamelCase ): """simple docstring""" @property def _UpperCAmelCase ( self ): '''simple docstring''' if self.task == "multiple-choice": _lowercase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: _lowercase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
398
"""simple docstring""" import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger A = get_logger(__name__) A = Path(__file__).parent / '''model_card_template.md''' A = uuida().hex A = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES A = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES A = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def __A ( a_ :Union[Dict, str, None] = None) -> str: __a : Union[str, Any] = F"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F"""; torch/{_torch_version}""" if is_flax_available(): ua += F"""; jax/{_jax_version}""" ua += F"""; flax/{_flax_version}""" if is_onnx_available(): ua += F"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''').upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(a_ , a_): ua += "; " + "; ".join(F"""{k}/{v}""" for k, v in user_agent.items()) elif isinstance(a_ , a_): ua += "; " + user_agent return ua def __A ( a_ :str , a_ :Optional[str] = None , a_ :Optional[str] = None) -> Optional[int]: if token is None: __a : Any = HfFolder.get_token() if organization is None: __a : List[Any] = whoami(a_)['''name'''] return F"""{username}/{model_id}""" else: return F"""{organization}/{model_id}""" def __A ( a_ :Union[str, Any] , a_ :List[str]) -> Optional[Any]: if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''') if hasattr(a_ , '''local_rank''') and args.local_rank not in [-1, 0]: return __a : int = args.hub_token if hasattr(a_ , '''hub_token''') else None __a : Any = get_full_repo_name(a_ , token=a_) __a : Tuple = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=a_ , model_name=a_ , repo_name=a_ , dataset_name=args.dataset_name if hasattr(a_ , '''dataset_name''') else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(a_ , '''gradient_accumulation_steps''') else None ) , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta1''') else None , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta2''') else None , adam_weight_decay=args.adam_weight_decay if hasattr(a_ , '''adam_weight_decay''') else None , adam_epsilon=args.adam_epsilon if hasattr(a_ , '''adam_epsilon''') else None , lr_scheduler=args.lr_scheduler if hasattr(a_ , '''lr_scheduler''') else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(a_ , '''lr_warmup_steps''') else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(a_ , '''ema_inv_gamma''') else None , ema_power=args.ema_power if hasattr(a_ , '''ema_power''') else None , ema_max_decay=args.ema_max_decay if hasattr(a_ , '''ema_max_decay''') else None , mixed_precision=args.mixed_precision , ) __a : List[Any] = os.path.join(args.output_dir , '''README.md''') model_card.save(a_) def __A ( a_ :Optional[str] , a_ :Optional[str] = None) -> Union[str, Any]: if resolved_file is None or commit_hash is not None: return commit_hash __a : Any = str(Path(a_).as_posix()) __a : Optional[int] = re.search(R'''snapshots/([^/]+)/''' , a_) if search is None: return None __a : Dict = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(a_) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. A = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) A = os.path.join(hf_cache_home, '''diffusers''') def __A ( a_ :Optional[str] = None , a_ :Optional[str] = None) -> None: if new_cache_dir is None: __a : Dict = DIFFUSERS_CACHE if old_cache_dir is None: __a : List[Any] = old_diffusers_cache __a : Union[str, Any] = Path(a_).expanduser() __a : Dict = Path(a_).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*'''): if old_blob_path.is_file() and not old_blob_path.is_symlink(): __a : List[Any] = new_cache_dir / old_blob_path.relative_to(a_) new_blob_path.parent.mkdir(parents=a_ , exist_ok=a_) os.replace(a_ , a_) try: os.symlink(a_ , a_) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''') # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). A = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): A = 0 else: with open(cache_version_file) as f: try: A = int(f.read()) except ValueError: A = 0 if cache_version < 1: A = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: A = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F'There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F'There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ' '''the directory exists and can be written to.''' ) def __A ( a_ :str , a_ :Optional[str] = None) -> str: if variant is not None: __a : Dict = weights_name.split('''.''') __a : List[Any] = splits[:-1] + [variant] + splits[-1:] __a : Tuple = '''.'''.join(a_) return weights_name def __A ( a_ :List[Any] , *, a_ :Union[str, Any] , a_ :Dict , a_ :Union[str, Any] , a_ :Optional[int] , a_ :str , a_ :Any , a_ :str , a_ :Optional[int] , a_ :str , a_ :Tuple , a_ :List[str]=None , ) -> Dict: __a : int = str(a_) if os.path.isfile(a_): return pretrained_model_name_or_path elif os.path.isdir(a_): if os.path.isfile(os.path.join(a_ , a_)): # Load from a PyTorch checkpoint __a : Union[str, Any] = os.path.join(a_ , a_) return model_file elif subfolder is not None and os.path.isfile( os.path.join(a_ , a_ , a_)): __a : Optional[Any] = os.path.join(a_ , a_ , a_) return model_file else: raise EnvironmentError( F"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""") else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(a_).base_version) >= version.parse('''0.20.0''') ): try: __a : Any = hf_hub_download( a_ , filename=_add_variant(a_ , a_) , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) warnings.warn( F"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.""" , a_ , ) return model_file except: # noqa: E722 warnings.warn( F"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(a_ , a_)} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(a_ , a_)}' so that the correct variant file can be added.""" , a_ , ) try: # 2. Load model file as usual __a : Optional[Any] = hf_hub_download( a_ , filename=a_ , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''') except RevisionNotFoundError: raise EnvironmentError( F"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ '''this model name. Check the model page at ''' F"""'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions.""") except EntryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""") except HTTPError as err: raise EnvironmentError( F"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""") except ValueError: raise EnvironmentError( F"""We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it""" F""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" F""" directory containing a file named {weights_name} or""" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''') except EnvironmentError: raise EnvironmentError( F"""Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from """ '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F"""Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory """ F"""containing a file named {weights_name}""")
52
0
'''simple docstring''' 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 __UpperCAmelCase =logging.get_logger(__name__) @add_end_docstrings( _UpperCamelCase , R"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class a__ ( _UpperCamelCase ): def SCREAMING_SNAKE_CASE__ ( self : str , a : Optional[int] ): """simple docstring""" if self.framework == "tf": __lowerCamelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": __lowerCamelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=_UpperCAmelCase ) else: raise ValueError('''Unsupported framework''' ) return masked_index def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Dict ): """simple docstring""" __lowerCamelCase = self.get_masked_index(_UpperCAmelCase ) __lowerCamelCase = 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 SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Any ): """simple docstring""" 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 SCREAMING_SNAKE_CASE__ ( self : Dict , a : List[Any] , a : Dict=None , **a : List[str] ): """simple docstring""" if return_tensors is None: __lowerCamelCase = self.framework __lowerCamelCase = self.tokenizer(_UpperCAmelCase , return_tensors=_UpperCAmelCase ) self.ensure_exactly_one_mask_token(_UpperCAmelCase ) return model_inputs def SCREAMING_SNAKE_CASE__ ( self : Any , a : List[str] ): """simple docstring""" __lowerCamelCase = self.model(**_UpperCAmelCase ) __lowerCamelCase = model_inputs['''input_ids'''] return model_outputs def SCREAMING_SNAKE_CASE__ ( self : int , a : List[str] , a : Optional[Any]=5 , a : str=None ): """simple docstring""" if target_ids is not None and target_ids.shape[0] < top_k: __lowerCamelCase = target_ids.shape[0] __lowerCamelCase = model_outputs['''input_ids'''][0] __lowerCamelCase = model_outputs['''logits'''] if self.framework == "tf": __lowerCamelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] __lowerCamelCase = outputs.numpy() __lowerCamelCase = outputs[0, masked_index, :] __lowerCamelCase = stable_softmax(_UpperCAmelCase , axis=-1 ) if target_ids is not None: __lowerCamelCase = tf.gather_nd(tf.squeeze(_UpperCAmelCase , 0 ) , target_ids.reshape(-1 , 1 ) ) __lowerCamelCase = tf.expand_dims(_UpperCAmelCase , 0 ) __lowerCamelCase = tf.math.top_k(_UpperCAmelCase , k=_UpperCAmelCase ) __lowerCamelCase = topk.values.numpy(), topk.indices.numpy() else: __lowerCamelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=_UpperCAmelCase ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample __lowerCamelCase = outputs[0, masked_index, :] __lowerCamelCase = logits.softmax(dim=-1 ) if target_ids is not None: __lowerCamelCase = probs[..., target_ids] __lowerCamelCase = probs.topk(_UpperCAmelCase ) __lowerCamelCase = [] __lowerCamelCase = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): __lowerCamelCase = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place __lowerCamelCase = input_ids.numpy().copy() if target_ids is not None: __lowerCamelCase = target_ids[p].tolist() __lowerCamelCase = p # Filter padding out: __lowerCamelCase = 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 __lowerCamelCase = self.tokenizer.decode(_UpperCAmelCase , skip_special_tokens=_UpperCAmelCase ) __lowerCamelCase = {'''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 SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Dict , a : Any=None ): """simple docstring""" if isinstance(_UpperCAmelCase , _UpperCAmelCase ): __lowerCamelCase = [targets] try: __lowerCamelCase = self.tokenizer.get_vocab() except Exception: __lowerCamelCase = {} __lowerCamelCase = [] for target in targets: __lowerCamelCase = vocab.get(_UpperCAmelCase , _UpperCAmelCase ) if id_ is None: __lowerCamelCase = 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 __lowerCamelCase = 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_ ) __lowerCamelCase = list(set(_UpperCAmelCase ) ) if len(_UpperCAmelCase ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) __lowerCamelCase = np.array(_UpperCAmelCase ) return target_ids def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Dict=None , a : Tuple=None ): """simple docstring""" __lowerCamelCase = {} if targets is not None: __lowerCamelCase = self.get_target_ids(_UpperCAmelCase , _UpperCAmelCase ) __lowerCamelCase = target_ids if top_k is not None: __lowerCamelCase = 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 : Optional[int] , a : Optional[Any] , *a : Optional[Any] , **a : int ): """simple docstring""" __lowerCamelCase = super().__call__(_UpperCAmelCase , **_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) and len(_UpperCAmelCase ) == 1: return outputs[0] return outputs
546
"""simple docstring""" import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''kakaobrain/align-base''': '''https://huggingface.co/kakaobrain/align-base/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_text_model''' def __init__( self , _UpperCAmelCase=30522 , _UpperCAmelCase=768 , _UpperCAmelCase=12 , _UpperCAmelCase=12 , _UpperCAmelCase=3072 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0 , _UpperCAmelCase="absolute" , _UpperCAmelCase=True , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = vocab_size __a : Optional[int] = hidden_size __a : Dict = num_hidden_layers __a : List[Any] = num_attention_heads __a : Optional[int] = hidden_act __a : List[Any] = intermediate_size __a : List[Any] = hidden_dropout_prob __a : List[str] = attention_probs_dropout_prob __a : Optional[int] = max_position_embeddings __a : List[str] = type_vocab_size __a : Tuple = initializer_range __a : Dict = layer_norm_eps __a : Any = position_embedding_type __a : Dict = use_cache __a : Dict = pad_token_id @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : List[str] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the text config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Dict = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_vision_model''' def __init__( self , _UpperCAmelCase = 3 , _UpperCAmelCase = 600 , _UpperCAmelCase = 2.0 , _UpperCAmelCase = 3.1 , _UpperCAmelCase = 8 , _UpperCAmelCase = [3, 3, 5, 3, 5, 5, 3] , _UpperCAmelCase = [32, 16, 24, 40, 80, 112, 192] , _UpperCAmelCase = [16, 24, 40, 80, 112, 192, 320] , _UpperCAmelCase = [] , _UpperCAmelCase = [1, 2, 2, 2, 1, 2, 1] , _UpperCAmelCase = [1, 2, 2, 3, 3, 4, 1] , _UpperCAmelCase = [1, 6, 6, 6, 6, 6, 6] , _UpperCAmelCase = 0.2_5 , _UpperCAmelCase = "swish" , _UpperCAmelCase = 2560 , _UpperCAmelCase = "mean" , _UpperCAmelCase = 0.0_2 , _UpperCAmelCase = 0.0_0_1 , _UpperCAmelCase = 0.9_9 , _UpperCAmelCase = 0.2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : Tuple = num_channels __a : str = image_size __a : List[Any] = width_coefficient __a : Optional[int] = depth_coefficient __a : Union[str, Any] = depth_divisor __a : int = kernel_sizes __a : Dict = in_channels __a : List[str] = out_channels __a : Any = depthwise_padding __a : str = strides __a : Optional[Any] = num_block_repeats __a : Optional[Any] = expand_ratios __a : Any = squeeze_expansion_ratio __a : int = hidden_act __a : Union[str, Any] = hidden_dim __a : Union[str, Any] = pooling_type __a : Tuple = initializer_range __a : List[str] = batch_norm_eps __a : List[Any] = batch_norm_momentum __a : Union[str, Any] = drop_connect_rate __a : List[Any] = sum(_UpperCAmelCase ) * 4 @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : Optional[Any] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the vision config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Optional[Any] = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align''' __lowerCAmelCase = True def __init__( self , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=640 , _UpperCAmelCase=1.0 , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) if text_config is None: __a : Dict = {} logger.info('''text_config is None. Initializing the AlignTextConfig with default values.''' ) if vision_config is None: __a : Any = {} logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.''' ) __a : Any = AlignTextConfig(**_UpperCAmelCase ) __a : Any = AlignVisionConfig(**_UpperCAmelCase ) __a : Optional[int] = projection_dim __a : Union[str, Any] = temperature_init_value __a : int = initializer_range @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = copy.deepcopy(self.__dict__ ) __a : Tuple = self.text_config.to_dict() __a : Union[str, Any] = self.vision_config.to_dict() __a : int = self.__class__.model_type return output
52
0
from __future__ import annotations def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = 0 snake_case_ = len(a_ ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: snake_case_ = i + 1 else: snake_case_ = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(F'''{two_pointer([2, 7, 11, 15], 9) = }''')
362
"""simple docstring""" from __future__ import annotations from random import choice def __A ( a_ :Tuple) -> List[str]: return choice(a_) def __A ( a_ :list[int] , a_ :int) -> int: __a : Optional[int] = random_pivot(a_) # partition based on pivot # linear time __a : Union[str, Any] = [e for e in lst if e < pivot] __a : Any = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(a_) == k - 1: return pivot # pivot is in elements bigger than k elif len(a_) < k - 1: return kth_number(a_ , k - len(a_) - 1) # pivot is in elements smaller than k else: return kth_number(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = int(number**0.5 ) return number == sq * sq def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ,lowercase ,lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den _UpperCAmelCase = x_den * y_den * z_den _UpperCAmelCase = gcd(a_ ,a_ ) top //= hcf bottom //= hcf return top, bottom def __UpperCAmelCase ( lowercase = 35 ): """simple docstring""" _UpperCAmelCase = set() _UpperCAmelCase = 42 _UpperCAmelCase = Fraction(0 ) _UpperCAmelCase = 42 for x_num in range(1 ,order + 1 ): for x_den in range(x_num + 1 ,order + 1 ): for y_num in range(1 ,order + 1 ): for y_den in range(y_num + 1 ,order + 1 ): # n=1 _UpperCAmelCase = x_num * y_den + x_den * y_num _UpperCAmelCase = x_den * y_den _UpperCAmelCase = gcd(a_ ,a_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _UpperCAmelCase = add_three( a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) unique_s.add(a_ ) # n=2 _UpperCAmelCase = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) _UpperCAmelCase = x_den * x_den * y_den * y_den if is_sq(a_ ) and is_sq(a_ ): _UpperCAmelCase = int(sqrt(a_ ) ) _UpperCAmelCase = int(sqrt(a_ ) ) _UpperCAmelCase = gcd(a_ ,a_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _UpperCAmelCase = add_three( a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) unique_s.add(a_ ) # n=-1 _UpperCAmelCase = x_num * y_num _UpperCAmelCase = x_den * y_num + x_num * y_den _UpperCAmelCase = gcd(a_ ,a_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _UpperCAmelCase = add_three( a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) unique_s.add(a_ ) # n=2 _UpperCAmelCase = x_num * x_num * y_num * y_num _UpperCAmelCase = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(a_ ) and is_sq(a_ ): _UpperCAmelCase = int(sqrt(a_ ) ) _UpperCAmelCase = int(sqrt(a_ ) ) _UpperCAmelCase = gcd(a_ ,a_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _UpperCAmelCase = add_three( a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) unique_s.add(a_ ) for num, den in unique_s: total += Fraction(a_ ,a_ ) return total.denominator + total.numerator if __name__ == "__main__": print(F'''{solution() = }''')
277
"""simple docstring""" import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A = logging.getLogger(__name__) def __A ( a_ :Union[str, Any] , a_ :Dict) -> Union[str, Any]: __a : Optional[int] = np.argmax(a_ , axis=1) return np.sum(outputs == labels) def __A ( a_ :Any) -> str: with open(a_ , encoding='''utf_8''') as f: __a : List[Any] = csv.reader(a_) __a : List[str] = [] next(a_) # skip the first line for line in tqdm(a_): output.append((''' '''.join(line[1:5]), line[5], line[6], int(line[-1]) - 1)) return output def __A ( a_ :Dict , a_ :str , a_ :str , a_ :List[Any] , a_ :Tuple , a_ :List[Any]) -> Any: __a : List[str] = [] for dataset in encoded_datasets: __a : List[str] = len(a_) __a : List[str] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa) __a : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa) __a : Tuple = np.full((n_batch, 2, input_len) , fill_value=-1_00 , dtype=np.intaa) __a : Optional[Any] = np.zeros((n_batch,) , dtype=np.intaa) for ( i, (story, conta, conta, mc_label), ) in enumerate(a_): __a : str = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __a : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __a : Tuple = with_conta __a : int = with_conta __a : List[str] = len(a_) - 1 __a : int = len(a_) - 1 __a : Optional[int] = with_conta __a : Tuple = with_conta __a : List[Any] = mc_label __a : Any = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(a_) for t in all_inputs)) return tensor_datasets def __A ( ) -> Union[str, Any]: __a : List[str] = argparse.ArgumentParser() parser.add_argument('''--model_name''' , type=a_ , default='''openai-gpt''' , help='''pretrained model name''') parser.add_argument('''--do_train''' , action='''store_true''' , help='''Whether to run training.''') parser.add_argument('''--do_eval''' , action='''store_true''' , help='''Whether to run eval on the dev set.''') parser.add_argument( '''--output_dir''' , default=a_ , type=a_ , required=a_ , help='''The output directory where the model predictions and checkpoints will be written.''' , ) parser.add_argument('''--train_dataset''' , type=a_ , default='''''') parser.add_argument('''--eval_dataset''' , type=a_ , default='''''') parser.add_argument('''--seed''' , type=a_ , default=42) parser.add_argument('''--num_train_epochs''' , type=a_ , default=3) parser.add_argument('''--train_batch_size''' , type=a_ , default=8) parser.add_argument('''--eval_batch_size''' , type=a_ , default=16) parser.add_argument('''--adam_epsilon''' , default=1e-8 , type=a_ , help='''Epsilon for Adam optimizer.''') parser.add_argument('''--max_grad_norm''' , type=a_ , default=1) parser.add_argument( '''--max_steps''' , default=-1 , type=a_ , help=( '''If > 0: set total number of training steps to perform. Override num_train_epochs.''' ) , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=a_ , default=1 , help='''Number of updates steps to accumulate before performing a backward/update pass.''' , ) parser.add_argument('''--learning_rate''' , type=a_ , default=6.25e-5) parser.add_argument('''--warmup_steps''' , default=0 , type=a_ , help='''Linear warmup over warmup_steps.''') parser.add_argument('''--lr_schedule''' , type=a_ , default='''warmup_linear''') parser.add_argument('''--weight_decay''' , type=a_ , default=0.0_1) parser.add_argument('''--lm_coef''' , type=a_ , default=0.9) parser.add_argument('''--n_valid''' , type=a_ , default=3_74) parser.add_argument('''--server_ip''' , type=a_ , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=a_ , default='''''' , help='''Can be used for distant debugging.''') __a : str = parser.parse_args() print(a_) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=a_) ptvsd.wait_for_attach() random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) __a : Tuple = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''') __a : str = torch.cuda.device_count() logger.info('''device: {}, n_gpu {}'''.format(a_ , a_)) if not args.do_train and not args.do_eval: raise ValueError('''At least one of `do_train` or `do_eval` must be True.''') if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset __a : List[str] = ['''_start_''', '''_delimiter_''', '''_classify_'''] __a : Union[str, Any] = OpenAIGPTTokenizer.from_pretrained(args.model_name) tokenizer.add_tokens(a_) __a : Union[str, Any] = tokenizer.convert_tokens_to_ids(a_) __a : Optional[Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name) model.resize_token_embeddings(len(a_)) model.to(a_) # Load and encode the datasets def tokenize_and_encode(a_ :List[Any]): if isinstance(a_ , a_): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(a_)) elif isinstance(a_ , a_): return obj return [tokenize_and_encode(a_) for o in obj] logger.info('''Encoding dataset...''') __a : Dict = load_rocstories_dataset(args.train_dataset) __a : int = load_rocstories_dataset(args.eval_dataset) __a : Optional[int] = (train_dataset, eval_dataset) __a : List[Any] = tokenize_and_encode(a_) # Compute the max input length for the Transformer __a : List[Any] = model.config.n_positions // 2 - 2 __a : int = max( len(story[:max_length]) + max(len(conta[:max_length]) , len(conta[:max_length])) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset) __a : Union[str, Any] = min(a_ , model.config.n_positions) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders __a : Tuple = pre_process_datasets(a_ , a_ , a_ , *a_) __a , __a : Tuple = tensor_datasets[0], tensor_datasets[1] __a : List[str] = TensorDataset(*a_) __a : Optional[Any] = RandomSampler(a_) __a : str = DataLoader(a_ , sampler=a_ , batch_size=args.train_batch_size) __a : List[str] = TensorDataset(*a_) __a : Optional[int] = SequentialSampler(a_) __a : Optional[Any] = DataLoader(a_ , sampler=a_ , batch_size=args.eval_batch_size) # Prepare optimizer if args.do_train: if args.max_steps > 0: __a : int = args.max_steps __a : Optional[int] = args.max_steps // (len(a_) // args.gradient_accumulation_steps) + 1 else: __a : str = len(a_) // args.gradient_accumulation_steps * args.num_train_epochs __a : List[Any] = list(model.named_parameters()) __a : Optional[int] = ['''bias''', '''LayerNorm.bias''', '''LayerNorm.weight'''] __a : List[str] = [ { '''params''': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], '''weight_decay''': args.weight_decay, }, {'''params''': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], '''weight_decay''': 0.0}, ] __a : int = AdamW(a_ , lr=args.learning_rate , eps=args.adam_epsilon) __a : Union[str, Any] = get_linear_schedule_with_warmup( a_ , num_warmup_steps=args.warmup_steps , num_training_steps=a_) if args.do_train: __a , __a , __a : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs) , desc='''Epoch'''): __a : Dict = 0 __a : Dict = 0 __a : List[str] = tqdm(a_ , desc='''Training''') for step, batch in enumerate(a_): __a : Dict = tuple(t.to(a_) for t in batch) __a , __a , __a , __a : str = batch __a : List[Any] = model(a_ , mc_token_ids=a_ , lm_labels=a_ , mc_labels=a_) __a : Optional[Any] = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() __a : int = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 __a : Tuple = '''Training loss: {:.2e} lr: {:.2e}'''.format(a_ , scheduler.get_lr()[0]) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer __a : Dict = model.module if hasattr(a_ , '''module''') else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` __a : int = os.path.join(args.output_dir , a_) __a : str = os.path.join(args.output_dir , a_) torch.save(model_to_save.state_dict() , a_) model_to_save.config.to_json_file(a_) tokenizer.save_vocabulary(args.output_dir) # Load a trained model and vocabulary that you have fine-tuned __a : str = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir) __a : Union[str, Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir) model.to(a_) if args.do_eval: model.eval() __a , __a : List[Any] = 0, 0 __a , __a : Union[str, Any] = 0, 0 for batch in tqdm(a_ , desc='''Evaluating'''): __a : str = tuple(t.to(a_) for t in batch) __a , __a , __a , __a : List[Any] = batch with torch.no_grad(): __a , __a , __a , __a : str = model( a_ , mc_token_ids=a_ , lm_labels=a_ , mc_labels=a_) __a : List[str] = mc_logits.detach().cpu().numpy() __a : Optional[Any] = mc_labels.to('''cpu''').numpy() __a : str = accuracy(a_ , a_) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0) nb_eval_steps += 1 __a : Tuple = eval_loss / nb_eval_steps __a : List[str] = eval_accuracy / nb_eval_examples __a : List[Any] = tr_loss / nb_tr_steps if args.do_train else None __a : List[str] = {'''eval_loss''': eval_loss, '''eval_accuracy''': eval_accuracy, '''train_loss''': train_loss} __a : Dict = os.path.join(args.output_dir , '''eval_results.txt''') with open(a_ , '''w''') as writer: logger.info('''***** Eval results *****''') for key in sorted(result.keys()): logger.info(''' %s = %s''' , a_ , str(result[key])) writer.write('''%s = %s\n''' % (key, str(result[key]))) if __name__ == "__main__": main()
52
0
def SCREAMING_SNAKE_CASE__ ( lowerCAmelCase_ : int ,lowerCAmelCase_ : int ) -> int: """simple docstring""" return int((input_a, input_a).count(0 ) == 0 ) def SCREAMING_SNAKE_CASE__ ( ) -> None: """simple docstring""" assert and_gate(0 ,0 ) == 0 assert and_gate(0 ,1 ) == 0 assert and_gate(1 ,0 ) == 0 assert and_gate(1 ,1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
220
"""simple docstring""" import unittest import numpy as np from transformers import RobertaConfig, 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.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=13 , _UpperCAmelCase=7 , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=99 , _UpperCAmelCase=32 , _UpperCAmelCase=5 , _UpperCAmelCase=4 , _UpperCAmelCase=37 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=16 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=4 , ): __a : Any = parent __a : Optional[int] = batch_size __a : str = seq_length __a : List[str] = is_training __a : Optional[Any] = use_attention_mask __a : Optional[Any] = use_token_type_ids __a : List[str] = use_labels __a : Union[str, Any] = vocab_size __a : int = hidden_size __a : Union[str, Any] = num_hidden_layers __a : Union[str, Any] = num_attention_heads __a : Dict = intermediate_size __a : List[str] = hidden_act __a : Dict = hidden_dropout_prob __a : Union[str, Any] = attention_probs_dropout_prob __a : int = max_position_embeddings __a : Tuple = type_vocab_size __a : Optional[int] = type_sequence_label_size __a : Optional[Any] = initializer_range __a : Optional[int] = num_choices def _lowerCamelCase ( self ): __a : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a : Union[str, Any] = None if self.use_attention_mask: __a : Any = random_attention_mask([self.batch_size, self.seq_length] ) __a : Optional[int] = None if self.use_token_type_ids: __a : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a : Any = RobertaConfig( 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 _lowerCamelCase ( self ): __a : Dict = self.prepare_config_and_inputs() __a , __a , __a , __a : str = config_and_inputs __a : str = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def _lowerCamelCase ( self ): __a : Any = self.prepare_config_and_inputs() __a , __a , __a , __a : Union[str, Any] = config_and_inputs __a : Optional[int] = True __a : str = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = True __lowerCAmelCase = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCamelCase ( self ): __a : Dict = FlaxRobertaModelTester(self ) @slow def _lowerCamelCase ( self ): for model_class_name in self.all_model_classes: __a : int = model_class_name.from_pretrained('''roberta-base''' , from_pt=_UpperCAmelCase ) __a : List[str] = model(np.ones((1, 1) ) ) self.assertIsNotNone(_UpperCAmelCase )
52
0
import os import tempfile import unittest from transformers import NezhaConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, NezhaModel, ) from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCamelCase_ : def __init__( self : List[str] , __A : Optional[Any] , __A : Any=13 , __A : List[str]=7 , __A : List[Any]=True , __A : str=True , __A : Any=True , __A : Tuple=True , __A : Any=99 , __A : int=32 , __A : List[Any]=5 , __A : List[str]=4 , __A : Dict=37 , __A : int="gelu" , __A : Optional[Any]=0.1 , __A : List[str]=0.1 , __A : Dict=128 , __A : Union[str, Any]=32 , __A : List[Any]=16 , __A : List[str]=2 , __A : Dict=0.0_2 , __A : str=3 , __A : Union[str, Any]=4 , __A : List[str]=None , ): __A : Optional[Any] = parent __A : Optional[int] = batch_size __A : Tuple = seq_length __A : List[Any] = is_training __A : Optional[int] = use_input_mask __A : int = use_token_type_ids __A : Optional[int] = use_labels __A : Any = vocab_size __A : Union[str, Any] = hidden_size __A : List[Any] = num_hidden_layers __A : Optional[Any] = num_attention_heads __A : List[str] = intermediate_size __A : Dict = hidden_act __A : List[str] = hidden_dropout_prob __A : Dict = attention_probs_dropout_prob __A : Any = max_position_embeddings __A : Any = type_vocab_size __A : Union[str, Any] = type_sequence_label_size __A : Optional[Any] = initializer_range __A : List[str] = num_labels __A : Tuple = num_choices __A : str = scope def lowerCAmelCase_ ( self : Optional[Any] ): __A : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __A : List[str] = None if self.use_input_mask: __A : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) __A : List[str] = None if self.use_token_type_ids: __A : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __A : str = None __A : List[str] = None __A : Union[str, Any] = None if self.use_labels: __A : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __A : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __A : str = ids_tensor([self.batch_size] , self.num_choices ) __A : List[str] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase_ ( self : str ): return NezhaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_UpperCAmelCase , initializer_range=self.initializer_range , ) def lowerCAmelCase_ ( self : List[str] ): ( __A ) : Any = self.prepare_config_and_inputs() __A : str = True __A : str = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __A : int = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def lowerCAmelCase_ ( self : List[str] , __A : Tuple , __A : Optional[Any] , __A : str , __A : Tuple , __A : str , __A : Optional[Any] , __A : List[str] ): __A : Any = NezhaModel(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : List[Any] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase ) __A : str = model(_UpperCAmelCase , token_type_ids=_UpperCAmelCase ) __A : int = model(_UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def lowerCAmelCase_ ( self : int , __A : int , __A : Union[str, Any] , __A : int , __A : int , __A : Union[str, Any] , __A : Any , __A : int , __A : Any , __A : Optional[Any] , ): __A : Dict = True __A : str = NezhaModel(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : List[Any] = model( _UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , encoder_hidden_states=_UpperCAmelCase , encoder_attention_mask=_UpperCAmelCase , ) __A : Union[str, Any] = model( _UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , encoder_hidden_states=_UpperCAmelCase , ) __A : Optional[int] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def lowerCAmelCase_ ( self : int , __A : Optional[int] , __A : str , __A : Tuple , __A : List[str] , __A : Optional[Any] , __A : Any , __A : Dict ): __A : List[str] = NezhaForMaskedLM(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : List[str] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , labels=_UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase_ ( self : str , __A : Union[str, Any] , __A : Union[str, Any] , __A : Any , __A : Dict , __A : Optional[Any] , __A : Optional[Any] , __A : Union[str, Any] ): __A : List[Any] = NezhaForNextSentencePrediction(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : Tuple = model( _UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , labels=_UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def lowerCAmelCase_ ( self : Tuple , __A : str , __A : str , __A : str , __A : Union[str, Any] , __A : Optional[int] , __A : Union[str, Any] , __A : Optional[int] ): __A : List[Any] = NezhaForPreTraining(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : str = model( _UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , labels=_UpperCAmelCase , next_sentence_label=_UpperCAmelCase , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def lowerCAmelCase_ ( self : List[Any] , __A : Any , __A : Optional[Any] , __A : List[Any] , __A : List[Any] , __A : int , __A : str , __A : Any ): __A : Dict = NezhaForQuestionAnswering(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : Dict = model( _UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , start_positions=_UpperCAmelCase , end_positions=_UpperCAmelCase , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCAmelCase_ ( self : Optional[Any] , __A : Any , __A : Any , __A : int , __A : Union[str, Any] , __A : Dict , __A : List[Any] , __A : int ): __A : Dict = self.num_labels __A : Any = NezhaForSequenceClassification(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : Optional[Any] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , labels=_UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase_ ( self : List[str] , __A : Optional[int] , __A : Dict , __A : List[Any] , __A : Dict , __A : Any , __A : List[str] , __A : List[Any] ): __A : Dict = self.num_labels __A : Tuple = NezhaForTokenClassification(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : Tuple = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , labels=_UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase_ ( self : Tuple , __A : str , __A : Any , __A : Any , __A : Union[str, Any] , __A : List[Any] , __A : List[str] , __A : List[str] ): __A : Union[str, Any] = self.num_choices __A : Optional[int] = NezhaForMultipleChoice(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() __A : Optional[int] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __A : str = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __A : Any = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __A : List[str] = model( _UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , labels=_UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCAmelCase_ ( self : int ): __A : str = self.prepare_config_and_inputs() ( __A ) : Optional[int] = config_and_inputs __A : Optional[Any] = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): _lowercase : str = ( ( NezhaModel, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, ) if is_torch_available() else () ) _lowercase : List[Any] = ( { '''feature-extraction''': NezhaModel, '''fill-mask''': NezhaForMaskedLM, '''question-answering''': NezhaForQuestionAnswering, '''text-classification''': NezhaForSequenceClassification, '''token-classification''': NezhaForTokenClassification, '''zero-shot''': NezhaForSequenceClassification, } if is_torch_available() else {} ) _lowercase : Optional[int] = True def lowerCAmelCase_ ( self : Union[str, Any] , __A : Any , __A : int , __A : Dict=False ): __A : Dict = super()._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase , return_labels=_UpperCAmelCase ) if return_labels: if model_class in get_values(_UpperCAmelCase ): __A : List[str] = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=_UpperCAmelCase ) __A : Union[str, Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_UpperCAmelCase ) return inputs_dict def lowerCAmelCase_ ( self : str ): __A : List[Any] = NezhaModelTester(self ) __A : Optional[int] = ConfigTester(self , config_class=_UpperCAmelCase , hidden_size=37 ) def lowerCAmelCase_ ( self : int ): self.config_tester.run_common_tests() def lowerCAmelCase_ ( self : Tuple ): __A : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCAmelCase ) def lowerCAmelCase_ ( self : Any ): __A : List[str] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_UpperCAmelCase ) def lowerCAmelCase_ ( self : Dict ): # This regression test was failing with PyTorch < 1.3 ( __A ) : int = self.model_tester.prepare_config_and_inputs_for_decoder() __A : List[Any] = None self.model_tester.create_and_check_model_as_decoder( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ) def lowerCAmelCase_ ( self : str ): __A : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase ) def lowerCAmelCase_ ( self : int ): __A : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] ): __A : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_next_sequence_prediction(*_UpperCAmelCase ) def lowerCAmelCase_ ( self : Dict ): __A : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_UpperCAmelCase ) def lowerCAmelCase_ ( self : str ): __A : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase ) def lowerCAmelCase_ ( self : List[Any] ): __A : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase ) def lowerCAmelCase_ ( self : List[str] ): __A : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase ) @slow def lowerCAmelCase_ ( self : Optional[Any] ): for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __A : Tuple = NezhaModel.from_pretrained(_UpperCAmelCase ) self.assertIsNotNone(_UpperCAmelCase ) @slow @require_torch_gpu def lowerCAmelCase_ ( self : List[Any] ): __A : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # NezhaForMultipleChoice behaves incorrectly in JIT environments. if model_class == NezhaForMultipleChoice: return __A : str = True __A : str = model_class(config=_UpperCAmelCase ) __A : List[Any] = self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) __A : int = torch.jit.trace( _UpperCAmelCase , (inputs_dict["""input_ids"""].to("""cpu""" ), inputs_dict["""attention_mask"""].to("""cpu""" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_UpperCAmelCase , os.path.join(_UpperCAmelCase , """bert.pt""" ) ) __A : int = torch.jit.load(os.path.join(_UpperCAmelCase , """bert.pt""" ) , map_location=_UpperCAmelCase ) loaded(inputs_dict["""input_ids"""].to(_UpperCAmelCase ) , inputs_dict["""attention_mask"""].to(_UpperCAmelCase ) ) @require_torch class lowerCamelCase_ ( unittest.TestCase ): @slow def lowerCAmelCase_ ( self : Any ): __A : str = NezhaModel.from_pretrained("""sijunhe/nezha-cn-base""" ) __A : Dict = torch.tensor([[0, 1, 2, 3, 4, 5]] ) __A : Optional[Any] = torch.tensor([[0, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __A : Optional[int] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase )[0] __A : Optional[int] = torch.Size((1, 6, 768) ) self.assertEqual(output.shape , _UpperCAmelCase ) __A : Optional[int] = torch.tensor([[[0.0_6_8_5, 0.2_4_4_1, 0.1_1_0_2], [0.0_6_0_0, 0.1_9_0_6, 0.1_3_4_9], [0.0_2_2_1, 0.0_8_1_9, 0.0_5_8_6]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _UpperCAmelCase , atol=1e-4 ) ) @slow def lowerCAmelCase_ ( self : int ): __A : Optional[int] = NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base""" ) __A : Optional[Any] = torch.tensor([[0, 1, 2, 3, 4, 5]] ) __A : Union[str, Any] = torch.tensor([[1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __A : Optional[Any] = model(_UpperCAmelCase , attention_mask=_UpperCAmelCase )[0] __A : List[Any] = torch.Size((1, 6, 2_1128) ) self.assertEqual(output.shape , _UpperCAmelCase ) __A : List[Any] = torch.tensor( [[-2.7_9_3_9, -1.7_9_0_2, -2.2_1_8_9], [-2.8_5_8_5, -1.8_9_0_8, -2.3_7_2_3], [-2.6_4_9_9, -1.7_7_5_0, -2.2_5_5_8]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _UpperCAmelCase , atol=1e-4 ) )
17
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''facebook/levit-128S''': '''https://huggingface.co/facebook/levit-128S/resolve/main/config.json''', # See all LeViT models at https://huggingface.co/models?filter=levit } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''levit''' def __init__( self , _UpperCAmelCase=224 , _UpperCAmelCase=3 , _UpperCAmelCase=3 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=16 , _UpperCAmelCase=[128, 256, 384] , _UpperCAmelCase=[4, 8, 12] , _UpperCAmelCase=[4, 4, 4] , _UpperCAmelCase=[16, 16, 16] , _UpperCAmelCase=0 , _UpperCAmelCase=[2, 2, 2] , _UpperCAmelCase=[2, 2, 2] , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = image_size __a : List[Any] = num_channels __a : Dict = kernel_size __a : Optional[int] = stride __a : Optional[int] = padding __a : Dict = hidden_sizes __a : int = num_attention_heads __a : Optional[int] = depths __a : str = key_dim __a : Union[str, Any] = drop_path_rate __a : Optional[Any] = patch_size __a : Tuple = attention_ratio __a : int = mlp_ratio __a : int = initializer_range __a : int = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = version.parse('''1.11''' ) @property def _lowerCamelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowerCamelCase ( self ): return 1e-4
52
0
import re def _a ( __UpperCamelCase : str ): if len(re.findall('''[ATCG]''' ,a_ ) ) != len(a_ ): raise ValueError('''Invalid Strand''' ) return dna.translate(dna.maketrans('''ATCG''' ,'''TAGC''' ) ) if __name__ == "__main__": import doctest doctest.testmod()
233
"""simple docstring""" def __A ( a_ :Tuple , a_ :Union[str, Any] , a_ :int=False) -> List[str]: if isinstance(a_ , a_) and isinstance(a_ , a_): __a : List[str] = len(set_a.intersection(a_)) if alternative_union: __a : List[str] = len(a_) + len(a_) else: __a : int = len(set_a.union(a_)) return intersection / union if isinstance(a_ , (list, tuple)) and isinstance(a_ , (list, tuple)): __a : Union[str, Any] = [element for element in set_a if element in set_b] if alternative_union: __a : Union[str, Any] = len(a_) + len(a_) return len(a_) / union else: __a : List[Any] = set_a + [element for element in set_b if element not in set_a] return len(a_) / len(a_) return len(a_) / len(a_) return None if __name__ == "__main__": A = {'''a''', '''b''', '''c''', '''d''', '''e'''} A = {'''c''', '''d''', '''e''', '''f''', '''h''', '''i'''} print(jaccard_similarity(set_a, set_b))
52
0
"""simple docstring""" import argparse import os import re snake_case_ : Dict = """src/diffusers""" # Pattern that looks at the indentation in a line. snake_case_ : Optional[int] = re.compile(r"""^(\s*)\S""") # Pattern that matches `"key":" and puts `key` in group 0. snake_case_ : Optional[Any] = re.compile(r"""^\s*\"([^\"]+)\":""") # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. snake_case_ : Optional[Any] = re.compile(r"""^\s*_import_structure\[\"([^\"]+)\"\]""") # Pattern that matches `"key",` and puts `key` in group 0. snake_case_ : Tuple = re.compile(r"""^\s*\"([^\"]+)\",\s*$""") # Pattern that matches any `[stuff]` and puts `stuff` in group 0. snake_case_ : Optional[int] = re.compile(r"""\[([^\]]+)\]""") def lowercase_ ( _lowercase : Optional[Any] ): '''simple docstring''' UpperCAmelCase : Dict = _re_indent.search(a_ ) return "" if search is None else search.groups()[0] def lowercase_ ( _lowercase : List[str] , _lowercase : int="" , _lowercase : int=None , _lowercase : Dict=None ): '''simple docstring''' UpperCAmelCase : Optional[int] = 0 UpperCAmelCase : List[Any] = code.split("\n" ) if start_prompt is not None: while not lines[index].startswith(a_ ): index += 1 UpperCAmelCase : int = ['''\n'''.join(lines[:index] )] else: UpperCAmelCase : Optional[int] = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). UpperCAmelCase : Optional[Any] = [lines[index]] index += 1 while index < len(a_ ) and (end_prompt is None or not lines[index].startswith(a_ )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(a_ ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + " " ): current_block.append(lines[index] ) blocks.append("\n".join(a_ ) ) if index < len(a_ ) - 1: UpperCAmelCase : List[str] = [lines[index + 1]] index += 1 else: UpperCAmelCase : str = [] else: blocks.append("\n".join(a_ ) ) UpperCAmelCase : Optional[Any] = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(a_ ) > 0: blocks.append("\n".join(a_ ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(a_ ): blocks.append("\n".join(lines[index:] ) ) return blocks def lowercase_ ( _lowercase : int ): '''simple docstring''' def _inner(_lowercase : Optional[int] ): return key(a_ ).lower().replace("_" , "" ) return _inner def lowercase_ ( _lowercase : Dict , _lowercase : str=None ): '''simple docstring''' def noop(_lowercase : int ): return x if key is None: UpperCAmelCase : int = noop # Constants are all uppercase, they go first. UpperCAmelCase : List[str] = [obj for obj in objects if key(a_ ).isupper()] # Classes are not all uppercase but start with a capital, they go second. UpperCAmelCase : Optional[int] = [obj for obj in objects if key(a_ )[0].isupper() and not key(a_ ).isupper()] # Functions begin with a lowercase, they go last. UpperCAmelCase : Tuple = [obj for obj in objects if not key(a_ )[0].isupper()] UpperCAmelCase : List[Any] = ignore_underscore(a_ ) return sorted(a_ , key=a_ ) + sorted(a_ , key=a_ ) + sorted(a_ , key=a_ ) def lowercase_ ( _lowercase : str ): '''simple docstring''' def _replace(_lowercase : str ): UpperCAmelCase : Optional[int] = match.groups()[0] if "," not in imports: return F"""[{imports}]""" UpperCAmelCase : Tuple = [part.strip().replace("\"" , "" ) for part in imports.split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCAmelCase : Dict = keys[:-1] return "[" + ", ".join([F"""\"{k}\"""" for k in sort_objects(a_ )] ) + "]" UpperCAmelCase : Optional[Any] = import_statement.split("\n" ) if len(a_ ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. UpperCAmelCase : Union[str, Any] = 2 if lines[1].strip() == '''[''' else 1 UpperCAmelCase : List[str] = [(i, _re_strip_line.search(a_ ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] UpperCAmelCase : Union[str, Any] = sort_objects(a_ , key=lambda _lowercase : x[1] ) UpperCAmelCase : List[str] = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(a_ ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: UpperCAmelCase : Optional[int] = _re_bracket_content.sub(_replace , lines[1] ) else: UpperCAmelCase : Dict = [part.strip().replace("\"" , "" ) for part in lines[1].split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCAmelCase : Union[str, Any] = keys[:-1] UpperCAmelCase : Union[str, Any] = get_indent(lines[1] ) + ''', '''.join([F"""\"{k}\"""" for k in sort_objects(a_ )] ) return "\n".join(a_ ) else: # Finally we have to deal with imports fitting on one line UpperCAmelCase : List[str] = _re_bracket_content.sub(_replace , a_ ) return import_statement def lowercase_ ( _lowercase : List[Any] , _lowercase : Optional[Any]=True ): '''simple docstring''' with open(a_ , "r" ) as f: UpperCAmelCase : Any = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 UpperCAmelCase : Any = split_code_in_indented_blocks( a_ , start_prompt="_import_structure = {" , end_prompt="if TYPE_CHECKING:" ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(a_ ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. UpperCAmelCase : List[str] = main_blocks[block_idx] UpperCAmelCase : str = block.split("\n" ) # Get to the start of the imports. UpperCAmelCase : Optional[int] = 0 while line_idx < len(a_ ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: UpperCAmelCase : Union[str, Any] = len(a_ ) else: line_idx += 1 if line_idx >= len(a_ ): continue # Ignore beginning and last line: they don't contain anything. UpperCAmelCase : str = '''\n'''.join(block_lines[line_idx:-1] ) UpperCAmelCase : Union[str, Any] = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. UpperCAmelCase : Any = split_code_in_indented_blocks(a_ , indent_level=a_ ) # We have two categories of import key: list or _import_structure[key].append/extend UpperCAmelCase : Dict = _re_direct_key if '''_import_structure''' in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. UpperCAmelCase : List[str] = [(pattern.search(a_ ).groups()[0] if pattern.search(a_ ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. UpperCAmelCase : Dict = [(i, key) for i, key in enumerate(a_ ) if key is not None] UpperCAmelCase : Optional[Any] = [x[0] for x in sorted(a_ , key=lambda _lowercase : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. UpperCAmelCase : Tuple = 0 UpperCAmelCase : List[str] = [] for i in range(len(a_ ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: UpperCAmelCase : Union[str, Any] = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(a_ ) count += 1 # And we put our main block back together with its first and last line. UpperCAmelCase : int = '''\n'''.join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(a_ ): if check_only: return True else: print(F"""Overwriting {file}.""" ) with open(a_ , "w" ) as f: f.write("\n".join(a_ ) ) def lowercase_ ( _lowercase : int=True ): '''simple docstring''' UpperCAmelCase : Any = [] for root, _, files in os.walk(a_ ): if "__init__.py" in files: UpperCAmelCase : Optional[int] = sort_imports(os.path.join(a_ , "__init__.py" ) , check_only=a_ ) if result: UpperCAmelCase : Optional[int] = [os.path.join(a_ , "__init__.py" )] if len(a_ ) > 0: raise ValueError(F"""Would overwrite {len(a_ )} files, run `make style`.""" ) if __name__ == "__main__": snake_case_ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument("""--check_only""", action="""store_true""", help="""Whether to only check or fix style.""") snake_case_ : Dict = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
595
"""simple docstring""" from __future__ import annotations import os from collections.abc import Mapping A = tuple[int, int] class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a : set[int] = vertices __a : dict[EdgeT, int] = { (min(_UpperCAmelCase ), max(_UpperCAmelCase )): weight for edge, weight in edges.items() } def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase ): self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) __a : Dict = weight def _lowerCamelCase ( self ): __a : Graph = Graph({min(self.vertices )} , {} ) __a : EdgeT __a : int __a : EdgeT __a : int while len(subgraph.vertices ) < len(self.vertices ): __a : Any = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: __a : List[str] = edge __a : Optional[int] = weight subgraph.add_edge(_UpperCAmelCase , _UpperCAmelCase ) return subgraph def __A ( a_ :str = "p107_network.txt") -> int: __a : str = os.path.abspath(os.path.dirname(a_)) __a : str = os.path.join(a_ , a_) __a : dict[EdgeT, int] = {} __a : list[str] __a : int __a : int with open(a_) as f: __a : Optional[int] = f.read().strip().split('''\n''') __a : Dict = [line.split(''',''') for line in data] for edgea in range(1 , len(a_)): for edgea in range(a_): if adjaceny_matrix[edgea][edgea] != "-": __a : Tuple = int(adjaceny_matrix[edgea][edgea]) __a : Graph = Graph(set(range(len(a_))) , a_) __a : Graph = graph.prims_algorithm() __a : int = sum(graph.edges.values()) __a : int = sum(subgraph.edges.values()) return initial_total - optimal_total if __name__ == "__main__": print(F'{solution() = }')
52
0
'''simple docstring''' from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
525
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''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 __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''trocr''' __lowerCAmelCase = ['''past_key_values'''] __lowerCAmelCase = { '''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model''', '''num_hidden_layers''': '''decoder_layers''', } def __init__( self , _UpperCAmelCase=50265 , _UpperCAmelCase=1024 , _UpperCAmelCase=12 , _UpperCAmelCase=16 , _UpperCAmelCase=4096 , _UpperCAmelCase="gelu" , _UpperCAmelCase=512 , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.0 , _UpperCAmelCase=0.0 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=1 , _UpperCAmelCase=0 , _UpperCAmelCase=2 , **_UpperCAmelCase , ): __a : List[str] = vocab_size __a : Optional[Any] = d_model __a : Optional[Any] = decoder_layers __a : Union[str, Any] = decoder_attention_heads __a : int = decoder_ffn_dim __a : List[Any] = activation_function __a : Any = max_position_embeddings __a : Dict = dropout __a : List[Any] = attention_dropout __a : Optional[Any] = activation_dropout __a : str = init_std __a : List[str] = decoder_layerdrop __a : Union[str, Any] = use_cache __a : Optional[Any] = scale_embedding __a : List[Any] = use_learned_position_embeddings __a : Optional[int] = layernorm_embedding super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , decoder_start_token_id=_UpperCAmelCase , **_UpperCAmelCase , )
52
0
'''simple docstring''' import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def lowerCamelCase__ ( A_ ): # picklable for multiprocessing return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def lowerCamelCase__ ( ): with parallel_backend("spark" ): assert ParallelBackendConfig.backend_name == "spark" UpperCAmelCase_ = [1, 2, 3] with pytest.raises(a_ ): with parallel_backend("unsupported backend" ): map_nested(a_ , a_ , num_proc=2 ) with pytest.raises(a_ ): with parallel_backend("unsupported backend" ): map_nested(a_ , a_ , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("num_proc" , [2, -1] ) def lowerCamelCase__ ( A_ ): UpperCAmelCase_ = [1, 2] UpperCAmelCase_ = {'''a''': 1, '''b''': 2} UpperCAmelCase_ = {'''a''': [1, 2], '''b''': [3, 4]} UpperCAmelCase_ = {'''a''': {'''1''': 1}, '''b''': 2} UpperCAmelCase_ = {'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4} UpperCAmelCase_ = [2, 3] UpperCAmelCase_ = {'''a''': 2, '''b''': 3} UpperCAmelCase_ = {'''a''': [2, 3], '''b''': [4, 5]} UpperCAmelCase_ = {'''a''': {'''1''': 2}, '''b''': 3} UpperCAmelCase_ = {'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5} with parallel_backend("spark" ): assert map_nested(a_ , a_ , num_proc=a_ ) == expected_map_nested_sa assert map_nested(a_ , a_ , num_proc=a_ ) == expected_map_nested_sa assert map_nested(a_ , a_ , num_proc=a_ ) == expected_map_nested_sa assert map_nested(a_ , a_ , num_proc=a_ ) == expected_map_nested_sa assert map_nested(a_ , a_ , num_proc=a_ ) == expected_map_nested_sa
660
"""simple docstring""" import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def __A ( a_ :Union[str, Any] , a_ :Union[str, Any] , a_ :Optional[Any] , a_ :Optional[int]=5) -> List[Any]: # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count('''<mask>''') == 1 __a : Optional[Any] = torch.tensor(tokenizer.encode(a_ , add_special_tokens=a_)).unsqueeze(0) # Batch size 1 __a : Dict = model(a_)[0] # The last hidden-state is the first element of the output tuple __a : Tuple = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() __a : Any = logits[0, masked_index, :] __a : Any = logits.softmax(dim=0) __a , __a : Optional[Any] = prob.topk(k=a_ , dim=0) __a : Optional[int] = ''' '''.join( [tokenizer.convert_ids_to_tokens(indices[i].item()) for i in range(len(a_))]) __a : List[str] = tokenizer.mask_token __a : Optional[int] = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(''' ''')): __a : Optional[Any] = predicted_token_bpe.replace('''\u2581''' , ''' ''') if " {0}".format(a_) in masked_input: topk_filled_outputs.append( ( masked_input.replace(''' {0}'''.format(a_) , a_), values[index].item(), predicted_token, )) else: topk_filled_outputs.append( ( masked_input.replace(a_ , a_), values[index].item(), predicted_token, )) return topk_filled_outputs A = CamembertTokenizer.from_pretrained('''camembert-base''') A = CamembertForMaskedLM.from_pretrained('''camembert-base''') model.eval() A = '''Le camembert est <mask> :)''' print(fill_mask(masked_input, model, tokenizer, topk=3))
52
0
__magic_name__ = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __magic_name__ = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __magic_name__ = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
250
"""simple docstring""" import unittest from knapsack import greedy_knapsack as kp class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): __a : Optional[int] = [10, 20, 30, 40, 50, 60] __a : Union[str, Any] = [2, 4, 6, 8, 10, 12] __a : List[str] = 100 self.assertEqual(kp.calc_profit(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) , 210 ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''max_weight must greater than zero.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''Weight can not be negative.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''Profit can not be negative.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex(_UpperCAmelCase , '''max_weight must greater than zero.''' ) def _lowerCamelCase ( self ): self.assertRaisesRegex( _UpperCAmelCase , '''The length of profit and weight must be same.''' ) if __name__ == "__main__": unittest.main()
52
0
import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs A_: List[str] = imread(R'digital_image_processing/image_data/lena_small.jpg') A_: Optional[int] = cvtColor(img, COLOR_BGR2GRAY) def __lowerCAmelCase ( ): """simple docstring""" _lowercase = cn.convert_to_negative(a_ ) # assert negative_img array for at least one True assert negative_img.any() def __lowerCAmelCase ( ): """simple docstring""" with Image.open("""digital_image_processing/image_data/lena_small.jpg""" ) as img: # Work around assertion for response assert str(cc.change_contrast(a_ ,110 ) ).startswith( """<PIL.Image.Image image mode=RGB size=100x100 at""" ) def __lowerCAmelCase ( ): """simple docstring""" _lowercase = canny.gen_gaussian_kernel(9 ,sigma=1.4 ) # Assert ambiguous array assert resp.all() def __lowerCAmelCase ( ): """simple docstring""" _lowercase = imread("""digital_image_processing/image_data/lena_small.jpg""" ,0 ) # assert ambiguous array for all == True assert canny_img.all() _lowercase = canny.canny(a_ ) # assert canny array for at least one True assert canny_array.any() def __lowerCAmelCase ( ): """simple docstring""" assert gg.gaussian_filter(a_ ,5 ,sigma=0.9 ).all() def __lowerCAmelCase ( ): """simple docstring""" _lowercase = array([[0.2_5, 0.5, 0.2_5], [0.5, -3, 0.5], [0.2_5, 0.5, 0.2_5]] ) _lowercase = conv.img_convolve(a_ ,a_ ).astype(a_ ) assert res.any() def __lowerCAmelCase ( ): """simple docstring""" assert med.median_filter(a_ ,3 ).any() def __lowerCAmelCase ( ): """simple docstring""" _lowercase = sob.sobel_filter(a_ ) assert grad.any() and theta.any() def __lowerCAmelCase ( ): """simple docstring""" _lowercase = sp.make_sepia(a_ ,20 ) assert sepia.all() def __lowerCAmelCase ( _A = "digital_image_processing/image_data/lena_small.jpg" ): """simple docstring""" _lowercase = bs.Burkes(imread(a_ ,1 ) ,120 ) burkes.process() assert burkes.output_img.any() def __lowerCAmelCase ( _A = "digital_image_processing/image_data/lena_small.jpg" ,): """simple docstring""" _lowercase = rs.NearestNeighbour(imread(a_ ,1 ) ,400 ,200 ) nn.process() assert nn.output.any() def __lowerCAmelCase ( ): """simple docstring""" _lowercase = '''digital_image_processing/image_data/lena.jpg''' # Reading the image and converting it to grayscale. _lowercase = imread(a_ ,0 ) # Test for get_neighbors_pixel function() return not None _lowercase = 0 _lowercase = 0 _lowercase = image[x_coordinate][y_coordinate] _lowercase = lbp.get_neighbors_pixel( a_ ,a_ ,a_ ,a_ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image _lowercase = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 ,image.shape[0] ): for j in range(0 ,image.shape[1] ): _lowercase = lbp.local_binary_value(a_ ,a_ ,a_ ) assert lbp_image.any()
398
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = {} class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''llama''' __lowerCAmelCase = ['''past_key_values'''] def __init__( self , _UpperCAmelCase=32000 , _UpperCAmelCase=4096 , _UpperCAmelCase=11008 , _UpperCAmelCase=32 , _UpperCAmelCase=32 , _UpperCAmelCase=None , _UpperCAmelCase="silu" , _UpperCAmelCase=2048 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-6 , _UpperCAmelCase=True , _UpperCAmelCase=0 , _UpperCAmelCase=1 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=False , _UpperCAmelCase=None , **_UpperCAmelCase , ): __a : Dict = vocab_size __a : Union[str, Any] = max_position_embeddings __a : str = hidden_size __a : List[str] = intermediate_size __a : Any = num_hidden_layers __a : int = num_attention_heads # for backward compatibility if num_key_value_heads is None: __a : Union[str, Any] = num_attention_heads __a : Optional[int] = num_key_value_heads __a : Dict = hidden_act __a : Union[str, Any] = initializer_range __a : int = rms_norm_eps __a : Optional[int] = pretraining_tp __a : Optional[Any] = use_cache __a : Optional[Any] = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , tie_word_embeddings=_UpperCAmelCase , **_UpperCAmelCase , ) def _lowerCamelCase ( self ): if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _UpperCAmelCase ) 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}""" ) __a : Tuple = self.rope_scaling.get('''type''' , _UpperCAmelCase ) __a : Optional[int] = self.rope_scaling.get('''factor''' , _UpperCAmelCase ) 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(_UpperCAmelCase , _UpperCAmelCase ) or rope_scaling_factor <= 1.0: raise ValueError(f"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""" )
52
0
'''simple docstring''' import argparse from pathlib import Path from transformers import AutoConfig, AutoTokenizer, RagConfig, RagSequenceForGeneration, RagTokenForGeneration def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , ) -> List[Any]: if config_name_or_path is None: __lowerCamelCase = '''facebook/rag-token-base''' if model_type == '''rag_token''' else '''facebook/rag-sequence-base''' if generator_tokenizer_name_or_path is None: __lowerCamelCase = generator_name_or_path if question_encoder_tokenizer_name_or_path is None: __lowerCamelCase = question_encoder_name_or_path __lowerCamelCase = RagTokenForGeneration if model_type == '''rag_token''' else RagSequenceForGeneration # Save model. __lowerCamelCase = RagConfig.from_pretrained(a_ ) __lowerCamelCase = AutoConfig.from_pretrained(a_ ) __lowerCamelCase = AutoConfig.from_pretrained(a_ ) __lowerCamelCase = gen_config __lowerCamelCase = question_encoder_config __lowerCamelCase = model_class.from_pretrained_question_encoder_generator( a_ , a_ , config=a_ ) rag_model.save_pretrained(a_ ) # Sanity check. model_class.from_pretrained(a_ ) # Save tokenizers. __lowerCamelCase = AutoTokenizer.from_pretrained(a_ ) gen_tokenizer.save_pretrained(dest_dir / '''generator_tokenizer/''' ) __lowerCamelCase = AutoTokenizer.from_pretrained(a_ ) question_encoder_tokenizer.save_pretrained(dest_dir / '''question_encoder_tokenizer/''' ) if __name__ == "__main__": __UpperCAmelCase =argparse.ArgumentParser() parser.add_argument( "--model_type", choices=["rag_sequence", "rag_token"], required=True, type=str, help="RAG model type: rag_sequence, rag_token", ) parser.add_argument("--dest", type=str, required=True, help="Path to the output checkpoint directory.") parser.add_argument("--generator_name_or_path", type=str, required=True, help="Generator model identifier") parser.add_argument( "--question_encoder_name_or_path", type=str, required=True, help="Question encoder model identifier" ) parser.add_argument( "--generator_tokenizer_name_or_path", type=str, help="Generator tokenizer identifier, if not specified, resolves to ``generator_name_or_path``", ) parser.add_argument( "--question_encoder_tokenizer_name_or_path", type=str, help="Question encoder tokenizer identifier, if not specified, resolves to ``question_encoder_name_or_path``", ) parser.add_argument( "--config_name_or_path", type=str, help=( "Identifier of the model config to use, if not provided, resolves to a base config for a given" " ``model_type``" ), ) __UpperCAmelCase =parser.parse_args() __UpperCAmelCase =Path(args.dest) dest_dir.mkdir(exist_ok=True) consolidate( args.model_type, args.generator_name_or_path, args.question_encoder_name_or_path, dest_dir, args.config_name_or_path, args.generator_tokenizer_name_or_path, args.question_encoder_tokenizer_name_or_path, )
546
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import is_flaky, 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 DonutImageProcessor class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=3 , _UpperCAmelCase=18 , _UpperCAmelCase=30 , _UpperCAmelCase=400 , _UpperCAmelCase=True , _UpperCAmelCase=None , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=[0.5, 0.5, 0.5] , _UpperCAmelCase=[0.5, 0.5, 0.5] , ): __a : int = parent __a : str = batch_size __a : List[Any] = num_channels __a : Union[str, Any] = image_size __a : List[Any] = min_resolution __a : str = max_resolution __a : List[str] = do_resize __a : Optional[int] = size if size is not None else {'''height''': 18, '''width''': 20} __a : str = do_thumbnail __a : str = do_align_axis __a : Dict = do_pad __a : Union[str, Any] = do_normalize __a : List[str] = image_mean __a : Optional[int] = image_std def _lowerCamelCase ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = DonutImageProcessor if is_vision_available() else None def _lowerCamelCase ( self ): __a : Tuple = DonutImageProcessingTester(self ) @property def _lowerCamelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_resize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_thumbnail''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_align_long_axis''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_pad''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_mean''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''image_std''' ) ) def _lowerCamelCase ( self ): __a : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 20} ) __a : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) # Previous config had dimensions in (width, height) order __a : int = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'''height''': 84, '''width''': 42} ) def _lowerCamelCase ( self ): pass @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , Image.Image ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : int = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , numpify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , np.ndarray ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : str = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) @is_flaky() def _lowerCamelCase ( self ): # Initialize image_processing __a : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCAmelCase , torchify=_UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(_UpperCAmelCase , torch.Tensor ) # Test not batched input __a : Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a : List[str] = image_processing(_UpperCAmelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
52
0
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 lowercase : def __init__( self , snake_case , snake_case=3 , snake_case=32 , snake_case=3 , snake_case=10 , snake_case=[10, 20, 30, 40] , snake_case=[1, 1, 2, 1] , snake_case=True , snake_case=True , snake_case="relu" , snake_case=3 , snake_case=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(_UpperCAmelCase ) def a ( self ): 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 a ( self ): 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 a ( self , snake_case , snake_case , snake_case ): snake_case_ = TFResNetModel(config=_UpperCAmelCase ) snake_case_ = model(_UpperCAmelCase ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def a ( self , snake_case , snake_case , snake_case ): snake_case_ = self.num_labels snake_case_ = TFResNetForImageClassification(_UpperCAmelCase ) snake_case_ = model(_UpperCAmelCase , labels=_UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def a ( self ): snake_case_ = self.prepare_config_and_inputs() snake_case_ = config_and_inputs snake_case_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class lowercase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): __SCREAMING_SNAKE_CASE : int = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () __SCREAMING_SNAKE_CASE : Union[str, Any] = ( {'''feature-extraction''': TFResNetModel, '''image-classification''': TFResNetForImageClassification} if is_tf_available() else {} ) __SCREAMING_SNAKE_CASE : str = False __SCREAMING_SNAKE_CASE : Optional[Any] = False __SCREAMING_SNAKE_CASE : Optional[int] = False __SCREAMING_SNAKE_CASE : Any = False __SCREAMING_SNAKE_CASE : List[Any] = False def a ( self ): snake_case_ = TFResNetModelTester(self ) snake_case_ = ConfigTester(self , config_class=_UpperCAmelCase , has_text_modality=_UpperCAmelCase ) def a ( self ): 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 a ( self ): return @unittest.skip(reason='ResNet does not use inputs_embeds' ) def a ( self ): pass @unittest.skip(reason='ResNet does not support input and output embeddings' ) def a ( self ): pass def a ( self ): snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = model_class(_UpperCAmelCase ) 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] , _UpperCAmelCase ) def a ( self ): snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCAmelCase ) def a ( self ): def check_hidden_states_output(snake_case , snake_case , snake_case ): snake_case_ = model_class(_UpperCAmelCase ) snake_case_ = model(**self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) ) 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(_UpperCAmelCase ) , 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_ = 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(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ = True check_hidden_states_output(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def a ( self ): snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase ) @slow def a ( self ): for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = TFResNetModel.from_pretrained(_UpperCAmelCase ) self.assertIsNotNone(_UpperCAmelCase ) def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class lowercase ( unittest.TestCase ): @cached_property def a ( self ): return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def a ( self ): 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=_UpperCAmelCase , return_tensors='tf' ) # forward pass snake_case_ = model(**_UpperCAmelCase ) # verify the logits snake_case_ = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , _UpperCAmelCase ) snake_case_ = tf.constant([-11.1069, -9.78_77, -8.37_77] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , _UpperCAmelCase , atol=1e-4 ) )
362
"""simple docstring""" from __future__ import annotations def __A ( a_ :list[int]) -> int: if not nums: return 0 __a : Any = nums[0] __a : Optional[Any] = 0 for num in nums[1:]: __a , __a : Optional[Any] = ( max_excluding + num, max(a_ , a_), ) return max(a_ , a_) if __name__ == "__main__": import doctest doctest.testmod()
52
0
"""simple docstring""" 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 a ( unittest.TestCase ): def __init__( self : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : Dict=13 , __lowerCAmelCase : Union[str, Any]=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : int=99 , __lowerCAmelCase : Any=32 , __lowerCAmelCase : str=5 , __lowerCAmelCase : List[Any]=4 , __lowerCAmelCase : List[Any]=37 , __lowerCAmelCase : List[str]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : Optional[Any]=512 , __lowerCAmelCase : Dict=16 , __lowerCAmelCase : Union[str, Any]=2 , __lowerCAmelCase : Dict=0.02 , __lowerCAmelCase : str=4 , ): _UpperCAmelCase = parent _UpperCAmelCase = batch_size _UpperCAmelCase = seq_length _UpperCAmelCase = is_training _UpperCAmelCase = use_attention_mask _UpperCAmelCase = use_token_type_ids _UpperCAmelCase = use_labels _UpperCAmelCase = vocab_size _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_act _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = max_position_embeddings _UpperCAmelCase = type_vocab_size _UpperCAmelCase = type_sequence_label_size _UpperCAmelCase = initializer_range _UpperCAmelCase = num_choices def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase = None if self.use_attention_mask: _UpperCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCAmelCase = None if self.use_token_type_ids: _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _UpperCAmelCase = 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=_UpperCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = self.prepare_config_and_inputs() _UpperCAmelCase = config_and_inputs _UpperCAmelCase = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def lowerCAmelCase_ ( self : List[Any] ): _UpperCAmelCase = self.prepare_config_and_inputs() _UpperCAmelCase = config_and_inputs _UpperCAmelCase = True _UpperCAmelCase = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, attention_mask, encoder_hidden_states, encoder_attention_mask, ) @require_flax class a ( _UpperCamelCase , unittest.TestCase ): _snake_case : Tuple = True _snake_case : Dict = ( ( FlaxBertModel, FlaxBertForPreTraining, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForQuestionAnswering, FlaxBertForNextSentencePrediction, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertForQuestionAnswering, ) if is_flax_available() else () ) def lowerCAmelCase_ ( self : Dict ): _UpperCAmelCase = FlaxBertModelTester(self ) @slow def lowerCAmelCase_ ( self : Optional[int] ): # Only check this for base model, not necessary for all model classes. # This will also help speed-up tests. _UpperCAmelCase = FlaxBertModel.from_pretrained("""bert-base-cased""" ) _UpperCAmelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(_UpperCAmelCase )
277
"""simple docstring""" import unittest from transformers import BigBirdTokenizer, BigBirdTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A = '''▁''' A = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = BigBirdTokenizer __lowerCAmelCase = BigBirdTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = True def _lowerCamelCase ( self ): super().setUp() __a : Dict = self.tokenizer_class(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCamelCase ( self ): __a : List[str] = '''<s>''' __a : Tuple = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''[MASK]''' ) self.assertEqual(len(_UpperCAmelCase ) , 1004 ) def _lowerCamelCase ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def _lowerCamelCase ( self ): if not self.test_rust_tokenizer: return __a : Dict = self.get_tokenizer() __a : Any = self.get_rust_tokenizer() __a : int = '''I was born in 92000, and this is falsé.''' __a : Optional[Any] = tokenizer.tokenize(_UpperCAmelCase ) __a : List[str] = rust_tokenizer.tokenize(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Dict = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) __a : Any = rust_tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) __a : Tuple = self.get_rust_tokenizer() __a : Tuple = tokenizer.encode(_UpperCAmelCase ) __a : List[Any] = rust_tokenizer.encode(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = BigBirdTokenizer(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) __a : Optional[int] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) __a : Optional[int] = 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''', '''é''', '''.''', ] , ) __a : Optional[Any] = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) __a : Optional[int] = 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>''', '''.''', ] , ) @cached_property def _lowerCamelCase ( self ): return BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) @slow def _lowerCamelCase ( self ): __a : str = '''Hello World!''' __a : str = [65, 18536, 2260, 101, 66] self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @slow def _lowerCamelCase ( self ): __a : 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 __a : Optional[Any] = [65, 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, 66] # noqa: E231 # fmt: on self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @require_torch @slow def _lowerCamelCase ( self ): import torch from transformers import BigBirdConfig, BigBirdModel # Build sequence __a : List[Any] = list(self.big_tokenizer.get_vocab().keys() )[:10] __a : List[str] = ''' '''.join(_UpperCAmelCase ) __a : Tuple = self.big_tokenizer.encode_plus(_UpperCAmelCase , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Any = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=_UpperCAmelCase ) __a : Optional[Any] = BigBirdConfig(attention_type='''original_full''' ) __a : Tuple = BigBirdModel(_UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_UpperCAmelCase ) model(**_UpperCAmelCase ) @slow def _lowerCamelCase ( self ): __a : Union[str, Any] = BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''' ) __a : List[Any] = tokenizer.decode(tokenizer('''Paris is the [MASK].''' ).input_ids ) self.assertTrue(decoded_text == '''[CLS] Paris is the[MASK].[SEP]''' ) @slow def _lowerCamelCase ( self ): # fmt: off __a : Optional[Any] = {'''input_ids''': [[65, 39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114, 66], [65, 448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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], [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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name='''google/bigbird-roberta-base''' , revision='''215c99f1600e06f83acce68422f2035b2b5c3510''' , )
52
0
class lowerCAmelCase_ : '''simple docstring''' def __init__( self ): SCREAMING_SNAKE_CASE_ : str =0 SCREAMING_SNAKE_CASE_ : int =0 SCREAMING_SNAKE_CASE_ : Optional[int] ={} def __lowerCamelCase ( self , __UpperCAmelCase ): if vertex not in self.adjacency: SCREAMING_SNAKE_CASE_ : Tuple ={} self.num_vertices += 1 def __lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): self.add_vertex(_UpperCAmelCase ) self.add_vertex(_UpperCAmelCase ) if head == tail: return SCREAMING_SNAKE_CASE_ : Optional[int] =weight SCREAMING_SNAKE_CASE_ : Dict =weight def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : str =self.get_edges() for edge in edges: SCREAMING_SNAKE_CASE_ : Any =edge edges.remove((tail, head, weight) ) for i in range(len(_UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ : Union[str, Any] =list(edges[i] ) edges.sort(key=lambda __UpperCAmelCase : e[2] ) for i in range(len(_UpperCAmelCase ) - 1 ): if edges[i][2] >= edges[i + 1][2]: SCREAMING_SNAKE_CASE_ : Dict =edges[i][2] + 1 for edge in edges: SCREAMING_SNAKE_CASE_ : str =edge SCREAMING_SNAKE_CASE_ : Any =weight SCREAMING_SNAKE_CASE_ : Optional[int] =weight def __str__( self ): SCREAMING_SNAKE_CASE_ : str ='''''' for tail in self.adjacency: for head in self.adjacency[tail]: SCREAMING_SNAKE_CASE_ : Optional[Any] =self.adjacency[head][tail] string += F"""{head} -> {tail} == {weight}\n""" return string.rstrip('\n' ) def __lowerCamelCase ( self ): SCREAMING_SNAKE_CASE_ : List[Any] =[] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def __lowerCamelCase ( self ): return self.adjacency.keys() @staticmethod def __lowerCamelCase ( __UpperCAmelCase=None , __UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_ : str =Graph() if vertices is None: SCREAMING_SNAKE_CASE_ : Any =[] if edges is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] =[] for vertex in vertices: g.add_vertex(_UpperCAmelCase ) for edge in edges: g.add_edge(*_UpperCAmelCase ) return g class lowerCAmelCase_ : '''simple docstring''' def __init__( self ): SCREAMING_SNAKE_CASE_ : str ={} SCREAMING_SNAKE_CASE_ : Union[str, Any] ={} def __len__( self ): return len(self.parent ) def __lowerCamelCase ( self , __UpperCAmelCase ): if item in self.parent: return self.find(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ : str =item SCREAMING_SNAKE_CASE_ : List[Any] =0 return item def __lowerCamelCase ( self , __UpperCAmelCase ): if item not in self.parent: return self.make_set(_UpperCAmelCase ) if item != self.parent[item]: SCREAMING_SNAKE_CASE_ : Any =self.find(self.parent[item] ) return self.parent[item] def __lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase ): SCREAMING_SNAKE_CASE_ : Optional[int] =self.find(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ : Tuple =self.find(_UpperCAmelCase ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: SCREAMING_SNAKE_CASE_ : List[str] =roota return roota if self.rank[roota] < self.rank[roota]: SCREAMING_SNAKE_CASE_ : Union[str, Any] =roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 SCREAMING_SNAKE_CASE_ : Any =roota return roota return None @staticmethod def __lowerCamelCase ( __UpperCAmelCase ): SCREAMING_SNAKE_CASE_ : Union[str, Any] =graph.num_vertices SCREAMING_SNAKE_CASE_ : Tuple =Graph.UnionFind() SCREAMING_SNAKE_CASE_ : Union[str, Any] =[] while num_components > 1: SCREAMING_SNAKE_CASE_ : Tuple ={} for vertex in graph.get_vertices(): SCREAMING_SNAKE_CASE_ : List[str] =-1 SCREAMING_SNAKE_CASE_ : int =graph.get_edges() for edge in edges: SCREAMING_SNAKE_CASE_ : Any =edge edges.remove((tail, head, weight) ) for edge in edges: SCREAMING_SNAKE_CASE_ : Optional[Any] =edge SCREAMING_SNAKE_CASE_ : Any =union_find.find(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ : Optional[int] =union_find.find(_UpperCAmelCase ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: SCREAMING_SNAKE_CASE_ : Any =[head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: SCREAMING_SNAKE_CASE_ : Dict =[head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: SCREAMING_SNAKE_CASE_ : Optional[int] =cheap_edge[vertex] if union_find.find(_UpperCAmelCase ) != union_find.find(_UpperCAmelCase ): union_find.union(_UpperCAmelCase , _UpperCAmelCase ) mst_edges.append(cheap_edge[vertex] ) SCREAMING_SNAKE_CASE_ : List[Any] =num_components - 1 SCREAMING_SNAKE_CASE_ : Optional[Any] =Graph.build(edges=_UpperCAmelCase ) return mst
220
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A = logging.get_logger(__name__) A = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''convnextv2''' def __init__( self , _UpperCAmelCase=3 , _UpperCAmelCase=4 , _UpperCAmelCase=4 , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0.0 , _UpperCAmelCase=224 , _UpperCAmelCase=None , _UpperCAmelCase=None , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : List[str] = num_channels __a : str = patch_size __a : Dict = num_stages __a : List[str] = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes __a : List[str] = [3, 3, 9, 3] if depths is None else depths __a : List[Any] = hidden_act __a : Any = initializer_range __a : Optional[int] = layer_norm_eps __a : List[Any] = drop_path_rate __a : Any = image_size __a : str = ['''stem'''] + [f"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] __a , __a : Optional[int] = get_aligned_output_features_output_indices( out_features=_UpperCAmelCase , out_indices=_UpperCAmelCase , stage_names=self.stage_names )
52
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) UpperCAmelCase_ : Optional[int] = { '''configuration_efficientformer''': [ '''EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientFormerConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Any = ['''EfficientFormerImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Tuple = [ '''EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientFormerForImageClassification''', '''EfficientFormerForImageClassificationWithTeacher''', '''EfficientFormerModel''', '''EfficientFormerPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : List[Any] = [ '''TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFEfficientFormerForImageClassification''', '''TFEfficientFormerForImageClassificationWithTeacher''', '''TFEfficientFormerModel''', '''TFEfficientFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys UpperCAmelCase_ : Optional[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
17
"""simple docstring""" import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = (DDPMScheduler,) def _lowerCamelCase ( self , **_UpperCAmelCase ): __a : int = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**_UpperCAmelCase ) return config def _lowerCamelCase ( self ): for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=_UpperCAmelCase , beta_end=_UpperCAmelCase ) def _lowerCamelCase ( self ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_UpperCAmelCase ) def _lowerCamelCase ( self ): for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=_UpperCAmelCase ) def _lowerCamelCase ( self ): self.check_over_configs(thresholding=_UpperCAmelCase ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_UpperCAmelCase , prediction_type=_UpperCAmelCase , sample_max_value=_UpperCAmelCase , ) def _lowerCamelCase ( self ): for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): for t in [0, 500, 999]: self.check_over_forward(time_step=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Dict = scheduler_class(**_UpperCAmelCase ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def _lowerCamelCase ( self ): __a : int = self.scheduler_classes[0] __a : int = self.get_scheduler_config() __a : Optional[Any] = scheduler_class(**_UpperCAmelCase ) __a : int = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[Any] = self.dummy_sample_deter __a : Union[str, Any] = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Optional[int] = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : List[Any] = pred_prev_sample __a : int = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : Union[str, Any] = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def _lowerCamelCase ( self ): __a : Dict = self.scheduler_classes[0] __a : int = self.get_scheduler_config(prediction_type='''v_prediction''' ) __a : int = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = len(_UpperCAmelCase ) __a : List[str] = self.dummy_model() __a : List[str] = self.dummy_sample_deter __a : str = torch.manual_seed(0 ) for t in reversed(range(_UpperCAmelCase ) ): # 1. predict noise residual __a : Dict = model(_UpperCAmelCase , _UpperCAmelCase ) # 2. predict previous mean of sample x_t-1 __a : Dict = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , generator=_UpperCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance __a : Optional[int] = pred_prev_sample __a : Optional[int] = torch.sum(torch.abs(_UpperCAmelCase ) ) __a : int = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Any = self.get_scheduler_config() __a : str = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_UpperCAmelCase ) __a : List[Any] = scheduler.timesteps for i, timestep in enumerate(_UpperCAmelCase ): if i == len(_UpperCAmelCase ) - 1: __a : Union[str, Any] = -1 else: __a : str = timesteps[i + 1] __a : Dict = scheduler.previous_timestep(_UpperCAmelCase ) __a : str = prev_t.item() self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Tuple = self.scheduler_classes[0] __a : Dict = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Optional[Any] = [100, 87, 50, 51, 0] with self.assertRaises(_UpperCAmelCase , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[Any] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : Any = scheduler_class(**_UpperCAmelCase ) __a : Union[str, Any] = [100, 87, 50, 1, 0] __a : Optional[int] = len(_UpperCAmelCase ) with self.assertRaises(_UpperCAmelCase , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=_UpperCAmelCase , timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Optional[int] = self.scheduler_classes[0] __a : Optional[Any] = self.get_scheduler_config() __a : List[str] = scheduler_class(**_UpperCAmelCase ) __a : List[Any] = [scheduler.config.num_train_timesteps] with self.assertRaises( _UpperCAmelCase , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=_UpperCAmelCase )
52
0
import json import logging import os import sys from pathlib import Path import finetune_rag from transformers.file_utils import is_apex_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, require_ray, require_torch_gpu, require_torch_multi_gpu, ) logging.basicConfig(level=logging.DEBUG) A__ : List[str] = logging.getLogger() A__ : Optional[int] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowercase ( _UpperCamelCase ): def lowercase_ ( self , SCREAMING_SNAKE_CASE__ ): """simple docstring""" os.makedirs(_UpperCAmelCase , exist_ok=_UpperCAmelCase ) lowerCAmelCase__ : Union[str, Any] = {'''source''': '''What is love ?''', '''target''': '''life'''} lowerCAmelCase__ : Tuple = {'''train''': 12, '''val''': 2, '''test''': 2} for split in ["train", "test", "val"]: for field in ["source", "target"]: lowerCAmelCase__ : Tuple = '''\n'''.join([contents[field]] * n_lines[split] ) with open(os.path.join(_UpperCAmelCase , f'''{split}.{field}''' ) , '''w''' ) as f: f.write(_UpperCAmelCase ) def lowercase_ ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = "pytorch" ): """simple docstring""" lowerCAmelCase__ : Optional[int] = self.get_auto_remove_tmp_dir() lowerCAmelCase__ : Optional[int] = os.path.join(_UpperCAmelCase , '''output''' ) lowerCAmelCase__ : Optional[int] = os.path.join(_UpperCAmelCase , '''data''' ) self._create_dummy_data(data_dir=_UpperCAmelCase ) lowerCAmelCase__ : int = f''' --data_dir {data_dir} \ --output_dir {output_dir} \ --model_name_or_path facebook/rag-sequence-base \ --model_type rag_sequence \ --do_train \ --do_predict \ --n_val -1 \ --val_check_interval 1.0 \ --train_batch_size 2 \ --eval_batch_size 1 \ --max_source_length 25 \ --max_target_length 25 \ --val_max_target_length 25 \ --test_max_target_length 25 \ --label_smoothing 0.1 \ --dropout 0.1 \ --attention_dropout 0.1 \ --weight_decay 0.001 \ --adam_epsilon 1e-08 \ --max_grad_norm 0.1 \ --lr_scheduler polynomial \ --learning_rate 3e-04 \ --num_train_epochs 1 \ --warmup_steps 4 \ --gradient_accumulation_steps 1 \ --distributed-port 8787 \ --use_dummy_dataset 1 \ --distributed_retriever {distributed_retriever} \ '''.split() if gpus > 0: testargs.append(f'''--gpus={gpus}''' ) if is_apex_available(): testargs.append('''--fp16''' ) else: testargs.append('''--gpus=0''' ) testargs.append('''--distributed_backend=ddp_cpu''' ) testargs.append('''--num_processes=2''' ) lowerCAmelCase__ : Dict = [sys.executable, str(Path(finetune_rag.__file__ ).resolve() )] + testargs execute_subprocess_async(_UpperCAmelCase , env=self.get_env() ) lowerCAmelCase__ : Optional[Any] = os.path.join(_UpperCAmelCase , '''metrics.json''' ) with open(_UpperCAmelCase ) as f: lowerCAmelCase__ : Dict = json.load(_UpperCAmelCase ) return result @require_torch_gpu def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Optional[int] = self._run_finetune(gpus=1 ) self.assertGreaterEqual(result['''test'''][0]['''test_avg_em'''] , 0.2 ) @require_torch_multi_gpu def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Optional[int] = self._run_finetune(gpus=2 ) self.assertGreaterEqual(result['''test'''][0]['''test_avg_em'''] , 0.2 ) @require_torch_gpu @require_ray def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Dict = self._run_finetune(gpus=1 , distributed_retriever='''ray''' ) self.assertGreaterEqual(result['''test'''][0]['''test_avg_em'''] , 0.2 ) @require_torch_multi_gpu @require_ray def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Dict = self._run_finetune(gpus=1 , distributed_retriever='''ray''' ) self.assertGreaterEqual(result['''test'''][0]['''test_avg_em'''] , 0.2 )
233
"""simple docstring""" import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset A = random.Random() def __A ( a_ :Tuple , a_ :Dict=1.0 , a_ :str=None , a_ :List[Any]=None) -> Dict: if rng is None: __a : Any = global_rng __a : Tuple = [] for batch_idx in range(shape[0]): values.append([]) for _ in range(shape[1]): values[-1].append(rng.random() * scale) return values class __lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=400 , _UpperCAmelCase=2000 , _UpperCAmelCase=2048 , _UpperCAmelCase=128 , _UpperCAmelCase=1 , _UpperCAmelCase=512 , _UpperCAmelCase=30 , _UpperCAmelCase=44100 , ): __a : Any = parent __a : Tuple = batch_size __a : Tuple = min_seq_length __a : List[str] = max_seq_length __a : List[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __a : Tuple = spectrogram_length __a : int = feature_size __a : int = num_audio_channels __a : Tuple = hop_length __a : List[Any] = chunk_length __a : Any = sampling_rate def _lowerCamelCase ( self ): return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def _lowerCamelCase ( self , _UpperCAmelCase=False , _UpperCAmelCase=False ): def _flatten(_UpperCAmelCase ): return list(itertools.chain(*_UpperCAmelCase ) ) if equal_length: __a : Tuple = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __a : Tuple = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __a : Optional[Any] = [np.asarray(_UpperCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __lowercase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = TvltFeatureExtractor def _lowerCamelCase ( self ): __a : Optional[Any] = TvltFeatureExtractionTester(self ) def _lowerCamelCase ( self ): __a : int = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_UpperCAmelCase , '''spectrogram_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''feature_size''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''num_audio_channels''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''hop_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''chunk_length''' ) ) self.assertTrue(hasattr(_UpperCAmelCase , '''sampling_rate''' ) ) def _lowerCamelCase ( self ): __a : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : List[str] = feat_extract_first.save_pretrained(_UpperCAmelCase )[0] check_json_file_has_correct_format(_UpperCAmelCase ) __a : Union[str, Any] = self.feature_extraction_class.from_pretrained(_UpperCAmelCase ) __a : Tuple = feat_extract_first.to_dict() __a : List[Any] = feat_extract_second.to_dict() __a : int = dict_first.pop('''mel_filters''' ) __a : List[Any] = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): __a : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a : int = os.path.join(_UpperCAmelCase , '''feat_extract.json''' ) feat_extract_first.to_json_file(_UpperCAmelCase ) __a : Optional[Any] = self.feature_extraction_class.from_json_file(_UpperCAmelCase ) __a : Optional[Any] = feat_extract_first.to_dict() __a : Any = feat_extract_second.to_dict() __a : Optional[Any] = dict_first.pop('''mel_filters''' ) __a : Dict = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_UpperCAmelCase , _UpperCAmelCase ) ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowerCamelCase ( self ): # Initialize feature_extractor __a : str = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 __a : Any = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __a : Union[str, Any] = [np.asarray(_UpperCAmelCase ) for speech_input in speech_inputs] # Test not batched input __a : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched __a : int = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking __a : List[Any] = feature_extractor( _UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 , mask_audio=_UpperCAmelCase ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. __a : str = [floats_list((1, x) )[0] for x in (800, 800, 800)] __a : Any = np.asarray(_UpperCAmelCase ) __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''np''' , sampling_rate=44100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def _lowerCamelCase ( self , _UpperCAmelCase ): __a : int = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech __a : int = ds.sort('''id''' ).select(range(_UpperCAmelCase ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowerCamelCase ( self ): __a : List[str] = self._load_datasamples(1 ) __a : Tuple = TvltFeatureExtractor() __a : Optional[Any] = feature_extractor(_UpperCAmelCase , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) __a : Dict = torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _UpperCAmelCase , atol=1e-4 ) )
52
0
"""simple docstring""" import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class snake_case__ ( unittest.TestCase ): @slow def __lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase : Optional[Any] = FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small" ) UpperCAmelCase : List[Any] = AutoTokenizer.from_pretrained("google/mt5-small" ) UpperCAmelCase : List[str] = tokenizer("Hello there" , return_tensors="np" ).input_ids UpperCAmelCase : List[str] = tokenizer("Hi I am" , return_tensors="np" ).input_ids UpperCAmelCase : Union[str, Any] = shift_tokens_right(_UpperCAmelCase , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCAmelCase : List[Any] = model(_UpperCAmelCase , decoder_input_ids=_UpperCAmelCase ).logits UpperCAmelCase : int = optax.softmax_cross_entropy(_UpperCAmelCase , onehot(_UpperCAmelCase , logits.shape[-1] ) ).mean() UpperCAmelCase : List[Any] = -(labels.shape[-1] * loss.item()) UpperCAmelCase : List[str] = -84.91_27 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
595
"""simple docstring""" from __future__ import annotations class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): __a , __a : List[Any] = text, pattern __a , __a : Tuple = len(_UpperCAmelCase ), len(_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def _lowerCamelCase ( self , _UpperCAmelCase ): for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def _lowerCamelCase ( self ): # searches pattern in text and returns index positions __a : Dict = [] for i in range(self.textLen - self.patLen + 1 ): __a : List[str] = self.mismatch_in_text(_UpperCAmelCase ) if mismatch_index == -1: positions.append(_UpperCAmelCase ) else: __a : Tuple = self.match_in_pattern(self.text[mismatch_index] ) __a : Optional[int] = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions A = '''ABAABA''' A = '''AB''' A = BoyerMooreSearch(text, pattern) A = bms.bad_character_heuristic() if len(positions) == 0: print('''No match found''') else: print('''Pattern found in following positions: ''') print(positions)
52
0
'''simple docstring''' import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowerCamelCase ( _UpperCamelCase ): snake_case_ = (KDPMaDiscreteScheduler,) snake_case_ = 10 def _lowerCamelCase ( self , **a_ ): lowerCAmelCase : Dict = { '''num_train_timesteps''': 1_100, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**_UpperCAmelCase ) return config def _lowerCamelCase ( self ): for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCAmelCase ) def _lowerCamelCase ( self ): for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_UpperCAmelCase , beta_end=_UpperCAmelCase ) def _lowerCamelCase ( self ): for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCAmelCase ) def _lowerCamelCase ( self ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCAmelCase ) def _lowerCamelCase ( self ): lowerCAmelCase : List[Any] = self.scheduler_classes[0] lowerCAmelCase : Union[str, Any] = self.get_scheduler_config(prediction_type="v_prediction" ) lowerCAmelCase : str = scheduler_class(**_UpperCAmelCase ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase : Optional[int] = self.dummy_model() lowerCAmelCase : Any = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase : Tuple = sample.to(_UpperCAmelCase ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase : Tuple = scheduler.scale_model_input(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : int = model(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : List[Any] = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : str = output.prev_sample lowerCAmelCase : Union[str, Any] = torch.sum(torch.abs(_UpperCAmelCase ) ) lowerCAmelCase : Any = torch.mean(torch.abs(_UpperCAmelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_9_3_4e-0_7 ) < 1e-2 assert abs(result_mean.item() - 6.1_1_1_2e-1_0 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_9_3_4_2_8_6_5_0_1_7_0_9_7_2e-0_7 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def _lowerCamelCase ( self ): if torch_device == "mps": return lowerCAmelCase : Tuple = self.scheduler_classes[0] lowerCAmelCase : Union[str, Any] = self.get_scheduler_config() lowerCAmelCase : Optional[Any] = scheduler_class(**_UpperCAmelCase ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase : int = self.dummy_model() lowerCAmelCase : Union[str, Any] = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase : str = sample.to(_UpperCAmelCase ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase : Union[str, Any] = scheduler.scale_model_input(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : str = model(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : Optional[int] = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : Tuple = output.prev_sample lowerCAmelCase : str = torch.sum(torch.abs(_UpperCAmelCase ) ) lowerCAmelCase : int = torch.mean(torch.abs(_UpperCAmelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4_125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4_125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def _lowerCamelCase ( self ): if torch_device == "mps": return lowerCAmelCase : Optional[int] = self.scheduler_classes[0] lowerCAmelCase : Optional[int] = self.get_scheduler_config() lowerCAmelCase : int = scheduler_class(**_UpperCAmelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCAmelCase ) lowerCAmelCase : List[Any] = self.dummy_model() lowerCAmelCase : Any = self.dummy_sample_deter.to(_UpperCAmelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCAmelCase : Optional[int] = scheduler.scale_model_input(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : int = model(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : Optional[Any] = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : int = output.prev_sample lowerCAmelCase : Optional[int] = torch.sum(torch.abs(_UpperCAmelCase ) ) lowerCAmelCase : Union[str, Any] = torch.mean(torch.abs(_UpperCAmelCase ) ) if str(_UpperCAmelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4_125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4_125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
525
"""simple docstring""" import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": A = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') A = F'https://www.google.com/search?q={query}&num=100' A = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: A = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: A = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
0
'''simple docstring''' import numpy as np from transformers import Pipeline def lowerCamelCase__ ( A_ ): UpperCAmelCase_ = np.max(a_ , axis=-1 , keepdims=a_ ) UpperCAmelCase_ = np.exp(outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=a_ ) class lowercase_ ( _UpperCamelCase ): def lowerCamelCase_ ( self , **UpperCamelCase__ ) -> Optional[int]: """simple docstring""" UpperCAmelCase_ = {} if "second_text" in kwargs: UpperCAmelCase_ = kwargs['''second_text'''] return preprocess_kwargs, {}, {} def lowerCamelCase_ ( self , UpperCamelCase__ , UpperCamelCase__=None ) -> str: """simple docstring""" return self.tokenizer(_UpperCAmelCase , text_pair=_UpperCAmelCase , return_tensors=self.framework ) def lowerCamelCase_ ( self , UpperCamelCase__ ) -> str: """simple docstring""" return self.model(**_UpperCAmelCase ) def lowerCamelCase_ ( self , UpperCamelCase__ ) -> Tuple: """simple docstring""" UpperCAmelCase_ = model_outputs.logits[0].numpy() UpperCAmelCase_ = softmax(_UpperCAmelCase ) UpperCAmelCase_ = np.argmax(_UpperCAmelCase ) UpperCAmelCase_ = self.model.config.idalabel[best_class] UpperCAmelCase_ = probabilities[best_class].item() UpperCAmelCase_ = logits.tolist() return {"label": label, "score": score, "logits": logits}
660
"""simple docstring""" import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = 0 __lowerCAmelCase = False __lowerCAmelCase = 3.0 class __lowercase ( unittest.TestCase ): '''simple docstring''' def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_UpperCAmelCase ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.2_5 ).to_kwargs() , {'''a''': 2, '''c''': 2.2_5} ) @require_cuda def _lowerCamelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a : List[Any] = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() __a : int = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a : Optional[Any] = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_0_2_4.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _UpperCAmelCase ) @require_multi_gpu def _lowerCamelCase ( self ): __a : Dict = ['''torchrun''', f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] execute_subprocess_async(_UpperCAmelCase , env=os.environ.copy() ) if __name__ == "__main__": A = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) A = Accelerator(kwargs_handlers=[ddp_scaler]) A = torch.nn.Linear(100, 200) A = accelerator.prepare(model) # Check the values changed in kwargs A = '''''' A = model.bucket_bytes_cap // (1_024 * 1_024) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
52
0
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets __magic_name__ = '''\ @inproceedings{snover-etal-2006-study, title = "A Study of Translation Edit Rate with Targeted Human Annotation", author = "Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John", booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers", month = aug # " 8-12", year = "2006", address = "Cambridge, Massachusetts, USA", publisher = "Association for Machine Translation in the Americas", url = "https://aclanthology.org/2006.amta-papers.25", pages = "223--231", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' __magic_name__ = '''\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. ''' __magic_name__ = ''' Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: \'score\' (float): TER score (num_edits / sum_ref_lengths * 100) \'num_edits\' (int): The cumulative number of edits \'ref_length\' (float): The cumulative average reference length Examples: Example 1: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0} Example 2: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0} Example 3: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5} Example 4: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0} Example 5: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class lowerCAmelCase__ ( datasets.Metric ): """simple docstring""" def _UpperCamelCase ( self ): if version.parse(scb.__version__ ) < version.parse("1.4.12" ): raise ImportWarning( "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n" "You can install it with `pip install \"sacrebleu>=1.4.12\"`." ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="http://www.cs.umd.edu/~snover/tercom/" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/mjpost/sacreBLEU#ter"] , reference_urls=[ "https://github.com/jhclark/tercom", ] , ) def _UpperCamelCase ( self , a_ , a_ , a_ = False , a_ = False , a_ = False , a_ = False , ): lowerCamelCase_ : str = len(references[0] ) if any(len(_UpperCAmelCase ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) lowerCamelCase_ : Tuple = [[refs[i] for refs in references] for i in range(_UpperCAmelCase )] lowerCamelCase_ : Optional[int] = TER( normalized=_UpperCAmelCase , no_punct=_UpperCAmelCase , asian_support=_UpperCAmelCase , case_sensitive=_UpperCAmelCase , ) lowerCamelCase_ : str = sb_ter.corpus_score(_UpperCAmelCase , _UpperCAmelCase ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
250
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
0
from __future__ import annotations A_: List[str] = list[list[int]] # assigning initial values to the grid A_: List[Any] = [ [3, 0, 6, 5, 0, 8, 4, 0, 0], [5, 2, 0, 0, 0, 0, 0, 0, 0], [0, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] # a grid with no solution A_: Any = [ [5, 0, 6, 5, 0, 8, 4, 0, 3], [5, 2, 0, 0, 0, 0, 0, 0, 2], [1, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] def __lowerCAmelCase ( _A ,_A ,_A ,_A ): """simple docstring""" for i in range(9 ): if grid[row][i] == n or grid[i][column] == n: return False for i in range(3 ): for j in range(3 ): if grid[(row - row % 3) + i][(column - column % 3) + j] == n: return False return True def __lowerCAmelCase ( _A ): """simple docstring""" for i in range(9 ): for j in range(9 ): if grid[i][j] == 0: return i, j return None def __lowerCAmelCase ( _A ): """simple docstring""" if location := find_empty_location(a_ ): _lowercase = location else: # If the location is ``None``, then the grid is solved. return grid for digit in range(1 ,10 ): if is_safe(a_ ,a_ ,a_ ,a_ ): _lowercase = digit if sudoku(a_ ) is not None: return grid _lowercase = 0 return None def __lowerCAmelCase ( _A ): """simple docstring""" for row in grid: for cell in row: print(a_ ,end=""" """ ) print() if __name__ == "__main__": # make a copy of grid so that you can compare with the unmodified grid for example_grid in (initial_grid, no_solution): print('\nExample grid:\n' + '=' * 20) print_solution(example_grid) print('\nExample grid solution:') A_: Optional[Any] = sudoku(example_grid) if solution is not None: print_solution(solution) else: print('Cannot find a solution.')
398
"""simple docstring""" import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger A = get_logger(__name__) A = Path(__file__).parent / '''model_card_template.md''' A = uuida().hex A = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES A = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES A = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def __A ( a_ :Union[Dict, str, None] = None) -> str: __a : Union[str, Any] = F"""diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}""" if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F"""; torch/{_torch_version}""" if is_flax_available(): ua += F"""; jax/{_jax_version}""" ua += F"""; flax/{_flax_version}""" if is_onnx_available(): ua += F"""; onnxruntime/{_onnxruntime_version}""" # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''').upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(a_ , a_): ua += "; " + "; ".join(F"""{k}/{v}""" for k, v in user_agent.items()) elif isinstance(a_ , a_): ua += "; " + user_agent return ua def __A ( a_ :str , a_ :Optional[str] = None , a_ :Optional[str] = None) -> Optional[int]: if token is None: __a : Any = HfFolder.get_token() if organization is None: __a : List[Any] = whoami(a_)['''name'''] return F"""{username}/{model_id}""" else: return F"""{organization}/{model_id}""" def __A ( a_ :Union[str, Any] , a_ :List[str]) -> Optional[Any]: if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''') if hasattr(a_ , '''local_rank''') and args.local_rank not in [-1, 0]: return __a : int = args.hub_token if hasattr(a_ , '''hub_token''') else None __a : Any = get_full_repo_name(a_ , token=a_) __a : Tuple = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=a_ , model_name=a_ , repo_name=a_ , dataset_name=args.dataset_name if hasattr(a_ , '''dataset_name''') else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(a_ , '''gradient_accumulation_steps''') else None ) , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta1''') else None , adam_betaa=args.adam_betaa if hasattr(a_ , '''adam_beta2''') else None , adam_weight_decay=args.adam_weight_decay if hasattr(a_ , '''adam_weight_decay''') else None , adam_epsilon=args.adam_epsilon if hasattr(a_ , '''adam_epsilon''') else None , lr_scheduler=args.lr_scheduler if hasattr(a_ , '''lr_scheduler''') else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(a_ , '''lr_warmup_steps''') else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(a_ , '''ema_inv_gamma''') else None , ema_power=args.ema_power if hasattr(a_ , '''ema_power''') else None , ema_max_decay=args.ema_max_decay if hasattr(a_ , '''ema_max_decay''') else None , mixed_precision=args.mixed_precision , ) __a : List[Any] = os.path.join(args.output_dir , '''README.md''') model_card.save(a_) def __A ( a_ :Optional[str] , a_ :Optional[str] = None) -> Union[str, Any]: if resolved_file is None or commit_hash is not None: return commit_hash __a : Any = str(Path(a_).as_posix()) __a : Optional[int] = re.search(R'''snapshots/([^/]+)/''' , a_) if search is None: return None __a : Dict = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(a_) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. A = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) A = os.path.join(hf_cache_home, '''diffusers''') def __A ( a_ :Optional[str] = None , a_ :Optional[str] = None) -> None: if new_cache_dir is None: __a : Dict = DIFFUSERS_CACHE if old_cache_dir is None: __a : List[Any] = old_diffusers_cache __a : Union[str, Any] = Path(a_).expanduser() __a : Dict = Path(a_).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*'''): if old_blob_path.is_file() and not old_blob_path.is_symlink(): __a : List[Any] = new_cache_dir / old_blob_path.relative_to(a_) new_blob_path.parent.mkdir(parents=a_ , exist_ok=a_) os.replace(a_ , a_) try: os.symlink(a_ , a_) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''') # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). A = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): A = 0 else: with open(cache_version_file) as f: try: A = int(f.read()) except ValueError: A = 0 if cache_version < 1: A = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: A = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F'There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F'There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ' '''the directory exists and can be written to.''' ) def __A ( a_ :str , a_ :Optional[str] = None) -> str: if variant is not None: __a : Dict = weights_name.split('''.''') __a : List[Any] = splits[:-1] + [variant] + splits[-1:] __a : Tuple = '''.'''.join(a_) return weights_name def __A ( a_ :List[Any] , *, a_ :Union[str, Any] , a_ :Dict , a_ :Union[str, Any] , a_ :Optional[int] , a_ :str , a_ :Any , a_ :str , a_ :Optional[int] , a_ :str , a_ :Tuple , a_ :List[str]=None , ) -> Dict: __a : int = str(a_) if os.path.isfile(a_): return pretrained_model_name_or_path elif os.path.isdir(a_): if os.path.isfile(os.path.join(a_ , a_)): # Load from a PyTorch checkpoint __a : Union[str, Any] = os.path.join(a_ , a_) return model_file elif subfolder is not None and os.path.isfile( os.path.join(a_ , a_ , a_)): __a : Optional[Any] = os.path.join(a_ , a_ , a_) return model_file else: raise EnvironmentError( F"""Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.""") else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(a_).base_version) >= version.parse('''0.20.0''') ): try: __a : Any = hf_hub_download( a_ , filename=_add_variant(a_ , a_) , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) warnings.warn( F"""Loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'` is deprecated. Loading instead from `revision='main'` with `variant={revision}`. Loading model variants via `revision='{revision}'` will be removed in diffusers v1. Please use `variant='{revision}'` instead.""" , a_ , ) return model_file except: # noqa: E722 warnings.warn( F"""You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision='{revision}'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='{revision}'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(a_ , a_)} file in the 'main' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title '{pretrained_model_name_or_path} is missing {_add_variant(a_ , a_)}' so that the correct variant file can be added.""" , a_ , ) try: # 2. Load model file as usual __a : Optional[Any] = hf_hub_download( a_ , filename=a_ , cache_dir=a_ , force_download=a_ , proxies=a_ , resume_download=a_ , local_files_only=a_ , use_auth_token=a_ , user_agent=a_ , subfolder=a_ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier """ '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''') except RevisionNotFoundError: raise EnvironmentError( F"""{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for """ '''this model name. Check the model page at ''' F"""'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions.""") except EntryNotFoundError: raise EnvironmentError( F"""{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.""") except HTTPError as err: raise EnvironmentError( F"""There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}""") except ValueError: raise EnvironmentError( F"""We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it""" F""" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a""" F""" directory containing a file named {weights_name} or""" ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''') except EnvironmentError: raise EnvironmentError( F"""Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from """ '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F"""Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory """ F"""containing a file named {weights_name}""")
52
0
'''simple docstring''' from itertools import permutations def __lowerCAmelCase ( UpperCamelCase__ ) -> bool: if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False __lowerCamelCase = [7, 11, 13, 17] for i, test in enumerate(a_ ): if (num[i + 4] * 1_00 + num[i + 5] * 10 + num[i + 6]) % test != 0: return False return True def __lowerCAmelCase ( UpperCamelCase__ = 10 ) -> int: return sum( int(''''''.join(map(a_ , a_ ) ) ) for num in permutations(range(a_ ) ) if is_substring_divisible(a_ ) ) if __name__ == "__main__": print(f'{solution() = }')
546
"""simple docstring""" import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A = logging.get_logger(__name__) A = { '''kakaobrain/align-base''': '''https://huggingface.co/kakaobrain/align-base/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_text_model''' def __init__( self , _UpperCAmelCase=30522 , _UpperCAmelCase=768 , _UpperCAmelCase=12 , _UpperCAmelCase=12 , _UpperCAmelCase=3072 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0 , _UpperCAmelCase="absolute" , _UpperCAmelCase=True , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : int = vocab_size __a : Optional[int] = hidden_size __a : Dict = num_hidden_layers __a : List[Any] = num_attention_heads __a : Optional[int] = hidden_act __a : List[Any] = intermediate_size __a : List[Any] = hidden_dropout_prob __a : List[str] = attention_probs_dropout_prob __a : Optional[int] = max_position_embeddings __a : List[str] = type_vocab_size __a : Tuple = initializer_range __a : Dict = layer_norm_eps __a : Any = position_embedding_type __a : Dict = use_cache __a : Dict = pad_token_id @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : List[str] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the text config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Dict = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align_vision_model''' def __init__( self , _UpperCAmelCase = 3 , _UpperCAmelCase = 600 , _UpperCAmelCase = 2.0 , _UpperCAmelCase = 3.1 , _UpperCAmelCase = 8 , _UpperCAmelCase = [3, 3, 5, 3, 5, 5, 3] , _UpperCAmelCase = [32, 16, 24, 40, 80, 112, 192] , _UpperCAmelCase = [16, 24, 40, 80, 112, 192, 320] , _UpperCAmelCase = [] , _UpperCAmelCase = [1, 2, 2, 2, 1, 2, 1] , _UpperCAmelCase = [1, 2, 2, 3, 3, 4, 1] , _UpperCAmelCase = [1, 6, 6, 6, 6, 6, 6] , _UpperCAmelCase = 0.2_5 , _UpperCAmelCase = "swish" , _UpperCAmelCase = 2560 , _UpperCAmelCase = "mean" , _UpperCAmelCase = 0.0_2 , _UpperCAmelCase = 0.0_0_1 , _UpperCAmelCase = 0.9_9 , _UpperCAmelCase = 0.2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) __a : Tuple = num_channels __a : str = image_size __a : List[Any] = width_coefficient __a : Optional[int] = depth_coefficient __a : Union[str, Any] = depth_divisor __a : int = kernel_sizes __a : Dict = in_channels __a : List[str] = out_channels __a : Any = depthwise_padding __a : str = strides __a : Optional[Any] = num_block_repeats __a : Optional[Any] = expand_ratios __a : Any = squeeze_expansion_ratio __a : int = hidden_act __a : Union[str, Any] = hidden_dim __a : Union[str, Any] = pooling_type __a : Tuple = initializer_range __a : List[str] = batch_norm_eps __a : List[Any] = batch_norm_momentum __a : Union[str, Any] = drop_connect_rate __a : List[Any] = sum(_UpperCAmelCase ) * 4 @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , **_UpperCAmelCase ): cls._set_token_in_kwargs(_UpperCAmelCase ) __a , __a : Optional[Any] = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the vision config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __a : Optional[Any] = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(_UpperCAmelCase , **_UpperCAmelCase ) class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''align''' __lowerCAmelCase = True def __init__( self , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=640 , _UpperCAmelCase=1.0 , _UpperCAmelCase=0.0_2 , **_UpperCAmelCase , ): super().__init__(**_UpperCAmelCase ) if text_config is None: __a : Dict = {} logger.info('''text_config is None. Initializing the AlignTextConfig with default values.''' ) if vision_config is None: __a : Any = {} logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.''' ) __a : Any = AlignTextConfig(**_UpperCAmelCase ) __a : Any = AlignVisionConfig(**_UpperCAmelCase ) __a : Optional[int] = projection_dim __a : Union[str, Any] = temperature_init_value __a : int = initializer_range @classmethod def _lowerCamelCase ( cls , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_UpperCAmelCase ) def _lowerCamelCase ( self ): __a : Union[str, Any] = copy.deepcopy(self.__dict__ ) __a : Tuple = self.text_config.to_dict() __a : Union[str, Any] = self.vision_config.to_dict() __a : int = self.__class__.model_type return output
52
0